* Patch by Thomas Elste, 10 Feb 2004:
[platform/kernel/u-boot.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 __u8     get_vfatname_block[MAX_CLUSTSIZE];
428 static int
429 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
430              dir_entry *retdent, char *l_name)
431 {
432         dir_entry *realdent;
433         dir_slot  *slotptr = (dir_slot*) retdent;
434         __u8      *nextclust = cluster + mydata->clust_size * SECTOR_SIZE;
435         __u8       counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
436         int idx = 0;
437
438         while ((__u8*)slotptr < nextclust) {
439                 if (counter == 0) break;
440                 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
441                         return -1;
442                 slotptr++;
443                 counter--;
444         }
445
446         if ((__u8*)slotptr >= nextclust) {
447                 dir_slot *slotptr2;
448
449                 slotptr--;
450                 curclust = get_fatent(mydata, curclust);
451                 if (curclust <= 0x0001 || curclust >= 0xfff0) {
452                         FAT_DPRINT("curclust: 0x%x\n", curclust);
453                         FAT_ERROR("Invalid FAT entry\n");
454                         return -1;
455                 }
456                 if (get_cluster(mydata, curclust, get_vfatname_block,
457                                 mydata->clust_size * SECTOR_SIZE) != 0) {
458                         FAT_DPRINT("Error: reading directory block\n");
459                         return -1;
460                 }
461                 slotptr2 = (dir_slot*) get_vfatname_block;
462                 while (slotptr2->id > 0x01) {
463                         slotptr2++;
464                 }
465                 /* Save the real directory entry */
466                 realdent = (dir_entry*)slotptr2 + 1;
467                 while ((__u8*)slotptr2 >= get_vfatname_block) {
468                         slot2str(slotptr2, l_name, &idx);
469                         slotptr2--;
470                 }
471         } else {
472                 /* Save the real directory entry */
473                 realdent = (dir_entry*)slotptr;
474         }
475
476         do {
477                 slotptr--;
478                 if (slot2str(slotptr, l_name, &idx)) break;
479         } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
480
481         l_name[idx] = '\0';
482         if (*l_name == DELETED_FLAG) *l_name = '\0';
483         else if (*l_name == aRING) *l_name = 'å';
484         downcase(l_name);
485
486         /* Return the real directory entry */
487         memcpy(retdent, realdent, sizeof(dir_entry));
488
489         return 0;
490 }
491
492
493 /* Calculate short name checksum */
494 static __u8
495 mkcksum(const char *str)
496 {
497         int i;
498         __u8 ret = 0;
499
500         for (i = 0; i < 11; i++) {
501                 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
502         }
503
504         return ret;
505 }
506 #endif
507
508
509 /*
510  * Get the directory entry associated with 'filename' from the directory
511  * starting at 'startsect'
512  */
513 __u8 get_dentfromdir_block[MAX_CLUSTSIZE];
514 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
515                                    char *filename, dir_entry * retdent,
516                                    int dols)
517 {
518     __u16 prevcksum = 0xffff;
519     __u32 curclust = START (retdent);
520     int files = 0, dirs = 0;
521
522     FAT_DPRINT ("get_dentfromdir: %s\n", filename);
523     while (1) {
524         dir_entry *dentptr;
525         int i;
526
527         if (get_cluster (mydata, curclust, get_dentfromdir_block,
528                  mydata->clust_size * SECTOR_SIZE) != 0) {
529             FAT_DPRINT ("Error: reading directory block\n");
530             return NULL;
531         }
532         dentptr = (dir_entry *) get_dentfromdir_block;
533         for (i = 0; i < DIRENTSPERCLUST; i++) {
534             char s_name[14], l_name[256];
535
536             l_name[0] = '\0';
537             if ((dentptr->attr & ATTR_VOLUME)) {
538 #ifdef CONFIG_SUPPORT_VFAT
539                 if ((dentptr->attr & ATTR_VFAT) &&
540                     (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
541                     prevcksum = ((dir_slot *) dentptr)
542                             ->alias_checksum;
543                     get_vfatname (mydata, curclust, get_dentfromdir_block,
544                                   dentptr, l_name);
545                     if (dols) {
546                         int isdir = (dentptr->attr & ATTR_DIR);
547                         char dirc;
548                         int doit = 0;
549
550                         if (isdir) {
551                             dirs++;
552                             dirc = '/';
553                             doit = 1;
554                         } else {
555                             dirc = ' ';
556                             if (l_name[0] != 0) {
557                                 files++;
558                                 doit = 1;
559                             }
560                         }
561                         if (doit) {
562                             if (dirc == ' ') {
563                                 printf (" %8ld   %s%c\n",
564                                         (long) FAT2CPU32 (dentptr->size),
565                                         l_name, dirc);
566                             } else {
567                                 printf ("            %s%c\n", l_name, dirc);
568                             }
569                         }
570                         dentptr++;
571                         continue;
572                     }
573                     FAT_DPRINT ("vfatname: |%s|\n", l_name);
574                 } else
575 #endif
576                 {
577                     /* Volume label or VFAT entry */
578                     dentptr++;
579                     continue;
580                 }
581             }
582             if (dentptr->name[0] == 0) {
583                 if (dols) {
584                     printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
585                 }
586                 FAT_DPRINT ("Dentname == NULL - %d\n", i);
587                 return NULL;
588             }
589 #ifdef CONFIG_SUPPORT_VFAT
590             if (dols && mkcksum (dentptr->name) == prevcksum) {
591                 dentptr++;
592                 continue;
593             }
594 #endif
595             get_name (dentptr, s_name);
596             if (dols) {
597                 int isdir = (dentptr->attr & ATTR_DIR);
598                 char dirc;
599                 int doit = 0;
600
601                 if (isdir) {
602                     dirs++;
603                     dirc = '/';
604                     doit = 1;
605                 } else {
606                     dirc = ' ';
607                     if (s_name[0] != 0) {
608                         files++;
609                         doit = 1;
610                     }
611                 }
612                 if (doit) {
613                     if (dirc == ' ') {
614                         printf (" %8ld   %s%c\n",
615                                 (long) FAT2CPU32 (dentptr->size), s_name,
616                                 dirc);
617                     } else {
618                         printf ("            %s%c\n", s_name, dirc);
619                     }
620                 }
621                 dentptr++;
622                 continue;
623             }
624             if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
625                 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
626                 dentptr++;
627                 continue;
628             }
629             memcpy (retdent, dentptr, sizeof (dir_entry));
630
631             FAT_DPRINT ("DentName: %s", s_name);
632             FAT_DPRINT (", start: 0x%x", START (dentptr));
633             FAT_DPRINT (", size:  0x%x %s\n",
634                         FAT2CPU32 (dentptr->size),
635                         (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
636
637             return retdent;
638         }
639         curclust = get_fatent (mydata, curclust);
640         if (curclust <= 0x0001 || curclust >= 0xfff0) {
641             FAT_DPRINT ("curclust: 0x%x\n", curclust);
642             FAT_ERROR ("Invalid FAT entry\n");
643             return NULL;
644         }
645     }
646
647     return NULL;
648 }
649
650
651 /*
652  * Read boot sector and volume info from a FAT filesystem
653  */
654 static int
655 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
656 {
657         __u8 block[FS_BLOCK_SIZE];
658         volume_info *vistart;
659
660         if (disk_read(0, 1, block) < 0) {
661                 FAT_DPRINT("Error: reading block\n");
662                 return -1;
663         }
664
665         memcpy(bs, block, sizeof(boot_sector));
666         bs->reserved    = FAT2CPU16(bs->reserved);
667         bs->fat_length  = FAT2CPU16(bs->fat_length);
668         bs->secs_track  = FAT2CPU16(bs->secs_track);
669         bs->heads       = FAT2CPU16(bs->heads);
670 #if 0 /* UNUSED */
671         bs->hidden      = FAT2CPU32(bs->hidden);
672 #endif
673         bs->total_sect  = FAT2CPU32(bs->total_sect);
674
675         /* FAT32 entries */
676         if (bs->fat_length == 0) {
677                 /* Assume FAT32 */
678                 bs->fat32_length = FAT2CPU32(bs->fat32_length);
679                 bs->flags        = FAT2CPU16(bs->flags);
680                 bs->root_cluster = FAT2CPU32(bs->root_cluster);
681                 bs->info_sector  = FAT2CPU16(bs->info_sector);
682                 bs->backup_boot  = FAT2CPU16(bs->backup_boot);
683                 vistart = (volume_info*) (block + sizeof(boot_sector));
684                 *fatsize = 32;
685         } else {
686                 vistart = (volume_info*) &(bs->fat32_length);
687                 *fatsize = 0;
688         }
689         memcpy(volinfo, vistart, sizeof(volume_info));
690
691         /* Terminate fs_type string. Writing past the end of vistart
692            is ok - it's just the buffer. */
693         vistart->fs_type[8] = '\0';
694
695         if (*fatsize == 32) {
696                 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
697                         return 0;
698                 }
699         } else {
700                 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
701                         *fatsize = 12;
702                         return 0;
703                 }
704                 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
705                         *fatsize = 16;
706                         return 0;
707                 }
708         }
709
710         FAT_DPRINT("Error: broken fs_type sign\n");
711         return -1;
712 }
713
714
715 __u8 do_fat_read_block[MAX_CLUSTSIZE];  /* Block buffer */
716 static long
717 do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
718              int dols)
719 {
720     char fnamecopy[2048];
721     boot_sector bs;
722     volume_info volinfo;
723     fsdata datablock;
724     fsdata *mydata = &datablock;
725     dir_entry *dentptr;
726     __u16 prevcksum = 0xffff;
727     char *subname = "";
728     int rootdir_size, cursect;
729     int idx, isdir = 0;
730     int files = 0, dirs = 0;
731     long ret = 0;
732     int firsttime;
733
734     if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
735         FAT_DPRINT ("Error: reading boot sector\n");
736         return -1;
737     }
738     if (mydata->fatsize == 32) {
739         mydata->fatlength = bs.fat32_length;
740     } else {
741         mydata->fatlength = bs.fat_length;
742     }
743     mydata->fat_sect = bs.reserved;
744     cursect = mydata->rootdir_sect
745             = mydata->fat_sect + mydata->fatlength * bs.fats;
746     mydata->clust_size = bs.cluster_size;
747     if (mydata->fatsize == 32) {
748         rootdir_size = mydata->clust_size;
749         mydata->data_begin = mydata->rootdir_sect   /* + rootdir_size */
750                 - (mydata->clust_size * 2);
751     } else {
752         rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
753                         * sizeof (dir_entry)) / SECTOR_SIZE;
754         mydata->data_begin = mydata->rootdir_sect + rootdir_size
755                 - (mydata->clust_size * 2);
756     }
757     mydata->fatbufnum = -1;
758
759     FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
760                 mydata->fatlength);
761     FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
762                 "Data begins at: %d\n",
763                 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
764                 rootdir_size, mydata->data_begin);
765     FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
766
767     /* "cwd" is always the root... */
768     while (ISDIRDELIM (*filename))
769         filename++;
770     /* Make a copy of the filename and convert it to lowercase */
771     strcpy (fnamecopy, filename);
772     downcase (fnamecopy);
773     if (*fnamecopy == '\0') {
774         if (!dols)
775             return -1;
776         dols = LS_ROOT;
777     } else if ((idx = dirdelim (fnamecopy)) >= 0) {
778         isdir = 1;
779         fnamecopy[idx] = '\0';
780         subname = fnamecopy + idx + 1;
781         /* Handle multiple delimiters */
782         while (ISDIRDELIM (*subname))
783             subname++;
784     } else if (dols) {
785         isdir = 1;
786     }
787
788     while (1) {
789         int i;
790
791         if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
792             FAT_DPRINT ("Error: reading rootdir block\n");
793             return -1;
794         }
795         dentptr = (dir_entry *) do_fat_read_block;
796         for (i = 0; i < DIRENTSPERBLOCK; i++) {
797             char s_name[14], l_name[256];
798
799             l_name[0] = '\0';
800             if ((dentptr->attr & ATTR_VOLUME)) {
801 #ifdef CONFIG_SUPPORT_VFAT
802                 if ((dentptr->attr & ATTR_VFAT) &&
803                     (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
804                     prevcksum = ((dir_slot *) dentptr)->alias_checksum;
805                     get_vfatname (mydata, 0, do_fat_read_block, dentptr, l_name);
806                     if (dols == LS_ROOT) {
807                         int isdir = (dentptr->attr & ATTR_DIR);
808                         char dirc;
809                         int doit = 0;
810
811                         if (isdir) {
812                             dirs++;
813                             dirc = '/';
814                             doit = 1;
815                         } else {
816                             dirc = ' ';
817                             if (l_name[0] != 0) {
818                                 files++;
819                                 doit = 1;
820                             }
821                         }
822                         if (doit) {
823                             if (dirc == ' ') {
824                                 printf (" %8ld   %s%c\n",
825                                         (long) FAT2CPU32 (dentptr->size),
826                                         l_name, dirc);
827                             } else {
828                                 printf ("            %s%c\n", l_name, dirc);
829                             }
830                         }
831                         dentptr++;
832                         continue;
833                     }
834                     FAT_DPRINT ("Rootvfatname: |%s|\n", l_name);
835                 } else
836 #endif
837                 {
838                     /* Volume label or VFAT entry */
839                     dentptr++;
840                     continue;
841                 }
842             } else if (dentptr->name[0] == 0) {
843                 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
844                 if (dols == LS_ROOT) {
845                     printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
846                     return 0;
847                 }
848                 return -1;
849             }
850 #ifdef CONFIG_SUPPORT_VFAT
851             else if (dols == LS_ROOT
852                      && mkcksum (dentptr->name) == prevcksum) {
853                 dentptr++;
854                 continue;
855             }
856 #endif
857             get_name (dentptr, s_name);
858             if (dols == LS_ROOT) {
859                 int isdir = (dentptr->attr & ATTR_DIR);
860                 char dirc;
861                 int doit = 0;
862
863                 if (isdir) {
864                     dirc = '/';
865                     if (s_name[0] != 0) {
866                         dirs++;
867                         doit = 1;
868                     }
869                 } else {
870                     dirc = ' ';
871                     if (s_name[0] != 0) {
872                         files++;
873                         doit = 1;
874                     }
875                 }
876                 if (doit) {
877                     if (dirc == ' ') {
878                         printf (" %8ld   %s%c\n",
879                                 (long) FAT2CPU32 (dentptr->size), s_name,
880                                 dirc);
881                     } else {
882                         printf ("            %s%c\n", s_name, dirc);
883                     }
884                 }
885                 dentptr++;
886                 continue;
887             }
888             if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
889                 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
890                 dentptr++;
891                 continue;
892             }
893             if (isdir && !(dentptr->attr & ATTR_DIR))
894                 return -1;
895
896             FAT_DPRINT ("RootName: %s", s_name);
897             FAT_DPRINT (", start: 0x%x", START (dentptr));
898             FAT_DPRINT (", size:  0x%x %s\n",
899                         FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
900
901             goto rootdir_done;  /* We got a match */
902         }
903         cursect++;
904     }
905   rootdir_done:
906
907     firsttime = 1;
908     while (isdir) {
909         int startsect = mydata->data_begin
910                 + START (dentptr) * mydata->clust_size;
911         dir_entry dent;
912         char *nextname = NULL;
913
914         dent = *dentptr;
915         dentptr = &dent;
916
917         idx = dirdelim (subname);
918         if (idx >= 0) {
919             subname[idx] = '\0';
920             nextname = subname + idx + 1;
921             /* Handle multiple delimiters */
922             while (ISDIRDELIM (*nextname))
923                 nextname++;
924             if (dols && *nextname == '\0')
925                 firsttime = 0;
926         } else {
927             if (dols && firsttime) {
928                 firsttime = 0;
929             } else {
930                 isdir = 0;
931             }
932         }
933
934         if (get_dentfromdir (mydata, startsect, subname, dentptr,
935                              isdir ? 0 : dols) == NULL) {
936             if (dols && !isdir)
937                 return 0;
938             return -1;
939         }
940
941         if (idx >= 0) {
942             if (!(dentptr->attr & ATTR_DIR))
943                 return -1;
944             subname = nextname;
945         }
946     }
947     ret = get_contents (mydata, dentptr, buffer, maxsize);
948     FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
949
950     return ret;
951 }
952
953
954 int
955 file_fat_detectfs(void)
956 {
957         boot_sector     bs;
958         volume_info     volinfo;
959         int             fatsize;
960         char    vol_label[12];
961
962         if(cur_dev==NULL) {
963                 printf("No current device\n");
964                 return 1;
965         }
966 #if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
967     (CONFIG_COMMANDS & CFG_CMD_USB) || (CONFIG_MMC)
968         printf("Interface:  ");
969         switch(cur_dev->if_type) {
970                 case IF_TYPE_IDE :      printf("IDE"); break;
971                 case IF_TYPE_SCSI :     printf("SCSI"); break;
972                 case IF_TYPE_ATAPI :    printf("ATAPI"); break;
973                 case IF_TYPE_USB :      printf("USB"); break;
974                 case IF_TYPE_DOC :      printf("DOC"); break;
975                 case IF_TYPE_MMC :      printf("MMC"); break;
976                 default :               printf("Unknown");
977         }
978         printf("\n  Device %d: ",cur_dev->dev);
979         dev_print(cur_dev);
980 #endif
981         if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
982                 printf("\nNo valid FAT fs found\n");
983                 return 1;
984         }
985         memcpy (vol_label, volinfo.volume_label, 11);
986         vol_label[11] = '\0';
987         volinfo.fs_type[5]='\0';
988         printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
989         return 0;
990 }
991
992
993 int
994 file_fat_ls(const char *dir)
995 {
996         return do_fat_read(dir, NULL, 0, LS_YES);
997 }
998
999
1000 long
1001 file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1002 {
1003         printf("reading %s\n",filename);
1004         return do_fat_read(filename, buffer, maxsize, LS_NO);
1005 }
1006
1007 #endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */