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