blob: 77f225ccd8d0d9565b7c7359f12c70bf84499a90 [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
Christian Taedckecd7a1a02023-11-15 13:44:18 +010029/* maximum number of clusters for FAT12 */
30#define MAX_FAT12 0xFF4
31
wdenk7a428cc2003-06-15 22:40:42 +000032/*
Rob Clarkfd6c6b42017-09-09 13:15:59 -040033 * Convert a string to lowercase. Converts at most 'len' characters,
34 * 'len' may be larger than the length of 'str' if 'str' is NULL
35 * terminated.
wdenk7a428cc2003-06-15 22:40:42 +000036 */
Rob Clarkfd6c6b42017-09-09 13:15:59 -040037static void downcase(char *str, size_t len)
wdenk7a428cc2003-06-15 22:40:42 +000038{
Rob Clarkfd6c6b42017-09-09 13:15:59 -040039 while (*str != '\0' && len--) {
Richard Genoud2e372022012-12-13 00:47:36 +000040 *str = tolower(*str);
wdenk7a428cc2003-06-15 22:40:42 +000041 str++;
42 }
43}
44
Simon Glasse3394752016-02-29 15:25:34 -070045static struct blk_desc *cur_dev;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060046static struct disk_partition cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020047
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000048#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000049#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020050#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000051
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000052static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000053{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020054 ulong ret;
55
Simon Glass2ee8ada2016-02-29 15:25:52 -070056 if (!cur_dev)
wdenk2c9b05d2003-09-10 22:30:53 +000057 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020058
Simon Glass2ee8ada2016-02-29 15:25:52 -070059 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020060
Tom Rini240124c2017-08-14 21:02:08 -040061 if (ret != nr_blocks)
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020062 return -1;
63
64 return ret;
wdenk7a428cc2003-06-15 22:40:42 +000065}
66
Simon Glassc1c4a8f2020-05-10 11:39:57 -060067int fat_set_blk_dev(struct blk_desc *dev_desc, struct disk_partition *info)
wdenk7a428cc2003-06-15 22:40:42 +000068{
Eric Nelson014705b2012-04-11 04:08:53 +000069 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000070
Stephen Warren288904d2012-10-17 06:44:59 +000071 cur_dev = dev_desc;
72 cur_part_info = *info;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000073
74 /* Make sure it has a valid FAT header */
75 if (disk_read(0, 1, buffer) != 1) {
76 cur_dev = NULL;
77 return -1;
Wolfgang Denke78f2582007-08-07 16:02:13 +020078 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000079
80 /* Check if it's actually a DOS volume */
81 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
82 cur_dev = NULL;
83 return -1;
84 }
85
86 /* Check for FAT12/FAT16/FAT32 filesystem */
87 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
88 return 0;
89 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
90 return 0;
91
92 cur_dev = NULL;
93 return -1;
wdenk7a428cc2003-06-15 22:40:42 +000094}
95
Simon Glasse3394752016-02-29 15:25:34 -070096int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren288904d2012-10-17 06:44:59 +000097{
Simon Glassc1c4a8f2020-05-10 11:39:57 -060098 struct disk_partition info;
Stephen Warren288904d2012-10-17 06:44:59 +000099
100 /* First close any currently found FAT filesystem */
101 cur_dev = NULL;
102
103 /* Read the partition table, if present */
Simon Glassb89a8442016-02-29 15:25:48 -0700104 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren288904d2012-10-17 06:44:59 +0000105 if (part_no != 0) {
Simon Glass47459812023-07-15 21:39:06 -0600106 log_err("Partition %d invalid on device %d\n", part_no,
107 dev_desc->devnum);
Stephen Warren288904d2012-10-17 06:44:59 +0000108 return -1;
109 }
110
111 info.start = 0;
112 info.size = dev_desc->lba;
113 info.blksz = dev_desc->blksz;
114 info.name[0] = 0;
115 info.type[0] = 0;
116 info.bootable = 0;
Simon Glass23e34532023-08-24 13:55:31 -0600117 disk_partition_clr_uuid(&info);
Stephen Warren288904d2012-10-17 06:44:59 +0000118 }
119
120 return fat_set_blk_dev(dev_desc, &info);
121}
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000122
wdenk7a428cc2003-06-15 22:40:42 +0000123/*
wdenk7a428cc2003-06-15 22:40:42 +0000124 * Extract zero terminated short name from a directory entry.
125 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200126static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000127{
128 char *ptr;
129
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100130 memcpy(s_name, dirent->nameext.name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000131 s_name[8] = '\0';
132 ptr = s_name;
133 while (*ptr && *ptr != ' ')
134 ptr++;
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400135 if (dirent->lcase & CASE_LOWER_BASE)
136 downcase(s_name, (unsigned)(ptr - s_name));
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100137 if (dirent->nameext.ext[0] && dirent->nameext.ext[0] != ' ') {
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400138 *ptr++ = '.';
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100139 memcpy(ptr, dirent->nameext.ext, 3);
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400140 if (dirent->lcase & CASE_LOWER_EXT)
141 downcase(ptr, 3);
wdenk7a428cc2003-06-15 22:40:42 +0000142 ptr[3] = '\0';
143 while (*ptr && *ptr != ' ')
144 ptr++;
145 }
146 *ptr = '\0';
147 if (*s_name == DELETED_FLAG)
148 *s_name = '\0';
149 else if (*s_name == aRING)
Remy Bohmer7c8f2ce2008-11-27 22:30:27 +0100150 *s_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000151}
152
Stefan Brüns9f95d812016-12-17 00:27:51 +0100153static int flush_dirty_fat_buffer(fsdata *mydata);
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800154
155#if !CONFIG_IS_ENABLED(FAT_WRITE)
Stefan Brüns9f95d812016-12-17 00:27:51 +0100156/* Stub for read only operation */
157int flush_dirty_fat_buffer(fsdata *mydata)
158{
159 (void)(mydata);
160 return 0;
161}
162#endif
163
wdenk7a428cc2003-06-15 22:40:42 +0000164/*
165 * Get the entry at index 'entry' in a FAT (12/16/32) table.
166 * On failure 0x00 is returned.
167 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200168static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000169{
170 __u32 bufnum;
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000171 __u32 offset, off8;
wdenk7a428cc2003-06-15 22:40:42 +0000172 __u32 ret = 0x00;
173
Stefan Brüns9f95d812016-12-17 00:27:51 +0100174 if (CHECK_CLUST(entry, mydata->fatsize)) {
Simon Glass47459812023-07-15 21:39:06 -0600175 log_err("Invalid FAT entry: %#08x\n", entry);
Stefan Brüns9f95d812016-12-17 00:27:51 +0100176 return ret;
177 }
178
wdenk7a428cc2003-06-15 22:40:42 +0000179 switch (mydata->fatsize) {
180 case 32:
181 bufnum = entry / FAT32BUFSIZE;
182 offset = entry - bufnum * FAT32BUFSIZE;
183 break;
184 case 16:
185 bufnum = entry / FAT16BUFSIZE;
186 offset = entry - bufnum * FAT16BUFSIZE;
187 break;
188 case 12:
189 bufnum = entry / FAT12BUFSIZE;
190 offset = entry - bufnum * FAT12BUFSIZE;
191 break;
192
193 default:
194 /* Unsupported FAT size */
195 return ret;
196 }
197
Stefan Brüns9f95d812016-12-17 00:27:51 +0100198 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200199 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200200
wdenk7a428cc2003-06-15 22:40:42 +0000201 /* Read a new block of FAT entries into the cache. */
202 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000203 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000204 __u8 *bufptr = mydata->fatbuf;
205 __u32 fatlength = mydata->fatlength;
206 __u32 startblock = bufnum * FATBUFBLOCKS;
207
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100208 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200209 if (startblock + getsize > fatlength)
210 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000211
wdenk7a428cc2003-06-15 22:40:42 +0000212 startblock += mydata->fat_sect; /* Offset from start of disk */
213
Stefan Brüns9f95d812016-12-17 00:27:51 +0100214 /* Write back the fatbuf to the disk */
215 if (flush_dirty_fat_buffer(mydata) < 0)
216 return -1;
217
wdenk7a428cc2003-06-15 22:40:42 +0000218 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200219 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000220 return ret;
221 }
222 mydata->fatbufnum = bufnum;
223 }
224
225 /* Get the actual entry from the table */
226 switch (mydata->fatsize) {
227 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200228 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000229 break;
230 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200231 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000232 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200233 case 12:
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000234 off8 = (offset * 3) / 2;
235 /* fatbut + off8 may be unaligned, read in byte granularity */
236 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk7a428cc2003-06-15 22:40:42 +0000237
Stefan Brüns15f40632016-12-17 03:55:10 +0100238 if (offset & 0x1)
239 ret >>= 4;
240 ret &= 0xfff;
wdenk7a428cc2003-06-15 22:40:42 +0000241 }
Stefan Brüns9f95d812016-12-17 00:27:51 +0100242 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
243 mydata->fatsize, ret, entry, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000244
245 return ret;
246}
247
wdenk7a428cc2003-06-15 22:40:42 +0000248/*
249 * Read at most 'size' bytes from the specified cluster into 'buffer'.
250 * Return 0 on success, -1 otherwise.
251 */
252static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200253get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000254{
wdenk7a428cc2003-06-15 22:40:42 +0000255 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000256 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000257
258 if (clustnum > 0) {
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400259 startsect = clust_to_sect(mydata, clustnum);
wdenk7a428cc2003-06-15 22:40:42 +0000260 } else {
261 startsect = mydata->rootdir_sect;
262 }
263
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200264 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
265
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200266 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000267 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200268
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200269 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200270
271 while (size >= mydata->sect_size) {
272 ret = disk_read(startsect++, 1, tmpbuf);
273 if (ret != 1) {
274 debug("Error reading data (got %d)\n", ret);
275 return -1;
276 }
277
278 memcpy(buffer, tmpbuf, mydata->sect_size);
279 buffer += mydata->sect_size;
280 size -= mydata->sect_size;
281 }
Ricardo Salveti8fb445a2021-09-26 21:36:04 +0300282 } else if (size >= mydata->sect_size) {
283 __u32 bytes_read;
284 __u32 sect_count = size / mydata->sect_size;
Heinrich Schuchardteb3587b2021-01-26 00:14:14 +0100285
Ricardo Salveti8fb445a2021-09-26 21:36:04 +0300286 ret = disk_read(startsect, sect_count, buffer);
287 if (ret != sect_count) {
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200288 debug("Error reading data (got %d)\n", ret);
289 return -1;
290 }
Ricardo Salveti8fb445a2021-09-26 21:36:04 +0300291 bytes_read = sect_count * mydata->sect_size;
292 startsect += sect_count;
293 buffer += bytes_read;
294 size -= bytes_read;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200295 }
296 if (size) {
297 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
298
299 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000300 if (ret != 1) {
301 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000302 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000303 }
wdenk2c9b05d2003-09-10 22:30:53 +0000304
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200305 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000306 }
307
308 return 0;
309}
310
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200311/**
312 * get_contents() - read from file
313 *
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000314 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200315 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
316 * fatal errors.
317 *
318 * @mydata: file system description
319 * @dentprt: directory entry pointer
320 * @pos: position from where to read
321 * @buffer: buffer into which to read
322 * @maxsize: maximum number of bytes to read
323 * @gotsize: number of bytes actually read
324 * Return: -1 on error, otherwise 0
wdenk7a428cc2003-06-15 22:40:42 +0000325 */
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800326static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
327 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk7a428cc2003-06-15 22:40:42 +0000328{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800329 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000330 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000331 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000332 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800333 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000334
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800335 *gotsize = 0;
336 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000337
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000338 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800339 debug("Read position past EOF: %llu\n", pos);
340 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000341 }
342
343 if (maxsize > 0 && filesize > pos + maxsize)
344 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000345
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800346 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000347
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200348 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000349
350 /* go to cluster at pos */
351 while (actsize <= pos) {
352 curclust = get_fatent(mydata, curclust);
353 if (CHECK_CLUST(curclust, mydata->fatsize)) {
354 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200355 printf("Invalid FAT entry\n");
356 return -1;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000357 }
358 actsize += bytesperclust;
359 }
360
361 /* actsize > pos */
362 actsize -= bytesperclust;
363 filesize -= actsize;
364 pos -= actsize;
365
366 /* align to beginning of next cluster if any */
367 if (pos) {
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800368 __u8 *tmp_buffer;
369
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800370 actsize = min(filesize, (loff_t)bytesperclust);
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800371 tmp_buffer = malloc_cache_aligned(actsize);
372 if (!tmp_buffer) {
373 debug("Error: allocating buffer\n");
Heinrich Schuchardtd37d47c2019-09-12 19:19:30 +0200374 return -1;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800375 }
376
377 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000378 printf("Error reading cluster\n");
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800379 free(tmp_buffer);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000380 return -1;
381 }
382 filesize -= actsize;
383 actsize -= pos;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800384 memcpy(buffer, tmp_buffer + pos, actsize);
385 free(tmp_buffer);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800386 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000387 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800388 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000389 buffer += actsize;
390
391 curclust = get_fatent(mydata, curclust);
392 if (CHECK_CLUST(curclust, mydata->fatsize)) {
393 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200394 printf("Invalid FAT entry\n");
395 return -1;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000396 }
397 }
398
399 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200400 endclust = curclust;
401
wdenk7a428cc2003-06-15 22:40:42 +0000402 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000403 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200404 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000405 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200406 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000407 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100408 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200409 debug("curclust: 0x%x\n", newclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200410 printf("Invalid FAT entry\n");
411 return -1;
wdenk2c9b05d2003-09-10 22:30:53 +0000412 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200413 endclust = newclust;
414 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000415 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200416
wdenk2c9b05d2003-09-10 22:30:53 +0000417 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200418 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200419 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200420 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000421 return -1;
422 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800423 *gotsize += actsize;
424 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000425getit:
426 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200427 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000428 return -1;
429 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800430 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000431 filesize -= actsize;
432 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200433
wdenk2c9b05d2003-09-10 22:30:53 +0000434 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100435 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200436 debug("curclust: 0x%x\n", curclust);
437 printf("Invalid FAT entry\n");
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200438 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000439 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200440 actsize = bytesperclust;
441 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000442 } while (1);
443}
444
wdenk7a428cc2003-06-15 22:40:42 +0000445/*
446 * Extract the file name information from 'slotptr' into 'l_name',
447 * starting at l_name[*idx].
448 * Return 1 if terminator (zero byte) is found, 0 otherwise.
449 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200450static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000451{
452 int j;
453
454 for (j = 0; j <= 8; j += 2) {
455 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200456 if (l_name[*idx] == 0x00)
457 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000458 (*idx)++;
459 }
460 for (j = 0; j <= 10; j += 2) {
461 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200462 if (l_name[*idx] == 0x00)
463 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000464 (*idx)++;
465 }
466 for (j = 0; j <= 2; j += 2) {
467 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200468 if (l_name[*idx] == 0x00)
469 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000470 (*idx)++;
471 }
472
473 return 0;
474}
475
wdenk7a428cc2003-06-15 22:40:42 +0000476/* Calculate short name checksum */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100477static __u8 mkcksum(struct nameext *nameext)
wdenk7a428cc2003-06-15 22:40:42 +0000478{
479 int i;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100480 u8 *pos = (void *)nameext;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200481
wdenk7a428cc2003-06-15 22:40:42 +0000482 __u8 ret = 0;
483
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100484 for (i = 0; i < 11; i++)
485 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + pos[i];
wdenk7a428cc2003-06-15 22:40:42 +0000486
487 return ret;
488}
wdenk7a428cc2003-06-15 22:40:42 +0000489
490/*
Christian Taedckecd7a1a02023-11-15 13:44:18 +0100491 * Determine if the FAT type is FAT12 or FAT16
492 *
493 * Based on fat_fill_super() from the Linux kernel's fs/fat/inode.c
494 */
495static int determine_legacy_fat_bits(const boot_sector *bs)
496{
497 u16 fat_start = bs->reserved;
498 u32 dir_start = fat_start + bs->fats * bs->fat_length;
499 u32 rootdir_sectors = get_unaligned_le16(bs->dir_entries) *
500 sizeof(dir_entry) /
501 get_unaligned_le16(bs->sector_size);
502 u32 data_start = dir_start + rootdir_sectors;
503 u16 sectors = get_unaligned_le16(bs->sectors);
504 u32 total_sectors = sectors ? sectors : bs->total_sect;
505 u32 total_clusters = (total_sectors - data_start) /
506 bs->cluster_size;
507
508 return (total_clusters > MAX_FAT12) ? 16 : 12;
509}
510
511/*
wdenk7a428cc2003-06-15 22:40:42 +0000512 * Read boot sector and volume info from a FAT filesystem
513 */
514static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200515read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000516{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000517 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000518 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000519 int ret = 0;
520
521 if (cur_dev == NULL) {
522 debug("Error: no device selected\n");
523 return -1;
524 }
525
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300526 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000527 if (block == NULL) {
528 debug("Error: allocating block\n");
529 return -1;
530 }
wdenk7a428cc2003-06-15 22:40:42 +0000531
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200532 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200533 debug("Error: reading block\n");
Christian Taedckeab985e82023-11-15 13:44:19 +0100534 ret = -1;
535 goto out_free;
wdenk7a428cc2003-06-15 22:40:42 +0000536 }
537
538 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200539 bs->reserved = FAT2CPU16(bs->reserved);
540 bs->fat_length = FAT2CPU16(bs->fat_length);
541 bs->secs_track = FAT2CPU16(bs->secs_track);
542 bs->heads = FAT2CPU16(bs->heads);
543 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000544
545 /* FAT32 entries */
Christian Taedckecd7a1a02023-11-15 13:44:18 +0100546 if (!bs->fat_length && bs->fat32_length) {
wdenk7a428cc2003-06-15 22:40:42 +0000547 /* Assume FAT32 */
548 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200549 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000550 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200551 bs->info_sector = FAT2CPU16(bs->info_sector);
552 bs->backup_boot = FAT2CPU16(bs->backup_boot);
553 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000554 *fatsize = 32;
555 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200556 vistart = (volume_info *)&(bs->fat32_length);
Christian Taedckecd7a1a02023-11-15 13:44:18 +0100557 *fatsize = determine_legacy_fat_bits(bs);
wdenk7a428cc2003-06-15 22:40:42 +0000558 }
559 memcpy(volinfo, vistart, sizeof(volume_info));
Christian Taedckeab985e82023-11-15 13:44:19 +0100560
561out_free:
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000562 free(block);
563 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000564}
565
Rob Clark4996a5f2017-09-09 13:15:52 -0400566static int get_fs_info(fsdata *mydata)
wdenk7a428cc2003-06-15 22:40:42 +0000567{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200568 boot_sector bs;
569 volume_info volinfo;
Rob Clark4996a5f2017-09-09 13:15:52 -0400570 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000571
Rob Clark4996a5f2017-09-09 13:15:52 -0400572 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
573 if (ret) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200574 debug("Error: reading boot sector\n");
Rob Clark4996a5f2017-09-09 13:15:52 -0400575 return ret;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200576 }
577
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000578 if (mydata->fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200579 mydata->fatlength = bs.fat32_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900580 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000581 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200582 mydata->fatlength = bs.fat_length;
Christian Taedckedfe17362023-11-15 13:44:16 +0100583 mydata->total_sect = get_unaligned_le16(bs.sectors);
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900584 if (!mydata->total_sect)
585 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000586 }
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900587 if (!mydata->total_sect) /* unlikely */
588 mydata->total_sect = (u32)cur_part_info.size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200589
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900590 mydata->fats = bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200591 mydata->fat_sect = bs.reserved;
592
Rob Clark4996a5f2017-09-09 13:15:52 -0400593 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200594
Christian Taedckedfe17362023-11-15 13:44:16 +0100595 mydata->sect_size = get_unaligned_le16(bs.sector_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200596 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000597 if (mydata->sect_size != cur_part_info.blksz) {
Simon Glass47459812023-07-15 21:39:06 -0600598 log_err("FAT sector size mismatch (fs=%u, dev=%lu)\n",
599 mydata->sect_size, cur_part_info.blksz);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000600 return -1;
601 }
Patrick Wildt1d614b92018-11-26 15:56:57 +0100602 if (mydata->clust_size == 0) {
Simon Glass47459812023-07-15 21:39:06 -0600603 log_err("FAT cluster size not set\n");
Patrick Wildt1d614b92018-11-26 15:56:57 +0100604 return -1;
605 }
606 if ((unsigned int)mydata->clust_size * mydata->sect_size >
607 MAX_CLUSTSIZE) {
Simon Glass47459812023-07-15 21:39:06 -0600608 log_err("FAT cluster size too big (cs=%u, max=%u)\n",
609 (uint)mydata->clust_size * mydata->sect_size,
610 MAX_CLUSTSIZE);
Patrick Wildt1d614b92018-11-26 15:56:57 +0100611 return -1;
612 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200613
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200614 if (mydata->fatsize == 32) {
615 mydata->data_begin = mydata->rootdir_sect -
616 (mydata->clust_size * 2);
Rob Clark0c33fce2017-09-09 13:15:53 -0400617 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200618 } else {
Christian Taedckedfe17362023-11-15 13:44:16 +0100619 mydata->rootdir_size = (get_unaligned_le16(bs.dir_entries) *
Rob Clark4996a5f2017-09-09 13:15:52 -0400620 sizeof(dir_entry)) /
621 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200622 mydata->data_begin = mydata->rootdir_sect +
Rob Clark4996a5f2017-09-09 13:15:52 -0400623 mydata->rootdir_size -
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200624 (mydata->clust_size * 2);
Anssi Hannulaccea7332019-02-27 12:55:57 +0200625
626 /*
627 * The root directory is not cluster-aligned and may be on a
628 * "negative" cluster, this will be handled specially in
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100629 * fat_next_cluster().
Anssi Hannulaccea7332019-02-27 12:55:57 +0200630 */
631 mydata->root_cluster = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200632 }
633
634 mydata->fatbufnum = -1;
Stefan Brüns751b31d2016-09-11 22:51:40 +0200635 mydata->fat_dirty = 0;
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300636 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000637 if (mydata->fatbuf == NULL) {
638 debug("Error: allocating memory\n");
639 return -1;
640 }
wdenk7a428cc2003-06-15 22:40:42 +0000641
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200642 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
643 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
644 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
645 "Data begins at: %d\n",
Rob Clark0c33fce2017-09-09 13:15:53 -0400646 mydata->root_cluster,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200647 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000648 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
649 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
650 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000651
Rob Clark4996a5f2017-09-09 13:15:52 -0400652 return 0;
653}
654
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100655/**
656 * struct fat_itr - directory iterator, to simplify filesystem traversal
Rob Clark0c33fce2017-09-09 13:15:53 -0400657 *
658 * Implements an iterator pattern to traverse directory tables,
659 * transparently handling directory tables split across multiple
660 * clusters, and the difference between FAT12/FAT16 root directory
661 * (contiguous) and subdirectories + FAT32 root (chained).
662 *
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100663 * Rough usage
664 *
665 * .. code-block:: c
Rob Clark0c33fce2017-09-09 13:15:53 -0400666 *
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100667 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
668 * // to traverse down to a subdirectory pointed to by
669 * // current iterator position:
670 * fat_itr_child(&itr, &itr);
671 * }
Rob Clark0c33fce2017-09-09 13:15:53 -0400672 *
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100673 * For a more complete example, see fat_itr_resolve().
Rob Clark0c33fce2017-09-09 13:15:53 -0400674 */
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100675struct fat_itr {
676 /**
677 * @fsdata: filesystem parameters
678 */
679 fsdata *fsdata;
680 /**
681 * @start_clust: first cluster
682 */
683 unsigned int start_clust;
684 /**
685 * @clust: current cluster
686 */
687 unsigned int clust;
688 /**
689 * @next_clust: next cluster if remaining == 0
690 */
691 unsigned int next_clust;
692 /**
693 * @last_cluster: set if last cluster of directory reached
694 */
695 int last_cluster;
696 /**
697 * @is_root: is iterator at root directory
698 */
699 int is_root;
700 /**
701 * @remaining: remaining directory entries in current cluster
702 */
703 int remaining;
704 /**
705 * @dent: current directory entry
706 */
707 dir_entry *dent;
708 /**
Heinrich Schuchardt9a4564e2020-11-19 07:44:08 +0100709 * @dent_rem: remaining entries after long name start
710 */
711 int dent_rem;
712 /**
713 * @dent_clust: cluster of long name start
714 */
715 unsigned int dent_clust;
716 /**
717 * @dent_start: first directory entry for long name
718 */
719 dir_entry *dent_start;
720 /**
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100721 * @l_name: long name of current directory entry
722 */
723 char l_name[VFAT_MAXLEN_BYTES];
724 /**
725 * @s_name: short 8.3 name of current directory entry
726 */
727 char s_name[14];
728 /**
729 * @name: l_name if there is one, else s_name
730 */
731 char *name;
732 /**
733 * @block: buffer for current cluster
734 */
735 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
736};
Rob Clark0c33fce2017-09-09 13:15:53 -0400737
738static int fat_itr_isdir(fat_itr *itr);
739
740/**
741 * fat_itr_root() - initialize an iterator to start at the root
742 * directory
743 *
744 * @itr: iterator to initialize
745 * @fsdata: filesystem data for the partition
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100746 * Return: 0 on success, else -errno
Rob Clark0c33fce2017-09-09 13:15:53 -0400747 */
748static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
749{
750 if (get_fs_info(fsdata))
751 return -ENXIO;
752
753 itr->fsdata = fsdata;
Heinrich Schuchardt51d5b2a2020-11-22 16:04:47 +0100754 itr->start_clust = fsdata->root_cluster;
Rob Clark0c33fce2017-09-09 13:15:53 -0400755 itr->clust = fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900756 itr->next_clust = fsdata->root_cluster;
Rob Clark0c33fce2017-09-09 13:15:53 -0400757 itr->dent = NULL;
758 itr->remaining = 0;
759 itr->last_cluster = 0;
760 itr->is_root = 1;
761
762 return 0;
763}
764
765/**
766 * fat_itr_child() - initialize an iterator to descend into a sub-
767 * directory
768 *
769 * Initializes 'itr' to iterate the contents of the directory at
770 * the current cursor position of 'parent'. It is an error to
771 * call this if the current cursor of 'parent' is pointing at a
772 * regular file.
773 *
774 * Note that 'itr' and 'parent' can be the same pointer if you do
775 * not need to preserve 'parent' after this call, which is useful
776 * for traversing directory structure to resolve a file/directory.
777 *
778 * @itr: iterator to initialize
779 * @parent: the iterator pointing at a directory entry in the
780 * parent directory of the directory to iterate
781 */
782static void fat_itr_child(fat_itr *itr, fat_itr *parent)
783{
784 fsdata *mydata = parent->fsdata; /* for silly macros */
785 unsigned clustnum = START(parent->dent);
786
787 assert(fat_itr_isdir(parent));
788
789 itr->fsdata = parent->fsdata;
AKASHI Takahirof0926ae2018-09-11 15:59:09 +0900790 itr->start_clust = clustnum;
Rob Clark0c33fce2017-09-09 13:15:53 -0400791 if (clustnum > 0) {
792 itr->clust = clustnum;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900793 itr->next_clust = clustnum;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300794 itr->is_root = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400795 } else {
796 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900797 itr->next_clust = parent->fsdata->root_cluster;
Heinrich Schuchardt51d5b2a2020-11-22 16:04:47 +0100798 itr->start_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300799 itr->is_root = 1;
Rob Clark0c33fce2017-09-09 13:15:53 -0400800 }
801 itr->dent = NULL;
802 itr->remaining = 0;
803 itr->last_cluster = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400804}
805
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100806/**
807 * fat_next_cluster() - load next FAT cluster
808 *
809 * The function is used when iterating through directories. It loads the
810 * next cluster with directory entries
811 *
812 * @itr: directory iterator
813 * @nbytes: number of bytes read, 0 on error
814 * Return: first directory entry, NULL on error
815 */
816void *fat_next_cluster(fat_itr *itr, unsigned int *nbytes)
Rob Clark0c33fce2017-09-09 13:15:53 -0400817{
Rob Clark0c33fce2017-09-09 13:15:53 -0400818 int ret;
819 u32 sect;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200820 u32 read_size;
Rob Clark0c33fce2017-09-09 13:15:53 -0400821
822 /* have we reached the end? */
823 if (itr->last_cluster)
824 return NULL;
825
Anssi Hannulaccea7332019-02-27 12:55:57 +0200826 if (itr->is_root && itr->fsdata->fatsize != 32) {
827 /*
828 * The root directory is located before the data area and
829 * cannot be indexed using the regular unsigned cluster
830 * numbers (it may start at a "negative" cluster or not at a
831 * cluster boundary at all), so consider itr->next_clust to be
832 * a offset in cluster-sized units from the start of rootdir.
833 */
834 unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
835 unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
836 sect = itr->fsdata->rootdir_sect + sect_offset;
837 /* do not read past the end of rootdir */
838 read_size = min_t(u32, itr->fsdata->clust_size,
839 remaining_sects);
840 } else {
841 sect = clust_to_sect(itr->fsdata, itr->next_clust);
842 read_size = itr->fsdata->clust_size;
843 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400844
Heinrich Schuchardtbb093932020-12-25 14:30:04 +0100845 log_debug("FAT read(sect=%d), clust_size=%d, read_size=%u\n",
846 sect, itr->fsdata->clust_size, read_size);
Rob Clark0c33fce2017-09-09 13:15:53 -0400847
848 /*
849 * NOTE: do_fat_read_at() had complicated logic to deal w/
850 * vfat names that span multiple clusters in the fat16 case,
851 * which get_dentfromdir() probably also needed (and was
852 * missing). And not entirely sure what fat32 didn't have
853 * the same issue.. We solve that by only caring about one
854 * dent at a time and iteratively constructing the vfat long
855 * name.
856 */
Anssi Hannulaccea7332019-02-27 12:55:57 +0200857 ret = disk_read(sect, read_size, itr->block);
Rob Clark0c33fce2017-09-09 13:15:53 -0400858 if (ret < 0) {
859 debug("Error: reading block\n");
860 return NULL;
861 }
862
Anssi Hannulaccea7332019-02-27 12:55:57 +0200863 *nbytes = read_size * itr->fsdata->sect_size;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900864 itr->clust = itr->next_clust;
Rob Clark0c33fce2017-09-09 13:15:53 -0400865 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900866 itr->next_clust++;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200867 if (itr->next_clust * itr->fsdata->clust_size >=
Rob Clark0c33fce2017-09-09 13:15:53 -0400868 itr->fsdata->rootdir_size) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900869 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400870 itr->last_cluster = 1;
871 }
872 } else {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900873 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
874 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
875 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400876 itr->last_cluster = 1;
877 }
878 }
879
880 return itr->block;
881}
882
883static dir_entry *next_dent(fat_itr *itr)
884{
885 if (itr->remaining == 0) {
Anssi Hannulaccea7332019-02-27 12:55:57 +0200886 unsigned nbytes;
Heinrich Schuchardt497d8742020-11-22 09:19:52 +0100887 struct dir_entry *dent = fat_next_cluster(itr, &nbytes);
Rob Clark0c33fce2017-09-09 13:15:53 -0400888
889 /* have we reached the last cluster? */
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900890 if (!dent) {
891 /* a sign for no more entries left */
892 itr->dent = NULL;
Rob Clark0c33fce2017-09-09 13:15:53 -0400893 return NULL;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900894 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400895
896 itr->remaining = nbytes / sizeof(dir_entry) - 1;
897 itr->dent = dent;
898 } else {
899 itr->remaining--;
900 itr->dent++;
901 }
902
903 /* have we reached the last valid entry? */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100904 if (itr->dent->nameext.name[0] == 0)
Rob Clark0c33fce2017-09-09 13:15:53 -0400905 return NULL;
906
907 return itr->dent;
908}
909
910static dir_entry *extract_vfat_name(fat_itr *itr)
911{
912 struct dir_entry *dent = itr->dent;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100913 int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
Rob Clark0c33fce2017-09-09 13:15:53 -0400914 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
915 int n = 0;
916
917 while (seqn--) {
918 char buf[13];
919 int idx = 0;
920
921 slot2str((dir_slot *)dent, buf, &idx);
922
Patrick Wildtd6153f12018-11-26 15:58:13 +0100923 if (n + idx >= sizeof(itr->l_name))
924 return NULL;
925
Rob Clark0c33fce2017-09-09 13:15:53 -0400926 /* shift accumulated long-name up and copy new part in: */
927 memmove(itr->l_name + idx, itr->l_name, n);
928 memcpy(itr->l_name, buf, idx);
929 n += idx;
930
931 dent = next_dent(itr);
932 if (!dent)
933 return NULL;
934 }
935
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900936 /*
937 * We are now at the short file name entry.
938 * If it is marked as deleted, just skip it.
939 */
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100940 if (dent->nameext.name[0] == DELETED_FLAG ||
941 dent->nameext.name[0] == aRING)
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900942 return NULL;
943
Rob Clark0c33fce2017-09-09 13:15:53 -0400944 itr->l_name[n] = '\0';
945
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100946 chksum = mkcksum(&dent->nameext);
Rob Clark0c33fce2017-09-09 13:15:53 -0400947
948 /* checksum mismatch could mean deleted file, etc.. skip it: */
949 if (chksum != alias_checksum) {
950 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100951 chksum, alias_checksum, itr->l_name, dent->nameext.name,
952 dent->nameext.ext);
Rob Clark0c33fce2017-09-09 13:15:53 -0400953 return NULL;
954 }
955
956 return dent;
957}
958
959/**
960 * fat_itr_next() - step to the next entry in a directory
961 *
962 * Must be called once on a new iterator before the cursor is valid.
963 *
964 * @itr: the iterator to iterate
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100965 * Return: boolean, 1 if success or 0 if no more entries in the
Rob Clark0c33fce2017-09-09 13:15:53 -0400966 * current directory
967 */
968static int fat_itr_next(fat_itr *itr)
969{
970 dir_entry *dent;
971
972 itr->name = NULL;
973
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900974 /*
975 * One logical directory entry consist of following slots:
976 * name[0] Attributes
977 * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT
978 * ...
979 * dent[N - 2]: LFN[1] 2 ATTR_VFAT
980 * dent[N - 1]: LFN[0] 1 ATTR_VFAT
981 * dent[N]: SFN ATTR_ARCH
982 */
983
Rob Clark0c33fce2017-09-09 13:15:53 -0400984 while (1) {
985 dent = next_dent(itr);
Heinrich Schuchardt9a4564e2020-11-19 07:44:08 +0100986 if (!dent) {
987 itr->dent_start = NULL;
Rob Clark0c33fce2017-09-09 13:15:53 -0400988 return 0;
Heinrich Schuchardt9a4564e2020-11-19 07:44:08 +0100989 }
990 itr->dent_rem = itr->remaining;
991 itr->dent_start = itr->dent;
992 itr->dent_clust = itr->clust;
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100993 if (dent->nameext.name[0] == DELETED_FLAG)
Rob Clark0c33fce2017-09-09 13:15:53 -0400994 continue;
995
996 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynena799dcd2018-01-05 02:45:21 +0200997 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Heinrich Schuchardt66ea58c2021-01-21 00:23:33 +0100998 (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +0900999 /* long file name */
Rob Clark0c33fce2017-09-09 13:15:53 -04001000 dent = extract_vfat_name(itr);
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +09001001 /*
1002 * If succeeded, dent has a valid short file
1003 * name entry for the current entry.
1004 * If failed, itr points to a current bogus
1005 * entry. So after fetching a next one,
1006 * it may have a short file name entry
1007 * for this bogus entry so that we can still
1008 * check for a short name.
1009 */
Rob Clark0c33fce2017-09-09 13:15:53 -04001010 if (!dent)
1011 continue;
1012 itr->name = itr->l_name;
1013 break;
1014 } else {
1015 /* Volume label or VFAT entry, skip */
1016 continue;
1017 }
Christian Gmeiner59e94402020-06-09 09:09:07 +02001018 }
Rob Clark0c33fce2017-09-09 13:15:53 -04001019
AKASHI Takahiro5f6ae542019-11-26 17:29:31 +09001020 /* short file name */
Rob Clark0c33fce2017-09-09 13:15:53 -04001021 break;
1022 }
1023
1024 get_name(dent, itr->s_name);
1025 if (!itr->name)
1026 itr->name = itr->s_name;
1027
1028 return 1;
1029}
1030
1031/**
1032 * fat_itr_isdir() - is current cursor position pointing to a directory
1033 *
1034 * @itr: the iterator
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001035 * Return: true if cursor is at a directory
Rob Clark0c33fce2017-09-09 13:15:53 -04001036 */
1037static int fat_itr_isdir(fat_itr *itr)
1038{
1039 return !!(itr->dent->attr & ATTR_DIR);
1040}
1041
1042/*
1043 * Helpers:
1044 */
1045
1046#define TYPE_FILE 0x1
1047#define TYPE_DIR 0x2
1048#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
1049
1050/**
1051 * fat_itr_resolve() - traverse directory structure to resolve the
1052 * requested path.
1053 *
1054 * Traverse directory structure to the requested path. If the specified
1055 * path is to a directory, this will descend into the directory and
1056 * leave it iterator at the start of the directory. If the path is to a
1057 * file, it will leave the iterator in the parent directory with current
1058 * cursor at file's entry in the directory.
1059 *
1060 * @itr: iterator initialized to root
1061 * @path: the requested path
1062 * @type: bitmask of allowable file types
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001063 * Return: 0 on success or -errno
Rob Clark0c33fce2017-09-09 13:15:53 -04001064 */
1065static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
1066{
1067 const char *next;
1068
1069 /* chomp any extra leading slashes: */
1070 while (path[0] && ISDIRDELIM(path[0]))
1071 path++;
1072
1073 /* are we at the end? */
1074 if (strlen(path) == 0) {
1075 if (!(type & TYPE_DIR))
1076 return -ENOENT;
1077 return 0;
1078 }
1079
1080 /* find length of next path entry: */
1081 next = path;
1082 while (next[0] && !ISDIRDELIM(next[0]))
1083 next++;
1084
AKASHI Takahirof0a47812018-09-11 15:58:59 +09001085 if (itr->is_root) {
1086 /* root dir doesn't have "." nor ".." */
1087 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
1088 (((next - path) == 2) && !strncmp(path, "..", 2))) {
1089 /* point back to itself */
1090 itr->clust = itr->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +09001091 itr->next_clust = itr->fsdata->root_cluster;
Heinrich Schuchardt51d5b2a2020-11-22 16:04:47 +01001092 itr->start_clust = itr->fsdata->root_cluster;
AKASHI Takahirof0a47812018-09-11 15:58:59 +09001093 itr->dent = NULL;
1094 itr->remaining = 0;
1095 itr->last_cluster = 0;
1096
1097 if (next[0] == 0) {
1098 if (type & TYPE_DIR)
1099 return 0;
1100 else
1101 return -ENOENT;
1102 }
1103
1104 return fat_itr_resolve(itr, next, type);
1105 }
1106 }
1107
Rob Clark0c33fce2017-09-09 13:15:53 -04001108 while (fat_itr_next(itr)) {
1109 int match = 0;
1110 unsigned n = max(strlen(itr->name), (size_t)(next - path));
1111
1112 /* check both long and short name: */
1113 if (!strncasecmp(path, itr->name, n))
1114 match = 1;
1115 else if (itr->name != itr->s_name &&
1116 !strncasecmp(path, itr->s_name, n))
1117 match = 1;
1118
1119 if (!match)
1120 continue;
1121
1122 if (fat_itr_isdir(itr)) {
1123 /* recurse into directory: */
1124 fat_itr_child(itr, itr);
1125 return fat_itr_resolve(itr, next, type);
1126 } else if (next[0]) {
1127 /*
1128 * If next is not empty then we have a case
1129 * like: /path/to/realfile/nonsense
1130 */
1131 debug("bad trailing path: %s\n", next);
1132 return -ENOENT;
1133 } else if (!(type & TYPE_FILE)) {
1134 return -ENOTDIR;
1135 } else {
1136 return 0;
1137 }
1138 }
1139
1140 return -ENOENT;
1141}
1142
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001143int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001144{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001145 boot_sector bs;
1146 volume_info volinfo;
1147 int fatsize;
1148 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001149
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001150 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001151 printf("No current device\n");
1152 return 1;
1153 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001154
Simon Glassf5ac3032022-08-11 19:34:45 -06001155 if (blk_enabled()) {
Simon Glassfada3f92022-09-17 09:00:09 -06001156 printf("Interface: %s\n", blk_get_uclass_name(cur_dev->uclass_id));
Heinrich Schuchardt1c579db2021-01-25 12:53:14 +01001157 printf(" Device %d: ", cur_dev->devnum);
1158 dev_print(cur_dev);
wdenk2c9b05d2003-09-10 22:30:53 +00001159 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001160
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001161 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001162 printf("\nNo valid FAT fs found\n");
1163 return 1;
1164 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001165
1166 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001167 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001168
Christian Taedckecd7a1a02023-11-15 13:44:18 +01001169 printf("Filesystem: FAT%d \"%s\"\n", fatsize, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001170
wdenk2c9b05d2003-09-10 22:30:53 +00001171 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001172}
1173
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001174int fat_exists(const char *filename)
1175{
Rob Clark71193c92017-09-09 13:15:54 -04001176 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001177 fat_itr *itr;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001178 int ret;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001179
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001180 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001181 if (!itr)
1182 return 0;
Rob Clark71193c92017-09-09 13:15:54 -04001183 ret = fat_itr_root(itr, &fsdata);
1184 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001185 goto out;
Rob Clark71193c92017-09-09 13:15:54 -04001186
1187 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001188 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001189out:
Tom Rini5257a092017-09-22 07:37:43 -04001190 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001191 return ret == 0;
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001192}
1193
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +02001194/**
1195 * fat2rtc() - convert FAT time stamp to RTC file stamp
1196 *
1197 * @date: FAT date
1198 * @time: FAT time
1199 * @tm: RTC time stamp
1200 */
1201static void __maybe_unused fat2rtc(u16 date, u16 time, struct rtc_time *tm)
1202{
1203 tm->tm_mday = date & 0x1f;
1204 tm->tm_mon = (date & 0x1e0) >> 4;
1205 tm->tm_year = (date >> 9) + 1980;
1206
1207 tm->tm_sec = (time & 0x1f) << 1;
1208 tm->tm_min = (time & 0x7e0) >> 5;
1209 tm->tm_hour = time >> 11;
1210
1211 rtc_calc_weekday(tm);
1212 tm->tm_yday = 0;
1213 tm->tm_isdst = 0;
1214}
1215
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001216int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001217{
Rob Clark71193c92017-09-09 13:15:54 -04001218 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001219 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001220 int ret;
1221
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001222 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001223 if (!itr)
1224 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001225 ret = fat_itr_root(itr, &fsdata);
1226 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001227 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001228
1229 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1230 if (ret) {
1231 /*
1232 * Directories don't have size, but fs_size() is not
1233 * expected to fail if passed a directory path:
1234 */
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001235 free(fsdata.fatbuf);
Andrew F. Davis8a6c6122019-05-16 09:34:31 -05001236 ret = fat_itr_root(itr, &fsdata);
1237 if (ret)
1238 goto out_free_itr;
1239 ret = fat_itr_resolve(itr, filename, TYPE_DIR);
1240 if (!ret)
Rob Clark71193c92017-09-09 13:15:54 -04001241 *size = 0;
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001242 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001243 }
1244
1245 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001246out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001247 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001248out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001249 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001250 return ret;
Stephen Warren3eb58f52014-06-11 12:47:26 -06001251}
1252
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001253int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1254 loff_t *actread)
wdenk7a428cc2003-06-15 22:40:42 +00001255{
Rob Clark71193c92017-09-09 13:15:54 -04001256 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001257 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001258 int ret;
1259
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001260 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001261 if (!itr)
1262 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001263 ret = fat_itr_root(itr, &fsdata);
1264 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001265 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001266
1267 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1268 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001269 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001270
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001271 debug("reading %s at pos %llu\n", filename, offset);
Tien Fong Cheed16b79b2019-02-11 14:56:20 +08001272
1273 /* For saving default max clustersize memory allocated to malloc pool */
1274 dir_entry *dentptr = itr->dent;
1275
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001276 ret = get_contents(&fsdata, dentptr, offset, buf, len, actread);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001277
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001278out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001279 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001280out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001281 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001282 return ret;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001283}
1284
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001285int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001286{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001287 loff_t actread;
1288 int ret;
1289
Heinrich Schuchardt6362d972023-01-19 15:37:40 +01001290 ret = fat_read_file(filename, buffer, 0, maxsize, &actread);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001291 if (ret)
1292 return ret;
1293 else
1294 return actread;
wdenk7a428cc2003-06-15 22:40:42 +00001295}
Simon Glass19e38582012-12-26 09:53:33 +00001296
Rob Clark4174d7f2017-09-09 13:15:56 -04001297typedef struct {
1298 struct fs_dir_stream parent;
1299 struct fs_dirent dirent;
1300 fsdata fsdata;
1301 fat_itr itr;
1302} fat_dir;
1303
1304int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1305{
Neil Armstrong6f625f82017-11-24 09:54:41 +01001306 fat_dir *dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001307 int ret;
1308
Neil Armstrong6f625f82017-11-24 09:54:41 +01001309 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001310 if (!dir)
1311 return -ENOMEM;
Neil Armstrong6f625f82017-11-24 09:54:41 +01001312 memset(dir, 0, sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001313
1314 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1315 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001316 goto fail_free_dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001317
1318 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1319 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001320 goto fail_free_both;
Rob Clark4174d7f2017-09-09 13:15:56 -04001321
1322 *dirsp = (struct fs_dir_stream *)dir;
1323 return 0;
1324
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001325fail_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001326 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001327fail_free_dir:
Rob Clark4174d7f2017-09-09 13:15:56 -04001328 free(dir);
1329 return ret;
1330}
1331
1332int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1333{
1334 fat_dir *dir = (fat_dir *)dirs;
1335 struct fs_dirent *dent = &dir->dirent;
1336
1337 if (!fat_itr_next(&dir->itr))
1338 return -ENOENT;
1339
1340 memset(dent, 0, sizeof(*dent));
1341 strcpy(dent->name, dir->itr.name);
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +02001342 if (CONFIG_IS_ENABLED(EFI_LOADER)) {
1343 dent->attr = dir->itr.dent->attr;
1344 fat2rtc(le16_to_cpu(dir->itr.dent->cdate),
1345 le16_to_cpu(dir->itr.dent->ctime), &dent->create_time);
1346 fat2rtc(le16_to_cpu(dir->itr.dent->date),
1347 le16_to_cpu(dir->itr.dent->time), &dent->change_time);
1348 fat2rtc(le16_to_cpu(dir->itr.dent->adate),
1349 0, &dent->access_time);
1350 }
Rob Clark4174d7f2017-09-09 13:15:56 -04001351 if (fat_itr_isdir(&dir->itr)) {
1352 dent->type = FS_DT_DIR;
1353 } else {
1354 dent->type = FS_DT_REG;
1355 dent->size = FAT2CPU32(dir->itr.dent->size);
1356 }
1357
1358 *dentp = dent;
1359
1360 return 0;
1361}
1362
1363void fat_closedir(struct fs_dir_stream *dirs)
1364{
1365 fat_dir *dir = (fat_dir *)dirs;
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001366 free(dir->fsdata.fatbuf);
Rob Clark4174d7f2017-09-09 13:15:56 -04001367 free(dir);
1368}
1369
Simon Glass19e38582012-12-26 09:53:33 +00001370void fat_close(void)
1371{
1372}
Heinrich Schuchardtfac91832020-12-31 00:38:13 +01001373
1374int fat_uuid(char *uuid_str)
1375{
1376 boot_sector bs;
1377 volume_info volinfo;
1378 int fatsize;
1379 int ret;
1380 u8 *id;
1381
1382 ret = read_bootsectandvi(&bs, &volinfo, &fatsize);
1383 if (ret)
1384 return ret;
1385
1386 id = volinfo.volume_id;
1387 sprintf(uuid_str, "%02X%02X-%02X%02X", id[3], id[2], id[1], id[0]);
1388
1389 return 0;
1390}