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