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