blob: 9578b74bae735e6586af600e0b3e514826c023dd [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk71f95112003-06-15 22:40:42 +00002/*
3 * fat.c
4 *
5 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
6 *
7 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
8 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
wdenk71f95112003-06-15 22:40:42 +00009 */
10
11#include <common.h>
Simon Glass2a981dc2016-02-29 15:25:52 -070012#include <blk.h>
wdenk71f95112003-06-15 22:40:42 +000013#include <config.h>
Sergei Shtylyovac497772011-08-08 09:38:33 +000014#include <exports.h>
wdenk71f95112003-06-15 22:40:42 +000015#include <fat.h>
Rob Clark1f403662017-09-09 13:15:56 -040016#include <fs.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
wdenk71f95112003-06-15 22:40:42 +000018#include <asm/byteorder.h>
wdenk7205e402003-09-10 22:30:53 +000019#include <part.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000020#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060021#include <memalign.h>
Simon Glass90526e92020-05-10 11:39:56 -060022#include <asm/cache.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000023#include <linux/compiler.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000024#include <linux/ctype.h>
wdenk71f95112003-06-15 22:40:42 +000025
wdenk71f95112003-06-15 22:40:42 +000026/*
Rob Clark21a24c32017-09-09 13:15:59 -040027 * Convert a string to lowercase. Converts at most 'len' characters,
28 * 'len' may be larger than the length of 'str' if 'str' is NULL
29 * terminated.
wdenk71f95112003-06-15 22:40:42 +000030 */
Rob Clark21a24c32017-09-09 13:15:59 -040031static void downcase(char *str, size_t len)
wdenk71f95112003-06-15 22:40:42 +000032{
Rob Clark21a24c32017-09-09 13:15:59 -040033 while (*str != '\0' && len--) {
Richard Genoudfb7e16c2012-12-13 00:47:36 +000034 *str = tolower(*str);
wdenk71f95112003-06-15 22:40:42 +000035 str++;
36 }
37}
38
Simon Glass4101f682016-02-29 15:25:34 -070039static struct blk_desc *cur_dev;
Simon Glass05289792020-05-10 11:39:57 -060040static struct disk_partition cur_part_info;
Wolfgang Denk7385c282010-07-19 11:37:00 +020041
Kyle Moffett9813b752011-12-21 07:08:10 +000042#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk7205e402003-09-10 22:30:53 +000043#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk66c2d732010-07-19 11:36:57 +020044#define DOS_FS32_TYPE_OFFSET 0x52
wdenk71f95112003-06-15 22:40:42 +000045
Kyle Moffett9813b752011-12-21 07:08:10 +000046static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk71f95112003-06-15 22:40:42 +000047{
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020048 ulong ret;
49
Simon Glass2a981dc2016-02-29 15:25:52 -070050 if (!cur_dev)
wdenk7205e402003-09-10 22:30:53 +000051 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020052
Simon Glass2a981dc2016-02-29 15:25:52 -070053 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020054
Tom Rini42a9f142017-08-14 21:02:08 -040055 if (ret != nr_blocks)
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020056 return -1;
57
58 return ret;
wdenk71f95112003-06-15 22:40:42 +000059}
60
Simon Glass05289792020-05-10 11:39:57 -060061int fat_set_blk_dev(struct blk_desc *dev_desc, struct disk_partition *info)
wdenk71f95112003-06-15 22:40:42 +000062{
Eric Nelson9a800ac2012-04-11 04:08:53 +000063 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk7205e402003-09-10 22:30:53 +000064
Stephen Warren5e8f9832012-10-17 06:44:59 +000065 cur_dev = dev_desc;
66 cur_part_info = *info;
Kyle Moffett9813b752011-12-21 07:08:10 +000067
68 /* Make sure it has a valid FAT header */
69 if (disk_read(0, 1, buffer) != 1) {
70 cur_dev = NULL;
71 return -1;
72 }
73
74 /* Check if it's actually a DOS volume */
75 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
76 cur_dev = NULL;
77 return -1;
78 }
79
80 /* Check for FAT12/FAT16/FAT32 filesystem */
81 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
82 return 0;
83 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
84 return 0;
85
86 cur_dev = NULL;
87 return -1;
wdenk71f95112003-06-15 22:40:42 +000088}
89
Simon Glass4101f682016-02-29 15:25:34 -070090int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren5e8f9832012-10-17 06:44:59 +000091{
Simon Glass05289792020-05-10 11:39:57 -060092 struct disk_partition info;
Stephen Warren5e8f9832012-10-17 06:44:59 +000093
94 /* First close any currently found FAT filesystem */
95 cur_dev = NULL;
96
97 /* Read the partition table, if present */
Simon Glass3e8bd462016-02-29 15:25:48 -070098 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren5e8f9832012-10-17 06:44:59 +000099 if (part_no != 0) {
100 printf("** Partition %d not valid on device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700101 part_no, dev_desc->devnum);
Stephen Warren5e8f9832012-10-17 06:44:59 +0000102 return -1;
103 }
104
105 info.start = 0;
106 info.size = dev_desc->lba;
107 info.blksz = dev_desc->blksz;
108 info.name[0] = 0;
109 info.type[0] = 0;
110 info.bootable = 0;
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100111#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren5e8f9832012-10-17 06:44:59 +0000112 info.uuid[0] = 0;
113#endif
114 }
115
116 return fat_set_blk_dev(dev_desc, &info);
117}
Kyle Moffett9813b752011-12-21 07:08:10 +0000118
wdenk71f95112003-06-15 22:40:42 +0000119/*
wdenk71f95112003-06-15 22:40:42 +0000120 * Extract zero terminated short name from a directory entry.
121 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200122static void get_name(dir_entry *dirent, char *s_name)
wdenk71f95112003-06-15 22:40:42 +0000123{
124 char *ptr;
125
Wolfgang Denk7385c282010-07-19 11:37:00 +0200126 memcpy(s_name, dirent->name, 8);
wdenk71f95112003-06-15 22:40:42 +0000127 s_name[8] = '\0';
128 ptr = s_name;
129 while (*ptr && *ptr != ' ')
130 ptr++;
Rob Clark21a24c32017-09-09 13:15:59 -0400131 if (dirent->lcase & CASE_LOWER_BASE)
132 downcase(s_name, (unsigned)(ptr - s_name));
wdenk71f95112003-06-15 22:40:42 +0000133 if (dirent->ext[0] && dirent->ext[0] != ' ') {
Rob Clark21a24c32017-09-09 13:15:59 -0400134 *ptr++ = '.';
Wolfgang Denk7385c282010-07-19 11:37:00 +0200135 memcpy(ptr, dirent->ext, 3);
Rob Clark21a24c32017-09-09 13:15:59 -0400136 if (dirent->lcase & CASE_LOWER_EXT)
137 downcase(ptr, 3);
wdenk71f95112003-06-15 22:40:42 +0000138 ptr[3] = '\0';
139 while (*ptr && *ptr != ' ')
140 ptr++;
141 }
142 *ptr = '\0';
143 if (*s_name == DELETED_FLAG)
144 *s_name = '\0';
145 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100146 *s_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000147}
148
Stefan Brünsb8948d22016-12-17 00:27:51 +0100149static int flush_dirty_fat_buffer(fsdata *mydata);
Tien Fong Cheed8c3ea92019-01-23 14:20:04 +0800150
151#if !CONFIG_IS_ENABLED(FAT_WRITE)
Stefan Brünsb8948d22016-12-17 00:27:51 +0100152/* Stub for read only operation */
153int flush_dirty_fat_buffer(fsdata *mydata)
154{
155 (void)(mydata);
156 return 0;
157}
158#endif
159
wdenk71f95112003-06-15 22:40:42 +0000160/*
161 * Get the entry at index 'entry' in a FAT (12/16/32) table.
162 * On failure 0x00 is returned.
163 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200164static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000165{
166 __u32 bufnum;
Stefan Brünsb352cae2017-01-26 20:22:36 +0000167 __u32 offset, off8;
wdenk71f95112003-06-15 22:40:42 +0000168 __u32 ret = 0x00;
169
Stefan Brünsb8948d22016-12-17 00:27:51 +0100170 if (CHECK_CLUST(entry, mydata->fatsize)) {
171 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
172 return ret;
173 }
174
wdenk71f95112003-06-15 22:40:42 +0000175 switch (mydata->fatsize) {
176 case 32:
177 bufnum = entry / FAT32BUFSIZE;
178 offset = entry - bufnum * FAT32BUFSIZE;
179 break;
180 case 16:
181 bufnum = entry / FAT16BUFSIZE;
182 offset = entry - bufnum * FAT16BUFSIZE;
183 break;
184 case 12:
185 bufnum = entry / FAT12BUFSIZE;
186 offset = entry - bufnum * FAT12BUFSIZE;
187 break;
188
189 default:
190 /* Unsupported FAT size */
191 return ret;
192 }
193
Stefan Brünsb8948d22016-12-17 00:27:51 +0100194 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk7385c282010-07-19 11:37:00 +0200195 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200196
wdenk71f95112003-06-15 22:40:42 +0000197 /* Read a new block of FAT entries into the cache. */
198 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000199 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000200 __u8 *bufptr = mydata->fatbuf;
201 __u32 fatlength = mydata->fatlength;
202 __u32 startblock = bufnum * FATBUFBLOCKS;
203
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100204 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau8006dd22012-07-20 15:19:29 +0200205 if (startblock + getsize > fatlength)
206 getsize = fatlength - startblock;
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000207
wdenk71f95112003-06-15 22:40:42 +0000208 startblock += mydata->fat_sect; /* Offset from start of disk */
209
Stefan Brünsb8948d22016-12-17 00:27:51 +0100210 /* Write back the fatbuf to the disk */
211 if (flush_dirty_fat_buffer(mydata) < 0)
212 return -1;
213
wdenk71f95112003-06-15 22:40:42 +0000214 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200215 debug("Error reading FAT blocks\n");
wdenk71f95112003-06-15 22:40:42 +0000216 return ret;
217 }
218 mydata->fatbufnum = bufnum;
219 }
220
221 /* Get the actual entry from the table */
222 switch (mydata->fatsize) {
223 case 32:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200224 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000225 break;
226 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200227 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000228 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200229 case 12:
Stefan Brünsb352cae2017-01-26 20:22:36 +0000230 off8 = (offset * 3) / 2;
231 /* fatbut + off8 may be unaligned, read in byte granularity */
232 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk71f95112003-06-15 22:40:42 +0000233
Stefan Brüns8d48c922016-12-17 03:55:10 +0100234 if (offset & 0x1)
235 ret >>= 4;
236 ret &= 0xfff;
wdenk71f95112003-06-15 22:40:42 +0000237 }
Stefan Brünsb8948d22016-12-17 00:27:51 +0100238 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
239 mydata->fatsize, ret, entry, offset);
wdenk71f95112003-06-15 22:40:42 +0000240
241 return ret;
242}
243
wdenk71f95112003-06-15 22:40:42 +0000244/*
245 * Read at most 'size' bytes from the specified cluster into 'buffer'.
246 * Return 0 on success, -1 otherwise.
247 */
248static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200249get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000250{
Erik Hansen3f270f42011-03-24 10:15:37 +0100251 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000252 __u32 startsect;
Kyle Moffett46236b12011-12-20 07:41:13 +0000253 int ret;
wdenk71f95112003-06-15 22:40:42 +0000254
255 if (clustnum > 0) {
Rob Clark265edc02017-09-09 13:16:00 -0400256 startsect = clust_to_sect(mydata, clustnum);
wdenk71f95112003-06-15 22:40:42 +0000257 } else {
258 startsect = mydata->rootdir_sect;
259 }
260
Wolfgang Denk7385c282010-07-19 11:37:00 +0200261 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
262
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200263 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson9a800ac2012-04-11 04:08:53 +0000264 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200265
Heinrich Schuchardt1c381ce2018-09-13 19:42:47 +0200266 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200267
268 while (size >= mydata->sect_size) {
269 ret = disk_read(startsect++, 1, tmpbuf);
270 if (ret != 1) {
271 debug("Error reading data (got %d)\n", ret);
272 return -1;
273 }
274
275 memcpy(buffer, tmpbuf, mydata->sect_size);
276 buffer += mydata->sect_size;
277 size -= mydata->sect_size;
278 }
279 } else {
Sergei Shtylyovac497772011-08-08 09:38:33 +0000280 idx = size / mydata->sect_size;
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200281 ret = disk_read(startsect, idx, buffer);
282 if (ret != idx) {
283 debug("Error reading data (got %d)\n", ret);
284 return -1;
285 }
286 startsect += idx;
287 idx *= mydata->sect_size;
288 buffer += idx;
289 size -= idx;
290 }
291 if (size) {
292 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
293
294 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett46236b12011-12-20 07:41:13 +0000295 if (ret != 1) {
296 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000297 return -1;
wdenk71f95112003-06-15 22:40:42 +0000298 }
wdenk7205e402003-09-10 22:30:53 +0000299
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200300 memcpy(buffer, tmpbuf, size);
wdenk71f95112003-06-15 22:40:42 +0000301 }
302
303 return 0;
304}
305
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200306/**
307 * get_contents() - read from file
308 *
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000309 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200310 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
311 * fatal errors.
312 *
313 * @mydata: file system description
314 * @dentprt: directory entry pointer
315 * @pos: position from where to read
316 * @buffer: buffer into which to read
317 * @maxsize: maximum number of bytes to read
318 * @gotsize: number of bytes actually read
319 * Return: -1 on error, otherwise 0
wdenk71f95112003-06-15 22:40:42 +0000320 */
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800321static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
322 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk71f95112003-06-15 22:40:42 +0000323{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800324 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000325 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000326 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000327 __u32 endclust, newclust;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800328 loff_t actsize;
wdenk71f95112003-06-15 22:40:42 +0000329
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800330 *gotsize = 0;
331 debug("Filesize: %llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000332
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000333 if (pos >= filesize) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800334 debug("Read position past EOF: %llu\n", pos);
335 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000336 }
337
338 if (maxsize > 0 && filesize > pos + maxsize)
339 filesize = pos + maxsize;
wdenk71f95112003-06-15 22:40:42 +0000340
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800341 debug("%llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000342
Wolfgang Denk7385c282010-07-19 11:37:00 +0200343 actsize = bytesperclust;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000344
345 /* go to cluster at pos */
346 while (actsize <= pos) {
347 curclust = get_fatent(mydata, curclust);
348 if (CHECK_CLUST(curclust, mydata->fatsize)) {
349 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200350 printf("Invalid FAT entry\n");
351 return -1;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000352 }
353 actsize += bytesperclust;
354 }
355
356 /* actsize > pos */
357 actsize -= bytesperclust;
358 filesize -= actsize;
359 pos -= actsize;
360
361 /* align to beginning of next cluster if any */
362 if (pos) {
Tien Fong Chee85378742019-02-11 14:56:19 +0800363 __u8 *tmp_buffer;
364
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800365 actsize = min(filesize, (loff_t)bytesperclust);
Tien Fong Chee85378742019-02-11 14:56:19 +0800366 tmp_buffer = malloc_cache_aligned(actsize);
367 if (!tmp_buffer) {
368 debug("Error: allocating buffer\n");
Heinrich Schuchardtf1368382019-09-12 19:19:30 +0200369 return -1;
Tien Fong Chee85378742019-02-11 14:56:19 +0800370 }
371
372 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000373 printf("Error reading cluster\n");
Tien Fong Chee85378742019-02-11 14:56:19 +0800374 free(tmp_buffer);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000375 return -1;
376 }
377 filesize -= actsize;
378 actsize -= pos;
Tien Fong Chee85378742019-02-11 14:56:19 +0800379 memcpy(buffer, tmp_buffer + pos, actsize);
380 free(tmp_buffer);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800381 *gotsize += actsize;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000382 if (!filesize)
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800383 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000384 buffer += actsize;
385
386 curclust = get_fatent(mydata, curclust);
387 if (CHECK_CLUST(curclust, mydata->fatsize)) {
388 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200389 printf("Invalid FAT entry\n");
390 return -1;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000391 }
392 }
393
394 actsize = bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200395 endclust = curclust;
396
wdenk71f95112003-06-15 22:40:42 +0000397 do {
wdenk7205e402003-09-10 22:30:53 +0000398 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200399 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000400 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200401 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000402 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100403 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200404 debug("curclust: 0x%x\n", newclust);
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200405 printf("Invalid FAT entry\n");
406 return -1;
wdenk7205e402003-09-10 22:30:53 +0000407 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200408 endclust = newclust;
409 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000410 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200411
wdenk7205e402003-09-10 22:30:53 +0000412 /* get remaining bytes */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200413 actsize = filesize;
Benoît Thébaudeau0880e5b2012-07-20 15:21:37 +0200414 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200415 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000416 return -1;
417 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800418 *gotsize += actsize;
419 return 0;
wdenk7205e402003-09-10 22:30:53 +0000420getit:
421 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200422 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000423 return -1;
424 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800425 *gotsize += (int)actsize;
wdenk7205e402003-09-10 22:30:53 +0000426 filesize -= actsize;
427 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200428
wdenk7205e402003-09-10 22:30:53 +0000429 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100430 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200431 debug("curclust: 0x%x\n", curclust);
432 printf("Invalid FAT entry\n");
Heinrich Schuchardtc7a86d12019-09-12 19:19:29 +0200433 return -1;
wdenk71f95112003-06-15 22:40:42 +0000434 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200435 actsize = bytesperclust;
436 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000437 } while (1);
438}
439
wdenk71f95112003-06-15 22:40:42 +0000440/*
441 * Extract the file name information from 'slotptr' into 'l_name',
442 * starting at l_name[*idx].
443 * Return 1 if terminator (zero byte) is found, 0 otherwise.
444 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200445static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000446{
447 int j;
448
449 for (j = 0; j <= 8; j += 2) {
450 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200451 if (l_name[*idx] == 0x00)
452 return 1;
wdenk71f95112003-06-15 22:40:42 +0000453 (*idx)++;
454 }
455 for (j = 0; j <= 10; j += 2) {
456 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200457 if (l_name[*idx] == 0x00)
458 return 1;
wdenk71f95112003-06-15 22:40:42 +0000459 (*idx)++;
460 }
461 for (j = 0; j <= 2; j += 2) {
462 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200463 if (l_name[*idx] == 0x00)
464 return 1;
wdenk71f95112003-06-15 22:40:42 +0000465 (*idx)++;
466 }
467
468 return 0;
469}
470
wdenk71f95112003-06-15 22:40:42 +0000471/* Calculate short name checksum */
Marek Vasutff04f6d2012-10-09 07:20:22 +0000472static __u8 mkcksum(const char name[8], const char ext[3])
wdenk71f95112003-06-15 22:40:42 +0000473{
474 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200475
wdenk71f95112003-06-15 22:40:42 +0000476 __u8 ret = 0;
477
Marek Vasut6ad77d82013-01-11 03:35:48 +0000478 for (i = 0; i < 8; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000479 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut6ad77d82013-01-11 03:35:48 +0000480 for (i = 0; i < 3; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000481 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[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
621 * next_cluster().
622 */
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
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400647
648/*
649 * Directory iterator, to simplify filesystem traversal
650 *
651 * Implements an iterator pattern to traverse directory tables,
652 * transparently handling directory tables split across multiple
653 * clusters, and the difference between FAT12/FAT16 root directory
654 * (contiguous) and subdirectories + FAT32 root (chained).
655 *
656 * Rough usage:
657 *
658 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
659 * // to traverse down to a subdirectory pointed to by
660 * // current iterator position:
661 * fat_itr_child(&itr, &itr);
662 * }
663 *
664 * For more complete example, see fat_itr_resolve()
665 */
666
667typedef struct {
668 fsdata *fsdata; /* filesystem parameters */
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900669 unsigned start_clust; /* first cluster */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400670 unsigned clust; /* current cluster */
AKASHI Takahirof528c142018-09-11 15:59:00 +0900671 unsigned next_clust; /* next cluster if remaining == 0 */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400672 int last_cluster; /* set once we've read last cluster */
673 int is_root; /* is iterator at root directory */
674 int remaining; /* remaining dent's in current cluster */
675
676 /* current iterator position values: */
677 dir_entry *dent; /* current directory entry */
678 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
679 char s_name[14]; /* short 8.3 name */
680 char *name; /* l_name if there is one, else s_name */
681
682 /* storage for current cluster in memory: */
683 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
684} fat_itr;
685
686static int fat_itr_isdir(fat_itr *itr);
687
688/**
689 * fat_itr_root() - initialize an iterator to start at the root
690 * directory
691 *
692 * @itr: iterator to initialize
693 * @fsdata: filesystem data for the partition
694 * @return 0 on success, else -errno
695 */
696static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
697{
698 if (get_fs_info(fsdata))
699 return -ENXIO;
700
701 itr->fsdata = fsdata;
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900702 itr->start_clust = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400703 itr->clust = fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900704 itr->next_clust = fsdata->root_cluster;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400705 itr->dent = NULL;
706 itr->remaining = 0;
707 itr->last_cluster = 0;
708 itr->is_root = 1;
709
710 return 0;
711}
712
713/**
714 * fat_itr_child() - initialize an iterator to descend into a sub-
715 * directory
716 *
717 * Initializes 'itr' to iterate the contents of the directory at
718 * the current cursor position of 'parent'. It is an error to
719 * call this if the current cursor of 'parent' is pointing at a
720 * regular file.
721 *
722 * Note that 'itr' and 'parent' can be the same pointer if you do
723 * not need to preserve 'parent' after this call, which is useful
724 * for traversing directory structure to resolve a file/directory.
725 *
726 * @itr: iterator to initialize
727 * @parent: the iterator pointing at a directory entry in the
728 * parent directory of the directory to iterate
729 */
730static void fat_itr_child(fat_itr *itr, fat_itr *parent)
731{
732 fsdata *mydata = parent->fsdata; /* for silly macros */
733 unsigned clustnum = START(parent->dent);
734
735 assert(fat_itr_isdir(parent));
736
737 itr->fsdata = parent->fsdata;
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900738 itr->start_clust = clustnum;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400739 if (clustnum > 0) {
740 itr->clust = clustnum;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900741 itr->next_clust = clustnum;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300742 itr->is_root = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400743 } else {
744 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900745 itr->next_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300746 itr->is_root = 1;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400747 }
748 itr->dent = NULL;
749 itr->remaining = 0;
750 itr->last_cluster = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400751}
752
Anssi Hannula9b183582019-02-27 12:55:57 +0200753static void *next_cluster(fat_itr *itr, unsigned *nbytes)
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400754{
755 fsdata *mydata = itr->fsdata; /* for silly macros */
756 int ret;
757 u32 sect;
Anssi Hannula9b183582019-02-27 12:55:57 +0200758 u32 read_size;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400759
760 /* have we reached the end? */
761 if (itr->last_cluster)
762 return NULL;
763
Anssi Hannula9b183582019-02-27 12:55:57 +0200764 if (itr->is_root && itr->fsdata->fatsize != 32) {
765 /*
766 * The root directory is located before the data area and
767 * cannot be indexed using the regular unsigned cluster
768 * numbers (it may start at a "negative" cluster or not at a
769 * cluster boundary at all), so consider itr->next_clust to be
770 * a offset in cluster-sized units from the start of rootdir.
771 */
772 unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
773 unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
774 sect = itr->fsdata->rootdir_sect + sect_offset;
775 /* do not read past the end of rootdir */
776 read_size = min_t(u32, itr->fsdata->clust_size,
777 remaining_sects);
778 } else {
779 sect = clust_to_sect(itr->fsdata, itr->next_clust);
780 read_size = itr->fsdata->clust_size;
781 }
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400782
Anssi Hannula9b183582019-02-27 12:55:57 +0200783 debug("FAT read(sect=%d), clust_size=%d, read_size=%u, DIRENTSPERBLOCK=%zd\n",
784 sect, itr->fsdata->clust_size, read_size, DIRENTSPERBLOCK);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400785
786 /*
787 * NOTE: do_fat_read_at() had complicated logic to deal w/
788 * vfat names that span multiple clusters in the fat16 case,
789 * which get_dentfromdir() probably also needed (and was
790 * missing). And not entirely sure what fat32 didn't have
791 * the same issue.. We solve that by only caring about one
792 * dent at a time and iteratively constructing the vfat long
793 * name.
794 */
Anssi Hannula9b183582019-02-27 12:55:57 +0200795 ret = disk_read(sect, read_size, itr->block);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400796 if (ret < 0) {
797 debug("Error: reading block\n");
798 return NULL;
799 }
800
Anssi Hannula9b183582019-02-27 12:55:57 +0200801 *nbytes = read_size * itr->fsdata->sect_size;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900802 itr->clust = itr->next_clust;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400803 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900804 itr->next_clust++;
Anssi Hannula9b183582019-02-27 12:55:57 +0200805 if (itr->next_clust * itr->fsdata->clust_size >=
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400806 itr->fsdata->rootdir_size) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900807 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400808 itr->last_cluster = 1;
809 }
810 } else {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900811 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
812 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
813 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400814 itr->last_cluster = 1;
815 }
816 }
817
818 return itr->block;
819}
820
821static dir_entry *next_dent(fat_itr *itr)
822{
823 if (itr->remaining == 0) {
Anssi Hannula9b183582019-02-27 12:55:57 +0200824 unsigned nbytes;
825 struct dir_entry *dent = next_cluster(itr, &nbytes);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400826
827 /* have we reached the last cluster? */
AKASHI Takahirof528c142018-09-11 15:59:00 +0900828 if (!dent) {
829 /* a sign for no more entries left */
830 itr->dent = NULL;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400831 return NULL;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900832 }
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400833
834 itr->remaining = nbytes / sizeof(dir_entry) - 1;
835 itr->dent = dent;
836 } else {
837 itr->remaining--;
838 itr->dent++;
839 }
840
841 /* have we reached the last valid entry? */
842 if (itr->dent->name[0] == 0)
843 return NULL;
844
845 return itr->dent;
846}
847
848static dir_entry *extract_vfat_name(fat_itr *itr)
849{
850 struct dir_entry *dent = itr->dent;
851 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
852 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
853 int n = 0;
854
855 while (seqn--) {
856 char buf[13];
857 int idx = 0;
858
859 slot2str((dir_slot *)dent, buf, &idx);
860
Patrick Wildt8b021bb2018-11-26 15:58:13 +0100861 if (n + idx >= sizeof(itr->l_name))
862 return NULL;
863
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400864 /* shift accumulated long-name up and copy new part in: */
865 memmove(itr->l_name + idx, itr->l_name, n);
866 memcpy(itr->l_name, buf, idx);
867 n += idx;
868
869 dent = next_dent(itr);
870 if (!dent)
871 return NULL;
872 }
873
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900874 /*
875 * We are now at the short file name entry.
876 * If it is marked as deleted, just skip it.
877 */
878 if (dent->name[0] == DELETED_FLAG ||
879 dent->name[0] == aRING)
880 return NULL;
881
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400882 itr->l_name[n] = '\0';
883
884 chksum = mkcksum(dent->name, dent->ext);
885
886 /* checksum mismatch could mean deleted file, etc.. skip it: */
887 if (chksum != alias_checksum) {
888 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
889 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
890 return NULL;
891 }
892
893 return dent;
894}
895
896/**
897 * fat_itr_next() - step to the next entry in a directory
898 *
899 * Must be called once on a new iterator before the cursor is valid.
900 *
901 * @itr: the iterator to iterate
902 * @return boolean, 1 if success or 0 if no more entries in the
903 * current directory
904 */
905static int fat_itr_next(fat_itr *itr)
906{
907 dir_entry *dent;
908
909 itr->name = NULL;
910
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900911 /*
912 * One logical directory entry consist of following slots:
913 * name[0] Attributes
914 * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT
915 * ...
916 * dent[N - 2]: LFN[1] 2 ATTR_VFAT
917 * dent[N - 1]: LFN[0] 1 ATTR_VFAT
918 * dent[N]: SFN ATTR_ARCH
919 */
920
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400921 while (1) {
922 dent = next_dent(itr);
923 if (!dent)
924 return 0;
925
926 if (dent->name[0] == DELETED_FLAG ||
927 dent->name[0] == aRING)
928 continue;
929
930 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynen8bad6cb2018-01-05 02:45:21 +0200931 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400932 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900933 /* long file name */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400934 dent = extract_vfat_name(itr);
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900935 /*
936 * If succeeded, dent has a valid short file
937 * name entry for the current entry.
938 * If failed, itr points to a current bogus
939 * entry. So after fetching a next one,
940 * it may have a short file name entry
941 * for this bogus entry so that we can still
942 * check for a short name.
943 */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400944 if (!dent)
945 continue;
946 itr->name = itr->l_name;
947 break;
948 } else {
949 /* Volume label or VFAT entry, skip */
950 continue;
951 }
Christian Gmeiner1788a962020-06-09 09:09:07 +0200952 }
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400953
AKASHI Takahiro39606d42019-11-26 17:29:31 +0900954 /* short file name */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400955 break;
956 }
957
958 get_name(dent, itr->s_name);
959 if (!itr->name)
960 itr->name = itr->s_name;
961
962 return 1;
963}
964
965/**
966 * fat_itr_isdir() - is current cursor position pointing to a directory
967 *
968 * @itr: the iterator
969 * @return true if cursor is at a directory
970 */
971static int fat_itr_isdir(fat_itr *itr)
972{
973 return !!(itr->dent->attr & ATTR_DIR);
974}
975
976/*
977 * Helpers:
978 */
979
980#define TYPE_FILE 0x1
981#define TYPE_DIR 0x2
982#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
983
984/**
985 * fat_itr_resolve() - traverse directory structure to resolve the
986 * requested path.
987 *
988 * Traverse directory structure to the requested path. If the specified
989 * path is to a directory, this will descend into the directory and
990 * leave it iterator at the start of the directory. If the path is to a
991 * file, it will leave the iterator in the parent directory with current
992 * cursor at file's entry in the directory.
993 *
994 * @itr: iterator initialized to root
995 * @path: the requested path
996 * @type: bitmask of allowable file types
997 * @return 0 on success or -errno
998 */
999static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
1000{
1001 const char *next;
1002
1003 /* chomp any extra leading slashes: */
1004 while (path[0] && ISDIRDELIM(path[0]))
1005 path++;
1006
1007 /* are we at the end? */
1008 if (strlen(path) == 0) {
1009 if (!(type & TYPE_DIR))
1010 return -ENOENT;
1011 return 0;
1012 }
1013
1014 /* find length of next path entry: */
1015 next = path;
1016 while (next[0] && !ISDIRDELIM(next[0]))
1017 next++;
1018
AKASHI Takahirob94b6be2018-09-11 15:58:59 +09001019 if (itr->is_root) {
1020 /* root dir doesn't have "." nor ".." */
1021 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
1022 (((next - path) == 2) && !strncmp(path, "..", 2))) {
1023 /* point back to itself */
1024 itr->clust = itr->fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +09001025 itr->next_clust = itr->fsdata->root_cluster;
AKASHI Takahirob94b6be2018-09-11 15:58:59 +09001026 itr->dent = NULL;
1027 itr->remaining = 0;
1028 itr->last_cluster = 0;
1029
1030 if (next[0] == 0) {
1031 if (type & TYPE_DIR)
1032 return 0;
1033 else
1034 return -ENOENT;
1035 }
1036
1037 return fat_itr_resolve(itr, next, type);
1038 }
1039 }
1040
Rob Clarkc6e3baa2017-09-09 13:15:53 -04001041 while (fat_itr_next(itr)) {
1042 int match = 0;
1043 unsigned n = max(strlen(itr->name), (size_t)(next - path));
1044
1045 /* check both long and short name: */
1046 if (!strncasecmp(path, itr->name, n))
1047 match = 1;
1048 else if (itr->name != itr->s_name &&
1049 !strncasecmp(path, itr->s_name, n))
1050 match = 1;
1051
1052 if (!match)
1053 continue;
1054
1055 if (fat_itr_isdir(itr)) {
1056 /* recurse into directory: */
1057 fat_itr_child(itr, itr);
1058 return fat_itr_resolve(itr, next, type);
1059 } else if (next[0]) {
1060 /*
1061 * If next is not empty then we have a case
1062 * like: /path/to/realfile/nonsense
1063 */
1064 debug("bad trailing path: %s\n", next);
1065 return -ENOENT;
1066 } else if (!(type & TYPE_FILE)) {
1067 return -ENOTDIR;
1068 } else {
1069 return 0;
1070 }
1071 }
1072
1073 return -ENOENT;
1074}
1075
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001076int file_fat_detectfs(void)
wdenk71f95112003-06-15 22:40:42 +00001077{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001078 boot_sector bs;
1079 volume_info volinfo;
1080 int fatsize;
1081 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001082
Wolfgang Denk7385c282010-07-19 11:37:00 +02001083 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001084 printf("No current device\n");
1085 return 1;
1086 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001087
Simon Glassfc843a02017-05-17 03:25:30 -06001088#if defined(CONFIG_IDE) || \
Simon Glass10e40d52017-06-14 21:28:25 -06001089 defined(CONFIG_SATA) || \
Simon Glassc649e3c2016-05-01 11:36:02 -06001090 defined(CONFIG_SCSI) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001091 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001092 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001093 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001094 switch (cur_dev->if_type) {
1095 case IF_TYPE_IDE:
1096 printf("IDE");
1097 break;
1098 case IF_TYPE_SATA:
1099 printf("SATA");
1100 break;
1101 case IF_TYPE_SCSI:
1102 printf("SCSI");
1103 break;
1104 case IF_TYPE_ATAPI:
1105 printf("ATAPI");
1106 break;
1107 case IF_TYPE_USB:
1108 printf("USB");
1109 break;
1110 case IF_TYPE_DOC:
1111 printf("DOC");
1112 break;
1113 case IF_TYPE_MMC:
1114 printf("MMC");
1115 break;
1116 default:
1117 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001118 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001119
Simon Glassbcce53d2016-02-29 15:25:51 -07001120 printf("\n Device %d: ", cur_dev->devnum);
wdenk7205e402003-09-10 22:30:53 +00001121 dev_print(cur_dev);
1122#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001123
1124 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001125 printf("\nNo valid FAT fs found\n");
1126 return 1;
1127 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001128
1129 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001130 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001131 volinfo.fs_type[5] = '\0';
1132
Stephen Warren461f86e2012-10-17 06:44:57 +00001133 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001134
wdenk7205e402003-09-10 22:30:53 +00001135 return 0;
wdenk71f95112003-06-15 22:40:42 +00001136}
1137
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001138int fat_exists(const char *filename)
1139{
Rob Clark8eafae22017-09-09 13:15:54 -04001140 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001141 fat_itr *itr;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001142 int ret;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001143
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001144 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001145 if (!itr)
1146 return 0;
Rob Clark8eafae22017-09-09 13:15:54 -04001147 ret = fat_itr_root(itr, &fsdata);
1148 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001149 goto out;
Rob Clark8eafae22017-09-09 13:15:54 -04001150
1151 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clark725ffdb2017-09-12 16:40:01 -04001152 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001153out:
Tom Rini24600982017-09-22 07:37:43 -04001154 free(itr);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001155 return ret == 0;
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001156}
1157
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001158int fat_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -06001159{
Rob Clark8eafae22017-09-09 13:15:54 -04001160 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001161 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001162 int ret;
1163
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001164 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001165 if (!itr)
1166 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001167 ret = fat_itr_root(itr, &fsdata);
1168 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001169 goto out_free_itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001170
1171 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1172 if (ret) {
1173 /*
1174 * Directories don't have size, but fs_size() is not
1175 * expected to fail if passed a directory path:
1176 */
Rob Clark725ffdb2017-09-12 16:40:01 -04001177 free(fsdata.fatbuf);
Andrew F. Davisd0cd30e2019-05-16 09:34:31 -05001178 ret = fat_itr_root(itr, &fsdata);
1179 if (ret)
1180 goto out_free_itr;
1181 ret = fat_itr_resolve(itr, filename, TYPE_DIR);
1182 if (!ret)
Rob Clark8eafae22017-09-09 13:15:54 -04001183 *size = 0;
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001184 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001185 }
1186
1187 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001188out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001189 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001190out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001191 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001192 return ret;
Stephen Warrencf659812014-06-11 12:47:26 -06001193}
1194
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001195int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1196 loff_t maxsize, loff_t *actread)
wdenk71f95112003-06-15 22:40:42 +00001197{
Rob Clark8eafae22017-09-09 13:15:54 -04001198 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001199 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001200 int ret;
1201
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001202 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001203 if (!itr)
1204 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001205 ret = fat_itr_root(itr, &fsdata);
1206 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001207 goto out_free_itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001208
1209 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1210 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001211 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001212
Andreas Dannenberg287c04e2018-08-13 21:35:15 -05001213 debug("reading %s at pos %llu\n", filename, pos);
Tien Fong Cheee48485f2019-02-11 14:56:20 +08001214
1215 /* For saving default max clustersize memory allocated to malloc pool */
1216 dir_entry *dentptr = itr->dent;
1217
Tien Fong Cheee48485f2019-02-11 14:56:20 +08001218 ret = get_contents(&fsdata, dentptr, pos, buffer, maxsize, actread);
Rob Clark725ffdb2017-09-12 16:40:01 -04001219
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001220out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001221 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001222out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001223 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001224 return ret;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001225}
1226
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001227int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001228{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001229 loff_t actread;
1230 int ret;
1231
1232 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1233 if (ret)
1234 return ret;
1235 else
1236 return actread;
wdenk71f95112003-06-15 22:40:42 +00001237}
Simon Glasse6d52412012-12-26 09:53:33 +00001238
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001239int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1240 loff_t *actread)
Simon Glasse6d52412012-12-26 09:53:33 +00001241{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001242 int ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001243
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001244 ret = file_fat_read_at(filename, offset, buf, len, actread);
1245 if (ret)
Simon Glasse6d52412012-12-26 09:53:33 +00001246 printf("** Unable to read file %s **\n", filename);
Simon Glasse6d52412012-12-26 09:53:33 +00001247
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001248 return ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001249}
1250
Rob Clark1f403662017-09-09 13:15:56 -04001251typedef struct {
1252 struct fs_dir_stream parent;
1253 struct fs_dirent dirent;
1254 fsdata fsdata;
1255 fat_itr itr;
1256} fat_dir;
1257
1258int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1259{
Neil Armstrong34dd8532017-11-24 09:54:41 +01001260 fat_dir *dir;
Rob Clark1f403662017-09-09 13:15:56 -04001261 int ret;
1262
Neil Armstrong34dd8532017-11-24 09:54:41 +01001263 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001264 if (!dir)
1265 return -ENOMEM;
Neil Armstrong34dd8532017-11-24 09:54:41 +01001266 memset(dir, 0, sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001267
1268 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1269 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001270 goto fail_free_dir;
Rob Clark1f403662017-09-09 13:15:56 -04001271
1272 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1273 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001274 goto fail_free_both;
Rob Clark1f403662017-09-09 13:15:56 -04001275
1276 *dirsp = (struct fs_dir_stream *)dir;
1277 return 0;
1278
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001279fail_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001280 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001281fail_free_dir:
Rob Clark1f403662017-09-09 13:15:56 -04001282 free(dir);
1283 return ret;
1284}
1285
1286int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1287{
1288 fat_dir *dir = (fat_dir *)dirs;
1289 struct fs_dirent *dent = &dir->dirent;
1290
1291 if (!fat_itr_next(&dir->itr))
1292 return -ENOENT;
1293
1294 memset(dent, 0, sizeof(*dent));
1295 strcpy(dent->name, dir->itr.name);
1296
1297 if (fat_itr_isdir(&dir->itr)) {
1298 dent->type = FS_DT_DIR;
1299 } else {
1300 dent->type = FS_DT_REG;
1301 dent->size = FAT2CPU32(dir->itr.dent->size);
1302 }
1303
1304 *dentp = dent;
1305
1306 return 0;
1307}
1308
1309void fat_closedir(struct fs_dir_stream *dirs)
1310{
1311 fat_dir *dir = (fat_dir *)dirs;
Rob Clark725ffdb2017-09-12 16:40:01 -04001312 free(dir->fsdata.fatbuf);
Rob Clark1f403662017-09-09 13:15:56 -04001313 free(dir);
1314}
1315
Simon Glasse6d52412012-12-26 09:53:33 +00001316void fat_close(void)
1317{
1318}