]> git.ipfire.org Git - thirdparty/u-boot.git/blame - fs/fat/fat.c
Merge branch 'sf' of git://git.denx.de/u-boot-blackfin
[thirdparty/u-boot.git] / fs / fat / fat.c
CommitLineData
71f95118
WD
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>
7205e407 32#include <part.h>
71f95118 33
71f95118
WD
34/*
35 * Convert a string to lowercase.
36 */
7385c28e 37static void downcase (char *str)
71f95118
WD
38{
39 while (*str != '\0') {
40 TOLOWER(*str);
41 str++;
42 }
43}
44
7385c28e
WD
45static block_dev_desc_t *cur_dev = NULL;
46
7205e407 47static unsigned long part_offset = 0;
7385c28e 48
7205e407
WD
49static int cur_part = 1;
50
51#define DOS_PART_TBL_OFFSET 0x1be
52#define DOS_PART_MAGIC_OFFSET 0x1fe
53#define DOS_FS_TYPE_OFFSET 0x36
66c2d73c 54#define DOS_FS32_TYPE_OFFSET 0x52
71f95118 55
7385c28e 56static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
71f95118 57{
7205e407
WD
58 if (cur_dev == NULL)
59 return -1;
7385c28e
WD
60
61 startblock += part_offset;
62
7205e407 63 if (cur_dev->block_read) {
7385c28e
WD
64 return cur_dev->block_read(cur_dev->dev, startblock, getsize,
65 (unsigned long *) bufptr);
71f95118
WD
66 }
67 return -1;
68}
69
7385c28e 70int fat_register_device (block_dev_desc_t * dev_desc, int part_no)
71f95118 71{
7205e407 72 unsigned char buffer[SECTOR_SIZE];
7385c28e 73
566a494f 74 disk_partition_t info;
7205e407
WD
75
76 if (!dev_desc->block_read)
77 return -1;
7385c28e 78
566a494f 79 cur_dev = dev_desc;
7205e407 80 /* check if we have a MBR (on floppies we have only a PBR) */
7385c28e
WD
81 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)buffer) != 1) {
82 printf("** Can't read from device %d **\n",
83 dev_desc->dev);
7205e407
WD
84 return -1;
85 }
86 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
7385c28e 87 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
7205e407
WD
88 /* no signature found */
89 return -1;
90 }
dd60d122 91#if (defined(CONFIG_CMD_IDE) || \
75eb82ec 92 defined(CONFIG_CMD_MG_DISK) || \
8c5170a7 93 defined(CONFIG_CMD_SATA) || \
dd60d122
JL
94 defined(CONFIG_CMD_SCSI) || \
95 defined(CONFIG_CMD_USB) || \
02df4a27
AF
96 defined(CONFIG_MMC) || \
97 defined(CONFIG_SYSTEMACE) )
7385c28e
WD
98 /* First we assume there is a MBR */
99 if (!get_partition_info(dev_desc, part_no, &info)) {
02df4a27
AF
100 part_offset = info.start;
101 cur_part = part_no;
7385c28e
WD
102 } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
103 (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
02df4a27
AF
104 /* ok, we assume we are on a PBR only */
105 cur_part = 1;
106 part_offset = 0;
107 } else {
7385c28e
WD
108 printf("** Partition %d not valid on device %d **\n",
109 part_no, dev_desc->dev);
02df4a27
AF
110 return -1;
111 }
112
7205e407 113#else
66c2d73c
WD
114 if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
115 (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
02df4a27
AF
116 /* ok, we assume we are on a PBR only */
117 cur_part = 1;
118 part_offset = 0;
119 info.start = part_offset;
120 } else {
121 /* FIXME we need to determine the start block of the
122 * partition where the DOS FS resides. This can be done
123 * by using the get_partition_info routine. For this
124 * purpose the libpart must be included.
125 */
126 part_offset = 32;
127 cur_part = 1;
bf1060ea 128 }
02df4a27 129#endif
71f95118
WD
130 return 0;
131}
132
71f95118
WD
133/*
134 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
135 * Return index into string if found, -1 otherwise.
136 */
7385c28e 137static int dirdelim (char *str)
71f95118
WD
138{
139 char *start = str;
140
141 while (*str != '\0') {
7385c28e
WD
142 if (ISDIRDELIM(*str))
143 return str - start;
71f95118
WD
144 str++;
145 }
146 return -1;
147}
148
71f95118
WD
149/*
150 * Extract zero terminated short name from a directory entry.
151 */
152static void get_name (dir_entry *dirent, char *s_name)
153{
154 char *ptr;
155
7385c28e 156 memcpy(s_name, dirent->name, 8);
71f95118
WD
157 s_name[8] = '\0';
158 ptr = s_name;
159 while (*ptr && *ptr != ' ')
160 ptr++;
161 if (dirent->ext[0] && dirent->ext[0] != ' ') {
162 *ptr = '.';
163 ptr++;
7385c28e 164 memcpy(ptr, dirent->ext, 3);
71f95118
WD
165 ptr[3] = '\0';
166 while (*ptr && *ptr != ' ')
167 ptr++;
168 }
169 *ptr = '\0';
170 if (*s_name == DELETED_FLAG)
171 *s_name = '\0';
172 else if (*s_name == aRING)
3c2c2f42 173 *s_name = DELETED_FLAG;
7385c28e 174 downcase(s_name);
71f95118
WD
175}
176
177/*
178 * Get the entry at index 'entry' in a FAT (12/16/32) table.
179 * On failure 0x00 is returned.
180 */
7385c28e 181static __u32 get_fatent (fsdata *mydata, __u32 entry)
71f95118
WD
182{
183 __u32 bufnum;
7385c28e 184 __u32 off16, offset;
71f95118 185 __u32 ret = 0x00;
7385c28e 186 __u16 val1, val2;
71f95118
WD
187
188 switch (mydata->fatsize) {
189 case 32:
190 bufnum = entry / FAT32BUFSIZE;
191 offset = entry - bufnum * FAT32BUFSIZE;
192 break;
193 case 16:
194 bufnum = entry / FAT16BUFSIZE;
195 offset = entry - bufnum * FAT16BUFSIZE;
196 break;
197 case 12:
198 bufnum = entry / FAT12BUFSIZE;
199 offset = entry - bufnum * FAT12BUFSIZE;
200 break;
201
202 default:
203 /* Unsupported FAT size */
204 return ret;
205 }
206
7385c28e
WD
207 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
208 mydata->fatsize, entry, entry, offset, offset);
2aa98c66 209
71f95118
WD
210 /* Read a new block of FAT entries into the cache. */
211 if (bufnum != mydata->fatbufnum) {
3f270f42 212 __u32 getsize = FATBUFSIZE / FS_BLOCK_SIZE;
71f95118
WD
213 __u8 *bufptr = mydata->fatbuf;
214 __u32 fatlength = mydata->fatlength;
215 __u32 startblock = bufnum * FATBUFBLOCKS;
216
217 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
218 startblock += mydata->fat_sect; /* Offset from start of disk */
219
7385c28e
WD
220 if (getsize > fatlength)
221 getsize = fatlength;
71f95118 222 if (disk_read(startblock, getsize, bufptr) < 0) {
7385c28e 223 debug("Error reading FAT blocks\n");
71f95118
WD
224 return ret;
225 }
226 mydata->fatbufnum = bufnum;
227 }
228
229 /* Get the actual entry from the table */
230 switch (mydata->fatsize) {
231 case 32:
7385c28e 232 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
71f95118
WD
233 break;
234 case 16:
7385c28e 235 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
71f95118 236 break;
7385c28e
WD
237 case 12:
238 off16 = (offset * 3) / 4;
71f95118
WD
239
240 switch (offset & 0x3) {
241 case 0:
7385c28e 242 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
71f95118
WD
243 ret &= 0xfff;
244 break;
245 case 1:
7385c28e 246 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
71f95118 247 val1 &= 0xf000;
7385c28e 248 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
71f95118
WD
249 val2 &= 0x00ff;
250 ret = (val2 << 4) | (val1 >> 12);
251 break;
252 case 2:
7385c28e 253 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
71f95118 254 val1 &= 0xff00;
7385c28e 255 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
71f95118
WD
256 val2 &= 0x000f;
257 ret = (val2 << 8) | (val1 >> 8);
258 break;
259 case 3:
7385c28e 260 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
71f95118
WD
261 ret = (ret & 0xfff0) >> 4;
262 break;
263 default:
264 break;
265 }
7385c28e 266 break;
71f95118 267 }
7385c28e
WD
268 debug("FAT%d: ret: %08x, offset: %04x\n",
269 mydata->fatsize, ret, offset);
71f95118
WD
270
271 return ret;
272}
273
71f95118
WD
274/*
275 * Read at most 'size' bytes from the specified cluster into 'buffer'.
276 * Return 0 on success, -1 otherwise.
277 */
278static int
7385c28e
WD
279get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
280 unsigned long size)
71f95118 281{
3f270f42 282 __u32 idx = 0;
71f95118
WD
283 __u32 startsect;
284
285 if (clustnum > 0) {
7385c28e
WD
286 startsect = mydata->data_begin +
287 clustnum * mydata->clust_size;
71f95118
WD
288 } else {
289 startsect = mydata->rootdir_sect;
290 }
291
7385c28e
WD
292 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
293
294 if (disk_read(startsect, size / FS_BLOCK_SIZE, buffer) < 0) {
295 debug("Error reading data\n");
7205e407
WD
296 return -1;
297 }
7385c28e 298 if (size % FS_BLOCK_SIZE) {
7205e407 299 __u8 tmpbuf[FS_BLOCK_SIZE];
7385c28e
WD
300
301 idx = size / FS_BLOCK_SIZE;
7205e407 302 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
7385c28e 303 debug("Error reading data\n");
7205e407 304 return -1;
71f95118 305 }
7385c28e 306 buffer += idx * FS_BLOCK_SIZE;
7205e407
WD
307
308 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
309 return 0;
71f95118
WD
310 }
311
312 return 0;
313}
314
71f95118
WD
315/*
316 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
317 * into 'buffer'.
318 * Return the number of bytes read or -1 on fatal errors.
319 */
320static long
7385c28e
WD
321get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
322 unsigned long maxsize)
71f95118
WD
323{
324 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
325 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
326 __u32 curclust = START(dentptr);
7205e407
WD
327 __u32 endclust, newclust;
328 unsigned long actsize;
71f95118 329
7385c28e 330 debug("Filesize: %ld bytes\n", filesize);
71f95118 331
7385c28e
WD
332 if (maxsize > 0 && filesize > maxsize)
333 filesize = maxsize;
71f95118 334
7385c28e
WD
335 debug("%ld bytes\n", filesize);
336
337 actsize = bytesperclust;
338 endclust = curclust;
71f95118
WD
339
340 do {
7205e407 341 /* search for consecutive clusters */
7385c28e 342 while (actsize < filesize) {
7205e407 343 newclust = get_fatent(mydata, endclust);
7385c28e 344 if ((newclust - 1) != endclust)
7205e407 345 goto getit;
8ce4e5c2 346 if (CHECK_CLUST(newclust, mydata->fatsize)) {
7385c28e
WD
347 debug("curclust: 0x%x\n", newclust);
348 debug("Invalid FAT entry\n");
7205e407
WD
349 return gotsize;
350 }
7385c28e
WD
351 endclust = newclust;
352 actsize += bytesperclust;
7205e407 353 }
7385c28e 354
7205e407
WD
355 /* actsize >= file size */
356 actsize -= bytesperclust;
7385c28e 357
7205e407
WD
358 /* get remaining clusters */
359 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
7385c28e 360 printf("Error reading cluster\n");
71f95118
WD
361 return -1;
362 }
7385c28e 363
7205e407
WD
364 /* get remaining bytes */
365 gotsize += (int)actsize;
366 filesize -= actsize;
367 buffer += actsize;
7385c28e 368 actsize = filesize;
7205e407 369 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
7385c28e 370 printf("Error reading cluster\n");
7205e407
WD
371 return -1;
372 }
7385c28e 373 gotsize += actsize;
7205e407
WD
374 return gotsize;
375getit:
376 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
7385c28e 377 printf("Error reading cluster\n");
7205e407
WD
378 return -1;
379 }
380 gotsize += (int)actsize;
381 filesize -= actsize;
382 buffer += actsize;
7385c28e 383
7205e407 384 curclust = get_fatent(mydata, endclust);
8ce4e5c2 385 if (CHECK_CLUST(curclust, mydata->fatsize)) {
7385c28e
WD
386 debug("curclust: 0x%x\n", curclust);
387 printf("Invalid FAT entry\n");
71f95118
WD
388 return gotsize;
389 }
7385c28e
WD
390 actsize = bytesperclust;
391 endclust = curclust;
71f95118
WD
392 } while (1);
393}
394
71f95118
WD
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 */
7385c28e 401static int slot2str (dir_slot *slotptr, char *l_name, int *idx)
71f95118
WD
402{
403 int j;
404
405 for (j = 0; j <= 8; j += 2) {
406 l_name[*idx] = slotptr->name0_4[j];
7385c28e
WD
407 if (l_name[*idx] == 0x00)
408 return 1;
71f95118
WD
409 (*idx)++;
410 }
411 for (j = 0; j <= 10; j += 2) {
412 l_name[*idx] = slotptr->name5_10[j];
7385c28e
WD
413 if (l_name[*idx] == 0x00)
414 return 1;
71f95118
WD
415 (*idx)++;
416 }
417 for (j = 0; j <= 2; j += 2) {
418 l_name[*idx] = slotptr->name11_12[j];
7385c28e
WD
419 if (l_name[*idx] == 0x00)
420 return 1;
71f95118
WD
421 (*idx)++;
422 }
423
424 return 0;
425}
426
71f95118
WD
427/*
428 * Extract the full long filename starting at 'retdent' (which is really
429 * a slot) into 'l_name'. If successful also copy the real directory entry
430 * into 'retdent'
431 * Return 0 on success, -1 otherwise.
432 */
7385c28e 433__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
7e4b9b4f 434__u8 get_vfatname_block[MAX_CLUSTSIZE];
7385c28e 435
71f95118 436static int
7385c28e
WD
437get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
438 dir_entry *retdent, char *l_name)
71f95118
WD
439{
440 dir_entry *realdent;
7385c28e 441 dir_slot *slotptr = (dir_slot *)retdent;
3831530d
MZ
442 __u8 *buflimit = cluster + ((curclust == 0) ?
443 LINEAR_PREFETCH_SIZE :
444 (mydata->clust_size * SECTOR_SIZE)
445 );
7385c28e 446 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
71f95118
WD
447 int idx = 0;
448
3831530d
MZ
449 if (counter > VFAT_MAXSEQ) {
450 debug("Error: VFAT name is too long\n");
451 return -1;
452 }
453
454 while ((__u8 *)slotptr < buflimit) {
7385c28e
WD
455 if (counter == 0)
456 break;
2d1a537d
WD
457 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
458 return -1;
71f95118
WD
459 slotptr++;
460 counter--;
461 }
462
3831530d 463 if ((__u8 *)slotptr >= buflimit) {
71f95118
WD
464 dir_slot *slotptr2;
465
3831530d
MZ
466 if (curclust == 0)
467 return -1;
71f95118 468 curclust = get_fatent(mydata, curclust);
8ce4e5c2 469 if (CHECK_CLUST(curclust, mydata->fatsize)) {
7385c28e
WD
470 debug("curclust: 0x%x\n", curclust);
471 printf("Invalid FAT entry\n");
71f95118
WD
472 return -1;
473 }
7385c28e 474
5fa66df6 475 if (get_cluster(mydata, curclust, get_vfatname_block,
71f95118 476 mydata->clust_size * SECTOR_SIZE) != 0) {
7385c28e 477 debug("Error: reading directory block\n");
71f95118
WD
478 return -1;
479 }
7385c28e
WD
480
481 slotptr2 = (dir_slot *)get_vfatname_block;
3831530d
MZ
482 while (counter > 0) {
483 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
484 & 0xff) != counter)
485 return -1;
71f95118 486 slotptr2++;
3831530d
MZ
487 counter--;
488 }
7385c28e 489
71f95118 490 /* Save the real directory entry */
3831530d
MZ
491 realdent = (dir_entry *)slotptr2;
492 while ((__u8 *)slotptr2 > get_vfatname_block) {
71f95118 493 slotptr2--;
3831530d 494 slot2str(slotptr2, l_name, &idx);
71f95118
WD
495 }
496 } else {
497 /* Save the real directory entry */
7385c28e 498 realdent = (dir_entry *)slotptr;
71f95118
WD
499 }
500
501 do {
502 slotptr--;
7385c28e
WD
503 if (slot2str(slotptr, l_name, &idx))
504 break;
2d1a537d 505 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
71f95118
WD
506
507 l_name[idx] = '\0';
7385c28e
WD
508 if (*l_name == DELETED_FLAG)
509 *l_name = '\0';
510 else if (*l_name == aRING)
511 *l_name = DELETED_FLAG;
71f95118
WD
512 downcase(l_name);
513
514 /* Return the real directory entry */
515 memcpy(retdent, realdent, sizeof(dir_entry));
516
517 return 0;
518}
519
71f95118 520/* Calculate short name checksum */
7385c28e 521static __u8 mkcksum (const char *str)
71f95118
WD
522{
523 int i;
7385c28e 524
71f95118
WD
525 __u8 ret = 0;
526
527 for (i = 0; i < 11; i++) {
7385c28e 528 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
71f95118
WD
529 }
530
531 return ret;
532}
7385c28e 533#endif /* CONFIG_SUPPORT_VFAT */
71f95118
WD
534
535/*
536 * Get the directory entry associated with 'filename' from the directory
537 * starting at 'startsect'
538 */
7385c28e 539__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
5fa66df6 540__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
7385c28e
WD
541
542static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
543 char *filename, dir_entry *retdent,
71f95118
WD
544 int dols)
545{
7385c28e
WD
546 __u16 prevcksum = 0xffff;
547 __u32 curclust = START(retdent);
548 int files = 0, dirs = 0;
71f95118 549
7385c28e 550 debug("get_dentfromdir: %s\n", filename);
71f95118 551
7385c28e
WD
552 while (1) {
553 dir_entry *dentptr;
554
555 int i;
556
557 if (get_cluster(mydata, curclust, get_dentfromdir_block,
558 mydata->clust_size * SECTOR_SIZE) != 0) {
559 debug("Error: reading directory block\n");
560 return NULL;
561 }
562
563 dentptr = (dir_entry *)get_dentfromdir_block;
564
565 for (i = 0; i < DIRENTSPERCLUST; i++) {
3831530d 566 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
7385c28e
WD
567
568 l_name[0] = '\0';
569 if (dentptr->name[0] == DELETED_FLAG) {
570 dentptr++;
571 continue;
572 }
573 if ((dentptr->attr & ATTR_VOLUME)) {
71f95118 574#ifdef CONFIG_SUPPORT_VFAT
7385c28e
WD
575 if ((dentptr->attr & ATTR_VFAT) &&
576 (dentptr-> name[0] & LAST_LONG_ENTRY_MASK)) {
577 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
578 get_vfatname(mydata, curclust,
579 get_dentfromdir_block,
580 dentptr, l_name);
581 if (dols) {
582 int isdir;
583 char dirc;
584 int doit = 0;
585
586 isdir = (dentptr->attr & ATTR_DIR);
587
588 if (isdir) {
589 dirs++;
590 dirc = '/';
591 doit = 1;
592 } else {
593 dirc = ' ';
594 if (l_name[0] != 0) {
595 files++;
596 doit = 1;
597 }
598 }
599 if (doit) {
600 if (dirc == ' ') {
601 printf(" %8ld %s%c\n",
602 (long)FAT2CPU32(dentptr->size),
603 l_name,
604 dirc);
605 } else {
606 printf(" %s%c\n",
607 l_name,
608 dirc);
609 }
610 }
611 dentptr++;
612 continue;
613 }
614 debug("vfatname: |%s|\n", l_name);
615 } else
616#endif
617 {
618 /* Volume label or VFAT entry */
619 dentptr++;
620 continue;
621 }
71f95118 622 }
7385c28e
WD
623 if (dentptr->name[0] == 0) {
624 if (dols) {
625 printf("\n%d file(s), %d dir(s)\n\n",
626 files, dirs);
627 }
628 debug("Dentname == NULL - %d\n", i);
629 return NULL;
71f95118 630 }
71f95118 631#ifdef CONFIG_SUPPORT_VFAT
7385c28e
WD
632 if (dols && mkcksum(dentptr->name) == prevcksum) {
633 dentptr++;
634 continue;
635 }
71f95118 636#endif
7385c28e
WD
637 get_name(dentptr, s_name);
638 if (dols) {
639 int isdir = (dentptr->attr & ATTR_DIR);
640 char dirc;
641 int doit = 0;
642
643 if (isdir) {
644 dirs++;
645 dirc = '/';
646 doit = 1;
647 } else {
648 dirc = ' ';
649 if (s_name[0] != 0) {
650 files++;
651 doit = 1;
652 }
653 }
654
655 if (doit) {
656 if (dirc == ' ') {
657 printf(" %8ld %s%c\n",
658 (long)FAT2CPU32(dentptr->size),
659 s_name, dirc);
660 } else {
661 printf(" %s%c\n",
662 s_name, dirc);
663 }
664 }
665
666 dentptr++;
667 continue;
668 }
669
670 if (strcmp(filename, s_name)
671 && strcmp(filename, l_name)) {
672 debug("Mismatch: |%s|%s|\n", s_name, l_name);
673 dentptr++;
674 continue;
675 }
676
677 memcpy(retdent, dentptr, sizeof(dir_entry));
678
679 debug("DentName: %s", s_name);
680 debug(", start: 0x%x", START(dentptr));
681 debug(", size: 0x%x %s\n",
682 FAT2CPU32(dentptr->size),
683 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
684
685 return retdent;
71f95118 686 }
7385c28e
WD
687
688 curclust = get_fatent(mydata, curclust);
689 if (CHECK_CLUST(curclust, mydata->fatsize)) {
690 debug("curclust: 0x%x\n", curclust);
691 printf("Invalid FAT entry\n");
692 return NULL;
71f95118 693 }
71f95118 694 }
71f95118 695
7385c28e 696 return NULL;
71f95118
WD
697}
698
71f95118
WD
699/*
700 * Read boot sector and volume info from a FAT filesystem
701 */
702static int
7385c28e 703read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
71f95118
WD
704{
705 __u8 block[FS_BLOCK_SIZE];
7385c28e 706
71f95118
WD
707 volume_info *vistart;
708
7385c28e
WD
709 if (disk_read (0, 1, block) < 0) {
710 debug("Error: reading block\n");
71f95118
WD
711 return -1;
712 }
713
714 memcpy(bs, block, sizeof(boot_sector));
7385c28e
WD
715 bs->reserved = FAT2CPU16(bs->reserved);
716 bs->fat_length = FAT2CPU16(bs->fat_length);
717 bs->secs_track = FAT2CPU16(bs->secs_track);
718 bs->heads = FAT2CPU16(bs->heads);
719 bs->total_sect = FAT2CPU32(bs->total_sect);
71f95118
WD
720
721 /* FAT32 entries */
722 if (bs->fat_length == 0) {
723 /* Assume FAT32 */
724 bs->fat32_length = FAT2CPU32(bs->fat32_length);
7385c28e 725 bs->flags = FAT2CPU16(bs->flags);
71f95118 726 bs->root_cluster = FAT2CPU32(bs->root_cluster);
7385c28e
WD
727 bs->info_sector = FAT2CPU16(bs->info_sector);
728 bs->backup_boot = FAT2CPU16(bs->backup_boot);
729 vistart = (volume_info *)(block + sizeof(boot_sector));
71f95118
WD
730 *fatsize = 32;
731 } else {
7385c28e 732 vistart = (volume_info *)&(bs->fat32_length);
71f95118
WD
733 *fatsize = 0;
734 }
735 memcpy(volinfo, vistart, sizeof(volume_info));
736
71f95118 737 if (*fatsize == 32) {
7385c28e 738 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
71f95118 739 return 0;
71f95118 740 } else {
651351fe 741 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
71f95118
WD
742 *fatsize = 12;
743 return 0;
744 }
651351fe 745 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
71f95118
WD
746 *fatsize = 16;
747 return 0;
748 }
749 }
750
7385c28e 751 debug("Error: broken fs_type sign\n");
71f95118
WD
752 return -1;
753}
754
7385c28e 755__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
7e4b9b4f 756__u8 do_fat_read_block[MAX_CLUSTSIZE];
7385c28e 757
20cc00dd 758long
71f95118
WD
759do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
760 int dols)
761{
7385c28e
WD
762 char fnamecopy[2048];
763 boot_sector bs;
764 volume_info volinfo;
765 fsdata datablock;
766 fsdata *mydata = &datablock;
767 dir_entry *dentptr;
768 __u16 prevcksum = 0xffff;
769 char *subname = "";
3f270f42 770 __u32 cursect;
7385c28e
WD
771 int idx, isdir = 0;
772 int files = 0, dirs = 0;
773 long ret = 0;
774 int firsttime;
3f270f42
EH
775 __u32 root_cluster;
776 int rootdir_size = 0;
7385c28e
WD
777 int j;
778
779 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
780 debug("Error: reading boot sector\n");
781 return -1;
782 }
783
2aa98c66 784 root_cluster = bs.root_cluster;
7385c28e
WD
785
786 if (mydata->fatsize == 32)
787 mydata->fatlength = bs.fat32_length;
788 else
789 mydata->fatlength = bs.fat_length;
790
791 mydata->fat_sect = bs.reserved;
792
793 cursect = mydata->rootdir_sect
794 = mydata->fat_sect + mydata->fatlength * bs.fats;
795
796 mydata->clust_size = bs.cluster_size;
797
798 if (mydata->fatsize == 32) {
799 mydata->data_begin = mydata->rootdir_sect -
800 (mydata->clust_size * 2);
801 } else {
7385c28e
WD
802 rootdir_size = ((bs.dir_entries[1] * (int)256 +
803 bs.dir_entries[0]) *
804 sizeof(dir_entry)) /
805 SECTOR_SIZE;
806 mydata->data_begin = mydata->rootdir_sect +
807 rootdir_size -
808 (mydata->clust_size * 2);
809 }
810
811 mydata->fatbufnum = -1;
71f95118 812
2aa98c66 813#ifdef CONFIG_SUPPORT_VFAT
7385c28e 814 debug("VFAT Support enabled\n");
2aa98c66 815#endif
7385c28e
WD
816 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
817 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
818 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
819 "Data begins at: %d\n",
820 root_cluster,
821 mydata->rootdir_sect,
822 mydata->rootdir_sect * SECTOR_SIZE, mydata->data_begin);
823 debug("Cluster size: %d\n", mydata->clust_size);
824
825 /* "cwd" is always the root... */
826 while (ISDIRDELIM(*filename))
827 filename++;
828
829 /* Make a copy of the filename and convert it to lowercase */
830 strcpy(fnamecopy, filename);
831 downcase(fnamecopy);
832
833 if (*fnamecopy == '\0') {
834 if (!dols)
835 return -1;
71f95118 836
7385c28e
WD
837 dols = LS_ROOT;
838 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
839 isdir = 1;
840 fnamecopy[idx] = '\0';
841 subname = fnamecopy + idx + 1;
842
843 /* Handle multiple delimiters */
844 while (ISDIRDELIM(*subname))
845 subname++;
846 } else if (dols) {
847 isdir = 1;
71f95118 848 }
71f95118 849
7385c28e
WD
850 j = 0;
851 while (1) {
852 int i;
853
854 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%d\n",
855 cursect, mydata->clust_size, DIRENTSPERBLOCK);
856
3831530d
MZ
857 if (disk_read(cursect,
858 (mydata->fatsize == 32) ?
859 (mydata->clust_size) :
11c8dd36 860 LINEAR_PREFETCH_SIZE / SECTOR_SIZE,
3831530d 861 do_fat_read_block) < 0) {
7385c28e
WD
862 debug("Error: reading rootdir block\n");
863 return -1;
864 }
865
866 dentptr = (dir_entry *) do_fat_read_block;
867
868 for (i = 0; i < DIRENTSPERBLOCK; i++) {
3831530d 869 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
7385c28e
WD
870
871 l_name[0] = '\0';
3831530d
MZ
872 if (dentptr->name[0] == DELETED_FLAG) {
873 dentptr++;
874 continue;
875 }
7385c28e 876 if ((dentptr->attr & ATTR_VOLUME)) {
71f95118 877#ifdef CONFIG_SUPPORT_VFAT
7385c28e
WD
878 if ((dentptr->attr & ATTR_VFAT) &&
879 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
880 prevcksum =
881 ((dir_slot *)dentptr)->alias_checksum;
882
3831530d
MZ
883 get_vfatname(mydata,
884 (mydata->fatsize == 32) ?
885 root_cluster :
886 0,
7385c28e
WD
887 do_fat_read_block,
888 dentptr, l_name);
889
890 if (dols == LS_ROOT) {
891 char dirc;
892 int doit = 0;
893 int isdir =
894 (dentptr->attr & ATTR_DIR);
895
896 if (isdir) {
897 dirs++;
898 dirc = '/';
899 doit = 1;
900 } else {
901 dirc = ' ';
902 if (l_name[0] != 0) {
903 files++;
904 doit = 1;
905 }
906 }
907 if (doit) {
908 if (dirc == ' ') {
909 printf(" %8ld %s%c\n",
910 (long)FAT2CPU32(dentptr->size),
911 l_name,
912 dirc);
913 } else {
914 printf(" %s%c\n",
915 l_name,
916 dirc);
917 }
918 }
919 dentptr++;
920 continue;
921 }
922 debug("Rootvfatname: |%s|\n",
923 l_name);
924 } else
925#endif
926 {
927 /* Volume label or VFAT entry */
928 dentptr++;
929 continue;
930 }
931 } else if (dentptr->name[0] == 0) {
932 debug("RootDentname == NULL - %d\n", i);
933 if (dols == LS_ROOT) {
934 printf("\n%d file(s), %d dir(s)\n\n",
935 files, dirs);
936 return 0;
937 }
938 return -1;
71f95118 939 }
7385c28e
WD
940#ifdef CONFIG_SUPPORT_VFAT
941 else if (dols == LS_ROOT &&
942 mkcksum(dentptr->name) == prevcksum) {
943 dentptr++;
944 continue;
71f95118 945 }
71f95118 946#endif
7385c28e
WD
947 get_name(dentptr, s_name);
948
949 if (dols == LS_ROOT) {
950 int isdir = (dentptr->attr & ATTR_DIR);
951 char dirc;
952 int doit = 0;
953
954 if (isdir) {
955 dirc = '/';
956 if (s_name[0] != 0) {
957 dirs++;
958 doit = 1;
959 }
960 } else {
961 dirc = ' ';
962 if (s_name[0] != 0) {
963 files++;
964 doit = 1;
965 }
966 }
967 if (doit) {
968 if (dirc == ' ') {
969 printf(" %8ld %s%c\n",
970 (long)FAT2CPU32(dentptr->size),
971 s_name, dirc);
972 } else {
973 printf(" %s%c\n",
974 s_name, dirc);
975 }
976 }
977 dentptr++;
978 continue;
979 }
980
981 if (strcmp(fnamecopy, s_name)
982 && strcmp(fnamecopy, l_name)) {
983 debug("RootMismatch: |%s|%s|\n", s_name,
984 l_name);
985 dentptr++;
986 continue;
987 }
988
989 if (isdir && !(dentptr->attr & ATTR_DIR))
990 return -1;
991
992 debug("RootName: %s", s_name);
993 debug(", start: 0x%x", START(dentptr));
994 debug(", size: 0x%x %s\n",
995 FAT2CPU32(dentptr->size),
996 isdir ? "(DIR)" : "");
997
998 goto rootdir_done; /* We got a match */
71f95118 999 }
7385c28e
WD
1000 debug("END LOOP: j=%d clust_size=%d\n", j,
1001 mydata->clust_size);
1002
1003 /*
1004 * On FAT32 we must fetch the FAT entries for the next
1005 * root directory clusters when a cluster has been
1006 * completely processed.
1007 */
3f270f42
EH
1008 ++j;
1009 int fat32_end = 0;
1010 if ((mydata->fatsize == 32) && (j == mydata->clust_size)) {
1011 int nxtsect = 0;
1012 int nxt_clust = 0;
7385c28e
WD
1013
1014 nxt_clust = get_fatent(mydata, root_cluster);
3f270f42 1015 fat32_end = CHECK_CLUST(nxt_clust, 32);
7385c28e
WD
1016
1017 nxtsect = mydata->data_begin +
1018 (nxt_clust * mydata->clust_size);
1019
7385c28e
WD
1020 root_cluster = nxt_clust;
1021
1022 cursect = nxtsect;
1023 j = 0;
71f95118 1024 } else {
7385c28e 1025 cursect++;
71f95118 1026 }
3f270f42
EH
1027
1028 /* If end of rootdir reached */
1029 if ((mydata->fatsize == 32 && fat32_end) ||
1030 (mydata->fatsize != 32 && j == rootdir_size)) {
1031 if (dols == LS_ROOT) {
1032 printf("\n%d file(s), %d dir(s)\n\n",
1033 files, dirs);
1034 return 0;
1035 } else {
1036 return -1;
1037 }
1038 }
7385c28e
WD
1039 }
1040rootdir_done:
71f95118 1041
7385c28e 1042 firsttime = 1;
71f95118 1043
7385c28e
WD
1044 while (isdir) {
1045 int startsect = mydata->data_begin
1046 + START(dentptr) * mydata->clust_size;
1047 dir_entry dent;
1048 char *nextname = NULL;
71f95118 1049
7385c28e
WD
1050 dent = *dentptr;
1051 dentptr = &dent;
71f95118 1052
7385c28e
WD
1053 idx = dirdelim(subname);
1054
1055 if (idx >= 0) {
1056 subname[idx] = '\0';
1057 nextname = subname + idx + 1;
1058 /* Handle multiple delimiters */
1059 while (ISDIRDELIM(*nextname))
1060 nextname++;
1061 if (dols && *nextname == '\0')
1062 firsttime = 0;
1063 } else {
1064 if (dols && firsttime) {
1065 firsttime = 0;
1066 } else {
1067 isdir = 0;
1068 }
1069 }
1070
1071 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1072 isdir ? 0 : dols) == NULL) {
1073 if (dols && !isdir)
1074 return 0;
1075 return -1;
1076 }
1077
1078 if (idx >= 0) {
1079 if (!(dentptr->attr & ATTR_DIR))
1080 return -1;
1081 subname = nextname;
1082 }
71f95118 1083 }
71f95118 1084
7385c28e
WD
1085 ret = get_contents(mydata, dentptr, buffer, maxsize);
1086 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
71f95118 1087
7385c28e
WD
1088 return ret;
1089}
71f95118 1090
7385c28e 1091int file_fat_detectfs (void)
71f95118 1092{
7385c28e
WD
1093 boot_sector bs;
1094 volume_info volinfo;
1095 int fatsize;
1096 char vol_label[12];
71f95118 1097
7385c28e 1098 if (cur_dev == NULL) {
7205e407
WD
1099 printf("No current device\n");
1100 return 1;
1101 }
7385c28e 1102
dd60d122 1103#if defined(CONFIG_CMD_IDE) || \
75eb82ec 1104 defined(CONFIG_CMD_MG_DISK) || \
8c5170a7 1105 defined(CONFIG_CMD_SATA) || \
dd60d122
JL
1106 defined(CONFIG_CMD_SCSI) || \
1107 defined(CONFIG_CMD_USB) || \
21f6f963 1108 defined(CONFIG_MMC)
7205e407 1109 printf("Interface: ");
7385c28e
WD
1110 switch (cur_dev->if_type) {
1111 case IF_TYPE_IDE:
1112 printf("IDE");
1113 break;
1114 case IF_TYPE_SATA:
1115 printf("SATA");
1116 break;
1117 case IF_TYPE_SCSI:
1118 printf("SCSI");
1119 break;
1120 case IF_TYPE_ATAPI:
1121 printf("ATAPI");
1122 break;
1123 case IF_TYPE_USB:
1124 printf("USB");
1125 break;
1126 case IF_TYPE_DOC:
1127 printf("DOC");
1128 break;
1129 case IF_TYPE_MMC:
1130 printf("MMC");
1131 break;
1132 default:
1133 printf("Unknown");
7205e407 1134 }
7385c28e
WD
1135
1136 printf("\n Device %d: ", cur_dev->dev);
7205e407
WD
1137 dev_print(cur_dev);
1138#endif
7385c28e
WD
1139
1140 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
7205e407
WD
1141 printf("\nNo valid FAT fs found\n");
1142 return 1;
1143 }
7385c28e
WD
1144
1145 memcpy(vol_label, volinfo.volume_label, 11);
7205e407 1146 vol_label[11] = '\0';
7385c28e
WD
1147 volinfo.fs_type[5] = '\0';
1148
1149 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part,
1150 volinfo.fs_type, vol_label);
1151
7205e407 1152 return 0;
71f95118
WD
1153}
1154
7385c28e 1155int file_fat_ls (const char *dir)
71f95118
WD
1156{
1157 return do_fat_read(dir, NULL, 0, LS_YES);
1158}
1159
7385c28e 1160long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
71f95118 1161{
7385c28e 1162 printf("reading %s\n", filename);
71f95118
WD
1163 return do_fat_read(filename, buffer, maxsize, LS_NO);
1164}