blob: 9e1b842dac6b9802960b19bb180a90e894679159 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +00002/*
3 * fat.c
4 *
5 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
6 *
7 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
8 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
wdenk7a428cc2003-06-15 22:40:42 +00009 */
10
11#include <common.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -070012#include <blk.h>
wdenk7a428cc2003-06-15 22:40:42 +000013#include <config.h>
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +000014#include <exports.h>
wdenk7a428cc2003-06-15 22:40:42 +000015#include <fat.h>
Rob Clark4174d7f2017-09-09 13:15:56 -040016#include <fs.h>
wdenk7a428cc2003-06-15 22:40:42 +000017#include <asm/byteorder.h>
wdenk2c9b05d2003-09-10 22:30:53 +000018#include <part.h>
Eric Nelson014705b2012-04-11 04:08:53 +000019#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060020#include <memalign.h>
Eric Nelson014705b2012-04-11 04:08:53 +000021#include <linux/compiler.h>
Richard Genoud2e372022012-12-13 00:47:36 +000022#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000023
wdenk7a428cc2003-06-15 22:40:42 +000024/*
Rob Clarkfd6c6b42017-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.
wdenk7a428cc2003-06-15 22:40:42 +000028 */
Rob Clarkfd6c6b42017-09-09 13:15:59 -040029static void downcase(char *str, size_t len)
wdenk7a428cc2003-06-15 22:40:42 +000030{
Rob Clarkfd6c6b42017-09-09 13:15:59 -040031 while (*str != '\0' && len--) {
Richard Genoud2e372022012-12-13 00:47:36 +000032 *str = tolower(*str);
wdenk7a428cc2003-06-15 22:40:42 +000033 str++;
34 }
35}
36
Simon Glasse3394752016-02-29 15:25:34 -070037static struct blk_desc *cur_dev;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000038static disk_partition_t cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020039
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000040#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000041#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020042#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000043
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000044static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000045{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020046 ulong ret;
47
Simon Glass2ee8ada2016-02-29 15:25:52 -070048 if (!cur_dev)
wdenk2c9b05d2003-09-10 22:30:53 +000049 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020050
Simon Glass2ee8ada2016-02-29 15:25:52 -070051 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020052
Tom Rini240124c2017-08-14 21:02:08 -040053 if (ret != nr_blocks)
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020054 return -1;
55
56 return ret;
wdenk7a428cc2003-06-15 22:40:42 +000057}
58
Simon Glasse3394752016-02-29 15:25:34 -070059int fat_set_blk_dev(struct blk_desc *dev_desc, disk_partition_t *info)
wdenk7a428cc2003-06-15 22:40:42 +000060{
Eric Nelson014705b2012-04-11 04:08:53 +000061 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000062
Stephen Warren288904d2012-10-17 06:44:59 +000063 cur_dev = dev_desc;
64 cur_part_info = *info;
Kyle Moffett1b3b4b72011-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;
Wolfgang Denke78f2582007-08-07 16:02:13 +020070 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000071
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;
wdenk7a428cc2003-06-15 22:40:42 +000086}
87
Simon Glasse3394752016-02-29 15:25:34 -070088int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren288904d2012-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 Glassb89a8442016-02-29 15:25:48 -070096 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren288904d2012-10-17 06:44:59 +000097 if (part_no != 0) {
98 printf("** Partition %d not valid on device %d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -070099 part_no, dev_desc->devnum);
Stephen Warren288904d2012-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 Delaunay73287092017-01-27 11:00:42 +0100109#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren288904d2012-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 Moffett1b3b4b72011-12-21 07:08:10 +0000116
wdenk7a428cc2003-06-15 22:40:42 +0000117/*
wdenk7a428cc2003-06-15 22:40:42 +0000118 * Extract zero terminated short name from a directory entry.
119 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200120static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000121{
122 char *ptr;
123
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200124 memcpy(s_name, dirent->name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000125 s_name[8] = '\0';
126 ptr = s_name;
127 while (*ptr && *ptr != ' ')
128 ptr++;
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400129 if (dirent->lcase & CASE_LOWER_BASE)
130 downcase(s_name, (unsigned)(ptr - s_name));
wdenk7a428cc2003-06-15 22:40:42 +0000131 if (dirent->ext[0] && dirent->ext[0] != ' ') {
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400132 *ptr++ = '.';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200133 memcpy(ptr, dirent->ext, 3);
Rob Clarkfd6c6b42017-09-09 13:15:59 -0400134 if (dirent->lcase & CASE_LOWER_EXT)
135 downcase(ptr, 3);
wdenk7a428cc2003-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 Bohmer7c8f2ce2008-11-27 22:30:27 +0100144 *s_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000145}
146
Stefan Brüns9f95d812016-12-17 00:27:51 +0100147static int flush_dirty_fat_buffer(fsdata *mydata);
Tien Fong Chee87fda0c2019-01-23 14:20:04 +0800148
149#if !CONFIG_IS_ENABLED(FAT_WRITE)
Stefan Brüns9f95d812016-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
wdenk7a428cc2003-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ébaudeau0a120372012-07-20 15:18:44 +0200162static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000163{
164 __u32 bufnum;
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000165 __u32 offset, off8;
wdenk7a428cc2003-06-15 22:40:42 +0000166 __u32 ret = 0x00;
167
Stefan Brüns9f95d812016-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
wdenk7a428cc2003-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üns9f95d812016-12-17 00:27:51 +0100192 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200193 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200194
wdenk7a428cc2003-06-15 22:40:42 +0000195 /* Read a new block of FAT entries into the cache. */
196 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000197 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000198 __u8 *bufptr = mydata->fatbuf;
199 __u32 fatlength = mydata->fatlength;
200 __u32 startblock = bufnum * FATBUFBLOCKS;
201
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100202 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200203 if (startblock + getsize > fatlength)
204 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000205
wdenk7a428cc2003-06-15 22:40:42 +0000206 startblock += mydata->fat_sect; /* Offset from start of disk */
207
Stefan Brüns9f95d812016-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
wdenk7a428cc2003-06-15 22:40:42 +0000212 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200213 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200222 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000223 break;
224 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200225 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000226 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200227 case 12:
Stefan Brüns0f3b6972017-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);
wdenk7a428cc2003-06-15 22:40:42 +0000231
Stefan Brüns15f40632016-12-17 03:55:10 +0100232 if (offset & 0x1)
233 ret >>= 4;
234 ret &= 0xfff;
wdenk7a428cc2003-06-15 22:40:42 +0000235 }
Stefan Brüns9f95d812016-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);
wdenk7a428cc2003-06-15 22:40:42 +0000238
239 return ret;
240}
241
wdenk7a428cc2003-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ébaudeau0a120372012-07-20 15:18:44 +0200247get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000248{
Erik Hansen4ea89662011-03-24 10:15:37 +0100249 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000250 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000251 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000252
253 if (clustnum > 0) {
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400254 startsect = clust_to_sect(mydata, clustnum);
wdenk7a428cc2003-06-15 22:40:42 +0000255 } else {
256 startsect = mydata->rootdir_sect;
257 }
258
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200259 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
260
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200261 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000262 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200263
Heinrich Schuchardtaa9d6ba2018-09-13 19:42:47 +0200264 debug("FAT: Misaligned buffer address (%p)\n", buffer);
Benoît Thébaudeaue957c072012-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 Shtylyov5ab3ba72011-08-08 09:38:33 +0000278 idx = size / mydata->sect_size;
Benoît Thébaudeaue957c072012-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 Moffett83c8a782011-12-20 07:41:13 +0000293 if (ret != 1) {
294 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000295 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000296 }
wdenk2c9b05d2003-09-10 22:30:53 +0000297
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200298 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000299 }
300
301 return 0;
302}
303
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200304/**
305 * get_contents() - read from file
306 *
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000307 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200308 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
309 * fatal errors.
310 *
311 * @mydata: file system description
312 * @dentprt: directory entry pointer
313 * @pos: position from where to read
314 * @buffer: buffer into which to read
315 * @maxsize: maximum number of bytes to read
316 * @gotsize: number of bytes actually read
317 * Return: -1 on error, otherwise 0
wdenk7a428cc2003-06-15 22:40:42 +0000318 */
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800319static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
320 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk7a428cc2003-06-15 22:40:42 +0000321{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800322 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000323 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000324 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000325 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800326 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000327
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800328 *gotsize = 0;
329 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000330
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000331 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800332 debug("Read position past EOF: %llu\n", pos);
333 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000334 }
335
336 if (maxsize > 0 && filesize > pos + maxsize)
337 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000338
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800339 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000340
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200341 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000342
343 /* go to cluster at pos */
344 while (actsize <= pos) {
345 curclust = get_fatent(mydata, curclust);
346 if (CHECK_CLUST(curclust, mydata->fatsize)) {
347 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200348 printf("Invalid FAT entry\n");
349 return -1;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000350 }
351 actsize += bytesperclust;
352 }
353
354 /* actsize > pos */
355 actsize -= bytesperclust;
356 filesize -= actsize;
357 pos -= actsize;
358
359 /* align to beginning of next cluster if any */
360 if (pos) {
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800361 __u8 *tmp_buffer;
362
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800363 actsize = min(filesize, (loff_t)bytesperclust);
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800364 tmp_buffer = malloc_cache_aligned(actsize);
365 if (!tmp_buffer) {
366 debug("Error: allocating buffer\n");
Heinrich Schuchardtd37d47c2019-09-12 19:19:30 +0200367 return -1;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800368 }
369
370 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000371 printf("Error reading cluster\n");
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800372 free(tmp_buffer);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000373 return -1;
374 }
375 filesize -= actsize;
376 actsize -= pos;
Tien Fong Cheef6764f22019-02-11 14:56:19 +0800377 memcpy(buffer, tmp_buffer + pos, actsize);
378 free(tmp_buffer);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800379 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000380 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800381 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000382 buffer += actsize;
383
384 curclust = get_fatent(mydata, curclust);
385 if (CHECK_CLUST(curclust, mydata->fatsize)) {
386 debug("curclust: 0x%x\n", curclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200387 printf("Invalid FAT entry\n");
388 return -1;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000389 }
390 }
391
392 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200393 endclust = curclust;
394
wdenk7a428cc2003-06-15 22:40:42 +0000395 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000396 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200397 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000398 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200399 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000400 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100401 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200402 debug("curclust: 0x%x\n", newclust);
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200403 printf("Invalid FAT entry\n");
404 return -1;
wdenk2c9b05d2003-09-10 22:30:53 +0000405 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200406 endclust = newclust;
407 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000408 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200409
wdenk2c9b05d2003-09-10 22:30:53 +0000410 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200411 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200412 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200413 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000414 return -1;
415 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800416 *gotsize += actsize;
417 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000418getit:
419 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200420 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000421 return -1;
422 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800423 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000424 filesize -= actsize;
425 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200426
wdenk2c9b05d2003-09-10 22:30:53 +0000427 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100428 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200429 debug("curclust: 0x%x\n", curclust);
430 printf("Invalid FAT entry\n");
Heinrich Schuchardtc1a159c2019-09-12 19:19:29 +0200431 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000432 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200433 actsize = bytesperclust;
434 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000435 } while (1);
436}
437
wdenk7a428cc2003-06-15 22:40:42 +0000438/*
439 * Extract the file name information from 'slotptr' into 'l_name',
440 * starting at l_name[*idx].
441 * Return 1 if terminator (zero byte) is found, 0 otherwise.
442 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200443static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000444{
445 int j;
446
447 for (j = 0; j <= 8; j += 2) {
448 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200449 if (l_name[*idx] == 0x00)
450 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000451 (*idx)++;
452 }
453 for (j = 0; j <= 10; j += 2) {
454 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200455 if (l_name[*idx] == 0x00)
456 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000457 (*idx)++;
458 }
459 for (j = 0; j <= 2; j += 2) {
460 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200461 if (l_name[*idx] == 0x00)
462 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000463 (*idx)++;
464 }
465
466 return 0;
467}
468
wdenk7a428cc2003-06-15 22:40:42 +0000469/* Calculate short name checksum */
Marek Vasut47a470f2012-10-09 07:20:22 +0000470static __u8 mkcksum(const char name[8], const char ext[3])
wdenk7a428cc2003-06-15 22:40:42 +0000471{
472 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200473
wdenk7a428cc2003-06-15 22:40:42 +0000474 __u8 ret = 0;
475
Marek Vasut4ab1f642013-01-11 03:35:48 +0000476 for (i = 0; i < 8; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000477 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut4ab1f642013-01-11 03:35:48 +0000478 for (i = 0; i < 3; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000479 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk7a428cc2003-06-15 22:40:42 +0000480
481 return ret;
482}
wdenk7a428cc2003-06-15 22:40:42 +0000483
484/*
wdenk7a428cc2003-06-15 22:40:42 +0000485 * Read boot sector and volume info from a FAT filesystem
486 */
487static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200488read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000489{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000490 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000491 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000492 int ret = 0;
493
494 if (cur_dev == NULL) {
495 debug("Error: no device selected\n");
496 return -1;
497 }
498
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300499 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000500 if (block == NULL) {
501 debug("Error: allocating block\n");
502 return -1;
503 }
wdenk7a428cc2003-06-15 22:40:42 +0000504
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200505 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200506 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000507 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000508 }
509
510 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200511 bs->reserved = FAT2CPU16(bs->reserved);
512 bs->fat_length = FAT2CPU16(bs->fat_length);
513 bs->secs_track = FAT2CPU16(bs->secs_track);
514 bs->heads = FAT2CPU16(bs->heads);
515 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000516
517 /* FAT32 entries */
518 if (bs->fat_length == 0) {
519 /* Assume FAT32 */
520 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200521 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000522 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200523 bs->info_sector = FAT2CPU16(bs->info_sector);
524 bs->backup_boot = FAT2CPU16(bs->backup_boot);
525 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000526 *fatsize = 32;
527 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200528 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000529 *fatsize = 0;
530 }
531 memcpy(volinfo, vistart, sizeof(volume_info));
532
wdenk7a428cc2003-06-15 22:40:42 +0000533 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200534 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000535 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000536 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500537 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000538 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000539 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000540 }
Tom Rix155abaa2009-05-20 07:55:41 -0500541 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000542 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000543 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000544 }
545 }
546
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200547 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000548fail:
549 ret = -1;
550exit:
551 free(block);
552 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000553}
554
Rob Clark4996a5f2017-09-09 13:15:52 -0400555static int get_fs_info(fsdata *mydata)
wdenk7a428cc2003-06-15 22:40:42 +0000556{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200557 boot_sector bs;
558 volume_info volinfo;
Rob Clark4996a5f2017-09-09 13:15:52 -0400559 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000560
Rob Clark4996a5f2017-09-09 13:15:52 -0400561 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
562 if (ret) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200563 debug("Error: reading boot sector\n");
Rob Clark4996a5f2017-09-09 13:15:52 -0400564 return ret;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200565 }
566
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000567 if (mydata->fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200568 mydata->fatlength = bs.fat32_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900569 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000570 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200571 mydata->fatlength = bs.fat_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900572 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
573 if (!mydata->total_sect)
574 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000575 }
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900576 if (!mydata->total_sect) /* unlikely */
577 mydata->total_sect = (u32)cur_part_info.size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200578
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900579 mydata->fats = bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200580 mydata->fat_sect = bs.reserved;
581
Rob Clark4996a5f2017-09-09 13:15:52 -0400582 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200583
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000584 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200585 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000586 if (mydata->sect_size != cur_part_info.blksz) {
587 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
588 mydata->sect_size, cur_part_info.blksz);
589 return -1;
590 }
Patrick Wildt1d614b92018-11-26 15:56:57 +0100591 if (mydata->clust_size == 0) {
592 printf("Error: FAT cluster size not set\n");
593 return -1;
594 }
595 if ((unsigned int)mydata->clust_size * mydata->sect_size >
596 MAX_CLUSTSIZE) {
597 printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
598 (unsigned int)mydata->clust_size * mydata->sect_size,
599 MAX_CLUSTSIZE);
600 return -1;
601 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200602
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200603 if (mydata->fatsize == 32) {
604 mydata->data_begin = mydata->rootdir_sect -
605 (mydata->clust_size * 2);
Rob Clark0c33fce2017-09-09 13:15:53 -0400606 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200607 } else {
Rob Clark4996a5f2017-09-09 13:15:52 -0400608 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
609 bs.dir_entries[0]) *
610 sizeof(dir_entry)) /
611 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200612 mydata->data_begin = mydata->rootdir_sect +
Rob Clark4996a5f2017-09-09 13:15:52 -0400613 mydata->rootdir_size -
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200614 (mydata->clust_size * 2);
Anssi Hannulaccea7332019-02-27 12:55:57 +0200615
616 /*
617 * The root directory is not cluster-aligned and may be on a
618 * "negative" cluster, this will be handled specially in
619 * next_cluster().
620 */
621 mydata->root_cluster = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200622 }
623
624 mydata->fatbufnum = -1;
Stefan Brüns751b31d2016-09-11 22:51:40 +0200625 mydata->fat_dirty = 0;
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300626 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000627 if (mydata->fatbuf == NULL) {
628 debug("Error: allocating memory\n");
629 return -1;
630 }
wdenk7a428cc2003-06-15 22:40:42 +0000631
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200632 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
633 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
634 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
635 "Data begins at: %d\n",
Rob Clark0c33fce2017-09-09 13:15:53 -0400636 mydata->root_cluster,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200637 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000638 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
639 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
640 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000641
Rob Clark4996a5f2017-09-09 13:15:52 -0400642 return 0;
643}
644
Rob Clark0c33fce2017-09-09 13:15:53 -0400645
646/*
647 * Directory iterator, to simplify filesystem traversal
648 *
649 * Implements an iterator pattern to traverse directory tables,
650 * transparently handling directory tables split across multiple
651 * clusters, and the difference between FAT12/FAT16 root directory
652 * (contiguous) and subdirectories + FAT32 root (chained).
653 *
654 * Rough usage:
655 *
656 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
657 * // to traverse down to a subdirectory pointed to by
658 * // current iterator position:
659 * fat_itr_child(&itr, &itr);
660 * }
661 *
662 * For more complete example, see fat_itr_resolve()
663 */
664
665typedef struct {
666 fsdata *fsdata; /* filesystem parameters */
AKASHI Takahirof0926ae2018-09-11 15:59:09 +0900667 unsigned start_clust; /* first cluster */
Rob Clark0c33fce2017-09-09 13:15:53 -0400668 unsigned clust; /* current cluster */
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900669 unsigned next_clust; /* next cluster if remaining == 0 */
Rob Clark0c33fce2017-09-09 13:15:53 -0400670 int last_cluster; /* set once we've read last cluster */
671 int is_root; /* is iterator at root directory */
672 int remaining; /* remaining dent's in current cluster */
673
674 /* current iterator position values: */
675 dir_entry *dent; /* current directory entry */
676 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
677 char s_name[14]; /* short 8.3 name */
678 char *name; /* l_name if there is one, else s_name */
679
680 /* storage for current cluster in memory: */
681 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
682} fat_itr;
683
684static int fat_itr_isdir(fat_itr *itr);
685
686/**
687 * fat_itr_root() - initialize an iterator to start at the root
688 * directory
689 *
690 * @itr: iterator to initialize
691 * @fsdata: filesystem data for the partition
692 * @return 0 on success, else -errno
693 */
694static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
695{
696 if (get_fs_info(fsdata))
697 return -ENXIO;
698
699 itr->fsdata = fsdata;
AKASHI Takahirof0926ae2018-09-11 15:59:09 +0900700 itr->start_clust = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400701 itr->clust = fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900702 itr->next_clust = fsdata->root_cluster;
Rob Clark0c33fce2017-09-09 13:15:53 -0400703 itr->dent = NULL;
704 itr->remaining = 0;
705 itr->last_cluster = 0;
706 itr->is_root = 1;
707
708 return 0;
709}
710
711/**
712 * fat_itr_child() - initialize an iterator to descend into a sub-
713 * directory
714 *
715 * Initializes 'itr' to iterate the contents of the directory at
716 * the current cursor position of 'parent'. It is an error to
717 * call this if the current cursor of 'parent' is pointing at a
718 * regular file.
719 *
720 * Note that 'itr' and 'parent' can be the same pointer if you do
721 * not need to preserve 'parent' after this call, which is useful
722 * for traversing directory structure to resolve a file/directory.
723 *
724 * @itr: iterator to initialize
725 * @parent: the iterator pointing at a directory entry in the
726 * parent directory of the directory to iterate
727 */
728static void fat_itr_child(fat_itr *itr, fat_itr *parent)
729{
730 fsdata *mydata = parent->fsdata; /* for silly macros */
731 unsigned clustnum = START(parent->dent);
732
733 assert(fat_itr_isdir(parent));
734
735 itr->fsdata = parent->fsdata;
AKASHI Takahirof0926ae2018-09-11 15:59:09 +0900736 itr->start_clust = clustnum;
Rob Clark0c33fce2017-09-09 13:15:53 -0400737 if (clustnum > 0) {
738 itr->clust = clustnum;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900739 itr->next_clust = clustnum;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300740 itr->is_root = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400741 } else {
742 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900743 itr->next_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300744 itr->is_root = 1;
Rob Clark0c33fce2017-09-09 13:15:53 -0400745 }
746 itr->dent = NULL;
747 itr->remaining = 0;
748 itr->last_cluster = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400749}
750
Anssi Hannulaccea7332019-02-27 12:55:57 +0200751static void *next_cluster(fat_itr *itr, unsigned *nbytes)
Rob Clark0c33fce2017-09-09 13:15:53 -0400752{
753 fsdata *mydata = itr->fsdata; /* for silly macros */
754 int ret;
755 u32 sect;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200756 u32 read_size;
Rob Clark0c33fce2017-09-09 13:15:53 -0400757
758 /* have we reached the end? */
759 if (itr->last_cluster)
760 return NULL;
761
Anssi Hannulaccea7332019-02-27 12:55:57 +0200762 if (itr->is_root && itr->fsdata->fatsize != 32) {
763 /*
764 * The root directory is located before the data area and
765 * cannot be indexed using the regular unsigned cluster
766 * numbers (it may start at a "negative" cluster or not at a
767 * cluster boundary at all), so consider itr->next_clust to be
768 * a offset in cluster-sized units from the start of rootdir.
769 */
770 unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
771 unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
772 sect = itr->fsdata->rootdir_sect + sect_offset;
773 /* do not read past the end of rootdir */
774 read_size = min_t(u32, itr->fsdata->clust_size,
775 remaining_sects);
776 } else {
777 sect = clust_to_sect(itr->fsdata, itr->next_clust);
778 read_size = itr->fsdata->clust_size;
779 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400780
Anssi Hannulaccea7332019-02-27 12:55:57 +0200781 debug("FAT read(sect=%d), clust_size=%d, read_size=%u, DIRENTSPERBLOCK=%zd\n",
782 sect, itr->fsdata->clust_size, read_size, DIRENTSPERBLOCK);
Rob Clark0c33fce2017-09-09 13:15:53 -0400783
784 /*
785 * NOTE: do_fat_read_at() had complicated logic to deal w/
786 * vfat names that span multiple clusters in the fat16 case,
787 * which get_dentfromdir() probably also needed (and was
788 * missing). And not entirely sure what fat32 didn't have
789 * the same issue.. We solve that by only caring about one
790 * dent at a time and iteratively constructing the vfat long
791 * name.
792 */
Anssi Hannulaccea7332019-02-27 12:55:57 +0200793 ret = disk_read(sect, read_size, itr->block);
Rob Clark0c33fce2017-09-09 13:15:53 -0400794 if (ret < 0) {
795 debug("Error: reading block\n");
796 return NULL;
797 }
798
Anssi Hannulaccea7332019-02-27 12:55:57 +0200799 *nbytes = read_size * itr->fsdata->sect_size;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900800 itr->clust = itr->next_clust;
Rob Clark0c33fce2017-09-09 13:15:53 -0400801 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900802 itr->next_clust++;
Anssi Hannulaccea7332019-02-27 12:55:57 +0200803 if (itr->next_clust * itr->fsdata->clust_size >=
Rob Clark0c33fce2017-09-09 13:15:53 -0400804 itr->fsdata->rootdir_size) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900805 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400806 itr->last_cluster = 1;
807 }
808 } else {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900809 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
810 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
811 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400812 itr->last_cluster = 1;
813 }
814 }
815
816 return itr->block;
817}
818
819static dir_entry *next_dent(fat_itr *itr)
820{
821 if (itr->remaining == 0) {
Anssi Hannulaccea7332019-02-27 12:55:57 +0200822 unsigned nbytes;
823 struct dir_entry *dent = next_cluster(itr, &nbytes);
Rob Clark0c33fce2017-09-09 13:15:53 -0400824
825 /* have we reached the last cluster? */
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900826 if (!dent) {
827 /* a sign for no more entries left */
828 itr->dent = NULL;
Rob Clark0c33fce2017-09-09 13:15:53 -0400829 return NULL;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900830 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400831
832 itr->remaining = nbytes / sizeof(dir_entry) - 1;
833 itr->dent = dent;
834 } else {
835 itr->remaining--;
836 itr->dent++;
837 }
838
839 /* have we reached the last valid entry? */
840 if (itr->dent->name[0] == 0)
841 return NULL;
842
843 return itr->dent;
844}
845
846static dir_entry *extract_vfat_name(fat_itr *itr)
847{
848 struct dir_entry *dent = itr->dent;
849 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
850 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
851 int n = 0;
852
853 while (seqn--) {
854 char buf[13];
855 int idx = 0;
856
857 slot2str((dir_slot *)dent, buf, &idx);
858
Patrick Wildtd6153f12018-11-26 15:58:13 +0100859 if (n + idx >= sizeof(itr->l_name))
860 return NULL;
861
Rob Clark0c33fce2017-09-09 13:15:53 -0400862 /* shift accumulated long-name up and copy new part in: */
863 memmove(itr->l_name + idx, itr->l_name, n);
864 memcpy(itr->l_name, buf, idx);
865 n += idx;
866
867 dent = next_dent(itr);
868 if (!dent)
869 return NULL;
870 }
871
872 itr->l_name[n] = '\0';
873
874 chksum = mkcksum(dent->name, dent->ext);
875
876 /* checksum mismatch could mean deleted file, etc.. skip it: */
877 if (chksum != alias_checksum) {
878 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
879 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
880 return NULL;
881 }
882
883 return dent;
884}
885
886/**
887 * fat_itr_next() - step to the next entry in a directory
888 *
889 * Must be called once on a new iterator before the cursor is valid.
890 *
891 * @itr: the iterator to iterate
892 * @return boolean, 1 if success or 0 if no more entries in the
893 * current directory
894 */
895static int fat_itr_next(fat_itr *itr)
896{
897 dir_entry *dent;
898
899 itr->name = NULL;
900
901 while (1) {
902 dent = next_dent(itr);
903 if (!dent)
904 return 0;
905
906 if (dent->name[0] == DELETED_FLAG ||
907 dent->name[0] == aRING)
908 continue;
909
910 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynena799dcd2018-01-05 02:45:21 +0200911 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Rob Clark0c33fce2017-09-09 13:15:53 -0400912 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
913 dent = extract_vfat_name(itr);
914 if (!dent)
915 continue;
916 itr->name = itr->l_name;
917 break;
918 } else {
919 /* Volume label or VFAT entry, skip */
920 continue;
921 }
922 }
923
924 break;
925 }
926
927 get_name(dent, itr->s_name);
928 if (!itr->name)
929 itr->name = itr->s_name;
930
931 return 1;
932}
933
934/**
935 * fat_itr_isdir() - is current cursor position pointing to a directory
936 *
937 * @itr: the iterator
938 * @return true if cursor is at a directory
939 */
940static int fat_itr_isdir(fat_itr *itr)
941{
942 return !!(itr->dent->attr & ATTR_DIR);
943}
944
945/*
946 * Helpers:
947 */
948
949#define TYPE_FILE 0x1
950#define TYPE_DIR 0x2
951#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
952
953/**
954 * fat_itr_resolve() - traverse directory structure to resolve the
955 * requested path.
956 *
957 * Traverse directory structure to the requested path. If the specified
958 * path is to a directory, this will descend into the directory and
959 * leave it iterator at the start of the directory. If the path is to a
960 * file, it will leave the iterator in the parent directory with current
961 * cursor at file's entry in the directory.
962 *
963 * @itr: iterator initialized to root
964 * @path: the requested path
965 * @type: bitmask of allowable file types
966 * @return 0 on success or -errno
967 */
968static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
969{
970 const char *next;
971
972 /* chomp any extra leading slashes: */
973 while (path[0] && ISDIRDELIM(path[0]))
974 path++;
975
976 /* are we at the end? */
977 if (strlen(path) == 0) {
978 if (!(type & TYPE_DIR))
979 return -ENOENT;
980 return 0;
981 }
982
983 /* find length of next path entry: */
984 next = path;
985 while (next[0] && !ISDIRDELIM(next[0]))
986 next++;
987
AKASHI Takahirof0a47812018-09-11 15:58:59 +0900988 if (itr->is_root) {
989 /* root dir doesn't have "." nor ".." */
990 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
991 (((next - path) == 2) && !strncmp(path, "..", 2))) {
992 /* point back to itself */
993 itr->clust = itr->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900994 itr->next_clust = itr->fsdata->root_cluster;
AKASHI Takahirof0a47812018-09-11 15:58:59 +0900995 itr->dent = NULL;
996 itr->remaining = 0;
997 itr->last_cluster = 0;
998
999 if (next[0] == 0) {
1000 if (type & TYPE_DIR)
1001 return 0;
1002 else
1003 return -ENOENT;
1004 }
1005
1006 return fat_itr_resolve(itr, next, type);
1007 }
1008 }
1009
Rob Clark0c33fce2017-09-09 13:15:53 -04001010 while (fat_itr_next(itr)) {
1011 int match = 0;
1012 unsigned n = max(strlen(itr->name), (size_t)(next - path));
1013
1014 /* check both long and short name: */
1015 if (!strncasecmp(path, itr->name, n))
1016 match = 1;
1017 else if (itr->name != itr->s_name &&
1018 !strncasecmp(path, itr->s_name, n))
1019 match = 1;
1020
1021 if (!match)
1022 continue;
1023
1024 if (fat_itr_isdir(itr)) {
1025 /* recurse into directory: */
1026 fat_itr_child(itr, itr);
1027 return fat_itr_resolve(itr, next, type);
1028 } else if (next[0]) {
1029 /*
1030 * If next is not empty then we have a case
1031 * like: /path/to/realfile/nonsense
1032 */
1033 debug("bad trailing path: %s\n", next);
1034 return -ENOENT;
1035 } else if (!(type & TYPE_FILE)) {
1036 return -ENOTDIR;
1037 } else {
1038 return 0;
1039 }
1040 }
1041
1042 return -ENOENT;
1043}
1044
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001045int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001046{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001047 boot_sector bs;
1048 volume_info volinfo;
1049 int fatsize;
1050 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001051
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001052 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001053 printf("No current device\n");
1054 return 1;
1055 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001056
Simon Glassb569a012017-05-17 03:25:30 -06001057#if defined(CONFIG_IDE) || \
Simon Glassab3055a2017-06-14 21:28:25 -06001058 defined(CONFIG_SATA) || \
Simon Glass8706b812016-05-01 11:36:02 -06001059 defined(CONFIG_SCSI) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001060 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001061 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001062 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001063 switch (cur_dev->if_type) {
1064 case IF_TYPE_IDE:
1065 printf("IDE");
1066 break;
1067 case IF_TYPE_SATA:
1068 printf("SATA");
1069 break;
1070 case IF_TYPE_SCSI:
1071 printf("SCSI");
1072 break;
1073 case IF_TYPE_ATAPI:
1074 printf("ATAPI");
1075 break;
1076 case IF_TYPE_USB:
1077 printf("USB");
1078 break;
1079 case IF_TYPE_DOC:
1080 printf("DOC");
1081 break;
1082 case IF_TYPE_MMC:
1083 printf("MMC");
1084 break;
1085 default:
1086 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001087 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001088
Simon Glass2f26fff2016-02-29 15:25:51 -07001089 printf("\n Device %d: ", cur_dev->devnum);
wdenk2c9b05d2003-09-10 22:30:53 +00001090 dev_print(cur_dev);
1091#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001092
1093 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001094 printf("\nNo valid FAT fs found\n");
1095 return 1;
1096 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001097
1098 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001099 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001100 volinfo.fs_type[5] = '\0';
1101
Stephen Warrend398dbe2012-10-17 06:44:57 +00001102 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001103
wdenk2c9b05d2003-09-10 22:30:53 +00001104 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001105}
1106
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001107int fat_exists(const char *filename)
1108{
Rob Clark71193c92017-09-09 13:15:54 -04001109 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001110 fat_itr *itr;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001111 int ret;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001112
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001113 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001114 if (!itr)
1115 return 0;
Rob Clark71193c92017-09-09 13:15:54 -04001116 ret = fat_itr_root(itr, &fsdata);
1117 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001118 goto out;
Rob Clark71193c92017-09-09 13:15:54 -04001119
1120 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001121 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001122out:
Tom Rini5257a092017-09-22 07:37:43 -04001123 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001124 return ret == 0;
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001125}
1126
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001127int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001128{
Rob Clark71193c92017-09-09 13:15:54 -04001129 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001130 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001131 int ret;
1132
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001133 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001134 if (!itr)
1135 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001136 ret = fat_itr_root(itr, &fsdata);
1137 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001138 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001139
1140 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1141 if (ret) {
1142 /*
1143 * Directories don't have size, but fs_size() is not
1144 * expected to fail if passed a directory path:
1145 */
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001146 free(fsdata.fatbuf);
Andrew F. Davis8a6c6122019-05-16 09:34:31 -05001147 ret = fat_itr_root(itr, &fsdata);
1148 if (ret)
1149 goto out_free_itr;
1150 ret = fat_itr_resolve(itr, filename, TYPE_DIR);
1151 if (!ret)
Rob Clark71193c92017-09-09 13:15:54 -04001152 *size = 0;
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001153 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001154 }
1155
1156 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001157out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001158 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001159out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001160 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001161 return ret;
Stephen Warren3eb58f52014-06-11 12:47:26 -06001162}
1163
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001164int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1165 loff_t maxsize, loff_t *actread)
wdenk7a428cc2003-06-15 22:40:42 +00001166{
Rob Clark71193c92017-09-09 13:15:54 -04001167 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001168 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001169 int ret;
1170
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001171 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001172 if (!itr)
1173 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001174 ret = fat_itr_root(itr, &fsdata);
1175 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001176 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001177
1178 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1179 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001180 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001181
Andreas Dannenberg315c81d2018-08-13 21:35:15 -05001182 debug("reading %s at pos %llu\n", filename, pos);
Tien Fong Cheed16b79b2019-02-11 14:56:20 +08001183
1184 /* For saving default max clustersize memory allocated to malloc pool */
1185 dir_entry *dentptr = itr->dent;
1186
Tien Fong Cheed16b79b2019-02-11 14:56:20 +08001187 ret = get_contents(&fsdata, dentptr, pos, buffer, maxsize, actread);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001188
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001189out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001190 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001191out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001192 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001193 return ret;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001194}
1195
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001196int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001197{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001198 loff_t actread;
1199 int ret;
1200
1201 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1202 if (ret)
1203 return ret;
1204 else
1205 return actread;
wdenk7a428cc2003-06-15 22:40:42 +00001206}
Simon Glass19e38582012-12-26 09:53:33 +00001207
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001208int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1209 loff_t *actread)
Simon Glass19e38582012-12-26 09:53:33 +00001210{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001211 int ret;
Simon Glass19e38582012-12-26 09:53:33 +00001212
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001213 ret = file_fat_read_at(filename, offset, buf, len, actread);
1214 if (ret)
Simon Glass19e38582012-12-26 09:53:33 +00001215 printf("** Unable to read file %s **\n", filename);
Simon Glass19e38582012-12-26 09:53:33 +00001216
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001217 return ret;
Simon Glass19e38582012-12-26 09:53:33 +00001218}
1219
Rob Clark4174d7f2017-09-09 13:15:56 -04001220typedef struct {
1221 struct fs_dir_stream parent;
1222 struct fs_dirent dirent;
1223 fsdata fsdata;
1224 fat_itr itr;
1225} fat_dir;
1226
1227int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1228{
Neil Armstrong6f625f82017-11-24 09:54:41 +01001229 fat_dir *dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001230 int ret;
1231
Neil Armstrong6f625f82017-11-24 09:54:41 +01001232 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001233 if (!dir)
1234 return -ENOMEM;
Neil Armstrong6f625f82017-11-24 09:54:41 +01001235 memset(dir, 0, sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001236
1237 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1238 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001239 goto fail_free_dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001240
1241 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1242 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001243 goto fail_free_both;
Rob Clark4174d7f2017-09-09 13:15:56 -04001244
1245 *dirsp = (struct fs_dir_stream *)dir;
1246 return 0;
1247
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001248fail_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001249 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001250fail_free_dir:
Rob Clark4174d7f2017-09-09 13:15:56 -04001251 free(dir);
1252 return ret;
1253}
1254
1255int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1256{
1257 fat_dir *dir = (fat_dir *)dirs;
1258 struct fs_dirent *dent = &dir->dirent;
1259
1260 if (!fat_itr_next(&dir->itr))
1261 return -ENOENT;
1262
1263 memset(dent, 0, sizeof(*dent));
1264 strcpy(dent->name, dir->itr.name);
1265
1266 if (fat_itr_isdir(&dir->itr)) {
1267 dent->type = FS_DT_DIR;
1268 } else {
1269 dent->type = FS_DT_REG;
1270 dent->size = FAT2CPU32(dir->itr.dent->size);
1271 }
1272
1273 *dentp = dent;
1274
1275 return 0;
1276}
1277
1278void fat_closedir(struct fs_dir_stream *dirs)
1279{
1280 fat_dir *dir = (fat_dir *)dirs;
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001281 free(dir->fsdata.fatbuf);
Rob Clark4174d7f2017-09-09 13:15:56 -04001282 free(dir);
1283}
1284
Simon Glass19e38582012-12-26 09:53:33 +00001285void fat_close(void)
1286{
1287}