blob: dac86ea5163c486a3561fc400c6f554a805da764 [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);
Tien Fong Cheed8c3ea92019-01-23 14:20:04 +0800148
149#if !CONFIG_IS_ENABLED(FAT_WRITE)
Stefan Brünsb8948d22016-12-17 00:27:51 +0100150/* Stub for read only operation */
151int flush_dirty_fat_buffer(fsdata *mydata)
152{
153 (void)(mydata);
154 return 0;
155}
156#endif
157
wdenk71f95112003-06-15 22:40:42 +0000158/*
159 * Get the entry at index 'entry' in a FAT (12/16/32) table.
160 * On failure 0x00 is returned.
161 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200162static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000163{
164 __u32 bufnum;
Stefan Brünsb352cae2017-01-26 20:22:36 +0000165 __u32 offset, off8;
wdenk71f95112003-06-15 22:40:42 +0000166 __u32 ret = 0x00;
167
Stefan Brünsb8948d22016-12-17 00:27:51 +0100168 if (CHECK_CLUST(entry, mydata->fatsize)) {
169 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
170 return ret;
171 }
172
wdenk71f95112003-06-15 22:40:42 +0000173 switch (mydata->fatsize) {
174 case 32:
175 bufnum = entry / FAT32BUFSIZE;
176 offset = entry - bufnum * FAT32BUFSIZE;
177 break;
178 case 16:
179 bufnum = entry / FAT16BUFSIZE;
180 offset = entry - bufnum * FAT16BUFSIZE;
181 break;
182 case 12:
183 bufnum = entry / FAT12BUFSIZE;
184 offset = entry - bufnum * FAT12BUFSIZE;
185 break;
186
187 default:
188 /* Unsupported FAT size */
189 return ret;
190 }
191
Stefan Brünsb8948d22016-12-17 00:27:51 +0100192 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk7385c282010-07-19 11:37:00 +0200193 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200194
wdenk71f95112003-06-15 22:40:42 +0000195 /* Read a new block of FAT entries into the cache. */
196 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000197 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000198 __u8 *bufptr = mydata->fatbuf;
199 __u32 fatlength = mydata->fatlength;
200 __u32 startblock = bufnum * FATBUFBLOCKS;
201
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100202 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau8006dd22012-07-20 15:19:29 +0200203 if (startblock + getsize > fatlength)
204 getsize = fatlength - startblock;
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000205
wdenk71f95112003-06-15 22:40:42 +0000206 startblock += mydata->fat_sect; /* Offset from start of disk */
207
Stefan Brünsb8948d22016-12-17 00:27:51 +0100208 /* Write back the fatbuf to the disk */
209 if (flush_dirty_fat_buffer(mydata) < 0)
210 return -1;
211
wdenk71f95112003-06-15 22:40:42 +0000212 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200213 debug("Error reading FAT blocks\n");
wdenk71f95112003-06-15 22:40:42 +0000214 return ret;
215 }
216 mydata->fatbufnum = bufnum;
217 }
218
219 /* Get the actual entry from the table */
220 switch (mydata->fatsize) {
221 case 32:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200222 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000223 break;
224 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200225 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000226 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200227 case 12:
Stefan Brünsb352cae2017-01-26 20:22:36 +0000228 off8 = (offset * 3) / 2;
229 /* fatbut + off8 may be unaligned, read in byte granularity */
230 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk71f95112003-06-15 22:40:42 +0000231
Stefan Brüns8d48c922016-12-17 03:55:10 +0100232 if (offset & 0x1)
233 ret >>= 4;
234 ret &= 0xfff;
wdenk71f95112003-06-15 22:40:42 +0000235 }
Stefan Brünsb8948d22016-12-17 00:27:51 +0100236 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
237 mydata->fatsize, ret, entry, offset);
wdenk71f95112003-06-15 22:40:42 +0000238
239 return ret;
240}
241
wdenk71f95112003-06-15 22:40:42 +0000242/*
243 * Read at most 'size' bytes from the specified cluster into 'buffer'.
244 * Return 0 on success, -1 otherwise.
245 */
246static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200247get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000248{
Erik Hansen3f270f42011-03-24 10:15:37 +0100249 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000250 __u32 startsect;
Kyle Moffett46236b12011-12-20 07:41:13 +0000251 int ret;
wdenk71f95112003-06-15 22:40:42 +0000252
253 if (clustnum > 0) {
Rob Clark265edc02017-09-09 13:16:00 -0400254 startsect = clust_to_sect(mydata, clustnum);
wdenk71f95112003-06-15 22:40:42 +0000255 } else {
256 startsect = mydata->rootdir_sect;
257 }
258
Wolfgang Denk7385c282010-07-19 11:37:00 +0200259 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
260
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200261 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson9a800ac2012-04-11 04:08:53 +0000262 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200263
Heinrich Schuchardt1c381ce2018-09-13 19:42:47 +0200264 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200265
266 while (size >= mydata->sect_size) {
267 ret = disk_read(startsect++, 1, tmpbuf);
268 if (ret != 1) {
269 debug("Error reading data (got %d)\n", ret);
270 return -1;
271 }
272
273 memcpy(buffer, tmpbuf, mydata->sect_size);
274 buffer += mydata->sect_size;
275 size -= mydata->sect_size;
276 }
277 } else {
Sergei Shtylyovac497772011-08-08 09:38:33 +0000278 idx = size / mydata->sect_size;
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200279 ret = disk_read(startsect, idx, buffer);
280 if (ret != idx) {
281 debug("Error reading data (got %d)\n", ret);
282 return -1;
283 }
284 startsect += idx;
285 idx *= mydata->sect_size;
286 buffer += idx;
287 size -= idx;
288 }
289 if (size) {
290 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
291
292 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett46236b12011-12-20 07:41:13 +0000293 if (ret != 1) {
294 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000295 return -1;
wdenk71f95112003-06-15 22:40:42 +0000296 }
wdenk7205e402003-09-10 22:30:53 +0000297
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200298 memcpy(buffer, tmpbuf, size);
wdenk71f95112003-06-15 22:40:42 +0000299 }
300
301 return 0;
302}
303
wdenk71f95112003-06-15 22:40:42 +0000304/*
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000305 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk71f95112003-06-15 22:40:42 +0000306 * into 'buffer'.
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800307 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
wdenk71f95112003-06-15 22:40:42 +0000308 */
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000309__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
310 __aligned(ARCH_DMA_MINALIGN);
311
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800312static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
313 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk71f95112003-06-15 22:40:42 +0000314{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800315 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000316 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000317 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000318 __u32 endclust, newclust;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800319 loff_t actsize;
wdenk71f95112003-06-15 22:40:42 +0000320
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800321 *gotsize = 0;
322 debug("Filesize: %llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000323
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000324 if (pos >= filesize) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800325 debug("Read position past EOF: %llu\n", pos);
326 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000327 }
328
329 if (maxsize > 0 && filesize > pos + maxsize)
330 filesize = pos + maxsize;
wdenk71f95112003-06-15 22:40:42 +0000331
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800332 debug("%llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000333
Wolfgang Denk7385c282010-07-19 11:37:00 +0200334 actsize = bytesperclust;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000335
336 /* go to cluster at pos */
337 while (actsize <= pos) {
338 curclust = get_fatent(mydata, curclust);
339 if (CHECK_CLUST(curclust, mydata->fatsize)) {
340 debug("curclust: 0x%x\n", curclust);
341 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800342 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000343 }
344 actsize += bytesperclust;
345 }
346
347 /* actsize > pos */
348 actsize -= bytesperclust;
349 filesize -= actsize;
350 pos -= actsize;
351
352 /* align to beginning of next cluster if any */
353 if (pos) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800354 actsize = min(filesize, (loff_t)bytesperclust);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000355 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
356 (int)actsize) != 0) {
357 printf("Error reading cluster\n");
358 return -1;
359 }
360 filesize -= actsize;
361 actsize -= pos;
362 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800363 *gotsize += actsize;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000364 if (!filesize)
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800365 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000366 buffer += actsize;
367
368 curclust = get_fatent(mydata, curclust);
369 if (CHECK_CLUST(curclust, mydata->fatsize)) {
370 debug("curclust: 0x%x\n", curclust);
371 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800372 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000373 }
374 }
375
376 actsize = bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200377 endclust = curclust;
378
wdenk71f95112003-06-15 22:40:42 +0000379 do {
wdenk7205e402003-09-10 22:30:53 +0000380 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200381 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000382 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200383 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000384 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100385 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200386 debug("curclust: 0x%x\n", newclust);
387 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800388 return 0;
wdenk7205e402003-09-10 22:30:53 +0000389 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200390 endclust = newclust;
391 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000392 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200393
wdenk7205e402003-09-10 22:30:53 +0000394 /* get remaining bytes */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200395 actsize = filesize;
Benoît Thébaudeau0880e5b2012-07-20 15:21:37 +0200396 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200397 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000398 return -1;
399 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800400 *gotsize += actsize;
401 return 0;
wdenk7205e402003-09-10 22:30:53 +0000402getit:
403 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200404 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000405 return -1;
406 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800407 *gotsize += (int)actsize;
wdenk7205e402003-09-10 22:30:53 +0000408 filesize -= actsize;
409 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200410
wdenk7205e402003-09-10 22:30:53 +0000411 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100412 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200413 debug("curclust: 0x%x\n", curclust);
414 printf("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800415 return 0;
wdenk71f95112003-06-15 22:40:42 +0000416 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200417 actsize = bytesperclust;
418 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000419 } while (1);
420}
421
wdenk71f95112003-06-15 22:40:42 +0000422/*
423 * Extract the file name information from 'slotptr' into 'l_name',
424 * starting at l_name[*idx].
425 * Return 1 if terminator (zero byte) is found, 0 otherwise.
426 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200427static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000428{
429 int j;
430
431 for (j = 0; j <= 8; j += 2) {
432 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200433 if (l_name[*idx] == 0x00)
434 return 1;
wdenk71f95112003-06-15 22:40:42 +0000435 (*idx)++;
436 }
437 for (j = 0; j <= 10; j += 2) {
438 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200439 if (l_name[*idx] == 0x00)
440 return 1;
wdenk71f95112003-06-15 22:40:42 +0000441 (*idx)++;
442 }
443 for (j = 0; j <= 2; j += 2) {
444 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200445 if (l_name[*idx] == 0x00)
446 return 1;
wdenk71f95112003-06-15 22:40:42 +0000447 (*idx)++;
448 }
449
450 return 0;
451}
452
wdenk71f95112003-06-15 22:40:42 +0000453/* Calculate short name checksum */
Marek Vasutff04f6d2012-10-09 07:20:22 +0000454static __u8 mkcksum(const char name[8], const char ext[3])
wdenk71f95112003-06-15 22:40:42 +0000455{
456 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200457
wdenk71f95112003-06-15 22:40:42 +0000458 __u8 ret = 0;
459
Marek Vasut6ad77d82013-01-11 03:35:48 +0000460 for (i = 0; i < 8; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000461 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut6ad77d82013-01-11 03:35:48 +0000462 for (i = 0; i < 3; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000463 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk71f95112003-06-15 22:40:42 +0000464
465 return ret;
466}
wdenk71f95112003-06-15 22:40:42 +0000467
468/*
wdenk71f95112003-06-15 22:40:42 +0000469 * Read boot sector and volume info from a FAT filesystem
470 */
471static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200472read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000473{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000474 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000475 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000476 int ret = 0;
477
478 if (cur_dev == NULL) {
479 debug("Error: no device selected\n");
480 return -1;
481 }
482
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +0300483 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000484 if (block == NULL) {
485 debug("Error: allocating block\n");
486 return -1;
487 }
wdenk71f95112003-06-15 22:40:42 +0000488
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200489 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200490 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000491 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000492 }
493
494 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200495 bs->reserved = FAT2CPU16(bs->reserved);
496 bs->fat_length = FAT2CPU16(bs->fat_length);
497 bs->secs_track = FAT2CPU16(bs->secs_track);
498 bs->heads = FAT2CPU16(bs->heads);
499 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000500
501 /* FAT32 entries */
502 if (bs->fat_length == 0) {
503 /* Assume FAT32 */
504 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200505 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000506 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200507 bs->info_sector = FAT2CPU16(bs->info_sector);
508 bs->backup_boot = FAT2CPU16(bs->backup_boot);
509 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000510 *fatsize = 32;
511 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200512 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000513 *fatsize = 0;
514 }
515 memcpy(volinfo, vistart, sizeof(volume_info));
516
wdenk71f95112003-06-15 22:40:42 +0000517 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200518 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000519 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000520 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500521 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000522 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000523 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000524 }
Tom Rix651351f2009-05-20 07:55:41 -0500525 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000526 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000527 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000528 }
529 }
530
Wolfgang Denk7385c282010-07-19 11:37:00 +0200531 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000532fail:
533 ret = -1;
534exit:
535 free(block);
536 return ret;
wdenk71f95112003-06-15 22:40:42 +0000537}
538
Rob Clark45449982017-09-09 13:15:52 -0400539static int get_fs_info(fsdata *mydata)
wdenk71f95112003-06-15 22:40:42 +0000540{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200541 boot_sector bs;
542 volume_info volinfo;
Rob Clark45449982017-09-09 13:15:52 -0400543 int ret;
wdenk71f95112003-06-15 22:40:42 +0000544
Rob Clark45449982017-09-09 13:15:52 -0400545 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
546 if (ret) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200547 debug("Error: reading boot sector\n");
Rob Clark45449982017-09-09 13:15:52 -0400548 return ret;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200549 }
550
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000551 if (mydata->fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200552 mydata->fatlength = bs.fat32_length;
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900553 mydata->total_sect = bs.total_sect;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000554 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200555 mydata->fatlength = bs.fat_length;
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900556 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
557 if (!mydata->total_sect)
558 mydata->total_sect = bs.total_sect;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000559 }
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900560 if (!mydata->total_sect) /* unlikely */
561 mydata->total_sect = (u32)cur_part_info.size;
wdenk71f95112003-06-15 22:40:42 +0000562
AKASHI Takahirof23101f2018-09-11 15:58:58 +0900563 mydata->fats = bs.fats;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200564 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000565
Rob Clark45449982017-09-09 13:15:52 -0400566 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000567
Sergei Shtylyovac497772011-08-08 09:38:33 +0000568 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200569 mydata->clust_size = bs.cluster_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000570 if (mydata->sect_size != cur_part_info.blksz) {
571 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
572 mydata->sect_size, cur_part_info.blksz);
573 return -1;
574 }
Patrick Wildtcd80a4f2018-11-26 15:56:57 +0100575 if (mydata->clust_size == 0) {
576 printf("Error: FAT cluster size not set\n");
577 return -1;
578 }
579 if ((unsigned int)mydata->clust_size * mydata->sect_size >
580 MAX_CLUSTSIZE) {
581 printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
582 (unsigned int)mydata->clust_size * mydata->sect_size,
583 MAX_CLUSTSIZE);
584 return -1;
585 }
wdenk71f95112003-06-15 22:40:42 +0000586
Wolfgang Denk7385c282010-07-19 11:37:00 +0200587 if (mydata->fatsize == 32) {
588 mydata->data_begin = mydata->rootdir_sect -
589 (mydata->clust_size * 2);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400590 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200591 } else {
Rob Clark45449982017-09-09 13:15:52 -0400592 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
593 bs.dir_entries[0]) *
594 sizeof(dir_entry)) /
595 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200596 mydata->data_begin = mydata->rootdir_sect +
Rob Clark45449982017-09-09 13:15:52 -0400597 mydata->rootdir_size -
Wolfgang Denk7385c282010-07-19 11:37:00 +0200598 (mydata->clust_size * 2);
Rob Clark265edc02017-09-09 13:16:00 -0400599 mydata->root_cluster =
600 sect_to_clust(mydata, mydata->rootdir_sect);
wdenk71f95112003-06-15 22:40:42 +0000601 }
wdenk71f95112003-06-15 22:40:42 +0000602
Wolfgang Denk7385c282010-07-19 11:37:00 +0200603 mydata->fatbufnum = -1;
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200604 mydata->fat_dirty = 0;
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +0300605 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000606 if (mydata->fatbuf == NULL) {
607 debug("Error: allocating memory\n");
608 return -1;
609 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200610
Wolfgang Denk7385c282010-07-19 11:37:00 +0200611 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
612 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
613 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
614 "Data begins at: %d\n",
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400615 mydata->root_cluster,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200616 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000617 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
618 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
619 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200620
Rob Clark45449982017-09-09 13:15:52 -0400621 return 0;
622}
623
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400624
625/*
626 * Directory iterator, to simplify filesystem traversal
627 *
628 * Implements an iterator pattern to traverse directory tables,
629 * transparently handling directory tables split across multiple
630 * clusters, and the difference between FAT12/FAT16 root directory
631 * (contiguous) and subdirectories + FAT32 root (chained).
632 *
633 * Rough usage:
634 *
635 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
636 * // to traverse down to a subdirectory pointed to by
637 * // current iterator position:
638 * fat_itr_child(&itr, &itr);
639 * }
640 *
641 * For more complete example, see fat_itr_resolve()
642 */
643
644typedef struct {
645 fsdata *fsdata; /* filesystem parameters */
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900646 unsigned start_clust; /* first cluster */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400647 unsigned clust; /* current cluster */
AKASHI Takahirof528c142018-09-11 15:59:00 +0900648 unsigned next_clust; /* next cluster if remaining == 0 */
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400649 int last_cluster; /* set once we've read last cluster */
650 int is_root; /* is iterator at root directory */
651 int remaining; /* remaining dent's in current cluster */
652
653 /* current iterator position values: */
654 dir_entry *dent; /* current directory entry */
655 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
656 char s_name[14]; /* short 8.3 name */
657 char *name; /* l_name if there is one, else s_name */
658
659 /* storage for current cluster in memory: */
660 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
661} fat_itr;
662
663static int fat_itr_isdir(fat_itr *itr);
664
665/**
666 * fat_itr_root() - initialize an iterator to start at the root
667 * directory
668 *
669 * @itr: iterator to initialize
670 * @fsdata: filesystem data for the partition
671 * @return 0 on success, else -errno
672 */
673static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
674{
675 if (get_fs_info(fsdata))
676 return -ENXIO;
677
678 itr->fsdata = fsdata;
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900679 itr->start_clust = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400680 itr->clust = fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900681 itr->next_clust = fsdata->root_cluster;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400682 itr->dent = NULL;
683 itr->remaining = 0;
684 itr->last_cluster = 0;
685 itr->is_root = 1;
686
687 return 0;
688}
689
690/**
691 * fat_itr_child() - initialize an iterator to descend into a sub-
692 * directory
693 *
694 * Initializes 'itr' to iterate the contents of the directory at
695 * the current cursor position of 'parent'. It is an error to
696 * call this if the current cursor of 'parent' is pointing at a
697 * regular file.
698 *
699 * Note that 'itr' and 'parent' can be the same pointer if you do
700 * not need to preserve 'parent' after this call, which is useful
701 * for traversing directory structure to resolve a file/directory.
702 *
703 * @itr: iterator to initialize
704 * @parent: the iterator pointing at a directory entry in the
705 * parent directory of the directory to iterate
706 */
707static void fat_itr_child(fat_itr *itr, fat_itr *parent)
708{
709 fsdata *mydata = parent->fsdata; /* for silly macros */
710 unsigned clustnum = START(parent->dent);
711
712 assert(fat_itr_isdir(parent));
713
714 itr->fsdata = parent->fsdata;
AKASHI Takahiro3a10e072018-09-11 15:59:09 +0900715 itr->start_clust = clustnum;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400716 if (clustnum > 0) {
717 itr->clust = clustnum;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900718 itr->next_clust = clustnum;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300719 itr->is_root = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400720 } else {
721 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900722 itr->next_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen8df87312017-09-25 22:06:33 +0300723 itr->is_root = 1;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400724 }
725 itr->dent = NULL;
726 itr->remaining = 0;
727 itr->last_cluster = 0;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400728}
729
730static void *next_cluster(fat_itr *itr)
731{
732 fsdata *mydata = itr->fsdata; /* for silly macros */
733 int ret;
734 u32 sect;
735
736 /* have we reached the end? */
737 if (itr->last_cluster)
738 return NULL;
739
Tom Rinid3035b82019-01-11 09:54:44 -0500740 sect = clust_to_sect(itr->fsdata, itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400741
742 debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
743 sect, itr->fsdata->clust_size, DIRENTSPERBLOCK);
744
745 /*
746 * NOTE: do_fat_read_at() had complicated logic to deal w/
747 * vfat names that span multiple clusters in the fat16 case,
748 * which get_dentfromdir() probably also needed (and was
749 * missing). And not entirely sure what fat32 didn't have
750 * the same issue.. We solve that by only caring about one
751 * dent at a time and iteratively constructing the vfat long
752 * name.
753 */
754 ret = disk_read(sect, itr->fsdata->clust_size,
755 itr->block);
756 if (ret < 0) {
757 debug("Error: reading block\n");
758 return NULL;
759 }
760
AKASHI Takahirof528c142018-09-11 15:59:00 +0900761 itr->clust = itr->next_clust;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400762 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900763 itr->next_clust++;
764 sect = clust_to_sect(itr->fsdata, itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400765 if (sect - itr->fsdata->rootdir_sect >=
766 itr->fsdata->rootdir_size) {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900767 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400768 itr->last_cluster = 1;
769 }
770 } else {
AKASHI Takahirof528c142018-09-11 15:59:00 +0900771 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
772 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
773 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400774 itr->last_cluster = 1;
775 }
776 }
777
778 return itr->block;
779}
780
781static dir_entry *next_dent(fat_itr *itr)
782{
783 if (itr->remaining == 0) {
784 struct dir_entry *dent = next_cluster(itr);
785 unsigned nbytes = itr->fsdata->sect_size *
786 itr->fsdata->clust_size;
787
788 /* have we reached the last cluster? */
AKASHI Takahirof528c142018-09-11 15:59:00 +0900789 if (!dent) {
790 /* a sign for no more entries left */
791 itr->dent = NULL;
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400792 return NULL;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900793 }
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400794
795 itr->remaining = nbytes / sizeof(dir_entry) - 1;
796 itr->dent = dent;
797 } else {
798 itr->remaining--;
799 itr->dent++;
800 }
801
802 /* have we reached the last valid entry? */
803 if (itr->dent->name[0] == 0)
804 return NULL;
805
806 return itr->dent;
807}
808
809static dir_entry *extract_vfat_name(fat_itr *itr)
810{
811 struct dir_entry *dent = itr->dent;
812 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
813 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
814 int n = 0;
815
816 while (seqn--) {
817 char buf[13];
818 int idx = 0;
819
820 slot2str((dir_slot *)dent, buf, &idx);
821
Patrick Wildt8b021bb2018-11-26 15:58:13 +0100822 if (n + idx >= sizeof(itr->l_name))
823 return NULL;
824
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400825 /* shift accumulated long-name up and copy new part in: */
826 memmove(itr->l_name + idx, itr->l_name, n);
827 memcpy(itr->l_name, buf, idx);
828 n += idx;
829
830 dent = next_dent(itr);
831 if (!dent)
832 return NULL;
833 }
834
835 itr->l_name[n] = '\0';
836
837 chksum = mkcksum(dent->name, dent->ext);
838
839 /* checksum mismatch could mean deleted file, etc.. skip it: */
840 if (chksum != alias_checksum) {
841 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
842 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
843 return NULL;
844 }
845
846 return dent;
847}
848
849/**
850 * fat_itr_next() - step to the next entry in a directory
851 *
852 * Must be called once on a new iterator before the cursor is valid.
853 *
854 * @itr: the iterator to iterate
855 * @return boolean, 1 if success or 0 if no more entries in the
856 * current directory
857 */
858static int fat_itr_next(fat_itr *itr)
859{
860 dir_entry *dent;
861
862 itr->name = NULL;
863
864 while (1) {
865 dent = next_dent(itr);
866 if (!dent)
867 return 0;
868
869 if (dent->name[0] == DELETED_FLAG ||
870 dent->name[0] == aRING)
871 continue;
872
873 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynen8bad6cb2018-01-05 02:45:21 +0200874 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400875 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
876 dent = extract_vfat_name(itr);
877 if (!dent)
878 continue;
879 itr->name = itr->l_name;
880 break;
881 } else {
882 /* Volume label or VFAT entry, skip */
883 continue;
884 }
885 }
886
887 break;
888 }
889
890 get_name(dent, itr->s_name);
891 if (!itr->name)
892 itr->name = itr->s_name;
893
894 return 1;
895}
896
897/**
898 * fat_itr_isdir() - is current cursor position pointing to a directory
899 *
900 * @itr: the iterator
901 * @return true if cursor is at a directory
902 */
903static int fat_itr_isdir(fat_itr *itr)
904{
905 return !!(itr->dent->attr & ATTR_DIR);
906}
907
908/*
909 * Helpers:
910 */
911
912#define TYPE_FILE 0x1
913#define TYPE_DIR 0x2
914#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
915
916/**
917 * fat_itr_resolve() - traverse directory structure to resolve the
918 * requested path.
919 *
920 * Traverse directory structure to the requested path. If the specified
921 * path is to a directory, this will descend into the directory and
922 * leave it iterator at the start of the directory. If the path is to a
923 * file, it will leave the iterator in the parent directory with current
924 * cursor at file's entry in the directory.
925 *
926 * @itr: iterator initialized to root
927 * @path: the requested path
928 * @type: bitmask of allowable file types
929 * @return 0 on success or -errno
930 */
931static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
932{
933 const char *next;
934
935 /* chomp any extra leading slashes: */
936 while (path[0] && ISDIRDELIM(path[0]))
937 path++;
938
939 /* are we at the end? */
940 if (strlen(path) == 0) {
941 if (!(type & TYPE_DIR))
942 return -ENOENT;
943 return 0;
944 }
945
946 /* find length of next path entry: */
947 next = path;
948 while (next[0] && !ISDIRDELIM(next[0]))
949 next++;
950
AKASHI Takahirob94b6be2018-09-11 15:58:59 +0900951 if (itr->is_root) {
952 /* root dir doesn't have "." nor ".." */
953 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
954 (((next - path) == 2) && !strncmp(path, "..", 2))) {
955 /* point back to itself */
956 itr->clust = itr->fsdata->root_cluster;
AKASHI Takahirof528c142018-09-11 15:59:00 +0900957 itr->next_clust = itr->fsdata->root_cluster;
AKASHI Takahirob94b6be2018-09-11 15:58:59 +0900958 itr->dent = NULL;
959 itr->remaining = 0;
960 itr->last_cluster = 0;
961
962 if (next[0] == 0) {
963 if (type & TYPE_DIR)
964 return 0;
965 else
966 return -ENOENT;
967 }
968
969 return fat_itr_resolve(itr, next, type);
970 }
971 }
972
Rob Clarkc6e3baa2017-09-09 13:15:53 -0400973 while (fat_itr_next(itr)) {
974 int match = 0;
975 unsigned n = max(strlen(itr->name), (size_t)(next - path));
976
977 /* check both long and short name: */
978 if (!strncasecmp(path, itr->name, n))
979 match = 1;
980 else if (itr->name != itr->s_name &&
981 !strncasecmp(path, itr->s_name, n))
982 match = 1;
983
984 if (!match)
985 continue;
986
987 if (fat_itr_isdir(itr)) {
988 /* recurse into directory: */
989 fat_itr_child(itr, itr);
990 return fat_itr_resolve(itr, next, type);
991 } else if (next[0]) {
992 /*
993 * If next is not empty then we have a case
994 * like: /path/to/realfile/nonsense
995 */
996 debug("bad trailing path: %s\n", next);
997 return -ENOENT;
998 } else if (!(type & TYPE_FILE)) {
999 return -ENOTDIR;
1000 } else {
1001 return 0;
1002 }
1003 }
1004
1005 return -ENOENT;
1006}
1007
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001008int file_fat_detectfs(void)
wdenk71f95112003-06-15 22:40:42 +00001009{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001010 boot_sector bs;
1011 volume_info volinfo;
1012 int fatsize;
1013 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001014
Wolfgang Denk7385c282010-07-19 11:37:00 +02001015 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001016 printf("No current device\n");
1017 return 1;
1018 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001019
Simon Glassfc843a02017-05-17 03:25:30 -06001020#if defined(CONFIG_IDE) || \
Simon Glass10e40d52017-06-14 21:28:25 -06001021 defined(CONFIG_SATA) || \
Simon Glassc649e3c2016-05-01 11:36:02 -06001022 defined(CONFIG_SCSI) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001023 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001024 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001025 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001026 switch (cur_dev->if_type) {
1027 case IF_TYPE_IDE:
1028 printf("IDE");
1029 break;
1030 case IF_TYPE_SATA:
1031 printf("SATA");
1032 break;
1033 case IF_TYPE_SCSI:
1034 printf("SCSI");
1035 break;
1036 case IF_TYPE_ATAPI:
1037 printf("ATAPI");
1038 break;
1039 case IF_TYPE_USB:
1040 printf("USB");
1041 break;
1042 case IF_TYPE_DOC:
1043 printf("DOC");
1044 break;
1045 case IF_TYPE_MMC:
1046 printf("MMC");
1047 break;
1048 default:
1049 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001050 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001051
Simon Glassbcce53d2016-02-29 15:25:51 -07001052 printf("\n Device %d: ", cur_dev->devnum);
wdenk7205e402003-09-10 22:30:53 +00001053 dev_print(cur_dev);
1054#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001055
1056 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001057 printf("\nNo valid FAT fs found\n");
1058 return 1;
1059 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001060
1061 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001062 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001063 volinfo.fs_type[5] = '\0';
1064
Stephen Warren461f86e2012-10-17 06:44:57 +00001065 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001066
wdenk7205e402003-09-10 22:30:53 +00001067 return 0;
wdenk71f95112003-06-15 22:40:42 +00001068}
1069
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001070int fat_exists(const char *filename)
1071{
Rob Clark8eafae22017-09-09 13:15:54 -04001072 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001073 fat_itr *itr;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001074 int ret;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001075
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001076 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001077 if (!itr)
1078 return 0;
Rob Clark8eafae22017-09-09 13:15:54 -04001079 ret = fat_itr_root(itr, &fsdata);
1080 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001081 goto out;
Rob Clark8eafae22017-09-09 13:15:54 -04001082
1083 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clark725ffdb2017-09-12 16:40:01 -04001084 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001085out:
Tom Rini24600982017-09-22 07:37:43 -04001086 free(itr);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001087 return ret == 0;
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001088}
1089
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001090int fat_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -06001091{
Rob Clark8eafae22017-09-09 13:15:54 -04001092 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001093 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001094 int ret;
1095
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001096 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001097 if (!itr)
1098 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001099 ret = fat_itr_root(itr, &fsdata);
1100 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001101 goto out_free_itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001102
1103 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1104 if (ret) {
1105 /*
1106 * Directories don't have size, but fs_size() is not
1107 * expected to fail if passed a directory path:
1108 */
Rob Clark725ffdb2017-09-12 16:40:01 -04001109 free(fsdata.fatbuf);
Rob Clark8eafae22017-09-09 13:15:54 -04001110 fat_itr_root(itr, &fsdata);
1111 if (!fat_itr_resolve(itr, filename, TYPE_DIR)) {
1112 *size = 0;
Rob Clark725ffdb2017-09-12 16:40:01 -04001113 ret = 0;
Rob Clark8eafae22017-09-09 13:15:54 -04001114 }
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001115 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001116 }
1117
1118 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001119out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001120 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001121out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001122 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001123 return ret;
Stephen Warrencf659812014-06-11 12:47:26 -06001124}
1125
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001126int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1127 loff_t maxsize, loff_t *actread)
wdenk71f95112003-06-15 22:40:42 +00001128{
Rob Clark8eafae22017-09-09 13:15:54 -04001129 fsdata fsdata;
Tom Rini24600982017-09-22 07:37:43 -04001130 fat_itr *itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001131 int ret;
1132
Tuomas Tynkkynen09fa9642017-10-01 02:25:21 +03001133 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001134 if (!itr)
1135 return -ENOMEM;
Rob Clark8eafae22017-09-09 13:15:54 -04001136 ret = fat_itr_root(itr, &fsdata);
1137 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001138 goto out_free_itr;
Rob Clark8eafae22017-09-09 13:15:54 -04001139
1140 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1141 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001142 goto out_free_both;
Rob Clark8eafae22017-09-09 13:15:54 -04001143
Andreas Dannenberg287c04e2018-08-13 21:35:15 -05001144 debug("reading %s at pos %llu\n", filename, pos);
Rob Clark725ffdb2017-09-12 16:40:01 -04001145 ret = get_contents(&fsdata, itr->dent, pos, buffer, maxsize, actread);
1146
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001147out_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001148 free(fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001149out_free_itr:
Tom Rini24600982017-09-22 07:37:43 -04001150 free(itr);
Rob Clark725ffdb2017-09-12 16:40:01 -04001151 return ret;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001152}
1153
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001154int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001155{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001156 loff_t actread;
1157 int ret;
1158
1159 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1160 if (ret)
1161 return ret;
1162 else
1163 return actread;
wdenk71f95112003-06-15 22:40:42 +00001164}
Simon Glasse6d52412012-12-26 09:53:33 +00001165
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001166int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1167 loff_t *actread)
Simon Glasse6d52412012-12-26 09:53:33 +00001168{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001169 int ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001170
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001171 ret = file_fat_read_at(filename, offset, buf, len, actread);
1172 if (ret)
Simon Glasse6d52412012-12-26 09:53:33 +00001173 printf("** Unable to read file %s **\n", filename);
Simon Glasse6d52412012-12-26 09:53:33 +00001174
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001175 return ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001176}
1177
Rob Clark1f403662017-09-09 13:15:56 -04001178typedef struct {
1179 struct fs_dir_stream parent;
1180 struct fs_dirent dirent;
1181 fsdata fsdata;
1182 fat_itr itr;
1183} fat_dir;
1184
1185int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1186{
Neil Armstrong34dd8532017-11-24 09:54:41 +01001187 fat_dir *dir;
Rob Clark1f403662017-09-09 13:15:56 -04001188 int ret;
1189
Neil Armstrong34dd8532017-11-24 09:54:41 +01001190 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001191 if (!dir)
1192 return -ENOMEM;
Neil Armstrong34dd8532017-11-24 09:54:41 +01001193 memset(dir, 0, sizeof(*dir));
Rob Clark1f403662017-09-09 13:15:56 -04001194
1195 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1196 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001197 goto fail_free_dir;
Rob Clark1f403662017-09-09 13:15:56 -04001198
1199 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1200 if (ret)
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001201 goto fail_free_both;
Rob Clark1f403662017-09-09 13:15:56 -04001202
1203 *dirsp = (struct fs_dir_stream *)dir;
1204 return 0;
1205
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001206fail_free_both:
Rob Clark725ffdb2017-09-12 16:40:01 -04001207 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenaf609e32017-10-01 02:25:22 +03001208fail_free_dir:
Rob Clark1f403662017-09-09 13:15:56 -04001209 free(dir);
1210 return ret;
1211}
1212
1213int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1214{
1215 fat_dir *dir = (fat_dir *)dirs;
1216 struct fs_dirent *dent = &dir->dirent;
1217
1218 if (!fat_itr_next(&dir->itr))
1219 return -ENOENT;
1220
1221 memset(dent, 0, sizeof(*dent));
1222 strcpy(dent->name, dir->itr.name);
1223
1224 if (fat_itr_isdir(&dir->itr)) {
1225 dent->type = FS_DT_DIR;
1226 } else {
1227 dent->type = FS_DT_REG;
1228 dent->size = FAT2CPU32(dir->itr.dent->size);
1229 }
1230
1231 *dentp = dent;
1232
1233 return 0;
1234}
1235
1236void fat_closedir(struct fs_dir_stream *dirs)
1237{
1238 fat_dir *dir = (fat_dir *)dirs;
Rob Clark725ffdb2017-09-12 16:40:01 -04001239 free(dir->fsdata.fatbuf);
Rob Clark1f403662017-09-09 13:15:56 -04001240 free(dir);
1241}
1242
Simon Glasse6d52412012-12-26 09:53:33 +00001243void fat_close(void)
1244{
1245}