blob: f68c8f73c48828bb298c5821e56ed2c0b92679e3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +00002/*
3 * fat.c
4 *
5 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
6 *
7 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
8 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
wdenk7a428cc2003-06-15 22:40:42 +00009 */
10
11#include <common.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -070012#include <blk.h>
wdenk7a428cc2003-06-15 22:40:42 +000013#include <config.h>
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +000014#include <exports.h>
wdenk7a428cc2003-06-15 22:40:42 +000015#include <fat.h>
Rob Clark4174d7f2017-09-09 13:15:56 -040016#include <fs.h>
wdenk7a428cc2003-06-15 22:40:42 +000017#include <asm/byteorder.h>
wdenk2c9b05d2003-09-10 22:30:53 +000018#include <part.h>
Eric Nelson014705b2012-04-11 04:08:53 +000019#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060020#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060021#include <asm/cache.h>
Eric Nelson014705b2012-04-11 04:08:53 +000022#include <linux/compiler.h>
Richard Genoud2e372022012-12-13 00:47:36 +000023#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000024
wdenk7a428cc2003-06-15 22:40:42 +000025/*
Rob Clarkfd6c6b42017-09-09 13:15:59 -040026 * Convert a string to lowercase. Converts at most 'len' characters,
27 * 'len' may be larger than the length of 'str' if 'str' is NULL
28 * terminated.
wdenk7a428cc2003-06-15 22:40:42 +000029 */
Rob Clarkfd6c6b42017-09-09 13:15:59 -040030static void downcase(char *str, size_t len)
wdenk7a428cc2003-06-15 22:40:42 +000031{
Rob Clarkfd6c6b42017-09-09 13:15:59 -040032 while (*str != '\0' && len--) {
Richard Genoud2e372022012-12-13 00:47:36 +000033 *str = tolower(*str);
wdenk7a428cc2003-06-15 22:40:42 +000034 str++;
35 }
36}
37
Simon Glasse3394752016-02-29 15:25:34 -070038static struct blk_desc *cur_dev;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000039static disk_partition_t cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020040
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000041#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000042#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020043#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000044
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000045static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000046{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020047 ulong ret;
48
Simon Glass2ee8ada2016-02-29 15:25:52 -070049 if (!cur_dev)
wdenk2c9b05d2003-09-10 22:30:53 +000050 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020051
Simon Glass2ee8ada2016-02-29 15:25:52 -070052 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020053
Tom Rini240124c2017-08-14 21:02:08 -040054 if (ret != nr_blocks)
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020055 return -1;
56
57 return ret;
wdenk7a428cc2003-06-15 22:40:42 +000058}
59
Simon Glasse3394752016-02-29 15:25:34 -070060int fat_set_blk_dev(struct blk_desc *dev_desc, disk_partition_t *info)
wdenk7a428cc2003-06-15 22:40:42 +000061{
Eric Nelson014705b2012-04-11 04:08:53 +000062 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000063
Stephen Warren288904d2012-10-17 06:44:59 +000064 cur_dev = dev_desc;
65 cur_part_info = *info;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000066
67 /* Make sure it has a valid FAT header */
68 if (disk_read(0, 1, buffer) != 1) {
69 cur_dev = NULL;
70 return -1;
Wolfgang Denke78f2582007-08-07 16:02:13 +020071 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000072
73 /* Check if it's actually a DOS volume */
74 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
75 cur_dev = NULL;
76 return -1;
77 }
78
79 /* Check for FAT12/FAT16/FAT32 filesystem */
80 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
81 return 0;
82 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
83 return 0;
84
85 cur_dev = NULL;
86 return -1;
wdenk7a428cc2003-06-15 22:40:42 +000087}
88
Simon Glasse3394752016-02-29 15:25:34 -070089int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren288904d2012-10-17 06:44:59 +000090{
91 disk_partition_t info;
92
93 /* First close any currently found FAT filesystem */
94 cur_dev = NULL;
95
96 /* Read the partition table, if present */
Simon Glassb89a8442016-02-29 15:25:48 -070097 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren288904d2012-10-17 06:44:59 +000098 if (part_no != 0) {
99 printf("** Partition %d not valid on device %d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700100 part_no, dev_desc->devnum);
Stephen Warren288904d2012-10-17 06:44:59 +0000101 return -1;
102 }
103
104 info.start = 0;
105 info.size = dev_desc->lba;
106 info.blksz = dev_desc->blksz;
107 info.name[0] = 0;
108 info.type[0] = 0;
109 info.bootable = 0;
Patrick Delaunay73287092017-01-27 11:00:42 +0100110#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren288904d2012-10-17 06:44:59 +0000111 info.uuid[0] = 0;
112#endif
113 }
114
115 return fat_set_blk_dev(dev_desc, &info);
116}
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000117
wdenk7a428cc2003-06-15 22:40:42 +0000118/*
wdenk7a428cc2003-06-15 22:40:42 +0000119 * Extract zero terminated short name from a directory entry.
120 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200121static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000122{
123 char *ptr;
124
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200125 memcpy(s_name, dirent->name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000126 s_name[8] = '\0';
127 ptr = s_name;
128 while (*ptr && *ptr != ' ')
129 ptr++;
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400130 if (dirent->lcase & CASE_LOWER_BASE)
131 downcase(s_name, (unsigned)(ptr - s_name));
wdenk7a428cc2003-06-15 22:40:42 +0000132 if (dirent->ext[0] && dirent->ext[0] != ' ') {
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400133 *ptr++ = '.';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200134 memcpy(ptr, dirent->ext, 3);
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400135 if (dirent->lcase & CASE_LOWER_EXT)
136 downcase(ptr, 3);
wdenk7a428cc2003-06-15 22:40:42 +0000137 ptr[3] = '\0';
138 while (*ptr && *ptr != ' ')
139 ptr++;
140 }
141 *ptr = '\0';
142 if (*s_name == DELETED_FLAG)
143 *s_name = '\0';
144 else if (*s_name == aRING)
Remy Bohmer7c8f2ce2008-11-27 22:30:27 +0100145 *s_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000146}
147
Stefan Brüns9f95d812016-12-17 00:27:51 +0100148static int flush_dirty_fat_buffer(fsdata *mydata);
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800149
150#if !CONFIG_IS_ENABLED(FAT_WRITE)
Stefan Brüns9f95d812016-12-17 00:27:51 +0100151/* Stub for read only operation */
152int flush_dirty_fat_buffer(fsdata *mydata)
153{
154 (void)(mydata);
155 return 0;
156}
157#endif
158
wdenk7a428cc2003-06-15 22:40:42 +0000159/*
160 * Get the entry at index 'entry' in a FAT (12/16/32) table.
161 * On failure 0x00 is returned.
162 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200163static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000164{
165 __u32 bufnum;
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000166 __u32 offset, off8;
wdenk7a428cc2003-06-15 22:40:42 +0000167 __u32 ret = 0x00;
168
Stefan Brüns9f95d812016-12-17 00:27:51 +0100169 if (CHECK_CLUST(entry, mydata->fatsize)) {
170 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
171 return ret;
172 }
173
wdenk7a428cc2003-06-15 22:40:42 +0000174 switch (mydata->fatsize) {
175 case 32:
176 bufnum = entry / FAT32BUFSIZE;
177 offset = entry - bufnum * FAT32BUFSIZE;
178 break;
179 case 16:
180 bufnum = entry / FAT16BUFSIZE;
181 offset = entry - bufnum * FAT16BUFSIZE;
182 break;
183 case 12:
184 bufnum = entry / FAT12BUFSIZE;
185 offset = entry - bufnum * FAT12BUFSIZE;
186 break;
187
188 default:
189 /* Unsupported FAT size */
190 return ret;
191 }
192
Stefan Brüns9f95d812016-12-17 00:27:51 +0100193 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200194 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200195
wdenk7a428cc2003-06-15 22:40:42 +0000196 /* Read a new block of FAT entries into the cache. */
197 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000198 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000199 __u8 *bufptr = mydata->fatbuf;
200 __u32 fatlength = mydata->fatlength;
201 __u32 startblock = bufnum * FATBUFBLOCKS;
202
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100203 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200204 if (startblock + getsize > fatlength)
205 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000206
wdenk7a428cc2003-06-15 22:40:42 +0000207 startblock += mydata->fat_sect; /* Offset from start of disk */
208
Stefan Brüns9f95d812016-12-17 00:27:51 +0100209 /* Write back the fatbuf to the disk */
210 if (flush_dirty_fat_buffer(mydata) < 0)
211 return -1;
212
wdenk7a428cc2003-06-15 22:40:42 +0000213 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200214 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000215 return ret;
216 }
217 mydata->fatbufnum = bufnum;
218 }
219
220 /* Get the actual entry from the table */
221 switch (mydata->fatsize) {
222 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200223 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000224 break;
225 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200226 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000227 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200228 case 12:
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000229 off8 = (offset * 3) / 2;
230 /* fatbut + off8 may be unaligned, read in byte granularity */
231 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk7a428cc2003-06-15 22:40:42 +0000232
Stefan Brüns15f40632016-12-17 03:55:10 +0100233 if (offset & 0x1)
234 ret >>= 4;
235 ret &= 0xfff;
wdenk7a428cc2003-06-15 22:40:42 +0000236 }
Stefan Brüns9f95d812016-12-17 00:27:51 +0100237 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
238 mydata->fatsize, ret, entry, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000239
240 return ret;
241}
242
wdenk7a428cc2003-06-15 22:40:42 +0000243/*
244 * Read at most 'size' bytes from the specified cluster into 'buffer'.
245 * Return 0 on success, -1 otherwise.
246 */
247static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200248get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000249{
Erik Hansen4ea89662011-03-24 10:15:37 +0100250 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000251 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000252 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000253
254 if (clustnum > 0) {
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400255 startsect = clust_to_sect(mydata, clustnum);
wdenk7a428cc2003-06-15 22:40:42 +0000256 } else {
257 startsect = mydata->rootdir_sect;
258 }
259
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200260 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
261
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200262 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000263 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200264
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200265 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200266
267 while (size >= mydata->sect_size) {
268 ret = disk_read(startsect++, 1, tmpbuf);
269 if (ret != 1) {
270 debug("Error reading data (got %d)\n", ret);
271 return -1;
272 }
273
274 memcpy(buffer, tmpbuf, mydata->sect_size);
275 buffer += mydata->sect_size;
276 size -= mydata->sect_size;
277 }
278 } else {
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000279 idx = size / mydata->sect_size;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200280 ret = disk_read(startsect, idx, buffer);
281 if (ret != idx) {
282 debug("Error reading data (got %d)\n", ret);
283 return -1;
284 }
285 startsect += idx;
286 idx *= mydata->sect_size;
287 buffer += idx;
288 size -= idx;
289 }
290 if (size) {
291 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
292
293 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000294 if (ret != 1) {
295 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000296 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000297 }
wdenk2c9b05d2003-09-10 22:30:53 +0000298
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200299 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000300 }
301
302 return 0;
303}
304
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200305/**
306 * get_contents() - read from file
307 *
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000308 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200309 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
310 * fatal errors.
311 *
312 * @mydata: file system description
313 * @dentprt: directory entry pointer
314 * @pos: position from where to read
315 * @buffer: buffer into which to read
316 * @maxsize: maximum number of bytes to read
317 * @gotsize: number of bytes actually read
318 * Return: -1 on error, otherwise 0
wdenk7a428cc2003-06-15 22:40:42 +0000319 */
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800320static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
321 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk7a428cc2003-06-15 22:40:42 +0000322{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800323 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000324 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000325 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000326 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800327 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000328
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800329 *gotsize = 0;
330 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000331
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000332 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800333 debug("Read position past EOF: %llu\n", pos);
334 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000335 }
336
337 if (maxsize > 0 && filesize > pos + maxsize)
338 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000339
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800340 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000341
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200342 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000343
344 /* go to cluster at pos */
345 while (actsize <= pos) {
346 curclust = get_fatent(mydata, curclust);
347 if (CHECK_CLUST(curclust, mydata->fatsize)) {
348 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200349 printf("Invalid FAT entry\n");
350 return -1;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000351 }
352 actsize += bytesperclust;
353 }
354
355 /* actsize > pos */
356 actsize -= bytesperclust;
357 filesize -= actsize;
358 pos -= actsize;
359
360 /* align to beginning of next cluster if any */
361 if (pos) {
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800362 __u8 *tmp_buffer;
363
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800364 actsize = min(filesize, (loff_t)bytesperclust);
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800365 tmp_buffer = malloc_cache_aligned(actsize);
366 if (!tmp_buffer) {
367 debug("Error: allocating buffer\n");
Heinrich Schuchardtd37d47c2019-09-12 19:19:30 +0200368 return -1;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800369 }
370
371 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000372 printf("Error reading cluster\n");
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800373 free(tmp_buffer);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000374 return -1;
375 }
376 filesize -= actsize;
377 actsize -= pos;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800378 memcpy(buffer, tmp_buffer + pos, actsize);
379 free(tmp_buffer);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800380 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000381 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800382 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000383 buffer += actsize;
384
385 curclust = get_fatent(mydata, curclust);
386 if (CHECK_CLUST(curclust, mydata->fatsize)) {
387 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200388 printf("Invalid FAT entry\n");
389 return -1;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000390 }
391 }
392
393 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200394 endclust = curclust;
395
wdenk7a428cc2003-06-15 22:40:42 +0000396 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000397 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200398 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000399 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200400 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000401 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100402 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200403 debug("curclust: 0x%x\n", newclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200404 printf("Invalid FAT entry\n");
405 return -1;
wdenk2c9b05d2003-09-10 22:30:53 +0000406 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200407 endclust = newclust;
408 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000409 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200410
wdenk2c9b05d2003-09-10 22:30:53 +0000411 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200412 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200413 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200414 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000415 return -1;
416 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800417 *gotsize += actsize;
418 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000419getit:
420 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200421 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000422 return -1;
423 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800424 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000425 filesize -= actsize;
426 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200427
wdenk2c9b05d2003-09-10 22:30:53 +0000428 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100429 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200430 debug("curclust: 0x%x\n", curclust);
431 printf("Invalid FAT entry\n");
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200432 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000433 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200434 actsize = bytesperclust;
435 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000436 } while (1);
437}
438
wdenk7a428cc2003-06-15 22:40:42 +0000439/*
440 * Extract the file name information from 'slotptr' into 'l_name',
441 * starting at l_name[*idx].
442 * Return 1 if terminator (zero byte) is found, 0 otherwise.
443 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200444static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000445{
446 int j;
447
448 for (j = 0; j <= 8; j += 2) {
449 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200450 if (l_name[*idx] == 0x00)
451 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000452 (*idx)++;
453 }
454 for (j = 0; j <= 10; j += 2) {
455 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200456 if (l_name[*idx] == 0x00)
457 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000458 (*idx)++;
459 }
460 for (j = 0; j <= 2; j += 2) {
461 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200462 if (l_name[*idx] == 0x00)
463 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000464 (*idx)++;
465 }
466
467 return 0;
468}
469
wdenk7a428cc2003-06-15 22:40:42 +0000470/* Calculate short name checksum */
Marek Vasut47a470f2012-10-09 07:20:22 +0000471static __u8 mkcksum(const char name[8], const char ext[3])
wdenk7a428cc2003-06-15 22:40:42 +0000472{
473 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200474
wdenk7a428cc2003-06-15 22:40:42 +0000475 __u8 ret = 0;
476
Marek Vasut4ab1f642013-01-11 03:35:48 +0000477 for (i = 0; i < 8; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000478 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut4ab1f642013-01-11 03:35:48 +0000479 for (i = 0; i < 3; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000480 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk7a428cc2003-06-15 22:40:42 +0000481
482 return ret;
483}
wdenk7a428cc2003-06-15 22:40:42 +0000484
485/*
wdenk7a428cc2003-06-15 22:40:42 +0000486 * Read boot sector and volume info from a FAT filesystem
487 */
488static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200489read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000490{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000491 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000492 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000493 int ret = 0;
494
495 if (cur_dev == NULL) {
496 debug("Error: no device selected\n");
497 return -1;
498 }
499
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300500 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000501 if (block == NULL) {
502 debug("Error: allocating block\n");
503 return -1;
504 }
wdenk7a428cc2003-06-15 22:40:42 +0000505
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200506 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200507 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000508 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000509 }
510
511 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200512 bs->reserved = FAT2CPU16(bs->reserved);
513 bs->fat_length = FAT2CPU16(bs->fat_length);
514 bs->secs_track = FAT2CPU16(bs->secs_track);
515 bs->heads = FAT2CPU16(bs->heads);
516 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000517
518 /* FAT32 entries */
519 if (bs->fat_length == 0) {
520 /* Assume FAT32 */
521 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200522 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000523 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200524 bs->info_sector = FAT2CPU16(bs->info_sector);
525 bs->backup_boot = FAT2CPU16(bs->backup_boot);
526 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000527 *fatsize = 32;
528 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200529 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000530 *fatsize = 0;
531 }
532 memcpy(volinfo, vistart, sizeof(volume_info));
533
wdenk7a428cc2003-06-15 22:40:42 +0000534 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200535 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000536 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000537 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500538 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000539 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000540 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000541 }
Tom Rix155abaa2009-05-20 07:55:41 -0500542 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000543 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000544 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000545 }
546 }
547
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200548 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000549fail:
550 ret = -1;
551exit:
552 free(block);
553 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000554}
555
Rob Clark4996a5f2017-09-09 13:15:52 -0400556static int get_fs_info(fsdata *mydata)
wdenk7a428cc2003-06-15 22:40:42 +0000557{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200558 boot_sector bs;
559 volume_info volinfo;
Rob Clark4996a5f2017-09-09 13:15:52 -0400560 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000561
Rob Clark4996a5f2017-09-09 13:15:52 -0400562 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
563 if (ret) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200564 debug("Error: reading boot sector\n");
Rob Clark4996a5f2017-09-09 13:15:52 -0400565 return ret;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200566 }
567
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000568 if (mydata->fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200569 mydata->fatlength = bs.fat32_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900570 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000571 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200572 mydata->fatlength = bs.fat_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900573 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
574 if (!mydata->total_sect)
575 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000576 }
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900577 if (!mydata->total_sect) /* unlikely */
578 mydata->total_sect = (u32)cur_part_info.size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200579
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900580 mydata->fats = bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200581 mydata->fat_sect = bs.reserved;
582
Rob Clark4996a5f2017-09-09 13:15:52 -0400583 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200584
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000585 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200586 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000587 if (mydata->sect_size != cur_part_info.blksz) {
588 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
589 mydata->sect_size, cur_part_info.blksz);
590 return -1;
591 }
Patrick Wildt1d614b92018-11-26 15:56:57 +0100592 if (mydata->clust_size == 0) {
593 printf("Error: FAT cluster size not set\n");
594 return -1;
595 }
596 if ((unsigned int)mydata->clust_size * mydata->sect_size >
597 MAX_CLUSTSIZE) {
598 printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
599 (unsigned int)mydata->clust_size * mydata->sect_size,
600 MAX_CLUSTSIZE);
601 return -1;
602 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200603
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200604 if (mydata->fatsize == 32) {
605 mydata->data_begin = mydata->rootdir_sect -
606 (mydata->clust_size * 2);
Rob Clark0c33fce2017-09-09 13:15:53 -0400607 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200608 } else {
Rob Clark4996a5f2017-09-09 13:15:52 -0400609 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
610 bs.dir_entries[0]) *
611 sizeof(dir_entry)) /
612 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200613 mydata->data_begin = mydata->rootdir_sect +
Rob Clark4996a5f2017-09-09 13:15:52 -0400614 mydata->rootdir_size -
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200615 (mydata->clust_size * 2);
Anssi Hannulaccea7332019-02-27 12:55:57 +0200616
617 /*
618 * The root directory is not cluster-aligned and may be on a
619 * "negative" cluster, this will be handled specially in
620 * next_cluster().
621 */
622 mydata->root_cluster = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200623 }
624
625 mydata->fatbufnum = -1;
Stefan Brüns751b31d2016-09-11 22:51:40 +0200626 mydata->fat_dirty = 0;
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300627 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000628 if (mydata->fatbuf == NULL) {
629 debug("Error: allocating memory\n");
630 return -1;
631 }
wdenk7a428cc2003-06-15 22:40:42 +0000632
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200633 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
634 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
635 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
636 "Data begins at: %d\n",
Rob Clark0c33fce2017-09-09 13:15:53 -0400637 mydata->root_cluster,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200638 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000639 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
640 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
641 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000642
Rob Clark4996a5f2017-09-09 13:15:52 -0400643 return 0;
644}
645
Rob Clark0c33fce2017-09-09 13:15:53 -0400646
647/*
648 * Directory iterator, to simplify filesystem traversal
649 *
650 * Implements an iterator pattern to traverse directory tables,
651 * transparently handling directory tables split across multiple
652 * clusters, and the difference between FAT12/FAT16 root directory
653 * (contiguous) and subdirectories + FAT32 root (chained).
654 *
655 * Rough usage:
656 *
657 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
658 * // to traverse down to a subdirectory pointed to by
659 * // current iterator position:
660 * fat_itr_child(&itr, &itr);
661 * }
662 *
663 * For more complete example, see fat_itr_resolve()
664 */
665
666typedef struct {
667 fsdata *fsdata; /* filesystem parameters */
AKASHI Takahirof0926ae2018-09-11 15:59:09 +0900668 unsigned start_clust; /* first cluster */
Rob Clark0c33fce2017-09-09 13:15:53 -0400669 unsigned clust; /* current cluster */
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900670 unsigned next_clust; /* next cluster if remaining == 0 */
Rob Clark0c33fce2017-09-09 13:15:53 -0400671 int last_cluster; /* set once we've read last cluster */
672 int is_root; /* is iterator at root directory */
673 int remaining; /* remaining dent's in current cluster */
674
675 /* current iterator position values: */
676 dir_entry *dent; /* current directory entry */
677 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
678 char s_name[14]; /* short 8.3 name */
679 char *name; /* l_name if there is one, else s_name */
680
681 /* storage for current cluster in memory: */
682 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
683} fat_itr;
684
685static int fat_itr_isdir(fat_itr *itr);
686
687/**
688 * fat_itr_root() - initialize an iterator to start at the root
689 * directory
690 *
691 * @itr: iterator to initialize
692 * @fsdata: filesystem data for the partition
693 * @return 0 on success, else -errno
694 */
695static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
696{
697 if (get_fs_info(fsdata))
698 return -ENXIO;
699
700 itr->fsdata = fsdata;
AKASHI Takahirof0926ae2018-09-11 15:59:09 +0900701 itr->start_clust = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400702 itr->clust = fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900703 itr->next_clust = fsdata->root_cluster;
Rob Clark0c33fce2017-09-09 13:15:53 -0400704 itr->dent = NULL;
705 itr->remaining = 0;
706 itr->last_cluster = 0;
707 itr->is_root = 1;
708
709 return 0;
710}
711
712/**
713 * fat_itr_child() - initialize an iterator to descend into a sub-
714 * directory
715 *
716 * Initializes 'itr' to iterate the contents of the directory at
717 * the current cursor position of 'parent'. It is an error to
718 * call this if the current cursor of 'parent' is pointing at a
719 * regular file.
720 *
721 * Note that 'itr' and 'parent' can be the same pointer if you do
722 * not need to preserve 'parent' after this call, which is useful
723 * for traversing directory structure to resolve a file/directory.
724 *
725 * @itr: iterator to initialize
726 * @parent: the iterator pointing at a directory entry in the
727 * parent directory of the directory to iterate
728 */
729static void fat_itr_child(fat_itr *itr, fat_itr *parent)
730{
731 fsdata *mydata = parent->fsdata; /* for silly macros */
732 unsigned clustnum = START(parent->dent);
733
734 assert(fat_itr_isdir(parent));
735
736 itr->fsdata = parent->fsdata;
AKASHI Takahirof0926ae2018-09-11 15:59:09 +0900737 itr->start_clust = clustnum;
Rob Clark0c33fce2017-09-09 13:15:53 -0400738 if (clustnum > 0) {
739 itr->clust = clustnum;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900740 itr->next_clust = clustnum;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300741 itr->is_root = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400742 } else {
743 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900744 itr->next_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300745 itr->is_root = 1;
Rob Clark0c33fce2017-09-09 13:15:53 -0400746 }
747 itr->dent = NULL;
748 itr->remaining = 0;
749 itr->last_cluster = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400750}
751
Anssi Hannulaccea7332019-02-27 12:55:57 +0200752static void *next_cluster(fat_itr *itr, unsigned *nbytes)
Rob Clark0c33fce2017-09-09 13:15:53 -0400753{
754 fsdata *mydata = itr->fsdata; /* for silly macros */
755 int ret;
756 u32 sect;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200757 u32 read_size;
Rob Clark0c33fce2017-09-09 13:15:53 -0400758
759 /* have we reached the end? */
760 if (itr->last_cluster)
761 return NULL;
762
Anssi Hannulaccea7332019-02-27 12:55:57 +0200763 if (itr->is_root && itr->fsdata->fatsize != 32) {
764 /*
765 * The root directory is located before the data area and
766 * cannot be indexed using the regular unsigned cluster
767 * numbers (it may start at a "negative" cluster or not at a
768 * cluster boundary at all), so consider itr->next_clust to be
769 * a offset in cluster-sized units from the start of rootdir.
770 */
771 unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
772 unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
773 sect = itr->fsdata->rootdir_sect + sect_offset;
774 /* do not read past the end of rootdir */
775 read_size = min_t(u32, itr->fsdata->clust_size,
776 remaining_sects);
777 } else {
778 sect = clust_to_sect(itr->fsdata, itr->next_clust);
779 read_size = itr->fsdata->clust_size;
780 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400781
Anssi Hannulaccea7332019-02-27 12:55:57 +0200782 debug("FAT read(sect=%d), clust_size=%d, read_size=%u, DIRENTSPERBLOCK=%zd\n",
783 sect, itr->fsdata->clust_size, read_size, DIRENTSPERBLOCK);
Rob Clark0c33fce2017-09-09 13:15:53 -0400784
785 /*
786 * NOTE: do_fat_read_at() had complicated logic to deal w/
787 * vfat names that span multiple clusters in the fat16 case,
788 * which get_dentfromdir() probably also needed (and was
789 * missing). And not entirely sure what fat32 didn't have
790 * the same issue.. We solve that by only caring about one
791 * dent at a time and iteratively constructing the vfat long
792 * name.
793 */
Anssi Hannulaccea7332019-02-27 12:55:57 +0200794 ret = disk_read(sect, read_size, itr->block);
Rob Clark0c33fce2017-09-09 13:15:53 -0400795 if (ret < 0) {
796 debug("Error: reading block\n");
797 return NULL;
798 }
799
Anssi Hannulaccea7332019-02-27 12:55:57 +0200800 *nbytes = read_size * itr->fsdata->sect_size;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900801 itr->clust = itr->next_clust;
Rob Clark0c33fce2017-09-09 13:15:53 -0400802 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900803 itr->next_clust++;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200804 if (itr->next_clust * itr->fsdata->clust_size >=
Rob Clark0c33fce2017-09-09 13:15:53 -0400805 itr->fsdata->rootdir_size) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900806 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400807 itr->last_cluster = 1;
808 }
809 } else {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900810 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
811 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
812 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400813 itr->last_cluster = 1;
814 }
815 }
816
817 return itr->block;
818}
819
820static dir_entry *next_dent(fat_itr *itr)
821{
822 if (itr->remaining == 0) {
Anssi Hannulaccea7332019-02-27 12:55:57 +0200823 unsigned nbytes;
824 struct dir_entry *dent = next_cluster(itr, &nbytes);
Rob Clark0c33fce2017-09-09 13:15:53 -0400825
826 /* have we reached the last cluster? */
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900827 if (!dent) {
828 /* a sign for no more entries left */
829 itr->dent = NULL;
Rob Clark0c33fce2017-09-09 13:15:53 -0400830 return NULL;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900831 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400832
833 itr->remaining = nbytes / sizeof(dir_entry) - 1;
834 itr->dent = dent;
835 } else {
836 itr->remaining--;
837 itr->dent++;
838 }
839
840 /* have we reached the last valid entry? */
841 if (itr->dent->name[0] == 0)
842 return NULL;
843
844 return itr->dent;
845}
846
847static dir_entry *extract_vfat_name(fat_itr *itr)
848{
849 struct dir_entry *dent = itr->dent;
850 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
851 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
852 int n = 0;
853
854 while (seqn--) {
855 char buf[13];
856 int idx = 0;
857
858 slot2str((dir_slot *)dent, buf, &idx);
859
Patrick Wildtd6153f12018-11-26 15:58:13 +0100860 if (n + idx >= sizeof(itr->l_name))
861 return NULL;
862
Rob Clark0c33fce2017-09-09 13:15:53 -0400863 /* shift accumulated long-name up and copy new part in: */
864 memmove(itr->l_name + idx, itr->l_name, n);
865 memcpy(itr->l_name, buf, idx);
866 n += idx;
867
868 dent = next_dent(itr);
869 if (!dent)
870 return NULL;
871 }
872
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900873 /*
874 * We are now at the short file name entry.
875 * If it is marked as deleted, just skip it.
876 */
877 if (dent->name[0] == DELETED_FLAG ||
878 dent->name[0] == aRING)
879 return NULL;
880
Rob Clark0c33fce2017-09-09 13:15:53 -0400881 itr->l_name[n] = '\0';
882
883 chksum = mkcksum(dent->name, dent->ext);
884
885 /* checksum mismatch could mean deleted file, etc.. skip it: */
886 if (chksum != alias_checksum) {
887 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
888 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
889 return NULL;
890 }
891
892 return dent;
893}
894
895/**
896 * fat_itr_next() - step to the next entry in a directory
897 *
898 * Must be called once on a new iterator before the cursor is valid.
899 *
900 * @itr: the iterator to iterate
901 * @return boolean, 1 if success or 0 if no more entries in the
902 * current directory
903 */
904static int fat_itr_next(fat_itr *itr)
905{
906 dir_entry *dent;
907
908 itr->name = NULL;
909
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900910 /*
911 * One logical directory entry consist of following slots:
912 * name[0] Attributes
913 * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT
914 * ...
915 * dent[N - 2]: LFN[1] 2 ATTR_VFAT
916 * dent[N - 1]: LFN[0] 1 ATTR_VFAT
917 * dent[N]: SFN ATTR_ARCH
918 */
919
Rob Clark0c33fce2017-09-09 13:15:53 -0400920 while (1) {
921 dent = next_dent(itr);
922 if (!dent)
923 return 0;
924
925 if (dent->name[0] == DELETED_FLAG ||
926 dent->name[0] == aRING)
927 continue;
928
929 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynena799dcd2018-01-05 02:45:21 +0200930 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Rob Clark0c33fce2017-09-09 13:15:53 -0400931 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900932 /* long file name */
Rob Clark0c33fce2017-09-09 13:15:53 -0400933 dent = extract_vfat_name(itr);
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900934 /*
935 * If succeeded, dent has a valid short file
936 * name entry for the current entry.
937 * If failed, itr points to a current bogus
938 * entry. So after fetching a next one,
939 * it may have a short file name entry
940 * for this bogus entry so that we can still
941 * check for a short name.
942 */
Rob Clark0c33fce2017-09-09 13:15:53 -0400943 if (!dent)
944 continue;
945 itr->name = itr->l_name;
946 break;
947 } else {
948 /* Volume label or VFAT entry, skip */
949 continue;
950 }
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900951 } else if (!(dent->attr & ATTR_ARCH) &&
952 !(dent->attr & ATTR_DIR))
953 continue;
Rob Clark0c33fce2017-09-09 13:15:53 -0400954
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900955 /* short file name */
Rob Clark0c33fce2017-09-09 13:15:53 -0400956 break;
957 }
958
959 get_name(dent, itr->s_name);
960 if (!itr->name)
961 itr->name = itr->s_name;
962
963 return 1;
964}
965
966/**
967 * fat_itr_isdir() - is current cursor position pointing to a directory
968 *
969 * @itr: the iterator
970 * @return true if cursor is at a directory
971 */
972static int fat_itr_isdir(fat_itr *itr)
973{
974 return !!(itr->dent->attr & ATTR_DIR);
975}
976
977/*
978 * Helpers:
979 */
980
981#define TYPE_FILE 0x1
982#define TYPE_DIR 0x2
983#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
984
985/**
986 * fat_itr_resolve() - traverse directory structure to resolve the
987 * requested path.
988 *
989 * Traverse directory structure to the requested path. If the specified
990 * path is to a directory, this will descend into the directory and
991 * leave it iterator at the start of the directory. If the path is to a
992 * file, it will leave the iterator in the parent directory with current
993 * cursor at file's entry in the directory.
994 *
995 * @itr: iterator initialized to root
996 * @path: the requested path
997 * @type: bitmask of allowable file types
998 * @return 0 on success or -errno
999 */
1000static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
1001{
1002 const char *next;
1003
1004 /* chomp any extra leading slashes: */
1005 while (path[0] && ISDIRDELIM(path[0]))
1006 path++;
1007
1008 /* are we at the end? */
1009 if (strlen(path) == 0) {
1010 if (!(type & TYPE_DIR))
1011 return -ENOENT;
1012 return 0;
1013 }
1014
1015 /* find length of next path entry: */
1016 next = path;
1017 while (next[0] && !ISDIRDELIM(next[0]))
1018 next++;
1019
AKASHI Takahirof0a47812018-09-11 15:58:59 +09001020 if (itr->is_root) {
1021 /* root dir doesn't have "." nor ".." */
1022 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
1023 (((next - path) == 2) && !strncmp(path, "..", 2))) {
1024 /* point back to itself */
1025 itr->clust = itr->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +09001026 itr->next_clust = itr->fsdata->root_cluster;
AKASHI Takahirof0a47812018-09-11 15:58:59 +09001027 itr->dent = NULL;
1028 itr->remaining = 0;
1029 itr->last_cluster = 0;
1030
1031 if (next[0] == 0) {
1032 if (type & TYPE_DIR)
1033 return 0;
1034 else
1035 return -ENOENT;
1036 }
1037
1038 return fat_itr_resolve(itr, next, type);
1039 }
1040 }
1041
Rob Clark0c33fce2017-09-09 13:15:53 -04001042 while (fat_itr_next(itr)) {
1043 int match = 0;
1044 unsigned n = max(strlen(itr->name), (size_t)(next - path));
1045
1046 /* check both long and short name: */
1047 if (!strncasecmp(path, itr->name, n))
1048 match = 1;
1049 else if (itr->name != itr->s_name &&
1050 !strncasecmp(path, itr->s_name, n))
1051 match = 1;
1052
1053 if (!match)
1054 continue;
1055
1056 if (fat_itr_isdir(itr)) {
1057 /* recurse into directory: */
1058 fat_itr_child(itr, itr);
1059 return fat_itr_resolve(itr, next, type);
1060 } else if (next[0]) {
1061 /*
1062 * If next is not empty then we have a case
1063 * like: /path/to/realfile/nonsense
1064 */
1065 debug("bad trailing path: %s\n", next);
1066 return -ENOENT;
1067 } else if (!(type & TYPE_FILE)) {
1068 return -ENOTDIR;
1069 } else {
1070 return 0;
1071 }
1072 }
1073
1074 return -ENOENT;
1075}
1076
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001077int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001078{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001079 boot_sector bs;
1080 volume_info volinfo;
1081 int fatsize;
1082 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001083
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001084 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001085 printf("No current device\n");
1086 return 1;
1087 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001088
Simon Glassb569a012017-05-17 03:25:30 -06001089#if defined(CONFIG_IDE) || \
Simon Glassab3055a2017-06-14 21:28:25 -06001090 defined(CONFIG_SATA) || \
Simon Glass8706b812016-05-01 11:36:02 -06001091 defined(CONFIG_SCSI) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001092 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001093 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001094 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001095 switch (cur_dev->if_type) {
1096 case IF_TYPE_IDE:
1097 printf("IDE");
1098 break;
1099 case IF_TYPE_SATA:
1100 printf("SATA");
1101 break;
1102 case IF_TYPE_SCSI:
1103 printf("SCSI");
1104 break;
1105 case IF_TYPE_ATAPI:
1106 printf("ATAPI");
1107 break;
1108 case IF_TYPE_USB:
1109 printf("USB");
1110 break;
1111 case IF_TYPE_DOC:
1112 printf("DOC");
1113 break;
1114 case IF_TYPE_MMC:
1115 printf("MMC");
1116 break;
1117 default:
1118 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001119 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001120
Simon Glass2f26fff2016-02-29 15:25:51 -07001121 printf("\n Device %d: ", cur_dev->devnum);
wdenk2c9b05d2003-09-10 22:30:53 +00001122 dev_print(cur_dev);
1123#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001124
1125 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001126 printf("\nNo valid FAT fs found\n");
1127 return 1;
1128 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001129
1130 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001131 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001132 volinfo.fs_type[5] = '\0';
1133
Stephen Warrend398dbe2012-10-17 06:44:57 +00001134 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001135
wdenk2c9b05d2003-09-10 22:30:53 +00001136 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001137}
1138
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001139int fat_exists(const char *filename)
1140{
Rob Clark71193c92017-09-09 13:15:54 -04001141 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001142 fat_itr *itr;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001143 int ret;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001144
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001145 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001146 if (!itr)
1147 return 0;
Rob Clark71193c92017-09-09 13:15:54 -04001148 ret = fat_itr_root(itr, &fsdata);
1149 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001150 goto out;
Rob Clark71193c92017-09-09 13:15:54 -04001151
1152 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001153 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001154out:
Tom Rini5257a092017-09-22 07:37:43 -04001155 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001156 return ret == 0;
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001157}
1158
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001159int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001160{
Rob Clark71193c92017-09-09 13:15:54 -04001161 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001162 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001163 int ret;
1164
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001165 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001166 if (!itr)
1167 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001168 ret = fat_itr_root(itr, &fsdata);
1169 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001170 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001171
1172 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1173 if (ret) {
1174 /*
1175 * Directories don't have size, but fs_size() is not
1176 * expected to fail if passed a directory path:
1177 */
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001178 free(fsdata.fatbuf);
Andrew F. Davis8a6c6122019-05-16 09:34:31 -05001179 ret = fat_itr_root(itr, &fsdata);
1180 if (ret)
1181 goto out_free_itr;
1182 ret = fat_itr_resolve(itr, filename, TYPE_DIR);
1183 if (!ret)
Rob Clark71193c92017-09-09 13:15:54 -04001184 *size = 0;
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001185 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001186 }
1187
1188 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001189out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001190 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001191out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001192 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001193 return ret;
Stephen Warren3eb58f52014-06-11 12:47:26 -06001194}
1195
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001196int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1197 loff_t maxsize, loff_t *actread)
wdenk7a428cc2003-06-15 22:40:42 +00001198{
Rob Clark71193c92017-09-09 13:15:54 -04001199 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001200 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001201 int ret;
1202
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001203 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001204 if (!itr)
1205 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001206 ret = fat_itr_root(itr, &fsdata);
1207 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001208 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001209
1210 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1211 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001212 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001213
Andreas Dannenberg315c81d2018-08-13 21:35:15 -05001214 debug("reading %s at pos %llu\n", filename, pos);
Tien Fong Cheed16b79b2019-02-11 14:56:20 +08001215
1216 /* For saving default max clustersize memory allocated to malloc pool */
1217 dir_entry *dentptr = itr->dent;
1218
Tien Fong Cheed16b79b2019-02-11 14:56:20 +08001219 ret = get_contents(&fsdata, dentptr, pos, buffer, maxsize, actread);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001220
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001221out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001222 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001223out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001224 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001225 return ret;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001226}
1227
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001228int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001229{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001230 loff_t actread;
1231 int ret;
1232
1233 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1234 if (ret)
1235 return ret;
1236 else
1237 return actread;
wdenk7a428cc2003-06-15 22:40:42 +00001238}
Simon Glass19e38582012-12-26 09:53:33 +00001239
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001240int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1241 loff_t *actread)
Simon Glass19e38582012-12-26 09:53:33 +00001242{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001243 int ret;
Simon Glass19e38582012-12-26 09:53:33 +00001244
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001245 ret = file_fat_read_at(filename, offset, buf, len, actread);
1246 if (ret)
Simon Glass19e38582012-12-26 09:53:33 +00001247 printf("** Unable to read file %s **\n", filename);
Simon Glass19e38582012-12-26 09:53:33 +00001248
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001249 return ret;
Simon Glass19e38582012-12-26 09:53:33 +00001250}
1251
Rob Clark4174d7f2017-09-09 13:15:56 -04001252typedef struct {
1253 struct fs_dir_stream parent;
1254 struct fs_dirent dirent;
1255 fsdata fsdata;
1256 fat_itr itr;
1257} fat_dir;
1258
1259int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1260{
Neil Armstrong6f625f82017-11-24 09:54:41 +01001261 fat_dir *dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001262 int ret;
1263
Neil Armstrong6f625f82017-11-24 09:54:41 +01001264 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001265 if (!dir)
1266 return -ENOMEM;
Neil Armstrong6f625f82017-11-24 09:54:41 +01001267 memset(dir, 0, sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001268
1269 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1270 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001271 goto fail_free_dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001272
1273 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1274 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001275 goto fail_free_both;
Rob Clark4174d7f2017-09-09 13:15:56 -04001276
1277 *dirsp = (struct fs_dir_stream *)dir;
1278 return 0;
1279
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001280fail_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001281 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001282fail_free_dir:
Rob Clark4174d7f2017-09-09 13:15:56 -04001283 free(dir);
1284 return ret;
1285}
1286
1287int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1288{
1289 fat_dir *dir = (fat_dir *)dirs;
1290 struct fs_dirent *dent = &dir->dirent;
1291
1292 if (!fat_itr_next(&dir->itr))
1293 return -ENOENT;
1294
1295 memset(dent, 0, sizeof(*dent));
1296 strcpy(dent->name, dir->itr.name);
1297
1298 if (fat_itr_isdir(&dir->itr)) {
1299 dent->type = FS_DT_DIR;
1300 } else {
1301 dent->type = FS_DT_REG;
1302 dent->size = FAT2CPU32(dir->itr.dent->size);
1303 }
1304
1305 *dentp = dent;
1306
1307 return 0;
1308}
1309
1310void fat_closedir(struct fs_dir_stream *dirs)
1311{
1312 fat_dir *dir = (fat_dir *)dirs;
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001313 free(dir->fsdata.fatbuf);
Rob Clark4174d7f2017-09-09 13:15:56 -04001314 free(dir);
1315}
1316
Simon Glass19e38582012-12-26 09:53:33 +00001317void fat_close(void)
1318{
1319}