blob: ac8913e71929b1a847d09fdc1d69552f72afc962 [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>
wdenk71f95112003-06-15 22:40:42 +000017#include <asm/byteorder.h>
wdenk7205e402003-09-10 22:30:53 +000018#include <part.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000019#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060020#include <memalign.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000021#include <linux/compiler.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000022#include <linux/ctype.h>
wdenk71f95112003-06-15 22:40:42 +000023
wdenk71f95112003-06-15 22:40:42 +000024/*
Rob Clark21a24c32017-09-09 13:15:59 -040025 * Convert a string to lowercase. Converts at most 'len' characters,
26 * 'len' may be larger than the length of 'str' if 'str' is NULL
27 * terminated.
wdenk71f95112003-06-15 22:40:42 +000028 */
Rob Clark21a24c32017-09-09 13:15:59 -040029static void downcase(char *str, size_t len)
wdenk71f95112003-06-15 22:40:42 +000030{
Rob Clark21a24c32017-09-09 13:15:59 -040031 while (*str != '\0' && len--) {
Richard Genoudfb7e16c2012-12-13 00:47:36 +000032 *str = tolower(*str);
wdenk71f95112003-06-15 22:40:42 +000033 str++;
34 }
35}
36
Simon Glass4101f682016-02-29 15:25:34 -070037static struct blk_desc *cur_dev;
Kyle Moffett9813b752011-12-21 07:08:10 +000038static disk_partition_t cur_part_info;
Wolfgang Denk7385c282010-07-19 11:37:00 +020039
Kyle Moffett9813b752011-12-21 07:08:10 +000040#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk7205e402003-09-10 22:30:53 +000041#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk66c2d732010-07-19 11:36:57 +020042#define DOS_FS32_TYPE_OFFSET 0x52
wdenk71f95112003-06-15 22:40:42 +000043
Kyle Moffett9813b752011-12-21 07:08:10 +000044static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk71f95112003-06-15 22:40:42 +000045{
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020046 ulong ret;
47
Simon Glass2a981dc2016-02-29 15:25:52 -070048 if (!cur_dev)
wdenk7205e402003-09-10 22:30:53 +000049 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020050
Simon Glass2a981dc2016-02-29 15:25:52 -070051 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020052
Tom Rini42a9f142017-08-14 21:02:08 -040053 if (ret != nr_blocks)
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020054 return -1;
55
56 return ret;
wdenk71f95112003-06-15 22:40:42 +000057}
58
Simon Glass4101f682016-02-29 15:25:34 -070059int fat_set_blk_dev(struct blk_desc *dev_desc, disk_partition_t *info)
wdenk71f95112003-06-15 22:40:42 +000060{
Eric Nelson9a800ac2012-04-11 04:08:53 +000061 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk7205e402003-09-10 22:30:53 +000062
Stephen Warren5e8f9832012-10-17 06:44:59 +000063 cur_dev = dev_desc;
64 cur_part_info = *info;
Kyle Moffett9813b752011-12-21 07:08:10 +000065
66 /* Make sure it has a valid FAT header */
67 if (disk_read(0, 1, buffer) != 1) {
68 cur_dev = NULL;
69 return -1;
70 }
71
72 /* Check if it's actually a DOS volume */
73 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
74 cur_dev = NULL;
75 return -1;
76 }
77
78 /* Check for FAT12/FAT16/FAT32 filesystem */
79 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
80 return 0;
81 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
82 return 0;
83
84 cur_dev = NULL;
85 return -1;
wdenk71f95112003-06-15 22:40:42 +000086}
87
Simon Glass4101f682016-02-29 15:25:34 -070088int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren5e8f9832012-10-17 06:44:59 +000089{
90 disk_partition_t info;
91
92 /* First close any currently found FAT filesystem */
93 cur_dev = NULL;
94
95 /* Read the partition table, if present */
Simon Glass3e8bd462016-02-29 15:25:48 -070096 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren5e8f9832012-10-17 06:44:59 +000097 if (part_no != 0) {
98 printf("** Partition %d not valid on device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -070099 part_no, dev_desc->devnum);
Stephen Warren5e8f9832012-10-17 06:44:59 +0000100 return -1;
101 }
102
103 info.start = 0;
104 info.size = dev_desc->lba;
105 info.blksz = dev_desc->blksz;
106 info.name[0] = 0;
107 info.type[0] = 0;
108 info.bootable = 0;
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100109#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren5e8f9832012-10-17 06:44:59 +0000110 info.uuid[0] = 0;
111#endif
112 }
113
114 return fat_set_blk_dev(dev_desc, &info);
115}
Kyle Moffett9813b752011-12-21 07:08:10 +0000116
wdenk71f95112003-06-15 22:40:42 +0000117/*
wdenk71f95112003-06-15 22:40:42 +0000118 * Extract zero terminated short name from a directory entry.
119 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200120static void get_name(dir_entry *dirent, char *s_name)
wdenk71f95112003-06-15 22:40:42 +0000121{
122 char *ptr;
123
Wolfgang Denk7385c282010-07-19 11:37:00 +0200124 memcpy(s_name, dirent->name, 8);
wdenk71f95112003-06-15 22:40:42 +0000125 s_name[8] = '\0';
126 ptr = s_name;
127 while (*ptr && *ptr != ' ')
128 ptr++;
Rob Clark21a24c32017-09-09 13:15:59 -0400129 if (dirent->lcase & CASE_LOWER_BASE)
130 downcase(s_name, (unsigned)(ptr - s_name));
wdenk71f95112003-06-15 22:40:42 +0000131 if (dirent->ext[0] && dirent->ext[0] != ' ') {
Rob Clark21a24c32017-09-09 13:15:59 -0400132 *ptr++ = '.';
Wolfgang Denk7385c282010-07-19 11:37:00 +0200133 memcpy(ptr, dirent->ext, 3);
Rob Clark21a24c32017-09-09 13:15:59 -0400134 if (dirent->lcase & CASE_LOWER_EXT)
135 downcase(ptr, 3);
wdenk71f95112003-06-15 22:40:42 +0000136 ptr[3] = '\0';
137 while (*ptr && *ptr != ' ')
138 ptr++;
139 }
140 *ptr = '\0';
141 if (*s_name == DELETED_FLAG)
142 *s_name = '\0';
143 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100144 *s_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000145}
146
Stefan Brünsb8948d22016-12-17 00:27:51 +0100147static int flush_dirty_fat_buffer(fsdata *mydata);
148#if !defined(CONFIG_FAT_WRITE)
149/* Stub for read only operation */
150int flush_dirty_fat_buffer(fsdata *mydata)
151{
152 (void)(mydata);
153 return 0;
154}
155#endif
156
wdenk71f95112003-06-15 22:40:42 +0000157/*
158 * Get the entry at index 'entry' in a FAT (12/16/32) table.
159 * On failure 0x00 is returned.
160 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200161static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000162{
163 __u32 bufnum;
Stefan Brünsb352cae2017-01-26 20:22:36 +0000164 __u32 offset, off8;
wdenk71f95112003-06-15 22:40:42 +0000165 __u32 ret = 0x00;
166
Stefan Brünsb8948d22016-12-17 00:27:51 +0100167 if (CHECK_CLUST(entry, mydata->fatsize)) {
168 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
169 return ret;
170 }
171
wdenk71f95112003-06-15 22:40:42 +0000172 switch (mydata->fatsize) {
173 case 32:
174 bufnum = entry / FAT32BUFSIZE;
175 offset = entry - bufnum * FAT32BUFSIZE;
176 break;
177 case 16:
178 bufnum = entry / FAT16BUFSIZE;
179 offset = entry - bufnum * FAT16BUFSIZE;
180 break;
181 case 12:
182 bufnum = entry / FAT12BUFSIZE;
183 offset = entry - bufnum * FAT12BUFSIZE;
184 break;
185
186 default:
187 /* Unsupported FAT size */
188 return ret;
189 }
190
Stefan Brünsb8948d22016-12-17 00:27:51 +0100191 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk7385c282010-07-19 11:37:00 +0200192 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200193
wdenk71f95112003-06-15 22:40:42 +0000194 /* Read a new block of FAT entries into the cache. */
195 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000196 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000197 __u8 *bufptr = mydata->fatbuf;
198 __u32 fatlength = mydata->fatlength;
199 __u32 startblock = bufnum * FATBUFBLOCKS;
200
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100201 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau8006dd22012-07-20 15:19:29 +0200202 if (startblock + getsize > fatlength)
203 getsize = fatlength - startblock;
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000204
wdenk71f95112003-06-15 22:40:42 +0000205 startblock += mydata->fat_sect; /* Offset from start of disk */
206
Stefan Brünsb8948d22016-12-17 00:27:51 +0100207 /* Write back the fatbuf to the disk */
208 if (flush_dirty_fat_buffer(mydata) < 0)
209 return -1;
210
wdenk71f95112003-06-15 22:40:42 +0000211 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200212 debug("Error reading FAT blocks\n");
wdenk71f95112003-06-15 22:40:42 +0000213 return ret;
214 }
215 mydata->fatbufnum = bufnum;
216 }
217
218 /* Get the actual entry from the table */
219 switch (mydata->fatsize) {
220 case 32:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200221 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000222 break;
223 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200224 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000225 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200226 case 12:
Stefan Brünsb352cae2017-01-26 20:22:36 +0000227 off8 = (offset * 3) / 2;
228 /* fatbut + off8 may be unaligned, read in byte granularity */
229 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk71f95112003-06-15 22:40:42 +0000230
Stefan Brüns8d48c922016-12-17 03:55:10 +0100231 if (offset & 0x1)
232 ret >>= 4;
233 ret &= 0xfff;
wdenk71f95112003-06-15 22:40:42 +0000234 }
Stefan Brünsb8948d22016-12-17 00:27:51 +0100235 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
236 mydata->fatsize, ret, entry, offset);
wdenk71f95112003-06-15 22:40:42 +0000237
238 return ret;
239}
240
wdenk71f95112003-06-15 22:40:42 +0000241/*
242 * Read at most 'size' bytes from the specified cluster into 'buffer'.
243 * Return 0 on success, -1 otherwise.
244 */
245static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200246get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000247{
Erik Hansen3f270f42011-03-24 10:15:37 +0100248 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000249 __u32 startsect;
Kyle Moffett46236b12011-12-20 07:41:13 +0000250 int ret;
wdenk71f95112003-06-15 22:40:42 +0000251
252 if (clustnum > 0) {
Rob Clark265edc02017-09-09 13:16:00 -0400253 startsect = clust_to_sect(mydata, clustnum);
wdenk71f95112003-06-15 22:40:42 +0000254 } else {
255 startsect = mydata->rootdir_sect;
256 }
257
Wolfgang Denk7385c282010-07-19 11:37:00 +0200258 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
259
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200260 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson9a800ac2012-04-11 04:08:53 +0000261 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200262
Heinrich Schuchardt1c381ce2018-09-13 19:42:47 +0200263 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200264
265 while (size >= mydata->sect_size) {
266 ret = disk_read(startsect++, 1, tmpbuf);
267 if (ret != 1) {
268 debug("Error reading data (got %d)\n", ret);
269 return -1;
270 }
271
272 memcpy(buffer, tmpbuf, mydata->sect_size);
273 buffer += mydata->sect_size;
274 size -= mydata->sect_size;
275 }
276 } else {
Sergei Shtylyovac497772011-08-08 09:38:33 +0000277 idx = size / mydata->sect_size;
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200278 ret = disk_read(startsect, idx, buffer);
279 if (ret != idx) {
280 debug("Error reading data (got %d)\n", ret);
281 return -1;
282 }
283 startsect += idx;
284 idx *= mydata->sect_size;
285 buffer += idx;
286 size -= idx;
287 }
288 if (size) {
289 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
290
291 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett46236b12011-12-20 07:41:13 +0000292 if (ret != 1) {
293 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000294 return -1;
wdenk71f95112003-06-15 22:40:42 +0000295 }
wdenk7205e402003-09-10 22:30:53 +0000296
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200297 memcpy(buffer, tmpbuf, size);
wdenk71f95112003-06-15 22:40:42 +0000298 }
299
300 return 0;
301}
302
wdenk71f95112003-06-15 22:40:42 +0000303/*
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000304 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk71f95112003-06-15 22:40:42 +0000305 * into 'buffer'.
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800306 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
wdenk71f95112003-06-15 22:40:42 +0000307 */
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000308__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
309 __aligned(ARCH_DMA_MINALIGN);
310
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800311static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
312 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk71f95112003-06-15 22:40:42 +0000313{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800314 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000315 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000316 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000317 __u32 endclust, newclust;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800318 loff_t actsize;
wdenk71f95112003-06-15 22:40:42 +0000319
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800320 *gotsize = 0;
321 debug("Filesize: %llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000322
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000323 if (pos >= filesize) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800324 debug("Read position past EOF: %llu\n", pos);
325 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000326 }
327
328 if (maxsize > 0 && filesize > pos + maxsize)
329 filesize = pos + maxsize;
wdenk71f95112003-06-15 22:40:42 +0000330
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800331 debug("%llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000332
Wolfgang Denk7385c282010-07-19 11:37:00 +0200333 actsize = bytesperclust;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000334
335 /* go to cluster at pos */
336 while (actsize <= pos) {
337 curclust = get_fatent(mydata, curclust);
338 if (CHECK_CLUST(curclust, mydata->fatsize)) {
339 debug("curclust: 0x%x\n", curclust);
340 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800341 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000342 }
343 actsize += bytesperclust;
344 }
345
346 /* actsize > pos */
347 actsize -= bytesperclust;
348 filesize -= actsize;
349 pos -= actsize;
350
351 /* align to beginning of next cluster if any */
352 if (pos) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800353 actsize = min(filesize, (loff_t)bytesperclust);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000354 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
355 (int)actsize) != 0) {
356 printf("Error reading cluster\n");
357 return -1;
358 }
359 filesize -= actsize;
360 actsize -= pos;
361 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800362 *gotsize += actsize;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000363 if (!filesize)
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800364 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000365 buffer += actsize;
366
367 curclust = get_fatent(mydata, curclust);
368 if (CHECK_CLUST(curclust, mydata->fatsize)) {
369 debug("curclust: 0x%x\n", curclust);
370 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800371 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000372 }
373 }
374
375 actsize = bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200376 endclust = curclust;
377
wdenk71f95112003-06-15 22:40:42 +0000378 do {
wdenk7205e402003-09-10 22:30:53 +0000379 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200380 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000381 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200382 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000383 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100384 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200385 debug("curclust: 0x%x\n", newclust);
386 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800387 return 0;
wdenk7205e402003-09-10 22:30:53 +0000388 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200389 endclust = newclust;
390 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000391 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200392
wdenk7205e402003-09-10 22:30:53 +0000393 /* get remaining bytes */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200394 actsize = filesize;
Benoît Thébaudeau0880e5b2012-07-20 15:21:37 +0200395 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200396 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000397 return -1;
398 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800399 *gotsize += actsize;
400 return 0;
wdenk7205e402003-09-10 22:30:53 +0000401getit:
402 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200403 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000404 return -1;
405 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800406 *gotsize += (int)actsize;
wdenk7205e402003-09-10 22:30:53 +0000407 filesize -= actsize;
408 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200409
wdenk7205e402003-09-10 22:30:53 +0000410 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100411 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200412 debug("curclust: 0x%x\n", curclust);
413 printf("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800414 return 0;
wdenk71f95112003-06-15 22:40:42 +0000415 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200416 actsize = bytesperclust;
417 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000418 } while (1);
419}
420
wdenk71f95112003-06-15 22:40:42 +0000421/*
422 * Extract the file name information from 'slotptr' into 'l_name',
423 * starting at l_name[*idx].
424 * Return 1 if terminator (zero byte) is found, 0 otherwise.
425 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200426static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000427{
428 int j;
429
430 for (j = 0; j <= 8; j += 2) {
431 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200432 if (l_name[*idx] == 0x00)
433 return 1;
wdenk71f95112003-06-15 22:40:42 +0000434 (*idx)++;
435 }
436 for (j = 0; j <= 10; j += 2) {
437 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200438 if (l_name[*idx] == 0x00)
439 return 1;
wdenk71f95112003-06-15 22:40:42 +0000440 (*idx)++;
441 }
442 for (j = 0; j <= 2; j += 2) {
443 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200444 if (l_name[*idx] == 0x00)
445 return 1;
wdenk71f95112003-06-15 22:40:42 +0000446 (*idx)++;
447 }
448
449 return 0;
450}
451
wdenk71f95112003-06-15 22:40:42 +0000452/* Calculate short name checksum */
Marek Vasutff04f6d2012-10-09 07:20:22 +0000453static __u8 mkcksum(const char name[8], const char ext[3])
wdenk71f95112003-06-15 22:40:42 +0000454{
455 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200456
wdenk71f95112003-06-15 22:40:42 +0000457 __u8 ret = 0;
458
Marek Vasut6ad77d82013-01-11 03:35:48 +0000459 for (i = 0; i < 8; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000460 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut6ad77d82013-01-11 03:35:48 +0000461 for (i = 0; i < 3; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000462 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk71f95112003-06-15 22:40:42 +0000463
464 return ret;
465}
wdenk71f95112003-06-15 22:40:42 +0000466
467/*
wdenk71f95112003-06-15 22:40:42 +0000468 * Read boot sector and volume info from a FAT filesystem
469 */
470static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200471read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000472{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000473 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000474 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000475 int ret = 0;
476
477 if (cur_dev == NULL) {
478 debug("Error: no device selected\n");
479 return -1;
480 }
481
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +0300482 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000483 if (block == NULL) {
484 debug("Error: allocating block\n");
485 return -1;
486 }
wdenk71f95112003-06-15 22:40:42 +0000487
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200488 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200489 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000490 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000491 }
492
493 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200494 bs->reserved = FAT2CPU16(bs->reserved);
495 bs->fat_length = FAT2CPU16(bs->fat_length);
496 bs->secs_track = FAT2CPU16(bs->secs_track);
497 bs->heads = FAT2CPU16(bs->heads);
498 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000499
500 /* FAT32 entries */
501 if (bs->fat_length == 0) {
502 /* Assume FAT32 */
503 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200504 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000505 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200506 bs->info_sector = FAT2CPU16(bs->info_sector);
507 bs->backup_boot = FAT2CPU16(bs->backup_boot);
508 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000509 *fatsize = 32;
510 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200511 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000512 *fatsize = 0;
513 }
514 memcpy(volinfo, vistart, sizeof(volume_info));
515
wdenk71f95112003-06-15 22:40:42 +0000516 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200517 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000518 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000519 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500520 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000521 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000522 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000523 }
Tom Rix651351f2009-05-20 07:55:41 -0500524 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000525 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000526 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000527 }
528 }
529
Wolfgang Denk7385c282010-07-19 11:37:00 +0200530 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000531fail:
532 ret = -1;
533exit:
534 free(block);
535 return ret;
wdenk71f95112003-06-15 22:40:42 +0000536}
537
Rob Clark45449982017-09-09 13:15:52 -0400538static int get_fs_info(fsdata *mydata)
wdenk71f95112003-06-15 22:40:42 +0000539{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200540 boot_sector bs;
541 volume_info volinfo;
Rob Clark45449982017-09-09 13:15:52 -0400542 int ret;
wdenk71f95112003-06-15 22:40:42 +0000543
Rob Clark45449982017-09-09 13:15:52 -0400544 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
545 if (ret) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200546 debug("Error: reading boot sector\n");
Rob Clark45449982017-09-09 13:15:52 -0400547 return ret;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200548 }
549
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000550 if (mydata->fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200551 mydata->fatlength = bs.fat32_length;
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900552 mydata->total_sect = bs.total_sect;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000553 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200554 mydata->fatlength = bs.fat_length;
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900555 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
556 if (!mydata->total_sect)
557 mydata->total_sect = bs.total_sect;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000558 }
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900559 if (!mydata->total_sect) /* unlikely */
560 mydata->total_sect = (u32)cur_part_info.size;
wdenk71f95112003-06-15 22:40:42 +0000561
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900562 mydata->fats = bs.fats;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200563 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000564
Rob Clark45449982017-09-09 13:15:52 -0400565 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000566
Sergei Shtylyovac497772011-08-08 09:38:33 +0000567 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200568 mydata->clust_size = bs.cluster_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000569 if (mydata->sect_size != cur_part_info.blksz) {
570 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
571 mydata->sect_size, cur_part_info.blksz);
572 return -1;
573 }
Patrick Wildtcd80a4f2018-11-26 15:56:57 +0100574 if (mydata->clust_size == 0) {
575 printf("Error: FAT cluster size not set\n");
576 return -1;
577 }
578 if ((unsigned int)mydata->clust_size * mydata->sect_size >
579 MAX_CLUSTSIZE) {
580 printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
581 (unsigned int)mydata->clust_size * mydata->sect_size,
582 MAX_CLUSTSIZE);
583 return -1;
584 }
wdenk71f95112003-06-15 22:40:42 +0000585
Wolfgang Denk7385c282010-07-19 11:37:00 +0200586 if (mydata->fatsize == 32) {
587 mydata->data_begin = mydata->rootdir_sect -
588 (mydata->clust_size * 2);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400589 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200590 } else {
Rob Clark45449982017-09-09 13:15:52 -0400591 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
592 bs.dir_entries[0]) *
593 sizeof(dir_entry)) /
594 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200595 mydata->data_begin = mydata->rootdir_sect +
Rob Clark45449982017-09-09 13:15:52 -0400596 mydata->rootdir_size -
Wolfgang Denk7385c282010-07-19 11:37:00 +0200597 (mydata->clust_size * 2);
Rob Clark265edc02017-09-09 13:16:00 -0400598 mydata->root_cluster =
599 sect_to_clust(mydata, mydata->rootdir_sect);
wdenk71f95112003-06-15 22:40:42 +0000600 }
wdenk71f95112003-06-15 22:40:42 +0000601
Wolfgang Denk7385c282010-07-19 11:37:00 +0200602 mydata->fatbufnum = -1;
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200603 mydata->fat_dirty = 0;
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +0300604 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000605 if (mydata->fatbuf == NULL) {
606 debug("Error: allocating memory\n");
607 return -1;
608 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200609
Wolfgang Denk7385c282010-07-19 11:37:00 +0200610 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
611 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
612 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
613 "Data begins at: %d\n",
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400614 mydata->root_cluster,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200615 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000616 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
617 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
618 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200619
Rob Clark45449982017-09-09 13:15:52 -0400620 return 0;
621}
622
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400623
624/*
625 * Directory iterator, to simplify filesystem traversal
626 *
627 * Implements an iterator pattern to traverse directory tables,
628 * transparently handling directory tables split across multiple
629 * clusters, and the difference between FAT12/FAT16 root directory
630 * (contiguous) and subdirectories + FAT32 root (chained).
631 *
632 * Rough usage:
633 *
634 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
635 * // to traverse down to a subdirectory pointed to by
636 * // current iterator position:
637 * fat_itr_child(&itr, &itr);
638 * }
639 *
640 * For more complete example, see fat_itr_resolve()
641 */
642
643typedef struct {
644 fsdata *fsdata; /* filesystem parameters */
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900645 unsigned start_clust; /* first cluster */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400646 unsigned clust; /* current cluster */
AKASHI Takahirof528c142018-09-11 15:59:00 +0900647 unsigned next_clust; /* next cluster if remaining == 0 */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400648 int last_cluster; /* set once we've read last cluster */
649 int is_root; /* is iterator at root directory */
650 int remaining; /* remaining dent's in current cluster */
651
652 /* current iterator position values: */
653 dir_entry *dent; /* current directory entry */
654 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
655 char s_name[14]; /* short 8.3 name */
656 char *name; /* l_name if there is one, else s_name */
657
658 /* storage for current cluster in memory: */
659 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
660} fat_itr;
661
662static int fat_itr_isdir(fat_itr *itr);
663
664/**
665 * fat_itr_root() - initialize an iterator to start at the root
666 * directory
667 *
668 * @itr: iterator to initialize
669 * @fsdata: filesystem data for the partition
670 * @return 0 on success, else -errno
671 */
672static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
673{
674 if (get_fs_info(fsdata))
675 return -ENXIO;
676
677 itr->fsdata = fsdata;
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900678 itr->start_clust = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400679 itr->clust = fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900680 itr->next_clust = fsdata->root_cluster;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400681 itr->dent = NULL;
682 itr->remaining = 0;
683 itr->last_cluster = 0;
684 itr->is_root = 1;
685
686 return 0;
687}
688
689/**
690 * fat_itr_child() - initialize an iterator to descend into a sub-
691 * directory
692 *
693 * Initializes 'itr' to iterate the contents of the directory at
694 * the current cursor position of 'parent'. It is an error to
695 * call this if the current cursor of 'parent' is pointing at a
696 * regular file.
697 *
698 * Note that 'itr' and 'parent' can be the same pointer if you do
699 * not need to preserve 'parent' after this call, which is useful
700 * for traversing directory structure to resolve a file/directory.
701 *
702 * @itr: iterator to initialize
703 * @parent: the iterator pointing at a directory entry in the
704 * parent directory of the directory to iterate
705 */
706static void fat_itr_child(fat_itr *itr, fat_itr *parent)
707{
708 fsdata *mydata = parent->fsdata; /* for silly macros */
709 unsigned clustnum = START(parent->dent);
710
711 assert(fat_itr_isdir(parent));
712
713 itr->fsdata = parent->fsdata;
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900714 itr->start_clust = clustnum;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400715 if (clustnum > 0) {
716 itr->clust = clustnum;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900717 itr->next_clust = clustnum;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300718 itr->is_root = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400719 } else {
720 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900721 itr->next_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300722 itr->is_root = 1;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400723 }
724 itr->dent = NULL;
725 itr->remaining = 0;
726 itr->last_cluster = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400727}
728
729static void *next_cluster(fat_itr *itr)
730{
731 fsdata *mydata = itr->fsdata; /* for silly macros */
732 int ret;
733 u32 sect;
734
735 /* have we reached the end? */
736 if (itr->last_cluster)
737 return NULL;
738
Thomas RIENOESSLa68b0e12018-11-13 14:00:59 +0100739 if (itr->fsdata->fatsize != 32 && itr->is_root)
740 sect = mydata->rootdir_sect;
741 else
742 sect = clust_to_sect(itr->fsdata, itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400743
744 debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
745 sect, itr->fsdata->clust_size, DIRENTSPERBLOCK);
746
747 /*
748 * NOTE: do_fat_read_at() had complicated logic to deal w/
749 * vfat names that span multiple clusters in the fat16 case,
750 * which get_dentfromdir() probably also needed (and was
751 * missing). And not entirely sure what fat32 didn't have
752 * the same issue.. We solve that by only caring about one
753 * dent at a time and iteratively constructing the vfat long
754 * name.
755 */
756 ret = disk_read(sect, itr->fsdata->clust_size,
757 itr->block);
758 if (ret < 0) {
759 debug("Error: reading block\n");
760 return NULL;
761 }
762
AKASHI Takahirof528c142018-09-11 15:59:00 +0900763 itr->clust = itr->next_clust;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400764 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900765 itr->next_clust++;
766 sect = clust_to_sect(itr->fsdata, itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400767 if (sect - itr->fsdata->rootdir_sect >=
768 itr->fsdata->rootdir_size) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900769 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400770 itr->last_cluster = 1;
771 }
772 } else {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900773 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
774 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
775 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400776 itr->last_cluster = 1;
777 }
778 }
779
780 return itr->block;
781}
782
783static dir_entry *next_dent(fat_itr *itr)
784{
785 if (itr->remaining == 0) {
786 struct dir_entry *dent = next_cluster(itr);
787 unsigned nbytes = itr->fsdata->sect_size *
788 itr->fsdata->clust_size;
789
790 /* have we reached the last cluster? */
AKASHI Takahirof528c142018-09-11 15:59:00 +0900791 if (!dent) {
792 /* a sign for no more entries left */
793 itr->dent = NULL;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400794 return NULL;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900795 }
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400796
797 itr->remaining = nbytes / sizeof(dir_entry) - 1;
798 itr->dent = dent;
799 } else {
800 itr->remaining--;
801 itr->dent++;
802 }
803
804 /* have we reached the last valid entry? */
805 if (itr->dent->name[0] == 0)
806 return NULL;
807
808 return itr->dent;
809}
810
811static dir_entry *extract_vfat_name(fat_itr *itr)
812{
813 struct dir_entry *dent = itr->dent;
814 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
815 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
816 int n = 0;
817
818 while (seqn--) {
819 char buf[13];
820 int idx = 0;
821
822 slot2str((dir_slot *)dent, buf, &idx);
823
Patrick Wildt8b021bb2018-11-26 15:58:13 +0100824 if (n + idx >= sizeof(itr->l_name))
825 return NULL;
826
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400827 /* shift accumulated long-name up and copy new part in: */
828 memmove(itr->l_name + idx, itr->l_name, n);
829 memcpy(itr->l_name, buf, idx);
830 n += idx;
831
832 dent = next_dent(itr);
833 if (!dent)
834 return NULL;
835 }
836
837 itr->l_name[n] = '\0';
838
839 chksum = mkcksum(dent->name, dent->ext);
840
841 /* checksum mismatch could mean deleted file, etc.. skip it: */
842 if (chksum != alias_checksum) {
843 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
844 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
845 return NULL;
846 }
847
848 return dent;
849}
850
851/**
852 * fat_itr_next() - step to the next entry in a directory
853 *
854 * Must be called once on a new iterator before the cursor is valid.
855 *
856 * @itr: the iterator to iterate
857 * @return boolean, 1 if success or 0 if no more entries in the
858 * current directory
859 */
860static int fat_itr_next(fat_itr *itr)
861{
862 dir_entry *dent;
863
864 itr->name = NULL;
865
866 while (1) {
867 dent = next_dent(itr);
868 if (!dent)
869 return 0;
870
871 if (dent->name[0] == DELETED_FLAG ||
872 dent->name[0] == aRING)
873 continue;
874
875 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynen8bad6cb2018-01-05 02:45:21 +0200876 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400877 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
878 dent = extract_vfat_name(itr);
879 if (!dent)
880 continue;
881 itr->name = itr->l_name;
882 break;
883 } else {
884 /* Volume label or VFAT entry, skip */
885 continue;
886 }
887 }
888
889 break;
890 }
891
892 get_name(dent, itr->s_name);
893 if (!itr->name)
894 itr->name = itr->s_name;
895
896 return 1;
897}
898
899/**
900 * fat_itr_isdir() - is current cursor position pointing to a directory
901 *
902 * @itr: the iterator
903 * @return true if cursor is at a directory
904 */
905static int fat_itr_isdir(fat_itr *itr)
906{
907 return !!(itr->dent->attr & ATTR_DIR);
908}
909
910/*
911 * Helpers:
912 */
913
914#define TYPE_FILE 0x1
915#define TYPE_DIR 0x2
916#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
917
918/**
919 * fat_itr_resolve() - traverse directory structure to resolve the
920 * requested path.
921 *
922 * Traverse directory structure to the requested path. If the specified
923 * path is to a directory, this will descend into the directory and
924 * leave it iterator at the start of the directory. If the path is to a
925 * file, it will leave the iterator in the parent directory with current
926 * cursor at file's entry in the directory.
927 *
928 * @itr: iterator initialized to root
929 * @path: the requested path
930 * @type: bitmask of allowable file types
931 * @return 0 on success or -errno
932 */
933static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
934{
935 const char *next;
936
937 /* chomp any extra leading slashes: */
938 while (path[0] && ISDIRDELIM(path[0]))
939 path++;
940
941 /* are we at the end? */
942 if (strlen(path) == 0) {
943 if (!(type & TYPE_DIR))
944 return -ENOENT;
945 return 0;
946 }
947
948 /* find length of next path entry: */
949 next = path;
950 while (next[0] && !ISDIRDELIM(next[0]))
951 next++;
952
AKASHI Takahirob94b6be2018-09-11 15:58:59 +0900953 if (itr->is_root) {
954 /* root dir doesn't have "." nor ".." */
955 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
956 (((next - path) == 2) && !strncmp(path, "..", 2))) {
957 /* point back to itself */
958 itr->clust = itr->fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900959 itr->next_clust = itr->fsdata->root_cluster;
AKASHI Takahirob94b6be2018-09-11 15:58:59 +0900960 itr->dent = NULL;
961 itr->remaining = 0;
962 itr->last_cluster = 0;
963
964 if (next[0] == 0) {
965 if (type & TYPE_DIR)
966 return 0;
967 else
968 return -ENOENT;
969 }
970
971 return fat_itr_resolve(itr, next, type);
972 }
973 }
974
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400975 while (fat_itr_next(itr)) {
976 int match = 0;
977 unsigned n = max(strlen(itr->name), (size_t)(next - path));
978
979 /* check both long and short name: */
980 if (!strncasecmp(path, itr->name, n))
981 match = 1;
982 else if (itr->name != itr->s_name &&
983 !strncasecmp(path, itr->s_name, n))
984 match = 1;
985
986 if (!match)
987 continue;
988
989 if (fat_itr_isdir(itr)) {
990 /* recurse into directory: */
991 fat_itr_child(itr, itr);
992 return fat_itr_resolve(itr, next, type);
993 } else if (next[0]) {
994 /*
995 * If next is not empty then we have a case
996 * like: /path/to/realfile/nonsense
997 */
998 debug("bad trailing path: %s\n", next);
999 return -ENOENT;
1000 } else if (!(type & TYPE_FILE)) {
1001 return -ENOTDIR;
1002 } else {
1003 return 0;
1004 }
1005 }
1006
1007 return -ENOENT;
1008}
1009
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001010int file_fat_detectfs(void)
wdenk71f95112003-06-15 22:40:42 +00001011{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001012 boot_sector bs;
1013 volume_info volinfo;
1014 int fatsize;
1015 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001016
Wolfgang Denk7385c282010-07-19 11:37:00 +02001017 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001018 printf("No current device\n");
1019 return 1;
1020 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001021
Simon Glassfc843a02017-05-17 03:25:30 -06001022#if defined(CONFIG_IDE) || \
Simon Glass10e40d52017-06-14 21:28:25 -06001023 defined(CONFIG_SATA) || \
Simon Glassc649e3c2016-05-01 11:36:02 -06001024 defined(CONFIG_SCSI) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001025 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001026 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001027 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001028 switch (cur_dev->if_type) {
1029 case IF_TYPE_IDE:
1030 printf("IDE");
1031 break;
1032 case IF_TYPE_SATA:
1033 printf("SATA");
1034 break;
1035 case IF_TYPE_SCSI:
1036 printf("SCSI");
1037 break;
1038 case IF_TYPE_ATAPI:
1039 printf("ATAPI");
1040 break;
1041 case IF_TYPE_USB:
1042 printf("USB");
1043 break;
1044 case IF_TYPE_DOC:
1045 printf("DOC");
1046 break;
1047 case IF_TYPE_MMC:
1048 printf("MMC");
1049 break;
1050 default:
1051 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001052 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001053
Simon Glassbcce53d2016-02-29 15:25:51 -07001054 printf("\n Device %d: ", cur_dev->devnum);
wdenk7205e402003-09-10 22:30:53 +00001055 dev_print(cur_dev);
1056#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001057
1058 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001059 printf("\nNo valid FAT fs found\n");
1060 return 1;
1061 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001062
1063 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001064 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001065 volinfo.fs_type[5] = '\0';
1066
Stephen Warren461f86e2012-10-17 06:44:57 +00001067 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001068
wdenk7205e402003-09-10 22:30:53 +00001069 return 0;
wdenk71f95112003-06-15 22:40:42 +00001070}
1071
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001072int fat_exists(const char *filename)
1073{
Rob Clark8eafae22017-09-09 13:15:54 -04001074 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001075 fat_itr *itr;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001076 int ret;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001077
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001078 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001079 if (!itr)
1080 return 0;
Rob Clark8eafae22017-09-09 13:15:54 -04001081 ret = fat_itr_root(itr, &fsdata);
1082 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001083 goto out;
Rob Clark8eafae22017-09-09 13:15:54 -04001084
1085 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clark725ffdb2017-09-12 16:40:01 -04001086 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001087out:
Tom Rini24600982017-09-22 07:37:43 -04001088 free(itr);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001089 return ret == 0;
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001090}
1091
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001092int fat_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -06001093{
Rob Clark8eafae22017-09-09 13:15:54 -04001094 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001095 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001096 int ret;
1097
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001098 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001099 if (!itr)
1100 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001101 ret = fat_itr_root(itr, &fsdata);
1102 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001103 goto out_free_itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001104
1105 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1106 if (ret) {
1107 /*
1108 * Directories don't have size, but fs_size() is not
1109 * expected to fail if passed a directory path:
1110 */
Rob Clark725ffdb2017-09-12 16:40:01 -04001111 free(fsdata.fatbuf);
Rob Clark8eafae22017-09-09 13:15:54 -04001112 fat_itr_root(itr, &fsdata);
1113 if (!fat_itr_resolve(itr, filename, TYPE_DIR)) {
1114 *size = 0;
Rob Clark725ffdb2017-09-12 16:40:01 -04001115 ret = 0;
Rob Clark8eafae22017-09-09 13:15:54 -04001116 }
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001117 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001118 }
1119
1120 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001121out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001122 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001123out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001124 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001125 return ret;
Stephen Warrencf659812014-06-11 12:47:26 -06001126}
1127
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001128int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1129 loff_t maxsize, loff_t *actread)
wdenk71f95112003-06-15 22:40:42 +00001130{
Rob Clark8eafae22017-09-09 13:15:54 -04001131 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001132 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001133 int ret;
1134
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001135 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001136 if (!itr)
1137 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001138 ret = fat_itr_root(itr, &fsdata);
1139 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001140 goto out_free_itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001141
1142 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1143 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001144 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001145
Andreas Dannenberg287c04e2018-08-13 21:35:15 -05001146 debug("reading %s at pos %llu\n", filename, pos);
Rob Clark725ffdb2017-09-12 16:40:01 -04001147 ret = get_contents(&fsdata, itr->dent, pos, buffer, maxsize, actread);
1148
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001149out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001150 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001151out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001152 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001153 return ret;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001154}
1155
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001156int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001157{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001158 loff_t actread;
1159 int ret;
1160
1161 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1162 if (ret)
1163 return ret;
1164 else
1165 return actread;
wdenk71f95112003-06-15 22:40:42 +00001166}
Simon Glasse6d52412012-12-26 09:53:33 +00001167
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001168int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1169 loff_t *actread)
Simon Glasse6d52412012-12-26 09:53:33 +00001170{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001171 int ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001172
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001173 ret = file_fat_read_at(filename, offset, buf, len, actread);
1174 if (ret)
Simon Glasse6d52412012-12-26 09:53:33 +00001175 printf("** Unable to read file %s **\n", filename);
Simon Glasse6d52412012-12-26 09:53:33 +00001176
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001177 return ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001178}
1179
Rob Clark1f403662017-09-09 13:15:56 -04001180typedef struct {
1181 struct fs_dir_stream parent;
1182 struct fs_dirent dirent;
1183 fsdata fsdata;
1184 fat_itr itr;
1185} fat_dir;
1186
1187int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1188{
Neil Armstrong34dd8532017-11-24 09:54:41 +01001189 fat_dir *dir;
Rob Clark1f403662017-09-09 13:15:56 -04001190 int ret;
1191
Neil Armstrong34dd8532017-11-24 09:54:41 +01001192 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001193 if (!dir)
1194 return -ENOMEM;
Neil Armstrong34dd8532017-11-24 09:54:41 +01001195 memset(dir, 0, sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001196
1197 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1198 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001199 goto fail_free_dir;
Rob Clark1f403662017-09-09 13:15:56 -04001200
1201 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1202 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001203 goto fail_free_both;
Rob Clark1f403662017-09-09 13:15:56 -04001204
1205 *dirsp = (struct fs_dir_stream *)dir;
1206 return 0;
1207
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001208fail_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001209 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001210fail_free_dir:
Rob Clark1f403662017-09-09 13:15:56 -04001211 free(dir);
1212 return ret;
1213}
1214
1215int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1216{
1217 fat_dir *dir = (fat_dir *)dirs;
1218 struct fs_dirent *dent = &dir->dirent;
1219
1220 if (!fat_itr_next(&dir->itr))
1221 return -ENOENT;
1222
1223 memset(dent, 0, sizeof(*dent));
1224 strcpy(dent->name, dir->itr.name);
1225
1226 if (fat_itr_isdir(&dir->itr)) {
1227 dent->type = FS_DT_DIR;
1228 } else {
1229 dent->type = FS_DT_REG;
1230 dent->size = FAT2CPU32(dir->itr.dent->size);
1231 }
1232
1233 *dentp = dent;
1234
1235 return 0;
1236}
1237
1238void fat_closedir(struct fs_dir_stream *dirs)
1239{
1240 fat_dir *dir = (fat_dir *)dirs;
Rob Clark725ffdb2017-09-12 16:40:01 -04001241 free(dir->fsdata.fatbuf);
Rob Clark1f403662017-09-09 13:15:56 -04001242 free(dir);
1243}
1244
Simon Glasse6d52412012-12-26 09:53:33 +00001245void fat_close(void)
1246{
1247}