blob: 2da93dae3cf3e0f1c4653def4a7c8845ab159589 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk71f95112003-06-15 22:40:42 +00002/*
3 * fat.c
4 *
5 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
6 *
7 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
8 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
wdenk71f95112003-06-15 22:40:42 +00009 */
10
11#include <common.h>
Simon Glass2a981dc2016-02-29 15:25:52 -070012#include <blk.h>
wdenk71f95112003-06-15 22:40:42 +000013#include <config.h>
Sergei Shtylyovac497772011-08-08 09:38:33 +000014#include <exports.h>
wdenk71f95112003-06-15 22:40:42 +000015#include <fat.h>
Rob Clark1f403662017-09-09 13:15:56 -040016#include <fs.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
wdenk71f95112003-06-15 22:40:42 +000018#include <asm/byteorder.h>
wdenk7205e402003-09-10 22:30:53 +000019#include <part.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000020#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060021#include <memalign.h>
Simon Glass90526e92020-05-10 11:39:56 -060022#include <asm/cache.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000023#include <linux/compiler.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000024#include <linux/ctype.h>
wdenk71f95112003-06-15 22:40:42 +000025
wdenk71f95112003-06-15 22:40:42 +000026/*
Rob Clark21a24c32017-09-09 13:15:59 -040027 * Convert a string to lowercase. Converts at most 'len' characters,
28 * 'len' may be larger than the length of 'str' if 'str' is NULL
29 * terminated.
wdenk71f95112003-06-15 22:40:42 +000030 */
Rob Clark21a24c32017-09-09 13:15:59 -040031static void downcase(char *str, size_t len)
wdenk71f95112003-06-15 22:40:42 +000032{
Rob Clark21a24c32017-09-09 13:15:59 -040033 while (*str != '\0' && len--) {
Richard Genoudfb7e16c2012-12-13 00:47:36 +000034 *str = tolower(*str);
wdenk71f95112003-06-15 22:40:42 +000035 str++;
36 }
37}
38
Simon Glass4101f682016-02-29 15:25:34 -070039static struct blk_desc *cur_dev;
Simon Glass05289792020-05-10 11:39:57 -060040static struct disk_partition cur_part_info;
Wolfgang Denk7385c282010-07-19 11:37:00 +020041
Kyle Moffett9813b752011-12-21 07:08:10 +000042#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk7205e402003-09-10 22:30:53 +000043#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk66c2d732010-07-19 11:36:57 +020044#define DOS_FS32_TYPE_OFFSET 0x52
wdenk71f95112003-06-15 22:40:42 +000045
Kyle Moffett9813b752011-12-21 07:08:10 +000046static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk71f95112003-06-15 22:40:42 +000047{
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020048 ulong ret;
49
Simon Glass2a981dc2016-02-29 15:25:52 -070050 if (!cur_dev)
wdenk7205e402003-09-10 22:30:53 +000051 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020052
Simon Glass2a981dc2016-02-29 15:25:52 -070053 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020054
Tom Rini42a9f142017-08-14 21:02:08 -040055 if (ret != nr_blocks)
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020056 return -1;
57
58 return ret;
wdenk71f95112003-06-15 22:40:42 +000059}
60
Simon Glass05289792020-05-10 11:39:57 -060061int fat_set_blk_dev(struct blk_desc *dev_desc, struct disk_partition *info)
wdenk71f95112003-06-15 22:40:42 +000062{
Eric Nelson9a800ac2012-04-11 04:08:53 +000063 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk7205e402003-09-10 22:30:53 +000064
Stephen Warren5e8f9832012-10-17 06:44:59 +000065 cur_dev = dev_desc;
66 cur_part_info = *info;
Kyle Moffett9813b752011-12-21 07:08:10 +000067
68 /* Make sure it has a valid FAT header */
69 if (disk_read(0, 1, buffer) != 1) {
70 cur_dev = NULL;
71 return -1;
72 }
73
74 /* Check if it's actually a DOS volume */
75 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
76 cur_dev = NULL;
77 return -1;
78 }
79
80 /* Check for FAT12/FAT16/FAT32 filesystem */
81 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
82 return 0;
83 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
84 return 0;
85
86 cur_dev = NULL;
87 return -1;
wdenk71f95112003-06-15 22:40:42 +000088}
89
Simon Glass4101f682016-02-29 15:25:34 -070090int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren5e8f9832012-10-17 06:44:59 +000091{
Simon Glass05289792020-05-10 11:39:57 -060092 struct disk_partition info;
Stephen Warren5e8f9832012-10-17 06:44:59 +000093
94 /* First close any currently found FAT filesystem */
95 cur_dev = NULL;
96
97 /* Read the partition table, if present */
Simon Glass3e8bd462016-02-29 15:25:48 -070098 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren5e8f9832012-10-17 06:44:59 +000099 if (part_no != 0) {
100 printf("** Partition %d not valid on device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700101 part_no, dev_desc->devnum);
Stephen Warren5e8f9832012-10-17 06:44:59 +0000102 return -1;
103 }
104
105 info.start = 0;
106 info.size = dev_desc->lba;
107 info.blksz = dev_desc->blksz;
108 info.name[0] = 0;
109 info.type[0] = 0;
110 info.bootable = 0;
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100111#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren5e8f9832012-10-17 06:44:59 +0000112 info.uuid[0] = 0;
113#endif
114 }
115
116 return fat_set_blk_dev(dev_desc, &info);
117}
Kyle Moffett9813b752011-12-21 07:08:10 +0000118
wdenk71f95112003-06-15 22:40:42 +0000119/*
wdenk71f95112003-06-15 22:40:42 +0000120 * Extract zero terminated short name from a directory entry.
121 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200122static void get_name(dir_entry *dirent, char *s_name)
wdenk71f95112003-06-15 22:40:42 +0000123{
124 char *ptr;
125
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100126 memcpy(s_name, dirent->nameext.name, 8);
wdenk71f95112003-06-15 22:40:42 +0000127 s_name[8] = '\0';
128 ptr = s_name;
129 while (*ptr && *ptr != ' ')
130 ptr++;
Rob Clark21a24c32017-09-09 13:15:59 -0400131 if (dirent->lcase & CASE_LOWER_BASE)
132 downcase(s_name, (unsigned)(ptr - s_name));
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100133 if (dirent->nameext.ext[0] && dirent->nameext.ext[0] != ' ') {
Rob Clark21a24c32017-09-09 13:15:59 -0400134 *ptr++ = '.';
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100135 memcpy(ptr, dirent->nameext.ext, 3);
Rob Clark21a24c32017-09-09 13:15:59 -0400136 if (dirent->lcase & CASE_LOWER_EXT)
137 downcase(ptr, 3);
wdenk71f95112003-06-15 22:40:42 +0000138 ptr[3] = '\0';
139 while (*ptr && *ptr != ' ')
140 ptr++;
141 }
142 *ptr = '\0';
143 if (*s_name == DELETED_FLAG)
144 *s_name = '\0';
145 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100146 *s_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000147}
148
Stefan Brünsb8948d22016-12-17 00:27:51 +0100149static int flush_dirty_fat_buffer(fsdata *mydata);
Tien Fong Cheed8c3ea92019-01-23 14:20:04 +0800150
151#if !CONFIG_IS_ENABLED(FAT_WRITE)
Stefan Brünsb8948d22016-12-17 00:27:51 +0100152/* Stub for read only operation */
153int flush_dirty_fat_buffer(fsdata *mydata)
154{
155 (void)(mydata);
156 return 0;
157}
158#endif
159
wdenk71f95112003-06-15 22:40:42 +0000160/*
161 * Get the entry at index 'entry' in a FAT (12/16/32) table.
162 * On failure 0x00 is returned.
163 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200164static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000165{
166 __u32 bufnum;
Stefan Brünsb352cae2017-01-26 20:22:36 +0000167 __u32 offset, off8;
wdenk71f95112003-06-15 22:40:42 +0000168 __u32 ret = 0x00;
169
Stefan Brünsb8948d22016-12-17 00:27:51 +0100170 if (CHECK_CLUST(entry, mydata->fatsize)) {
171 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
172 return ret;
173 }
174
wdenk71f95112003-06-15 22:40:42 +0000175 switch (mydata->fatsize) {
176 case 32:
177 bufnum = entry / FAT32BUFSIZE;
178 offset = entry - bufnum * FAT32BUFSIZE;
179 break;
180 case 16:
181 bufnum = entry / FAT16BUFSIZE;
182 offset = entry - bufnum * FAT16BUFSIZE;
183 break;
184 case 12:
185 bufnum = entry / FAT12BUFSIZE;
186 offset = entry - bufnum * FAT12BUFSIZE;
187 break;
188
189 default:
190 /* Unsupported FAT size */
191 return ret;
192 }
193
Stefan Brünsb8948d22016-12-17 00:27:51 +0100194 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk7385c282010-07-19 11:37:00 +0200195 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200196
wdenk71f95112003-06-15 22:40:42 +0000197 /* Read a new block of FAT entries into the cache. */
198 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000199 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000200 __u8 *bufptr = mydata->fatbuf;
201 __u32 fatlength = mydata->fatlength;
202 __u32 startblock = bufnum * FATBUFBLOCKS;
203
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100204 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau8006dd22012-07-20 15:19:29 +0200205 if (startblock + getsize > fatlength)
206 getsize = fatlength - startblock;
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000207
wdenk71f95112003-06-15 22:40:42 +0000208 startblock += mydata->fat_sect; /* Offset from start of disk */
209
Stefan Brünsb8948d22016-12-17 00:27:51 +0100210 /* Write back the fatbuf to the disk */
211 if (flush_dirty_fat_buffer(mydata) < 0)
212 return -1;
213
wdenk71f95112003-06-15 22:40:42 +0000214 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200215 debug("Error reading FAT blocks\n");
wdenk71f95112003-06-15 22:40:42 +0000216 return ret;
217 }
218 mydata->fatbufnum = bufnum;
219 }
220
221 /* Get the actual entry from the table */
222 switch (mydata->fatsize) {
223 case 32:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200224 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000225 break;
226 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200227 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000228 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200229 case 12:
Stefan Brünsb352cae2017-01-26 20:22:36 +0000230 off8 = (offset * 3) / 2;
231 /* fatbut + off8 may be unaligned, read in byte granularity */
232 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk71f95112003-06-15 22:40:42 +0000233
Stefan Brüns8d48c922016-12-17 03:55:10 +0100234 if (offset & 0x1)
235 ret >>= 4;
236 ret &= 0xfff;
wdenk71f95112003-06-15 22:40:42 +0000237 }
Stefan Brünsb8948d22016-12-17 00:27:51 +0100238 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
239 mydata->fatsize, ret, entry, offset);
wdenk71f95112003-06-15 22:40:42 +0000240
241 return ret;
242}
243
wdenk71f95112003-06-15 22:40:42 +0000244/*
245 * Read at most 'size' bytes from the specified cluster into 'buffer'.
246 * Return 0 on success, -1 otherwise.
247 */
248static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200249get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000250{
wdenk71f95112003-06-15 22:40:42 +0000251 __u32 startsect;
Kyle Moffett46236b12011-12-20 07:41:13 +0000252 int ret;
wdenk71f95112003-06-15 22:40:42 +0000253
254 if (clustnum > 0) {
Rob Clark265edc02017-09-09 13:16:00 -0400255 startsect = clust_to_sect(mydata, clustnum);
wdenk71f95112003-06-15 22:40:42 +0000256 } else {
257 startsect = mydata->rootdir_sect;
258 }
259
Wolfgang Denk7385c282010-07-19 11:37:00 +0200260 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
261
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200262 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson9a800ac2012-04-11 04:08:53 +0000263 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200264
Heinrich Schuchardt1c381ce2018-09-13 19:42:47 +0200265 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200266
267 while (size >= mydata->sect_size) {
268 ret = disk_read(startsect++, 1, tmpbuf);
269 if (ret != 1) {
270 debug("Error reading data (got %d)\n", ret);
271 return -1;
272 }
273
274 memcpy(buffer, tmpbuf, mydata->sect_size);
275 buffer += mydata->sect_size;
276 size -= mydata->sect_size;
277 }
Ricardo Salveti41130eb2021-09-26 21:36:04 +0300278 } else if (size >= mydata->sect_size) {
279 __u32 bytes_read;
280 __u32 sect_count = size / mydata->sect_size;
Heinrich Schuchardt84ca3052021-01-26 00:14:14 +0100281
Ricardo Salveti41130eb2021-09-26 21:36:04 +0300282 ret = disk_read(startsect, sect_count, buffer);
283 if (ret != sect_count) {
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200284 debug("Error reading data (got %d)\n", ret);
285 return -1;
286 }
Ricardo Salveti41130eb2021-09-26 21:36:04 +0300287 bytes_read = sect_count * mydata->sect_size;
288 startsect += sect_count;
289 buffer += bytes_read;
290 size -= bytes_read;
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200291 }
292 if (size) {
293 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
294
295 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett46236b12011-12-20 07:41:13 +0000296 if (ret != 1) {
297 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000298 return -1;
wdenk71f95112003-06-15 22:40:42 +0000299 }
wdenk7205e402003-09-10 22:30:53 +0000300
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200301 memcpy(buffer, tmpbuf, size);
wdenk71f95112003-06-15 22:40:42 +0000302 }
303
304 return 0;
305}
306
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200307/**
308 * get_contents() - read from file
309 *
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000310 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200311 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
312 * fatal errors.
313 *
314 * @mydata: file system description
315 * @dentprt: directory entry pointer
316 * @pos: position from where to read
317 * @buffer: buffer into which to read
318 * @maxsize: maximum number of bytes to read
319 * @gotsize: number of bytes actually read
320 * Return: -1 on error, otherwise 0
wdenk71f95112003-06-15 22:40:42 +0000321 */
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800322static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
323 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk71f95112003-06-15 22:40:42 +0000324{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800325 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000326 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000327 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000328 __u32 endclust, newclust;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800329 loff_t actsize;
wdenk71f95112003-06-15 22:40:42 +0000330
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800331 *gotsize = 0;
332 debug("Filesize: %llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000333
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000334 if (pos >= filesize) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800335 debug("Read position past EOF: %llu\n", pos);
336 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000337 }
338
339 if (maxsize > 0 && filesize > pos + maxsize)
340 filesize = pos + maxsize;
wdenk71f95112003-06-15 22:40:42 +0000341
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800342 debug("%llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000343
Wolfgang Denk7385c282010-07-19 11:37:00 +0200344 actsize = bytesperclust;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000345
346 /* go to cluster at pos */
347 while (actsize <= pos) {
348 curclust = get_fatent(mydata, curclust);
349 if (CHECK_CLUST(curclust, mydata->fatsize)) {
350 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200351 printf("Invalid FAT entry\n");
352 return -1;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000353 }
354 actsize += bytesperclust;
355 }
356
357 /* actsize > pos */
358 actsize -= bytesperclust;
359 filesize -= actsize;
360 pos -= actsize;
361
362 /* align to beginning of next cluster if any */
363 if (pos) {
Tien Fong Chee85378742019-02-11 14:56:19 +0800364 __u8 *tmp_buffer;
365
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800366 actsize = min(filesize, (loff_t)bytesperclust);
Tien Fong Chee85378742019-02-11 14:56:19 +0800367 tmp_buffer = malloc_cache_aligned(actsize);
368 if (!tmp_buffer) {
369 debug("Error: allocating buffer\n");
Heinrich Schuchardtf1368382019-09-12 19:19:30 +0200370 return -1;
Tien Fong Chee85378742019-02-11 14:56:19 +0800371 }
372
373 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000374 printf("Error reading cluster\n");
Tien Fong Chee85378742019-02-11 14:56:19 +0800375 free(tmp_buffer);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000376 return -1;
377 }
378 filesize -= actsize;
379 actsize -= pos;
Tien Fong Chee85378742019-02-11 14:56:19 +0800380 memcpy(buffer, tmp_buffer + pos, actsize);
381 free(tmp_buffer);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800382 *gotsize += actsize;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000383 if (!filesize)
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800384 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000385 buffer += actsize;
386
387 curclust = get_fatent(mydata, curclust);
388 if (CHECK_CLUST(curclust, mydata->fatsize)) {
389 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200390 printf("Invalid FAT entry\n");
391 return -1;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000392 }
393 }
394
395 actsize = bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200396 endclust = curclust;
397
wdenk71f95112003-06-15 22:40:42 +0000398 do {
wdenk7205e402003-09-10 22:30:53 +0000399 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200400 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000401 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200402 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000403 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100404 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200405 debug("curclust: 0x%x\n", newclust);
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200406 printf("Invalid FAT entry\n");
407 return -1;
wdenk7205e402003-09-10 22:30:53 +0000408 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200409 endclust = newclust;
410 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000411 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200412
wdenk7205e402003-09-10 22:30:53 +0000413 /* get remaining bytes */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200414 actsize = filesize;
Benoît Thébaudeau0880e5b2012-07-20 15:21:37 +0200415 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200416 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000417 return -1;
418 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800419 *gotsize += actsize;
420 return 0;
wdenk7205e402003-09-10 22:30:53 +0000421getit:
422 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200423 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000424 return -1;
425 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800426 *gotsize += (int)actsize;
wdenk7205e402003-09-10 22:30:53 +0000427 filesize -= actsize;
428 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200429
wdenk7205e402003-09-10 22:30:53 +0000430 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100431 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200432 debug("curclust: 0x%x\n", curclust);
433 printf("Invalid FAT entry\n");
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200434 return -1;
wdenk71f95112003-06-15 22:40:42 +0000435 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200436 actsize = bytesperclust;
437 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000438 } while (1);
439}
440
wdenk71f95112003-06-15 22:40:42 +0000441/*
442 * Extract the file name information from 'slotptr' into 'l_name',
443 * starting at l_name[*idx].
444 * Return 1 if terminator (zero byte) is found, 0 otherwise.
445 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200446static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000447{
448 int j;
449
450 for (j = 0; j <= 8; j += 2) {
451 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200452 if (l_name[*idx] == 0x00)
453 return 1;
wdenk71f95112003-06-15 22:40:42 +0000454 (*idx)++;
455 }
456 for (j = 0; j <= 10; j += 2) {
457 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200458 if (l_name[*idx] == 0x00)
459 return 1;
wdenk71f95112003-06-15 22:40:42 +0000460 (*idx)++;
461 }
462 for (j = 0; j <= 2; j += 2) {
463 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200464 if (l_name[*idx] == 0x00)
465 return 1;
wdenk71f95112003-06-15 22:40:42 +0000466 (*idx)++;
467 }
468
469 return 0;
470}
471
wdenk71f95112003-06-15 22:40:42 +0000472/* Calculate short name checksum */
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100473static __u8 mkcksum(struct nameext *nameext)
wdenk71f95112003-06-15 22:40:42 +0000474{
475 int i;
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100476 u8 *pos = (void *)nameext;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200477
wdenk71f95112003-06-15 22:40:42 +0000478 __u8 ret = 0;
479
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100480 for (i = 0; i < 11; i++)
481 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + pos[i];
wdenk71f95112003-06-15 22:40:42 +0000482
483 return ret;
484}
wdenk71f95112003-06-15 22:40:42 +0000485
486/*
wdenk71f95112003-06-15 22:40:42 +0000487 * Read boot sector and volume info from a FAT filesystem
488 */
489static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200490read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000491{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000492 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000493 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000494 int ret = 0;
495
496 if (cur_dev == NULL) {
497 debug("Error: no device selected\n");
498 return -1;
499 }
500
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +0300501 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000502 if (block == NULL) {
503 debug("Error: allocating block\n");
504 return -1;
505 }
wdenk71f95112003-06-15 22:40:42 +0000506
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200507 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200508 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000509 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000510 }
511
512 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200513 bs->reserved = FAT2CPU16(bs->reserved);
514 bs->fat_length = FAT2CPU16(bs->fat_length);
515 bs->secs_track = FAT2CPU16(bs->secs_track);
516 bs->heads = FAT2CPU16(bs->heads);
517 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000518
519 /* FAT32 entries */
520 if (bs->fat_length == 0) {
521 /* Assume FAT32 */
522 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200523 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000524 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200525 bs->info_sector = FAT2CPU16(bs->info_sector);
526 bs->backup_boot = FAT2CPU16(bs->backup_boot);
527 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000528 *fatsize = 32;
529 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200530 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000531 *fatsize = 0;
532 }
533 memcpy(volinfo, vistart, sizeof(volume_info));
534
wdenk71f95112003-06-15 22:40:42 +0000535 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200536 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000537 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000538 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500539 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000540 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000541 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000542 }
Tom Rix651351f2009-05-20 07:55:41 -0500543 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000544 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000545 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000546 }
547 }
548
Wolfgang Denk7385c282010-07-19 11:37:00 +0200549 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000550fail:
551 ret = -1;
552exit:
553 free(block);
554 return ret;
wdenk71f95112003-06-15 22:40:42 +0000555}
556
Rob Clark45449982017-09-09 13:15:52 -0400557static int get_fs_info(fsdata *mydata)
wdenk71f95112003-06-15 22:40:42 +0000558{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200559 boot_sector bs;
560 volume_info volinfo;
Rob Clark45449982017-09-09 13:15:52 -0400561 int ret;
wdenk71f95112003-06-15 22:40:42 +0000562
Rob Clark45449982017-09-09 13:15:52 -0400563 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
564 if (ret) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200565 debug("Error: reading boot sector\n");
Rob Clark45449982017-09-09 13:15:52 -0400566 return ret;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200567 }
568
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000569 if (mydata->fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200570 mydata->fatlength = bs.fat32_length;
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900571 mydata->total_sect = bs.total_sect;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000572 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200573 mydata->fatlength = bs.fat_length;
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900574 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
575 if (!mydata->total_sect)
576 mydata->total_sect = bs.total_sect;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000577 }
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900578 if (!mydata->total_sect) /* unlikely */
579 mydata->total_sect = (u32)cur_part_info.size;
wdenk71f95112003-06-15 22:40:42 +0000580
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900581 mydata->fats = bs.fats;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200582 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000583
Rob Clark45449982017-09-09 13:15:52 -0400584 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000585
Sergei Shtylyovac497772011-08-08 09:38:33 +0000586 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200587 mydata->clust_size = bs.cluster_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000588 if (mydata->sect_size != cur_part_info.blksz) {
589 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
590 mydata->sect_size, cur_part_info.blksz);
591 return -1;
592 }
Patrick Wildtcd80a4f2018-11-26 15:56:57 +0100593 if (mydata->clust_size == 0) {
594 printf("Error: FAT cluster size not set\n");
595 return -1;
596 }
597 if ((unsigned int)mydata->clust_size * mydata->sect_size >
598 MAX_CLUSTSIZE) {
599 printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
600 (unsigned int)mydata->clust_size * mydata->sect_size,
601 MAX_CLUSTSIZE);
602 return -1;
603 }
wdenk71f95112003-06-15 22:40:42 +0000604
Wolfgang Denk7385c282010-07-19 11:37:00 +0200605 if (mydata->fatsize == 32) {
606 mydata->data_begin = mydata->rootdir_sect -
607 (mydata->clust_size * 2);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400608 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200609 } else {
Rob Clark45449982017-09-09 13:15:52 -0400610 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
611 bs.dir_entries[0]) *
612 sizeof(dir_entry)) /
613 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200614 mydata->data_begin = mydata->rootdir_sect +
Rob Clark45449982017-09-09 13:15:52 -0400615 mydata->rootdir_size -
Wolfgang Denk7385c282010-07-19 11:37:00 +0200616 (mydata->clust_size * 2);
Anssi Hannula9b183582019-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 Schuchardtd236e822020-11-22 09:19:52 +0100621 * fat_next_cluster().
Anssi Hannula9b183582019-02-27 12:55:57 +0200622 */
623 mydata->root_cluster = 0;
wdenk71f95112003-06-15 22:40:42 +0000624 }
wdenk71f95112003-06-15 22:40:42 +0000625
Wolfgang Denk7385c282010-07-19 11:37:00 +0200626 mydata->fatbufnum = -1;
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200627 mydata->fat_dirty = 0;
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +0300628 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000629 if (mydata->fatbuf == NULL) {
630 debug("Error: allocating memory\n");
631 return -1;
632 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200633
Wolfgang Denk7385c282010-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 Clarkc6e3baa2017-09-09 13:15:53 -0400638 mydata->root_cluster,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200639 mydata->rootdir_sect,
Sergei Shtylyovac497772011-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);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200643
Rob Clark45449982017-09-09 13:15:52 -0400644 return 0;
645}
646
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100647/**
648 * struct fat_itr - directory iterator, to simplify filesystem traversal
Rob Clarkc6e3baa2017-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 Schuchardtd236e822020-11-22 09:19:52 +0100655 * Rough usage
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400656 *
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100657 * .. code-block:: c
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400658 *
Heinrich Schuchardtd236e822020-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 * }
664 *
665 * For a more complete example, see fat_itr_resolve().
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400666 */
Heinrich Schuchardtd236e822020-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 Schuchardt89735b42020-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 Schuchardtd236e822020-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 Clarkc6e3baa2017-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 Schuchardt185f8122022-01-19 18:05:50 +0100738 * Return: 0 on success, else -errno
Rob Clarkc6e3baa2017-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 Schuchardt7557c842020-11-22 16:04:47 +0100746 itr->start_clust = fsdata->root_cluster;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400747 itr->clust = fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900748 itr->next_clust = fsdata->root_cluster;
Rob Clarkc6e3baa2017-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 Takahiro3a10e072018-09-11 15:59:09 +0900782 itr->start_clust = clustnum;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400783 if (clustnum > 0) {
784 itr->clust = clustnum;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900785 itr->next_clust = clustnum;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300786 itr->is_root = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400787 } else {
788 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900789 itr->next_clust = parent->fsdata->root_cluster;
Heinrich Schuchardt7557c842020-11-22 16:04:47 +0100790 itr->start_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300791 itr->is_root = 1;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400792 }
793 itr->dent = NULL;
794 itr->remaining = 0;
795 itr->last_cluster = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400796}
797
Heinrich Schuchardtd236e822020-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 Clarkc6e3baa2017-09-09 13:15:53 -0400809{
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400810 int ret;
811 u32 sect;
Anssi Hannula9b183582019-02-27 12:55:57 +0200812 u32 read_size;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400813
814 /* have we reached the end? */
815 if (itr->last_cluster)
816 return NULL;
817
Anssi Hannula9b183582019-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 Clarkc6e3baa2017-09-09 13:15:53 -0400836
Heinrich Schuchardtd0be6762020-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 Clarkc6e3baa2017-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 Hannula9b183582019-02-27 12:55:57 +0200849 ret = disk_read(sect, read_size, itr->block);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400850 if (ret < 0) {
851 debug("Error: reading block\n");
852 return NULL;
853 }
854
Anssi Hannula9b183582019-02-27 12:55:57 +0200855 *nbytes = read_size * itr->fsdata->sect_size;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900856 itr->clust = itr->next_clust;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400857 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900858 itr->next_clust++;
Anssi Hannula9b183582019-02-27 12:55:57 +0200859 if (itr->next_clust * itr->fsdata->clust_size >=
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400860 itr->fsdata->rootdir_size) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900861 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400862 itr->last_cluster = 1;
863 }
864 } else {
AKASHI Takahirof528c142018-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 Clarkc6e3baa2017-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 Hannula9b183582019-02-27 12:55:57 +0200878 unsigned nbytes;
Heinrich Schuchardtd236e822020-11-22 09:19:52 +0100879 struct dir_entry *dent = fat_next_cluster(itr, &nbytes);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400880
881 /* have we reached the last cluster? */
AKASHI Takahirof528c142018-09-11 15:59:00 +0900882 if (!dent) {
883 /* a sign for no more entries left */
884 itr->dent = NULL;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400885 return NULL;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900886 }
Rob Clarkc6e3baa2017-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 Schuchardt041f0af2021-01-21 00:23:33 +0100896 if (itr->dent->nameext.name[0] == 0)
Rob Clarkc6e3baa2017-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 Schuchardt041f0af2021-01-21 00:23:33 +0100905 int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
Rob Clarkc6e3baa2017-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 Wildt8b021bb2018-11-26 15:58:13 +0100915 if (n + idx >= sizeof(itr->l_name))
916 return NULL;
917
Rob Clarkc6e3baa2017-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 Takahiro39606d42019-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 Schuchardt041f0af2021-01-21 00:23:33 +0100932 if (dent->nameext.name[0] == DELETED_FLAG ||
933 dent->nameext.name[0] == aRING)
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900934 return NULL;
935
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400936 itr->l_name[n] = '\0';
937
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100938 chksum = mkcksum(&dent->nameext);
Rob Clarkc6e3baa2017-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 Schuchardt041f0af2021-01-21 00:23:33 +0100943 chksum, alias_checksum, itr->l_name, dent->nameext.name,
944 dent->nameext.ext);
Rob Clarkc6e3baa2017-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 Schuchardt185f8122022-01-19 18:05:50 +0100957 * Return: boolean, 1 if success or 0 if no more entries in the
Rob Clarkc6e3baa2017-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 Takahiro39606d42019-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 Clarkc6e3baa2017-09-09 13:15:53 -0400976 while (1) {
977 dent = next_dent(itr);
Heinrich Schuchardt89735b42020-11-19 07:44:08 +0100978 if (!dent) {
979 itr->dent_start = NULL;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400980 return 0;
Heinrich Schuchardt89735b42020-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 Schuchardt041f0af2021-01-21 00:23:33 +0100985 if (dent->nameext.name[0] == DELETED_FLAG)
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400986 continue;
987
988 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynen8bad6cb2018-01-05 02:45:21 +0200989 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Heinrich Schuchardt041f0af2021-01-21 00:23:33 +0100990 (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900991 /* long file name */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400992 dent = extract_vfat_name(itr);
AKASHI Takahiro39606d42019-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 Clarkc6e3baa2017-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 Gmeiner1788a962020-06-09 09:09:07 +02001010 }
Rob Clarkc6e3baa2017-09-09 13:15:53 -04001011
AKASHI Takahiro39606d42019-11-26 17:29:31 +09001012 /* short file name */
Rob Clarkc6e3baa2017-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 Schuchardt185f8122022-01-19 18:05:50 +01001027 * Return: true if cursor is at a directory
Rob Clarkc6e3baa2017-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 Schuchardt185f8122022-01-19 18:05:50 +01001055 * Return: 0 on success or -errno
Rob Clarkc6e3baa2017-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 Takahirob94b6be2018-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 Takahirof528c142018-09-11 15:59:00 +09001083 itr->next_clust = itr->fsdata->root_cluster;
Heinrich Schuchardt7557c842020-11-22 16:04:47 +01001084 itr->start_clust = itr->fsdata->root_cluster;
AKASHI Takahirob94b6be2018-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 Clarkc6e3baa2017-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ébaudeau9795e072012-07-20 15:18:44 +02001135int file_fat_detectfs(void)
wdenk71f95112003-06-15 22:40:42 +00001136{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001137 boot_sector bs;
1138 volume_info volinfo;
1139 int fatsize;
1140 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001141
Wolfgang Denk7385c282010-07-19 11:37:00 +02001142 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001143 printf("No current device\n");
1144 return 1;
1145 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001146
Simon Glassa51eb8d2022-08-11 19:34:45 -06001147 if (blk_enabled()) {
Simon Glass8149b152022-09-17 09:00:09 -06001148 printf("Interface: %s\n", blk_get_uclass_name(cur_dev->uclass_id));
Heinrich Schuchardt02079eb2021-01-25 12:53:14 +01001149 printf(" Device %d: ", cur_dev->devnum);
1150 dev_print(cur_dev);
wdenk7205e402003-09-10 22:30:53 +00001151 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001152
Wolfgang Denk7385c282010-07-19 11:37:00 +02001153 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001154 printf("\nNo valid FAT fs found\n");
1155 return 1;
1156 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001157
1158 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001159 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001160 volinfo.fs_type[5] = '\0';
1161
Stephen Warren461f86e2012-10-17 06:44:57 +00001162 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001163
wdenk7205e402003-09-10 22:30:53 +00001164 return 0;
wdenk71f95112003-06-15 22:40:42 +00001165}
1166
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001167int fat_exists(const char *filename)
1168{
Rob Clark8eafae22017-09-09 13:15:54 -04001169 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001170 fat_itr *itr;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001171 int ret;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001172
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001173 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001174 if (!itr)
1175 return 0;
Rob Clark8eafae22017-09-09 13:15:54 -04001176 ret = fat_itr_root(itr, &fsdata);
1177 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001178 goto out;
Rob Clark8eafae22017-09-09 13:15:54 -04001179
1180 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clark725ffdb2017-09-12 16:40:01 -04001181 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001182out:
Tom Rini24600982017-09-22 07:37:43 -04001183 free(itr);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001184 return ret == 0;
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001185}
1186
Heinrich Schuchardt13c11c62021-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 Ramasamid455d872014-11-17 14:39:38 -08001209int fat_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -06001210{
Rob Clark8eafae22017-09-09 13:15:54 -04001211 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001212 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001213 int ret;
1214
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001215 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001216 if (!itr)
1217 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001218 ret = fat_itr_root(itr, &fsdata);
1219 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001220 goto out_free_itr;
Rob Clark8eafae22017-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 Clark725ffdb2017-09-12 16:40:01 -04001228 free(fsdata.fatbuf);
Andrew F. Davisd0cd30e2019-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 Clark8eafae22017-09-09 13:15:54 -04001234 *size = 0;
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001235 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001236 }
1237
1238 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001239out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001240 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001241out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001242 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001243 return ret;
Stephen Warrencf659812014-06-11 12:47:26 -06001244}
1245
Heinrich Schuchardtde943352023-01-19 15:37:40 +01001246int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1247 loff_t *actread)
wdenk71f95112003-06-15 22:40:42 +00001248{
Rob Clark8eafae22017-09-09 13:15:54 -04001249 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001250 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001251 int ret;
1252
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001253 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001254 if (!itr)
1255 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001256 ret = fat_itr_root(itr, &fsdata);
1257 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001258 goto out_free_itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001259
1260 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1261 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001262 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001263
Heinrich Schuchardtde943352023-01-19 15:37:40 +01001264 debug("reading %s at pos %llu\n", filename, offset);
Tien Fong Cheee48485f2019-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 Schuchardtde943352023-01-19 15:37:40 +01001269 ret = get_contents(&fsdata, dentptr, offset, buf, len, actread);
Rob Clark725ffdb2017-09-12 16:40:01 -04001270
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001271out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001272 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001273out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001274 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001275 return ret;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001276}
1277
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001278int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001279{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001280 loff_t actread;
1281 int ret;
1282
Heinrich Schuchardtde943352023-01-19 15:37:40 +01001283 ret = fat_read_file(filename, buffer, 0, maxsize, &actread);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001284 if (ret)
1285 return ret;
1286 else
1287 return actread;
wdenk71f95112003-06-15 22:40:42 +00001288}
Simon Glasse6d52412012-12-26 09:53:33 +00001289
Rob Clark1f403662017-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 Armstrong34dd8532017-11-24 09:54:41 +01001299 fat_dir *dir;
Rob Clark1f403662017-09-09 13:15:56 -04001300 int ret;
1301
Neil Armstrong34dd8532017-11-24 09:54:41 +01001302 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001303 if (!dir)
1304 return -ENOMEM;
Neil Armstrong34dd8532017-11-24 09:54:41 +01001305 memset(dir, 0, sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001306
1307 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1308 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001309 goto fail_free_dir;
Rob Clark1f403662017-09-09 13:15:56 -04001310
1311 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1312 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001313 goto fail_free_both;
Rob Clark1f403662017-09-09 13:15:56 -04001314
1315 *dirsp = (struct fs_dir_stream *)dir;
1316 return 0;
1317
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001318fail_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001319 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001320fail_free_dir:
Rob Clark1f403662017-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 Schuchardt13c11c62021-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 Clark1f403662017-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 Clark725ffdb2017-09-12 16:40:01 -04001359 free(dir->fsdata.fatbuf);
Rob Clark1f403662017-09-09 13:15:56 -04001360 free(dir);
1361}
1362
Simon Glasse6d52412012-12-26 09:53:33 +00001363void fat_close(void)
1364{
1365}
Heinrich Schuchardtc0029e42020-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}