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