Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2340