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