blob: 084ee44459e8f1bd3631c47890546caa931ad375 [file] [log] [blame]
wdenk71f95112003-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>
wdenk7205e402003-09-10 22:30:53 +000032#include <part.h>
wdenk71f95112003-06-15 22:40:42 +000033
34#if (CONFIG_COMMANDS & CFG_CMD_FAT)
35
36/*
37 * Convert a string to lowercase.
38 */
39static void
40downcase(char *str)
41{
42 while (*str != '\0') {
43 TOLOWER(*str);
44 str++;
45 }
46}
47
wdenk7205e402003-09-10 22:30:53 +000048static block_dev_desc_t *cur_dev = NULL;
49static unsigned long part_offset = 0;
50static 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
wdenk71f95112003-06-15 22:40:42 +000055
56int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
57{
wdenk7205e402003-09-10 22:30:53 +000058 startblock += part_offset;
59 if (cur_dev == NULL)
60 return -1;
61 if (cur_dev->block_read) {
62 return cur_dev->block_read (cur_dev->dev, startblock, getsize, (unsigned long *)bufptr);
wdenk71f95112003-06-15 22:40:42 +000063 }
64 return -1;
65}
66
67
68int
wdenk7205e402003-09-10 22:30:53 +000069fat_register_device(block_dev_desc_t *dev_desc, int part_no)
wdenk71f95112003-06-15 22:40:42 +000070{
wdenk7205e402003-09-10 22:30:53 +000071 unsigned char buffer[SECTOR_SIZE];
72
73 if (!dev_desc->block_read)
74 return -1;
75 cur_dev=dev_desc;
76 /* check if we have a MBR (on floppies we have only a PBR) */
77 if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
78 printf ("** Can't read from device %d **\n", dev_desc->dev);
79 return -1;
80 }
81 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
82 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
83 /* no signature found */
84 return -1;
85 }
Wolfgang Denk77ddac92005-10-13 16:45:02 +020086 if(!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
wdenk7205e402003-09-10 22:30:53 +000087 /* ok, we assume we are on a PBR only */
88 cur_part = 1;
89 part_offset=0;
90 }
91 else {
Peter Pearseb0d8f5b2007-05-09 11:37:56 +010092#if ((CONFIG_COMMANDS & CFG_CMD_IDE) || \
93 (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
94 (CONFIG_COMMANDS & CFG_CMD_USB) || \
95 (defined(CONFIG_MMC) && defined(CONFIG_LPC2292)) || \
96 defined(CONFIG_SYSTEMACE) )
wdenk7205e402003-09-10 22:30:53 +000097 disk_partition_t info;
98 if(!get_partition_info(dev_desc, part_no, &info)) {
99 part_offset = info.start;
100 cur_part = part_no;
101 }
102 else {
103 printf ("** Partition %d not valid on device %d **\n",part_no,dev_desc->dev);
104 return -1;
105 }
106#else
107 /* FIXME we need to determine the start block of the
108 * partition where the DOS FS resides. This can be done
109 * by using the get_partition_info routine. For this
110 * purpose the libpart must be included.
111 */
112 part_offset=32;
113 cur_part = 1;
114#endif
115 }
wdenk71f95112003-06-15 22:40:42 +0000116 return 0;
117}
118
119
120/*
121 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
122 * Return index into string if found, -1 otherwise.
123 */
124static int
125dirdelim(char *str)
126{
127 char *start = str;
128
129 while (*str != '\0') {
130 if (ISDIRDELIM(*str)) return str - start;
131 str++;
132 }
133 return -1;
134}
135
136
137/*
138 * Match volume_info fs_type strings.
139 * Return 0 on match, -1 otherwise.
140 */
141static int
142compare_sign(char *str1, char *str2)
143{
144 char *end = str1+SIGNLEN;
145
146 while (str1 != end) {
147 if (*str1 != *str2) {
148 return -1;
149 }
150 str1++;
151 str2++;
152 }
153
154 return 0;
155}
156
157
158/*
159 * Extract zero terminated short name from a directory entry.
160 */
161static void get_name (dir_entry *dirent, char *s_name)
162{
163 char *ptr;
164
165 memcpy (s_name, dirent->name, 8);
166 s_name[8] = '\0';
167 ptr = s_name;
168 while (*ptr && *ptr != ' ')
169 ptr++;
170 if (dirent->ext[0] && dirent->ext[0] != ' ') {
171 *ptr = '.';
172 ptr++;
173 memcpy (ptr, dirent->ext, 3);
174 ptr[3] = '\0';
175 while (*ptr && *ptr != ' ')
176 ptr++;
177 }
178 *ptr = '\0';
179 if (*s_name == DELETED_FLAG)
180 *s_name = '\0';
181 else if (*s_name == aRING)
182 *s_name = 'Ã¥';
183 downcase (s_name);
184}
185
186/*
187 * Get the entry at index 'entry' in a FAT (12/16/32) table.
188 * On failure 0x00 is returned.
189 */
190static __u32
191get_fatent(fsdata *mydata, __u32 entry)
192{
193 __u32 bufnum;
194 __u32 offset;
195 __u32 ret = 0x00;
196
197 switch (mydata->fatsize) {
198 case 32:
199 bufnum = entry / FAT32BUFSIZE;
200 offset = entry - bufnum * FAT32BUFSIZE;
201 break;
202 case 16:
203 bufnum = entry / FAT16BUFSIZE;
204 offset = entry - bufnum * FAT16BUFSIZE;
205 break;
206 case 12:
207 bufnum = entry / FAT12BUFSIZE;
208 offset = entry - bufnum * FAT12BUFSIZE;
209 break;
210
211 default:
212 /* Unsupported FAT size */
213 return ret;
214 }
215
216 /* Read a new block of FAT entries into the cache. */
217 if (bufnum != mydata->fatbufnum) {
218 int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
219 __u8 *bufptr = mydata->fatbuf;
220 __u32 fatlength = mydata->fatlength;
221 __u32 startblock = bufnum * FATBUFBLOCKS;
222
223 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
224 startblock += mydata->fat_sect; /* Offset from start of disk */
225
226 if (getsize > fatlength) getsize = fatlength;
227 if (disk_read(startblock, getsize, bufptr) < 0) {
228 FAT_DPRINT("Error reading FAT blocks\n");
229 return ret;
230 }
231 mydata->fatbufnum = bufnum;
232 }
233
234 /* Get the actual entry from the table */
235 switch (mydata->fatsize) {
236 case 32:
237 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
238 break;
239 case 16:
240 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
241 break;
242 case 12: {
243 __u32 off16 = (offset*3)/4;
244 __u16 val1, val2;
245
246 switch (offset & 0x3) {
247 case 0:
248 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
249 ret &= 0xfff;
250 break;
251 case 1:
252 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
253 val1 &= 0xf000;
254 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
255 val2 &= 0x00ff;
256 ret = (val2 << 4) | (val1 >> 12);
257 break;
258 case 2:
259 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
260 val1 &= 0xff00;
261 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
262 val2 &= 0x000f;
263 ret = (val2 << 8) | (val1 >> 8);
264 break;
265 case 3:
266 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
267 ret = (ret & 0xfff0) >> 4;
268 break;
269 default:
270 break;
271 }
272 }
273 break;
274 }
275 FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
276
277 return ret;
278}
279
280
281/*
282 * Read at most 'size' bytes from the specified cluster into 'buffer'.
283 * Return 0 on success, -1 otherwise.
284 */
285static int
286get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
287{
288 int idx = 0;
289 __u32 startsect;
290
291 if (clustnum > 0) {
292 startsect = mydata->data_begin + clustnum*mydata->clust_size;
293 } else {
294 startsect = mydata->rootdir_sect;
295 }
296
297 FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
wdenk7205e402003-09-10 22:30:53 +0000298 if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
299 FAT_DPRINT("Error reading data\n");
300 return -1;
301 }
302 if(size % FS_BLOCK_SIZE) {
303 __u8 tmpbuf[FS_BLOCK_SIZE];
304 idx= size/FS_BLOCK_SIZE;
305 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
306 FAT_DPRINT("Error reading data\n");
307 return -1;
wdenk71f95112003-06-15 22:40:42 +0000308 }
wdenk7205e402003-09-10 22:30:53 +0000309 buffer += idx*FS_BLOCK_SIZE;
310
311 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
312 return 0;
wdenk71f95112003-06-15 22:40:42 +0000313 }
314
315 return 0;
316}
317
318
319/*
320 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
321 * into 'buffer'.
322 * Return the number of bytes read or -1 on fatal errors.
323 */
324static long
325get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
326 unsigned long maxsize)
327{
328 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
329 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
330 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000331 __u32 endclust, newclust;
332 unsigned long actsize;
wdenk71f95112003-06-15 22:40:42 +0000333
334 FAT_DPRINT("Filesize: %ld bytes\n", filesize);
335
336 if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
337
338 FAT_DPRINT("Reading: %ld bytes\n", filesize);
339
wdenk7205e402003-09-10 22:30:53 +0000340 actsize=bytesperclust;
341 endclust=curclust;
wdenk71f95112003-06-15 22:40:42 +0000342 do {
wdenk7205e402003-09-10 22:30:53 +0000343 /* search for consecutive clusters */
344 while(actsize < filesize) {
345 newclust = get_fatent(mydata, endclust);
346 if((newclust -1)!=endclust)
347 goto getit;
348 if (newclust <= 0x0001 || newclust >= 0xfff0) {
349 FAT_DPRINT("curclust: 0x%x\n", newclust);
350 FAT_DPRINT("Invalid FAT entry\n");
351 return gotsize;
352 }
353 endclust=newclust;
354 actsize+= bytesperclust;
355 }
356 /* actsize >= file size */
357 actsize -= bytesperclust;
358 /* get remaining clusters */
359 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
wdenk71f95112003-06-15 22:40:42 +0000360 FAT_ERROR("Error reading cluster\n");
361 return -1;
362 }
wdenk7205e402003-09-10 22:30:53 +0000363 /* get remaining bytes */
364 gotsize += (int)actsize;
365 filesize -= actsize;
366 buffer += actsize;
367 actsize= filesize;
368 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
369 FAT_ERROR("Error reading cluster\n");
370 return -1;
371 }
372 gotsize+=actsize;
373 return gotsize;
374getit:
375 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
376 FAT_ERROR("Error reading cluster\n");
377 return -1;
378 }
379 gotsize += (int)actsize;
380 filesize -= actsize;
381 buffer += actsize;
382 curclust = get_fatent(mydata, endclust);
wdenk71f95112003-06-15 22:40:42 +0000383 if (curclust <= 0x0001 || curclust >= 0xfff0) {
384 FAT_DPRINT("curclust: 0x%x\n", curclust);
385 FAT_ERROR("Invalid FAT entry\n");
386 return gotsize;
387 }
wdenk7205e402003-09-10 22:30:53 +0000388 actsize=bytesperclust;
389 endclust=curclust;
wdenk71f95112003-06-15 22:40:42 +0000390 } while (1);
391}
392
393
394#ifdef CONFIG_SUPPORT_VFAT
395/*
396 * Extract the file name information from 'slotptr' into 'l_name',
397 * starting at l_name[*idx].
398 * Return 1 if terminator (zero byte) is found, 0 otherwise.
399 */
400static int
401slot2str(dir_slot *slotptr, char *l_name, int *idx)
402{
403 int j;
404
405 for (j = 0; j <= 8; j += 2) {
406 l_name[*idx] = slotptr->name0_4[j];
407 if (l_name[*idx] == 0x00) return 1;
408 (*idx)++;
409 }
410 for (j = 0; j <= 10; j += 2) {
411 l_name[*idx] = slotptr->name5_10[j];
412 if (l_name[*idx] == 0x00) return 1;
413 (*idx)++;
414 }
415 for (j = 0; j <= 2; j += 2) {
416 l_name[*idx] = slotptr->name11_12[j];
417 if (l_name[*idx] == 0x00) return 1;
418 (*idx)++;
419 }
420
421 return 0;
422}
423
424
425/*
426 * Extract the full long filename starting at 'retdent' (which is really
427 * a slot) into 'l_name'. If successful also copy the real directory entry
428 * into 'retdent'
429 * Return 0 on success, -1 otherwise.
430 */
wdenk5fa66df2003-10-29 23:18:55 +0000431__u8 get_vfatname_block[MAX_CLUSTSIZE];
wdenk71f95112003-06-15 22:40:42 +0000432static 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;
wdenk2d1a5372004-02-23 19:30:57 +0000439 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000440 int idx = 0;
441
442 while ((__u8*)slotptr < nextclust) {
443 if (counter == 0) break;
wdenk2d1a5372004-02-23 19:30:57 +0000444 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
445 return -1;
wdenk71f95112003-06-15 22:40:42 +0000446 slotptr++;
447 counter--;
448 }
449
450 if ((__u8*)slotptr >= nextclust) {
wdenk71f95112003-06-15 22:40:42 +0000451 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 }
wdenk5fa66df2003-10-29 23:18:55 +0000460 if (get_cluster(mydata, curclust, get_vfatname_block,
wdenk71f95112003-06-15 22:40:42 +0000461 mydata->clust_size * SECTOR_SIZE) != 0) {
462 FAT_DPRINT("Error: reading directory block\n");
463 return -1;
464 }
wdenk5fa66df2003-10-29 23:18:55 +0000465 slotptr2 = (dir_slot*) get_vfatname_block;
wdenk71f95112003-06-15 22:40:42 +0000466 while (slotptr2->id > 0x01) {
467 slotptr2++;
468 }
469 /* Save the real directory entry */
470 realdent = (dir_entry*)slotptr2 + 1;
wdenk5fa66df2003-10-29 23:18:55 +0000471 while ((__u8*)slotptr2 >= get_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000472 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;
wdenk2d1a5372004-02-23 19:30:57 +0000483 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000484
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 */
wdenk5fa66df2003-10-29 23:18:55 +0000517__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
wdenk71f95112003-06-15 22:40:42 +0000518static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
519 char *filename, dir_entry * retdent,
520 int dols)
521{
522 __u16 prevcksum = 0xffff;
wdenk71f95112003-06-15 22:40:42 +0000523 __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
wdenk5fa66df2003-10-29 23:18:55 +0000531 if (get_cluster (mydata, curclust, get_dentfromdir_block,
wdenk71f95112003-06-15 22:40:42 +0000532 mydata->clust_size * SECTOR_SIZE) != 0) {
533 FAT_DPRINT ("Error: reading directory block\n");
534 return NULL;
535 }
wdenk5fa66df2003-10-29 23:18:55 +0000536 dentptr = (dir_entry *) get_dentfromdir_block;
wdenk71f95112003-06-15 22:40:42 +0000537 for (i = 0; i < DIRENTSPERCLUST; i++) {
538 char s_name[14], l_name[256];
539
540 l_name[0] = '\0';
wdenk855a4962004-03-14 18:23:55 +0000541 if (dentptr->name[0] == DELETED_FLAG) {
542 dentptr++;
543 continue;
544 }
wdenk71f95112003-06-15 22:40:42 +0000545 if ((dentptr->attr & ATTR_VOLUME)) {
546#ifdef CONFIG_SUPPORT_VFAT
547 if ((dentptr->attr & ATTR_VFAT) &&
wdenk2d1a5372004-02-23 19:30:57 +0000548 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
wdenk71f95112003-06-15 22:40:42 +0000549 prevcksum = ((dir_slot *) dentptr)
550 ->alias_checksum;
wdenk5fa66df2003-10-29 23:18:55 +0000551 get_vfatname (mydata, curclust, get_dentfromdir_block,
wdenk71f95112003-06-15 22:40:42 +0000552 dentptr, l_name);
553 if (dols) {
554 int isdir = (dentptr->attr & ATTR_DIR);
555 char dirc;
556 int doit = 0;
557
558 if (isdir) {
559 dirs++;
560 dirc = '/';
561 doit = 1;
562 } else {
563 dirc = ' ';
564 if (l_name[0] != 0) {
565 files++;
566 doit = 1;
567 }
568 }
569 if (doit) {
570 if (dirc == ' ') {
571 printf (" %8ld %s%c\n",
572 (long) FAT2CPU32 (dentptr->size),
573 l_name, dirc);
574 } else {
575 printf (" %s%c\n", l_name, dirc);
576 }
577 }
578 dentptr++;
579 continue;
580 }
581 FAT_DPRINT ("vfatname: |%s|\n", l_name);
582 } else
583#endif
584 {
585 /* Volume label or VFAT entry */
586 dentptr++;
587 continue;
588 }
589 }
590 if (dentptr->name[0] == 0) {
591 if (dols) {
592 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
593 }
594 FAT_DPRINT ("Dentname == NULL - %d\n", i);
595 return NULL;
596 }
597#ifdef CONFIG_SUPPORT_VFAT
598 if (dols && mkcksum (dentptr->name) == prevcksum) {
599 dentptr++;
600 continue;
601 }
602#endif
603 get_name (dentptr, s_name);
604 if (dols) {
605 int isdir = (dentptr->attr & ATTR_DIR);
606 char dirc;
607 int doit = 0;
608
609 if (isdir) {
610 dirs++;
611 dirc = '/';
612 doit = 1;
613 } else {
614 dirc = ' ';
615 if (s_name[0] != 0) {
616 files++;
617 doit = 1;
618 }
619 }
620 if (doit) {
621 if (dirc == ' ') {
622 printf (" %8ld %s%c\n",
623 (long) FAT2CPU32 (dentptr->size), s_name,
624 dirc);
625 } else {
626 printf (" %s%c\n", s_name, dirc);
627 }
628 }
629 dentptr++;
630 continue;
631 }
632 if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
633 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
634 dentptr++;
635 continue;
636 }
637 memcpy (retdent, dentptr, sizeof (dir_entry));
638
639 FAT_DPRINT ("DentName: %s", s_name);
640 FAT_DPRINT (", start: 0x%x", START (dentptr));
641 FAT_DPRINT (", size: 0x%x %s\n",
642 FAT2CPU32 (dentptr->size),
643 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
644
645 return retdent;
646 }
647 curclust = get_fatent (mydata, curclust);
648 if (curclust <= 0x0001 || curclust >= 0xfff0) {
649 FAT_DPRINT ("curclust: 0x%x\n", curclust);
650 FAT_ERROR ("Invalid FAT entry\n");
651 return NULL;
652 }
653 }
654
655 return NULL;
656}
657
658
659/*
660 * Read boot sector and volume info from a FAT filesystem
661 */
662static int
663read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
664{
665 __u8 block[FS_BLOCK_SIZE];
666 volume_info *vistart;
667
668 if (disk_read(0, 1, block) < 0) {
669 FAT_DPRINT("Error: reading block\n");
670 return -1;
671 }
672
673 memcpy(bs, block, sizeof(boot_sector));
674 bs->reserved = FAT2CPU16(bs->reserved);
675 bs->fat_length = FAT2CPU16(bs->fat_length);
676 bs->secs_track = FAT2CPU16(bs->secs_track);
677 bs->heads = FAT2CPU16(bs->heads);
678#if 0 /* UNUSED */
679 bs->hidden = FAT2CPU32(bs->hidden);
680#endif
681 bs->total_sect = FAT2CPU32(bs->total_sect);
682
683 /* FAT32 entries */
684 if (bs->fat_length == 0) {
685 /* Assume FAT32 */
686 bs->fat32_length = FAT2CPU32(bs->fat32_length);
687 bs->flags = FAT2CPU16(bs->flags);
688 bs->root_cluster = FAT2CPU32(bs->root_cluster);
689 bs->info_sector = FAT2CPU16(bs->info_sector);
690 bs->backup_boot = FAT2CPU16(bs->backup_boot);
691 vistart = (volume_info*) (block + sizeof(boot_sector));
692 *fatsize = 32;
693 } else {
694 vistart = (volume_info*) &(bs->fat32_length);
695 *fatsize = 0;
696 }
697 memcpy(volinfo, vistart, sizeof(volume_info));
698
699 /* Terminate fs_type string. Writing past the end of vistart
700 is ok - it's just the buffer. */
701 vistart->fs_type[8] = '\0';
702
703 if (*fatsize == 32) {
704 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
705 return 0;
706 }
707 } else {
708 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
709 *fatsize = 12;
710 return 0;
711 }
712 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
713 *fatsize = 16;
714 return 0;
715 }
716 }
717
718 FAT_DPRINT("Error: broken fs_type sign\n");
719 return -1;
720}
721
722
wdenk5fa66df2003-10-29 23:18:55 +0000723__u8 do_fat_read_block[MAX_CLUSTSIZE]; /* Block buffer */
stroese20cc00d2004-12-16 17:57:26 +0000724long
wdenk71f95112003-06-15 22:40:42 +0000725do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
726 int dols)
727{
Wolfgang Denkd06a5f72005-08-06 01:56:59 +0200728#if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
729 static
730#endif
wdenk71f95112003-06-15 22:40:42 +0000731 char fnamecopy[2048];
732 boot_sector bs;
733 volume_info volinfo;
734 fsdata datablock;
735 fsdata *mydata = &datablock;
736 dir_entry *dentptr;
737 __u16 prevcksum = 0xffff;
738 char *subname = "";
739 int rootdir_size, cursect;
740 int idx, isdir = 0;
741 int files = 0, dirs = 0;
742 long ret = 0;
743 int firsttime;
744
745 if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
746 FAT_DPRINT ("Error: reading boot sector\n");
747 return -1;
748 }
749 if (mydata->fatsize == 32) {
750 mydata->fatlength = bs.fat32_length;
751 } else {
752 mydata->fatlength = bs.fat_length;
753 }
754 mydata->fat_sect = bs.reserved;
755 cursect = mydata->rootdir_sect
756 = mydata->fat_sect + mydata->fatlength * bs.fats;
757 mydata->clust_size = bs.cluster_size;
758 if (mydata->fatsize == 32) {
759 rootdir_size = mydata->clust_size;
760 mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
761 - (mydata->clust_size * 2);
762 } else {
763 rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
764 * sizeof (dir_entry)) / SECTOR_SIZE;
765 mydata->data_begin = mydata->rootdir_sect + rootdir_size
766 - (mydata->clust_size * 2);
767 }
768 mydata->fatbufnum = -1;
769
770 FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
771 mydata->fatlength);
772 FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
773 "Data begins at: %d\n",
774 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
775 rootdir_size, mydata->data_begin);
776 FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
777
778 /* "cwd" is always the root... */
779 while (ISDIRDELIM (*filename))
780 filename++;
781 /* Make a copy of the filename and convert it to lowercase */
782 strcpy (fnamecopy, filename);
783 downcase (fnamecopy);
784 if (*fnamecopy == '\0') {
785 if (!dols)
786 return -1;
787 dols = LS_ROOT;
788 } else if ((idx = dirdelim (fnamecopy)) >= 0) {
789 isdir = 1;
790 fnamecopy[idx] = '\0';
791 subname = fnamecopy + idx + 1;
792 /* Handle multiple delimiters */
793 while (ISDIRDELIM (*subname))
794 subname++;
795 } else if (dols) {
796 isdir = 1;
797 }
798
799 while (1) {
800 int i;
801
wdenk5fa66df2003-10-29 23:18:55 +0000802 if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
wdenk71f95112003-06-15 22:40:42 +0000803 FAT_DPRINT ("Error: reading rootdir block\n");
804 return -1;
805 }
wdenk5fa66df2003-10-29 23:18:55 +0000806 dentptr = (dir_entry *) do_fat_read_block;
wdenk71f95112003-06-15 22:40:42 +0000807 for (i = 0; i < DIRENTSPERBLOCK; i++) {
808 char s_name[14], l_name[256];
809
810 l_name[0] = '\0';
811 if ((dentptr->attr & ATTR_VOLUME)) {
812#ifdef CONFIG_SUPPORT_VFAT
813 if ((dentptr->attr & ATTR_VFAT) &&
wdenk2d1a5372004-02-23 19:30:57 +0000814 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
wdenk71f95112003-06-15 22:40:42 +0000815 prevcksum = ((dir_slot *) dentptr)->alias_checksum;
wdenk5fa66df2003-10-29 23:18:55 +0000816 get_vfatname (mydata, 0, do_fat_read_block, dentptr, l_name);
wdenk71f95112003-06-15 22:40:42 +0000817 if (dols == LS_ROOT) {
818 int isdir = (dentptr->attr & ATTR_DIR);
819 char dirc;
820 int doit = 0;
821
822 if (isdir) {
823 dirs++;
824 dirc = '/';
825 doit = 1;
826 } else {
827 dirc = ' ';
828 if (l_name[0] != 0) {
829 files++;
830 doit = 1;
831 }
832 }
833 if (doit) {
834 if (dirc == ' ') {
835 printf (" %8ld %s%c\n",
836 (long) FAT2CPU32 (dentptr->size),
837 l_name, dirc);
838 } else {
839 printf (" %s%c\n", l_name, dirc);
840 }
841 }
842 dentptr++;
843 continue;
844 }
845 FAT_DPRINT ("Rootvfatname: |%s|\n", l_name);
846 } else
847#endif
848 {
849 /* Volume label or VFAT entry */
850 dentptr++;
851 continue;
852 }
853 } else if (dentptr->name[0] == 0) {
854 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
855 if (dols == LS_ROOT) {
856 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
857 return 0;
858 }
859 return -1;
860 }
861#ifdef CONFIG_SUPPORT_VFAT
862 else if (dols == LS_ROOT
863 && mkcksum (dentptr->name) == prevcksum) {
864 dentptr++;
865 continue;
866 }
867#endif
868 get_name (dentptr, s_name);
869 if (dols == LS_ROOT) {
870 int isdir = (dentptr->attr & ATTR_DIR);
871 char dirc;
872 int doit = 0;
873
874 if (isdir) {
wdenk71f95112003-06-15 22:40:42 +0000875 dirc = '/';
wdenka43278a2003-09-11 19:48:06 +0000876 if (s_name[0] != 0) {
877 dirs++;
878 doit = 1;
879 }
wdenk71f95112003-06-15 22:40:42 +0000880 } else {
881 dirc = ' ';
882 if (s_name[0] != 0) {
883 files++;
884 doit = 1;
885 }
886 }
887 if (doit) {
888 if (dirc == ' ') {
889 printf (" %8ld %s%c\n",
890 (long) FAT2CPU32 (dentptr->size), s_name,
891 dirc);
892 } else {
893 printf (" %s%c\n", s_name, dirc);
894 }
895 }
896 dentptr++;
897 continue;
898 }
899 if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
900 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
901 dentptr++;
902 continue;
903 }
904 if (isdir && !(dentptr->attr & ATTR_DIR))
905 return -1;
906
907 FAT_DPRINT ("RootName: %s", s_name);
908 FAT_DPRINT (", start: 0x%x", START (dentptr));
909 FAT_DPRINT (", size: 0x%x %s\n",
910 FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
911
912 goto rootdir_done; /* We got a match */
913 }
914 cursect++;
915 }
916 rootdir_done:
917
918 firsttime = 1;
919 while (isdir) {
920 int startsect = mydata->data_begin
921 + START (dentptr) * mydata->clust_size;
922 dir_entry dent;
923 char *nextname = NULL;
924
925 dent = *dentptr;
926 dentptr = &dent;
927
928 idx = dirdelim (subname);
929 if (idx >= 0) {
930 subname[idx] = '\0';
931 nextname = subname + idx + 1;
932 /* Handle multiple delimiters */
933 while (ISDIRDELIM (*nextname))
934 nextname++;
935 if (dols && *nextname == '\0')
936 firsttime = 0;
937 } else {
938 if (dols && firsttime) {
939 firsttime = 0;
940 } else {
941 isdir = 0;
942 }
943 }
944
945 if (get_dentfromdir (mydata, startsect, subname, dentptr,
946 isdir ? 0 : dols) == NULL) {
947 if (dols && !isdir)
948 return 0;
949 return -1;
950 }
951
952 if (idx >= 0) {
953 if (!(dentptr->attr & ATTR_DIR))
954 return -1;
955 subname = nextname;
956 }
957 }
958 ret = get_contents (mydata, dentptr, buffer, maxsize);
959 FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
960
961 return ret;
962}
963
964
965int
966file_fat_detectfs(void)
967{
968 boot_sector bs;
969 volume_info volinfo;
970 int fatsize;
wdenk7205e402003-09-10 22:30:53 +0000971 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +0000972
wdenk7205e402003-09-10 22:30:53 +0000973 if(cur_dev==NULL) {
974 printf("No current device\n");
975 return 1;
976 }
wdenka2663ea2003-12-07 18:32:37 +0000977#if (CONFIG_COMMANDS & CFG_CMD_IDE) || (CONFIG_COMMANDS & CFG_CMD_SCSI) || \
978 (CONFIG_COMMANDS & CFG_CMD_USB) || (CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +0000979 printf("Interface: ");
980 switch(cur_dev->if_type) {
981 case IF_TYPE_IDE : printf("IDE"); break;
982 case IF_TYPE_SCSI : printf("SCSI"); break;
983 case IF_TYPE_ATAPI : printf("ATAPI"); break;
984 case IF_TYPE_USB : printf("USB"); break;
985 case IF_TYPE_DOC : printf("DOC"); break;
986 case IF_TYPE_MMC : printf("MMC"); break;
987 default : printf("Unknown");
988 }
989 printf("\n Device %d: ",cur_dev->dev);
990 dev_print(cur_dev);
991#endif
992 if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
993 printf("\nNo valid FAT fs found\n");
994 return 1;
995 }
996 memcpy (vol_label, volinfo.volume_label, 11);
997 vol_label[11] = '\0';
998 volinfo.fs_type[5]='\0';
999 printf("Partition %d: Filesystem: %s \"%s\"\n",cur_part,volinfo.fs_type,vol_label);
1000 return 0;
wdenk71f95112003-06-15 22:40:42 +00001001}
1002
1003
1004int
1005file_fat_ls(const char *dir)
1006{
1007 return do_fat_read(dir, NULL, 0, LS_YES);
1008}
1009
1010
1011long
1012file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1013{
wdenk7205e402003-09-10 22:30:53 +00001014 printf("reading %s\n",filename);
wdenk71f95112003-06-15 22:40:42 +00001015 return do_fat_read(filename, buffer, maxsize, LS_NO);
1016}
1017
1018#endif /* #if (CONFIG_COMMANDS & CFG_CMD_FAT) */