blob: d1476aa433d6fe920ac2a58d842f842cbe3d5311 [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
Simon Glass47459812023-07-15 21:39:06 -060011#define LOG_CATEGORY LOGC_FS
12
wdenk7a428cc2003-06-15 22:40:42 +000013#include <common.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -070014#include <blk.h>
wdenk7a428cc2003-06-15 22:40:42 +000015#include <config.h>
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +000016#include <exports.h>
wdenk7a428cc2003-06-15 22:40:42 +000017#include <fat.h>
Rob Clark4174d7f2017-09-09 13:15:56 -040018#include <fs.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
wdenk7a428cc2003-06-15 22:40:42 +000020#include <asm/byteorder.h>
wdenk2c9b05d2003-09-10 22:30:53 +000021#include <part.h>
Eric Nelson014705b2012-04-11 04:08:53 +000022#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060023#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060024#include <asm/cache.h>
Eric Nelson014705b2012-04-11 04:08:53 +000025#include <linux/compiler.h>
Richard Genoud2e372022012-12-13 00:47:36 +000026#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000027
wdenk7a428cc2003-06-15 22:40:42 +000028/*
Rob Clarkfd6c6b42017-09-09 13:15:59 -040029 * Convert a string to lowercase. Converts at most 'len' characters,
30 * 'len' may be larger than the length of 'str' if 'str' is NULL
31 * terminated.
wdenk7a428cc2003-06-15 22:40:42 +000032 */
Rob Clarkfd6c6b42017-09-09 13:15:59 -040033static void downcase(char *str, size_t len)
wdenk7a428cc2003-06-15 22:40:42 +000034{
Rob Clarkfd6c6b42017-09-09 13:15:59 -040035 while (*str != '\0' && len--) {
Richard Genoud2e372022012-12-13 00:47:36 +000036 *str = tolower(*str);
wdenk7a428cc2003-06-15 22:40:42 +000037 str++;
38 }
39}
40
Simon Glasse3394752016-02-29 15:25:34 -070041static struct blk_desc *cur_dev;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060042static struct disk_partition cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020043
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000044#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000045#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020046#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000047
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000048static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000049{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020050 ulong ret;
51
Simon Glass2ee8ada2016-02-29 15:25:52 -070052 if (!cur_dev)
wdenk2c9b05d2003-09-10 22:30:53 +000053 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020054
Simon Glass2ee8ada2016-02-29 15:25:52 -070055 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020056
Tom Rini240124c2017-08-14 21:02:08 -040057 if (ret != nr_blocks)
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020058 return -1;
59
60 return ret;
wdenk7a428cc2003-06-15 22:40:42 +000061}
62
Simon Glassc1c4a8f2020-05-10 11:39:57 -060063int fat_set_blk_dev(struct blk_desc *dev_desc, struct disk_partition *info)
wdenk7a428cc2003-06-15 22:40:42 +000064{
Eric Nelson014705b2012-04-11 04:08:53 +000065 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000066
Stephen Warren288904d2012-10-17 06:44:59 +000067 cur_dev = dev_desc;
68 cur_part_info = *info;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000069
70 /* Make sure it has a valid FAT header */
71 if (disk_read(0, 1, buffer) != 1) {
72 cur_dev = NULL;
73 return -1;
Wolfgang Denke78f2582007-08-07 16:02:13 +020074 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000075
76 /* Check if it's actually a DOS volume */
77 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
78 cur_dev = NULL;
79 return -1;
80 }
81
82 /* Check for FAT12/FAT16/FAT32 filesystem */
83 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
84 return 0;
85 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
86 return 0;
87
88 cur_dev = NULL;
89 return -1;
wdenk7a428cc2003-06-15 22:40:42 +000090}
91
Simon Glasse3394752016-02-29 15:25:34 -070092int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren288904d2012-10-17 06:44:59 +000093{
Simon Glassc1c4a8f2020-05-10 11:39:57 -060094 struct disk_partition info;
Stephen Warren288904d2012-10-17 06:44:59 +000095
96 /* First close any currently found FAT filesystem */
97 cur_dev = NULL;
98
99 /* Read the partition table, if present */
Simon Glassb89a8442016-02-29 15:25:48 -0700100 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren288904d2012-10-17 06:44:59 +0000101 if (part_no != 0) {
Simon Glass47459812023-07-15 21:39:06 -0600102 log_err("Partition %d invalid on device %d\n", part_no,
103 dev_desc->devnum);
Stephen Warren288904d2012-10-17 06:44:59 +0000104 return -1;
105 }
106
107 info.start = 0;
108 info.size = dev_desc->lba;
109 info.blksz = dev_desc->blksz;
110 info.name[0] = 0;
111 info.type[0] = 0;
112 info.bootable = 0;
Patrick Delaunay73287092017-01-27 11:00:42 +0100113#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren288904d2012-10-17 06:44:59 +0000114 info.uuid[0] = 0;
115#endif
116 }
117
118 return fat_set_blk_dev(dev_desc, &info);
119}
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000120
wdenk7a428cc2003-06-15 22:40:42 +0000121/*
wdenk7a428cc2003-06-15 22:40:42 +0000122 * Extract zero terminated short name from a directory entry.
123 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200124static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000125{
126 char *ptr;
127
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100128 memcpy(s_name, dirent->nameext.name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000129 s_name[8] = '\0';
130 ptr = s_name;
131 while (*ptr && *ptr != ' ')
132 ptr++;
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400133 if (dirent->lcase & CASE_LOWER_BASE)
134 downcase(s_name, (unsigned)(ptr - s_name));
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100135 if (dirent->nameext.ext[0] && dirent->nameext.ext[0] != ' ') {
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400136 *ptr++ = '.';
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100137 memcpy(ptr, dirent->nameext.ext, 3);
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400138 if (dirent->lcase & CASE_LOWER_EXT)
139 downcase(ptr, 3);
wdenk7a428cc2003-06-15 22:40:42 +0000140 ptr[3] = '\0';
141 while (*ptr && *ptr != ' ')
142 ptr++;
143 }
144 *ptr = '\0';
145 if (*s_name == DELETED_FLAG)
146 *s_name = '\0';
147 else if (*s_name == aRING)
Remy Bohmer7c8f2ce2008-11-27 22:30:27 +0100148 *s_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000149}
150
Stefan Brüns9f95d812016-12-17 00:27:51 +0100151static int flush_dirty_fat_buffer(fsdata *mydata);
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800152
153#if !CONFIG_IS_ENABLED(FAT_WRITE)
Stefan Brüns9f95d812016-12-17 00:27:51 +0100154/* Stub for read only operation */
155int flush_dirty_fat_buffer(fsdata *mydata)
156{
157 (void)(mydata);
158 return 0;
159}
160#endif
161
wdenk7a428cc2003-06-15 22:40:42 +0000162/*
163 * Get the entry at index 'entry' in a FAT (12/16/32) table.
164 * On failure 0x00 is returned.
165 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200166static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000167{
168 __u32 bufnum;
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000169 __u32 offset, off8;
wdenk7a428cc2003-06-15 22:40:42 +0000170 __u32 ret = 0x00;
171
Stefan Brüns9f95d812016-12-17 00:27:51 +0100172 if (CHECK_CLUST(entry, mydata->fatsize)) {
Simon Glass47459812023-07-15 21:39:06 -0600173 log_err("Invalid FAT entry: %#08x\n", entry);
Stefan Brüns9f95d812016-12-17 00:27:51 +0100174 return ret;
175 }
176
wdenk7a428cc2003-06-15 22:40:42 +0000177 switch (mydata->fatsize) {
178 case 32:
179 bufnum = entry / FAT32BUFSIZE;
180 offset = entry - bufnum * FAT32BUFSIZE;
181 break;
182 case 16:
183 bufnum = entry / FAT16BUFSIZE;
184 offset = entry - bufnum * FAT16BUFSIZE;
185 break;
186 case 12:
187 bufnum = entry / FAT12BUFSIZE;
188 offset = entry - bufnum * FAT12BUFSIZE;
189 break;
190
191 default:
192 /* Unsupported FAT size */
193 return ret;
194 }
195
Stefan Brüns9f95d812016-12-17 00:27:51 +0100196 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200197 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200198
wdenk7a428cc2003-06-15 22:40:42 +0000199 /* Read a new block of FAT entries into the cache. */
200 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000201 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000202 __u8 *bufptr = mydata->fatbuf;
203 __u32 fatlength = mydata->fatlength;
204 __u32 startblock = bufnum * FATBUFBLOCKS;
205
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100206 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200207 if (startblock + getsize > fatlength)
208 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000209
wdenk7a428cc2003-06-15 22:40:42 +0000210 startblock += mydata->fat_sect; /* Offset from start of disk */
211
Stefan Brüns9f95d812016-12-17 00:27:51 +0100212 /* Write back the fatbuf to the disk */
213 if (flush_dirty_fat_buffer(mydata) < 0)
214 return -1;
215
wdenk7a428cc2003-06-15 22:40:42 +0000216 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200217 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000218 return ret;
219 }
220 mydata->fatbufnum = bufnum;
221 }
222
223 /* Get the actual entry from the table */
224 switch (mydata->fatsize) {
225 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200226 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000227 break;
228 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200229 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000230 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200231 case 12:
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000232 off8 = (offset * 3) / 2;
233 /* fatbut + off8 may be unaligned, read in byte granularity */
234 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk7a428cc2003-06-15 22:40:42 +0000235
Stefan Brüns15f40632016-12-17 03:55:10 +0100236 if (offset & 0x1)
237 ret >>= 4;
238 ret &= 0xfff;
wdenk7a428cc2003-06-15 22:40:42 +0000239 }
Stefan Brüns9f95d812016-12-17 00:27:51 +0100240 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
241 mydata->fatsize, ret, entry, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000242
243 return ret;
244}
245
wdenk7a428cc2003-06-15 22:40:42 +0000246/*
247 * Read at most 'size' bytes from the specified cluster into 'buffer'.
248 * Return 0 on success, -1 otherwise.
249 */
250static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200251get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000252{
wdenk7a428cc2003-06-15 22:40:42 +0000253 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000254 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000255
256 if (clustnum > 0) {
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400257 startsect = clust_to_sect(mydata, clustnum);
wdenk7a428cc2003-06-15 22:40:42 +0000258 } else {
259 startsect = mydata->rootdir_sect;
260 }
261
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200262 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
263
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200264 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000265 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200266
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200267 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200268
269 while (size >= mydata->sect_size) {
270 ret = disk_read(startsect++, 1, tmpbuf);
271 if (ret != 1) {
272 debug("Error reading data (got %d)\n", ret);
273 return -1;
274 }
275
276 memcpy(buffer, tmpbuf, mydata->sect_size);
277 buffer += mydata->sect_size;
278 size -= mydata->sect_size;
279 }
Ricardo Salveti8fb445a2021-09-26 21:36:04 +0300280 } else if (size >= mydata->sect_size) {
281 __u32 bytes_read;
282 __u32 sect_count = size / mydata->sect_size;
Heinrich Schuchardteb3587b2021-01-26 00:14:14 +0100283
Ricardo Salveti8fb445a2021-09-26 21:36:04 +0300284 ret = disk_read(startsect, sect_count, buffer);
285 if (ret != sect_count) {
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200286 debug("Error reading data (got %d)\n", ret);
287 return -1;
288 }
Ricardo Salveti8fb445a2021-09-26 21:36:04 +0300289 bytes_read = sect_count * mydata->sect_size;
290 startsect += sect_count;
291 buffer += bytes_read;
292 size -= bytes_read;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200293 }
294 if (size) {
295 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
296
297 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000298 if (ret != 1) {
299 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000300 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000301 }
wdenk2c9b05d2003-09-10 22:30:53 +0000302
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200303 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000304 }
305
306 return 0;
307}
308
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200309/**
310 * get_contents() - read from file
311 *
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000312 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
Heinrich Schuchardtc1a159c2019-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
wdenk7a428cc2003-06-15 22:40:42 +0000323 */
Suriyan Ramasami441c2232014-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)
wdenk7a428cc2003-06-15 22:40:42 +0000326{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800327 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000328 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000329 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000330 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800331 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000332
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800333 *gotsize = 0;
334 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000335
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000336 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800337 debug("Read position past EOF: %llu\n", pos);
338 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000339 }
340
341 if (maxsize > 0 && filesize > pos + maxsize)
342 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000343
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800344 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000345
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200346 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-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 Schuchardtc1a159c2019-09-12 19:19:29 +0200353 printf("Invalid FAT entry\n");
354 return -1;
Benoît Thébaudeau5295aa82012-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 Cheef6764f22019-02-11 14:56:19 +0800366 __u8 *tmp_buffer;
367
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800368 actsize = min(filesize, (loff_t)bytesperclust);
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800369 tmp_buffer = malloc_cache_aligned(actsize);
370 if (!tmp_buffer) {
371 debug("Error: allocating buffer\n");
Heinrich Schuchardtd37d47c2019-09-12 19:19:30 +0200372 return -1;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800373 }
374
375 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000376 printf("Error reading cluster\n");
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800377 free(tmp_buffer);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000378 return -1;
379 }
380 filesize -= actsize;
381 actsize -= pos;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800382 memcpy(buffer, tmp_buffer + pos, actsize);
383 free(tmp_buffer);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800384 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000385 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800386 return 0;
Benoît Thébaudeau5295aa82012-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 Schuchardtc1a159c2019-09-12 19:19:29 +0200392 printf("Invalid FAT entry\n");
393 return -1;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000394 }
395 }
396
397 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200398 endclust = curclust;
399
wdenk7a428cc2003-06-15 22:40:42 +0000400 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000401 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200402 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000403 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200404 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000405 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100406 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200407 debug("curclust: 0x%x\n", newclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200408 printf("Invalid FAT entry\n");
409 return -1;
wdenk2c9b05d2003-09-10 22:30:53 +0000410 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200411 endclust = newclust;
412 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000413 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200414
wdenk2c9b05d2003-09-10 22:30:53 +0000415 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200416 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200417 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200418 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000419 return -1;
420 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800421 *gotsize += actsize;
422 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000423getit:
424 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200425 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000426 return -1;
427 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800428 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000429 filesize -= actsize;
430 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200431
wdenk2c9b05d2003-09-10 22:30:53 +0000432 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100433 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200434 debug("curclust: 0x%x\n", curclust);
435 printf("Invalid FAT entry\n");
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200436 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000437 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200438 actsize = bytesperclust;
439 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000440 } while (1);
441}
442
wdenk7a428cc2003-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ébaudeau0a120372012-07-20 15:18:44 +0200448static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200454 if (l_name[*idx] == 0x00)
455 return 1;
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200460 if (l_name[*idx] == 0x00)
461 return 1;
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200466 if (l_name[*idx] == 0x00)
467 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000468 (*idx)++;
469 }
470
471 return 0;
472}
473
wdenk7a428cc2003-06-15 22:40:42 +0000474/* Calculate short name checksum */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100475static __u8 mkcksum(struct nameext *nameext)
wdenk7a428cc2003-06-15 22:40:42 +0000476{
477 int i;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100478 u8 *pos = (void *)nameext;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200479
wdenk7a428cc2003-06-15 22:40:42 +0000480 __u8 ret = 0;
481
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100482 for (i = 0; i < 11; i++)
483 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + pos[i];
wdenk7a428cc2003-06-15 22:40:42 +0000484
485 return ret;
486}
wdenk7a428cc2003-06-15 22:40:42 +0000487
488/*
wdenk7a428cc2003-06-15 22:40:42 +0000489 * Read boot sector and volume info from a FAT filesystem
490 */
491static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200492read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000493{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000494 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000495 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000496 int ret = 0;
497
498 if (cur_dev == NULL) {
499 debug("Error: no device selected\n");
500 return -1;
501 }
502
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300503 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000504 if (block == NULL) {
505 debug("Error: allocating block\n");
506 return -1;
507 }
wdenk7a428cc2003-06-15 22:40:42 +0000508
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200509 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200510 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000511 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000512 }
513
514 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200515 bs->reserved = FAT2CPU16(bs->reserved);
516 bs->fat_length = FAT2CPU16(bs->fat_length);
517 bs->secs_track = FAT2CPU16(bs->secs_track);
518 bs->heads = FAT2CPU16(bs->heads);
519 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000520
521 /* FAT32 entries */
522 if (bs->fat_length == 0) {
523 /* Assume FAT32 */
524 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200525 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000526 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200527 bs->info_sector = FAT2CPU16(bs->info_sector);
528 bs->backup_boot = FAT2CPU16(bs->backup_boot);
529 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000530 *fatsize = 32;
531 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200532 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000533 *fatsize = 0;
534 }
535 memcpy(volinfo, vistart, sizeof(volume_info));
536
wdenk7a428cc2003-06-15 22:40:42 +0000537 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200538 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000539 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000540 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500541 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000542 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000543 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000544 }
Tom Rix155abaa2009-05-20 07:55:41 -0500545 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000546 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000547 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000548 }
549 }
550
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200551 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000552fail:
553 ret = -1;
554exit:
555 free(block);
556 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000557}
558
Rob Clark4996a5f2017-09-09 13:15:52 -0400559static int get_fs_info(fsdata *mydata)
wdenk7a428cc2003-06-15 22:40:42 +0000560{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200561 boot_sector bs;
562 volume_info volinfo;
Rob Clark4996a5f2017-09-09 13:15:52 -0400563 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000564
Rob Clark4996a5f2017-09-09 13:15:52 -0400565 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
566 if (ret) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200567 debug("Error: reading boot sector\n");
Rob Clark4996a5f2017-09-09 13:15:52 -0400568 return ret;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200569 }
570
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000571 if (mydata->fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200572 mydata->fatlength = bs.fat32_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900573 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000574 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200575 mydata->fatlength = bs.fat_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900576 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
577 if (!mydata->total_sect)
578 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000579 }
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900580 if (!mydata->total_sect) /* unlikely */
581 mydata->total_sect = (u32)cur_part_info.size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200582
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900583 mydata->fats = bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200584 mydata->fat_sect = bs.reserved;
585
Rob Clark4996a5f2017-09-09 13:15:52 -0400586 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200587
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000588 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200589 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000590 if (mydata->sect_size != cur_part_info.blksz) {
Simon Glass47459812023-07-15 21:39:06 -0600591 log_err("FAT sector size mismatch (fs=%u, dev=%lu)\n",
592 mydata->sect_size, cur_part_info.blksz);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000593 return -1;
594 }
Patrick Wildt1d614b92018-11-26 15:56:57 +0100595 if (mydata->clust_size == 0) {
Simon Glass47459812023-07-15 21:39:06 -0600596 log_err("FAT cluster size not set\n");
Patrick Wildt1d614b92018-11-26 15:56:57 +0100597 return -1;
598 }
599 if ((unsigned int)mydata->clust_size * mydata->sect_size >
600 MAX_CLUSTSIZE) {
Simon Glass47459812023-07-15 21:39:06 -0600601 log_err("FAT cluster size too big (cs=%u, max=%u)\n",
602 (uint)mydata->clust_size * mydata->sect_size,
603 MAX_CLUSTSIZE);
Patrick Wildt1d614b92018-11-26 15:56:57 +0100604 return -1;
605 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200606
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200607 if (mydata->fatsize == 32) {
608 mydata->data_begin = mydata->rootdir_sect -
609 (mydata->clust_size * 2);
Rob Clark0c33fce2017-09-09 13:15:53 -0400610 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200611 } else {
Rob Clark4996a5f2017-09-09 13:15:52 -0400612 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
613 bs.dir_entries[0]) *
614 sizeof(dir_entry)) /
615 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200616 mydata->data_begin = mydata->rootdir_sect +
Rob Clark4996a5f2017-09-09 13:15:52 -0400617 mydata->rootdir_size -
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200618 (mydata->clust_size * 2);
Anssi Hannulaccea7332019-02-27 12:55:57 +0200619
620 /*
621 * The root directory is not cluster-aligned and may be on a
622 * "negative" cluster, this will be handled specially in
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100623 * fat_next_cluster().
Anssi Hannulaccea7332019-02-27 12:55:57 +0200624 */
625 mydata->root_cluster = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200626 }
627
628 mydata->fatbufnum = -1;
Stefan Brüns751b31d2016-09-11 22:51:40 +0200629 mydata->fat_dirty = 0;
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300630 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000631 if (mydata->fatbuf == NULL) {
632 debug("Error: allocating memory\n");
633 return -1;
634 }
wdenk7a428cc2003-06-15 22:40:42 +0000635
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200636 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
637 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
638 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
639 "Data begins at: %d\n",
Rob Clark0c33fce2017-09-09 13:15:53 -0400640 mydata->root_cluster,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200641 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000642 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
643 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
644 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000645
Rob Clark4996a5f2017-09-09 13:15:52 -0400646 return 0;
647}
648
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100649/**
650 * struct fat_itr - directory iterator, to simplify filesystem traversal
Rob Clark0c33fce2017-09-09 13:15:53 -0400651 *
652 * Implements an iterator pattern to traverse directory tables,
653 * transparently handling directory tables split across multiple
654 * clusters, and the difference between FAT12/FAT16 root directory
655 * (contiguous) and subdirectories + FAT32 root (chained).
656 *
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100657 * Rough usage
658 *
659 * .. code-block:: c
Rob Clark0c33fce2017-09-09 13:15:53 -0400660 *
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100661 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
662 * // to traverse down to a subdirectory pointed to by
663 * // current iterator position:
664 * fat_itr_child(&itr, &itr);
665 * }
Rob Clark0c33fce2017-09-09 13:15:53 -0400666 *
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100667 * For a more complete example, see fat_itr_resolve().
Rob Clark0c33fce2017-09-09 13:15:53 -0400668 */
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100669struct fat_itr {
670 /**
671 * @fsdata: filesystem parameters
672 */
673 fsdata *fsdata;
674 /**
675 * @start_clust: first cluster
676 */
677 unsigned int start_clust;
678 /**
679 * @clust: current cluster
680 */
681 unsigned int clust;
682 /**
683 * @next_clust: next cluster if remaining == 0
684 */
685 unsigned int next_clust;
686 /**
687 * @last_cluster: set if last cluster of directory reached
688 */
689 int last_cluster;
690 /**
691 * @is_root: is iterator at root directory
692 */
693 int is_root;
694 /**
695 * @remaining: remaining directory entries in current cluster
696 */
697 int remaining;
698 /**
699 * @dent: current directory entry
700 */
701 dir_entry *dent;
702 /**
Heinrich Schuchardt9a4564e2020-11-19 07:44:08 +0100703 * @dent_rem: remaining entries after long name start
704 */
705 int dent_rem;
706 /**
707 * @dent_clust: cluster of long name start
708 */
709 unsigned int dent_clust;
710 /**
711 * @dent_start: first directory entry for long name
712 */
713 dir_entry *dent_start;
714 /**
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100715 * @l_name: long name of current directory entry
716 */
717 char l_name[VFAT_MAXLEN_BYTES];
718 /**
719 * @s_name: short 8.3 name of current directory entry
720 */
721 char s_name[14];
722 /**
723 * @name: l_name if there is one, else s_name
724 */
725 char *name;
726 /**
727 * @block: buffer for current cluster
728 */
729 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
730};
Rob Clark0c33fce2017-09-09 13:15:53 -0400731
732static int fat_itr_isdir(fat_itr *itr);
733
734/**
735 * fat_itr_root() - initialize an iterator to start at the root
736 * directory
737 *
738 * @itr: iterator to initialize
739 * @fsdata: filesystem data for the partition
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100740 * Return: 0 on success, else -errno
Rob Clark0c33fce2017-09-09 13:15:53 -0400741 */
742static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
743{
744 if (get_fs_info(fsdata))
745 return -ENXIO;
746
747 itr->fsdata = fsdata;
Heinrich Schuchardt51d5b2a2020-11-22 16:04:47 +0100748 itr->start_clust = fsdata->root_cluster;
Rob Clark0c33fce2017-09-09 13:15:53 -0400749 itr->clust = fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900750 itr->next_clust = fsdata->root_cluster;
Rob Clark0c33fce2017-09-09 13:15:53 -0400751 itr->dent = NULL;
752 itr->remaining = 0;
753 itr->last_cluster = 0;
754 itr->is_root = 1;
755
756 return 0;
757}
758
759/**
760 * fat_itr_child() - initialize an iterator to descend into a sub-
761 * directory
762 *
763 * Initializes 'itr' to iterate the contents of the directory at
764 * the current cursor position of 'parent'. It is an error to
765 * call this if the current cursor of 'parent' is pointing at a
766 * regular file.
767 *
768 * Note that 'itr' and 'parent' can be the same pointer if you do
769 * not need to preserve 'parent' after this call, which is useful
770 * for traversing directory structure to resolve a file/directory.
771 *
772 * @itr: iterator to initialize
773 * @parent: the iterator pointing at a directory entry in the
774 * parent directory of the directory to iterate
775 */
776static void fat_itr_child(fat_itr *itr, fat_itr *parent)
777{
778 fsdata *mydata = parent->fsdata; /* for silly macros */
779 unsigned clustnum = START(parent->dent);
780
781 assert(fat_itr_isdir(parent));
782
783 itr->fsdata = parent->fsdata;
AKASHI Takahirof0926ae2018-09-11 15:59:09 +0900784 itr->start_clust = clustnum;
Rob Clark0c33fce2017-09-09 13:15:53 -0400785 if (clustnum > 0) {
786 itr->clust = clustnum;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900787 itr->next_clust = clustnum;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300788 itr->is_root = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400789 } else {
790 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900791 itr->next_clust = parent->fsdata->root_cluster;
Heinrich Schuchardt51d5b2a2020-11-22 16:04:47 +0100792 itr->start_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300793 itr->is_root = 1;
Rob Clark0c33fce2017-09-09 13:15:53 -0400794 }
795 itr->dent = NULL;
796 itr->remaining = 0;
797 itr->last_cluster = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400798}
799
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100800/**
801 * fat_next_cluster() - load next FAT cluster
802 *
803 * The function is used when iterating through directories. It loads the
804 * next cluster with directory entries
805 *
806 * @itr: directory iterator
807 * @nbytes: number of bytes read, 0 on error
808 * Return: first directory entry, NULL on error
809 */
810void *fat_next_cluster(fat_itr *itr, unsigned int *nbytes)
Rob Clark0c33fce2017-09-09 13:15:53 -0400811{
Rob Clark0c33fce2017-09-09 13:15:53 -0400812 int ret;
813 u32 sect;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200814 u32 read_size;
Rob Clark0c33fce2017-09-09 13:15:53 -0400815
816 /* have we reached the end? */
817 if (itr->last_cluster)
818 return NULL;
819
Anssi Hannulaccea7332019-02-27 12:55:57 +0200820 if (itr->is_root && itr->fsdata->fatsize != 32) {
821 /*
822 * The root directory is located before the data area and
823 * cannot be indexed using the regular unsigned cluster
824 * numbers (it may start at a "negative" cluster or not at a
825 * cluster boundary at all), so consider itr->next_clust to be
826 * a offset in cluster-sized units from the start of rootdir.
827 */
828 unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
829 unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
830 sect = itr->fsdata->rootdir_sect + sect_offset;
831 /* do not read past the end of rootdir */
832 read_size = min_t(u32, itr->fsdata->clust_size,
833 remaining_sects);
834 } else {
835 sect = clust_to_sect(itr->fsdata, itr->next_clust);
836 read_size = itr->fsdata->clust_size;
837 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400838
Heinrich Schuchardtbb093932020-12-25 14:30:04 +0100839 log_debug("FAT read(sect=%d), clust_size=%d, read_size=%u\n",
840 sect, itr->fsdata->clust_size, read_size);
Rob Clark0c33fce2017-09-09 13:15:53 -0400841
842 /*
843 * NOTE: do_fat_read_at() had complicated logic to deal w/
844 * vfat names that span multiple clusters in the fat16 case,
845 * which get_dentfromdir() probably also needed (and was
846 * missing). And not entirely sure what fat32 didn't have
847 * the same issue.. We solve that by only caring about one
848 * dent at a time and iteratively constructing the vfat long
849 * name.
850 */
Anssi Hannulaccea7332019-02-27 12:55:57 +0200851 ret = disk_read(sect, read_size, itr->block);
Rob Clark0c33fce2017-09-09 13:15:53 -0400852 if (ret < 0) {
853 debug("Error: reading block\n");
854 return NULL;
855 }
856
Anssi Hannulaccea7332019-02-27 12:55:57 +0200857 *nbytes = read_size * itr->fsdata->sect_size;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900858 itr->clust = itr->next_clust;
Rob Clark0c33fce2017-09-09 13:15:53 -0400859 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900860 itr->next_clust++;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200861 if (itr->next_clust * itr->fsdata->clust_size >=
Rob Clark0c33fce2017-09-09 13:15:53 -0400862 itr->fsdata->rootdir_size) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900863 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400864 itr->last_cluster = 1;
865 }
866 } else {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900867 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
868 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
869 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400870 itr->last_cluster = 1;
871 }
872 }
873
874 return itr->block;
875}
876
877static dir_entry *next_dent(fat_itr *itr)
878{
879 if (itr->remaining == 0) {
Anssi Hannulaccea7332019-02-27 12:55:57 +0200880 unsigned nbytes;
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100881 struct dir_entry *dent = fat_next_cluster(itr, &nbytes);
Rob Clark0c33fce2017-09-09 13:15:53 -0400882
883 /* have we reached the last cluster? */
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900884 if (!dent) {
885 /* a sign for no more entries left */
886 itr->dent = NULL;
Rob Clark0c33fce2017-09-09 13:15:53 -0400887 return NULL;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900888 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400889
890 itr->remaining = nbytes / sizeof(dir_entry) - 1;
891 itr->dent = dent;
892 } else {
893 itr->remaining--;
894 itr->dent++;
895 }
896
897 /* have we reached the last valid entry? */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100898 if (itr->dent->nameext.name[0] == 0)
Rob Clark0c33fce2017-09-09 13:15:53 -0400899 return NULL;
900
901 return itr->dent;
902}
903
904static dir_entry *extract_vfat_name(fat_itr *itr)
905{
906 struct dir_entry *dent = itr->dent;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100907 int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
Rob Clark0c33fce2017-09-09 13:15:53 -0400908 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
909 int n = 0;
910
911 while (seqn--) {
912 char buf[13];
913 int idx = 0;
914
915 slot2str((dir_slot *)dent, buf, &idx);
916
Patrick Wildtd6153f12018-11-26 15:58:13 +0100917 if (n + idx >= sizeof(itr->l_name))
918 return NULL;
919
Rob Clark0c33fce2017-09-09 13:15:53 -0400920 /* shift accumulated long-name up and copy new part in: */
921 memmove(itr->l_name + idx, itr->l_name, n);
922 memcpy(itr->l_name, buf, idx);
923 n += idx;
924
925 dent = next_dent(itr);
926 if (!dent)
927 return NULL;
928 }
929
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900930 /*
931 * We are now at the short file name entry.
932 * If it is marked as deleted, just skip it.
933 */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100934 if (dent->nameext.name[0] == DELETED_FLAG ||
935 dent->nameext.name[0] == aRING)
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900936 return NULL;
937
Rob Clark0c33fce2017-09-09 13:15:53 -0400938 itr->l_name[n] = '\0';
939
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100940 chksum = mkcksum(&dent->nameext);
Rob Clark0c33fce2017-09-09 13:15:53 -0400941
942 /* checksum mismatch could mean deleted file, etc.. skip it: */
943 if (chksum != alias_checksum) {
944 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100945 chksum, alias_checksum, itr->l_name, dent->nameext.name,
946 dent->nameext.ext);
Rob Clark0c33fce2017-09-09 13:15:53 -0400947 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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100959 * Return: boolean, 1 if success or 0 if no more entries in the
Rob Clark0c33fce2017-09-09 13:15:53 -0400960 * current directory
961 */
962static int fat_itr_next(fat_itr *itr)
963{
964 dir_entry *dent;
965
966 itr->name = NULL;
967
AKASHI Takahiro5f6ae542019-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 Clark0c33fce2017-09-09 13:15:53 -0400978 while (1) {
979 dent = next_dent(itr);
Heinrich Schuchardt9a4564e2020-11-19 07:44:08 +0100980 if (!dent) {
981 itr->dent_start = NULL;
Rob Clark0c33fce2017-09-09 13:15:53 -0400982 return 0;
Heinrich Schuchardt9a4564e2020-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 Schuchardt66ea58c2021-01-21 00:23:33 +0100987 if (dent->nameext.name[0] == DELETED_FLAG)
Rob Clark0c33fce2017-09-09 13:15:53 -0400988 continue;
989
990 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynena799dcd2018-01-05 02:45:21 +0200991 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100992 (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900993 /* long file name */
Rob Clark0c33fce2017-09-09 13:15:53 -0400994 dent = extract_vfat_name(itr);
AKASHI Takahiro5f6ae542019-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 Clark0c33fce2017-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 Gmeiner59e94402020-06-09 09:09:07 +02001012 }
Rob Clark0c33fce2017-09-09 13:15:53 -04001013
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +09001014 /* short file name */
Rob Clark0c33fce2017-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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001029 * Return: true if cursor is at a directory
Rob Clark0c33fce2017-09-09 13:15:53 -04001030 */
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001057 * Return: 0 on success or -errno
Rob Clark0c33fce2017-09-09 13:15:53 -04001058 */
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 Takahirof0a47812018-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 Takahirob88dff22018-09-11 15:59:00 +09001085 itr->next_clust = itr->fsdata->root_cluster;
Heinrich Schuchardt51d5b2a2020-11-22 16:04:47 +01001086 itr->start_clust = itr->fsdata->root_cluster;
AKASHI Takahirof0a47812018-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 Clark0c33fce2017-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ébaudeau0a120372012-07-20 15:18:44 +02001137int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001138{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001139 boot_sector bs;
1140 volume_info volinfo;
1141 int fatsize;
1142 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001143
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001144 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001145 printf("No current device\n");
1146 return 1;
1147 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001148
Simon Glassf5ac3032022-08-11 19:34:45 -06001149 if (blk_enabled()) {
Simon Glassfada3f92022-09-17 09:00:09 -06001150 printf("Interface: %s\n", blk_get_uclass_name(cur_dev->uclass_id));
Heinrich Schuchardt1c579db2021-01-25 12:53:14 +01001151 printf(" Device %d: ", cur_dev->devnum);
1152 dev_print(cur_dev);
wdenk2c9b05d2003-09-10 22:30:53 +00001153 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001154
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001155 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001156 printf("\nNo valid FAT fs found\n");
1157 return 1;
1158 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001159
1160 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001161 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001162 volinfo.fs_type[5] = '\0';
1163
Stephen Warrend398dbe2012-10-17 06:44:57 +00001164 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001165
wdenk2c9b05d2003-09-10 22:30:53 +00001166 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001167}
1168
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001169int fat_exists(const char *filename)
1170{
Rob Clark71193c92017-09-09 13:15:54 -04001171 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001172 fat_itr *itr;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001173 int ret;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001174
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001175 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001176 if (!itr)
1177 return 0;
Rob Clark71193c92017-09-09 13:15:54 -04001178 ret = fat_itr_root(itr, &fsdata);
1179 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001180 goto out;
Rob Clark71193c92017-09-09 13:15:54 -04001181
1182 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001183 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001184out:
Tom Rini5257a092017-09-22 07:37:43 -04001185 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001186 return ret == 0;
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001187}
1188
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +02001189/**
1190 * fat2rtc() - convert FAT time stamp to RTC file stamp
1191 *
1192 * @date: FAT date
1193 * @time: FAT time
1194 * @tm: RTC time stamp
1195 */
1196static void __maybe_unused fat2rtc(u16 date, u16 time, struct rtc_time *tm)
1197{
1198 tm->tm_mday = date & 0x1f;
1199 tm->tm_mon = (date & 0x1e0) >> 4;
1200 tm->tm_year = (date >> 9) + 1980;
1201
1202 tm->tm_sec = (time & 0x1f) << 1;
1203 tm->tm_min = (time & 0x7e0) >> 5;
1204 tm->tm_hour = time >> 11;
1205
1206 rtc_calc_weekday(tm);
1207 tm->tm_yday = 0;
1208 tm->tm_isdst = 0;
1209}
1210
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001211int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001212{
Rob Clark71193c92017-09-09 13:15:54 -04001213 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001214 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001215 int ret;
1216
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001217 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001218 if (!itr)
1219 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001220 ret = fat_itr_root(itr, &fsdata);
1221 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001222 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001223
1224 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1225 if (ret) {
1226 /*
1227 * Directories don't have size, but fs_size() is not
1228 * expected to fail if passed a directory path:
1229 */
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001230 free(fsdata.fatbuf);
Andrew F. Davis8a6c6122019-05-16 09:34:31 -05001231 ret = fat_itr_root(itr, &fsdata);
1232 if (ret)
1233 goto out_free_itr;
1234 ret = fat_itr_resolve(itr, filename, TYPE_DIR);
1235 if (!ret)
Rob Clark71193c92017-09-09 13:15:54 -04001236 *size = 0;
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001237 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001238 }
1239
1240 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001241out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001242 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001243out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001244 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001245 return ret;
Stephen Warren3eb58f52014-06-11 12:47:26 -06001246}
1247
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001248int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1249 loff_t *actread)
wdenk7a428cc2003-06-15 22:40:42 +00001250{
Rob Clark71193c92017-09-09 13:15:54 -04001251 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001252 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001253 int ret;
1254
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001255 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001256 if (!itr)
1257 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001258 ret = fat_itr_root(itr, &fsdata);
1259 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001260 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001261
1262 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1263 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001264 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001265
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001266 debug("reading %s at pos %llu\n", filename, offset);
Tien Fong Cheed16b79b2019-02-11 14:56:20 +08001267
1268 /* For saving default max clustersize memory allocated to malloc pool */
1269 dir_entry *dentptr = itr->dent;
1270
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001271 ret = get_contents(&fsdata, dentptr, offset, buf, len, actread);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001272
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001273out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001274 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001275out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001276 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001277 return ret;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001278}
1279
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001280int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001281{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001282 loff_t actread;
1283 int ret;
1284
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001285 ret = fat_read_file(filename, buffer, 0, maxsize, &actread);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001286 if (ret)
1287 return ret;
1288 else
1289 return actread;
wdenk7a428cc2003-06-15 22:40:42 +00001290}
Simon Glass19e38582012-12-26 09:53:33 +00001291
Rob Clark4174d7f2017-09-09 13:15:56 -04001292typedef struct {
1293 struct fs_dir_stream parent;
1294 struct fs_dirent dirent;
1295 fsdata fsdata;
1296 fat_itr itr;
1297} fat_dir;
1298
1299int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1300{
Neil Armstrong6f625f82017-11-24 09:54:41 +01001301 fat_dir *dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001302 int ret;
1303
Neil Armstrong6f625f82017-11-24 09:54:41 +01001304 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001305 if (!dir)
1306 return -ENOMEM;
Neil Armstrong6f625f82017-11-24 09:54:41 +01001307 memset(dir, 0, sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001308
1309 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1310 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001311 goto fail_free_dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001312
1313 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1314 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001315 goto fail_free_both;
Rob Clark4174d7f2017-09-09 13:15:56 -04001316
1317 *dirsp = (struct fs_dir_stream *)dir;
1318 return 0;
1319
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001320fail_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001321 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001322fail_free_dir:
Rob Clark4174d7f2017-09-09 13:15:56 -04001323 free(dir);
1324 return ret;
1325}
1326
1327int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1328{
1329 fat_dir *dir = (fat_dir *)dirs;
1330 struct fs_dirent *dent = &dir->dirent;
1331
1332 if (!fat_itr_next(&dir->itr))
1333 return -ENOENT;
1334
1335 memset(dent, 0, sizeof(*dent));
1336 strcpy(dent->name, dir->itr.name);
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +02001337 if (CONFIG_IS_ENABLED(EFI_LOADER)) {
1338 dent->attr = dir->itr.dent->attr;
1339 fat2rtc(le16_to_cpu(dir->itr.dent->cdate),
1340 le16_to_cpu(dir->itr.dent->ctime), &dent->create_time);
1341 fat2rtc(le16_to_cpu(dir->itr.dent->date),
1342 le16_to_cpu(dir->itr.dent->time), &dent->change_time);
1343 fat2rtc(le16_to_cpu(dir->itr.dent->adate),
1344 0, &dent->access_time);
1345 }
Rob Clark4174d7f2017-09-09 13:15:56 -04001346 if (fat_itr_isdir(&dir->itr)) {
1347 dent->type = FS_DT_DIR;
1348 } else {
1349 dent->type = FS_DT_REG;
1350 dent->size = FAT2CPU32(dir->itr.dent->size);
1351 }
1352
1353 *dentp = dent;
1354
1355 return 0;
1356}
1357
1358void fat_closedir(struct fs_dir_stream *dirs)
1359{
1360 fat_dir *dir = (fat_dir *)dirs;
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001361 free(dir->fsdata.fatbuf);
Rob Clark4174d7f2017-09-09 13:15:56 -04001362 free(dir);
1363}
1364
Simon Glass19e38582012-12-26 09:53:33 +00001365void fat_close(void)
1366{
1367}
Heinrich Schuchardtfac91832020-12-31 00:38:13 +01001368
1369int fat_uuid(char *uuid_str)
1370{
1371 boot_sector bs;
1372 volume_info volinfo;
1373 int fatsize;
1374 int ret;
1375 u8 *id;
1376
1377 ret = read_bootsectandvi(&bs, &volinfo, &fatsize);
1378 if (ret)
1379 return ret;
1380
1381 id = volinfo.volume_id;
1382 sprintf(uuid_str, "%02X%02X-%02X%02X", id[3], id[2], id[1], id[0]);
1383
1384 return 0;
1385}