xref: /openbmc/u-boot/fs/fat/fat.c (revision 047375bf)
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 defined(CONFIG_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
63 			, startblock, getsize, (unsigned long *)bufptr);
64 	}
65 	return -1;
66 }
67 
68 
69 int
70 fat_register_device(block_dev_desc_t *dev_desc, int part_no)
71 {
72 	unsigned char buffer[SECTOR_SIZE];
73 	disk_partition_t info;
74 
75 	if (!dev_desc->block_read)
76 		return -1;
77 	cur_dev = dev_desc;
78 	/* check if we have a MBR (on floppies we have only a PBR) */
79 	if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
80 		printf ("** Can't read from device %d **\n", dev_desc->dev);
81 		return -1;
82 	}
83 	if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
84 		buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
85 		/* no signature found */
86 		return -1;
87 	}
88 	if(!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
89 		/* ok, we assume we are on a PBR only */
90 		cur_part = 1;
91 		part_offset=0;
92 	} else {
93 #if (defined(CONFIG_CMD_IDE) || \
94      defined(CONFIG_CMD_SCSI) || \
95      defined(CONFIG_CMD_USB) || \
96      (defined(CONFIG_MMC) && defined(CONFIG_LPC2292)) || \
97      defined(CONFIG_SYSTEMACE)          )
98 		/* First we assume, there is a MBR */
99 		if (!get_partition_info (dev_desc, part_no, &info)) {
100 			part_offset = info.start;
101 			cur_part = part_no;
102 		} else if (!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3)) {
103 			/* ok, we assume we are on a PBR only */
104 			cur_part = 1;
105 			part_offset = 0;
106 		} else {
107 			printf ("** Partition %d not valid on device %d **\n",
108 				part_no, dev_desc->dev);
109 			return -1;
110 		}
111 #else
112 		if(!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
113 			/* ok, we assume we are on a PBR only */
114 			cur_part = 1;
115 			part_offset = 0;
116 			info.start = part_offset;
117 		} else {
118 			/* FIXME we need to determine the start block of the
119 			 * partition where the DOS FS resides. This can be done
120 			 * by using the get_partition_info routine. For this
121 			 * purpose the libpart must be included.
122 			 */
123 			part_offset = 32;
124 			cur_part = 1;
125 		}
126 #endif
127 	}
128 	return 0;
129 }
130 
131 
132 /*
133  * Get the first occurence of a directory delimiter ('/' or '\') in a string.
134  * Return index into string if found, -1 otherwise.
135  */
136 static int
137 dirdelim(char *str)
138 {
139 	char *start = str;
140 
141 	while (*str != '\0') {
142 		if (ISDIRDELIM(*str)) return str - start;
143 		str++;
144 	}
145 	return -1;
146 }
147 
148 
149 /*
150  * Match volume_info fs_type strings.
151  * Return 0 on match, -1 otherwise.
152  */
153 static int
154 compare_sign(char *str1, char *str2)
155 {
156 	char *end = str1+SIGNLEN;
157 
158 	while (str1 != end) {
159 		if (*str1 != *str2) {
160 			return -1;
161 		}
162 		str1++;
163 		str2++;
164 	}
165 
166 	return 0;
167 }
168 
169 
170 /*
171  * Extract zero terminated short name from a directory entry.
172  */
173 static void get_name (dir_entry *dirent, char *s_name)
174 {
175 	char *ptr;
176 
177 	memcpy (s_name, dirent->name, 8);
178 	s_name[8] = '\0';
179 	ptr = s_name;
180 	while (*ptr && *ptr != ' ')
181 		ptr++;
182 	if (dirent->ext[0] && dirent->ext[0] != ' ') {
183 		*ptr = '.';
184 		ptr++;
185 		memcpy (ptr, dirent->ext, 3);
186 		ptr[3] = '\0';
187 		while (*ptr && *ptr != ' ')
188 			ptr++;
189 	}
190 	*ptr = '\0';
191 	if (*s_name == DELETED_FLAG)
192 		*s_name = '\0';
193 	else if (*s_name == aRING)
194 		*s_name = '�';
195 	downcase (s_name);
196 }
197 
198 /*
199  * Get the entry at index 'entry' in a FAT (12/16/32) table.
200  * On failure 0x00 is returned.
201  */
202 static __u32
203 get_fatent(fsdata *mydata, __u32 entry)
204 {
205 	__u32 bufnum;
206 	__u32 offset;
207 	__u32 ret = 0x00;
208 
209 	switch (mydata->fatsize) {
210 	case 32:
211 		bufnum = entry / FAT32BUFSIZE;
212 		offset = entry - bufnum * FAT32BUFSIZE;
213 		break;
214 	case 16:
215 		bufnum = entry / FAT16BUFSIZE;
216 		offset = entry - bufnum * FAT16BUFSIZE;
217 		break;
218 	case 12:
219 		bufnum = entry / FAT12BUFSIZE;
220 		offset = entry - bufnum * FAT12BUFSIZE;
221 		break;
222 
223 	default:
224 		/* Unsupported FAT size */
225 		return ret;
226 	}
227 
228 	/* Read a new block of FAT entries into the cache. */
229 	if (bufnum != mydata->fatbufnum) {
230 		int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
231 		__u8 *bufptr = mydata->fatbuf;
232 		__u32 fatlength = mydata->fatlength;
233 		__u32 startblock = bufnum * FATBUFBLOCKS;
234 
235 		fatlength *= SECTOR_SIZE;	/* We want it in bytes now */
236 		startblock += mydata->fat_sect;	/* Offset from start of disk */
237 
238 		if (getsize > fatlength) getsize = fatlength;
239 		if (disk_read(startblock, getsize, bufptr) < 0) {
240 			FAT_DPRINT("Error reading FAT blocks\n");
241 			return ret;
242 		}
243 		mydata->fatbufnum = bufnum;
244 	}
245 
246 	/* Get the actual entry from the table */
247 	switch (mydata->fatsize) {
248 	case 32:
249 		ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
250 		break;
251 	case 16:
252 		ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
253 		break;
254 	case 12: {
255 		__u32 off16 = (offset*3)/4;
256 		__u16 val1, val2;
257 
258 		switch (offset & 0x3) {
259 		case 0:
260 			ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
261 			ret &= 0xfff;
262 			break;
263 		case 1:
264 			val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
265 			val1 &= 0xf000;
266 			val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
267 			val2 &= 0x00ff;
268 			ret = (val2 << 4) | (val1 >> 12);
269 			break;
270 		case 2:
271 			val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
272 			val1 &= 0xff00;
273 			val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
274 			val2 &= 0x000f;
275 			ret = (val2 << 8) | (val1 >> 8);
276 			break;
277 		case 3:
278 			ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
279 			ret = (ret & 0xfff0) >> 4;
280 			break;
281 		default:
282 			break;
283 		}
284 	}
285 	break;
286 	}
287 	FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
288 
289 	return ret;
290 }
291 
292 
293 /*
294  * Read at most 'size' bytes from the specified cluster into 'buffer'.
295  * Return 0 on success, -1 otherwise.
296  */
297 static int
298 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
299 {
300 	int idx = 0;
301 	__u32 startsect;
302 
303 	if (clustnum > 0) {
304 		startsect = mydata->data_begin + clustnum*mydata->clust_size;
305 	} else {
306 		startsect = mydata->rootdir_sect;
307 	}
308 
309 	FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
310 	if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
311 		FAT_DPRINT("Error reading data\n");
312 		return -1;
313 	}
314 	if(size % FS_BLOCK_SIZE) {
315 		__u8 tmpbuf[FS_BLOCK_SIZE];
316 		idx= size/FS_BLOCK_SIZE;
317 		if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
318 			FAT_DPRINT("Error reading data\n");
319 			return -1;
320 		}
321 		buffer += idx*FS_BLOCK_SIZE;
322 
323 		memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
324 		return 0;
325 	}
326 
327 	return 0;
328 }
329 
330 
331 /*
332  * Read at most 'maxsize' bytes from the file associated with 'dentptr'
333  * into 'buffer'.
334  * Return the number of bytes read or -1 on fatal errors.
335  */
336 static long
337 get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
338 	     unsigned long maxsize)
339 {
340 	unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
341 	unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
342 	__u32 curclust = START(dentptr);
343 	__u32 endclust, newclust;
344 	unsigned long actsize;
345 
346 	FAT_DPRINT("Filesize: %ld bytes\n", filesize);
347 
348 	if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
349 
350 	FAT_DPRINT("Reading: %ld bytes\n", filesize);
351 
352 	actsize=bytesperclust;
353 	endclust=curclust;
354 	do {
355 		/* search for consecutive clusters */
356 		while(actsize < filesize) {
357 			newclust = get_fatent(mydata, endclust);
358 			if((newclust -1)!=endclust)
359 				goto getit;
360 			if (newclust <= 0x0001 || newclust >= 0xfff0) {
361 				FAT_DPRINT("curclust: 0x%x\n", newclust);
362 				FAT_DPRINT("Invalid FAT entry\n");
363 				return gotsize;
364 			}
365 			endclust=newclust;
366 			actsize+= bytesperclust;
367 		}
368 		/* actsize >= file size */
369 		actsize -= bytesperclust;
370 		/* get remaining clusters */
371 		if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
372 			FAT_ERROR("Error reading cluster\n");
373 			return -1;
374 		}
375 		/* get remaining bytes */
376 		gotsize += (int)actsize;
377 		filesize -= actsize;
378 		buffer += actsize;
379 		actsize= filesize;
380 		if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
381 			FAT_ERROR("Error reading cluster\n");
382 			return -1;
383 		}
384 		gotsize+=actsize;
385 		return gotsize;
386 getit:
387 		if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
388 			FAT_ERROR("Error reading cluster\n");
389 			return -1;
390 		}
391 		gotsize += (int)actsize;
392 		filesize -= actsize;
393 		buffer += actsize;
394 		curclust = get_fatent(mydata, endclust);
395 		if (curclust <= 0x0001 || curclust >= 0xfff0) {
396 			FAT_DPRINT("curclust: 0x%x\n", curclust);
397 			FAT_ERROR("Invalid FAT entry\n");
398 			return gotsize;
399 		}
400 		actsize=bytesperclust;
401 		endclust=curclust;
402 	} while (1);
403 }
404 
405 
406 #ifdef CONFIG_SUPPORT_VFAT
407 /*
408  * Extract the file name information from 'slotptr' into 'l_name',
409  * starting at l_name[*idx].
410  * Return 1 if terminator (zero byte) is found, 0 otherwise.
411  */
412 static int
413 slot2str(dir_slot *slotptr, char *l_name, int *idx)
414 {
415 	int j;
416 
417 	for (j = 0; j <= 8; j += 2) {
418 		l_name[*idx] = slotptr->name0_4[j];
419 		if (l_name[*idx] == 0x00) return 1;
420 		(*idx)++;
421 	}
422 	for (j = 0; j <= 10; j += 2) {
423 		l_name[*idx] = slotptr->name5_10[j];
424 		if (l_name[*idx] == 0x00) return 1;
425 		(*idx)++;
426 	}
427 	for (j = 0; j <= 2; j += 2) {
428 		l_name[*idx] = slotptr->name11_12[j];
429 		if (l_name[*idx] == 0x00) return 1;
430 		(*idx)++;
431 	}
432 
433 	return 0;
434 }
435 
436 
437 /*
438  * Extract the full long filename starting at 'retdent' (which is really
439  * a slot) into 'l_name'. If successful also copy the real directory entry
440  * into 'retdent'
441  * Return 0 on success, -1 otherwise.
442  */
443 __u8	 get_vfatname_block[MAX_CLUSTSIZE];
444 static int
445 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
446 	     dir_entry *retdent, char *l_name)
447 {
448 	dir_entry *realdent;
449 	dir_slot  *slotptr = (dir_slot*) retdent;
450 	__u8	  *nextclust = cluster + mydata->clust_size * SECTOR_SIZE;
451 	__u8	   counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
452 	int idx = 0;
453 
454 	while ((__u8*)slotptr < nextclust) {
455 		if (counter == 0) break;
456 		if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
457 			return -1;
458 		slotptr++;
459 		counter--;
460 	}
461 
462 	if ((__u8*)slotptr >= nextclust) {
463 		dir_slot *slotptr2;
464 
465 		slotptr--;
466 		curclust = get_fatent(mydata, curclust);
467 		if (curclust <= 0x0001 || curclust >= 0xfff0) {
468 			FAT_DPRINT("curclust: 0x%x\n", curclust);
469 			FAT_ERROR("Invalid FAT entry\n");
470 			return -1;
471 		}
472 		if (get_cluster(mydata, curclust, get_vfatname_block,
473 				mydata->clust_size * SECTOR_SIZE) != 0) {
474 			FAT_DPRINT("Error: reading directory block\n");
475 			return -1;
476 		}
477 		slotptr2 = (dir_slot*) get_vfatname_block;
478 		while (slotptr2->id > 0x01) {
479 			slotptr2++;
480 		}
481 		/* Save the real directory entry */
482 		realdent = (dir_entry*)slotptr2 + 1;
483 		while ((__u8*)slotptr2 >= get_vfatname_block) {
484 			slot2str(slotptr2, l_name, &idx);
485 			slotptr2--;
486 		}
487 	} else {
488 		/* Save the real directory entry */
489 		realdent = (dir_entry*)slotptr;
490 	}
491 
492 	do {
493 		slotptr--;
494 		if (slot2str(slotptr, l_name, &idx)) break;
495 	} while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
496 
497 	l_name[idx] = '\0';
498 	if (*l_name == DELETED_FLAG) *l_name = '\0';
499 	else if (*l_name == aRING) *l_name = '�';
500 	downcase(l_name);
501 
502 	/* Return the real directory entry */
503 	memcpy(retdent, realdent, sizeof(dir_entry));
504 
505 	return 0;
506 }
507 
508 
509 /* Calculate short name checksum */
510 static __u8
511 mkcksum(const char *str)
512 {
513 	int i;
514 	__u8 ret = 0;
515 
516 	for (i = 0; i < 11; i++) {
517 		ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
518 	}
519 
520 	return ret;
521 }
522 #endif
523 
524 
525 /*
526  * Get the directory entry associated with 'filename' from the directory
527  * starting at 'startsect'
528  */
529 __u8 get_dentfromdir_block[MAX_CLUSTSIZE];
530 static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
531 				   char *filename, dir_entry * retdent,
532 				   int dols)
533 {
534     __u16 prevcksum = 0xffff;
535     __u32 curclust = START (retdent);
536     int files = 0, dirs = 0;
537 
538     FAT_DPRINT ("get_dentfromdir: %s\n", filename);
539     while (1) {
540 	dir_entry *dentptr;
541 	int i;
542 
543 	if (get_cluster (mydata, curclust, get_dentfromdir_block,
544 		 mydata->clust_size * SECTOR_SIZE) != 0) {
545 	    FAT_DPRINT ("Error: reading directory block\n");
546 	    return NULL;
547 	}
548 	dentptr = (dir_entry *) get_dentfromdir_block;
549 	for (i = 0; i < DIRENTSPERCLUST; i++) {
550 	    char s_name[14], l_name[256];
551 
552 	    l_name[0] = '\0';
553 	    if (dentptr->name[0] == DELETED_FLAG) {
554 		    dentptr++;
555 		    continue;
556 	    }
557 	    if ((dentptr->attr & ATTR_VOLUME)) {
558 #ifdef CONFIG_SUPPORT_VFAT
559 		if ((dentptr->attr & ATTR_VFAT) &&
560 		    (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
561 		    prevcksum = ((dir_slot *) dentptr)
562 			    ->alias_checksum;
563 		    get_vfatname (mydata, curclust, get_dentfromdir_block,
564 				  dentptr, l_name);
565 		    if (dols) {
566 			int isdir = (dentptr->attr & ATTR_DIR);
567 			char dirc;
568 			int doit = 0;
569 
570 			if (isdir) {
571 			    dirs++;
572 			    dirc = '/';
573 			    doit = 1;
574 			} else {
575 			    dirc = ' ';
576 			    if (l_name[0] != 0) {
577 				files++;
578 				doit = 1;
579 			    }
580 			}
581 			if (doit) {
582 			    if (dirc == ' ') {
583 				printf (" %8ld   %s%c\n",
584 					(long) FAT2CPU32 (dentptr->size),
585 					l_name, dirc);
586 			    } else {
587 				printf ("            %s%c\n", l_name, dirc);
588 			    }
589 			}
590 			dentptr++;
591 			continue;
592 		    }
593 		    FAT_DPRINT ("vfatname: |%s|\n", l_name);
594 		} else
595 #endif
596 		{
597 		    /* Volume label or VFAT entry */
598 		    dentptr++;
599 		    continue;
600 		}
601 	    }
602 	    if (dentptr->name[0] == 0) {
603 		if (dols) {
604 		    printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
605 		}
606 		FAT_DPRINT ("Dentname == NULL - %d\n", i);
607 		return NULL;
608 	    }
609 #ifdef CONFIG_SUPPORT_VFAT
610 	    if (dols && mkcksum (dentptr->name) == prevcksum) {
611 		dentptr++;
612 		continue;
613 	    }
614 #endif
615 	    get_name (dentptr, s_name);
616 	    if (dols) {
617 		int isdir = (dentptr->attr & ATTR_DIR);
618 		char dirc;
619 		int doit = 0;
620 
621 		if (isdir) {
622 		    dirs++;
623 		    dirc = '/';
624 		    doit = 1;
625 		} else {
626 		    dirc = ' ';
627 		    if (s_name[0] != 0) {
628 			files++;
629 			doit = 1;
630 		    }
631 		}
632 		if (doit) {
633 		    if (dirc == ' ') {
634 			printf (" %8ld   %s%c\n",
635 				(long) FAT2CPU32 (dentptr->size), s_name,
636 				dirc);
637 		    } else {
638 			printf ("            %s%c\n", s_name, dirc);
639 		    }
640 		}
641 		dentptr++;
642 		continue;
643 	    }
644 	    if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
645 		FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
646 		dentptr++;
647 		continue;
648 	    }
649 	    memcpy (retdent, dentptr, sizeof (dir_entry));
650 
651 	    FAT_DPRINT ("DentName: %s", s_name);
652 	    FAT_DPRINT (", start: 0x%x", START (dentptr));
653 	    FAT_DPRINT (", size:  0x%x %s\n",
654 			FAT2CPU32 (dentptr->size),
655 			(dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
656 
657 	    return retdent;
658 	}
659 	curclust = get_fatent (mydata, curclust);
660 	if (curclust <= 0x0001 || curclust >= 0xfff0) {
661 	    FAT_DPRINT ("curclust: 0x%x\n", curclust);
662 	    FAT_ERROR ("Invalid FAT entry\n");
663 	    return NULL;
664 	}
665     }
666 
667     return NULL;
668 }
669 
670 
671 /*
672  * Read boot sector and volume info from a FAT filesystem
673  */
674 static int
675 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
676 {
677 	__u8 block[FS_BLOCK_SIZE];
678 	volume_info *vistart;
679 
680 	if (disk_read(0, 1, block) < 0) {
681 		FAT_DPRINT("Error: reading block\n");
682 		return -1;
683 	}
684 
685 	memcpy(bs, block, sizeof(boot_sector));
686 	bs->reserved	= FAT2CPU16(bs->reserved);
687 	bs->fat_length	= FAT2CPU16(bs->fat_length);
688 	bs->secs_track	= FAT2CPU16(bs->secs_track);
689 	bs->heads	= FAT2CPU16(bs->heads);
690 #if 0 /* UNUSED */
691 	bs->hidden	= FAT2CPU32(bs->hidden);
692 #endif
693 	bs->total_sect	= FAT2CPU32(bs->total_sect);
694 
695 	/* FAT32 entries */
696 	if (bs->fat_length == 0) {
697 		/* Assume FAT32 */
698 		bs->fat32_length = FAT2CPU32(bs->fat32_length);
699 		bs->flags	 = FAT2CPU16(bs->flags);
700 		bs->root_cluster = FAT2CPU32(bs->root_cluster);
701 		bs->info_sector  = FAT2CPU16(bs->info_sector);
702 		bs->backup_boot  = FAT2CPU16(bs->backup_boot);
703 		vistart = (volume_info*) (block + sizeof(boot_sector));
704 		*fatsize = 32;
705 	} else {
706 		vistart = (volume_info*) &(bs->fat32_length);
707 		*fatsize = 0;
708 	}
709 	memcpy(volinfo, vistart, sizeof(volume_info));
710 
711 	/* Terminate fs_type string. Writing past the end of vistart
712 	   is ok - it's just the buffer. */
713 	vistart->fs_type[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 
735 __u8 do_fat_read_block[MAX_CLUSTSIZE];  /* Block buffer */
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_SCSI) || \
991     defined(CONFIG_CMD_USB) || \
992     (CONFIG_MMC)
993 	printf("Interface:  ");
994 	switch(cur_dev->if_type) {
995 		case IF_TYPE_IDE :	printf("IDE"); break;
996 		case IF_TYPE_SCSI :	printf("SCSI"); break;
997 		case IF_TYPE_ATAPI :	printf("ATAPI"); break;
998 		case IF_TYPE_USB :	printf("USB"); break;
999 		case IF_TYPE_DOC :	printf("DOC"); break;
1000 		case IF_TYPE_MMC :	printf("MMC"); break;
1001 		default :		printf("Unknown");
1002 	}
1003 	printf("\n  Device %d: ",cur_dev->dev);
1004 	dev_print(cur_dev);
1005 #endif
1006 	if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1007 		printf("\nNo valid FAT fs found\n");
1008 		return 1;
1009 	}
1010 	memcpy (vol_label, volinfo.volume_label, 11);
1011 	vol_label[11] = '\0';
1012 	volinfo.fs_type[5]='\0';
1013 	printf("Partition %d: Filesystem: %s \"%s\"\n"
1014 			,cur_part,volinfo.fs_type,vol_label);
1015 	return 0;
1016 }
1017 
1018 
1019 int
1020 file_fat_ls(const char *dir)
1021 {
1022 	return do_fat_read(dir, NULL, 0, LS_YES);
1023 }
1024 
1025 
1026 long
1027 file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1028 {
1029 	printf("reading %s\n",filename);
1030 	return do_fat_read(filename, buffer, maxsize, LS_NO);
1031 }
1032 
1033 #endif
1034