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