Add some missing function prototypes
[pandora-x-loader.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 <part.h>
31 #include <fat.h>
32 #include <asm/byteorder.h>
33
34 #ifdef 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      0x52
55
56 int strncmp(const char * cs,const char * ct,size_t count)
57 {
58         register signed char __res = 0;
59
60         while (count) {
61                 if ((__res = *cs - *ct++) != 0 || !*cs++)
62                         break;
63                 count--;
64         }
65
66         return __res;
67 }
68
69 char * strcpy(char * dest,const char *src)
70 {
71         char *tmp = dest;
72
73         while ((*dest++ = *src++) != '\0')
74                 /* nothing */;
75         return tmp;
76 }
77
78 int strcmp(const char * cs,const char * ct)
79 {
80         register signed char __res;
81
82         while (1) {
83                 if ((__res = *cs - *ct++) != 0 || !*cs++)
84                         break;
85         }
86
87         return __res;
88 }
89 void * memcpy(void * dest,const void *src,size_t count)
90 {
91         char *tmp = (char *) dest, *s = (char *) src;
92
93         while (count--)
94                 *tmp++ = *s++;
95
96         return dest;
97 }
98
99
100 int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
101 {
102         startblock += part_offset;
103         if (cur_dev == NULL)
104                 return -1;
105         if (cur_dev->block_read) {
106                 return cur_dev->block_read (cur_dev->dev, startblock, getsize, (unsigned long *)bufptr);
107         }
108         return -1;
109 }
110
111
112 int
113 fat_register_device(block_dev_desc_t *dev_desc, int part_no)
114 {
115         unsigned char buffer[SECTOR_SIZE];
116
117         if (!dev_desc->block_read)
118                 return -1;
119         cur_dev=dev_desc;
120         /* check if we have a MBR (on floppies we have only a PBR) */
121         if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
122                 printf ("** Can't read from device %d **\n", dev_desc->dev);
123                 return -1;
124         }
125         if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
126                 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
127                 /* no signature found */
128                 return -1;
129         }
130         if(!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
131                 /* ok, we assume we are on a PBR only */
132                 cur_part = 1;
133                 part_offset=0;
134         }
135         else {
136 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
137     (CONFIG_COMMANDS & CFG_CMD_USB) || (CONFIG_COMMANDS & CFG_CMD_MMC) || defined(CONFIG_SYSTEMACE)
138                 disk_partition_t info;
139                 if(!get_partition_info(dev_desc, part_no, &info)) {
140                         part_offset = info.start;
141                         cur_part = part_no;
142                 }
143                 else {
144                         printf ("** Partition %d not valid on device %d **\n",part_no,dev_desc->dev);
145                         return -1;
146                 }
147 #else
148                 part_offset = buffer[DOS_PART_TBL_OFFSET+8]      |
149                               buffer[DOS_PART_TBL_OFFSET+9] <<8  |
150                               buffer[DOS_PART_TBL_OFFSET+10]<<16 |
151                               buffer[DOS_PART_TBL_OFFSET+11]<<24;
152
153                 cur_part = 1;
154 #endif
155         }
156         return 0;
157 }
158
159
160 /*
161  * Get the first occurence of a directory delimiter ('/' or '\') in a string.
162  * Return index into string if found, -1 otherwise.
163  */
164 static int
165 dirdelim(char *str)
166 {
167         char *start = str;
168
169         while (*str != '\0') {
170                 if (ISDIRDELIM(*str)) return str - start;
171                 str++;
172         }
173         return -1;
174 }
175
176
177 /*
178  * Match volume_info fs_type strings.
179  * Return 0 on match, -1 otherwise.
180  */
181 static int
182 compare_sign(char *str1, char *str2)
183 {
184         char *end = str1+SIGNLEN;
185         
186         while (str1 != end) {
187                 if (*str1 != *str2) {
188                         return -1;
189                 }
190                 str1++;
191                 str2++;
192         }
193
194         return 0;
195 }
196
197
198 /*
199  * Extract zero terminated short name from a directory entry.
200  */
201 static void get_name (dir_entry *dirent, char *s_name)
202 {
203         char *ptr;
204
205         memcpy (s_name, dirent->name, 8);
206         s_name[8] = '\0';
207         ptr = s_name;
208         while (*ptr && *ptr != ' ')
209                 ptr++;
210         if (dirent->ext[0] && dirent->ext[0] != ' ') {
211                 *ptr = '.';
212                 ptr++;
213                 memcpy (ptr, dirent->ext, 3);
214                 ptr[3] = '\0';
215                 while (*ptr && *ptr != ' ')
216                         ptr++;
217         }
218         *ptr = '\0';
219         if (*s_name == DELETED_FLAG)
220                 *s_name = '\0';
221         else if (*s_name == aRING)
222                 *s_name = 'Ã¥';
223         downcase (s_name);
224 }
225
226 /*
227  * Get the entry at index 'entry' in a FAT (12/16/32) table.
228  * On failure 0x00 is returned.
229  */
230 static __u32
231 get_fatent(fsdata *mydata, __u32 entry)
232 {
233         __u32 bufnum;
234         __u32 offset;
235         __u32 ret = 0x00;
236
237         switch (mydata->fatsize) {
238         case 32:
239                 bufnum = entry / FAT32BUFSIZE;
240                 offset = entry - bufnum * FAT32BUFSIZE;
241                 break;
242         case 16:
243                 bufnum = entry / FAT16BUFSIZE;
244                 offset = entry - bufnum * FAT16BUFSIZE;
245                 break;
246         case 12:
247                 bufnum = entry / FAT12BUFSIZE;
248                 offset = entry - bufnum * FAT12BUFSIZE;
249                 break;
250
251         default:
252                 /* Unsupported FAT size */
253                 return ret;
254         }
255         /* Read a new block of FAT entries into the cache. */
256         if (bufnum != mydata->fatbufnum) {
257                 int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
258                 __u8 *bufptr = (__u8 *)mydata->fatbuf;
259                 __u32 fatlength = mydata->fatlength;
260                 __u32 startblock = bufnum * FATBUFBLOCKS;
261
262                 fatlength *= SECTOR_SIZE;       /* We want it in bytes now */
263                 startblock += mydata->fat_sect; /* Offset from start of disk */
264
265                 if (getsize > fatlength) getsize = fatlength;
266                 if (disk_read(startblock, getsize, bufptr) < 0) {
267                         FAT_DPRINT("Error reading FAT blocks\n");
268                         return ret;
269                 }
270                 mydata->fatbufnum = bufnum;
271         }
272
273         /* Get the actual entry from the table */
274         switch (mydata->fatsize) {
275         case 32:
276                 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
277                 break;
278         case 16:
279                 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
280                 break;
281         case 12: {
282                 __u32 off16 = (offset*3)/4;
283                 __u16 val1, val2;
284
285                 switch (offset & 0x3) {
286                 case 0:
287                         ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
288                         ret &= 0xfff;
289                         break;
290                 case 1:
291                         val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
292                         val1 &= 0xf000;
293                         val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
294                         val2 &= 0x00ff;
295                         ret = (val2 << 4) | (val1 >> 12);
296                         break;
297                 case 2:
298                         val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
299                         val1 &= 0xff00;
300                         val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
301                         val2 &= 0x000f;
302                         ret = (val2 << 8) | (val1 >> 8);
303                         break;
304                 case 3:
305                         ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
306                         ret = (ret & 0xfff0) >> 4;
307                         break;
308                 default:
309                         break;
310                 }
311         }
312         break;
313         }
314         FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
315
316         return ret;
317 }
318
319
320 /*
321  * Read at most 'size' bytes from the specified cluster into 'buffer'.
322  * Return 0 on success, -1 otherwise.
323  */
324 static int
325 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
326 {
327         int idx = 0;
328         __u32 startsect;
329
330         if (clustnum > 0) {
331                 startsect = mydata->data_begin + clustnum*mydata->clust_size;
332         } else {
333                 startsect = mydata->rootdir_sect;
334         }
335
336         if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
337                 FAT_DPRINT("Error reading data\n");
338                 return -1;
339         }
340         if(size % FS_BLOCK_SIZE) {
341                 __u8 tmpbuf[FS_BLOCK_SIZE];
342                 idx= size/FS_BLOCK_SIZE;
343                 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
344                         FAT_DPRINT("Error reading data\n");
345                         return -1;
346                 }
347                 buffer += idx*FS_BLOCK_SIZE;
348
349                 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
350                 return 0;
351         }
352
353         return 0;
354 }
355
356
357 /*
358  * Read at most 'maxsize' bytes from the file associated with 'dentptr'
359  * into 'buffer'.
360  * Return the number of bytes read or -1 on fatal errors.
361  */
362 static long
363 get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
364              unsigned long maxsize)
365 {
366         unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
367         unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
368         __u32 curclust = START(dentptr);
369         __u32 endclust, newclust;
370         unsigned long actsize;
371
372         FAT_DPRINT("Filesize: %ld bytes\n", filesize);
373
374         if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
375
376         FAT_DPRINT("Reading: %ld bytes\n", filesize);
377
378         actsize=bytesperclust;
379         endclust=curclust;
380         do {
381                 /* search for consecutive clusters */
382                 while(actsize < filesize) {
383                         newclust = get_fatent(mydata, endclust);
384                         if((newclust -1)!=endclust)
385                                 goto getit;
386                         if (newclust <= 0x0001 || newclust >= 0xfffff0) {
387                                 FAT_DPRINT("curclust: 0x%x\n", newclust);
388                                 FAT_DPRINT("Invalid FAT entry\n");
389                                 return gotsize;
390                         }
391                         endclust=newclust;
392                         actsize+= bytesperclust;
393                 }
394                 /* actsize >= file size */
395                 actsize -= bytesperclust;
396                 /* get remaining clusters */
397                 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
398                         FAT_ERROR("Error reading cluster\n");
399                         return -1;
400                 }
401                 /* get remaining bytes */
402                 gotsize += (int)actsize;
403                 filesize -= actsize;
404                 buffer += actsize;
405                 actsize= filesize;
406                 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
407                         FAT_ERROR("Error reading cluster\n");
408                         return -1;
409                 }
410                 gotsize+=actsize;
411                 return gotsize;
412 getit:
413                 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
414                         FAT_ERROR("Error reading cluster\n");
415                         return -1;
416                 }
417                 gotsize += (int)actsize;
418                 filesize -= actsize;
419                 buffer += actsize;
420                 curclust = get_fatent(mydata, endclust);
421                 if (curclust <= 0x0001 || curclust >= 0xfffff0) {
422                         FAT_DPRINT("curclust: 0x%x\n", curclust);
423                         FAT_ERROR("Invalid FAT entry\n");
424                         return gotsize;
425                 }
426                 actsize=bytesperclust;
427                 endclust=curclust;
428         } while (1);
429 }
430
431
432 #ifdef CONFIG_SUPPORT_VFAT
433
434 /* Calculate short name checksum */
435 static __u8
436 mkcksum(const char *str)
437 {
438         int i;
439         __u8 ret = 0;
440
441         for (i = 0; i < 11; i++) {
442                 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
443         }
444
445         return ret;
446 }
447 #endif
448
449
450 /*
451  * Get the directory entry associated with 'filename' from the directory
452  * starting at 'startsect'
453  */
454
455
456 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
457                                    char *filename, dir_entry * retdent,
458                                    int dols)
459 {
460         return NULL;
461 }
462
463 #if 0
464 __u8 get_dentfromdir_block[MAX_CLUSTSIZE];
465 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
466                                    char *filename, dir_entry * retdent,
467                                    int dols)
468 {
469     __u16 prevcksum = 0xffff;
470     __u32 curclust = START (retdent);
471     int files = 0, dirs = 0;
472
473     while (1) {
474         dir_entry *dentptr;
475         int i;
476
477         if (get_cluster (mydata, curclust, get_dentfromdir_block,
478                  mydata->clust_size * SECTOR_SIZE) != 0) {
479             FAT_DPRINT ("Error: reading directory block\n");
480             return NULL;
481         }
482         dentptr = (dir_entry *) get_dentfromdir_block;
483         for (i = 0; i < DIRENTSPERCLUST; i++) {
484             char s_name[14], l_name[256];
485
486             l_name[0] = '\0';
487             if (dentptr->name[0] == DELETED_FLAG) {
488                     dentptr++;
489                     continue;
490             }
491             if ((dentptr->attr & ATTR_VOLUME)) {
492                     /* Volume label or VFAT entry */
493                     dentptr++;
494                     continue;
495             }
496             if (dentptr->name[0] == 0) {
497                 if (dols) {
498                     printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
499                 }
500                 FAT_DPRINT ("Dentname == NULL - %d\n", i);
501                 return NULL;
502             }
503 #ifdef CONFIG_SUPPORT_VFAT
504             if (dols && mkcksum (dentptr->name) == prevcksum) {
505                 dentptr++;
506                 continue;
507             }
508 #endif
509             get_name (dentptr, s_name);
510             if (dols) {
511                 int isdir = (dentptr->attr & ATTR_DIR);
512                 char dirc;
513                 int doit = 0;
514
515                 if (isdir) {
516                     dirs++;
517                     dirc = '/';
518                     doit = 1;
519                 } else {
520                     dirc = ' ';
521                     if (s_name[0] != 0) {
522                         files++;
523                         doit = 1;
524                     }
525                 }
526                 if (doit) {
527                     if (dirc == ' ') {
528                         printf (" %8ld   %s%c\n",
529                                 (long) FAT2CPU32 (dentptr->size), s_name,
530                                 dirc);
531                     } else {
532                         printf ("            %s%c\n", s_name, dirc);
533                     }
534                 }
535                 dentptr++;
536                 continue;
537             }
538             if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
539                 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
540                 dentptr++;
541                 continue;
542             }
543             memcpy (retdent, dentptr, sizeof (dir_entry));
544
545             FAT_DPRINT ("DentName: %s", s_name);
546             FAT_DPRINT (", start: 0x%x", START (dentptr));
547             FAT_DPRINT (", size:  0x%x %s\n",
548                         FAT2CPU32 (dentptr->size),
549                         (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
550
551             return retdent;
552         }
553         curclust = get_fatent (mydata, curclust);
554         if (curclust <= 0x0001 || curclust >= 0xfffff0) {
555             FAT_DPRINT ("curclust: 0x%x\n", curclust);
556             FAT_ERROR ("Invalid FAT entry\n");
557             return NULL;
558         }
559     }
560
561     return NULL;
562 }
563 #endif
564
565
566 /*
567  * Read boot sector and volume info from a FAT filesystem
568  */
569 static int
570 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
571 {
572         __u8 block[FS_BLOCK_SIZE];
573         volume_info *vistart;
574         char *p;
575
576         printf("Reading boot sector\n");
577
578         if (disk_read(0, 1, block) < 0) {
579                 FAT_DPRINT("Error: reading block\n");
580                 return -1;
581         }
582
583         memcpy(bs, block, sizeof(boot_sector));
584         bs->reserved    = FAT2CPU16(bs->reserved);
585         bs->fat_length  = FAT2CPU16(bs->fat_length);
586         bs->secs_track  = FAT2CPU16(bs->secs_track);
587         bs->heads       = FAT2CPU16(bs->heads);
588 #if 0 /* UNUSED */
589         bs->hidden      = FAT2CPU32(bs->hidden);
590 #endif
591         bs->total_sect  = FAT2CPU32(bs->total_sect);
592
593         /* FAT32 entries */
594         if (bs->fat_length == 0) {
595                 /* Assume FAT32 */
596                 bs->fat32_length = FAT2CPU32(bs->fat32_length);
597                 bs->flags        = FAT2CPU16(bs->flags);
598                 bs->root_cluster = FAT2CPU32(bs->root_cluster);
599                 bs->info_sector  = FAT2CPU16(bs->info_sector);
600                 bs->backup_boot  = FAT2CPU16(bs->backup_boot);
601                 vistart = (volume_info*) (block + sizeof(boot_sector));
602                 *fatsize = 32;
603         } else {
604                 vistart = (volume_info*) &(bs->fat32_length);
605                 *fatsize = 0;
606         }
607         memcpy(volinfo, vistart, sizeof(volume_info));
608
609         /* Terminate fs_type string. Writing past the end of vistart
610            is ok - it's just the buffer. */
611         p = (char *)&vistart->fs_type[0];
612         p[8] = '\0';
613
614         if (*fatsize == 32) {
615                 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
616                         return 0;
617                 }
618         } else {
619                 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
620                         *fatsize = 12;
621                         return 0;
622                 }
623                 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
624                         *fatsize = 16;
625                         return 0;
626                 }
627         }
628
629         FAT_DPRINT("Error: broken fs_type sign\n");
630         return -1;
631 }
632
633 #if 0
634 __u8 do_fat_read_block[MAX_CLUSTSIZE];  /* Block buffer */
635 #endif
636
637 __u8 *fnamecopy = (__u8 *)0x80500000;
638 __u8 *do_fat_read_block = (__u8 *)0x80500880;
639
640 boot_sector bs;
641 volume_info volinfo;
642 fsdata datablock;
643
644 long
645 do_fat_read(const char *filename, void *buffer, unsigned long maxsize,
646              int dols)
647 {
648 #if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
649     static
650 #endif
651     fsdata *mydata = &datablock;
652     dir_entry *dentptr;
653     __u16 prevcksum = 0xffff;
654     char *subname = "";
655     int rootdir_size, cursect, curclus;
656     int idx, isdir = 0;
657     int files = 0, dirs = 0;
658     long ret = 0;
659     int firsttime;
660
661     if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
662         printf ("Error: reading boot sector\n");
663         return -1;
664     }
665     if (mydata->fatsize == 32) {
666         mydata->fatlength = bs.fat32_length;
667     } else {
668         mydata->fatlength = bs.fat_length;
669     }
670     mydata->fat_sect = bs.reserved;
671     cursect = mydata->rootdir_sect
672             = mydata->fat_sect + mydata->fatlength * bs.fats;
673     curclus = bs.root_cluster;   // For FAT32 only
674     mydata->clust_size = bs.cluster_size;
675     if (mydata->fatsize == 32) {
676         rootdir_size = mydata->clust_size;
677         mydata->data_begin = mydata->rootdir_sect   /* + rootdir_size */
678                 - (mydata->clust_size * 2);
679     } else {
680         rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
681                         * sizeof (dir_entry)) / SECTOR_SIZE;
682         mydata->data_begin = mydata->rootdir_sect + rootdir_size
683                 - (mydata->clust_size * 2);
684     }
685     mydata->fatbufnum = -1;
686
687     FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
688                 mydata->fatlength);
689     FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
690                 "Data begins at: %d\n",
691                 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
692                 rootdir_size, mydata->data_begin);
693     FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
694
695     /* "cwd" is always the root... */
696     while (ISDIRDELIM (*filename))
697         filename++;
698     /* Make a copy of the filename and convert it to lowercase */
699     strcpy ((char *)fnamecopy, filename);
700     downcase ((char *)fnamecopy);
701     if (*fnamecopy == '\0') {
702         if (!dols){
703                 printf("\n not there\n");
704             return -1;
705         }
706         dols = LS_ROOT;
707     } else if ((idx = dirdelim ((char *)fnamecopy)) >= 0) {
708         isdir = 1;
709         fnamecopy[idx] = '\0';
710         subname = (char *)fnamecopy + idx + 1;
711         /* Handle multiple delimiters */
712         while (ISDIRDELIM (*subname))
713             subname++;
714     } else if (dols) {
715         isdir = 1;
716     }
717
718     while (1) {
719         int i;
720
721         if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
722             printf ("Error: reading rootdir block\n");
723             return -1;
724         }
725         dentptr = (dir_entry *) do_fat_read_block;
726         for (i = 0; i < DIRENTSPERBLOCK; i++) {
727             char s_name[14], l_name[256];
728
729             l_name[0] = '\0';
730             if ((dentptr->attr & ATTR_VOLUME)) {
731                     /* Volume label or VFAT entry */
732                     dentptr++;
733                     continue;
734             } else if (dentptr->name[0] == 0) {
735                 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
736                 if (dols == LS_ROOT) {
737                     printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
738                     return 0;
739                 }
740                 return -1;
741             }
742 #ifdef CONFIG_SUPPORT_VFAT
743             else if (dols == LS_ROOT
744                      && mkcksum (dentptr->name) == prevcksum) {
745                 dentptr++;
746                 continue;
747             }
748 #endif
749             get_name (dentptr, s_name);
750             if (dols == LS_ROOT) {
751                 int isdir = (dentptr->attr & ATTR_DIR);
752                 char dirc;
753                 int doit = 0;
754
755                 if (isdir) {
756                     dirc = '/';
757                     if (s_name[0] != 0) {
758                         dirs++;
759                         doit = 1;
760                     }
761                 } else {
762                     dirc = ' ';
763                     if (s_name[0] != 0) {
764                         files++;
765                         doit = 1;
766                     }
767                 }
768                 if (doit) {
769                     if (dirc == ' ') {
770                         printf (" %8ld   %s%c\n",
771                                 (long) FAT2CPU32 (dentptr->size), s_name,
772                                 dirc);
773                     } else {
774                         printf ("            %s%c\n", s_name, dirc);
775                     }
776                 }
777                 dentptr++;
778                 continue;
779             }
780             if (strcmp ((char *)fnamecopy, s_name) &&
781                                            strcmp ((char *)fnamecopy, l_name)) {
782                 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
783                 dentptr++;
784                 continue;
785             }
786             if (isdir && !(dentptr->attr & ATTR_DIR))
787                 return -1;
788
789             FAT_DPRINT ("RootName: %s", s_name);
790             FAT_DPRINT (", start: 0x%x", START (dentptr));
791             FAT_DPRINT (", size:  0x%x %s\n",
792                         FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
793
794             goto rootdir_done;  /* We got a match */
795         }
796
797         if (mydata->fatsize != 32)
798            cursect++;
799         else {
800            // FAT32 does not guarantee contiguous root directory
801            curclus = get_fatent (mydata, curclus);
802            cursect = (curclus * mydata->clust_size) + mydata->data_begin;
803
804            FAT_DPRINT ("root clus %d sector %d\n", curclus, cursect);
805         }
806     }
807   rootdir_done:
808
809     firsttime = 1;
810     while (isdir) {
811         int startsect = mydata->data_begin
812                 + START (dentptr) * mydata->clust_size;
813         dir_entry dent;
814         char *nextname = NULL;
815
816         dent = *dentptr;
817         dentptr = &dent;
818         idx = dirdelim (subname);
819         if (idx >= 0) {
820             subname[idx] = '\0';
821             nextname = subname + idx + 1;
822             /* Handle multiple delimiters */
823             while (ISDIRDELIM (*nextname))
824                 nextname++;
825             if (dols && *nextname == '\0')
826                 firsttime = 0;
827         } else {
828             if (dols && firsttime) {
829                 firsttime = 0;
830             } else {
831                 isdir = 0;
832             }
833         }
834
835         if (get_dentfromdir (mydata, startsect, subname, dentptr,
836                              isdir ? 0 : dols) == NULL) {
837             if (dols && !isdir)
838                 return 0;
839             return -1;
840         }
841
842         if (idx >= 0) {
843             if (!(dentptr->attr & ATTR_DIR))
844                 return -1;
845             subname = nextname;
846         }
847     }
848     ret = get_contents (mydata, dentptr, buffer, maxsize);
849     FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
850
851     return ret;
852 }
853
854
855 int
856 file_fat_detectfs(void)
857 {
858         boot_sector     bs;
859         volume_info     volinfo;
860         int             fatsize;
861         char    vol_label[12];
862
863         if(cur_dev==NULL) {
864                 printf("No current device\n");
865                 return 1;
866         }
867 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
868     (CONFIG_COMMANDS & CFG_CMD_USB) || (CONFIG_MMC)
869         printf("Interface:  ");
870         switch(cur_dev->if_type) {
871                 case IF_TYPE_IDE :      printf("IDE"); break;
872                 case IF_TYPE_SCSI :     printf("SCSI"); break;
873                 case IF_TYPE_ATAPI :    printf("ATAPI"); break;
874                 case IF_TYPE_USB :      printf("USB"); break;
875                 case IF_TYPE_DOC :      printf("DOC"); break;
876                 case IF_TYPE_MMC :      printf("MMC"); break;
877                 default :               printf("Unknown");
878         }
879         printf("\n  Device %d: ",cur_dev->dev);
880         dev_print(cur_dev);
881 #endif
882         if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
883                 printf("\nNo valid FAT fs found\n");
884                 return 1;
885         }
886         memcpy (vol_label, volinfo.volume_label, 11);
887         vol_label[11] = '\0';
888         volinfo.fs_type[5]='\0';
889         printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
890         return 0;
891 }
892
893
894 int
895 file_fat_ls(const char *dir)
896 {
897         return do_fat_read(dir, NULL, 0, LS_YES);
898 }
899
900
901 long
902 file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
903 {
904         long ret;
905         ret = do_fat_read(filename, buffer, maxsize, LS_NO);
906         return ret;
907 }
908
909 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */