Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/msdos.c

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

Archive Download this file

Revision: 1977