]> git.ipfire.org Git - people/ms/u-boot.git/blame - fs/fat/fat.c
fs/fat: Reduce stack usage
[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 *
1a459660 9 * SPDX-License-Identifier: GPL-2.0+
71f95118
WD
10 */
11
12#include <common.h>
2a981dc2 13#include <blk.h>
71f95118 14#include <config.h>
ac497771 15#include <exports.h>
71f95118 16#include <fat.h>
1f40366b 17#include <fs.h>
71f95118 18#include <asm/byteorder.h>
7205e407 19#include <part.h>
9a800ac7 20#include <malloc.h>
cf92e05c 21#include <memalign.h>
9a800ac7 22#include <linux/compiler.h>
fb7e16cc 23#include <linux/ctype.h>
71f95118 24
cb940c7e
RG
25#ifdef CONFIG_SUPPORT_VFAT
26static const int vfat_enabled = 1;
27#else
28static const int vfat_enabled = 0;
29#endif
30
71f95118 31/*
21a24c3b
RC
32 * Convert a string to lowercase. Converts at most 'len' characters,
33 * 'len' may be larger than the length of 'str' if 'str' is NULL
34 * terminated.
71f95118 35 */
21a24c3b 36static void downcase(char *str, size_t len)
71f95118 37{
21a24c3b 38 while (*str != '\0' && len--) {
fb7e16cc 39 *str = tolower(*str);
71f95118
WD
40 str++;
41 }
42}
43
4101f687 44static struct blk_desc *cur_dev;
9813b750 45static disk_partition_t cur_part_info;
7385c28e 46
9813b750 47#define DOS_BOOT_MAGIC_OFFSET 0x1fe
7205e407 48#define DOS_FS_TYPE_OFFSET 0x36
66c2d73c 49#define DOS_FS32_TYPE_OFFSET 0x52
71f95118 50
9813b750 51static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
71f95118 52{
0a04ed86
ŁM
53 ulong ret;
54
2a981dc2 55 if (!cur_dev)
7205e407 56 return -1;
7385c28e 57
2a981dc2 58 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
0a04ed86 59
42a9f147 60 if (ret != nr_blocks)
0a04ed86
ŁM
61 return -1;
62
63 return ret;
71f95118
WD
64}
65
4101f687 66int fat_set_blk_dev(struct blk_desc *dev_desc, disk_partition_t *info)
71f95118 67{
9a800ac7 68 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
7205e407 69
5e8f9831
SW
70 cur_dev = dev_desc;
71 cur_part_info = *info;
9813b750
KM
72
73 /* Make sure it has a valid FAT header */
74 if (disk_read(0, 1, buffer) != 1) {
75 cur_dev = NULL;
76 return -1;
bf1060ea 77 }
9813b750
KM
78
79 /* Check if it's actually a DOS volume */
80 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
81 cur_dev = NULL;
82 return -1;
83 }
84
85 /* Check for FAT12/FAT16/FAT32 filesystem */
86 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
87 return 0;
88 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
89 return 0;
90
91 cur_dev = NULL;
92 return -1;
71f95118
WD
93}
94
4101f687 95int fat_register_device(struct blk_desc *dev_desc, int part_no)
5e8f9831
SW
96{
97 disk_partition_t info;
98
99 /* First close any currently found FAT filesystem */
100 cur_dev = NULL;
101
102 /* Read the partition table, if present */
3e8bd469 103 if (part_get_info(dev_desc, part_no, &info)) {
5e8f9831
SW
104 if (part_no != 0) {
105 printf("** Partition %d not valid on device %d **\n",
bcce53d0 106 part_no, dev_desc->devnum);
5e8f9831
SW
107 return -1;
108 }
109
110 info.start = 0;
111 info.size = dev_desc->lba;
112 info.blksz = dev_desc->blksz;
113 info.name[0] = 0;
114 info.type[0] = 0;
115 info.bootable = 0;
b331cd62 116#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
5e8f9831
SW
117 info.uuid[0] = 0;
118#endif
119 }
120
121 return fat_set_blk_dev(dev_desc, &info);
122}
9813b750 123
71f95118
WD
124/*
125 * Extract zero terminated short name from a directory entry.
126 */
9795e07b 127static void get_name(dir_entry *dirent, char *s_name)
71f95118
WD
128{
129 char *ptr;
130
7385c28e 131 memcpy(s_name, dirent->name, 8);
71f95118
WD
132 s_name[8] = '\0';
133 ptr = s_name;
134 while (*ptr && *ptr != ' ')
135 ptr++;
21a24c3b
RC
136 if (dirent->lcase & CASE_LOWER_BASE)
137 downcase(s_name, (unsigned)(ptr - s_name));
71f95118 138 if (dirent->ext[0] && dirent->ext[0] != ' ') {
21a24c3b 139 *ptr++ = '.';
7385c28e 140 memcpy(ptr, dirent->ext, 3);
21a24c3b
RC
141 if (dirent->lcase & CASE_LOWER_EXT)
142 downcase(ptr, 3);
71f95118
WD
143 ptr[3] = '\0';
144 while (*ptr && *ptr != ' ')
145 ptr++;
146 }
147 *ptr = '\0';
148 if (*s_name == DELETED_FLAG)
149 *s_name = '\0';
150 else if (*s_name == aRING)
3c2c2f42 151 *s_name = DELETED_FLAG;
71f95118
WD
152}
153
b8948d2a
SB
154static int flush_dirty_fat_buffer(fsdata *mydata);
155#if !defined(CONFIG_FAT_WRITE)
156/* Stub for read only operation */
157int flush_dirty_fat_buffer(fsdata *mydata)
158{
159 (void)(mydata);
160 return 0;
161}
162#endif
163
71f95118
WD
164/*
165 * Get the entry at index 'entry' in a FAT (12/16/32) table.
166 * On failure 0x00 is returned.
167 */
9795e07b 168static __u32 get_fatent(fsdata *mydata, __u32 entry)
71f95118
WD
169{
170 __u32 bufnum;
b352caea 171 __u32 offset, off8;
71f95118
WD
172 __u32 ret = 0x00;
173
b8948d2a
SB
174 if (CHECK_CLUST(entry, mydata->fatsize)) {
175 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
176 return ret;
177 }
178
71f95118
WD
179 switch (mydata->fatsize) {
180 case 32:
181 bufnum = entry / FAT32BUFSIZE;
182 offset = entry - bufnum * FAT32BUFSIZE;
183 break;
184 case 16:
185 bufnum = entry / FAT16BUFSIZE;
186 offset = entry - bufnum * FAT16BUFSIZE;
187 break;
188 case 12:
189 bufnum = entry / FAT12BUFSIZE;
190 offset = entry - bufnum * FAT12BUFSIZE;
191 break;
192
193 default:
194 /* Unsupported FAT size */
195 return ret;
196 }
197
b8948d2a 198 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
7385c28e 199 mydata->fatsize, entry, entry, offset, offset);
2aa98c66 200
71f95118
WD
201 /* Read a new block of FAT entries into the cache. */
202 if (bufnum != mydata->fatbufnum) {
60b36f0f 203 __u32 getsize = FATBUFBLOCKS;
71f95118
WD
204 __u8 *bufptr = mydata->fatbuf;
205 __u32 fatlength = mydata->fatlength;
206 __u32 startblock = bufnum * FATBUFBLOCKS;
207
6c1a8080 208 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
8006dd2e
BT
209 if (startblock + getsize > fatlength)
210 getsize = fatlength - startblock;
60b36f0f 211
71f95118
WD
212 startblock += mydata->fat_sect; /* Offset from start of disk */
213
b8948d2a
SB
214 /* Write back the fatbuf to the disk */
215 if (flush_dirty_fat_buffer(mydata) < 0)
216 return -1;
217
71f95118 218 if (disk_read(startblock, getsize, bufptr) < 0) {
7385c28e 219 debug("Error reading FAT blocks\n");
71f95118
WD
220 return ret;
221 }
222 mydata->fatbufnum = bufnum;
223 }
224
225 /* Get the actual entry from the table */
226 switch (mydata->fatsize) {
227 case 32:
7385c28e 228 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
71f95118
WD
229 break;
230 case 16:
7385c28e 231 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
71f95118 232 break;
7385c28e 233 case 12:
b352caea
SB
234 off8 = (offset * 3) / 2;
235 /* fatbut + off8 may be unaligned, read in byte granularity */
236 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
8d48c92b
SB
237
238 if (offset & 0x1)
239 ret >>= 4;
240 ret &= 0xfff;
71f95118 241 }
b8948d2a
SB
242 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
243 mydata->fatsize, ret, entry, offset);
71f95118
WD
244
245 return ret;
246}
247
71f95118
WD
248/*
249 * Read at most 'size' bytes from the specified cluster into 'buffer'.
250 * Return 0 on success, -1 otherwise.
251 */
252static int
9795e07b 253get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
71f95118 254{
3f270f42 255 __u32 idx = 0;
71f95118 256 __u32 startsect;
46236b14 257 int ret;
71f95118
WD
258
259 if (clustnum > 0) {
265edc03 260 startsect = clust_to_sect(mydata, clustnum);
71f95118
WD
261 } else {
262 startsect = mydata->rootdir_sect;
263 }
264
7385c28e
WD
265 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
266
cc63b25e 267 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
9a800ac7 268 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
7385c28e 269
cc63b25e
BT
270 printf("FAT: Misaligned buffer address (%p)\n", buffer);
271
272 while (size >= mydata->sect_size) {
273 ret = disk_read(startsect++, 1, tmpbuf);
274 if (ret != 1) {
275 debug("Error reading data (got %d)\n", ret);
276 return -1;
277 }
278
279 memcpy(buffer, tmpbuf, mydata->sect_size);
280 buffer += mydata->sect_size;
281 size -= mydata->sect_size;
282 }
283 } else {
ac497771 284 idx = size / mydata->sect_size;
cc63b25e
BT
285 ret = disk_read(startsect, idx, buffer);
286 if (ret != idx) {
287 debug("Error reading data (got %d)\n", ret);
288 return -1;
289 }
290 startsect += idx;
291 idx *= mydata->sect_size;
292 buffer += idx;
293 size -= idx;
294 }
295 if (size) {
296 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
297
298 ret = disk_read(startsect, 1, tmpbuf);
46236b14
KM
299 if (ret != 1) {
300 debug("Error reading data (got %d)\n", ret);
7205e407 301 return -1;
71f95118 302 }
7205e407 303
cc63b25e 304 memcpy(buffer, tmpbuf, size);
71f95118
WD
305 }
306
307 return 0;
308}
309
71f95118 310/*
1170e634 311 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
71f95118 312 * into 'buffer'.
1ad0b98a 313 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
71f95118 314 */
1170e634
BT
315__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
316 __aligned(ARCH_DMA_MINALIGN);
317
1ad0b98a
SR
318static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
319 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
71f95118 320{
1ad0b98a 321 loff_t filesize = FAT2CPU32(dentptr->size);
ac497771 322 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
71f95118 323 __u32 curclust = START(dentptr);
7205e407 324 __u32 endclust, newclust;
1ad0b98a 325 loff_t actsize;
71f95118 326
1ad0b98a
SR
327 *gotsize = 0;
328 debug("Filesize: %llu bytes\n", filesize);
71f95118 329
1170e634 330 if (pos >= filesize) {
1ad0b98a
SR
331 debug("Read position past EOF: %llu\n", pos);
332 return 0;
1170e634
BT
333 }
334
335 if (maxsize > 0 && filesize > pos + maxsize)
336 filesize = pos + maxsize;
71f95118 337
1ad0b98a 338 debug("%llu bytes\n", filesize);
7385c28e 339
1170e634
BT
340 actsize = bytesperclust;
341
342 /* go to cluster at pos */
343 while (actsize <= pos) {
344 curclust = get_fatent(mydata, curclust);
345 if (CHECK_CLUST(curclust, mydata->fatsize)) {
346 debug("curclust: 0x%x\n", curclust);
347 debug("Invalid FAT entry\n");
1ad0b98a 348 return 0;
1170e634
BT
349 }
350 actsize += bytesperclust;
351 }
352
353 /* actsize > pos */
354 actsize -= bytesperclust;
355 filesize -= actsize;
356 pos -= actsize;
357
358 /* align to beginning of next cluster if any */
359 if (pos) {
1ad0b98a 360 actsize = min(filesize, (loff_t)bytesperclust);
1170e634
BT
361 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
362 (int)actsize) != 0) {
363 printf("Error reading cluster\n");
364 return -1;
365 }
366 filesize -= actsize;
367 actsize -= pos;
368 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
1ad0b98a 369 *gotsize += actsize;
1170e634 370 if (!filesize)
1ad0b98a 371 return 0;
1170e634
BT
372 buffer += actsize;
373
374 curclust = get_fatent(mydata, curclust);
375 if (CHECK_CLUST(curclust, mydata->fatsize)) {
376 debug("curclust: 0x%x\n", curclust);
377 debug("Invalid FAT entry\n");
1ad0b98a 378 return 0;
1170e634
BT
379 }
380 }
381
7385c28e
WD
382 actsize = bytesperclust;
383 endclust = curclust;
71f95118
WD
384
385 do {
7205e407 386 /* search for consecutive clusters */
7385c28e 387 while (actsize < filesize) {
7205e407 388 newclust = get_fatent(mydata, endclust);
7385c28e 389 if ((newclust - 1) != endclust)
7205e407 390 goto getit;
8ce4e5c2 391 if (CHECK_CLUST(newclust, mydata->fatsize)) {
7385c28e
WD
392 debug("curclust: 0x%x\n", newclust);
393 debug("Invalid FAT entry\n");
1ad0b98a 394 return 0;
7205e407 395 }
7385c28e
WD
396 endclust = newclust;
397 actsize += bytesperclust;
7205e407 398 }
7385c28e 399
7205e407 400 /* get remaining bytes */
7385c28e 401 actsize = filesize;
0880e5bb 402 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
7385c28e 403 printf("Error reading cluster\n");
7205e407
WD
404 return -1;
405 }
1ad0b98a
SR
406 *gotsize += actsize;
407 return 0;
7205e407
WD
408getit:
409 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
7385c28e 410 printf("Error reading cluster\n");
7205e407
WD
411 return -1;
412 }
1ad0b98a 413 *gotsize += (int)actsize;
7205e407
WD
414 filesize -= actsize;
415 buffer += actsize;
7385c28e 416
7205e407 417 curclust = get_fatent(mydata, endclust);
8ce4e5c2 418 if (CHECK_CLUST(curclust, mydata->fatsize)) {
7385c28e
WD
419 debug("curclust: 0x%x\n", curclust);
420 printf("Invalid FAT entry\n");
1ad0b98a 421 return 0;
71f95118 422 }
7385c28e
WD
423 actsize = bytesperclust;
424 endclust = curclust;
71f95118
WD
425 } while (1);
426}
427
71f95118
WD
428/*
429 * Extract the file name information from 'slotptr' into 'l_name',
430 * starting at l_name[*idx].
431 * Return 1 if terminator (zero byte) is found, 0 otherwise.
432 */
9795e07b 433static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
71f95118
WD
434{
435 int j;
436
437 for (j = 0; j <= 8; j += 2) {
438 l_name[*idx] = slotptr->name0_4[j];
7385c28e
WD
439 if (l_name[*idx] == 0x00)
440 return 1;
71f95118
WD
441 (*idx)++;
442 }
443 for (j = 0; j <= 10; j += 2) {
444 l_name[*idx] = slotptr->name5_10[j];
7385c28e
WD
445 if (l_name[*idx] == 0x00)
446 return 1;
71f95118
WD
447 (*idx)++;
448 }
449 for (j = 0; j <= 2; j += 2) {
450 l_name[*idx] = slotptr->name11_12[j];
7385c28e
WD
451 if (l_name[*idx] == 0x00)
452 return 1;
71f95118
WD
453 (*idx)++;
454 }
455
456 return 0;
457}
458
71f95118 459/* Calculate short name checksum */
ff04f6d1 460static __u8 mkcksum(const char name[8], const char ext[3])
71f95118
WD
461{
462 int i;
7385c28e 463
71f95118
WD
464 __u8 ret = 0;
465
6ad77d88 466 for (i = 0; i < 8; i++)
ff04f6d1 467 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
6ad77d88 468 for (i = 0; i < 3; i++)
ff04f6d1 469 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
71f95118
WD
470
471 return ret;
472}
71f95118
WD
473
474/*
8eafae20
RC
475 * TODO these should go away once fat_write is reworked to use the
476 * directory iterator
71f95118 477 */
9a800ac7
EN
478__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
479 __aligned(ARCH_DMA_MINALIGN);
8eafae20
RC
480__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
481 __aligned(ARCH_DMA_MINALIGN);
71f95118 482
71f95118
WD
483/*
484 * Read boot sector and volume info from a FAT filesystem
485 */
486static int
9795e07b 487read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
71f95118 488{
ac497771 489 __u8 *block;
71f95118 490 volume_info *vistart;
ac497771
SS
491 int ret = 0;
492
493 if (cur_dev == NULL) {
494 debug("Error: no device selected\n");
495 return -1;
496 }
497
9a800ac7 498 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
ac497771
SS
499 if (block == NULL) {
500 debug("Error: allocating block\n");
501 return -1;
502 }
71f95118 503
9795e07b 504 if (disk_read(0, 1, block) < 0) {
7385c28e 505 debug("Error: reading block\n");
ac497771 506 goto fail;
71f95118
WD
507 }
508
509 memcpy(bs, block, sizeof(boot_sector));
7385c28e
WD
510 bs->reserved = FAT2CPU16(bs->reserved);
511 bs->fat_length = FAT2CPU16(bs->fat_length);
512 bs->secs_track = FAT2CPU16(bs->secs_track);
513 bs->heads = FAT2CPU16(bs->heads);
514 bs->total_sect = FAT2CPU32(bs->total_sect);
71f95118
WD
515
516 /* FAT32 entries */
517 if (bs->fat_length == 0) {
518 /* Assume FAT32 */
519 bs->fat32_length = FAT2CPU32(bs->fat32_length);
7385c28e 520 bs->flags = FAT2CPU16(bs->flags);
71f95118 521 bs->root_cluster = FAT2CPU32(bs->root_cluster);
7385c28e
WD
522 bs->info_sector = FAT2CPU16(bs->info_sector);
523 bs->backup_boot = FAT2CPU16(bs->backup_boot);
524 vistart = (volume_info *)(block + sizeof(boot_sector));
71f95118
WD
525 *fatsize = 32;
526 } else {
7385c28e 527 vistart = (volume_info *)&(bs->fat32_length);
71f95118
WD
528 *fatsize = 0;
529 }
530 memcpy(volinfo, vistart, sizeof(volume_info));
531
71f95118 532 if (*fatsize == 32) {
7385c28e 533 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
ac497771 534 goto exit;
71f95118 535 } else {
651351fe 536 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
71f95118 537 *fatsize = 12;
ac497771 538 goto exit;
71f95118 539 }
651351fe 540 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
71f95118 541 *fatsize = 16;
ac497771 542 goto exit;
71f95118
WD
543 }
544 }
545
7385c28e 546 debug("Error: broken fs_type sign\n");
ac497771
SS
547fail:
548 ret = -1;
549exit:
550 free(block);
551 return ret;
71f95118
WD
552}
553
45449980 554static int get_fs_info(fsdata *mydata)
71f95118 555{
7385c28e
WD
556 boot_sector bs;
557 volume_info volinfo;
45449980 558 int ret;
7385c28e 559
45449980
RC
560 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
561 if (ret) {
7385c28e 562 debug("Error: reading boot sector\n");
45449980 563 return ret;
7385c28e
WD
564 }
565
40e21916 566 if (mydata->fatsize == 32) {
7385c28e 567 mydata->fatlength = bs.fat32_length;
40e21916 568 } else {
7385c28e 569 mydata->fatlength = bs.fat_length;
40e21916 570 }
7385c28e
WD
571
572 mydata->fat_sect = bs.reserved;
573
45449980 574 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
7385c28e 575
ac497771 576 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
7385c28e 577 mydata->clust_size = bs.cluster_size;
46236b14
KM
578 if (mydata->sect_size != cur_part_info.blksz) {
579 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
580 mydata->sect_size, cur_part_info.blksz);
581 return -1;
582 }
7385c28e
WD
583
584 if (mydata->fatsize == 32) {
585 mydata->data_begin = mydata->rootdir_sect -
586 (mydata->clust_size * 2);
c6e3baa5 587 mydata->root_cluster = bs.root_cluster;
7385c28e 588 } else {
45449980
RC
589 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
590 bs.dir_entries[0]) *
591 sizeof(dir_entry)) /
592 mydata->sect_size;
7385c28e 593 mydata->data_begin = mydata->rootdir_sect +
45449980 594 mydata->rootdir_size -
7385c28e 595 (mydata->clust_size * 2);
265edc03
RC
596 mydata->root_cluster =
597 sect_to_clust(mydata, mydata->rootdir_sect);
7385c28e
WD
598 }
599
600 mydata->fatbufnum = -1;
3c0ed9c3 601 mydata->fat_dirty = 0;
9a800ac7 602 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
ac497771
SS
603 if (mydata->fatbuf == NULL) {
604 debug("Error: allocating memory\n");
605 return -1;
606 }
71f95118 607
cb940c7e
RG
608 if (vfat_enabled)
609 debug("VFAT Support enabled\n");
610
7385c28e
WD
611 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
612 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
613 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
614 "Data begins at: %d\n",
c6e3baa5 615 mydata->root_cluster,
7385c28e 616 mydata->rootdir_sect,
ac497771
SS
617 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
618 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
619 mydata->clust_size);
7385c28e 620
45449980
RC
621 return 0;
622}
623
c6e3baa5
RC
624
625/*
626 * Directory iterator, to simplify filesystem traversal
627 *
628 * Implements an iterator pattern to traverse directory tables,
629 * transparently handling directory tables split across multiple
630 * clusters, and the difference between FAT12/FAT16 root directory
631 * (contiguous) and subdirectories + FAT32 root (chained).
632 *
633 * Rough usage:
634 *
635 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
636 * // to traverse down to a subdirectory pointed to by
637 * // current iterator position:
638 * fat_itr_child(&itr, &itr);
639 * }
640 *
641 * For more complete example, see fat_itr_resolve()
642 */
643
644typedef struct {
645 fsdata *fsdata; /* filesystem parameters */
646 unsigned clust; /* current cluster */
647 int last_cluster; /* set once we've read last cluster */
648 int is_root; /* is iterator at root directory */
649 int remaining; /* remaining dent's in current cluster */
650
651 /* current iterator position values: */
652 dir_entry *dent; /* current directory entry */
653 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
654 char s_name[14]; /* short 8.3 name */
655 char *name; /* l_name if there is one, else s_name */
656
657 /* storage for current cluster in memory: */
658 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
659} fat_itr;
660
661static int fat_itr_isdir(fat_itr *itr);
662
663/**
664 * fat_itr_root() - initialize an iterator to start at the root
665 * directory
666 *
667 * @itr: iterator to initialize
668 * @fsdata: filesystem data for the partition
669 * @return 0 on success, else -errno
670 */
671static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
672{
673 if (get_fs_info(fsdata))
674 return -ENXIO;
675
676 itr->fsdata = fsdata;
677 itr->clust = fsdata->root_cluster;
678 itr->dent = NULL;
679 itr->remaining = 0;
680 itr->last_cluster = 0;
681 itr->is_root = 1;
682
683 return 0;
684}
685
686/**
687 * fat_itr_child() - initialize an iterator to descend into a sub-
688 * directory
689 *
690 * Initializes 'itr' to iterate the contents of the directory at
691 * the current cursor position of 'parent'. It is an error to
692 * call this if the current cursor of 'parent' is pointing at a
693 * regular file.
694 *
695 * Note that 'itr' and 'parent' can be the same pointer if you do
696 * not need to preserve 'parent' after this call, which is useful
697 * for traversing directory structure to resolve a file/directory.
698 *
699 * @itr: iterator to initialize
700 * @parent: the iterator pointing at a directory entry in the
701 * parent directory of the directory to iterate
702 */
703static void fat_itr_child(fat_itr *itr, fat_itr *parent)
704{
705 fsdata *mydata = parent->fsdata; /* for silly macros */
706 unsigned clustnum = START(parent->dent);
707
708 assert(fat_itr_isdir(parent));
709
710 itr->fsdata = parent->fsdata;
711 if (clustnum > 0) {
712 itr->clust = clustnum;
713 } else {
714 itr->clust = parent->fsdata->root_cluster;
715 }
716 itr->dent = NULL;
717 itr->remaining = 0;
718 itr->last_cluster = 0;
719 itr->is_root = 0;
720}
721
722static void *next_cluster(fat_itr *itr)
723{
724 fsdata *mydata = itr->fsdata; /* for silly macros */
725 int ret;
726 u32 sect;
727
728 /* have we reached the end? */
729 if (itr->last_cluster)
730 return NULL;
731
732 sect = clust_to_sect(itr->fsdata, itr->clust);
733
734 debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
735 sect, itr->fsdata->clust_size, DIRENTSPERBLOCK);
736
737 /*
738 * NOTE: do_fat_read_at() had complicated logic to deal w/
739 * vfat names that span multiple clusters in the fat16 case,
740 * which get_dentfromdir() probably also needed (and was
741 * missing). And not entirely sure what fat32 didn't have
742 * the same issue.. We solve that by only caring about one
743 * dent at a time and iteratively constructing the vfat long
744 * name.
745 */
746 ret = disk_read(sect, itr->fsdata->clust_size,
747 itr->block);
748 if (ret < 0) {
749 debug("Error: reading block\n");
750 return NULL;
751 }
752
753 if (itr->is_root && itr->fsdata->fatsize != 32) {
754 itr->clust++;
755 sect = clust_to_sect(itr->fsdata, itr->clust);
756 if (sect - itr->fsdata->rootdir_sect >=
757 itr->fsdata->rootdir_size) {
758 debug("cursect: 0x%x\n", itr->clust);
759 itr->last_cluster = 1;
760 }
761 } else {
762 itr->clust = get_fatent(itr->fsdata, itr->clust);
763 if (CHECK_CLUST(itr->clust, itr->fsdata->fatsize)) {
764 debug("cursect: 0x%x\n", itr->clust);
765 itr->last_cluster = 1;
766 }
767 }
768
769 return itr->block;
770}
771
772static dir_entry *next_dent(fat_itr *itr)
773{
774 if (itr->remaining == 0) {
775 struct dir_entry *dent = next_cluster(itr);
776 unsigned nbytes = itr->fsdata->sect_size *
777 itr->fsdata->clust_size;
778
779 /* have we reached the last cluster? */
780 if (!dent)
781 return NULL;
782
783 itr->remaining = nbytes / sizeof(dir_entry) - 1;
784 itr->dent = dent;
785 } else {
786 itr->remaining--;
787 itr->dent++;
788 }
789
790 /* have we reached the last valid entry? */
791 if (itr->dent->name[0] == 0)
792 return NULL;
793
794 return itr->dent;
795}
796
797static dir_entry *extract_vfat_name(fat_itr *itr)
798{
799 struct dir_entry *dent = itr->dent;
800 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
801 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
802 int n = 0;
803
804 while (seqn--) {
805 char buf[13];
806 int idx = 0;
807
808 slot2str((dir_slot *)dent, buf, &idx);
809
810 /* shift accumulated long-name up and copy new part in: */
811 memmove(itr->l_name + idx, itr->l_name, n);
812 memcpy(itr->l_name, buf, idx);
813 n += idx;
814
815 dent = next_dent(itr);
816 if (!dent)
817 return NULL;
818 }
819
820 itr->l_name[n] = '\0';
821
822 chksum = mkcksum(dent->name, dent->ext);
823
824 /* checksum mismatch could mean deleted file, etc.. skip it: */
825 if (chksum != alias_checksum) {
826 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
827 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
828 return NULL;
829 }
830
831 return dent;
832}
833
834/**
835 * fat_itr_next() - step to the next entry in a directory
836 *
837 * Must be called once on a new iterator before the cursor is valid.
838 *
839 * @itr: the iterator to iterate
840 * @return boolean, 1 if success or 0 if no more entries in the
841 * current directory
842 */
843static int fat_itr_next(fat_itr *itr)
844{
845 dir_entry *dent;
846
847 itr->name = NULL;
848
849 while (1) {
850 dent = next_dent(itr);
851 if (!dent)
852 return 0;
853
854 if (dent->name[0] == DELETED_FLAG ||
855 dent->name[0] == aRING)
856 continue;
857
858 if (dent->attr & ATTR_VOLUME) {
859 if (vfat_enabled &&
860 (dent->attr & ATTR_VFAT) == ATTR_VFAT &&
861 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
862 dent = extract_vfat_name(itr);
863 if (!dent)
864 continue;
865 itr->name = itr->l_name;
866 break;
867 } else {
868 /* Volume label or VFAT entry, skip */
869 continue;
870 }
871 }
872
873 break;
874 }
875
876 get_name(dent, itr->s_name);
877 if (!itr->name)
878 itr->name = itr->s_name;
879
880 return 1;
881}
882
883/**
884 * fat_itr_isdir() - is current cursor position pointing to a directory
885 *
886 * @itr: the iterator
887 * @return true if cursor is at a directory
888 */
889static int fat_itr_isdir(fat_itr *itr)
890{
891 return !!(itr->dent->attr & ATTR_DIR);
892}
893
894/*
895 * Helpers:
896 */
897
898#define TYPE_FILE 0x1
899#define TYPE_DIR 0x2
900#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
901
902/**
903 * fat_itr_resolve() - traverse directory structure to resolve the
904 * requested path.
905 *
906 * Traverse directory structure to the requested path. If the specified
907 * path is to a directory, this will descend into the directory and
908 * leave it iterator at the start of the directory. If the path is to a
909 * file, it will leave the iterator in the parent directory with current
910 * cursor at file's entry in the directory.
911 *
912 * @itr: iterator initialized to root
913 * @path: the requested path
914 * @type: bitmask of allowable file types
915 * @return 0 on success or -errno
916 */
917static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
918{
919 const char *next;
920
921 /* chomp any extra leading slashes: */
922 while (path[0] && ISDIRDELIM(path[0]))
923 path++;
924
925 /* are we at the end? */
926 if (strlen(path) == 0) {
927 if (!(type & TYPE_DIR))
928 return -ENOENT;
929 return 0;
930 }
931
932 /* find length of next path entry: */
933 next = path;
934 while (next[0] && !ISDIRDELIM(next[0]))
935 next++;
936
937 while (fat_itr_next(itr)) {
938 int match = 0;
939 unsigned n = max(strlen(itr->name), (size_t)(next - path));
940
941 /* check both long and short name: */
942 if (!strncasecmp(path, itr->name, n))
943 match = 1;
944 else if (itr->name != itr->s_name &&
945 !strncasecmp(path, itr->s_name, n))
946 match = 1;
947
948 if (!match)
949 continue;
950
951 if (fat_itr_isdir(itr)) {
952 /* recurse into directory: */
953 fat_itr_child(itr, itr);
954 return fat_itr_resolve(itr, next, type);
955 } else if (next[0]) {
956 /*
957 * If next is not empty then we have a case
958 * like: /path/to/realfile/nonsense
959 */
960 debug("bad trailing path: %s\n", next);
961 return -ENOENT;
962 } else if (!(type & TYPE_FILE)) {
963 return -ENOTDIR;
964 } else {
965 return 0;
966 }
967 }
968
969 return -ENOENT;
970}
971
9795e07b 972int file_fat_detectfs(void)
71f95118 973{
7385c28e
WD
974 boot_sector bs;
975 volume_info volinfo;
976 int fatsize;
977 char vol_label[12];
71f95118 978
7385c28e 979 if (cur_dev == NULL) {
7205e407
WD
980 printf("No current device\n");
981 return 1;
982 }
7385c28e 983
fc843a02 984#if defined(CONFIG_IDE) || \
10e40d54 985 defined(CONFIG_SATA) || \
c649e3c9 986 defined(CONFIG_SCSI) || \
dd60d122 987 defined(CONFIG_CMD_USB) || \
21f6f963 988 defined(CONFIG_MMC)
7205e407 989 printf("Interface: ");
7385c28e
WD
990 switch (cur_dev->if_type) {
991 case IF_TYPE_IDE:
992 printf("IDE");
993 break;
994 case IF_TYPE_SATA:
995 printf("SATA");
996 break;
997 case IF_TYPE_SCSI:
998 printf("SCSI");
999 break;
1000 case IF_TYPE_ATAPI:
1001 printf("ATAPI");
1002 break;
1003 case IF_TYPE_USB:
1004 printf("USB");
1005 break;
1006 case IF_TYPE_DOC:
1007 printf("DOC");
1008 break;
1009 case IF_TYPE_MMC:
1010 printf("MMC");
1011 break;
1012 default:
1013 printf("Unknown");
7205e407 1014 }
7385c28e 1015
bcce53d0 1016 printf("\n Device %d: ", cur_dev->devnum);
7205e407
WD
1017 dev_print(cur_dev);
1018#endif
7385c28e
WD
1019
1020 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
7205e407
WD
1021 printf("\nNo valid FAT fs found\n");
1022 return 1;
1023 }
7385c28e
WD
1024
1025 memcpy(vol_label, volinfo.volume_label, 11);
7205e407 1026 vol_label[11] = '\0';
7385c28e
WD
1027 volinfo.fs_type[5] = '\0';
1028
461f86e6 1029 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
7385c28e 1030
7205e407 1031 return 0;
71f95118
WD
1032}
1033
b7b5f319
SW
1034int fat_exists(const char *filename)
1035{
8eafae20 1036 fsdata fsdata;
2460098c 1037 fat_itr *itr;
1ad0b98a 1038 int ret;
1ad0b98a 1039
2460098c 1040 itr = malloc(sizeof(fat_itr));
8eafae20
RC
1041 ret = fat_itr_root(itr, &fsdata);
1042 if (ret)
1043 return 0;
1044
1045 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
725ffdb5 1046 free(fsdata.fatbuf);
2460098c 1047 free(itr);
1ad0b98a 1048 return ret == 0;
b7b5f319
SW
1049}
1050
d455d878 1051int fat_size(const char *filename, loff_t *size)
cf659819 1052{
8eafae20 1053 fsdata fsdata;
2460098c 1054 fat_itr *itr;
8eafae20
RC
1055 int ret;
1056
2460098c 1057 itr = malloc(sizeof(fat_itr));
8eafae20
RC
1058 ret = fat_itr_root(itr, &fsdata);
1059 if (ret)
1060 return ret;
1061
1062 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1063 if (ret) {
1064 /*
1065 * Directories don't have size, but fs_size() is not
1066 * expected to fail if passed a directory path:
1067 */
725ffdb5 1068 free(fsdata.fatbuf);
8eafae20
RC
1069 fat_itr_root(itr, &fsdata);
1070 if (!fat_itr_resolve(itr, filename, TYPE_DIR)) {
1071 *size = 0;
725ffdb5 1072 ret = 0;
8eafae20 1073 }
725ffdb5 1074 goto out;
8eafae20
RC
1075 }
1076
1077 *size = FAT2CPU32(itr->dent->size);
725ffdb5 1078 free(fsdata.fatbuf);
2460098c
TR
1079out:
1080 free(itr);
725ffdb5 1081 return ret;
cf659819
SW
1082}
1083
1ad0b98a
SR
1084int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1085 loff_t maxsize, loff_t *actread)
71f95118 1086{
8eafae20 1087 fsdata fsdata;
2460098c 1088 fat_itr *itr;
8eafae20
RC
1089 int ret;
1090
2460098c 1091 itr = malloc(sizeof(fat_itr));
8eafae20
RC
1092 ret = fat_itr_root(itr, &fsdata);
1093 if (ret)
1094 return ret;
1095
1096 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1097 if (ret)
725ffdb5 1098 goto out;
8eafae20 1099
7385c28e 1100 printf("reading %s\n", filename);
725ffdb5
RC
1101 ret = get_contents(&fsdata, itr->dent, pos, buffer, maxsize, actread);
1102
1103out:
1104 free(fsdata.fatbuf);
2460098c 1105 free(itr);
725ffdb5 1106 return ret;
1170e634
BT
1107}
1108
1ad0b98a 1109int file_fat_read(const char *filename, void *buffer, int maxsize)
1170e634 1110{
1ad0b98a
SR
1111 loff_t actread;
1112 int ret;
1113
1114 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1115 if (ret)
1116 return ret;
1117 else
1118 return actread;
71f95118 1119}
e6d52415 1120
d455d878
SR
1121int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1122 loff_t *actread)
e6d52415 1123{
1ad0b98a 1124 int ret;
e6d52415 1125
d455d878
SR
1126 ret = file_fat_read_at(filename, offset, buf, len, actread);
1127 if (ret)
e6d52415 1128 printf("** Unable to read file %s **\n", filename);
e6d52415 1129
d455d878 1130 return ret;
e6d52415
SG
1131}
1132
1f40366b
RC
1133typedef struct {
1134 struct fs_dir_stream parent;
1135 struct fs_dirent dirent;
1136 fsdata fsdata;
1137 fat_itr itr;
1138} fat_dir;
1139
1140int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1141{
725ffdb5 1142 fat_dir *dir = calloc(1, sizeof(*dir));
1f40366b
RC
1143 int ret;
1144
1145 if (!dir)
1146 return -ENOMEM;
1147
1148 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1149 if (ret)
1150 goto fail;
1151
1152 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1153 if (ret)
1154 goto fail;
1155
1156 *dirsp = (struct fs_dir_stream *)dir;
1157 return 0;
1158
1159fail:
725ffdb5 1160 free(dir->fsdata.fatbuf);
1f40366b
RC
1161 free(dir);
1162 return ret;
1163}
1164
1165int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1166{
1167 fat_dir *dir = (fat_dir *)dirs;
1168 struct fs_dirent *dent = &dir->dirent;
1169
1170 if (!fat_itr_next(&dir->itr))
1171 return -ENOENT;
1172
1173 memset(dent, 0, sizeof(*dent));
1174 strcpy(dent->name, dir->itr.name);
1175
1176 if (fat_itr_isdir(&dir->itr)) {
1177 dent->type = FS_DT_DIR;
1178 } else {
1179 dent->type = FS_DT_REG;
1180 dent->size = FAT2CPU32(dir->itr.dent->size);
1181 }
1182
1183 *dentp = dent;
1184
1185 return 0;
1186}
1187
1188void fat_closedir(struct fs_dir_stream *dirs)
1189{
1190 fat_dir *dir = (fat_dir *)dirs;
725ffdb5 1191 free(dir->fsdata.fatbuf);
1f40366b
RC
1192 free(dir);
1193}
1194
e6d52415
SG
1195void fat_close(void)
1196{
1197}