blob: a3522340efc478c21d2cbfb716a55b3f69fcd2ba [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>
Christian Taedckedfe17362023-11-15 13:44:16 +010021#include <asm/unaligned.h>
wdenk2c9b05d2003-09-10 22:30:53 +000022#include <part.h>
Eric Nelson014705b2012-04-11 04:08:53 +000023#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060024#include <memalign.h>
Simon Glass274e0b02020-05-10 11:39:56 -060025#include <asm/cache.h>
Eric Nelson014705b2012-04-11 04:08:53 +000026#include <linux/compiler.h>
Richard Genoud2e372022012-12-13 00:47:36 +000027#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000028
wdenk7a428cc2003-06-15 22:40:42 +000029/*
Rob Clarkfd6c6b42017-09-09 13:15:59 -040030 * Convert a string to lowercase. Converts at most 'len' characters,
31 * 'len' may be larger than the length of 'str' if 'str' is NULL
32 * terminated.
wdenk7a428cc2003-06-15 22:40:42 +000033 */
Rob Clarkfd6c6b42017-09-09 13:15:59 -040034static void downcase(char *str, size_t len)
wdenk7a428cc2003-06-15 22:40:42 +000035{
Rob Clarkfd6c6b42017-09-09 13:15:59 -040036 while (*str != '\0' && len--) {
Richard Genoud2e372022012-12-13 00:47:36 +000037 *str = tolower(*str);
wdenk7a428cc2003-06-15 22:40:42 +000038 str++;
39 }
40}
41
Simon Glasse3394752016-02-29 15:25:34 -070042static struct blk_desc *cur_dev;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060043static struct disk_partition cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020044
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000045#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000046#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020047#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000048
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000049static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000050{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020051 ulong ret;
52
Simon Glass2ee8ada2016-02-29 15:25:52 -070053 if (!cur_dev)
wdenk2c9b05d2003-09-10 22:30:53 +000054 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020055
Simon Glass2ee8ada2016-02-29 15:25:52 -070056 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020057
Tom Rini240124c2017-08-14 21:02:08 -040058 if (ret != nr_blocks)
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020059 return -1;
60
61 return ret;
wdenk7a428cc2003-06-15 22:40:42 +000062}
63
Simon Glassc1c4a8f2020-05-10 11:39:57 -060064int fat_set_blk_dev(struct blk_desc *dev_desc, struct disk_partition *info)
wdenk7a428cc2003-06-15 22:40:42 +000065{
Eric Nelson014705b2012-04-11 04:08:53 +000066 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000067
Stephen Warren288904d2012-10-17 06:44:59 +000068 cur_dev = dev_desc;
69 cur_part_info = *info;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000070
71 /* Make sure it has a valid FAT header */
72 if (disk_read(0, 1, buffer) != 1) {
73 cur_dev = NULL;
74 return -1;
Wolfgang Denke78f2582007-08-07 16:02:13 +020075 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000076
77 /* Check if it's actually a DOS volume */
78 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
79 cur_dev = NULL;
80 return -1;
81 }
82
83 /* Check for FAT12/FAT16/FAT32 filesystem */
84 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
85 return 0;
86 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
87 return 0;
88
89 cur_dev = NULL;
90 return -1;
wdenk7a428cc2003-06-15 22:40:42 +000091}
92
Simon Glasse3394752016-02-29 15:25:34 -070093int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren288904d2012-10-17 06:44:59 +000094{
Simon Glassc1c4a8f2020-05-10 11:39:57 -060095 struct disk_partition info;
Stephen Warren288904d2012-10-17 06:44:59 +000096
97 /* First close any currently found FAT filesystem */
98 cur_dev = NULL;
99
100 /* Read the partition table, if present */
Simon Glassb89a8442016-02-29 15:25:48 -0700101 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren288904d2012-10-17 06:44:59 +0000102 if (part_no != 0) {
Simon Glass47459812023-07-15 21:39:06 -0600103 log_err("Partition %d invalid on device %d\n", part_no,
104 dev_desc->devnum);
Stephen Warren288904d2012-10-17 06:44:59 +0000105 return -1;
106 }
107
108 info.start = 0;
109 info.size = dev_desc->lba;
110 info.blksz = dev_desc->blksz;
111 info.name[0] = 0;
112 info.type[0] = 0;
113 info.bootable = 0;
Simon Glass23e34532023-08-24 13:55:31 -0600114 disk_partition_clr_uuid(&info);
Stephen Warren288904d2012-10-17 06:44:59 +0000115 }
116
117 return fat_set_blk_dev(dev_desc, &info);
118}
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000119
wdenk7a428cc2003-06-15 22:40:42 +0000120/*
wdenk7a428cc2003-06-15 22:40:42 +0000121 * Extract zero terminated short name from a directory entry.
122 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200123static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000124{
125 char *ptr;
126
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100127 memcpy(s_name, dirent->nameext.name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000128 s_name[8] = '\0';
129 ptr = s_name;
130 while (*ptr && *ptr != ' ')
131 ptr++;
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400132 if (dirent->lcase & CASE_LOWER_BASE)
133 downcase(s_name, (unsigned)(ptr - s_name));
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100134 if (dirent->nameext.ext[0] && dirent->nameext.ext[0] != ' ') {
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400135 *ptr++ = '.';
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100136 memcpy(ptr, dirent->nameext.ext, 3);
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400137 if (dirent->lcase & CASE_LOWER_EXT)
138 downcase(ptr, 3);
wdenk7a428cc2003-06-15 22:40:42 +0000139 ptr[3] = '\0';
140 while (*ptr && *ptr != ' ')
141 ptr++;
142 }
143 *ptr = '\0';
144 if (*s_name == DELETED_FLAG)
145 *s_name = '\0';
146 else if (*s_name == aRING)
Remy Bohmer7c8f2ce2008-11-27 22:30:27 +0100147 *s_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000148}
149
Stefan Brüns9f95d812016-12-17 00:27:51 +0100150static int flush_dirty_fat_buffer(fsdata *mydata);
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800151
152#if !CONFIG_IS_ENABLED(FAT_WRITE)
Stefan Brüns9f95d812016-12-17 00:27:51 +0100153/* Stub for read only operation */
154int flush_dirty_fat_buffer(fsdata *mydata)
155{
156 (void)(mydata);
157 return 0;
158}
159#endif
160
wdenk7a428cc2003-06-15 22:40:42 +0000161/*
162 * Get the entry at index 'entry' in a FAT (12/16/32) table.
163 * On failure 0x00 is returned.
164 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200165static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000166{
167 __u32 bufnum;
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000168 __u32 offset, off8;
wdenk7a428cc2003-06-15 22:40:42 +0000169 __u32 ret = 0x00;
170
Stefan Brüns9f95d812016-12-17 00:27:51 +0100171 if (CHECK_CLUST(entry, mydata->fatsize)) {
Simon Glass47459812023-07-15 21:39:06 -0600172 log_err("Invalid FAT entry: %#08x\n", entry);
Stefan Brüns9f95d812016-12-17 00:27:51 +0100173 return ret;
174 }
175
wdenk7a428cc2003-06-15 22:40:42 +0000176 switch (mydata->fatsize) {
177 case 32:
178 bufnum = entry / FAT32BUFSIZE;
179 offset = entry - bufnum * FAT32BUFSIZE;
180 break;
181 case 16:
182 bufnum = entry / FAT16BUFSIZE;
183 offset = entry - bufnum * FAT16BUFSIZE;
184 break;
185 case 12:
186 bufnum = entry / FAT12BUFSIZE;
187 offset = entry - bufnum * FAT12BUFSIZE;
188 break;
189
190 default:
191 /* Unsupported FAT size */
192 return ret;
193 }
194
Stefan Brüns9f95d812016-12-17 00:27:51 +0100195 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200196 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200197
wdenk7a428cc2003-06-15 22:40:42 +0000198 /* Read a new block of FAT entries into the cache. */
199 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000200 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000201 __u8 *bufptr = mydata->fatbuf;
202 __u32 fatlength = mydata->fatlength;
203 __u32 startblock = bufnum * FATBUFBLOCKS;
204
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100205 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200206 if (startblock + getsize > fatlength)
207 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000208
wdenk7a428cc2003-06-15 22:40:42 +0000209 startblock += mydata->fat_sect; /* Offset from start of disk */
210
Stefan Brüns9f95d812016-12-17 00:27:51 +0100211 /* Write back the fatbuf to the disk */
212 if (flush_dirty_fat_buffer(mydata) < 0)
213 return -1;
214
wdenk7a428cc2003-06-15 22:40:42 +0000215 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200216 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000217 return ret;
218 }
219 mydata->fatbufnum = bufnum;
220 }
221
222 /* Get the actual entry from the table */
223 switch (mydata->fatsize) {
224 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200225 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000226 break;
227 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200228 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000229 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200230 case 12:
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000231 off8 = (offset * 3) / 2;
232 /* fatbut + off8 may be unaligned, read in byte granularity */
233 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk7a428cc2003-06-15 22:40:42 +0000234
Stefan Brüns15f40632016-12-17 03:55:10 +0100235 if (offset & 0x1)
236 ret >>= 4;
237 ret &= 0xfff;
wdenk7a428cc2003-06-15 22:40:42 +0000238 }
Stefan Brüns9f95d812016-12-17 00:27:51 +0100239 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
240 mydata->fatsize, ret, entry, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000241
242 return ret;
243}
244
wdenk7a428cc2003-06-15 22:40:42 +0000245/*
246 * Read at most 'size' bytes from the specified cluster into 'buffer'.
247 * Return 0 on success, -1 otherwise.
248 */
249static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200250get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000251{
wdenk7a428cc2003-06-15 22:40:42 +0000252 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000253 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000254
255 if (clustnum > 0) {
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400256 startsect = clust_to_sect(mydata, clustnum);
wdenk7a428cc2003-06-15 22:40:42 +0000257 } else {
258 startsect = mydata->rootdir_sect;
259 }
260
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200261 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
262
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200263 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000264 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200265
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200266 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaue957c072012-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 }
Ricardo Salveti8fb445a2021-09-26 21:36:04 +0300279 } else if (size >= mydata->sect_size) {
280 __u32 bytes_read;
281 __u32 sect_count = size / mydata->sect_size;
Heinrich Schuchardteb3587b2021-01-26 00:14:14 +0100282
Ricardo Salveti8fb445a2021-09-26 21:36:04 +0300283 ret = disk_read(startsect, sect_count, buffer);
284 if (ret != sect_count) {
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200285 debug("Error reading data (got %d)\n", ret);
286 return -1;
287 }
Ricardo Salveti8fb445a2021-09-26 21:36:04 +0300288 bytes_read = sect_count * mydata->sect_size;
289 startsect += sect_count;
290 buffer += bytes_read;
291 size -= bytes_read;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200292 }
293 if (size) {
294 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
295
296 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000297 if (ret != 1) {
298 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000299 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000300 }
wdenk2c9b05d2003-09-10 22:30:53 +0000301
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200302 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000303 }
304
305 return 0;
306}
307
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200308/**
309 * get_contents() - read from file
310 *
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000311 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200312 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
313 * fatal errors.
314 *
315 * @mydata: file system description
316 * @dentprt: directory entry pointer
317 * @pos: position from where to read
318 * @buffer: buffer into which to read
319 * @maxsize: maximum number of bytes to read
320 * @gotsize: number of bytes actually read
321 * Return: -1 on error, otherwise 0
wdenk7a428cc2003-06-15 22:40:42 +0000322 */
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800323static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
324 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk7a428cc2003-06-15 22:40:42 +0000325{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800326 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000327 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000328 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000329 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800330 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000331
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800332 *gotsize = 0;
333 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000334
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000335 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800336 debug("Read position past EOF: %llu\n", pos);
337 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000338 }
339
340 if (maxsize > 0 && filesize > pos + maxsize)
341 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000342
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800343 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000344
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200345 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000346
347 /* go to cluster at pos */
348 while (actsize <= pos) {
349 curclust = get_fatent(mydata, curclust);
350 if (CHECK_CLUST(curclust, mydata->fatsize)) {
351 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200352 printf("Invalid FAT entry\n");
353 return -1;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000354 }
355 actsize += bytesperclust;
356 }
357
358 /* actsize > pos */
359 actsize -= bytesperclust;
360 filesize -= actsize;
361 pos -= actsize;
362
363 /* align to beginning of next cluster if any */
364 if (pos) {
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800365 __u8 *tmp_buffer;
366
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800367 actsize = min(filesize, (loff_t)bytesperclust);
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800368 tmp_buffer = malloc_cache_aligned(actsize);
369 if (!tmp_buffer) {
370 debug("Error: allocating buffer\n");
Heinrich Schuchardtd37d47c2019-09-12 19:19:30 +0200371 return -1;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800372 }
373
374 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000375 printf("Error reading cluster\n");
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800376 free(tmp_buffer);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000377 return -1;
378 }
379 filesize -= actsize;
380 actsize -= pos;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800381 memcpy(buffer, tmp_buffer + pos, actsize);
382 free(tmp_buffer);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800383 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000384 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800385 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000386 buffer += actsize;
387
388 curclust = get_fatent(mydata, curclust);
389 if (CHECK_CLUST(curclust, mydata->fatsize)) {
390 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200391 printf("Invalid FAT entry\n");
392 return -1;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000393 }
394 }
395
396 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200397 endclust = curclust;
398
wdenk7a428cc2003-06-15 22:40:42 +0000399 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000400 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200401 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000402 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200403 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000404 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100405 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200406 debug("curclust: 0x%x\n", newclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200407 printf("Invalid FAT entry\n");
408 return -1;
wdenk2c9b05d2003-09-10 22:30:53 +0000409 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200410 endclust = newclust;
411 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000412 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200413
wdenk2c9b05d2003-09-10 22:30:53 +0000414 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200415 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200416 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200417 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000418 return -1;
419 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800420 *gotsize += actsize;
421 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000422getit:
423 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200424 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000425 return -1;
426 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800427 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000428 filesize -= actsize;
429 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200430
wdenk2c9b05d2003-09-10 22:30:53 +0000431 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100432 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200433 debug("curclust: 0x%x\n", curclust);
434 printf("Invalid FAT entry\n");
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200435 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000436 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200437 actsize = bytesperclust;
438 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000439 } while (1);
440}
441
wdenk7a428cc2003-06-15 22:40:42 +0000442/*
443 * Extract the file name information from 'slotptr' into 'l_name',
444 * starting at l_name[*idx].
445 * Return 1 if terminator (zero byte) is found, 0 otherwise.
446 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200447static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000448{
449 int j;
450
451 for (j = 0; j <= 8; j += 2) {
452 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200453 if (l_name[*idx] == 0x00)
454 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000455 (*idx)++;
456 }
457 for (j = 0; j <= 10; j += 2) {
458 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200459 if (l_name[*idx] == 0x00)
460 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000461 (*idx)++;
462 }
463 for (j = 0; j <= 2; j += 2) {
464 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200465 if (l_name[*idx] == 0x00)
466 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000467 (*idx)++;
468 }
469
470 return 0;
471}
472
wdenk7a428cc2003-06-15 22:40:42 +0000473/* Calculate short name checksum */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100474static __u8 mkcksum(struct nameext *nameext)
wdenk7a428cc2003-06-15 22:40:42 +0000475{
476 int i;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100477 u8 *pos = (void *)nameext;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200478
wdenk7a428cc2003-06-15 22:40:42 +0000479 __u8 ret = 0;
480
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100481 for (i = 0; i < 11; i++)
482 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + pos[i];
wdenk7a428cc2003-06-15 22:40:42 +0000483
484 return ret;
485}
wdenk7a428cc2003-06-15 22:40:42 +0000486
487/*
wdenk7a428cc2003-06-15 22:40:42 +0000488 * Read boot sector and volume info from a FAT filesystem
489 */
490static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200491read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000492{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000493 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000494 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000495 int ret = 0;
496
497 if (cur_dev == NULL) {
498 debug("Error: no device selected\n");
499 return -1;
500 }
501
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300502 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000503 if (block == NULL) {
504 debug("Error: allocating block\n");
505 return -1;
506 }
wdenk7a428cc2003-06-15 22:40:42 +0000507
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200508 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200509 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000510 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000511 }
512
513 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200514 bs->reserved = FAT2CPU16(bs->reserved);
515 bs->fat_length = FAT2CPU16(bs->fat_length);
516 bs->secs_track = FAT2CPU16(bs->secs_track);
517 bs->heads = FAT2CPU16(bs->heads);
518 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000519
520 /* FAT32 entries */
521 if (bs->fat_length == 0) {
522 /* Assume FAT32 */
523 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200524 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000525 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200526 bs->info_sector = FAT2CPU16(bs->info_sector);
527 bs->backup_boot = FAT2CPU16(bs->backup_boot);
528 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000529 *fatsize = 32;
530 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200531 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000532 *fatsize = 0;
533 }
534 memcpy(volinfo, vistart, sizeof(volume_info));
535
wdenk7a428cc2003-06-15 22:40:42 +0000536 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200537 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000538 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000539 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500540 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000541 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000542 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000543 }
Tom Rix155abaa2009-05-20 07:55:41 -0500544 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000545 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000546 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000547 }
548 }
549
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200550 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000551fail:
552 ret = -1;
553exit:
554 free(block);
555 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000556}
557
Rob Clark4996a5f2017-09-09 13:15:52 -0400558static int get_fs_info(fsdata *mydata)
wdenk7a428cc2003-06-15 22:40:42 +0000559{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200560 boot_sector bs;
561 volume_info volinfo;
Rob Clark4996a5f2017-09-09 13:15:52 -0400562 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000563
Rob Clark4996a5f2017-09-09 13:15:52 -0400564 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
565 if (ret) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200566 debug("Error: reading boot sector\n");
Rob Clark4996a5f2017-09-09 13:15:52 -0400567 return ret;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200568 }
569
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000570 if (mydata->fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200571 mydata->fatlength = bs.fat32_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900572 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000573 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200574 mydata->fatlength = bs.fat_length;
Christian Taedckedfe17362023-11-15 13:44:16 +0100575 mydata->total_sect = get_unaligned_le16(bs.sectors);
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900576 if (!mydata->total_sect)
577 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000578 }
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900579 if (!mydata->total_sect) /* unlikely */
580 mydata->total_sect = (u32)cur_part_info.size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200581
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900582 mydata->fats = bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200583 mydata->fat_sect = bs.reserved;
584
Rob Clark4996a5f2017-09-09 13:15:52 -0400585 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200586
Christian Taedckedfe17362023-11-15 13:44:16 +0100587 mydata->sect_size = get_unaligned_le16(bs.sector_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200588 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000589 if (mydata->sect_size != cur_part_info.blksz) {
Simon Glass47459812023-07-15 21:39:06 -0600590 log_err("FAT sector size mismatch (fs=%u, dev=%lu)\n",
591 mydata->sect_size, cur_part_info.blksz);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000592 return -1;
593 }
Patrick Wildt1d614b92018-11-26 15:56:57 +0100594 if (mydata->clust_size == 0) {
Simon Glass47459812023-07-15 21:39:06 -0600595 log_err("FAT cluster size not set\n");
Patrick Wildt1d614b92018-11-26 15:56:57 +0100596 return -1;
597 }
598 if ((unsigned int)mydata->clust_size * mydata->sect_size >
599 MAX_CLUSTSIZE) {
Simon Glass47459812023-07-15 21:39:06 -0600600 log_err("FAT cluster size too big (cs=%u, max=%u)\n",
601 (uint)mydata->clust_size * mydata->sect_size,
602 MAX_CLUSTSIZE);
Patrick Wildt1d614b92018-11-26 15:56:57 +0100603 return -1;
604 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200605
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200606 if (mydata->fatsize == 32) {
607 mydata->data_begin = mydata->rootdir_sect -
608 (mydata->clust_size * 2);
Rob Clark0c33fce2017-09-09 13:15:53 -0400609 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200610 } else {
Christian Taedckedfe17362023-11-15 13:44:16 +0100611 mydata->rootdir_size = (get_unaligned_le16(bs.dir_entries) *
Rob Clark4996a5f2017-09-09 13:15:52 -0400612 sizeof(dir_entry)) /
613 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200614 mydata->data_begin = mydata->rootdir_sect +
Rob Clark4996a5f2017-09-09 13:15:52 -0400615 mydata->rootdir_size -
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200616 (mydata->clust_size * 2);
Anssi Hannulaccea7332019-02-27 12:55:57 +0200617
618 /*
619 * The root directory is not cluster-aligned and may be on a
620 * "negative" cluster, this will be handled specially in
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100621 * fat_next_cluster().
Anssi Hannulaccea7332019-02-27 12:55:57 +0200622 */
623 mydata->root_cluster = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200624 }
625
626 mydata->fatbufnum = -1;
Stefan Brüns751b31d2016-09-11 22:51:40 +0200627 mydata->fat_dirty = 0;
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300628 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000629 if (mydata->fatbuf == NULL) {
630 debug("Error: allocating memory\n");
631 return -1;
632 }
wdenk7a428cc2003-06-15 22:40:42 +0000633
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200634 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
635 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
636 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
637 "Data begins at: %d\n",
Rob Clark0c33fce2017-09-09 13:15:53 -0400638 mydata->root_cluster,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200639 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000640 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
641 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
642 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000643
Rob Clark4996a5f2017-09-09 13:15:52 -0400644 return 0;
645}
646
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100647/**
648 * struct fat_itr - directory iterator, to simplify filesystem traversal
Rob Clark0c33fce2017-09-09 13:15:53 -0400649 *
650 * Implements an iterator pattern to traverse directory tables,
651 * transparently handling directory tables split across multiple
652 * clusters, and the difference between FAT12/FAT16 root directory
653 * (contiguous) and subdirectories + FAT32 root (chained).
654 *
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100655 * Rough usage
656 *
657 * .. code-block:: c
Rob Clark0c33fce2017-09-09 13:15:53 -0400658 *
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100659 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
660 * // to traverse down to a subdirectory pointed to by
661 * // current iterator position:
662 * fat_itr_child(&itr, &itr);
663 * }
Rob Clark0c33fce2017-09-09 13:15:53 -0400664 *
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100665 * For a more complete example, see fat_itr_resolve().
Rob Clark0c33fce2017-09-09 13:15:53 -0400666 */
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100667struct fat_itr {
668 /**
669 * @fsdata: filesystem parameters
670 */
671 fsdata *fsdata;
672 /**
673 * @start_clust: first cluster
674 */
675 unsigned int start_clust;
676 /**
677 * @clust: current cluster
678 */
679 unsigned int clust;
680 /**
681 * @next_clust: next cluster if remaining == 0
682 */
683 unsigned int next_clust;
684 /**
685 * @last_cluster: set if last cluster of directory reached
686 */
687 int last_cluster;
688 /**
689 * @is_root: is iterator at root directory
690 */
691 int is_root;
692 /**
693 * @remaining: remaining directory entries in current cluster
694 */
695 int remaining;
696 /**
697 * @dent: current directory entry
698 */
699 dir_entry *dent;
700 /**
Heinrich Schuchardt9a4564e2020-11-19 07:44:08 +0100701 * @dent_rem: remaining entries after long name start
702 */
703 int dent_rem;
704 /**
705 * @dent_clust: cluster of long name start
706 */
707 unsigned int dent_clust;
708 /**
709 * @dent_start: first directory entry for long name
710 */
711 dir_entry *dent_start;
712 /**
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100713 * @l_name: long name of current directory entry
714 */
715 char l_name[VFAT_MAXLEN_BYTES];
716 /**
717 * @s_name: short 8.3 name of current directory entry
718 */
719 char s_name[14];
720 /**
721 * @name: l_name if there is one, else s_name
722 */
723 char *name;
724 /**
725 * @block: buffer for current cluster
726 */
727 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
728};
Rob Clark0c33fce2017-09-09 13:15:53 -0400729
730static int fat_itr_isdir(fat_itr *itr);
731
732/**
733 * fat_itr_root() - initialize an iterator to start at the root
734 * directory
735 *
736 * @itr: iterator to initialize
737 * @fsdata: filesystem data for the partition
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100738 * Return: 0 on success, else -errno
Rob Clark0c33fce2017-09-09 13:15:53 -0400739 */
740static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
741{
742 if (get_fs_info(fsdata))
743 return -ENXIO;
744
745 itr->fsdata = fsdata;
Heinrich Schuchardt51d5b2a2020-11-22 16:04:47 +0100746 itr->start_clust = fsdata->root_cluster;
Rob Clark0c33fce2017-09-09 13:15:53 -0400747 itr->clust = fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900748 itr->next_clust = fsdata->root_cluster;
Rob Clark0c33fce2017-09-09 13:15:53 -0400749 itr->dent = NULL;
750 itr->remaining = 0;
751 itr->last_cluster = 0;
752 itr->is_root = 1;
753
754 return 0;
755}
756
757/**
758 * fat_itr_child() - initialize an iterator to descend into a sub-
759 * directory
760 *
761 * Initializes 'itr' to iterate the contents of the directory at
762 * the current cursor position of 'parent'. It is an error to
763 * call this if the current cursor of 'parent' is pointing at a
764 * regular file.
765 *
766 * Note that 'itr' and 'parent' can be the same pointer if you do
767 * not need to preserve 'parent' after this call, which is useful
768 * for traversing directory structure to resolve a file/directory.
769 *
770 * @itr: iterator to initialize
771 * @parent: the iterator pointing at a directory entry in the
772 * parent directory of the directory to iterate
773 */
774static void fat_itr_child(fat_itr *itr, fat_itr *parent)
775{
776 fsdata *mydata = parent->fsdata; /* for silly macros */
777 unsigned clustnum = START(parent->dent);
778
779 assert(fat_itr_isdir(parent));
780
781 itr->fsdata = parent->fsdata;
AKASHI Takahirof0926ae2018-09-11 15:59:09 +0900782 itr->start_clust = clustnum;
Rob Clark0c33fce2017-09-09 13:15:53 -0400783 if (clustnum > 0) {
784 itr->clust = clustnum;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900785 itr->next_clust = clustnum;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300786 itr->is_root = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400787 } else {
788 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900789 itr->next_clust = parent->fsdata->root_cluster;
Heinrich Schuchardt51d5b2a2020-11-22 16:04:47 +0100790 itr->start_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300791 itr->is_root = 1;
Rob Clark0c33fce2017-09-09 13:15:53 -0400792 }
793 itr->dent = NULL;
794 itr->remaining = 0;
795 itr->last_cluster = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400796}
797
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100798/**
799 * fat_next_cluster() - load next FAT cluster
800 *
801 * The function is used when iterating through directories. It loads the
802 * next cluster with directory entries
803 *
804 * @itr: directory iterator
805 * @nbytes: number of bytes read, 0 on error
806 * Return: first directory entry, NULL on error
807 */
808void *fat_next_cluster(fat_itr *itr, unsigned int *nbytes)
Rob Clark0c33fce2017-09-09 13:15:53 -0400809{
Rob Clark0c33fce2017-09-09 13:15:53 -0400810 int ret;
811 u32 sect;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200812 u32 read_size;
Rob Clark0c33fce2017-09-09 13:15:53 -0400813
814 /* have we reached the end? */
815 if (itr->last_cluster)
816 return NULL;
817
Anssi Hannulaccea7332019-02-27 12:55:57 +0200818 if (itr->is_root && itr->fsdata->fatsize != 32) {
819 /*
820 * The root directory is located before the data area and
821 * cannot be indexed using the regular unsigned cluster
822 * numbers (it may start at a "negative" cluster or not at a
823 * cluster boundary at all), so consider itr->next_clust to be
824 * a offset in cluster-sized units from the start of rootdir.
825 */
826 unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
827 unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
828 sect = itr->fsdata->rootdir_sect + sect_offset;
829 /* do not read past the end of rootdir */
830 read_size = min_t(u32, itr->fsdata->clust_size,
831 remaining_sects);
832 } else {
833 sect = clust_to_sect(itr->fsdata, itr->next_clust);
834 read_size = itr->fsdata->clust_size;
835 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400836
Heinrich Schuchardtbb093932020-12-25 14:30:04 +0100837 log_debug("FAT read(sect=%d), clust_size=%d, read_size=%u\n",
838 sect, itr->fsdata->clust_size, read_size);
Rob Clark0c33fce2017-09-09 13:15:53 -0400839
840 /*
841 * NOTE: do_fat_read_at() had complicated logic to deal w/
842 * vfat names that span multiple clusters in the fat16 case,
843 * which get_dentfromdir() probably also needed (and was
844 * missing). And not entirely sure what fat32 didn't have
845 * the same issue.. We solve that by only caring about one
846 * dent at a time and iteratively constructing the vfat long
847 * name.
848 */
Anssi Hannulaccea7332019-02-27 12:55:57 +0200849 ret = disk_read(sect, read_size, itr->block);
Rob Clark0c33fce2017-09-09 13:15:53 -0400850 if (ret < 0) {
851 debug("Error: reading block\n");
852 return NULL;
853 }
854
Anssi Hannulaccea7332019-02-27 12:55:57 +0200855 *nbytes = read_size * itr->fsdata->sect_size;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900856 itr->clust = itr->next_clust;
Rob Clark0c33fce2017-09-09 13:15:53 -0400857 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900858 itr->next_clust++;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200859 if (itr->next_clust * itr->fsdata->clust_size >=
Rob Clark0c33fce2017-09-09 13:15:53 -0400860 itr->fsdata->rootdir_size) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900861 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400862 itr->last_cluster = 1;
863 }
864 } else {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900865 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
866 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
867 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400868 itr->last_cluster = 1;
869 }
870 }
871
872 return itr->block;
873}
874
875static dir_entry *next_dent(fat_itr *itr)
876{
877 if (itr->remaining == 0) {
Anssi Hannulaccea7332019-02-27 12:55:57 +0200878 unsigned nbytes;
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100879 struct dir_entry *dent = fat_next_cluster(itr, &nbytes);
Rob Clark0c33fce2017-09-09 13:15:53 -0400880
881 /* have we reached the last cluster? */
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900882 if (!dent) {
883 /* a sign for no more entries left */
884 itr->dent = NULL;
Rob Clark0c33fce2017-09-09 13:15:53 -0400885 return NULL;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900886 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400887
888 itr->remaining = nbytes / sizeof(dir_entry) - 1;
889 itr->dent = dent;
890 } else {
891 itr->remaining--;
892 itr->dent++;
893 }
894
895 /* have we reached the last valid entry? */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100896 if (itr->dent->nameext.name[0] == 0)
Rob Clark0c33fce2017-09-09 13:15:53 -0400897 return NULL;
898
899 return itr->dent;
900}
901
902static dir_entry *extract_vfat_name(fat_itr *itr)
903{
904 struct dir_entry *dent = itr->dent;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100905 int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
Rob Clark0c33fce2017-09-09 13:15:53 -0400906 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
907 int n = 0;
908
909 while (seqn--) {
910 char buf[13];
911 int idx = 0;
912
913 slot2str((dir_slot *)dent, buf, &idx);
914
Patrick Wildtd6153f12018-11-26 15:58:13 +0100915 if (n + idx >= sizeof(itr->l_name))
916 return NULL;
917
Rob Clark0c33fce2017-09-09 13:15:53 -0400918 /* shift accumulated long-name up and copy new part in: */
919 memmove(itr->l_name + idx, itr->l_name, n);
920 memcpy(itr->l_name, buf, idx);
921 n += idx;
922
923 dent = next_dent(itr);
924 if (!dent)
925 return NULL;
926 }
927
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900928 /*
929 * We are now at the short file name entry.
930 * If it is marked as deleted, just skip it.
931 */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100932 if (dent->nameext.name[0] == DELETED_FLAG ||
933 dent->nameext.name[0] == aRING)
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900934 return NULL;
935
Rob Clark0c33fce2017-09-09 13:15:53 -0400936 itr->l_name[n] = '\0';
937
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100938 chksum = mkcksum(&dent->nameext);
Rob Clark0c33fce2017-09-09 13:15:53 -0400939
940 /* checksum mismatch could mean deleted file, etc.. skip it: */
941 if (chksum != alias_checksum) {
942 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100943 chksum, alias_checksum, itr->l_name, dent->nameext.name,
944 dent->nameext.ext);
Rob Clark0c33fce2017-09-09 13:15:53 -0400945 return NULL;
946 }
947
948 return dent;
949}
950
951/**
952 * fat_itr_next() - step to the next entry in a directory
953 *
954 * Must be called once on a new iterator before the cursor is valid.
955 *
956 * @itr: the iterator to iterate
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100957 * Return: boolean, 1 if success or 0 if no more entries in the
Rob Clark0c33fce2017-09-09 13:15:53 -0400958 * current directory
959 */
960static int fat_itr_next(fat_itr *itr)
961{
962 dir_entry *dent;
963
964 itr->name = NULL;
965
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900966 /*
967 * One logical directory entry consist of following slots:
968 * name[0] Attributes
969 * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT
970 * ...
971 * dent[N - 2]: LFN[1] 2 ATTR_VFAT
972 * dent[N - 1]: LFN[0] 1 ATTR_VFAT
973 * dent[N]: SFN ATTR_ARCH
974 */
975
Rob Clark0c33fce2017-09-09 13:15:53 -0400976 while (1) {
977 dent = next_dent(itr);
Heinrich Schuchardt9a4564e2020-11-19 07:44:08 +0100978 if (!dent) {
979 itr->dent_start = NULL;
Rob Clark0c33fce2017-09-09 13:15:53 -0400980 return 0;
Heinrich Schuchardt9a4564e2020-11-19 07:44:08 +0100981 }
982 itr->dent_rem = itr->remaining;
983 itr->dent_start = itr->dent;
984 itr->dent_clust = itr->clust;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100985 if (dent->nameext.name[0] == DELETED_FLAG)
Rob Clark0c33fce2017-09-09 13:15:53 -0400986 continue;
987
988 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynena799dcd2018-01-05 02:45:21 +0200989 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100990 (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900991 /* long file name */
Rob Clark0c33fce2017-09-09 13:15:53 -0400992 dent = extract_vfat_name(itr);
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900993 /*
994 * If succeeded, dent has a valid short file
995 * name entry for the current entry.
996 * If failed, itr points to a current bogus
997 * entry. So after fetching a next one,
998 * it may have a short file name entry
999 * for this bogus entry so that we can still
1000 * check for a short name.
1001 */
Rob Clark0c33fce2017-09-09 13:15:53 -04001002 if (!dent)
1003 continue;
1004 itr->name = itr->l_name;
1005 break;
1006 } else {
1007 /* Volume label or VFAT entry, skip */
1008 continue;
1009 }
Christian Gmeiner59e94402020-06-09 09:09:07 +02001010 }
Rob Clark0c33fce2017-09-09 13:15:53 -04001011
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +09001012 /* short file name */
Rob Clark0c33fce2017-09-09 13:15:53 -04001013 break;
1014 }
1015
1016 get_name(dent, itr->s_name);
1017 if (!itr->name)
1018 itr->name = itr->s_name;
1019
1020 return 1;
1021}
1022
1023/**
1024 * fat_itr_isdir() - is current cursor position pointing to a directory
1025 *
1026 * @itr: the iterator
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001027 * Return: true if cursor is at a directory
Rob Clark0c33fce2017-09-09 13:15:53 -04001028 */
1029static int fat_itr_isdir(fat_itr *itr)
1030{
1031 return !!(itr->dent->attr & ATTR_DIR);
1032}
1033
1034/*
1035 * Helpers:
1036 */
1037
1038#define TYPE_FILE 0x1
1039#define TYPE_DIR 0x2
1040#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
1041
1042/**
1043 * fat_itr_resolve() - traverse directory structure to resolve the
1044 * requested path.
1045 *
1046 * Traverse directory structure to the requested path. If the specified
1047 * path is to a directory, this will descend into the directory and
1048 * leave it iterator at the start of the directory. If the path is to a
1049 * file, it will leave the iterator in the parent directory with current
1050 * cursor at file's entry in the directory.
1051 *
1052 * @itr: iterator initialized to root
1053 * @path: the requested path
1054 * @type: bitmask of allowable file types
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001055 * Return: 0 on success or -errno
Rob Clark0c33fce2017-09-09 13:15:53 -04001056 */
1057static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
1058{
1059 const char *next;
1060
1061 /* chomp any extra leading slashes: */
1062 while (path[0] && ISDIRDELIM(path[0]))
1063 path++;
1064
1065 /* are we at the end? */
1066 if (strlen(path) == 0) {
1067 if (!(type & TYPE_DIR))
1068 return -ENOENT;
1069 return 0;
1070 }
1071
1072 /* find length of next path entry: */
1073 next = path;
1074 while (next[0] && !ISDIRDELIM(next[0]))
1075 next++;
1076
AKASHI Takahirof0a47812018-09-11 15:58:59 +09001077 if (itr->is_root) {
1078 /* root dir doesn't have "." nor ".." */
1079 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
1080 (((next - path) == 2) && !strncmp(path, "..", 2))) {
1081 /* point back to itself */
1082 itr->clust = itr->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +09001083 itr->next_clust = itr->fsdata->root_cluster;
Heinrich Schuchardt51d5b2a2020-11-22 16:04:47 +01001084 itr->start_clust = itr->fsdata->root_cluster;
AKASHI Takahirof0a47812018-09-11 15:58:59 +09001085 itr->dent = NULL;
1086 itr->remaining = 0;
1087 itr->last_cluster = 0;
1088
1089 if (next[0] == 0) {
1090 if (type & TYPE_DIR)
1091 return 0;
1092 else
1093 return -ENOENT;
1094 }
1095
1096 return fat_itr_resolve(itr, next, type);
1097 }
1098 }
1099
Rob Clark0c33fce2017-09-09 13:15:53 -04001100 while (fat_itr_next(itr)) {
1101 int match = 0;
1102 unsigned n = max(strlen(itr->name), (size_t)(next - path));
1103
1104 /* check both long and short name: */
1105 if (!strncasecmp(path, itr->name, n))
1106 match = 1;
1107 else if (itr->name != itr->s_name &&
1108 !strncasecmp(path, itr->s_name, n))
1109 match = 1;
1110
1111 if (!match)
1112 continue;
1113
1114 if (fat_itr_isdir(itr)) {
1115 /* recurse into directory: */
1116 fat_itr_child(itr, itr);
1117 return fat_itr_resolve(itr, next, type);
1118 } else if (next[0]) {
1119 /*
1120 * If next is not empty then we have a case
1121 * like: /path/to/realfile/nonsense
1122 */
1123 debug("bad trailing path: %s\n", next);
1124 return -ENOENT;
1125 } else if (!(type & TYPE_FILE)) {
1126 return -ENOTDIR;
1127 } else {
1128 return 0;
1129 }
1130 }
1131
1132 return -ENOENT;
1133}
1134
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001135int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001136{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001137 boot_sector bs;
1138 volume_info volinfo;
1139 int fatsize;
1140 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001141
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001142 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001143 printf("No current device\n");
1144 return 1;
1145 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001146
Simon Glassf5ac3032022-08-11 19:34:45 -06001147 if (blk_enabled()) {
Simon Glassfada3f92022-09-17 09:00:09 -06001148 printf("Interface: %s\n", blk_get_uclass_name(cur_dev->uclass_id));
Heinrich Schuchardt1c579db2021-01-25 12:53:14 +01001149 printf(" Device %d: ", cur_dev->devnum);
1150 dev_print(cur_dev);
wdenk2c9b05d2003-09-10 22:30:53 +00001151 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001152
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001153 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001154 printf("\nNo valid FAT fs found\n");
1155 return 1;
1156 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001157
1158 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001159 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001160 volinfo.fs_type[5] = '\0';
1161
Stephen Warrend398dbe2012-10-17 06:44:57 +00001162 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001163
wdenk2c9b05d2003-09-10 22:30:53 +00001164 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001165}
1166
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001167int fat_exists(const char *filename)
1168{
Rob Clark71193c92017-09-09 13:15:54 -04001169 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001170 fat_itr *itr;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001171 int ret;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001172
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001173 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001174 if (!itr)
1175 return 0;
Rob Clark71193c92017-09-09 13:15:54 -04001176 ret = fat_itr_root(itr, &fsdata);
1177 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001178 goto out;
Rob Clark71193c92017-09-09 13:15:54 -04001179
1180 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001181 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001182out:
Tom Rini5257a092017-09-22 07:37:43 -04001183 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001184 return ret == 0;
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001185}
1186
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +02001187/**
1188 * fat2rtc() - convert FAT time stamp to RTC file stamp
1189 *
1190 * @date: FAT date
1191 * @time: FAT time
1192 * @tm: RTC time stamp
1193 */
1194static void __maybe_unused fat2rtc(u16 date, u16 time, struct rtc_time *tm)
1195{
1196 tm->tm_mday = date & 0x1f;
1197 tm->tm_mon = (date & 0x1e0) >> 4;
1198 tm->tm_year = (date >> 9) + 1980;
1199
1200 tm->tm_sec = (time & 0x1f) << 1;
1201 tm->tm_min = (time & 0x7e0) >> 5;
1202 tm->tm_hour = time >> 11;
1203
1204 rtc_calc_weekday(tm);
1205 tm->tm_yday = 0;
1206 tm->tm_isdst = 0;
1207}
1208
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001209int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001210{
Rob Clark71193c92017-09-09 13:15:54 -04001211 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001212 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001213 int ret;
1214
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001215 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001216 if (!itr)
1217 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001218 ret = fat_itr_root(itr, &fsdata);
1219 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001220 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001221
1222 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1223 if (ret) {
1224 /*
1225 * Directories don't have size, but fs_size() is not
1226 * expected to fail if passed a directory path:
1227 */
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001228 free(fsdata.fatbuf);
Andrew F. Davis8a6c6122019-05-16 09:34:31 -05001229 ret = fat_itr_root(itr, &fsdata);
1230 if (ret)
1231 goto out_free_itr;
1232 ret = fat_itr_resolve(itr, filename, TYPE_DIR);
1233 if (!ret)
Rob Clark71193c92017-09-09 13:15:54 -04001234 *size = 0;
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001235 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001236 }
1237
1238 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001239out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001240 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001241out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001242 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001243 return ret;
Stephen Warren3eb58f52014-06-11 12:47:26 -06001244}
1245
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001246int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1247 loff_t *actread)
wdenk7a428cc2003-06-15 22:40:42 +00001248{
Rob Clark71193c92017-09-09 13:15:54 -04001249 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001250 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001251 int ret;
1252
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001253 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001254 if (!itr)
1255 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001256 ret = fat_itr_root(itr, &fsdata);
1257 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001258 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001259
1260 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1261 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001262 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001263
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001264 debug("reading %s at pos %llu\n", filename, offset);
Tien Fong Cheed16b79b2019-02-11 14:56:20 +08001265
1266 /* For saving default max clustersize memory allocated to malloc pool */
1267 dir_entry *dentptr = itr->dent;
1268
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001269 ret = get_contents(&fsdata, dentptr, offset, buf, len, actread);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001270
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001271out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001272 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001273out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001274 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001275 return ret;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001276}
1277
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001278int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001279{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001280 loff_t actread;
1281 int ret;
1282
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001283 ret = fat_read_file(filename, buffer, 0, maxsize, &actread);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001284 if (ret)
1285 return ret;
1286 else
1287 return actread;
wdenk7a428cc2003-06-15 22:40:42 +00001288}
Simon Glass19e38582012-12-26 09:53:33 +00001289
Rob Clark4174d7f2017-09-09 13:15:56 -04001290typedef struct {
1291 struct fs_dir_stream parent;
1292 struct fs_dirent dirent;
1293 fsdata fsdata;
1294 fat_itr itr;
1295} fat_dir;
1296
1297int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1298{
Neil Armstrong6f625f82017-11-24 09:54:41 +01001299 fat_dir *dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001300 int ret;
1301
Neil Armstrong6f625f82017-11-24 09:54:41 +01001302 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001303 if (!dir)
1304 return -ENOMEM;
Neil Armstrong6f625f82017-11-24 09:54:41 +01001305 memset(dir, 0, sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001306
1307 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1308 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001309 goto fail_free_dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001310
1311 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1312 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001313 goto fail_free_both;
Rob Clark4174d7f2017-09-09 13:15:56 -04001314
1315 *dirsp = (struct fs_dir_stream *)dir;
1316 return 0;
1317
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001318fail_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001319 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001320fail_free_dir:
Rob Clark4174d7f2017-09-09 13:15:56 -04001321 free(dir);
1322 return ret;
1323}
1324
1325int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1326{
1327 fat_dir *dir = (fat_dir *)dirs;
1328 struct fs_dirent *dent = &dir->dirent;
1329
1330 if (!fat_itr_next(&dir->itr))
1331 return -ENOENT;
1332
1333 memset(dent, 0, sizeof(*dent));
1334 strcpy(dent->name, dir->itr.name);
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +02001335 if (CONFIG_IS_ENABLED(EFI_LOADER)) {
1336 dent->attr = dir->itr.dent->attr;
1337 fat2rtc(le16_to_cpu(dir->itr.dent->cdate),
1338 le16_to_cpu(dir->itr.dent->ctime), &dent->create_time);
1339 fat2rtc(le16_to_cpu(dir->itr.dent->date),
1340 le16_to_cpu(dir->itr.dent->time), &dent->change_time);
1341 fat2rtc(le16_to_cpu(dir->itr.dent->adate),
1342 0, &dent->access_time);
1343 }
Rob Clark4174d7f2017-09-09 13:15:56 -04001344 if (fat_itr_isdir(&dir->itr)) {
1345 dent->type = FS_DT_DIR;
1346 } else {
1347 dent->type = FS_DT_REG;
1348 dent->size = FAT2CPU32(dir->itr.dent->size);
1349 }
1350
1351 *dentp = dent;
1352
1353 return 0;
1354}
1355
1356void fat_closedir(struct fs_dir_stream *dirs)
1357{
1358 fat_dir *dir = (fat_dir *)dirs;
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001359 free(dir->fsdata.fatbuf);
Rob Clark4174d7f2017-09-09 13:15:56 -04001360 free(dir);
1361}
1362
Simon Glass19e38582012-12-26 09:53:33 +00001363void fat_close(void)
1364{
1365}
Heinrich Schuchardtfac91832020-12-31 00:38:13 +01001366
1367int fat_uuid(char *uuid_str)
1368{
1369 boot_sector bs;
1370 volume_info volinfo;
1371 int fatsize;
1372 int ret;
1373 u8 *id;
1374
1375 ret = read_bootsectandvi(&bs, &volinfo, &fatsize);
1376 if (ret)
1377 return ret;
1378
1379 id = volinfo.volume_id;
1380 sprintf(uuid_str, "%02X%02X-%02X%02X", id[3], id[2], id[1], id[0]);
1381
1382 return 0;
1383}