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