blob: 8122fefae49edb70d8d17401916ab0d4c9000c69 [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);
148#if !defined(CONFIG_FAT_WRITE)
149/* Stub for read only operation */
150int flush_dirty_fat_buffer(fsdata *mydata)
151{
152 (void)(mydata);
153 return 0;
154}
155#endif
156
wdenk7a428cc2003-06-15 22:40:42 +0000157/*
158 * Get the entry at index 'entry' in a FAT (12/16/32) table.
159 * On failure 0x00 is returned.
160 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200161static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000162{
163 __u32 bufnum;
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000164 __u32 offset, off8;
wdenk7a428cc2003-06-15 22:40:42 +0000165 __u32 ret = 0x00;
166
Stefan Brüns9f95d812016-12-17 00:27:51 +0100167 if (CHECK_CLUST(entry, mydata->fatsize)) {
168 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
169 return ret;
170 }
171
wdenk7a428cc2003-06-15 22:40:42 +0000172 switch (mydata->fatsize) {
173 case 32:
174 bufnum = entry / FAT32BUFSIZE;
175 offset = entry - bufnum * FAT32BUFSIZE;
176 break;
177 case 16:
178 bufnum = entry / FAT16BUFSIZE;
179 offset = entry - bufnum * FAT16BUFSIZE;
180 break;
181 case 12:
182 bufnum = entry / FAT12BUFSIZE;
183 offset = entry - bufnum * FAT12BUFSIZE;
184 break;
185
186 default:
187 /* Unsupported FAT size */
188 return ret;
189 }
190
Stefan Brüns9f95d812016-12-17 00:27:51 +0100191 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200192 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200193
wdenk7a428cc2003-06-15 22:40:42 +0000194 /* Read a new block of FAT entries into the cache. */
195 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000196 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000197 __u8 *bufptr = mydata->fatbuf;
198 __u32 fatlength = mydata->fatlength;
199 __u32 startblock = bufnum * FATBUFBLOCKS;
200
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100201 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200202 if (startblock + getsize > fatlength)
203 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000204
wdenk7a428cc2003-06-15 22:40:42 +0000205 startblock += mydata->fat_sect; /* Offset from start of disk */
206
Stefan Brüns9f95d812016-12-17 00:27:51 +0100207 /* Write back the fatbuf to the disk */
208 if (flush_dirty_fat_buffer(mydata) < 0)
209 return -1;
210
wdenk7a428cc2003-06-15 22:40:42 +0000211 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200212 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000213 return ret;
214 }
215 mydata->fatbufnum = bufnum;
216 }
217
218 /* Get the actual entry from the table */
219 switch (mydata->fatsize) {
220 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200221 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000222 break;
223 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200224 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000225 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200226 case 12:
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000227 off8 = (offset * 3) / 2;
228 /* fatbut + off8 may be unaligned, read in byte granularity */
229 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk7a428cc2003-06-15 22:40:42 +0000230
Stefan Brüns15f40632016-12-17 03:55:10 +0100231 if (offset & 0x1)
232 ret >>= 4;
233 ret &= 0xfff;
wdenk7a428cc2003-06-15 22:40:42 +0000234 }
Stefan Brüns9f95d812016-12-17 00:27:51 +0100235 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
236 mydata->fatsize, ret, entry, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000237
238 return ret;
239}
240
wdenk7a428cc2003-06-15 22:40:42 +0000241/*
242 * Read at most 'size' bytes from the specified cluster into 'buffer'.
243 * Return 0 on success, -1 otherwise.
244 */
245static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200246get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000247{
Erik Hansen4ea89662011-03-24 10:15:37 +0100248 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000249 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000250 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000251
252 if (clustnum > 0) {
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400253 startsect = clust_to_sect(mydata, clustnum);
wdenk7a428cc2003-06-15 22:40:42 +0000254 } else {
255 startsect = mydata->rootdir_sect;
256 }
257
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200258 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
259
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200260 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000261 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200262
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200263 printf("FAT: Misaligned buffer address (%p)\n", buffer);
264
265 while (size >= mydata->sect_size) {
266 ret = disk_read(startsect++, 1, tmpbuf);
267 if (ret != 1) {
268 debug("Error reading data (got %d)\n", ret);
269 return -1;
270 }
271
272 memcpy(buffer, tmpbuf, mydata->sect_size);
273 buffer += mydata->sect_size;
274 size -= mydata->sect_size;
275 }
276 } else {
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000277 idx = size / mydata->sect_size;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200278 ret = disk_read(startsect, idx, buffer);
279 if (ret != idx) {
280 debug("Error reading data (got %d)\n", ret);
281 return -1;
282 }
283 startsect += idx;
284 idx *= mydata->sect_size;
285 buffer += idx;
286 size -= idx;
287 }
288 if (size) {
289 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
290
291 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000292 if (ret != 1) {
293 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000294 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000295 }
wdenk2c9b05d2003-09-10 22:30:53 +0000296
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200297 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000298 }
299
300 return 0;
301}
302
wdenk7a428cc2003-06-15 22:40:42 +0000303/*
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000304 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk7a428cc2003-06-15 22:40:42 +0000305 * into 'buffer'.
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800306 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
wdenk7a428cc2003-06-15 22:40:42 +0000307 */
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000308__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
309 __aligned(ARCH_DMA_MINALIGN);
310
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800311static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
312 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk7a428cc2003-06-15 22:40:42 +0000313{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800314 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000315 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000316 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000317 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800318 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000319
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800320 *gotsize = 0;
321 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000322
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000323 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800324 debug("Read position past EOF: %llu\n", pos);
325 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000326 }
327
328 if (maxsize > 0 && filesize > pos + maxsize)
329 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000330
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800331 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000332
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200333 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000334
335 /* go to cluster at pos */
336 while (actsize <= pos) {
337 curclust = get_fatent(mydata, curclust);
338 if (CHECK_CLUST(curclust, mydata->fatsize)) {
339 debug("curclust: 0x%x\n", curclust);
340 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800341 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000342 }
343 actsize += bytesperclust;
344 }
345
346 /* actsize > pos */
347 actsize -= bytesperclust;
348 filesize -= actsize;
349 pos -= actsize;
350
351 /* align to beginning of next cluster if any */
352 if (pos) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800353 actsize = min(filesize, (loff_t)bytesperclust);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000354 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
355 (int)actsize) != 0) {
356 printf("Error reading cluster\n");
357 return -1;
358 }
359 filesize -= actsize;
360 actsize -= pos;
361 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800362 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000363 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800364 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000365 buffer += actsize;
366
367 curclust = get_fatent(mydata, curclust);
368 if (CHECK_CLUST(curclust, mydata->fatsize)) {
369 debug("curclust: 0x%x\n", curclust);
370 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800371 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000372 }
373 }
374
375 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200376 endclust = curclust;
377
wdenk7a428cc2003-06-15 22:40:42 +0000378 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000379 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200380 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000381 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200382 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000383 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100384 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200385 debug("curclust: 0x%x\n", newclust);
386 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800387 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000388 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200389 endclust = newclust;
390 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000391 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200392
wdenk2c9b05d2003-09-10 22:30:53 +0000393 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200394 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200395 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200396 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000397 return -1;
398 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800399 *gotsize += actsize;
400 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000401getit:
402 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200403 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000404 return -1;
405 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800406 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000407 filesize -= actsize;
408 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200409
wdenk2c9b05d2003-09-10 22:30:53 +0000410 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100411 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200412 debug("curclust: 0x%x\n", curclust);
413 printf("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800414 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000415 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200416 actsize = bytesperclust;
417 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000418 } while (1);
419}
420
wdenk7a428cc2003-06-15 22:40:42 +0000421/*
422 * Extract the file name information from 'slotptr' into 'l_name',
423 * starting at l_name[*idx].
424 * Return 1 if terminator (zero byte) is found, 0 otherwise.
425 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200426static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000427{
428 int j;
429
430 for (j = 0; j <= 8; j += 2) {
431 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200432 if (l_name[*idx] == 0x00)
433 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000434 (*idx)++;
435 }
436 for (j = 0; j <= 10; j += 2) {
437 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200438 if (l_name[*idx] == 0x00)
439 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000440 (*idx)++;
441 }
442 for (j = 0; j <= 2; j += 2) {
443 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200444 if (l_name[*idx] == 0x00)
445 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000446 (*idx)++;
447 }
448
449 return 0;
450}
451
wdenk7a428cc2003-06-15 22:40:42 +0000452/* Calculate short name checksum */
Marek Vasut47a470f2012-10-09 07:20:22 +0000453static __u8 mkcksum(const char name[8], const char ext[3])
wdenk7a428cc2003-06-15 22:40:42 +0000454{
455 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200456
wdenk7a428cc2003-06-15 22:40:42 +0000457 __u8 ret = 0;
458
Marek Vasut4ab1f642013-01-11 03:35:48 +0000459 for (i = 0; i < 8; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000460 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut4ab1f642013-01-11 03:35:48 +0000461 for (i = 0; i < 3; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000462 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk7a428cc2003-06-15 22:40:42 +0000463
464 return ret;
465}
wdenk7a428cc2003-06-15 22:40:42 +0000466
467/*
Rob Clark71193c92017-09-09 13:15:54 -0400468 * TODO these should go away once fat_write is reworked to use the
469 * directory iterator
wdenk7a428cc2003-06-15 22:40:42 +0000470 */
Eric Nelson014705b2012-04-11 04:08:53 +0000471__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
472 __aligned(ARCH_DMA_MINALIGN);
Rob Clark71193c92017-09-09 13:15:54 -0400473__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
474 __aligned(ARCH_DMA_MINALIGN);
wdenk7a428cc2003-06-15 22:40:42 +0000475
wdenk7a428cc2003-06-15 22:40:42 +0000476/*
477 * Read boot sector and volume info from a FAT filesystem
478 */
479static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200480read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000481{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000482 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000483 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000484 int ret = 0;
485
486 if (cur_dev == NULL) {
487 debug("Error: no device selected\n");
488 return -1;
489 }
490
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300491 block = malloc_cache_aligned(cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000492 if (block == NULL) {
493 debug("Error: allocating block\n");
494 return -1;
495 }
wdenk7a428cc2003-06-15 22:40:42 +0000496
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200497 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200498 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000499 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000500 }
501
502 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200503 bs->reserved = FAT2CPU16(bs->reserved);
504 bs->fat_length = FAT2CPU16(bs->fat_length);
505 bs->secs_track = FAT2CPU16(bs->secs_track);
506 bs->heads = FAT2CPU16(bs->heads);
507 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000508
509 /* FAT32 entries */
510 if (bs->fat_length == 0) {
511 /* Assume FAT32 */
512 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200513 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000514 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200515 bs->info_sector = FAT2CPU16(bs->info_sector);
516 bs->backup_boot = FAT2CPU16(bs->backup_boot);
517 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000518 *fatsize = 32;
519 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200520 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000521 *fatsize = 0;
522 }
523 memcpy(volinfo, vistart, sizeof(volume_info));
524
wdenk7a428cc2003-06-15 22:40:42 +0000525 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200526 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000527 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000528 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500529 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000530 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000531 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000532 }
Tom Rix155abaa2009-05-20 07:55:41 -0500533 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000534 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000535 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000536 }
537 }
538
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200539 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000540fail:
541 ret = -1;
542exit:
543 free(block);
544 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000545}
546
Rob Clark4996a5f2017-09-09 13:15:52 -0400547static int get_fs_info(fsdata *mydata)
wdenk7a428cc2003-06-15 22:40:42 +0000548{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200549 boot_sector bs;
550 volume_info volinfo;
Rob Clark4996a5f2017-09-09 13:15:52 -0400551 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000552
Rob Clark4996a5f2017-09-09 13:15:52 -0400553 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
554 if (ret) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200555 debug("Error: reading boot sector\n");
Rob Clark4996a5f2017-09-09 13:15:52 -0400556 return ret;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200557 }
558
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000559 if (mydata->fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200560 mydata->fatlength = bs.fat32_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900561 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000562 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200563 mydata->fatlength = bs.fat_length;
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900564 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
565 if (!mydata->total_sect)
566 mydata->total_sect = bs.total_sect;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000567 }
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900568 if (!mydata->total_sect) /* unlikely */
569 mydata->total_sect = (u32)cur_part_info.size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200570
AKASHI Takahiro0d898422018-09-11 15:58:58 +0900571 mydata->fats = bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200572 mydata->fat_sect = bs.reserved;
573
Rob Clark4996a5f2017-09-09 13:15:52 -0400574 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200575
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000576 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200577 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000578 if (mydata->sect_size != cur_part_info.blksz) {
579 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
580 mydata->sect_size, cur_part_info.blksz);
581 return -1;
582 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200583
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200584 if (mydata->fatsize == 32) {
585 mydata->data_begin = mydata->rootdir_sect -
586 (mydata->clust_size * 2);
Rob Clark0c33fce2017-09-09 13:15:53 -0400587 mydata->root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200588 } else {
Rob Clark4996a5f2017-09-09 13:15:52 -0400589 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
590 bs.dir_entries[0]) *
591 sizeof(dir_entry)) /
592 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200593 mydata->data_begin = mydata->rootdir_sect +
Rob Clark4996a5f2017-09-09 13:15:52 -0400594 mydata->rootdir_size -
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200595 (mydata->clust_size * 2);
Rob Clarkc3ab0b82017-09-09 13:16:00 -0400596 mydata->root_cluster =
597 sect_to_clust(mydata, mydata->rootdir_sect);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200598 }
599
600 mydata->fatbufnum = -1;
Stefan Brüns751b31d2016-09-11 22:51:40 +0200601 mydata->fat_dirty = 0;
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +0300602 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000603 if (mydata->fatbuf == NULL) {
604 debug("Error: allocating memory\n");
605 return -1;
606 }
wdenk7a428cc2003-06-15 22:40:42 +0000607
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200608 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
609 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
610 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
611 "Data begins at: %d\n",
Rob Clark0c33fce2017-09-09 13:15:53 -0400612 mydata->root_cluster,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200613 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000614 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
615 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
616 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000617
Rob Clark4996a5f2017-09-09 13:15:52 -0400618 return 0;
619}
620
Rob Clark0c33fce2017-09-09 13:15:53 -0400621
622/*
623 * Directory iterator, to simplify filesystem traversal
624 *
625 * Implements an iterator pattern to traverse directory tables,
626 * transparently handling directory tables split across multiple
627 * clusters, and the difference between FAT12/FAT16 root directory
628 * (contiguous) and subdirectories + FAT32 root (chained).
629 *
630 * Rough usage:
631 *
632 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
633 * // to traverse down to a subdirectory pointed to by
634 * // current iterator position:
635 * fat_itr_child(&itr, &itr);
636 * }
637 *
638 * For more complete example, see fat_itr_resolve()
639 */
640
641typedef struct {
642 fsdata *fsdata; /* filesystem parameters */
643 unsigned clust; /* current cluster */
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900644 unsigned next_clust; /* next cluster if remaining == 0 */
Rob Clark0c33fce2017-09-09 13:15:53 -0400645 int last_cluster; /* set once we've read last cluster */
646 int is_root; /* is iterator at root directory */
647 int remaining; /* remaining dent's in current cluster */
648
649 /* current iterator position values: */
650 dir_entry *dent; /* current directory entry */
651 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
652 char s_name[14]; /* short 8.3 name */
653 char *name; /* l_name if there is one, else s_name */
654
655 /* storage for current cluster in memory: */
656 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
657} fat_itr;
658
659static int fat_itr_isdir(fat_itr *itr);
660
661/**
662 * fat_itr_root() - initialize an iterator to start at the root
663 * directory
664 *
665 * @itr: iterator to initialize
666 * @fsdata: filesystem data for the partition
667 * @return 0 on success, else -errno
668 */
669static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
670{
671 if (get_fs_info(fsdata))
672 return -ENXIO;
673
674 itr->fsdata = fsdata;
675 itr->clust = fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900676 itr->next_clust = fsdata->root_cluster;
Rob Clark0c33fce2017-09-09 13:15:53 -0400677 itr->dent = NULL;
678 itr->remaining = 0;
679 itr->last_cluster = 0;
680 itr->is_root = 1;
681
682 return 0;
683}
684
685/**
686 * fat_itr_child() - initialize an iterator to descend into a sub-
687 * directory
688 *
689 * Initializes 'itr' to iterate the contents of the directory at
690 * the current cursor position of 'parent'. It is an error to
691 * call this if the current cursor of 'parent' is pointing at a
692 * regular file.
693 *
694 * Note that 'itr' and 'parent' can be the same pointer if you do
695 * not need to preserve 'parent' after this call, which is useful
696 * for traversing directory structure to resolve a file/directory.
697 *
698 * @itr: iterator to initialize
699 * @parent: the iterator pointing at a directory entry in the
700 * parent directory of the directory to iterate
701 */
702static void fat_itr_child(fat_itr *itr, fat_itr *parent)
703{
704 fsdata *mydata = parent->fsdata; /* for silly macros */
705 unsigned clustnum = START(parent->dent);
706
707 assert(fat_itr_isdir(parent));
708
709 itr->fsdata = parent->fsdata;
710 if (clustnum > 0) {
711 itr->clust = clustnum;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900712 itr->next_clust = clustnum;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300713 itr->is_root = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400714 } else {
715 itr->clust = parent->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900716 itr->next_clust = parent->fsdata->root_cluster;
Tuomas Tynkkynen3e78e852017-09-25 22:06:33 +0300717 itr->is_root = 1;
Rob Clark0c33fce2017-09-09 13:15:53 -0400718 }
719 itr->dent = NULL;
720 itr->remaining = 0;
721 itr->last_cluster = 0;
Rob Clark0c33fce2017-09-09 13:15:53 -0400722}
723
724static void *next_cluster(fat_itr *itr)
725{
726 fsdata *mydata = itr->fsdata; /* for silly macros */
727 int ret;
728 u32 sect;
729
730 /* have we reached the end? */
731 if (itr->last_cluster)
732 return NULL;
733
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900734 sect = clust_to_sect(itr->fsdata, itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400735
736 debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
737 sect, itr->fsdata->clust_size, DIRENTSPERBLOCK);
738
739 /*
740 * NOTE: do_fat_read_at() had complicated logic to deal w/
741 * vfat names that span multiple clusters in the fat16 case,
742 * which get_dentfromdir() probably also needed (and was
743 * missing). And not entirely sure what fat32 didn't have
744 * the same issue.. We solve that by only caring about one
745 * dent at a time and iteratively constructing the vfat long
746 * name.
747 */
748 ret = disk_read(sect, itr->fsdata->clust_size,
749 itr->block);
750 if (ret < 0) {
751 debug("Error: reading block\n");
752 return NULL;
753 }
754
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900755 itr->clust = itr->next_clust;
Rob Clark0c33fce2017-09-09 13:15:53 -0400756 if (itr->is_root && itr->fsdata->fatsize != 32) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900757 itr->next_clust++;
758 sect = clust_to_sect(itr->fsdata, itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400759 if (sect - itr->fsdata->rootdir_sect >=
760 itr->fsdata->rootdir_size) {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900761 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400762 itr->last_cluster = 1;
763 }
764 } else {
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900765 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
766 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
767 debug("nextclust: 0x%x\n", itr->next_clust);
Rob Clark0c33fce2017-09-09 13:15:53 -0400768 itr->last_cluster = 1;
769 }
770 }
771
772 return itr->block;
773}
774
775static dir_entry *next_dent(fat_itr *itr)
776{
777 if (itr->remaining == 0) {
778 struct dir_entry *dent = next_cluster(itr);
779 unsigned nbytes = itr->fsdata->sect_size *
780 itr->fsdata->clust_size;
781
782 /* have we reached the last cluster? */
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900783 if (!dent) {
784 /* a sign for no more entries left */
785 itr->dent = NULL;
Rob Clark0c33fce2017-09-09 13:15:53 -0400786 return NULL;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900787 }
Rob Clark0c33fce2017-09-09 13:15:53 -0400788
789 itr->remaining = nbytes / sizeof(dir_entry) - 1;
790 itr->dent = dent;
791 } else {
792 itr->remaining--;
793 itr->dent++;
794 }
795
796 /* have we reached the last valid entry? */
797 if (itr->dent->name[0] == 0)
798 return NULL;
799
800 return itr->dent;
801}
802
803static dir_entry *extract_vfat_name(fat_itr *itr)
804{
805 struct dir_entry *dent = itr->dent;
806 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
807 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
808 int n = 0;
809
810 while (seqn--) {
811 char buf[13];
812 int idx = 0;
813
814 slot2str((dir_slot *)dent, buf, &idx);
815
816 /* shift accumulated long-name up and copy new part in: */
817 memmove(itr->l_name + idx, itr->l_name, n);
818 memcpy(itr->l_name, buf, idx);
819 n += idx;
820
821 dent = next_dent(itr);
822 if (!dent)
823 return NULL;
824 }
825
826 itr->l_name[n] = '\0';
827
828 chksum = mkcksum(dent->name, dent->ext);
829
830 /* checksum mismatch could mean deleted file, etc.. skip it: */
831 if (chksum != alias_checksum) {
832 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
833 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
834 return NULL;
835 }
836
837 return dent;
838}
839
840/**
841 * fat_itr_next() - step to the next entry in a directory
842 *
843 * Must be called once on a new iterator before the cursor is valid.
844 *
845 * @itr: the iterator to iterate
846 * @return boolean, 1 if success or 0 if no more entries in the
847 * current directory
848 */
849static int fat_itr_next(fat_itr *itr)
850{
851 dir_entry *dent;
852
853 itr->name = NULL;
854
855 while (1) {
856 dent = next_dent(itr);
857 if (!dent)
858 return 0;
859
860 if (dent->name[0] == DELETED_FLAG ||
861 dent->name[0] == aRING)
862 continue;
863
864 if (dent->attr & ATTR_VOLUME) {
Tuomas Tynkkynena799dcd2018-01-05 02:45:21 +0200865 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
Rob Clark0c33fce2017-09-09 13:15:53 -0400866 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
867 dent = extract_vfat_name(itr);
868 if (!dent)
869 continue;
870 itr->name = itr->l_name;
871 break;
872 } else {
873 /* Volume label or VFAT entry, skip */
874 continue;
875 }
876 }
877
878 break;
879 }
880
881 get_name(dent, itr->s_name);
882 if (!itr->name)
883 itr->name = itr->s_name;
884
885 return 1;
886}
887
888/**
889 * fat_itr_isdir() - is current cursor position pointing to a directory
890 *
891 * @itr: the iterator
892 * @return true if cursor is at a directory
893 */
894static int fat_itr_isdir(fat_itr *itr)
895{
896 return !!(itr->dent->attr & ATTR_DIR);
897}
898
899/*
900 * Helpers:
901 */
902
903#define TYPE_FILE 0x1
904#define TYPE_DIR 0x2
905#define TYPE_ANY (TYPE_FILE | TYPE_DIR)
906
907/**
908 * fat_itr_resolve() - traverse directory structure to resolve the
909 * requested path.
910 *
911 * Traverse directory structure to the requested path. If the specified
912 * path is to a directory, this will descend into the directory and
913 * leave it iterator at the start of the directory. If the path is to a
914 * file, it will leave the iterator in the parent directory with current
915 * cursor at file's entry in the directory.
916 *
917 * @itr: iterator initialized to root
918 * @path: the requested path
919 * @type: bitmask of allowable file types
920 * @return 0 on success or -errno
921 */
922static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
923{
924 const char *next;
925
926 /* chomp any extra leading slashes: */
927 while (path[0] && ISDIRDELIM(path[0]))
928 path++;
929
930 /* are we at the end? */
931 if (strlen(path) == 0) {
932 if (!(type & TYPE_DIR))
933 return -ENOENT;
934 return 0;
935 }
936
937 /* find length of next path entry: */
938 next = path;
939 while (next[0] && !ISDIRDELIM(next[0]))
940 next++;
941
AKASHI Takahirof0a47812018-09-11 15:58:59 +0900942 if (itr->is_root) {
943 /* root dir doesn't have "." nor ".." */
944 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
945 (((next - path) == 2) && !strncmp(path, "..", 2))) {
946 /* point back to itself */
947 itr->clust = itr->fsdata->root_cluster;
AKASHI Takahirob88dff22018-09-11 15:59:00 +0900948 itr->next_clust = itr->fsdata->root_cluster;
AKASHI Takahirof0a47812018-09-11 15:58:59 +0900949 itr->dent = NULL;
950 itr->remaining = 0;
951 itr->last_cluster = 0;
952
953 if (next[0] == 0) {
954 if (type & TYPE_DIR)
955 return 0;
956 else
957 return -ENOENT;
958 }
959
960 return fat_itr_resolve(itr, next, type);
961 }
962 }
963
Rob Clark0c33fce2017-09-09 13:15:53 -0400964 while (fat_itr_next(itr)) {
965 int match = 0;
966 unsigned n = max(strlen(itr->name), (size_t)(next - path));
967
968 /* check both long and short name: */
969 if (!strncasecmp(path, itr->name, n))
970 match = 1;
971 else if (itr->name != itr->s_name &&
972 !strncasecmp(path, itr->s_name, n))
973 match = 1;
974
975 if (!match)
976 continue;
977
978 if (fat_itr_isdir(itr)) {
979 /* recurse into directory: */
980 fat_itr_child(itr, itr);
981 return fat_itr_resolve(itr, next, type);
982 } else if (next[0]) {
983 /*
984 * If next is not empty then we have a case
985 * like: /path/to/realfile/nonsense
986 */
987 debug("bad trailing path: %s\n", next);
988 return -ENOENT;
989 } else if (!(type & TYPE_FILE)) {
990 return -ENOTDIR;
991 } else {
992 return 0;
993 }
994 }
995
996 return -ENOENT;
997}
998
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200999int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001000{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001001 boot_sector bs;
1002 volume_info volinfo;
1003 int fatsize;
1004 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001005
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001006 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001007 printf("No current device\n");
1008 return 1;
1009 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001010
Simon Glassb569a012017-05-17 03:25:30 -06001011#if defined(CONFIG_IDE) || \
Simon Glassab3055a2017-06-14 21:28:25 -06001012 defined(CONFIG_SATA) || \
Simon Glass8706b812016-05-01 11:36:02 -06001013 defined(CONFIG_SCSI) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001014 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001015 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001016 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001017 switch (cur_dev->if_type) {
1018 case IF_TYPE_IDE:
1019 printf("IDE");
1020 break;
1021 case IF_TYPE_SATA:
1022 printf("SATA");
1023 break;
1024 case IF_TYPE_SCSI:
1025 printf("SCSI");
1026 break;
1027 case IF_TYPE_ATAPI:
1028 printf("ATAPI");
1029 break;
1030 case IF_TYPE_USB:
1031 printf("USB");
1032 break;
1033 case IF_TYPE_DOC:
1034 printf("DOC");
1035 break;
1036 case IF_TYPE_MMC:
1037 printf("MMC");
1038 break;
1039 default:
1040 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001041 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001042
Simon Glass2f26fff2016-02-29 15:25:51 -07001043 printf("\n Device %d: ", cur_dev->devnum);
wdenk2c9b05d2003-09-10 22:30:53 +00001044 dev_print(cur_dev);
1045#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001046
1047 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001048 printf("\nNo valid FAT fs found\n");
1049 return 1;
1050 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001051
1052 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001053 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001054 volinfo.fs_type[5] = '\0';
1055
Stephen Warrend398dbe2012-10-17 06:44:57 +00001056 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001057
wdenk2c9b05d2003-09-10 22:30:53 +00001058 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001059}
1060
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001061int fat_exists(const char *filename)
1062{
Rob Clark71193c92017-09-09 13:15:54 -04001063 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001064 fat_itr *itr;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001065 int ret;
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001066
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001067 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001068 if (!itr)
1069 return 0;
Rob Clark71193c92017-09-09 13:15:54 -04001070 ret = fat_itr_root(itr, &fsdata);
1071 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001072 goto out;
Rob Clark71193c92017-09-09 13:15:54 -04001073
1074 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001075 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001076out:
Tom Rini5257a092017-09-22 07:37:43 -04001077 free(itr);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001078 return ret == 0;
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001079}
1080
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001081int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001082{
Rob Clark71193c92017-09-09 13:15:54 -04001083 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001084 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001085 int ret;
1086
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001087 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001088 if (!itr)
1089 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001090 ret = fat_itr_root(itr, &fsdata);
1091 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001092 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001093
1094 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1095 if (ret) {
1096 /*
1097 * Directories don't have size, but fs_size() is not
1098 * expected to fail if passed a directory path:
1099 */
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001100 free(fsdata.fatbuf);
Rob Clark71193c92017-09-09 13:15:54 -04001101 fat_itr_root(itr, &fsdata);
1102 if (!fat_itr_resolve(itr, filename, TYPE_DIR)) {
1103 *size = 0;
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001104 ret = 0;
Rob Clark71193c92017-09-09 13:15:54 -04001105 }
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001106 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001107 }
1108
1109 *size = FAT2CPU32(itr->dent->size);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001110out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001111 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001112out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001113 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001114 return ret;
Stephen Warren3eb58f52014-06-11 12:47:26 -06001115}
1116
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001117int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1118 loff_t maxsize, loff_t *actread)
wdenk7a428cc2003-06-15 22:40:42 +00001119{
Rob Clark71193c92017-09-09 13:15:54 -04001120 fsdata fsdata;
Tom Rini5257a092017-09-22 07:37:43 -04001121 fat_itr *itr;
Rob Clark71193c92017-09-09 13:15:54 -04001122 int ret;
1123
Tuomas Tynkkynenbb93c912017-10-01 02:25:21 +03001124 itr = malloc_cache_aligned(sizeof(fat_itr));
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001125 if (!itr)
1126 return -ENOMEM;
Rob Clark71193c92017-09-09 13:15:54 -04001127 ret = fat_itr_root(itr, &fsdata);
1128 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001129 goto out_free_itr;
Rob Clark71193c92017-09-09 13:15:54 -04001130
1131 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1132 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001133 goto out_free_both;
Rob Clark71193c92017-09-09 13:15:54 -04001134
Andreas Dannenberg315c81d2018-08-13 21:35:15 -05001135 debug("reading %s at pos %llu\n", filename, pos);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001136 ret = get_contents(&fsdata, itr->dent, pos, buffer, maxsize, actread);
1137
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001138out_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001139 free(fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001140out_free_itr:
Tom Rini5257a092017-09-22 07:37:43 -04001141 free(itr);
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001142 return ret;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001143}
1144
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001145int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001146{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001147 loff_t actread;
1148 int ret;
1149
1150 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1151 if (ret)
1152 return ret;
1153 else
1154 return actread;
wdenk7a428cc2003-06-15 22:40:42 +00001155}
Simon Glass19e38582012-12-26 09:53:33 +00001156
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001157int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1158 loff_t *actread)
Simon Glass19e38582012-12-26 09:53:33 +00001159{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001160 int ret;
Simon Glass19e38582012-12-26 09:53:33 +00001161
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001162 ret = file_fat_read_at(filename, offset, buf, len, actread);
1163 if (ret)
Simon Glass19e38582012-12-26 09:53:33 +00001164 printf("** Unable to read file %s **\n", filename);
Simon Glass19e38582012-12-26 09:53:33 +00001165
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001166 return ret;
Simon Glass19e38582012-12-26 09:53:33 +00001167}
1168
Rob Clark4174d7f2017-09-09 13:15:56 -04001169typedef struct {
1170 struct fs_dir_stream parent;
1171 struct fs_dirent dirent;
1172 fsdata fsdata;
1173 fat_itr itr;
1174} fat_dir;
1175
1176int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1177{
Neil Armstrong6f625f82017-11-24 09:54:41 +01001178 fat_dir *dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001179 int ret;
1180
Neil Armstrong6f625f82017-11-24 09:54:41 +01001181 dir = malloc_cache_aligned(sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001182 if (!dir)
1183 return -ENOMEM;
Neil Armstrong6f625f82017-11-24 09:54:41 +01001184 memset(dir, 0, sizeof(*dir));
Rob Clark4174d7f2017-09-09 13:15:56 -04001185
1186 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1187 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001188 goto fail_free_dir;
Rob Clark4174d7f2017-09-09 13:15:56 -04001189
1190 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1191 if (ret)
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001192 goto fail_free_both;
Rob Clark4174d7f2017-09-09 13:15:56 -04001193
1194 *dirsp = (struct fs_dir_stream *)dir;
1195 return 0;
1196
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001197fail_free_both:
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001198 free(dir->fsdata.fatbuf);
Tuomas Tynkkynenf25bbb22017-10-01 02:25:22 +03001199fail_free_dir:
Rob Clark4174d7f2017-09-09 13:15:56 -04001200 free(dir);
1201 return ret;
1202}
1203
1204int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1205{
1206 fat_dir *dir = (fat_dir *)dirs;
1207 struct fs_dirent *dent = &dir->dirent;
1208
1209 if (!fat_itr_next(&dir->itr))
1210 return -ENOENT;
1211
1212 memset(dent, 0, sizeof(*dent));
1213 strcpy(dent->name, dir->itr.name);
1214
1215 if (fat_itr_isdir(&dir->itr)) {
1216 dent->type = FS_DT_DIR;
1217 } else {
1218 dent->type = FS_DT_REG;
1219 dent->size = FAT2CPU32(dir->itr.dent->size);
1220 }
1221
1222 *dentp = dent;
1223
1224 return 0;
1225}
1226
1227void fat_closedir(struct fs_dir_stream *dirs)
1228{
1229 fat_dir *dir = (fat_dir *)dirs;
Rob Clarkd17f4fe2017-09-12 16:40:01 -04001230 free(dir->fsdata.fatbuf);
Rob Clark4174d7f2017-09-09 13:15:56 -04001231 free(dir);
1232}
1233
Simon Glass19e38582012-12-26 09:53:33 +00001234void fat_close(void)
1235{
1236}