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