]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - fs/fat/fat.c
972ef33d03c4a2d7445e850fe0e3fa9706ee5f2f
[karo-tx-uboot.git] / fs / fat / fat.c
1 /*
2  * fat.c
3  *
4  * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
5  *
6  * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
7  * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 #include <common.h>
29 #include <config.h>
30 #include <fat.h>
31 #include <asm/byteorder.h>
32 #include <part.h>
33
34 #if (CONFIG_COMMANDS & CFG_CMD_FAT)
35
36 /*
37  * Convert a string to lowercase.
38  */
39 static void
40 downcase(char *str)
41 {
42         while (*str != '\0') {
43                 TOLOWER(*str);
44                 str++;
45         }
46 }
47
48 static  block_dev_desc_t *cur_dev = NULL;
49 static unsigned long part_offset = 0;
50 static int cur_part = 1;
51
52 #define DOS_PART_TBL_OFFSET     0x1be
53 #define DOS_PART_MAGIC_OFFSET   0x1fe
54 #define DOS_FS_TYPE_OFFSET      0x36
55
56 int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
57 {
58         startblock += part_offset;
59         if (cur_dev == NULL)
60                 return -1;
61         if (cur_dev->block_read) {
62                 return cur_dev->block_read (cur_dev->dev, startblock, getsize, (unsigned long *)bufptr);
63         }
64         return -1;
65 }
66
67
68 int
69 fat_register_device(block_dev_desc_t *dev_desc, int part_no)
70 {
71         unsigned char buffer[SECTOR_SIZE];
72
73         if (!dev_desc->block_read)
74                 return -1;
75         cur_dev=dev_desc;
76         /* check if we have a MBR (on floppies we have only a PBR) */
77         if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
78                 printf ("** Can't read from device %d **\n", dev_desc->dev);
79                 return -1;
80         }
81         if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
82                 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
83                 /* no signature found */
84                 return -1;
85         }
86         if(!strncmp(&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
87                 /* ok, we assume we are on a PBR only */
88                 cur_part = 1;
89                 part_offset=0;
90         }
91         else {
92 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI)
93                 disk_partition_t info;
94                 if(!get_partition_info(dev_desc, part_no, &info)) {
95                         part_offset = info.start;
96                         cur_part = part_no;
97                 }
98                 else {
99                         printf ("** Partition %d not valid on device %d **\n",part_no,dev_desc->dev);
100                         return -1;
101                 }
102 #else
103                 /* FIXME we need to determine the start block of the
104                  * partition where the DOS FS resides. This can be done
105                  * by using the get_partition_info routine. For this
106                  * purpose the libpart must be included.
107                  */
108                 part_offset=32;
109                 cur_part = 1;
110 #endif
111         }
112         return 0;
113 }
114
115
116 /*
117  * Get the first occurence of a directory delimiter ('/' or '\') in a string.
118  * Return index into string if found, -1 otherwise.
119  */
120 static int
121 dirdelim(char *str)
122 {
123         char *start = str;
124
125         while (*str != '\0') {
126                 if (ISDIRDELIM(*str)) return str - start;
127                 str++;
128         }
129         return -1;
130 }
131
132
133 /*
134  * Match volume_info fs_type strings.
135  * Return 0 on match, -1 otherwise.
136  */
137 static int
138 compare_sign(char *str1, char *str2)
139 {
140         char *end = str1+SIGNLEN;
141
142         while (str1 != end) {
143                 if (*str1 != *str2) {
144                         return -1;
145                 }
146                 str1++;
147                 str2++;
148         }
149
150         return 0;
151 }
152
153
154 /*
155  * Extract zero terminated short name from a directory entry.
156  */
157 static void get_name (dir_entry *dirent, char *s_name)
158 {
159         char *ptr;
160
161         memcpy (s_name, dirent->name, 8);
162         s_name[8] = '\0';
163         ptr = s_name;
164         while (*ptr && *ptr != ' ')
165                 ptr++;
166         if (dirent->ext[0] && dirent->ext[0] != ' ') {
167                 *ptr = '.';
168                 ptr++;
169                 memcpy (ptr, dirent->ext, 3);
170                 ptr[3] = '\0';
171                 while (*ptr && *ptr != ' ')
172                         ptr++;
173         }
174         *ptr = '\0';
175         if (*s_name == DELETED_FLAG)
176                 *s_name = '\0';
177         else if (*s_name == aRING)
178                 *s_name = 'å';
179         downcase (s_name);
180 }
181
182 /*
183  * Get the entry at index 'entry' in a FAT (12/16/32) table.
184  * On failure 0x00 is returned.
185  */
186 static __u32
187 get_fatent(fsdata *mydata, __u32 entry)
188 {
189         __u32 bufnum;
190         __u32 offset;
191         __u32 ret = 0x00;
192
193         switch (mydata->fatsize) {
194         case 32:
195                 bufnum = entry / FAT32BUFSIZE;
196                 offset = entry - bufnum * FAT32BUFSIZE;
197                 break;
198         case 16:
199                 bufnum = entry / FAT16BUFSIZE;
200                 offset = entry - bufnum * FAT16BUFSIZE;
201                 break;
202         case 12:
203                 bufnum = entry / FAT12BUFSIZE;
204                 offset = entry - bufnum * FAT12BUFSIZE;
205                 break;
206
207         default:
208                 /* Unsupported FAT size */
209                 return ret;
210         }
211
212         /* Read a new block of FAT entries into the cache. */
213         if (bufnum != mydata->fatbufnum) {
214                 int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
215                 __u8 *bufptr = mydata->fatbuf;
216                 __u32 fatlength = mydata->fatlength;
217                 __u32 startblock = bufnum * FATBUFBLOCKS;
218
219                 fatlength *= SECTOR_SIZE;       /* We want it in bytes now */
220                 startblock += mydata->fat_sect; /* Offset from start of disk */
221
222                 if (getsize > fatlength) getsize = fatlength;
223                 if (disk_read(startblock, getsize, bufptr) < 0) {
224                         FAT_DPRINT("Error reading FAT blocks\n");
225                         return ret;
226                 }
227                 mydata->fatbufnum = bufnum;
228         }
229
230         /* Get the actual entry from the table */
231         switch (mydata->fatsize) {
232         case 32:
233                 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
234                 break;
235         case 16:
236                 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
237                 break;
238         case 12: {
239                 __u32 off16 = (offset*3)/4;
240                 __u16 val1, val2;
241
242                 switch (offset & 0x3) {
243                 case 0:
244                         ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
245                         ret &= 0xfff;
246                         break;
247                 case 1:
248                         val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
249                         val1 &= 0xf000;
250                         val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
251                         val2 &= 0x00ff;
252                         ret = (val2 << 4) | (val1 >> 12);
253                         break;
254                 case 2:
255                         val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
256                         val1 &= 0xff00;
257                         val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
258                         val2 &= 0x000f;
259                         ret = (val2 << 8) | (val1 >> 8);
260                         break;
261                 case 3:
262                         ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
263                         ret = (ret & 0xfff0) >> 4;
264                         break;
265                 default:
266                         break;
267                 }
268         }
269         break;
270         }
271         FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
272
273         return ret;
274 }
275
276
277 /*
278  * Read at most 'size' bytes from the specified cluster into 'buffer'.
279  * Return 0 on success, -1 otherwise.
280  */
281 static int
282 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
283 {
284         int idx = 0;
285         __u32 startsect;
286
287         if (clustnum > 0) {
288                 startsect = mydata->data_begin + clustnum*mydata->clust_size;
289         } else {
290                 startsect = mydata->rootdir_sect;
291         }
292
293         FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
294         if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
295                 FAT_DPRINT("Error reading data\n");
296                 return -1;
297         }
298         if(size % FS_BLOCK_SIZE) {
299                 __u8 tmpbuf[FS_BLOCK_SIZE];
300                 idx= size/FS_BLOCK_SIZE;
301                 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
302                         FAT_DPRINT("Error reading data\n");
303                         return -1;
304                 }
305                 buffer += idx*FS_BLOCK_SIZE;
306
307                 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
308                 return 0;
309         }
310
311         return 0;
312 }
313
314
315 /*
316  * Read at most 'maxsize' bytes from the file associated with 'dentptr'
317  * into 'buffer'.
318  * Return the number of bytes read or -1 on fatal errors.
319  */
320 static long
321 get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
322              unsigned long maxsize)
323 {
324         unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
325         unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
326         __u32 curclust = START(dentptr);
327         __u32 endclust, newclust;
328         unsigned long actsize;
329
330         FAT_DPRINT("Filesize: %ld bytes\n", filesize);
331
332         if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
333
334         FAT_DPRINT("Reading: %ld bytes\n", filesize);
335
336         actsize=bytesperclust;
337         endclust=curclust;
338         do {
339                 /* search for consecutive clusters */
340                 while(actsize < filesize) {
341                         newclust = get_fatent(mydata, endclust);
342                         if((newclust -1)!=endclust)
343                                 goto getit;
344                         if (newclust <= 0x0001 || newclust >= 0xfff0) {
345                                 FAT_DPRINT("curclust: 0x%x\n", newclust);
346                                 FAT_DPRINT("Invalid FAT entry\n");
347                                 return gotsize;
348                         }
349                         endclust=newclust;
350                         actsize+= bytesperclust;
351                 }
352                 /* actsize >= file size */
353                 actsize -= bytesperclust;
354                 /* get remaining clusters */
355                 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
356                         FAT_ERROR("Error reading cluster\n");
357                         return -1;
358                 }
359                 /* get remaining bytes */
360                 gotsize += (int)actsize;
361                 filesize -= actsize;
362                 buffer += actsize;
363                 actsize= filesize;
364                 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
365                         FAT_ERROR("Error reading cluster\n");
366                         return -1;
367                 }
368                 gotsize+=actsize;
369                 return gotsize;
370 getit:
371                 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
372                         FAT_ERROR("Error reading cluster\n");
373                         return -1;
374                 }
375                 gotsize += (int)actsize;
376                 filesize -= actsize;
377                 buffer += actsize;
378                 curclust = get_fatent(mydata, endclust);
379                 if (curclust <= 0x0001 || curclust >= 0xfff0) {
380                         FAT_DPRINT("curclust: 0x%x\n", curclust);
381                         FAT_ERROR("Invalid FAT entry\n");
382                         return gotsize;
383                 }
384                 actsize=bytesperclust;
385                 endclust=curclust;
386         } while (1);
387 }
388
389
390 #ifdef CONFIG_SUPPORT_VFAT
391 /*
392  * Extract the file name information from 'slotptr' into 'l_name',
393  * starting at l_name[*idx].
394  * Return 1 if terminator (zero byte) is found, 0 otherwise.
395  */
396 static int
397 slot2str(dir_slot *slotptr, char *l_name, int *idx)
398 {
399         int j;
400
401         for (j = 0; j <= 8; j += 2) {
402                 l_name[*idx] = slotptr->name0_4[j];
403                 if (l_name[*idx] == 0x00) return 1;
404                 (*idx)++;
405         }
406         for (j = 0; j <= 10; j += 2) {
407                 l_name[*idx] = slotptr->name5_10[j];
408                 if (l_name[*idx] == 0x00) return 1;
409                 (*idx)++;
410         }
411         for (j = 0; j <= 2; j += 2) {
412                 l_name[*idx] = slotptr->name11_12[j];
413                 if (l_name[*idx] == 0x00) return 1;
414                 (*idx)++;
415         }
416
417         return 0;
418 }
419
420
421 /*
422  * Extract the full long filename starting at 'retdent' (which is really
423  * a slot) into 'l_name'. If successful also copy the real directory entry
424  * into 'retdent'
425  * Return 0 on success, -1 otherwise.
426  */
427 static int
428 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
429              dir_entry *retdent, char *l_name)
430 {
431         dir_entry *realdent;
432         dir_slot  *slotptr = (dir_slot*) retdent;
433         __u8      *nextclust = cluster + mydata->clust_size * SECTOR_SIZE;
434         __u8       counter = slotptr->id & 0xf;
435         int idx = 0;
436
437         while ((__u8*)slotptr < nextclust) {
438                 if (counter == 0) break;
439                 if ((slotptr->id & 0x0f) != counter) return -1;
440                 slotptr++;
441                 counter--;
442         }
443
444         if ((__u8*)slotptr >= nextclust) {
445                 __u8     block[MAX_CLUSTSIZE];
446                 dir_slot *slotptr2;
447
448                 slotptr--;
449                 curclust = get_fatent(mydata, curclust);
450                 if (curclust <= 0x0001 || curclust >= 0xfff0) {
451                         FAT_DPRINT("curclust: 0x%x\n", curclust);
452                         FAT_ERROR("Invalid FAT entry\n");
453                         return -1;
454                 }
455                 if (get_cluster(mydata, curclust, block,
456                                 mydata->clust_size * SECTOR_SIZE) != 0) {
457                         FAT_DPRINT("Error: reading directory block\n");
458                         return -1;
459                 }
460                 slotptr2 = (dir_slot*) block;
461                 while (slotptr2->id > 0x01) {
462                         slotptr2++;
463                 }
464                 /* Save the real directory entry */
465                 realdent = (dir_entry*)slotptr2 + 1;
466                 while ((__u8*)slotptr2 >= block) {
467                         slot2str(slotptr2, l_name, &idx);
468                         slotptr2--;
469                 }
470         } else {
471                 /* Save the real directory entry */
472                 realdent = (dir_entry*)slotptr;
473         }
474
475         do {
476                 slotptr--;
477                 if (slot2str(slotptr, l_name, &idx)) break;
478         } while (!(slotptr->id & 0x40));
479
480         l_name[idx] = '\0';
481         if (*l_name == DELETED_FLAG) *l_name = '\0';
482         else if (*l_name == aRING) *l_name = 'å';
483         downcase(l_name);
484
485         /* Return the real directory entry */
486         memcpy(retdent, realdent, sizeof(dir_entry));
487
488         return 0;
489 }
490
491
492 /* Calculate short name checksum */
493 static __u8
494 mkcksum(const char *str)
495 {
496         int i;
497         __u8 ret = 0;
498
499         for (i = 0; i < 11; i++) {
500                 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
501         }
502
503         return ret;
504 }
505 #endif
506
507
508 /*
509  * Get the directory entry associated with 'filename' from the directory
510  * starting at 'startsect'
511  */
512 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
513                                    char *filename, dir_entry * retdent,
514                                    int dols)
515 {
516     __u16 prevcksum = 0xffff;
517     __u8 block[MAX_CLUSTSIZE];
518     __u32 curclust = START (retdent);
519     int files = 0, dirs = 0;
520
521     FAT_DPRINT ("get_dentfromdir: %s\n", filename);
522     while (1) {
523         dir_entry *dentptr;
524         int i;
525
526         if (get_cluster (mydata, curclust, block,
527                  mydata->clust_size * SECTOR_SIZE) != 0) {
528             FAT_DPRINT ("Error: reading directory block\n");
529             return NULL;
530         }
531         dentptr = (dir_entry *) block;
532         for (i = 0; i < DIRENTSPERCLUST; i++) {
533             char s_name[14], l_name[256];
534
535             l_name[0] = '\0';
536             if ((dentptr->attr & ATTR_VOLUME)) {
537 #ifdef CONFIG_SUPPORT_VFAT
538                 if ((dentptr->attr & ATTR_VFAT) &&
539                     (dentptr->name[0] & 0x40)) {
540                     prevcksum = ((dir_slot *) dentptr)
541                             ->alias_checksum;
542                     get_vfatname (mydata, curclust, block,
543                                   dentptr, l_name);
544                     if (dols) {
545                         int isdir = (dentptr->attr & ATTR_DIR);
546                         char dirc;
547                         int doit = 0;
548
549                         if (isdir) {
550                             dirs++;
551                             dirc = '/';
552                             doit = 1;
553                         } else {
554                             dirc = ' ';
555                             if (l_name[0] != 0) {
556                                 files++;
557                                 doit = 1;
558                             }
559                         }
560                         if (doit) {
561                             if (dirc == ' ') {
562                                 printf (" %8ld   %s%c\n",
563                                         (long) FAT2CPU32 (dentptr->size),
564                                         l_name, dirc);
565                             } else {
566                                 printf ("            %s%c\n", l_name, dirc);
567                             }
568                         }
569                         dentptr++;
570                         continue;
571                     }
572                     FAT_DPRINT ("vfatname: |%s|\n", l_name);
573                 } else
574 #endif
575                 {
576                     /* Volume label or VFAT entry */
577                     dentptr++;
578                     continue;
579                 }
580             }
581             if (dentptr->name[0] == 0) {
582                 if (dols) {
583                     printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
584                 }
585                 FAT_DPRINT ("Dentname == NULL - %d\n", i);
586                 return NULL;
587             }
588 #ifdef CONFIG_SUPPORT_VFAT
589             if (dols && mkcksum (dentptr->name) == prevcksum) {
590                 dentptr++;
591                 continue;
592             }
593 #endif
594             get_name (dentptr, s_name);
595             if (dols) {
596                 int isdir = (dentptr->attr & ATTR_DIR);
597                 char dirc;
598                 int doit = 0;
599
600                 if (isdir) {
601                     dirs++;
602                     dirc = '/';
603                     doit = 1;
604                 } else {
605                     dirc = ' ';
606                     if (s_name[0] != 0) {
607                         files++;
608                         doit = 1;
609                     }
610                 }
611                 if (doit) {
612                     if (dirc == ' ') {
613                         printf (" %8ld   %s%c\n",
614                                 (long) FAT2CPU32 (dentptr->size), s_name,
615                                 dirc);
616                     } else {
617                         printf ("            %s%c\n", s_name, dirc);
618                     }
619                 }
620                 dentptr++;
621                 continue;
622             }
623             if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
624                 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
625                 dentptr++;
626                 continue;
627             }
628             memcpy (retdent, dentptr, sizeof (dir_entry));
629
630             FAT_DPRINT ("DentName: %s", s_name);
631             FAT_DPRINT (", start: 0x%x", START (dentptr));
632             FAT_DPRINT (", size:  0x%x %s\n",
633                         FAT2CPU32 (dentptr->size),
634                         (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
635
636             return retdent;
637         }
638         curclust = get_fatent (mydata, curclust);
639         if (curclust <= 0x0001 || curclust >= 0xfff0) {
640             FAT_DPRINT ("curclust: 0x%x\n", curclust);
641             FAT_ERROR ("Invalid FAT entry\n");
642             return NULL;
643         }
644     }
645
646     return NULL;
647 }
648
649
650 /*
651  * Read boot sector and volume info from a FAT filesystem
652  */
653 static int
654 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
655 {
656         __u8 block[FS_BLOCK_SIZE];
657         volume_info *vistart;
658
659         if (disk_read(0, 1, block) < 0) {
660                 FAT_DPRINT("Error: reading block\n");
661                 return -1;
662         }
663
664         memcpy(bs, block, sizeof(boot_sector));
665         bs->reserved    = FAT2CPU16(bs->reserved);
666         bs->fat_length  = FAT2CPU16(bs->fat_length);
667         bs->secs_track  = FAT2CPU16(bs->secs_track);
668         bs->heads       = FAT2CPU16(bs->heads);
669 #if 0 /* UNUSED */
670         bs->hidden      = FAT2CPU32(bs->hidden);
671 #endif
672         bs->total_sect  = FAT2CPU32(bs->total_sect);
673
674         /* FAT32 entries */
675         if (bs->fat_length == 0) {
676                 /* Assume FAT32 */
677                 bs->fat32_length = FAT2CPU32(bs->fat32_length);
678                 bs->flags        = FAT2CPU16(bs->flags);
679                 bs->root_cluster = FAT2CPU32(bs->root_cluster);
680                 bs->info_sector  = FAT2CPU16(bs->info_sector);
681                 bs->backup_boot  = FAT2CPU16(bs->backup_boot);
682                 vistart = (volume_info*) (block + sizeof(boot_sector));
683                 *fatsize = 32;
684         } else {
685                 vistart = (volume_info*) &(bs->fat32_length);
686                 *fatsize = 0;
687         }
688         memcpy(volinfo, vistart, sizeof(volume_info));
689
690         /* Terminate fs_type string. Writing past the end of vistart
691            is ok - it's just the buffer. */
692         vistart->fs_type[8] = '\0';
693
694         if (*fatsize == 32) {
695                 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
696                         return 0;
697                 }
698         } else {
699                 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
700                         *fatsize = 12;
701                         return 0;
702                 }
703                 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
704                         *fatsize = 16;
705                         return 0;
706                 }
707         }
708
709         FAT_DPRINT("Error: broken fs_type sign\n");
710         return -1;
711 }
712
713
714 static long
715 do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
716              int dols)
717 {
718     __u8 block[MAX_CLUSTSIZE];  /* Block buffer */
719     char fnamecopy[2048];
720     boot_sector bs;
721     volume_info volinfo;
722     fsdata datablock;
723     fsdata *mydata = &datablock;
724     dir_entry *dentptr;
725     __u16 prevcksum = 0xffff;
726     char *subname = "";
727     int rootdir_size, cursect;
728     int idx, isdir = 0;
729     int files = 0, dirs = 0;
730     long ret = 0;
731     int firsttime;
732
733     if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
734         FAT_DPRINT ("Error: reading boot sector\n");
735         return -1;
736     }
737     if (mydata->fatsize == 32) {
738         mydata->fatlength = bs.fat32_length;
739     } else {
740         mydata->fatlength = bs.fat_length;
741     }
742     mydata->fat_sect = bs.reserved;
743     cursect = mydata->rootdir_sect
744             = mydata->fat_sect + mydata->fatlength * bs.fats;
745     mydata->clust_size = bs.cluster_size;
746     if (mydata->fatsize == 32) {
747         rootdir_size = mydata->clust_size;
748         mydata->data_begin = mydata->rootdir_sect   /* + rootdir_size */
749                 - (mydata->clust_size * 2);
750     } else {
751         rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
752                         * sizeof (dir_entry)) / SECTOR_SIZE;
753         mydata->data_begin = mydata->rootdir_sect + rootdir_size
754                 - (mydata->clust_size * 2);
755     }
756     mydata->fatbufnum = -1;
757
758     FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
759                 mydata->fatlength);
760     FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
761                 "Data begins at: %d\n",
762                 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
763                 rootdir_size, mydata->data_begin);
764     FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
765
766     /* "cwd" is always the root... */
767     while (ISDIRDELIM (*filename))
768         filename++;
769     /* Make a copy of the filename and convert it to lowercase */
770     strcpy (fnamecopy, filename);
771     downcase (fnamecopy);
772     if (*fnamecopy == '\0') {
773         if (!dols)
774             return -1;
775         dols = LS_ROOT;
776     } else if ((idx = dirdelim (fnamecopy)) >= 0) {
777         isdir = 1;
778         fnamecopy[idx] = '\0';
779         subname = fnamecopy + idx + 1;
780         /* Handle multiple delimiters */
781         while (ISDIRDELIM (*subname))
782             subname++;
783     } else if (dols) {
784         isdir = 1;
785     }
786
787     while (1) {
788         int i;
789
790         if (disk_read (cursect, mydata->clust_size, block) < 0) {
791             FAT_DPRINT ("Error: reading rootdir block\n");
792             return -1;
793         }
794         dentptr = (dir_entry *) block;
795         for (i = 0; i < DIRENTSPERBLOCK; i++) {
796             char s_name[14], l_name[256];
797
798             l_name[0] = '\0';
799             if ((dentptr->attr & ATTR_VOLUME)) {
800 #ifdef CONFIG_SUPPORT_VFAT
801                 if ((dentptr->attr & ATTR_VFAT) &&
802                     (dentptr->name[0] & 0x40)) {
803                     prevcksum = ((dir_slot *) dentptr)->alias_checksum;
804                     get_vfatname (mydata, 0, block, dentptr, l_name);
805                     if (dols == LS_ROOT) {
806                         int isdir = (dentptr->attr & ATTR_DIR);
807                         char dirc;
808                         int doit = 0;
809
810                         if (isdir) {
811                             dirs++;
812                             dirc = '/';
813                             doit = 1;
814                         } else {
815                             dirc = ' ';
816                             if (l_name[0] != 0) {
817                                 files++;
818                                 doit = 1;
819                             }
820                         }
821                         if (doit) {
822                             if (dirc == ' ') {
823                                 printf (" %8ld   %s%c\n",
824                                         (long) FAT2CPU32 (dentptr->size),
825                                         l_name, dirc);
826                             } else {
827                                 printf ("            %s%c\n", l_name, dirc);
828                             }
829                         }
830                         dentptr++;
831                         continue;
832                     }
833                     FAT_DPRINT ("Rootvfatname: |%s|\n", l_name);
834                 } else
835 #endif
836                 {
837                     /* Volume label or VFAT entry */
838                     dentptr++;
839                     continue;
840                 }
841             } else if (dentptr->name[0] == 0) {
842                 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
843                 if (dols == LS_ROOT) {
844                     printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
845                     return 0;
846                 }
847                 return -1;
848             }
849 #ifdef CONFIG_SUPPORT_VFAT
850             else if (dols == LS_ROOT
851                      && mkcksum (dentptr->name) == prevcksum) {
852                 dentptr++;
853                 continue;
854             }
855 #endif
856             get_name (dentptr, s_name);
857             if (dols == LS_ROOT) {
858                 int isdir = (dentptr->attr & ATTR_DIR);
859                 char dirc;
860                 int doit = 0;
861
862                 if (isdir) {
863                     dirc = '/';
864                     if (s_name[0] != 0) {
865                         dirs++;
866                         doit = 1;
867                     }
868                 } else {
869                     dirc = ' ';
870                     if (s_name[0] != 0) {
871                         files++;
872                         doit = 1;
873                     }
874                 }
875                 if (doit) {
876                     if (dirc == ' ') {
877                         printf (" %8ld   %s%c\n",
878                                 (long) FAT2CPU32 (dentptr->size), s_name,
879                                 dirc);
880                     } else {
881                         printf ("            %s%c\n", s_name, dirc);
882                     }
883                 }
884                 dentptr++;
885                 continue;
886             }
887             if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
888                 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
889                 dentptr++;
890                 continue;
891             }
892             if (isdir && !(dentptr->attr & ATTR_DIR))
893                 return -1;
894
895             FAT_DPRINT ("RootName: %s", s_name);
896             FAT_DPRINT (", start: 0x%x", START (dentptr));
897             FAT_DPRINT (", size:  0x%x %s\n",
898                         FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
899
900             goto rootdir_done;  /* We got a match */
901         }
902         cursect++;
903     }
904   rootdir_done:
905
906     firsttime = 1;
907     while (isdir) {
908         int startsect = mydata->data_begin
909                 + START (dentptr) * mydata->clust_size;
910         dir_entry dent;
911         char *nextname = NULL;
912
913         dent = *dentptr;
914         dentptr = &dent;
915
916         idx = dirdelim (subname);
917         if (idx >= 0) {
918             subname[idx] = '\0';
919             nextname = subname + idx + 1;
920             /* Handle multiple delimiters */
921             while (ISDIRDELIM (*nextname))
922                 nextname++;
923             if (dols && *nextname == '\0')
924                 firsttime = 0;
925         } else {
926             if (dols && firsttime) {
927                 firsttime = 0;
928             } else {
929                 isdir = 0;
930             }
931         }
932
933         if (get_dentfromdir (mydata, startsect, subname, dentptr,
934                              isdir ? 0 : dols) == NULL) {
935             if (dols && !isdir)
936                 return 0;
937             return -1;
938         }
939
940         if (idx >= 0) {
941             if (!(dentptr->attr & ATTR_DIR))
942                 return -1;
943             subname = nextname;
944         }
945     }
946     ret = get_contents (mydata, dentptr, buffer, maxsize);
947     FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
948
949     return ret;
950 }
951
952
953 int
954 file_fat_detectfs(void)
955 {
956         boot_sector     bs;
957         volume_info     volinfo;
958         int             fatsize;
959         char    vol_label[12];
960
961         if(cur_dev==NULL) {
962                 printf("No current device\n");
963                 return 1;
964         }
965 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI)
966         printf("Interface:  ");
967         switch(cur_dev->if_type) {
968                 case IF_TYPE_IDE :      printf("IDE"); break;
969                 case IF_TYPE_SCSI :     printf("SCSI"); break;
970                 case IF_TYPE_ATAPI :    printf("ATAPI"); break;
971                 case IF_TYPE_USB :      printf("USB"); break;
972                 case IF_TYPE_DOC :      printf("DOC"); break;
973                 case IF_TYPE_MMC :      printf("MMC"); break;
974                 default :               printf("Unknown");
975         }
976         printf("\n  Device %d: ",cur_dev->dev);
977         dev_print(cur_dev);
978 #endif
979         if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
980                 printf("\nNo valid FAT fs found\n");
981                 return 1;
982         }
983         memcpy (vol_label, volinfo.volume_label, 11);
984         vol_label[11] = '\0';
985         volinfo.fs_type[5]='\0';
986         printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
987         return 0;
988 }
989
990
991 int
992 file_fat_ls(const char *dir)
993 {
994         return do_fat_read(dir, NULL, 0, LS_YES);
995 }
996
997
998 long
999 file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1000 {
1001         printf("reading %s\n",filename);
1002         return do_fat_read(filename, buffer, maxsize, LS_NO);
1003 }
1004
1005 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */