blob: c9742d534bbc4deb944b219040e6eab79e9cedaa [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk71f95112003-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
wdenk71f95112003-06-15 22:40:42 +00009 */
10
11#include <common.h>
Simon Glass2a981dc2016-02-29 15:25:52 -070012#include <blk.h>
wdenk71f95112003-06-15 22:40:42 +000013#include <config.h>
Sergei Shtylyovac497772011-08-08 09:38:33 +000014#include <exports.h>
wdenk71f95112003-06-15 22:40:42 +000015#include <fat.h>
Rob Clark1f403662017-09-09 13:15:56 -040016#include <fs.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
wdenk71f95112003-06-15 22:40:42 +000018#include <asm/byteorder.h>
wdenk7205e402003-09-10 22:30:53 +000019#include <part.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000020#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060021#include <memalign.h>
Simon Glass90526e92020-05-10 11:39:56 -060022#include <asm/cache.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000023#include <linux/compiler.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000024#include <linux/ctype.h>
wdenk71f95112003-06-15 22:40:42 +000025
wdenk71f95112003-06-15 22:40:42 +000026/*
Rob Clark21a24c32017-09-09 13:15:59 -040027 * Convert a string to lowercase. Converts at most 'len' characters,
28 * 'len' may be larger than the length of 'str' if 'str' is NULL
29 * terminated.
wdenk71f95112003-06-15 22:40:42 +000030 */
Rob Clark21a24c32017-09-09 13:15:59 -040031static void downcase(char *str, size_t len)
wdenk71f95112003-06-15 22:40:42 +000032{
Rob Clark21a24c32017-09-09 13:15:59 -040033 while (*str != '\0' && len--) {
Richard Genoudfb7e16c2012-12-13 00:47:36 +000034 *str = tolower(*str);
wdenk71f95112003-06-15 22:40:42 +000035 str++;
36 }
37}
38
Simon Glass4101f682016-02-29 15:25:34 -070039static struct blk_desc *cur_dev;
Simon Glass05289792020-05-10 11:39:57 -060040static struct disk_partition cur_part_info;
Wolfgang Denk7385c282010-07-19 11:37:00 +020041
Kyle Moffett9813b752011-12-21 07:08:10 +000042#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk7205e402003-09-10 22:30:53 +000043#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk66c2d732010-07-19 11:36:57 +020044#define DOS_FS32_TYPE_OFFSET 0x52
wdenk71f95112003-06-15 22:40:42 +000045
Kyle Moffett9813b752011-12-21 07:08:10 +000046static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk71f95112003-06-15 22:40:42 +000047{
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020048 ulong ret;
49
Simon Glass2a981dc2016-02-29 15:25:52 -070050 if (!cur_dev)
wdenk7205e402003-09-10 22:30:53 +000051 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020052
Simon Glass2a981dc2016-02-29 15:25:52 -070053 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020054
Tom Rini42a9f142017-08-14 21:02:08 -040055 if (ret != nr_blocks)
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020056 return -1;
57
58 return ret;
wdenk71f95112003-06-15 22:40:42 +000059}
60
Simon Glass05289792020-05-10 11:39:57 -060061int fat_set_blk_dev(struct blk_desc *dev_desc, struct disk_partition *info)
wdenk71f95112003-06-15 22:40:42 +000062{
Eric Nelson9a800ac2012-04-11 04:08:53 +000063 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk7205e402003-09-10 22:30:53 +000064
Stephen Warren5e8f9832012-10-17 06:44:59 +000065 cur_dev = dev_desc;
66 cur_part_info = *info;
Kyle Moffett9813b752011-12-21 07:08:10 +000067
68 /* Make sure it has a valid FAT header */
69 if (disk_read(0, 1, buffer) != 1) {
70 cur_dev = NULL;
71 return -1;
72 }
73
74 /* Check if it's actually a DOS volume */
75 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
76 cur_dev = NULL;
77 return -1;
78 }
79
80 /* Check for FAT12/FAT16/FAT32 filesystem */
81 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
82 return 0;
83 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
84 return 0;
85
86 cur_dev = NULL;
87 return -1;
wdenk71f95112003-06-15 22:40:42 +000088}
89
Simon Glass4101f682016-02-29 15:25:34 -070090int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren5e8f9832012-10-17 06:44:59 +000091{
Simon Glass05289792020-05-10 11:39:57 -060092 struct disk_partition info;
Stephen Warren5e8f9832012-10-17 06:44:59 +000093
94 /* First close any currently found FAT filesystem */
95 cur_dev = NULL;
96
97 /* Read the partition table, if present */
Simon Glass3e8bd462016-02-29 15:25:48 -070098 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren5e8f9832012-10-17 06:44:59 +000099 if (part_no != 0) {
100 printf("** Partition %d not valid on device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700101 part_no, dev_desc->devnum);
Stephen Warren5e8f9832012-10-17 06:44:59 +0000102 return -1;
103 }
104
105 info.start = 0;
106 info.size = dev_desc->lba;
107 info.blksz = dev_desc->blksz;
108 info.name[0] = 0;
109 info.type[0] = 0;
110 info.bootable = 0;
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100111#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren5e8f9832012-10-17 06:44:59 +0000112 info.uuid[0] = 0;
113#endif
114 }
115
116 return fat_set_blk_dev(dev_desc, &info);
117}
Kyle Moffett9813b752011-12-21 07:08:10 +0000118
wdenk71f95112003-06-15 22:40:42 +0000119/*
wdenk71f95112003-06-15 22:40:42 +0000120 * Extract zero terminated short name from a directory entry.
121 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200122static void get_name(dir_entry *dirent, char *s_name)
wdenk71f95112003-06-15 22:40:42 +0000123{
124 char *ptr;
125
Wolfgang Denk7385c282010-07-19 11:37:00 +0200126 memcpy(s_name, dirent->name, 8);
wdenk71f95112003-06-15 22:40:42 +0000127 s_name[8] = '\0';
128 ptr = s_name;
129 while (*ptr && *ptr != ' ')
130 ptr++;
Rob Clark21a24c32017-09-09 13:15:59 -0400131 if (dirent->lcase & CASE_LOWER_BASE)
132 downcase(s_name, (unsigned)(ptr - s_name));
wdenk71f95112003-06-15 22:40:42 +0000133 if (dirent->ext[0] && dirent->ext[0] != ' ') {
Rob Clark21a24c32017-09-09 13:15:59 -0400134 *ptr++ = '.';
Wolfgang Denk7385c282010-07-19 11:37:00 +0200135 memcpy(ptr, dirent->ext, 3);
Rob Clark21a24c32017-09-09 13:15:59 -0400136 if (dirent->lcase & CASE_LOWER_EXT)
137 downcase(ptr, 3);
wdenk71f95112003-06-15 22:40:42 +0000138 ptr[3] = '\0';
139 while (*ptr && *ptr != ' ')
140 ptr++;
141 }
142 *ptr = '\0';
143 if (*s_name == DELETED_FLAG)
144 *s_name = '\0';
145 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100146 *s_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000147}
148
Stefan Brünsb8948d22016-12-17 00:27:51 +0100149static int flush_dirty_fat_buffer(fsdata *mydata);
Tien Fong Cheed8c3ea92019-01-23 14:20:04 +0800150
151#if !CONFIG_IS_ENABLED(FAT_WRITE)
Stefan Brünsb8948d22016-12-17 00:27:51 +0100152/* Stub for read only operation */
153int flush_dirty_fat_buffer(fsdata *mydata)
154{
155 (void)(mydata);
156 return 0;
157}
158#endif
159
wdenk71f95112003-06-15 22:40:42 +0000160/*
161 * Get the entry at index 'entry' in a FAT (12/16/32) table.
162 * On failure 0x00 is returned.
163 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200164static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000165{
166 __u32 bufnum;
Stefan Brünsb352cae2017-01-26 20:22:36 +0000167 __u32 offset, off8;
wdenk71f95112003-06-15 22:40:42 +0000168 __u32 ret = 0x00;
169
Stefan Brünsb8948d22016-12-17 00:27:51 +0100170 if (CHECK_CLUST(entry, mydata->fatsize)) {
171 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
172 return ret;
173 }
174
wdenk71f95112003-06-15 22:40:42 +0000175 switch (mydata->fatsize) {
176 case 32:
177 bufnum = entry / FAT32BUFSIZE;
178 offset = entry - bufnum * FAT32BUFSIZE;
179 break;
180 case 16:
181 bufnum = entry / FAT16BUFSIZE;
182 offset = entry - bufnum * FAT16BUFSIZE;
183 break;
184 case 12:
185 bufnum = entry / FAT12BUFSIZE;
186 offset = entry - bufnum * FAT12BUFSIZE;
187 break;
188
189 default:
190 /* Unsupported FAT size */
191 return ret;
192 }
193
Stefan Brünsb8948d22016-12-17 00:27:51 +0100194 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk7385c282010-07-19 11:37:00 +0200195 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200196
wdenk71f95112003-06-15 22:40:42 +0000197 /* Read a new block of FAT entries into the cache. */
198 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000199 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000200 __u8 *bufptr = mydata->fatbuf;
201 __u32 fatlength = mydata->fatlength;
202 __u32 startblock = bufnum * FATBUFBLOCKS;
203
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100204 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau8006dd22012-07-20 15:19:29 +0200205 if (startblock + getsize > fatlength)
206 getsize = fatlength - startblock;
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000207
wdenk71f95112003-06-15 22:40:42 +0000208 startblock += mydata->fat_sect; /* Offset from start of disk */
209
Stefan Brünsb8948d22016-12-17 00:27:51 +0100210 /* Write back the fatbuf to the disk */
211 if (flush_dirty_fat_buffer(mydata) < 0)
212 return -1;
213
wdenk71f95112003-06-15 22:40:42 +0000214 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200215 debug("Error reading FAT blocks\n");
wdenk71f95112003-06-15 22:40:42 +0000216 return ret;
217 }
218 mydata->fatbufnum = bufnum;
219 }
220
221 /* Get the actual entry from the table */
222 switch (mydata->fatsize) {
223 case 32:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200224 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000225 break;
226 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200227 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000228 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200229 case 12:
Stefan Brünsb352cae2017-01-26 20:22:36 +0000230 off8 = (offset * 3) / 2;
231 /* fatbut + off8 may be unaligned, read in byte granularity */
232 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk71f95112003-06-15 22:40:42 +0000233
Stefan Brüns8d48c922016-12-17 03:55:10 +0100234 if (offset & 0x1)
235 ret >>= 4;
236 ret &= 0xfff;
wdenk71f95112003-06-15 22:40:42 +0000237 }
Stefan Brünsb8948d22016-12-17 00:27:51 +0100238 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
239 mydata->fatsize, ret, entry, offset);
wdenk71f95112003-06-15 22:40:42 +0000240
241 return ret;
242}
243
wdenk71f95112003-06-15 22:40:42 +0000244/*
245 * Read at most 'size' bytes from the specified cluster into 'buffer'.
246 * Return 0 on success, -1 otherwise.
247 */
248static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200249get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000250{
Erik Hansen3f270f42011-03-24 10:15:37 +0100251 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000252 __u32 startsect;
Kyle Moffett46236b12011-12-20 07:41:13 +0000253 int ret;
wdenk71f95112003-06-15 22:40:42 +0000254
255 if (clustnum > 0) {
Rob Clark265edc02017-09-09 13:16:00 -0400256 startsect = clust_to_sect(mydata, clustnum);
wdenk71f95112003-06-15 22:40:42 +0000257 } else {
258 startsect = mydata->rootdir_sect;
259 }
260
Wolfgang Denk7385c282010-07-19 11:37:00 +0200261 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
262
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200263 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson9a800ac2012-04-11 04:08:53 +0000264 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200265
Heinrich Schuchardt1c381ce2018-09-13 19:42:47 +0200266 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200267
268 while (size >= mydata->sect_size) {
269 ret = disk_read(startsect++, 1, tmpbuf);
270 if (ret != 1) {
271 debug("Error reading data (got %d)\n", ret);
272 return -1;
273 }
274
275 memcpy(buffer, tmpbuf, mydata->sect_size);
276 buffer += mydata->sect_size;
277 size -= mydata->sect_size;
278 }
279 } else {
Sergei Shtylyovac497772011-08-08 09:38:33 +0000280 idx = size / mydata->sect_size;
Jason Wessel5b3ddb12020-06-23 14:36:54 -0700281 if (idx == 0)
282 ret = 0;
283 else
284 ret = disk_read(startsect, idx, buffer);
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200285 if (ret != idx) {
286 debug("Error reading data (got %d)\n", ret);
287 return -1;
288 }
289 startsect += idx;
290 idx *= mydata->sect_size;
291 buffer += idx;
292 size -= idx;
293 }
294 if (size) {
295 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
296
297 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett46236b12011-12-20 07:41:13 +0000298 if (ret != 1) {
299 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000300 return -1;
wdenk71f95112003-06-15 22:40:42 +0000301 }
wdenk7205e402003-09-10 22:30:53 +0000302
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200303 memcpy(buffer, tmpbuf, size);
wdenk71f95112003-06-15 22:40:42 +0000304 }
305
306 return 0;
307}
308
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200309/**
310 * get_contents() - read from file
311 *
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000312 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200313 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
314 * fatal errors.
315 *
316 * @mydata: file system description
317 * @dentprt: directory entry pointer
318 * @pos: position from where to read
319 * @buffer: buffer into which to read
320 * @maxsize: maximum number of bytes to read
321 * @gotsize: number of bytes actually read
322 * Return: -1 on error, otherwise 0
wdenk71f95112003-06-15 22:40:42 +0000323 */
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800324static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
325 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk71f95112003-06-15 22:40:42 +0000326{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800327 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000328 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000329 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000330 __u32 endclust, newclust;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800331 loff_t actsize;
wdenk71f95112003-06-15 22:40:42 +0000332
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800333 *gotsize = 0;
334 debug("Filesize: %llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000335
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000336 if (pos >= filesize) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800337 debug("Read position past EOF: %llu\n", pos);
338 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000339 }
340
341 if (maxsize > 0 && filesize > pos + maxsize)
342 filesize = pos + maxsize;
wdenk71f95112003-06-15 22:40:42 +0000343
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800344 debug("%llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000345
Wolfgang Denk7385c282010-07-19 11:37:00 +0200346 actsize = bytesperclust;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000347
348 /* go to cluster at pos */
349 while (actsize <= pos) {
350 curclust = get_fatent(mydata, curclust);
351 if (CHECK_CLUST(curclust, mydata->fatsize)) {
352 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200353 printf("Invalid FAT entry\n");
354 return -1;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000355 }
356 actsize += bytesperclust;
357 }
358
359 /* actsize > pos */
360 actsize -= bytesperclust;
361 filesize -= actsize;
362 pos -= actsize;
363
364 /* align to beginning of next cluster if any */
365 if (pos) {
Tien Fong Chee85378742019-02-11 14:56:19 +0800366 __u8 *tmp_buffer;
367
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800368 actsize = min(filesize, (loff_t)bytesperclust);
Tien Fong Chee85378742019-02-11 14:56:19 +0800369 tmp_buffer = malloc_cache_aligned(actsize);
370 if (!tmp_buffer) {
371 debug("Error: allocating buffer\n");
Heinrich Schuchardtf1368382019-09-12 19:19:30 +0200372 return -1;
Tien Fong Chee85378742019-02-11 14:56:19 +0800373 }
374
375 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000376 printf("Error reading cluster\n");
Tien Fong Chee85378742019-02-11 14:56:19 +0800377 free(tmp_buffer);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000378 return -1;
379 }
380 filesize -= actsize;
381 actsize -= pos;
Tien Fong Chee85378742019-02-11 14:56:19 +0800382 memcpy(buffer, tmp_buffer + pos, actsize);
383 free(tmp_buffer);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800384 *gotsize += actsize;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000385 if (!filesize)
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800386 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000387 buffer += actsize;
388
389 curclust = get_fatent(mydata, curclust);
390 if (CHECK_CLUST(curclust, mydata->fatsize)) {
391 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200392 printf("Invalid FAT entry\n");
393 return -1;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000394 }
395 }
396
397 actsize = bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200398 endclust = curclust;
399
wdenk71f95112003-06-15 22:40:42 +0000400 do {
wdenk7205e402003-09-10 22:30:53 +0000401 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200402 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000403 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200404 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000405 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100406 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200407 debug("curclust: 0x%x\n", newclust);
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200408 printf("Invalid FAT entry\n");
409 return -1;
wdenk7205e402003-09-10 22:30:53 +0000410 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200411 endclust = newclust;
412 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000413 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200414
wdenk7205e402003-09-10 22:30:53 +0000415 /* get remaining bytes */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200416 actsize = filesize;
Benoît Thébaudeau0880e5b2012-07-20 15:21:37 +0200417 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200418 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000419 return -1;
420 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800421 *gotsize += actsize;
422 return 0;
wdenk7205e402003-09-10 22:30:53 +0000423getit:
424 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200425 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000426 return -1;
427 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800428 *gotsize += (int)actsize;
wdenk7205e402003-09-10 22:30:53 +0000429 filesize -= actsize;
430 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200431
wdenk7205e402003-09-10 22:30:53 +0000432 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100433 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200434 debug("curclust: 0x%x\n", curclust);
435 printf("Invalid FAT entry\n");
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200436 return -1;
wdenk71f95112003-06-15 22:40:42 +0000437 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200438 actsize = bytesperclust;
439 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000440 } while (1);
441}
442
wdenk71f95112003-06-15 22:40:42 +0000443/*
444 * Extract the file name information from 'slotptr' into 'l_name',
445 * starting at l_name[*idx].
446 * Return 1 if terminator (zero byte) is found, 0 otherwise.
447 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200448static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000449{
450 int j;
451
452 for (j = 0; j <= 8; j += 2) {
453 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200454 if (l_name[*idx] == 0x00)
455 return 1;
wdenk71f95112003-06-15 22:40:42 +0000456 (*idx)++;
457 }
458 for (j = 0; j <= 10; j += 2) {
459 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200460 if (l_name[*idx] == 0x00)
461 return 1;
wdenk71f95112003-06-15 22:40:42 +0000462 (*idx)++;
463 }
464 for (j = 0; j <= 2; j += 2) {
465 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200466 if (l_name[*idx] == 0x00)
467 return 1;
wdenk71f95112003-06-15 22:40:42 +0000468 (*idx)++;
469 }
470
471 return 0;
472}
473
wdenk71f95112003-06-15 22:40:42 +0000474/* Calculate short name checksum */
Marek Vasutff04f6d2012-10-09 07:20:22 +0000475static __u8 mkcksum(const char name[8], const char ext[3])
wdenk71f95112003-06-15 22:40:42 +0000476{
477 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200478
wdenk71f95112003-06-15 22:40:42 +0000479 __u8 ret = 0;
480
Marek Vasut6ad77d82013-01-11 03:35:48 +0000481 for (i = 0; i < 8; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000482 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut6ad77d82013-01-11 03:35:48 +0000483 for (i = 0; i < 3; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000484 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk71f95112003-06-15 22:40:42 +0000485
486 return ret;
487}
wdenk71f95112003-06-15 22:40:42 +0000488
489/*
wdenk71f95112003-06-15 22:40:42 +0000490 * Read boot sector and volume info from a FAT filesystem
491 */
492static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200493read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000494{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000495 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000496 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000497 int ret = 0;
498
499 if (cur_dev == NULL) {
500 debug("Error: no device selected\n");
501 return -1;
502 }
503
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +0300504 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000505 if (block == NULL) {
506 debug("Error: allocating block\n");
507 return -1;
508 }
wdenk71f95112003-06-15 22:40:42 +0000509
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200510 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200511 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000512 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000513 }
514
515 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200516 bs->reserved = FAT2CPU16(bs->reserved);
517 bs->fat_length = FAT2CPU16(bs->fat_length);
518 bs->secs_track = FAT2CPU16(bs->secs_track);
519 bs->heads = FAT2CPU16(bs->heads);
520 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000521
522 /* FAT32 entries */
523 if (bs->fat_length == 0) {
524 /* Assume FAT32 */
525 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200526 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000527 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200528 bs->info_sector = FAT2CPU16(bs->info_sector);
529 bs->backup_boot = FAT2CPU16(bs->backup_boot);
530 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000531 *fatsize = 32;
532 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200533 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000534 *fatsize = 0;
535 }
536 memcpy(volinfo, vistart, sizeof(volume_info));
537
wdenk71f95112003-06-15 22:40:42 +0000538 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200539 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000540 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000541 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500542 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000543 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000544 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000545 }
Tom Rix651351f2009-05-20 07:55:41 -0500546 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000547 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000548 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000549 }
550 }
551
Wolfgang Denk7385c282010-07-19 11:37:00 +0200552 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000553fail:
554 ret = -1;
555exit:
556 free(block);
557 return ret;
wdenk71f95112003-06-15 22:40:42 +0000558}
559
Rob Clark45449982017-09-09 13:15:52 -0400560static int get_fs_info(fsdata *mydata)
wdenk71f95112003-06-15 22:40:42 +0000561{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200562 boot_sector bs;
563 volume_info volinfo;
Rob Clark45449982017-09-09 13:15:52 -0400564 int ret;
wdenk71f95112003-06-15 22:40:42 +0000565
Rob Clark45449982017-09-09 13:15:52 -0400566 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
567 if (ret) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200568 debug("Error: reading boot sector\n");
Rob Clark45449982017-09-09 13:15:52 -0400569 return ret;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200570 }
571
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000572 if (mydata->fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200573 mydata->fatlength = bs.fat32_length;
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900574 mydata->total_sect = bs.total_sect;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000575 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200576 mydata->fatlength = bs.fat_length;
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900577 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
578 if (!mydata->total_sect)
579 mydata->total_sect = bs.total_sect;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000580 }
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900581 if (!mydata->total_sect) /* unlikely */
582 mydata->total_sect = (u32)cur_part_info.size;
wdenk71f95112003-06-15 22:40:42 +0000583
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900584 mydata->fats = bs.fats;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200585 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000586
Rob Clark45449982017-09-09 13:15:52 -0400587 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000588
Sergei Shtylyovac497772011-08-08 09:38:33 +0000589 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200590 mydata->clust_size = bs.cluster_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000591 if (mydata->sect_size != cur_part_info.blksz) {
592 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
593 mydata->sect_size, cur_part_info.blksz);
594 return -1;
595 }
Patrick Wildtcd80a4f2018-11-26 15:56:57 +0100596 if (mydata->clust_size == 0) {
597 printf("Error: FAT cluster size not set\n");
598 return -1;
599 }
600 if ((unsigned int)mydata->clust_size * mydata->sect_size >
601 MAX_CLUSTSIZE) {
602 printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
603 (unsigned int)mydata->clust_size * mydata->sect_size,
604 MAX_CLUSTSIZE);
605 return -1;
606 }
wdenk71f95112003-06-15 22:40:42 +0000607
Wolfgang Denk7385c282010-07-19 11:37:00 +0200608 if (mydata->fatsize == 32) {
609 mydata->data_begin = mydata->rootdir_sect -
610 (mydata->clust_size * 2);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400611 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200612 } else {
Rob Clark45449982017-09-09 13:15:52 -0400613 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
614 bs.dir_entries[0]) *
615 sizeof(dir_entry)) /
616 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200617 mydata->data_begin = mydata->rootdir_sect +
Rob Clark45449982017-09-09 13:15:52 -0400618 mydata->rootdir_size -
Wolfgang Denk7385c282010-07-19 11:37:00 +0200619 (mydata->clust_size * 2);
Anssi Hannula9b183582019-02-27 12:55:57 +0200620
621 /*
622 * The root directory is not cluster-aligned and may be on a
623 * "negative" cluster, this will be handled specially in
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100624 * fat_next_cluster().
Anssi Hannula9b183582019-02-27 12:55:57 +0200625 */
626 mydata->root_cluster = 0;
wdenk71f95112003-06-15 22:40:42 +0000627 }
wdenk71f95112003-06-15 22:40:42 +0000628
Wolfgang Denk7385c282010-07-19 11:37:00 +0200629 mydata->fatbufnum = -1;
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200630 mydata->fat_dirty = 0;
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +0300631 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000632 if (mydata->fatbuf == NULL) {
633 debug("Error: allocating memory\n");
634 return -1;
635 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200636
Wolfgang Denk7385c282010-07-19 11:37:00 +0200637 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
638 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
639 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
640 "Data begins at: %d\n",
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400641 mydata->root_cluster,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200642 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000643 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
644 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
645 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200646
Rob Clark45449982017-09-09 13:15:52 -0400647 return 0;
648}
649
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100650/**
651 * struct fat_itr - directory iterator, to simplify filesystem traversal
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400652 *
653 * Implements an iterator pattern to traverse directory tables,
654 * transparently handling directory tables split across multiple
655 * clusters, and the difference between FAT12/FAT16 root directory
656 * (contiguous) and subdirectories + FAT32 root (chained).
657 *
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100658 * Rough usage
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400659 *
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100660 * .. code-block:: c
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400661 *
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100662 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
663 * // to traverse down to a subdirectory pointed to by
664 * // current iterator position:
665 * fat_itr_child(&itr, &itr);
666 * }
667 *
668 * For a more complete example, see fat_itr_resolve().
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400669 */
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100670struct fat_itr {
671 /**
672 * @fsdata: filesystem parameters
673 */
674 fsdata *fsdata;
675 /**
676 * @start_clust: first cluster
677 */
678 unsigned int start_clust;
679 /**
680 * @clust: current cluster
681 */
682 unsigned int clust;
683 /**
684 * @next_clust: next cluster if remaining == 0
685 */
686 unsigned int next_clust;
687 /**
688 * @last_cluster: set if last cluster of directory reached
689 */
690 int last_cluster;
691 /**
692 * @is_root: is iterator at root directory
693 */
694 int is_root;
695 /**
696 * @remaining: remaining directory entries in current cluster
697 */
698 int remaining;
699 /**
700 * @dent: current directory entry
701 */
702 dir_entry *dent;
703 /**
Heinrich Schuchardt89735b42020-11-19 07:44:08 +0100704 * @dent_rem: remaining entries after long name start
705 */
706 int dent_rem;
707 /**
708 * @dent_clust: cluster of long name start
709 */
710 unsigned int dent_clust;
711 /**
712 * @dent_start: first directory entry for long name
713 */
714 dir_entry *dent_start;
715 /**
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100716 * @l_name: long name of current directory entry
717 */
718 char l_name[VFAT_MAXLEN_BYTES];
719 /**
720 * @s_name: short 8.3 name of current directory entry
721 */
722 char s_name[14];
723 /**
724 * @name: l_name if there is one, else s_name
725 */
726 char *name;
727 /**
728 * @block: buffer for current cluster
729 */
730 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
731};
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400732
733static int fat_itr_isdir(fat_itr *itr);
734
735/**
736 * fat_itr_root() - initialize an iterator to start at the root
737 * directory
738 *
739 * @itr: iterator to initialize
740 * @fsdata: filesystem data for the partition
741 * @return 0 on success, else -errno
742 */
743static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
744{
745 if (get_fs_info(fsdata))
746 return -ENXIO;
747
748 itr->fsdata = fsdata;
Heinrich Schuchardt7557c842020-11-22 16:04:47 +0100749 itr->start_clust = fsdata->root_cluster;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400750 itr->clust = fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900751 itr->next_clust = fsdata->root_cluster;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400752 itr->dent = NULL;
753 itr->remaining = 0;
754 itr->last_cluster = 0;
755 itr->is_root = 1;
756
757 return 0;
758}
759
760/**
761 * fat_itr_child() - initialize an iterator to descend into a sub-
762 * directory
763 *
764 * Initializes 'itr' to iterate the contents of the directory at
765 * the current cursor position of 'parent'. It is an error to
766 * call this if the current cursor of 'parent' is pointing at a
767 * regular file.
768 *
769 * Note that 'itr' and 'parent' can be the same pointer if you do
770 * not need to preserve 'parent' after this call, which is useful
771 * for traversing directory structure to resolve a file/directory.
772 *
773 * @itr: iterator to initialize
774 * @parent: the iterator pointing at a directory entry in the
775 * parent directory of the directory to iterate
776 */
777static void fat_itr_child(fat_itr *itr, fat_itr *parent)
778{
779 fsdata *mydata = parent->fsdata; /* for silly macros */
780 unsigned clustnum = START(parent->dent);
781
782 assert(fat_itr_isdir(parent));
783
784 itr->fsdata = parent->fsdata;
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900785 itr->start_clust = clustnum;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400786 if (clustnum > 0) {
787 itr->clust = clustnum;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900788 itr->next_clust = clustnum;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300789 itr->is_root = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400790 } else {
791 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900792 itr->next_clust = parent->fsdata->root_cluster;
Heinrich Schuchardt7557c842020-11-22 16:04:47 +0100793 itr->start_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300794 itr->is_root = 1;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400795 }
796 itr->dent = NULL;
797 itr->remaining = 0;
798 itr->last_cluster = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400799}
800
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100801/**
802 * fat_next_cluster() - load next FAT cluster
803 *
804 * The function is used when iterating through directories. It loads the
805 * next cluster with directory entries
806 *
807 * @itr: directory iterator
808 * @nbytes: number of bytes read, 0 on error
809 * Return: first directory entry, NULL on error
810 */
811void *fat_next_cluster(fat_itr *itr, unsigned int *nbytes)
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400812{
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400813 int ret;
814 u32 sect;
Anssi Hannula9b183582019-02-27 12:55:57 +0200815 u32 read_size;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400816
817 /* have we reached the end? */
818 if (itr->last_cluster)
819 return NULL;
820
Anssi Hannula9b183582019-02-27 12:55:57 +0200821 if (itr->is_root && itr->fsdata->fatsize != 32) {
822 /*
823 * The root directory is located before the data area and
824 * cannot be indexed using the regular unsigned cluster
825 * numbers (it may start at a "negative" cluster or not at a
826 * cluster boundary at all), so consider itr->next_clust to be
827 * a offset in cluster-sized units from the start of rootdir.
828 */
829 unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
830 unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
831 sect = itr->fsdata->rootdir_sect + sect_offset;
832 /* do not read past the end of rootdir */
833 read_size = min_t(u32, itr->fsdata->clust_size,
834 remaining_sects);
835 } else {
836 sect = clust_to_sect(itr->fsdata, itr->next_clust);
837 read_size = itr->fsdata->clust_size;
838 }
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400839
Heinrich Schuchardtd0be6762020-12-25 14:30:04 +0100840 log_debug("FAT read(sect=%d), clust_size=%d, read_size=%u\n",
841 sect, itr->fsdata->clust_size, read_size);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400842
843 /*
844 * NOTE: do_fat_read_at() had complicated logic to deal w/
845 * vfat names that span multiple clusters in the fat16 case,
846 * which get_dentfromdir() probably also needed (and was
847 * missing). And not entirely sure what fat32 didn't have
848 * the same issue.. We solve that by only caring about one
849 * dent at a time and iteratively constructing the vfat long
850 * name.
851 */
Anssi Hannula9b183582019-02-27 12:55:57 +0200852 ret = disk_read(sect, read_size, itr->block);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400853 if (ret < 0) {
854 debug("Error: reading block\n");
855 return NULL;
856 }
857
Anssi Hannula9b183582019-02-27 12:55:57 +0200858 *nbytes = read_size * itr->fsdata->sect_size;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900859 itr->clust = itr->next_clust;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400860 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900861 itr->next_clust++;
Anssi Hannula9b183582019-02-27 12:55:57 +0200862 if (itr->next_clust * itr->fsdata->clust_size >=
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400863 itr->fsdata->rootdir_size) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900864 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400865 itr->last_cluster = 1;
866 }
867 } else {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900868 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
869 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
870 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400871 itr->last_cluster = 1;
872 }
873 }
874
875 return itr->block;
876}
877
878static dir_entry *next_dent(fat_itr *itr)
879{
880 if (itr->remaining == 0) {
Anssi Hannula9b183582019-02-27 12:55:57 +0200881 unsigned nbytes;
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100882 struct dir_entry *dent = fat_next_cluster(itr, &nbytes);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400883
884 /* have we reached the last cluster? */
AKASHI Takahirof528c142018-09-11 15:59:00 +0900885 if (!dent) {
886 /* a sign for no more entries left */
887 itr->dent = NULL;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400888 return NULL;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900889 }
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400890
891 itr->remaining = nbytes / sizeof(dir_entry) - 1;
892 itr->dent = dent;
893 } else {
894 itr->remaining--;
895 itr->dent++;
896 }
897
898 /* have we reached the last valid entry? */
899 if (itr->dent->name[0] == 0)
900 return NULL;
901
902 return itr->dent;
903}
904
905static dir_entry *extract_vfat_name(fat_itr *itr)
906{
907 struct dir_entry *dent = itr->dent;
908 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
909 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
910 int n = 0;
911
912 while (seqn--) {
913 char buf[13];
914 int idx = 0;
915
916 slot2str((dir_slot *)dent, buf, &idx);
917
Patrick Wildt8b021bb2018-11-26 15:58:13 +0100918 if (n + idx >= sizeof(itr->l_name))
919 return NULL;
920
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400921 /* shift accumulated long-name up and copy new part in: */
922 memmove(itr->l_name + idx, itr->l_name, n);
923 memcpy(itr->l_name, buf, idx);
924 n += idx;
925
926 dent = next_dent(itr);
927 if (!dent)
928 return NULL;
929 }
930
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900931 /*
932 * We are now at the short file name entry.
933 * If it is marked as deleted, just skip it.
934 */
935 if (dent->name[0] == DELETED_FLAG ||
936 dent->name[0] == aRING)
937 return NULL;
938
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400939 itr->l_name[n] = '\0';
940
941 chksum = mkcksum(dent->name, dent->ext);
942
943 /* checksum mismatch could mean deleted file, etc.. skip it: */
944 if (chksum != alias_checksum) {
945 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
946 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
947 return NULL;
948 }
949
950 return dent;
951}
952
953/**
954 * fat_itr_next() - step to the next entry in a directory
955 *
956 * Must be called once on a new iterator before the cursor is valid.
957 *
958 * @itr: the iterator to iterate
959 * @return boolean, 1 if success or 0 if no more entries in the
960 * current directory
961 */
962static int fat_itr_next(fat_itr *itr)
963{
964 dir_entry *dent;
965
966 itr->name = NULL;
967
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900968 /*
969 * One logical directory entry consist of following slots:
970 * name[0] Attributes
971 * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT
972 * ...
973 * dent[N - 2]: LFN[1] 2 ATTR_VFAT
974 * dent[N - 1]: LFN[0] 1 ATTR_VFAT
975 * dent[N]: SFN ATTR_ARCH
976 */
977
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400978 while (1) {
979 dent = next_dent(itr);
Heinrich Schuchardt89735b42020-11-19 07:44:08 +0100980 if (!dent) {
981 itr->dent_start = NULL;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400982 return 0;
Heinrich Schuchardt89735b42020-11-19 07:44:08 +0100983 }
984 itr->dent_rem = itr->remaining;
985 itr->dent_start = itr->dent;
986 itr->dent_clust = itr->clust;
Heinrich Schuchardta2c5a922020-11-21 12:34:20 +0100987 if (dent->name[0] == DELETED_FLAG)
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400988 continue;
989
990 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynen8bad6cb2018-01-05 02:45:21 +0200991 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400992 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900993 /* long file name */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400994 dent = extract_vfat_name(itr);
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900995 /*
996 * If succeeded, dent has a valid short file
997 * name entry for the current entry.
998 * If failed, itr points to a current bogus
999 * entry. So after fetching a next one,
1000 * it may have a short file name entry
1001 * for this bogus entry so that we can still
1002 * check for a short name.
1003 */
Rob Clarkc6e3baa2017-09-09 13:15:53 -04001004 if (!dent)
1005 continue;
1006 itr->name = itr->l_name;
1007 break;
1008 } else {
1009 /* Volume label or VFAT entry, skip */
1010 continue;
1011 }
Christian Gmeiner1788a962020-06-09 09:09:07 +02001012 }
Rob Clarkc6e3baa2017-09-09 13:15:53 -04001013
AKASHI Takahiro39606d42019-11-26 17:29:31 +09001014 /* short file name */
Rob Clarkc6e3baa2017-09-09 13:15:53 -04001015 break;
1016 }
1017
1018 get_name(dent, itr->s_name);
1019 if (!itr->name)
1020 itr->name = itr->s_name;
1021
1022 return 1;
1023}
1024
1025/**
1026 * fat_itr_isdir() - is current cursor position pointing to a directory
1027 *
1028 * @itr: the iterator
1029 * @return true if cursor is at a directory
1030 */
1031static int fat_itr_isdir(fat_itr *itr)
1032{
1033 return !!(itr->dent->attr & ATTR_DIR);
1034}
1035
1036/*
1037 * Helpers:
1038 */
1039
1040#define TYPE_FILE 0x1
1041#define TYPE_DIR 0x2
1042#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
1043
1044/**
1045 * fat_itr_resolve() - traverse directory structure to resolve the
1046 * requested path.
1047 *
1048 * Traverse directory structure to the requested path. If the specified
1049 * path is to a directory, this will descend into the directory and
1050 * leave it iterator at the start of the directory. If the path is to a
1051 * file, it will leave the iterator in the parent directory with current
1052 * cursor at file's entry in the directory.
1053 *
1054 * @itr: iterator initialized to root
1055 * @path: the requested path
1056 * @type: bitmask of allowable file types
1057 * @return 0 on success or -errno
1058 */
1059static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
1060{
1061 const char *next;
1062
1063 /* chomp any extra leading slashes: */
1064 while (path[0] && ISDIRDELIM(path[0]))
1065 path++;
1066
1067 /* are we at the end? */
1068 if (strlen(path) == 0) {
1069 if (!(type & TYPE_DIR))
1070 return -ENOENT;
1071 return 0;
1072 }
1073
1074 /* find length of next path entry: */
1075 next = path;
1076 while (next[0] && !ISDIRDELIM(next[0]))
1077 next++;
1078
AKASHI Takahirob94b6be2018-09-11 15:58:59 +09001079 if (itr->is_root) {
1080 /* root dir doesn't have "." nor ".." */
1081 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
1082 (((next - path) == 2) && !strncmp(path, "..", 2))) {
1083 /* point back to itself */
1084 itr->clust = itr->fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +09001085 itr->next_clust = itr->fsdata->root_cluster;
Heinrich Schuchardt7557c842020-11-22 16:04:47 +01001086 itr->start_clust = itr->fsdata->root_cluster;
AKASHI Takahirob94b6be2018-09-11 15:58:59 +09001087 itr->dent = NULL;
1088 itr->remaining = 0;
1089 itr->last_cluster = 0;
1090
1091 if (next[0] == 0) {
1092 if (type & TYPE_DIR)
1093 return 0;
1094 else
1095 return -ENOENT;
1096 }
1097
1098 return fat_itr_resolve(itr, next, type);
1099 }
1100 }
1101
Rob Clarkc6e3baa2017-09-09 13:15:53 -04001102 while (fat_itr_next(itr)) {
1103 int match = 0;
1104 unsigned n = max(strlen(itr->name), (size_t)(next - path));
1105
1106 /* check both long and short name: */
1107 if (!strncasecmp(path, itr->name, n))
1108 match = 1;
1109 else if (itr->name != itr->s_name &&
1110 !strncasecmp(path, itr->s_name, n))
1111 match = 1;
1112
1113 if (!match)
1114 continue;
1115
1116 if (fat_itr_isdir(itr)) {
1117 /* recurse into directory: */
1118 fat_itr_child(itr, itr);
1119 return fat_itr_resolve(itr, next, type);
1120 } else if (next[0]) {
1121 /*
1122 * If next is not empty then we have a case
1123 * like: /path/to/realfile/nonsense
1124 */
1125 debug("bad trailing path: %s\n", next);
1126 return -ENOENT;
1127 } else if (!(type & TYPE_FILE)) {
1128 return -ENOTDIR;
1129 } else {
1130 return 0;
1131 }
1132 }
1133
1134 return -ENOENT;
1135}
1136
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001137int file_fat_detectfs(void)
wdenk71f95112003-06-15 22:40:42 +00001138{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001139 boot_sector bs;
1140 volume_info volinfo;
1141 int fatsize;
1142 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001143
Wolfgang Denk7385c282010-07-19 11:37:00 +02001144 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001145 printf("No current device\n");
1146 return 1;
1147 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001148
Simon Glassfc843a02017-05-17 03:25:30 -06001149#if defined(CONFIG_IDE) || \
Simon Glass10e40d52017-06-14 21:28:25 -06001150 defined(CONFIG_SATA) || \
Simon Glassc649e3c2016-05-01 11:36:02 -06001151 defined(CONFIG_SCSI) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001152 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001153 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001154 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001155 switch (cur_dev->if_type) {
1156 case IF_TYPE_IDE:
1157 printf("IDE");
1158 break;
1159 case IF_TYPE_SATA:
1160 printf("SATA");
1161 break;
1162 case IF_TYPE_SCSI:
1163 printf("SCSI");
1164 break;
1165 case IF_TYPE_ATAPI:
1166 printf("ATAPI");
1167 break;
1168 case IF_TYPE_USB:
1169 printf("USB");
1170 break;
1171 case IF_TYPE_DOC:
1172 printf("DOC");
1173 break;
1174 case IF_TYPE_MMC:
1175 printf("MMC");
1176 break;
1177 default:
1178 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001179 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001180
Simon Glassbcce53d2016-02-29 15:25:51 -07001181 printf("\n Device %d: ", cur_dev->devnum);
wdenk7205e402003-09-10 22:30:53 +00001182 dev_print(cur_dev);
1183#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001184
1185 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001186 printf("\nNo valid FAT fs found\n");
1187 return 1;
1188 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001189
1190 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001191 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001192 volinfo.fs_type[5] = '\0';
1193
Stephen Warren461f86e2012-10-17 06:44:57 +00001194 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001195
wdenk7205e402003-09-10 22:30:53 +00001196 return 0;
wdenk71f95112003-06-15 22:40:42 +00001197}
1198
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001199int fat_exists(const char *filename)
1200{
Rob Clark8eafae22017-09-09 13:15:54 -04001201 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001202 fat_itr *itr;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001203 int ret;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001204
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001205 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001206 if (!itr)
1207 return 0;
Rob Clark8eafae22017-09-09 13:15:54 -04001208 ret = fat_itr_root(itr, &fsdata);
1209 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001210 goto out;
Rob Clark8eafae22017-09-09 13:15:54 -04001211
1212 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clark725ffdb2017-09-12 16:40:01 -04001213 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001214out:
Tom Rini24600982017-09-22 07:37:43 -04001215 free(itr);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001216 return ret == 0;
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001217}
1218
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001219int fat_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -06001220{
Rob Clark8eafae22017-09-09 13:15:54 -04001221 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001222 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001223 int ret;
1224
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001225 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001226 if (!itr)
1227 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001228 ret = fat_itr_root(itr, &fsdata);
1229 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001230 goto out_free_itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001231
1232 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1233 if (ret) {
1234 /*
1235 * Directories don't have size, but fs_size() is not
1236 * expected to fail if passed a directory path:
1237 */
Rob Clark725ffdb2017-09-12 16:40:01 -04001238 free(fsdata.fatbuf);
Andrew F. Davisd0cd30e2019-05-16 09:34:31 -05001239 ret = fat_itr_root(itr, &fsdata);
1240 if (ret)
1241 goto out_free_itr;
1242 ret = fat_itr_resolve(itr, filename, TYPE_DIR);
1243 if (!ret)
Rob Clark8eafae22017-09-09 13:15:54 -04001244 *size = 0;
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001245 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001246 }
1247
1248 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001249out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001250 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001251out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001252 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001253 return ret;
Stephen Warrencf659812014-06-11 12:47:26 -06001254}
1255
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001256int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1257 loff_t maxsize, loff_t *actread)
wdenk71f95112003-06-15 22:40:42 +00001258{
Rob Clark8eafae22017-09-09 13:15:54 -04001259 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001260 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001261 int ret;
1262
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001263 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001264 if (!itr)
1265 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001266 ret = fat_itr_root(itr, &fsdata);
1267 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001268 goto out_free_itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001269
1270 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1271 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001272 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001273
Andreas Dannenberg287c04e2018-08-13 21:35:15 -05001274 debug("reading %s at pos %llu\n", filename, pos);
Tien Fong Cheee48485f2019-02-11 14:56:20 +08001275
1276 /* For saving default max clustersize memory allocated to malloc pool */
1277 dir_entry *dentptr = itr->dent;
1278
Tien Fong Cheee48485f2019-02-11 14:56:20 +08001279 ret = get_contents(&fsdata, dentptr, pos, buffer, maxsize, actread);
Rob Clark725ffdb2017-09-12 16:40:01 -04001280
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001281out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001282 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001283out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001284 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001285 return ret;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001286}
1287
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001288int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001289{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001290 loff_t actread;
1291 int ret;
1292
1293 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1294 if (ret)
1295 return ret;
1296 else
1297 return actread;
wdenk71f95112003-06-15 22:40:42 +00001298}
Simon Glasse6d52412012-12-26 09:53:33 +00001299
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001300int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1301 loff_t *actread)
Simon Glasse6d52412012-12-26 09:53:33 +00001302{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001303 int ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001304
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001305 ret = file_fat_read_at(filename, offset, buf, len, actread);
1306 if (ret)
Simon Glasse6d52412012-12-26 09:53:33 +00001307 printf("** Unable to read file %s **\n", filename);
Simon Glasse6d52412012-12-26 09:53:33 +00001308
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001309 return ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001310}
1311
Rob Clark1f403662017-09-09 13:15:56 -04001312typedef struct {
1313 struct fs_dir_stream parent;
1314 struct fs_dirent dirent;
1315 fsdata fsdata;
1316 fat_itr itr;
1317} fat_dir;
1318
1319int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1320{
Neil Armstrong34dd8532017-11-24 09:54:41 +01001321 fat_dir *dir;
Rob Clark1f403662017-09-09 13:15:56 -04001322 int ret;
1323
Neil Armstrong34dd8532017-11-24 09:54:41 +01001324 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001325 if (!dir)
1326 return -ENOMEM;
Neil Armstrong34dd8532017-11-24 09:54:41 +01001327 memset(dir, 0, sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001328
1329 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1330 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001331 goto fail_free_dir;
Rob Clark1f403662017-09-09 13:15:56 -04001332
1333 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1334 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001335 goto fail_free_both;
Rob Clark1f403662017-09-09 13:15:56 -04001336
1337 *dirsp = (struct fs_dir_stream *)dir;
1338 return 0;
1339
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001340fail_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001341 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001342fail_free_dir:
Rob Clark1f403662017-09-09 13:15:56 -04001343 free(dir);
1344 return ret;
1345}
1346
1347int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1348{
1349 fat_dir *dir = (fat_dir *)dirs;
1350 struct fs_dirent *dent = &dir->dirent;
1351
1352 if (!fat_itr_next(&dir->itr))
1353 return -ENOENT;
1354
1355 memset(dent, 0, sizeof(*dent));
1356 strcpy(dent->name, dir->itr.name);
1357
1358 if (fat_itr_isdir(&dir->itr)) {
1359 dent->type = FS_DT_DIR;
1360 } else {
1361 dent->type = FS_DT_REG;
1362 dent->size = FAT2CPU32(dir->itr.dent->size);
1363 }
1364
1365 *dentp = dent;
1366
1367 return 0;
1368}
1369
1370void fat_closedir(struct fs_dir_stream *dirs)
1371{
1372 fat_dir *dir = (fat_dir *)dirs;
Rob Clark725ffdb2017-09-12 16:40:01 -04001373 free(dir->fsdata.fatbuf);
Rob Clark1f403662017-09-09 13:15:56 -04001374 free(dir);
1375}
1376
Simon Glasse6d52412012-12-26 09:53:33 +00001377void fat_close(void)
1378{
1379}