1 | /*␊ |
2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * The contents of this file constitute Original Code as defined in and␊ |
7 | * are subject to the Apple Public Source License Version 2.0 (the␊ |
8 | * "License"). You may not use this file except in compliance with the␊ |
9 | * License. Please obtain a copy of the License at␊ |
10 | * http://www.apple.com/publicsource and read it before using this file.␊ |
11 | * ␊ |
12 | * This Original Code and all software distributed under the License are␊ |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
17 | * License for the specific language governing rights and limitations␊ |
18 | * under the License.␊ |
19 | * ␊ |
20 | * @APPLE_LICENSE_HEADER_END@␊ |
21 | */␊ |
22 | /*␊ |
23 | * Copyright (c) 1998 Robert Nordier␊ |
24 | * All rights reserved.␊ |
25 | *␊ |
26 | * Redistribution and use in source and binary forms, with or without␊ |
27 | * modification, are permitted provided that the following conditions␊ |
28 | * are met:␊ |
29 | * 1. Redistributions of source code must retain the above copyright␊ |
30 | * notice, this list of conditions and the following disclaimer.␊ |
31 | * 2. Redistributions in binary form must reproduce the above copyright␊ |
32 | * notice, this list of conditions and the following disclaimer in␊ |
33 | * the documentation and/or other materials provided with the␊ |
34 | * distribution.␊ |
35 | *␊ |
36 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS␊ |
37 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED␊ |
38 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE␊ |
39 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY␊ |
40 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL␊ |
41 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE␊ |
42 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS␊ |
43 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER␊ |
44 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR␊ |
45 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN␊ |
46 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.␊ |
47 | */␊ |
48 | ␊ |
49 | #include "libsaio.h"␊ |
50 | #include "sl.h"␊ |
51 | #include "msdos_private.h"␊ |
52 | #include "msdos.h"␊ |
53 | ␊ |
54 | #define LABEL_LENGTH␉␉11␊ |
55 | #define MSDOS_CACHE_BLOCKSIZE BPS␊ |
56 | ␊ |
57 | #define␉CLUST_FIRST␉␉2/* reserved cluster range */␊ |
58 | #define␉CLUST_RSRVD32␉0x0ffffff8␉/* reserved cluster range */␊ |
59 | #define␉CLUST_RSRVD16␉0xfff8␉/* reserved cluster range */␊ |
60 | #define␉CLUST_RSRVD12␉0xff8␉/* reserved cluster range */␊ |
61 | ␊ |
62 | #define tolower(c) (((c)>='A' && c<='Z')?((c) | 0x20):(c))␊ |
63 | ␊ |
64 | static int msdosressector=0;␊ |
65 | static int msdosnfats = 0;␊ |
66 | static int msdosfatsecs = 0;␊ |
67 | static int msdosbps = 0;␊ |
68 | static int msdosclustersize = 0;␊ |
69 | static int msdosrootDirSectors = 0;␊ |
70 | static CICell msdoscurrent = 0;␊ |
71 | static int msdosrootcluster = 0;␊ |
72 | static int msdosfatbits = 0;␊ |
73 | ␊ |
74 | #if UNUSED␊ |
75 | /*␊ |
76 | * Check a volume label.␊ |
77 | */␊ |
78 | static int␊ |
79 | oklabel(const char *src)␊ |
80 | {␊ |
81 | int c, i;␊ |
82 | ␊ |
83 | for (i = 0, c = 0; i <= 11; i++) {␊ |
84 | c = (u_char)*src++;␊ |
85 | if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))␊ |
86 | break;␊ |
87 | }␊ |
88 | return i && !c;␊ |
89 | }␊ |
90 | #endif /* UNUSED */␊ |
91 | ␊ |
92 | void MSDOSFree(CICell ih)␊ |
93 | {␊ |
94 | ␉if(msdoscurrent == ih)␊ |
95 | msdoscurrent = 0;␊ |
96 | free(ih);␊ |
97 | }␊ |
98 | ␊ |
99 | int MSDOSProbe(const void * buffer)␊ |
100 | {␊ |
101 | union bootsector *bsp;␊ |
102 | struct bpb33 *b33;␊ |
103 | struct bpb50 *b50;␊ |
104 | struct bpb710 *b710;␊ |
105 | u_int16_t␉ bps;␊ |
106 | u_int8_t␉␉spc;␊ |
107 | ␉␊ |
108 | bsp = (union bootsector *)buffer;␊ |
109 | b33 = (struct bpb33 *)bsp->bs33.bsBPB;␊ |
110 | b50 = (struct bpb50 *)bsp->bs50.bsBPB;␊ |
111 | b710 = (struct bpb710 *)bsp->bs710.bsBPB;␊ |
112 | ␊ |
113 | /* We only work with 512, 1024, and 2048 byte sectors */␊ |
114 | bps = OSSwapLittleToHostInt16(b33->bpbBytesPerSec);␊ |
115 | if ((bps < 0x200) || (bps & (bps - 1)) || (bps > 0x800)) ␊ |
116 | return 0;␊ |
117 | ␊ |
118 | ␉/* Check to make sure valid sectors per cluster */␊ |
119 | spc = b33->bpbSecPerClust;␊ |
120 | if ((spc == 0 ) || (spc & (spc - 1))) ␊ |
121 | return 0;␉␊ |
122 | ␉␊ |
123 | ␉if (OSSwapLittleToHostInt16(b50->bpbRootDirEnts) == 0) { /* It's FAT32 */␊ |
124 | ␉␉if (!memcmp(((struct extboot *)bsp->bs710.bsExt)->exFileSysType, "FAT32 ", 8))␊ |
125 | ␉␉␉return 32;␊ |
126 | ␉}␊ |
127 | ␉else if (((struct extboot *)bsp->bs50.bsExt)->exBootSignature == EXBOOTSIG) {␊ |
128 | ␉␉if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT16 ", 8))␊ |
129 | ␉␉␉return 16;␊ |
130 | ␉␉if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT12 ", 8))␊ |
131 | ␉␉␉return 12;␊ |
132 | ␉}␉␊ |
133 | ␉␉␊ |
134 | ␉return 0;␊ |
135 | }␊ |
136 | ␊ |
137 | ␊ |
138 | long␊ |
139 | MSDOSInitPartition (CICell ih)␊ |
140 | {␊ |
141 | union bootsector *bsp;␊ |
142 | struct bpb33 *b33;␊ |
143 | struct bpb50 *b50;␊ |
144 | struct bpb710 *b710;␊ |
145 | u_int8_t␉␉spc;␊ |
146 | char *buf;␊ |
147 | ␉␊ |
148 | ␉if (msdoscurrent == ih)␊ |
149 | ␉{␊ |
150 | ␉␉CacheInit(ih, MSDOS_CACHE_BLOCKSIZE);␊ |
151 | ␉␉return 0;␊ |
152 | ␉}␊ |
153 | ␉␊ |
154 | ␉buf=malloc (512);␊ |
155 | ␉if (!buf) ␊ |
156 | ␉{ ␊ |
157 | ␉␉return -1;␊ |
158 | ␉}␊ |
159 | ␊ |
160 | ␉/*␊ |
161 | ␉ * Read the boot sector of the filesystem, and then check the␊ |
162 | ␉ * boot signature. If not a dos boot sector then error out.␊ |
163 | ␉ *␊ |
164 | ␉ * NOTE: 2048 is a maximum sector size in current...␊ |
165 | ␉ */␊ |
166 | ␊ |
167 | ␉Seek(ih, 0);␊ |
168 | ␉Read(ih, (long)buf, 512);␊ |
169 | ␉␊ |
170 | bsp = (union bootsector *)buf;␊ |
171 | b33 = (struct bpb33 *)bsp->bs33.bsBPB;␊ |
172 | b50 = (struct bpb50 *)bsp->bs50.bsBPB;␊ |
173 | b710 = (struct bpb710 *)bsp->bs710.bsBPB;␊ |
174 | ␊ |
175 | ␉␊ |
176 | ␉/* We only work with 512, 1024, and 2048 byte sectors */␊ |
177 | ␉msdosbps = OSSwapLittleToHostInt16(b33->bpbBytesPerSec);␊ |
178 | ␉if ((msdosbps < 0x200) || (msdosbps & (msdosbps - 1)) || (msdosbps > 0x800)) ␊ |
179 | ␉{␊ |
180 | ␉␉free (buf);␊ |
181 | ␉␉return -1;␊ |
182 | ␉}␊ |
183 | ␉␊ |
184 | ␉/* Check to make sure valid sectors per cluster */␊ |
185 | ␉spc = b33->bpbSecPerClust;␊ |
186 | ␉if ((spc == 0 ) || (spc & (spc - 1)))␊ |
187 | ␉{␊ |
188 | ␉␉free (buf);␊ |
189 | ␉␉return -1;␊ |
190 | ␉}␊ |
191 | ␉if (OSSwapLittleToHostInt16(b50->bpbRootDirEnts) == 0)␊ |
192 | ␉{␊ |
193 | ␉␉/* It's FAT32 */␊ |
194 | ␉␉if (memcmp(((struct extboot *)bsp->bs710.bsExt)->exFileSysType, "FAT32 ", 8))␊ |
195 | ␉␉{␊ |
196 | ␉␉␉free (buf);␊ |
197 | ␉␉␉return -1;␊ |
198 | ␉␉}␊ |
199 | ␉␉msdosressector = OSSwapLittleToHostInt16(b710->bpbResSectors);␊ |
200 | ␉␉msdosnfats = b710->bpbFATs;␊ |
201 | ␉␉msdosfatsecs = OSSwapLittleToHostInt16(b710->bpbBigFATsecs);␊ |
202 | ␉␉msdosrootcluster = OSSwapLittleToHostInt32(b710->bpbRootClust);␊ |
203 | ␉␉msdosrootDirSectors = 0;␊ |
204 | ␉␉msdosfatbits = 32;␊ |
205 | ␉}␊ |
206 | ␉else if (((struct extboot *)bsp->bs50.bsExt)->exBootSignature == EXBOOTSIG) {␊ |
207 | ␉␉if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT16 ", 8))␊ |
208 | ␉␉␉msdosfatbits = 16;␊ |
209 | ␉␉else if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT12 ", 8))␊ |
210 | ␉␉␉msdosfatbits = 12;␊ |
211 | ␉␉else␊ |
212 | ␉␉{␊ |
213 | ␉␉␉free (buf);␊ |
214 | ␉␉␉return -1;␊ |
215 | ␉␉}␉␉␊ |
216 | ␉␉␊ |
217 | ␉␉msdosressector = OSSwapLittleToHostInt16(b33->bpbResSectors);␊ |
218 | ␉␉msdosnfats = b33->bpbFATs;␊ |
219 | ␉␉msdosfatsecs = OSSwapLittleToHostInt16(b33->bpbFATsecs);␊ |
220 | ␉␉msdosrootcluster = 0;␉␉␊ |
221 | ␉␉msdosrootDirSectors = ((OSSwapLittleToHostInt16(b50->bpbRootDirEnts) * sizeof(struct direntry)) +␊ |
222 | ␉␉␉␉␉␉␉ (msdosbps-1)) / msdosbps;␊ |
223 | ␉} else {␊ |
224 | ␉␉free (buf);␊ |
225 | ␉␉return -1;␊ |
226 | ␉}␊ |
227 | ␉␊ |
228 | ␉msdosclustersize = msdosbps * spc;␊ |
229 | ␉msdoscurrent = ih;␊ |
230 | ␊ |
231 | ␉CacheInit(ih, MSDOS_CACHE_BLOCKSIZE);␊ |
232 | ␉free (buf);␊ |
233 | ␉return 0;␊ |
234 | }␊ |
235 | ␊ |
236 | static int␊ |
237 | readSector(CICell ih, off_t readOffset, char *buf, int size)␊ |
238 | {␊ |
239 | // Caching only FAT entries (4 bytes) by utlizing the cache with sector aligned read requests.␊ |
240 | ␉if (size < BPS)␊ |
241 | ␉{␊ |
242 | ␉␉long long sectorOffset = (uint64_t)readOffset / BPS * BPS;␊ |
243 | ␉␉long relOffset = readOffset % BPS;␊ |
244 | ␉␉char *cacheBuffer;␊ |
245 | ␉␊ |
246 | ␉␉cacheBuffer = malloc(MSDOS_CACHE_BLOCKSIZE);␊ |
247 | ␉␉if (!cacheBuffer) ␊ |
248 | ␉␉{␊ |
249 | ␉␉␉return -1;␊ |
250 | ␉␉}␊ |
251 | ␉␉CacheRead(ih, cacheBuffer, sectorOffset, MSDOS_CACHE_BLOCKSIZE, true);␊ |
252 | ␉␉bcopy(cacheBuffer + relOffset, buf, size);␊ |
253 | ␉␉free(cacheBuffer);␊ |
254 | ␉}␊ |
255 | ␉else␊ |
256 | ␉{␊ |
257 | ␉␉Seek(ih, readOffset);␊ |
258 | ␉␉Read(ih, (long)buf, size);␊ |
259 | ␉}␊ |
260 | ␊ |
261 | ␉return 0;␊ |
262 | }␊ |
263 | ␊ |
264 | static int␊ |
265 | msdosreadcluster (CICell ih, uint8_t *buf, int size, off_t *cluster)␊ |
266 | {␊ |
267 | off_t readOffset;␊ |
268 | ␉char tmpbuf[8];␊ |
269 | ␉off_t clusn;␊ |
270 | ␉␊ |
271 | ␉switch (msdosfatbits) {␊ |
272 | ␉␉case 32:␊ |
273 | ␉␉␉if (*cluster < CLUST_FIRST ||*cluster >= CLUST_RSRVD32)␊ |
274 | ␉␉␉␉return 0;␊ |
275 | ␉␉␉clusn = *cluster - CLUST_FIRST;␊ |
276 | ␉␉␉break;␊ |
277 | ␉␉case 16:␊ |
278 | ␉␉␉if (*cluster < CLUST_FIRST ||*cluster >= CLUST_RSRVD16)␊ |
279 | ␉␉␉␉return 0;␊ |
280 | ␉␉␉clusn = *cluster - CLUST_FIRST;␊ |
281 | ␉␉␉break;␉␉␉␊ |
282 | ␉␉case 12:␊ |
283 | ␉␉␉if (*cluster < CLUST_FIRST ||*cluster >= CLUST_RSRVD12)␊ |
284 | ␉␉␉␉return 0;␊ |
285 | ␉␉␉clusn = *cluster - CLUST_FIRST;␊ |
286 | ␉␉␉break;␊ |
287 | ␉␉default:␊ |
288 | ␉␉␉return 0;␊ |
289 | ␉}␊ |
290 | ␉␊ |
291 | ␉/* Find sector where clusters start */␊ |
292 | ␉readOffset = (msdosressector +␊ |
293 | ␉␉␉␉ (msdosnfats * msdosfatsecs)+msdosrootDirSectors)*msdosbps;␊ |
294 | ␉/* Find sector where "cluster" starts */␊ |
295 | ␉readOffset += clusn * msdosclustersize;␊ |
296 | ␉␊ |
297 | ␉/* Read in "cluster" */␊ |
298 | ␉if (buf)␊ |
299 | ␉{␊ |
300 | ␉␉Seek(ih, readOffset);␊ |
301 | ␉␉Read(ih, (long)buf, size);␊ |
302 | ␉}␊ |
303 | ␊ |
304 | ␉/* Find first sector of FAT */␊ |
305 | ␉readOffset = msdosressector * msdosbps;␊ |
306 | ␊ |
307 | ␉/* Find sector containing "cluster" entry in FAT */␊ |
308 | ␉readOffset += ((uint64_t)*cluster * (uint64_t)msdosfatbits) / 8;␊ |
309 | ␉␊ |
310 | ␉/* Read one sector of the FAT */␊ |
311 | ␉if (readSector(ih, readOffset, tmpbuf, 4) != 0) return 0;␊ |
312 | ␉␊ |
313 | ␉switch (msdosfatbits) {␊ |
314 | ␉␉case 32:␊ |
315 | ␉␉␉*cluster = OSReadLittleInt32(tmpbuf, 0);␊ |
316 | ␉␉␉*cluster &= 0x0FFFFFFF;␉// ignore reserved upper bits␊ |
317 | ␉␉␉return 1;␊ |
318 | ␉␉case 16:␊ |
319 | ␉␉␉*cluster = OSReadLittleInt16(tmpbuf, 0);␊ |
320 | ␉␉␉return 1;␉␉␉␊ |
321 | ␉␉case 12:␊ |
322 | ␉␉␉*cluster = OSReadLittleInt16(tmpbuf, 0)>>(((uint64_t)*cluster * (uint64_t)msdosfatbits)%8);␊ |
323 | ␉␉␉*cluster &= 0xfff;␊ |
324 | ␉␉␉return 1;␊ |
325 | ␉␉default:␊ |
326 | ␉␉␉return 0;␊ |
327 | ␉}␊ |
328 | }␊ |
329 | ␊ |
330 | struct msdosdirstate␊ |
331 | {␊ |
332 | ␉struct direntry *buf;␊ |
333 | ␉uint8_t vfatchecksum;␊ |
334 | ␉int root16;␊ |
335 | ␉off_t cluster;␊ |
336 | ␉int nument;␊ |
337 | ␉int vfatnumber;␊ |
338 | };␊ |
339 | ␊ |
340 | static struct direntry *␊ |
341 | getnextdirent (CICell ih, uint16_t *longname, struct msdosdirstate *st)␊ |
342 | {␊ |
343 | ␉struct direntry *dirp;␊ |
344 | ␉while (1)␊ |
345 | ␉{␊ |
346 | ␉␉if (st->root16)␊ |
347 | ␉␉{␊ |
348 | ␉␉␉if (st->cluster >= msdosrootDirSectors && st->nument == 0)␊ |
349 | ␉␉␉␉return 0;␊ |
350 | ␉␉␉if (st->nument == 0)␊ |
351 | ␉␉␉{␊ |
352 | ␉␉␉␉Seek(ih, (msdosressector +␊ |
353 | ␉␉␉␉␉␉ (msdosnfats * msdosfatsecs)+st->cluster)*msdosbps);␊ |
354 | ␉␉␉␉Read(ih, (long)st->buf, msdosbps);␊ |
355 | ␉␉␉␉st->cluster++;␊ |
356 | ␉␉␉}␊ |
357 | ␉␉} else if (st->nument == 0 && !msdosreadcluster (ih, (uint8_t *)st->buf, msdosclustersize, &(st->cluster)))␊ |
358 | ␉␉␉return 0;␊ |
359 | ␉␉␊ |
360 | ␉␉dirp=st->buf+st->nument;␊ |
361 | ␉␉␊ |
362 | ␉␉if (dirp->deName[0] == SLOT_EMPTY)␊ |
363 | ␉␉␉return 0;␊ |
364 | ␉␉else if (dirp->deName[0] == SLOT_DELETED)␊ |
365 | ␉␉␉st->vfatnumber = 0;␊ |
366 | ␉␉else if (dirp->deAttributes == ATTR_WIN95)␊ |
367 | ␉␉{␊ |
368 | ␉␉␉struct winentry *wdirp = (struct winentry *)dirp;␊ |
369 | ␉␉␉int num;␊ |
370 | ␉␉␉if (wdirp->weCnt & 0x80)␊ |
371 | ␉␉␉␉continue;␊ |
372 | ␉␉␉num=(wdirp->weCnt&0x3f);␊ |
373 | ␉␉␉if (WIN_CHARS * num > WIN_MAXLEN)␊ |
374 | ␉␉␉␉continue;␊ |
375 | ␉␉␉if (st->vfatchecksum!=wdirp->weChksum)␊ |
376 | ␉␉␉{␊ |
377 | ␉␉␉␉st->vfatnumber = 0;␊ |
378 | ␉␉␉␉st->vfatchecksum = wdirp->weChksum;␊ |
379 | ␉␉␉}␊ |
380 | ␉␉␉if (st->vfatnumber < num)␊ |
381 | ␉␉␉␉st->vfatnumber = num;␊ |
382 | ␉␉␉bcopy (&(wdirp->wePart1),longname+WIN_CHARS*(num-1),sizeof (wdirp->wePart1));␊ |
383 | ␉␉␉bcopy (&(wdirp->wePart2),longname+WIN_CHARS*(num-1)+5,sizeof (wdirp->wePart2));␊ |
384 | ␉␉␉bcopy (&(wdirp->wePart3),longname+WIN_CHARS*(num-1)+11,sizeof (wdirp->wePart3));␊ |
385 | ␉␉} else {␊ |
386 | ␉␉␉uint8_t labelchecksum;␊ |
387 | ␉␉␉int i;␊ |
388 | ␉␉␉longname[st->vfatnumber*WIN_CHARS]=0;␊ |
389 | ␉␉␉␊ |
390 | ␉␉␉labelchecksum=0;␊ |
391 | ␉␉␉for(i=0;i<LABEL_LENGTH;i++)␊ |
392 | ␉␉␉␉labelchecksum=(labelchecksum>>1)+(labelchecksum<<7)+dirp->deName[i];␊ |
393 | ␉␉␉if (!(labelchecksum==st->vfatchecksum && st->vfatnumber))␊ |
394 | ␉␉␉␉longname[0]=0;␊ |
395 | ␉␉␉st->vfatnumber = 0;␊ |
396 | ␉␉␉st->vfatchecksum = 0;␊ |
397 | ␉␉␉st->vfatnumber = 0;␊ |
398 | ␉␉␉st->nument++;␊ |
399 | ␉␉␉if (((int)(!st->root16 &&st->nument * sizeof (struct direntry))>=msdosclustersize)␊ |
400 | ␉␉␉␉|| ((int)(st->root16 &&st->nument * sizeof (struct direntry))>=msdosbps))␊ |
401 | ␉␉␉␉st->nument = 0;␊ |
402 | ␉␉␉return dirp;␊ |
403 | ␉␉}␊ |
404 | ␉␉st->nument++;␊ |
405 | ␉␉if (((int)(!st->root16 &&st->nument * sizeof (struct direntry))>=msdosclustersize)␊ |
406 | ␉␉␉|| ((int)(st->root16 &&st->nument * sizeof (struct direntry))>=msdosbps))␊ |
407 | ␉␉␉st->nument = 0;␉␉␊ |
408 | ␉}␊ |
409 | }␊ |
410 | ␊ |
411 | static void␊ |
412 | initRoot (struct msdosdirstate *st)␊ |
413 | {␊ |
414 | ␉if (msdosrootDirSectors) {␉␉␉/* FAT12 or FAT16 */␊ |
415 | ␉␉st->root16 = 1;␊ |
416 | ␉␉st->vfatchecksum = 0;␊ |
417 | ␉␉st->nument = 0;␊ |
418 | ␉␉st->cluster = 0;␊ |
419 | ␉␉st->vfatnumber = 0;␊ |
420 | ␉} else {␉/* FAT32 */␊ |
421 | ␉␉st->root16 = 0;␊ |
422 | ␉␉st->vfatchecksum = 0;␊ |
423 | ␉␉st->nument = 0;␊ |
424 | ␉␉st->cluster = msdosrootcluster;␊ |
425 | ␉␉st->vfatnumber = 0;␊ |
426 | ␉}␊ |
427 | }␊ |
428 | ␊ |
429 | /* First comes lowercase, then uppercase*/␊ |
430 | static uint16_t cp850[128][2]=␊ |
431 | {␊ |
432 | {0x00E7,0x00C7},␊ |
433 | {0x00FC,0x00DC},␊ |
434 | {0x00E9,0x00C9},␊ |
435 | {0x00E2,0x00C2},␊ |
436 | {0x00E4,0x00C4},␊ |
437 | {0x00E0,0x00C0},␊ |
438 | {0x00E5,0x00C5},␊ |
439 | {0x00E7,0x00C7},␊ |
440 | {0x00EA,0x00CA},␊ |
441 | {0x00EB,0x00CB},␊ |
442 | {0x00E8,0x00C8},␊ |
443 | {0x00EF,0x00CF},␊ |
444 | {0x00EE,0x00CE},␊ |
445 | {0x00EC,0x00CC},␊ |
446 | {0x00E4,0x00C4},␊ |
447 | {0x00E5,0x00C5},␊ |
448 | {0x00E9,0x00C9},␊ |
449 | {0x00E6,0x00C6},␊ |
450 | {0x00E6,0x00C6},␊ |
451 | {0x00F4,0x00D4},␊ |
452 | {0x00F6,0x00D6},␊ |
453 | {0x00F2,0x00D2},␊ |
454 | {0x00FB,0x00DB},␊ |
455 | {0x00F9,0x00D9},␊ |
456 | {0x00FF,0x0178},␊ |
457 | {0x00F6,0x00D6},␊ |
458 | {0x00FC,0x00DC},␊ |
459 | {0x00F8,0x00D8},␊ |
460 | {0x00A3,0x00A3},␊ |
461 | {0x00F8,0x00D8},␊ |
462 | {0x00D7,0x00D7},␊ |
463 | {0x0192,0x0191},␊ |
464 | {0x00E1,0x00C1},␊ |
465 | {0x00ED,0x00CD},␊ |
466 | {0x00F3,0x00D3},␊ |
467 | {0x00FA,0x00DA},␊ |
468 | {0x00F1,0x00D1},␊ |
469 | {0x00F1,0x00D1},␊ |
470 | {0x00AA,0x00AA},␊ |
471 | {0x00BA,0x00BA},␊ |
472 | {0x00BF,0x00BF},␊ |
473 | {0x00AE,0x00AE},␊ |
474 | {0x00AC,0x00AC},␊ |
475 | {0x00BD,0x00BD},␊ |
476 | {0x00BC,0x00BC},␊ |
477 | {0x00A1,0x00A1},␊ |
478 | {0x00AB,0x00AB},␊ |
479 | {0x00BB,0x00BB},␊ |
480 | {0x2591,0x2591},␊ |
481 | {0x2592,0x2592},␊ |
482 | {0x2593,0x2593},␊ |
483 | {0x2502,0x2502},␊ |
484 | {0x2524,0x2524},␊ |
485 | {0x00E1,0x00C1},␊ |
486 | {0x00E2,0x00C2},␊ |
487 | {0x00E0,0x00C0},␊ |
488 | {0x00A9,0x00A9},␊ |
489 | {0x2563,0x2563},␊ |
490 | {0x2551,0x2551},␊ |
491 | {0x2557,0x2557},␊ |
492 | {0x255D,0x255D},␊ |
493 | {0x00A2,0x00A2},␊ |
494 | {0x00A5,0x00A5},␊ |
495 | {0x2510,0x2510},␊ |
496 | {0x2514,0x2514},␊ |
497 | {0x2534,0x2534},␊ |
498 | {0x252C,0x252C},␊ |
499 | {0x251C,0x251C},␊ |
500 | {0x2500,0x2500},␊ |
501 | {0x253C,0x253C},␊ |
502 | {0x00E3,0x00C3},␊ |
503 | {0x00E3,0x00C3},␊ |
504 | {0x255A,0x255A},␊ |
505 | {0x2554,0x2554},␊ |
506 | {0x2569,0x2569},␊ |
507 | {0x2566,0x2566},␊ |
508 | {0x2560,0x2560},␊ |
509 | {0x2550,0x2550},␊ |
510 | {0x256C,0x256C},␊ |
511 | {0x00A4,0x00A4},␊ |
512 | {0x00F0,0x00D0},␊ |
513 | {0x00F0,0x00D0},␊ |
514 | {0x00EA,0x00CA},␊ |
515 | {0x00EB,0x00CB},␊ |
516 | {0x00E8,0x00C8},␊ |
517 | {0x0131,0x0049},␊ |
518 | {0x00ED,0x00CD},␊ |
519 | {0x00EE,0x00CE},␊ |
520 | {0x00EF,0x00CF},␊ |
521 | {0x2518,0x2518},␊ |
522 | {0x250C,0x250C},␊ |
523 | {0x2588,0x2588},␊ |
524 | {0x2584,0x2584},␊ |
525 | {0x00A6,0x00A6},␊ |
526 | {0x00EC,0x00CC},␊ |
527 | {0x2580,0x2580},␊ |
528 | {0x00F3,0x00D3},␊ |
529 | {0x00DF,0x00DF},␊ |
530 | {0x00F4,0x00D4},␊ |
531 | {0x00F2,0x00D2},␊ |
532 | {0x00F5,0x00D5},␊ |
533 | {0x00F5,0x00D5},␊ |
534 | {0x00B5,0x00B5},␊ |
535 | {0x00FE,0x00DE},␊ |
536 | {0x00FE,0x00DE},␊ |
537 | {0x00FA,0x00DA},␊ |
538 | {0x00FB,0x00DB},␊ |
539 | {0x00F9,0x00D9},␊ |
540 | {0x00FD,0x00DD},␊ |
541 | {0x00FD,0x00DD},␊ |
542 | {0x00AF,0x00AF},␊ |
543 | {0x00B4,0x00B4},␊ |
544 | {0x00AD,0x00AD},␊ |
545 | {0x00B1,0x00B1},␊ |
546 | {0x2017,0x2017},␊ |
547 | {0x00BE,0x00BE},␊ |
548 | {0x00B6,0x00B6},␊ |
549 | {0x00A7,0x00A7},␊ |
550 | {0x00F7,0x00F7},␊ |
551 | {0x00B8,0x00B8},␊ |
552 | {0x00B0,0x00B0},␊ |
553 | {0x00A8,0x00A8},␊ |
554 | {0x00B7,0x00B7},␊ |
555 | {0x00B9,0x00B9},␊ |
556 | {0x00B3,0x00B3},␊ |
557 | {0x00B2,0x00B2},␊ |
558 | {0x25A0,0x25A0},␊ |
559 | {0x00A0,0x00A0}␊ |
560 | };␊ |
561 | ␊ |
562 | static int␊ |
563 | checkname (uint16_t *ucsname, int ucslen, struct direntry *dirp, uint16_t *vfatname)␊ |
564 | {␊ |
565 | ␉uint16_t tmp[15];␊ |
566 | ␉if (vfatname[0])␊ |
567 | ␉{␊ |
568 | ␉␉int i;␊ |
569 | ␉␉for (i=0;vfatname[i];i++);␊ |
570 | ␉␉return !FastUnicodeCompare (ucsname, ucslen, vfatname, i, OSLittleEndian);␊ |
571 | ␉}␊ |
572 | ␉else␊ |
573 | ␉{␊ |
574 | ␉␉int i, j, k;␊ |
575 | ␉␉for (i=7;i>=0;i--)␊ |
576 | ␉␉␉if (dirp->deName[i]!=' ')␊ |
577 | ␉␉␉␉break;␊ |
578 | ␉␉j=i+1;␊ |
579 | ␉␉tmp[i+1]=0;␊ |
580 | ␉␉for(;i>=0;i--)␊ |
581 | ␉␉␉tmp[i]=SWAP_LE16((dirp->deName[i]>=128)?cp850[dirp->deName[i]-128][0]:tolower(dirp->deName[i]));␊ |
582 | ␉␉for (i=2;i>=0;i--)␊ |
583 | ␉␉␉if (dirp->deName[8+i]!=' ')␊ |
584 | ␉␉␉␉break;␊ |
585 | ␉␉if (i>=0)␊ |
586 | ␉␉{␊ |
587 | ␉␉␉tmp[j++]='.';␊ |
588 | ␉␉␉tmp[j+i+1]=0;␊ |
589 | ␉␉␉k=j+i+1;␊ |
590 | ␉␉␉for(;i>=0;i--)␊ |
591 | ␉␉␉␉tmp[j+i]=SWAP_LE16((dirp->deName[i+8]>=128)?cp850[dirp->deName[i+8]-128][0]:tolower(dirp->deName[i+8]));␊ |
592 | ␉␉␉j=k;␊ |
593 | ␉␉}␊ |
594 | ␉␉return !FastUnicodeCompare (ucsname, ucslen, tmp, j, OSLittleEndian);␊ |
595 | ␉}␊ |
596 | ␉␊ |
597 | }␊ |
598 | ␊ |
599 | static struct direntry *␊ |
600 | getdirpfrompath (CICell ih, char *dirspec, uint8_t *buf)␊ |
601 | {␊ |
602 | ␉struct msdosdirstate st;␊ |
603 | ␉struct direntry *dirp;␊ |
604 | ␉uint8_t * ptr;␊ |
605 | ␉uint8_t *slash;␊ |
606 | ␉char c;␊ |
607 | ␉uint16_t␉␉vfatname[WIN_MAXLEN+2*WIN_CHARS];␊ |
608 | ␉uint16_t ucsname[WIN_MAXLEN+1];␊ |
609 | ␉uint16_t ucslen;␊ |
610 | ␉int ucslenhost;␊ |
611 | ␉initRoot (&st);␊ |
612 | ␉st.buf = (struct direntry *)buf;␊ |
613 | ␉ptr=(uint8_t*)dirspec;␊ |
614 | ␉for (slash=ptr;*slash && *slash!='/';slash++);␊ |
615 | ␉c=*slash;␊ |
616 | ␉*slash=0;␊ |
617 | ␉utf_decodestr (ptr, ucsname, &ucslen, WIN_MAXLEN, OSLittleEndian);␊ |
618 | ␉ucslenhost = OSReadLittleInt16 (&ucslen,0);␊ |
619 | ␉ucsname[ucslenhost]=0;␊ |
620 | ␉*slash=c;␉␊ |
621 | ␉while ((dirp = getnextdirent (ih, vfatname, &st)))␊ |
622 | ␉{␊ |
623 | ␉␉if (checkname (ucsname, ucslenhost, dirp, vfatname))␊ |
624 | ␉␉{␊ |
625 | ␉␉␉for (;*ptr && *ptr!='/';ptr++);␊ |
626 | ␉␉␉if (!*ptr)␊ |
627 | ␉␉␉␉return dirp;␊ |
628 | ␉␉␉ptr++;␊ |
629 | ␉␉␉if (!*ptr)␊ |
630 | ␉␉␉␉return dirp;␊ |
631 | ␉␉␉for (slash=ptr;*slash && *slash!='/';slash++);␊ |
632 | ␉␉␉c=*slash;␊ |
633 | ␉␉␉*slash=0;␊ |
634 | ␉␉␉utf_decodestr (ptr, ucsname, &ucslen, WIN_MAXLEN, OSLittleEndian);␊ |
635 | ␉␉␉ucslenhost = OSReadLittleInt16 (&ucslen,0);␊ |
636 | ␉␉␉ucsname[ucslenhost]=0;␊ |
637 | ␉␉␉*slash=c;␊ |
638 | ␉␉␉if (!(dirp->deAttributes & ATTR_DIRECTORY))␊ |
639 | ␉␉␉␉return 0;␊ |
640 | ␉␉␉st.root16 = 0;␊ |
641 | ␉␉␉st.vfatchecksum = 0;␊ |
642 | ␉␉␉st.nument = 0;␊ |
643 | ␉␉␉st.cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);␊ |
644 | ␉␉␉if (msdosfatbits == 32)␊ |
645 | ␉␉␉␉st.cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;␊ |
646 | ␉␉␉st.vfatnumber = 0;␊ |
647 | ␉␉}␊ |
648 | ␉}␊ |
649 | ␉return 0;␊ |
650 | }␊ |
651 | ␊ |
652 | long MSDOSGetDirEntry(CICell ih, char * dirPath, long long * dirIndex,␊ |
653 | ␉␉␉␉␉ char ** name, long * flags, long * time,␊ |
654 | ␉␉␉␉␉ FinderInfo * finderInfo, long * infoValid)␊ |
655 | {␊ |
656 | ␉struct msdosdirstate *st;␊ |
657 | ␉struct direntry *dirp;␊ |
658 | ␉uint16_t␉␉vfatname[WIN_MAXLEN+2*WIN_CHARS];␊ |
659 | ␉if (MSDOSInitPartition (ih)<0)␊ |
660 | ␉␉return -1;␊ |
661 | ␉if (dirPath[0] == '/')␊ |
662 | ␉␉dirPath++;␊ |
663 | ␉st = (struct msdosdirstate *)(long) *dirIndex;␊ |
664 | ␉if (!st)␊ |
665 | ␉{␊ |
666 | ␉␉st=malloc (sizeof (struct msdosdirstate));␊ |
667 | ␉␉if (!st)␊ |
668 | ␉␉{␊ |
669 | ␉␉␉return -1;␊ |
670 | ␉␉}␊ |
671 | ␉␉if (dirPath[0])␊ |
672 | ␉␉{␊ |
673 | ␉␉␉uint8_t *buf=malloc(msdosclustersize);␊ |
674 | ␉␉␉if (!buf)␊ |
675 | ␉␉␉{␊ |
676 | ␉␉␉␉free (st);␊ |
677 | ␉␉␉␉return -1;␊ |
678 | ␉␉␉}␊ |
679 | ␉␉␉dirp = getdirpfrompath (ih, dirPath, buf);␊ |
680 | ␉␉␉if (!dirp || !(dirp->deAttributes & ATTR_DIRECTORY))␊ |
681 | ␉␉␉{␊ |
682 | ␉␉␉␉free (buf);␊ |
683 | ␉␉␉␉free (st);␊ |
684 | ␉␉␉␉return -1;␊ |
685 | ␉␉␉}␊ |
686 | ␉␉␉st->buf = (struct direntry *)buf;␊ |
687 | ␉␉␉st->root16 = 0;␊ |
688 | ␉␉␉st->vfatchecksum = 0;␊ |
689 | ␉␉␉st->nument = 0;␊ |
690 | ␉␉␉st->cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);␊ |
691 | ␉␉␉st->vfatnumber = 0;␊ |
692 | ␉␉␉if (msdosfatbits == 32)␊ |
693 | ␉␉␉␉st->cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;␊ |
694 | ␉␉}␊ |
695 | ␉␉else␊ |
696 | ␉␉␉initRoot (st);␊ |
697 | ␉␉*dirIndex = (long long) (long) st;␊ |
698 | ␉}␊ |
699 | ␉while((dirp = getnextdirent (ih, vfatname, st))&& (dirp->deAttributes & ATTR_VOLUME));␊ |
700 | ␉if (!dirp)␊ |
701 | ␉{␊ |
702 | ␉␉free (st->buf);␊ |
703 | ␉␉free (st);␊ |
704 | ␉␉return -1;␊ |
705 | ␉}␊ |
706 | ␉if (vfatname[0])␊ |
707 | ␉{␊ |
708 | ␉␉int i;␊ |
709 | ␉␉for (i=0;vfatname[i];i++);␊ |
710 | ␉␉*name = malloc (256);␊ |
711 | ␉␉if (!*name) ␊ |
712 | ␉␉{␊ |
713 | ␉␉␉free (st->buf);␊ |
714 | ␉␉␉free (st);␊ |
715 | ␉␉␉return -1;␊ |
716 | ␉␉}␊ |
717 | ␉␉utf_encodestr(vfatname, i, (u_int8_t *)*name, 255, OSLittleEndian );␉␊ |
718 | ␉}␊ |
719 | ␉else␊ |
720 | ␉{␊ |
721 | ␉␉int i, j, k;␊ |
722 | ␉␉uint16_t tmp[13];␊ |
723 | ␉␉*name = malloc (26);␊ |
724 | ␉␉if (!*name) ␊ |
725 | ␉␉{␊ |
726 | ␉␉␉free (st->buf);␊ |
727 | ␉␉␉free (st);␊ |
728 | ␉␉␉return -1;␊ |
729 | ␉␉}␊ |
730 | ␉␉for (i=7;i>=0;i--)␊ |
731 | ␉␉␉if (dirp->deName[i]!=' ')␊ |
732 | ␉␉␉␉break;␊ |
733 | ␉␉j=i+1;␊ |
734 | ␉␉tmp[i+1]=0;␊ |
735 | ␉␉for(;i>=0;i--)␊ |
736 | ␉␉␉tmp[i]=(dirp->deName[i]>=128)?cp850[dirp->deName[i]-128][0]:tolower(dirp->deName[i]);␊ |
737 | ␉␉for (i=2;i>=0;i--)␊ |
738 | ␉␉␉if (dirp->deName[8+i]!=' ')␊ |
739 | ␉␉␉␉break;␊ |
740 | ␉␉␊ |
741 | ␉␉if (i>=0)␊ |
742 | ␉␉{␊ |
743 | ␉␉␉tmp[j++]='.';␊ |
744 | ␉␉␉tmp[j+i+1]=0;␊ |
745 | ␉␉␉k=j+i+1;␊ |
746 | ␉␉␉for(;i>=0;i--)␊ |
747 | ␉␉␉␉tmp[j+i]=(dirp->deName[i]>=128)?cp850[dirp->deName[i+8]-128][0]:tolower(dirp->deName[i+8]);␊ |
748 | ␉␉␉j=k;␊ |
749 | ␉␉}␊ |
750 | ␉␉␊ |
751 | ␉␉utf_encodestr(tmp, j, (uint8_t*)*name, 25, OSHostByteOrder() );␊ |
752 | ␉}␊ |
753 | ␉if (dirp->deAttributes & ATTR_DIRECTORY)␊ |
754 | ␉␉*flags = kFileTypeDirectory;␊ |
755 | ␉else␊ |
756 | ␉␉*flags = kFileTypeFlat;␊ |
757 | ␉␊ |
758 | ␉// Calculate a fake timestamp using modification date and time values.␊ |
759 | ␉*time = ((dirp->deMDate & 0x7FFF) << 16) + dirp->deMTime;␊ |
760 | ␉␊ |
761 | ␉if (infoValid)␊ |
762 | ␉␉*infoValid = 1;␊ |
763 | ␊ |
764 | ␉return 0;␊ |
765 | }␊ |
766 | ␊ |
767 | long ␊ |
768 | MSDOSReadFile(CICell ih, char * filePath, void *base, uint64_t offset, uint64_t length)␊ |
769 | {␊ |
770 | ␉uint8_t *buf;␊ |
771 | ␉off_t cluster;␊ |
772 | ␉uint64_t size;␊ |
773 | ␉uint64_t nskip;␊ |
774 | ␉int toread, wastoread;␊ |
775 | ␉char *ptr = (char *)base;␊ |
776 | ␉struct direntry *dirp;␊ |
777 | ␉uint64_t i;␊ |
778 | char devStr[12];␊ |
779 | ␊ |
780 | ␉if (MSDOSInitPartition (ih)<0)␊ |
781 | ␉␉return -1;␊ |
782 | ␉if (filePath[0] == '/')␊ |
783 | ␉␉filePath++;␊ |
784 | ␉buf = malloc(msdosclustersize);␊ |
785 | ␉if (!buf)␊ |
786 | ␉{␊ |
787 | ␉␉return -1;␊ |
788 | ␉}␊ |
789 | ␉dirp = getdirpfrompath (ih, filePath, buf);␊ |
790 | ␉␊ |
791 | ␉if (!dirp || (dirp->deAttributes & ATTR_DIRECTORY))␊ |
792 | ␉{␊ |
793 | ␉␉free (buf);␊ |
794 | ␉␉return -1;␊ |
795 | ␉}␊ |
796 | ␉cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);␊ |
797 | ␉if (msdosfatbits == 32)␊ |
798 | ␉␉cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;␊ |
799 | ␉size = (uint32_t)OSReadLittleInt32 ((dirp->deFileSize),0);␊ |
800 | ␉if (size<=offset)␊ |
801 | ␉{␊ |
802 | ␉␉free (buf);␊ |
803 | ␉␉return -1;␊ |
804 | ␉}␊ |
805 | ␉nskip=offset/msdosclustersize;␊ |
806 | ␉for (i=0;i<nskip;i++)␊ |
807 | ␉␉msdosreadcluster (ih, 0, 0, &cluster);␊ |
808 | ␉msdosreadcluster (ih, buf, msdosclustersize, &cluster);␊ |
809 | ␉toread=length;␊ |
810 | ␉if (length==0 || length>size-offset)␊ |
811 | ␉␉toread=size-offset;␊ |
812 | ␉wastoread=toread;␊ |
813 | ␉bcopy (buf+(offset%msdosclustersize),ptr,MIN((msdosclustersize-(offset%msdosclustersize)),(unsigned)toread));␊ |
814 | ␉ptr+=msdosclustersize-(offset%msdosclustersize);␊ |
815 | ␉toread-=msdosclustersize-(offset%msdosclustersize);␊ |
816 | ␉while (toread>0 && msdosreadcluster (ih, (uint8_t *)ptr, MIN(msdosclustersize,toread), &cluster))␊ |
817 | ␉{␊ |
818 | ␉␉ptr+=msdosclustersize;␊ |
819 | ␉␉toread-=msdosclustersize;␊ |
820 | ␉}␊ |
821 | ␉␊ |
822 | ␉getDeviceDescription(ih, devStr);␊ |
823 | ␉verbose("Read FAT%d file: [%s/%s] %d bytes.\n",␊ |
824 | msdosfatbits, devStr, filePath, (uint32_t)( toread<0 ) ? wastoread : wastoread-toread);␊ |
825 | ␉free (buf);␊ |
826 | ␉if (toread<0)␊ |
827 | ␉␉return wastoread;␊ |
828 | ␉else␊ |
829 | ␉␉return wastoread-toread;␊ |
830 | }␊ |
831 | ␊ |
832 | long ␊ |
833 | MSDOSGetFileBlock(CICell ih, char *filePath, unsigned long long *firstBlock)␊ |
834 | {␊ |
835 | ␉uint8_t *buf;␊ |
836 | ␉off_t cluster;␊ |
837 | ␉struct direntry *dirp;␊ |
838 | ␉if (MSDOSInitPartition (ih)<0)␊ |
839 | ␉␉return -1;␊ |
840 | ␉if (filePath[0] == '/')␊ |
841 | ␉␉filePath++;␊ |
842 | ␉buf = malloc(msdosclustersize);␊ |
843 | ␉if (!buf)␊ |
844 | ␉{␊ |
845 | ␉␉return -1;␊ |
846 | ␉}␊ |
847 | ␉dirp = getdirpfrompath (ih, filePath, buf);␊ |
848 | ␉if (!dirp || (dirp->deAttributes & ATTR_DIRECTORY))␊ |
849 | ␉{␊ |
850 | ␉␉free (buf);␊ |
851 | ␉␉return -1;␊ |
852 | ␉}␊ |
853 | ␉cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);␊ |
854 | ␉if (msdosfatbits == 32)␊ |
855 | ␉␉cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;␊ |
856 | ␉␊ |
857 | ␉off_t clusn;␊ |
858 | ␉␊ |
859 | ␉switch (msdosfatbits) {␊ |
860 | ␉␉case 32:␊ |
861 | ␉␉␉if (cluster < CLUST_FIRST ||cluster >= CLUST_RSRVD32)␊ |
862 | ␉␉␉␉return -1;␊ |
863 | ␉␉␉clusn = cluster - CLUST_FIRST;␊ |
864 | ␉␉␉break;␊ |
865 | ␉␉case 16:␊ |
866 | ␉␉␉if (cluster < CLUST_FIRST ||cluster >= CLUST_RSRVD16)␊ |
867 | ␉␉␉␉return 0;␊ |
868 | ␉␉␉clusn = cluster - CLUST_FIRST;␊ |
869 | ␉␉␉break;␉␉␉␊ |
870 | ␉␉case 12:␊ |
871 | ␉␉␉␊ |
872 | ␉␉␉if (cluster < CLUST_FIRST ||cluster >= CLUST_RSRVD12)␊ |
873 | ␉␉␉␉return 0;␊ |
874 | ␉␉␉clusn = cluster - CLUST_FIRST;␊ |
875 | ␉␉␉break;␊ |
876 | ␉␉default:␊ |
877 | ␉␉␉return 0;␊ |
878 | ␉}␊ |
879 | ␉␊ |
880 | ␉*firstBlock = ((msdosressector +␊ |
881 | ␉␉␉␉ (msdosnfats * msdosfatsecs))*msdosbps + clusn * msdosclustersize)/512;␊ |
882 | ␉free (buf);␊ |
883 | ␉return 0;␊ |
884 | }␊ |
885 | ␊ |
886 | ␊ |
887 | long MSDOSLoadFile(CICell ih, char * filePath)␊ |
888 | {␊ |
889 | ␉return MSDOSReadFile(ih, filePath, (void *)gFSLoadAddress, 0, 0);␊ |
890 | }␊ |
891 | ␊ |
892 | /* Fix up volume label. */␊ |
893 | static void␊ |
894 | fixLabel(uint8_t *label, char *str, long strMaxLen)␊ |
895 | {␊ |
896 | ␉int␉␉␉i, len;␊ |
897 | ␉uint16_t␉␉labelucs[13];␊ |
898 | ␉//unsigned char␉␉labelUTF8[LABEL_LENGTH*3];␊ |
899 | ␉␊ |
900 | ␉/* Convert leading 0x05 to 0xE5 for multibyte languages like Japanese */␊ |
901 | ␉if (label[0] == 0x05)␊ |
902 | ␉label[0] = 0xE5;␊ |
903 | ␉␊ |
904 | ␉/* Remove any trailing spaces */␊ |
905 | ␉for (i=LABEL_LENGTH-1; i>=0; --i) {␊ |
906 | ␉if (label[i] == ' ')␊ |
907 | label[i] = 0;␊ |
908 | ␉else␊ |
909 | ␉␉break;␊ |
910 | ␉}␊ |
911 | ␉labelucs[i++]=0;␊ |
912 | ␉len=i;␊ |
913 | ␉for (;i>=0;--i)␊ |
914 | ␉␉labelucs[i]=label[i]>=128?cp850[label[i]-128][1]:(label[i]);␊ |
915 | ␉␊ |
916 | ␉utf_encodestr(labelucs, len, (uint8_t *)str, strMaxLen, OSHostByteOrder() );␊ |
917 | }␊ |
918 | ␊ |
919 | ␊ |
920 | void␊ |
921 | MSDOSGetDescription(CICell ih, char *str, long strMaxLen)␊ |
922 | {␊ |
923 | ␉struct direntry␉␉*dirp;␊ |
924 | ␉uint8_t␉␉␉label[LABEL_LENGTH+1];␊ |
925 | ␉uint16_t␉␉vfatlabel[WIN_MAXLEN+2*WIN_CHARS];␊ |
926 | ␉struct msdosdirstate st;␊ |
927 | ␉int labelfound = 0;␊ |
928 | ␊ |
929 | ␉if (MSDOSInitPartition (ih)<0)␊ |
930 | ␉{␊ |
931 | ␉␉str[0]=0;␊ |
932 | ␉␉return;␊ |
933 | ␉}␊ |
934 | ␊ |
935 | ␉label[0] = '\0';␊ |
936 | ␉␊ |
937 | ␉initRoot (&st);␊ |
938 | ␉st.buf = malloc(msdosclustersize);␊ |
939 | ␉if (!st.buf)␊ |
940 | ␉{␊ |
941 | ␉␉return;␊ |
942 | ␉}␊ |
943 | ␉while ((dirp = getnextdirent (ih, vfatlabel, &st)))␊ |
944 | ␉␉if (dirp->deAttributes & ATTR_VOLUME) {␊ |
945 | ␉␉␉strncpy((char *)label, (char *)dirp->deName, LABEL_LENGTH);␊ |
946 | ␉␉␉labelfound = 1;␊ |
947 | ␉␉␉break;␊ |
948 | ␉␉}␊ |
949 | ␉␉␊ |
950 | ␉free(st.buf);␉␉␊ |
951 | ␊ |
952 | ␉if (vfatlabel[0] && labelfound)␊ |
953 | ␉{␊ |
954 | ␉␉int i;␊ |
955 | ␉␉for (i=0;vfatlabel[i];i++);␊ |
956 | ␉␉utf_encodestr(vfatlabel, i, (u_int8_t *)str, strMaxLen, OSLittleEndian );␉␉␊ |
957 | ␉}␉␊ |
958 | ␉else if (labelfound)␊ |
959 | ␉␉fixLabel(label, str, strMaxLen);␊ |
960 | ␉␊ |
961 | ␉/* else look in the boot blocks */␊ |
962 | ␉if (!labelfound || str[0] == '\0')␊ |
963 | ␉{␊ |
964 | ␉␉char *buf = malloc (512);␊ |
965 | ␉␉if (!buf)␊ |
966 | ␉␉{␊ |
967 | ␉␉␉return;␊ |
968 | ␉␉}␊ |
969 | ␉␉union bootsector *bsp = (union bootsector *)buf;␊ |
970 | ␉␉Seek(ih, 0);␊ |
971 | ␉␉Read(ih, (long)buf, 512);␊ |
972 | ␉␉if (msdosfatbits == 32)␊ |
973 | ␉␉{␊ |
974 | ␉␉␉/* It's FAT32 */␊ |
975 | ␉␉␉strncpy((char *)label, (char *)((struct extboot *)bsp->bs710.bsExt)->exVolumeLabel, LABEL_LENGTH);␊ |
976 | ␉␉}␊ |
977 | ␉␉else if (msdosfatbits == 16)␊ |
978 | ␉␉{␊ |
979 | ␉␉␉strncpy((char *)label, (char *)((struct extboot *)bsp->bs50.bsExt)->exVolumeLabel, LABEL_LENGTH);␊ |
980 | ␉␉}␊ |
981 | ␉␉free (buf);␊ |
982 | ␉␉fixLabel(label, str, strMaxLen);␊ |
983 | ␉}␊ |
984 | ␊ |
985 | ␉return;␊ |
986 | }␊ |
987 | ␊ |
988 | long ␊ |
989 | MSDOSGetUUID(CICell ih, char *uuidStr)␊ |
990 | {␊ |
991 | ␉char *buf = malloc (512);␊ |
992 | ␉if (!buf)␊ |
993 | ␉{␊ |
994 | ␉␉return -1;␊ |
995 | ␉}␊ |
996 | ␉union bootsector *bsp = (union bootsector *)buf;␊ |
997 | ␊ |
998 | ␉if (MSDOSInitPartition (ih)<0)␊ |
999 | ␉{␊ |
1000 | ␉␉free (buf);␊ |
1001 | ␉␉return -1;␊ |
1002 | ␉}␊ |
1003 | ␉bzero (uuidStr, 16);␊ |
1004 | ␉Seek(ih, 0);␊ |
1005 | ␉Read(ih, (long)buf, 512);␊ |
1006 | ␉if (msdosfatbits == 32) { /* It's FAT32 */␊ |
1007 | ␉␉memcpy(uuidStr+12, (char *)((struct extboot *)bsp->bs710.bsExt)->exVolumeID, 4);␊ |
1008 | ␉}␊ |
1009 | ␉else if (msdosfatbits == 16) {␊ |
1010 | ␉␉memcpy(uuidStr+12, (char *)((struct extboot *)bsp->bs50.bsExt)->exVolumeID, 4);␊ |
1011 | ␉}␊ |
1012 | ␉free (buf);␊ |
1013 | return 0;␊ |
1014 | ␊ |
1015 | }␊ |
1016 | |