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