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