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