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