blob: bccc3e3ed8fd0929c7f1adf33b2125178be75ccb [file] [log] [blame]
wdenk7a428cc2003-06-15 22:40:42 +00001/*
2 * fat.c
3 *
4 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
5 *
6 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
7 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +000010 */
11
12#include <common.h>
13#include <config.h>
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +000014#include <exports.h>
wdenk7a428cc2003-06-15 22:40:42 +000015#include <fat.h>
16#include <asm/byteorder.h>
wdenk2c9b05d2003-09-10 22:30:53 +000017#include <part.h>
Eric Nelson014705b2012-04-11 04:08:53 +000018#include <malloc.h>
19#include <linux/compiler.h>
Richard Genoud2e372022012-12-13 00:47:36 +000020#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000021
Richard Genouda43f41f2012-12-13 03:30:10 +000022#ifdef CONFIG_SUPPORT_VFAT
23static const int vfat_enabled = 1;
24#else
25static const int vfat_enabled = 0;
26#endif
27
wdenk7a428cc2003-06-15 22:40:42 +000028/*
29 * Convert a string to lowercase.
30 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +020031static void downcase(char *str)
wdenk7a428cc2003-06-15 22:40:42 +000032{
33 while (*str != '\0') {
Richard Genoud2e372022012-12-13 00:47:36 +000034 *str = tolower(*str);
wdenk7a428cc2003-06-15 22:40:42 +000035 str++;
36 }
37}
38
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000039static block_dev_desc_t *cur_dev;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000040static disk_partition_t cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020041
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000042#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000043#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020044#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000045
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000046static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000047{
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000048 if (!cur_dev || !cur_dev->block_read)
wdenk2c9b05d2003-09-10 22:30:53 +000049 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020050
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000051 return cur_dev->block_read(cur_dev->dev,
52 cur_part_info.start + block, nr_blocks, buf);
wdenk7a428cc2003-06-15 22:40:42 +000053}
54
Stephen Warren288904d2012-10-17 06:44:59 +000055int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info)
wdenk7a428cc2003-06-15 22:40:42 +000056{
Eric Nelson014705b2012-04-11 04:08:53 +000057 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000058
Stephen Warren288904d2012-10-17 06:44:59 +000059 cur_dev = dev_desc;
60 cur_part_info = *info;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000061
62 /* Make sure it has a valid FAT header */
63 if (disk_read(0, 1, buffer) != 1) {
64 cur_dev = NULL;
65 return -1;
Wolfgang Denke78f2582007-08-07 16:02:13 +020066 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000067
68 /* Check if it's actually a DOS volume */
69 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
70 cur_dev = NULL;
71 return -1;
72 }
73
74 /* Check for FAT12/FAT16/FAT32 filesystem */
75 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
76 return 0;
77 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
78 return 0;
79
80 cur_dev = NULL;
81 return -1;
wdenk7a428cc2003-06-15 22:40:42 +000082}
83
Stephen Warren288904d2012-10-17 06:44:59 +000084int fat_register_device(block_dev_desc_t *dev_desc, int part_no)
85{
86 disk_partition_t info;
87
88 /* First close any currently found FAT filesystem */
89 cur_dev = NULL;
90
91 /* Read the partition table, if present */
92 if (get_partition_info(dev_desc, part_no, &info)) {
93 if (part_no != 0) {
94 printf("** Partition %d not valid on device %d **\n",
95 part_no, dev_desc->dev);
96 return -1;
97 }
98
99 info.start = 0;
100 info.size = dev_desc->lba;
101 info.blksz = dev_desc->blksz;
102 info.name[0] = 0;
103 info.type[0] = 0;
104 info.bootable = 0;
105#ifdef CONFIG_PARTITION_UUIDS
106 info.uuid[0] = 0;
107#endif
108 }
109
110 return fat_set_blk_dev(dev_desc, &info);
111}
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000112
wdenk7a428cc2003-06-15 22:40:42 +0000113/*
114 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
115 * Return index into string if found, -1 otherwise.
116 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200117static int dirdelim(char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000118{
119 char *start = str;
120
121 while (*str != '\0') {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200122 if (ISDIRDELIM(*str))
123 return str - start;
wdenk7a428cc2003-06-15 22:40:42 +0000124 str++;
125 }
126 return -1;
127}
128
wdenk7a428cc2003-06-15 22:40:42 +0000129/*
130 * Extract zero terminated short name from a directory entry.
131 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200132static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000133{
134 char *ptr;
135
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200136 memcpy(s_name, dirent->name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000137 s_name[8] = '\0';
138 ptr = s_name;
139 while (*ptr && *ptr != ' ')
140 ptr++;
141 if (dirent->ext[0] && dirent->ext[0] != ' ') {
142 *ptr = '.';
143 ptr++;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200144 memcpy(ptr, dirent->ext, 3);
wdenk7a428cc2003-06-15 22:40:42 +0000145 ptr[3] = '\0';
146 while (*ptr && *ptr != ' ')
147 ptr++;
148 }
149 *ptr = '\0';
150 if (*s_name == DELETED_FLAG)
151 *s_name = '\0';
152 else if (*s_name == aRING)
Remy Bohmer7c8f2ce2008-11-27 22:30:27 +0100153 *s_name = DELETED_FLAG;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200154 downcase(s_name);
wdenk7a428cc2003-06-15 22:40:42 +0000155}
156
157/*
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;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200164 __u32 off16, offset;
wdenk7a428cc2003-06-15 22:40:42 +0000165 __u32 ret = 0x00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200166 __u16 val1, val2;
wdenk7a428cc2003-06-15 22:40:42 +0000167
168 switch (mydata->fatsize) {
169 case 32:
170 bufnum = entry / FAT32BUFSIZE;
171 offset = entry - bufnum * FAT32BUFSIZE;
172 break;
173 case 16:
174 bufnum = entry / FAT16BUFSIZE;
175 offset = entry - bufnum * FAT16BUFSIZE;
176 break;
177 case 12:
178 bufnum = entry / FAT12BUFSIZE;
179 offset = entry - bufnum * FAT12BUFSIZE;
180 break;
181
182 default:
183 /* Unsupported FAT size */
184 return ret;
185 }
186
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200187 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
188 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200189
wdenk7a428cc2003-06-15 22:40:42 +0000190 /* Read a new block of FAT entries into the cache. */
191 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000192 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000193 __u8 *bufptr = mydata->fatbuf;
194 __u32 fatlength = mydata->fatlength;
195 __u32 startblock = bufnum * FATBUFBLOCKS;
196
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200197 if (startblock + getsize > fatlength)
198 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000199
wdenk7a428cc2003-06-15 22:40:42 +0000200 startblock += mydata->fat_sect; /* Offset from start of disk */
201
wdenk7a428cc2003-06-15 22:40:42 +0000202 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200203 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000204 return ret;
205 }
206 mydata->fatbufnum = bufnum;
207 }
208
209 /* Get the actual entry from the table */
210 switch (mydata->fatsize) {
211 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200212 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000213 break;
214 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200215 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000216 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200217 case 12:
218 off16 = (offset * 3) / 4;
wdenk7a428cc2003-06-15 22:40:42 +0000219
220 switch (offset & 0x3) {
221 case 0:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200222 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000223 ret &= 0xfff;
224 break;
225 case 1:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200226 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000227 val1 &= 0xf000;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200228 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000229 val2 &= 0x00ff;
230 ret = (val2 << 4) | (val1 >> 12);
231 break;
232 case 2:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200233 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000234 val1 &= 0xff00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200235 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000236 val2 &= 0x000f;
237 ret = (val2 << 8) | (val1 >> 8);
238 break;
239 case 3:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200240 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000241 ret = (ret & 0xfff0) >> 4;
242 break;
243 default:
244 break;
245 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200246 break;
wdenk7a428cc2003-06-15 22:40:42 +0000247 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200248 debug("FAT%d: ret: %08x, offset: %04x\n",
249 mydata->fatsize, ret, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000250
251 return ret;
252}
253
wdenk7a428cc2003-06-15 22:40:42 +0000254/*
255 * Read at most 'size' bytes from the specified cluster into 'buffer'.
256 * Return 0 on success, -1 otherwise.
257 */
258static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200259get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000260{
Erik Hansen4ea89662011-03-24 10:15:37 +0100261 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000262 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000263 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000264
265 if (clustnum > 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200266 startsect = mydata->data_begin +
267 clustnum * mydata->clust_size;
wdenk7a428cc2003-06-15 22:40:42 +0000268 } else {
269 startsect = mydata->rootdir_sect;
270 }
271
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200272 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
273
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200274 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000275 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200276
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200277 printf("FAT: Misaligned buffer address (%p)\n", buffer);
278
279 while (size >= mydata->sect_size) {
280 ret = disk_read(startsect++, 1, tmpbuf);
281 if (ret != 1) {
282 debug("Error reading data (got %d)\n", ret);
283 return -1;
284 }
285
286 memcpy(buffer, tmpbuf, mydata->sect_size);
287 buffer += mydata->sect_size;
288 size -= mydata->sect_size;
289 }
290 } else {
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000291 idx = size / mydata->sect_size;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200292 ret = disk_read(startsect, idx, buffer);
293 if (ret != idx) {
294 debug("Error reading data (got %d)\n", ret);
295 return -1;
296 }
297 startsect += idx;
298 idx *= mydata->sect_size;
299 buffer += idx;
300 size -= idx;
301 }
302 if (size) {
303 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
304
305 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000306 if (ret != 1) {
307 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000308 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000309 }
wdenk2c9b05d2003-09-10 22:30:53 +0000310
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200311 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000312 }
313
314 return 0;
315}
316
wdenk7a428cc2003-06-15 22:40:42 +0000317/*
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000318 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk7a428cc2003-06-15 22:40:42 +0000319 * into 'buffer'.
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800320 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
wdenk7a428cc2003-06-15 22:40:42 +0000321 */
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000322__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
323 __aligned(ARCH_DMA_MINALIGN);
324
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800325static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
326 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk7a428cc2003-06-15 22:40:42 +0000327{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800328 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000329 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000330 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000331 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800332 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000333
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800334 *gotsize = 0;
335 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000336
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000337 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800338 debug("Read position past EOF: %llu\n", pos);
339 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000340 }
341
342 if (maxsize > 0 && filesize > pos + maxsize)
343 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000344
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800345 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000346
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200347 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000348
349 /* go to cluster at pos */
350 while (actsize <= pos) {
351 curclust = get_fatent(mydata, curclust);
352 if (CHECK_CLUST(curclust, mydata->fatsize)) {
353 debug("curclust: 0x%x\n", curclust);
354 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800355 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000356 }
357 actsize += bytesperclust;
358 }
359
360 /* actsize > pos */
361 actsize -= bytesperclust;
362 filesize -= actsize;
363 pos -= actsize;
364
365 /* align to beginning of next cluster if any */
366 if (pos) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800367 actsize = min(filesize, (loff_t)bytesperclust);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000368 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
369 (int)actsize) != 0) {
370 printf("Error reading cluster\n");
371 return -1;
372 }
373 filesize -= actsize;
374 actsize -= pos;
375 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800376 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000377 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800378 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000379 buffer += actsize;
380
381 curclust = get_fatent(mydata, curclust);
382 if (CHECK_CLUST(curclust, mydata->fatsize)) {
383 debug("curclust: 0x%x\n", curclust);
384 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800385 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000386 }
387 }
388
389 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200390 endclust = curclust;
391
wdenk7a428cc2003-06-15 22:40:42 +0000392 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000393 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200394 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000395 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200396 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000397 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100398 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200399 debug("curclust: 0x%x\n", newclust);
400 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800401 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000402 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200403 endclust = newclust;
404 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000405 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200406
wdenk2c9b05d2003-09-10 22:30:53 +0000407 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200408 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200409 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200410 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000411 return -1;
412 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800413 *gotsize += actsize;
414 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000415getit:
416 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200417 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000418 return -1;
419 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800420 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000421 filesize -= actsize;
422 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200423
wdenk2c9b05d2003-09-10 22:30:53 +0000424 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100425 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200426 debug("curclust: 0x%x\n", curclust);
427 printf("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800428 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000429 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200430 actsize = bytesperclust;
431 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000432 } while (1);
433}
434
wdenk7a428cc2003-06-15 22:40:42 +0000435/*
436 * Extract the file name information from 'slotptr' into 'l_name',
437 * starting at l_name[*idx].
438 * Return 1 if terminator (zero byte) is found, 0 otherwise.
439 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200440static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000441{
442 int j;
443
444 for (j = 0; j <= 8; j += 2) {
445 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200446 if (l_name[*idx] == 0x00)
447 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000448 (*idx)++;
449 }
450 for (j = 0; j <= 10; j += 2) {
451 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200452 if (l_name[*idx] == 0x00)
453 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000454 (*idx)++;
455 }
456 for (j = 0; j <= 2; j += 2) {
457 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200458 if (l_name[*idx] == 0x00)
459 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000460 (*idx)++;
461 }
462
463 return 0;
464}
465
wdenk7a428cc2003-06-15 22:40:42 +0000466/*
467 * Extract the full long filename starting at 'retdent' (which is really
468 * a slot) into 'l_name'. If successful also copy the real directory entry
469 * into 'retdent'
470 * Return 0 on success, -1 otherwise.
471 */
472static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200473get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
474 dir_entry *retdent, char *l_name)
wdenk7a428cc2003-06-15 22:40:42 +0000475{
476 dir_entry *realdent;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200477 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000478 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
479 PREFETCH_BLOCKS :
480 mydata->clust_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200481 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk7a428cc2003-06-15 22:40:42 +0000482 int idx = 0;
483
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300484 if (counter > VFAT_MAXSEQ) {
485 debug("Error: VFAT name is too long\n");
486 return -1;
487 }
488
489 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200490 if (counter == 0)
491 break;
wdenk2ebee312004-02-23 19:30:57 +0000492 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
493 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000494 slotptr++;
495 counter--;
496 }
497
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300498 if ((__u8 *)slotptr >= buflimit) {
wdenk7a428cc2003-06-15 22:40:42 +0000499 dir_slot *slotptr2;
500
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300501 if (curclust == 0)
502 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000503 curclust = get_fatent(mydata, curclust);
michael2c4d2982008-03-02 23:33:46 +0100504 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200505 debug("curclust: 0x%x\n", curclust);
506 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000507 return -1;
508 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200509
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000510 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000511 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200512 debug("Error: reading directory block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000513 return -1;
514 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200515
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000516 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300517 while (counter > 0) {
518 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
519 & 0xff) != counter)
520 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000521 slotptr2++;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300522 counter--;
523 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200524
wdenk7a428cc2003-06-15 22:40:42 +0000525 /* Save the real directory entry */
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300526 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000527 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
wdenk7a428cc2003-06-15 22:40:42 +0000528 slotptr2--;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300529 slot2str(slotptr2, l_name, &idx);
wdenk7a428cc2003-06-15 22:40:42 +0000530 }
531 } else {
532 /* Save the real directory entry */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200533 realdent = (dir_entry *)slotptr;
wdenk7a428cc2003-06-15 22:40:42 +0000534 }
535
536 do {
537 slotptr--;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200538 if (slot2str(slotptr, l_name, &idx))
539 break;
wdenk2ebee312004-02-23 19:30:57 +0000540 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk7a428cc2003-06-15 22:40:42 +0000541
542 l_name[idx] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200543 if (*l_name == DELETED_FLAG)
544 *l_name = '\0';
545 else if (*l_name == aRING)
546 *l_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000547 downcase(l_name);
548
549 /* Return the real directory entry */
550 memcpy(retdent, realdent, sizeof(dir_entry));
551
552 return 0;
553}
554
wdenk7a428cc2003-06-15 22:40:42 +0000555/* Calculate short name checksum */
Marek Vasut47a470f2012-10-09 07:20:22 +0000556static __u8 mkcksum(const char name[8], const char ext[3])
wdenk7a428cc2003-06-15 22:40:42 +0000557{
558 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200559
wdenk7a428cc2003-06-15 22:40:42 +0000560 __u8 ret = 0;
561
Marek Vasut4ab1f642013-01-11 03:35:48 +0000562 for (i = 0; i < 8; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000563 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut4ab1f642013-01-11 03:35:48 +0000564 for (i = 0; i < 3; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000565 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk7a428cc2003-06-15 22:40:42 +0000566
567 return ret;
568}
wdenk7a428cc2003-06-15 22:40:42 +0000569
570/*
571 * Get the directory entry associated with 'filename' from the directory
572 * starting at 'startsect'
573 */
Eric Nelson014705b2012-04-11 04:08:53 +0000574__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
575 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200576
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200577static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
578 char *filename, dir_entry *retdent,
579 int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000580{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200581 __u16 prevcksum = 0xffff;
582 __u32 curclust = START(retdent);
583 int files = 0, dirs = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000584
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200585 debug("get_dentfromdir: %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +0000586
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200587 while (1) {
588 dir_entry *dentptr;
589
590 int i;
591
592 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000593 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200594 debug("Error: reading directory block\n");
595 return NULL;
596 }
wdenk7a428cc2003-06-15 22:40:42 +0000597
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200598 dentptr = (dir_entry *)get_dentfromdir_block;
599
600 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300601 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200602
603 l_name[0] = '\0';
604 if (dentptr->name[0] == DELETED_FLAG) {
605 dentptr++;
606 continue;
607 }
608 if ((dentptr->attr & ATTR_VOLUME)) {
Richard Genouda43f41f2012-12-13 03:30:10 +0000609 if (vfat_enabled &&
610 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
J. Vijayanand575e21e2011-10-19 07:43:08 +0000611 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200612 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
613 get_vfatname(mydata, curclust,
614 get_dentfromdir_block,
615 dentptr, l_name);
616 if (dols) {
617 int isdir;
618 char dirc;
619 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000620
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200621 isdir = (dentptr->attr & ATTR_DIR);
622
623 if (isdir) {
624 dirs++;
625 dirc = '/';
626 doit = 1;
627 } else {
628 dirc = ' ';
629 if (l_name[0] != 0) {
630 files++;
631 doit = 1;
632 }
633 }
634 if (doit) {
635 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800636 printf(" %8u %s%c\n",
637 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200638 l_name,
639 dirc);
640 } else {
641 printf(" %s%c\n",
642 l_name,
643 dirc);
644 }
645 }
646 dentptr++;
647 continue;
648 }
649 debug("vfatname: |%s|\n", l_name);
Richard Genouda43f41f2012-12-13 03:30:10 +0000650 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200651 /* Volume label or VFAT entry */
652 dentptr++;
653 continue;
654 }
wdenk7a428cc2003-06-15 22:40:42 +0000655 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200656 if (dentptr->name[0] == 0) {
657 if (dols) {
658 printf("\n%d file(s), %d dir(s)\n\n",
659 files, dirs);
660 }
661 debug("Dentname == NULL - %d\n", i);
662 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000663 }
Richard Genouda43f41f2012-12-13 03:30:10 +0000664 if (vfat_enabled) {
665 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
666 if (dols && csum == prevcksum) {
667 prevcksum = 0xffff;
668 dentptr++;
669 continue;
670 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200671 }
Richard Genouda43f41f2012-12-13 03:30:10 +0000672
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200673 get_name(dentptr, s_name);
674 if (dols) {
675 int isdir = (dentptr->attr & ATTR_DIR);
676 char dirc;
677 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000678
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200679 if (isdir) {
680 dirs++;
681 dirc = '/';
682 doit = 1;
683 } else {
684 dirc = ' ';
685 if (s_name[0] != 0) {
686 files++;
687 doit = 1;
688 }
689 }
wdenk7a428cc2003-06-15 22:40:42 +0000690
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200691 if (doit) {
692 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800693 printf(" %8u %s%c\n",
694 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200695 s_name, dirc);
696 } else {
697 printf(" %s%c\n",
698 s_name, dirc);
699 }
700 }
wdenk7a428cc2003-06-15 22:40:42 +0000701
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200702 dentptr++;
703 continue;
704 }
705
706 if (strcmp(filename, s_name)
707 && strcmp(filename, l_name)) {
708 debug("Mismatch: |%s|%s|\n", s_name, l_name);
709 dentptr++;
710 continue;
711 }
712
713 memcpy(retdent, dentptr, sizeof(dir_entry));
714
715 debug("DentName: %s", s_name);
716 debug(", start: 0x%x", START(dentptr));
717 debug(", size: 0x%x %s\n",
718 FAT2CPU32(dentptr->size),
719 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
720
721 return retdent;
722 }
723
724 curclust = get_fatent(mydata, curclust);
725 if (CHECK_CLUST(curclust, mydata->fatsize)) {
726 debug("curclust: 0x%x\n", curclust);
727 printf("Invalid FAT entry\n");
728 return NULL;
729 }
wdenk7a428cc2003-06-15 22:40:42 +0000730 }
wdenk7a428cc2003-06-15 22:40:42 +0000731
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200732 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000733}
734
wdenk7a428cc2003-06-15 22:40:42 +0000735/*
736 * Read boot sector and volume info from a FAT filesystem
737 */
738static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200739read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000740{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000741 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000742 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000743 int ret = 0;
744
745 if (cur_dev == NULL) {
746 debug("Error: no device selected\n");
747 return -1;
748 }
749
Eric Nelson014705b2012-04-11 04:08:53 +0000750 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000751 if (block == NULL) {
752 debug("Error: allocating block\n");
753 return -1;
754 }
wdenk7a428cc2003-06-15 22:40:42 +0000755
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200756 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200757 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000758 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000759 }
760
761 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200762 bs->reserved = FAT2CPU16(bs->reserved);
763 bs->fat_length = FAT2CPU16(bs->fat_length);
764 bs->secs_track = FAT2CPU16(bs->secs_track);
765 bs->heads = FAT2CPU16(bs->heads);
766 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000767
768 /* FAT32 entries */
769 if (bs->fat_length == 0) {
770 /* Assume FAT32 */
771 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200772 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000773 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200774 bs->info_sector = FAT2CPU16(bs->info_sector);
775 bs->backup_boot = FAT2CPU16(bs->backup_boot);
776 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000777 *fatsize = 32;
778 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200779 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000780 *fatsize = 0;
781 }
782 memcpy(volinfo, vistart, sizeof(volume_info));
783
wdenk7a428cc2003-06-15 22:40:42 +0000784 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200785 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000786 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000787 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500788 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000789 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000790 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000791 }
Tom Rix155abaa2009-05-20 07:55:41 -0500792 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000793 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000794 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000795 }
796 }
797
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200798 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000799fail:
800 ret = -1;
801exit:
802 free(block);
803 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000804}
805
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000806__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
Eric Nelson014705b2012-04-11 04:08:53 +0000807 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200808
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800809int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
810 loff_t maxsize, int dols, int dogetsize, loff_t *size)
wdenk7a428cc2003-06-15 22:40:42 +0000811{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200812 char fnamecopy[2048];
813 boot_sector bs;
814 volume_info volinfo;
815 fsdata datablock;
816 fsdata *mydata = &datablock;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200817 dir_entry *dentptr = NULL;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200818 __u16 prevcksum = 0xffff;
819 char *subname = "";
Erik Hansen4ea89662011-03-24 10:15:37 +0100820 __u32 cursect;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200821 int idx, isdir = 0;
822 int files = 0, dirs = 0;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800823 int ret = -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200824 int firsttime;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000825 __u32 root_cluster = 0;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100826 __u32 read_blk;
Erik Hansen4ea89662011-03-24 10:15:37 +0100827 int rootdir_size = 0;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100828 int buffer_blk_cnt;
829 int do_read;
830 __u8 *dir_ptr;
wdenk7a428cc2003-06-15 22:40:42 +0000831
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200832 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
833 debug("Error: reading boot sector\n");
834 return -1;
835 }
836
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000837 if (mydata->fatsize == 32) {
838 root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200839 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000840 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200841 mydata->fatlength = bs.fat_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000842 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200843
844 mydata->fat_sect = bs.reserved;
845
846 cursect = mydata->rootdir_sect
847 = mydata->fat_sect + mydata->fatlength * bs.fats;
848
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000849 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200850 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000851 if (mydata->sect_size != cur_part_info.blksz) {
852 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
853 mydata->sect_size, cur_part_info.blksz);
854 return -1;
855 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200856
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200857 if (mydata->fatsize == 32) {
858 mydata->data_begin = mydata->rootdir_sect -
859 (mydata->clust_size * 2);
860 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200861 rootdir_size = ((bs.dir_entries[1] * (int)256 +
862 bs.dir_entries[0]) *
863 sizeof(dir_entry)) /
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000864 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200865 mydata->data_begin = mydata->rootdir_sect +
866 rootdir_size -
867 (mydata->clust_size * 2);
868 }
869
870 mydata->fatbufnum = -1;
Eric Nelson014705b2012-04-11 04:08:53 +0000871 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000872 if (mydata->fatbuf == NULL) {
873 debug("Error: allocating memory\n");
874 return -1;
875 }
wdenk7a428cc2003-06-15 22:40:42 +0000876
Richard Genouda43f41f2012-12-13 03:30:10 +0000877 if (vfat_enabled)
878 debug("VFAT Support enabled\n");
879
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200880 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
881 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
882 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
883 "Data begins at: %d\n",
884 root_cluster,
885 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000886 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
887 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
888 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000889
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200890 /* "cwd" is always the root... */
891 while (ISDIRDELIM(*filename))
892 filename++;
wdenk7a428cc2003-06-15 22:40:42 +0000893
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200894 /* Make a copy of the filename and convert it to lowercase */
895 strcpy(fnamecopy, filename);
896 downcase(fnamecopy);
wdenk7a428cc2003-06-15 22:40:42 +0000897
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200898 if (*fnamecopy == '\0') {
899 if (!dols)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000900 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200901
902 dols = LS_ROOT;
903 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
904 isdir = 1;
905 fnamecopy[idx] = '\0';
906 subname = fnamecopy + idx + 1;
907
908 /* Handle multiple delimiters */
909 while (ISDIRDELIM(*subname))
910 subname++;
911 } else if (dols) {
912 isdir = 1;
wdenk7a428cc2003-06-15 22:40:42 +0000913 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200914
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100915 buffer_blk_cnt = 0;
916 firsttime = 1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200917 while (1) {
918 int i;
919
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100920 if (mydata->fatsize == 32 || firsttime) {
921 dir_ptr = do_fat_read_at_block;
922 firsttime = 0;
923 } else {
924 /**
925 * FAT16 sector buffer modification:
926 * Each loop, the second buffered block is moved to
927 * the buffer begin, and two next sectors are read
928 * next to the previously moved one. So the sector
929 * buffer keeps always 3 sectors for fat16.
930 * And the current sector is the buffer second sector
931 * beside the "firsttime" read, when it is the first one.
932 *
933 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
934 * n = computed root dir sector
935 * loop | cursect-1 | cursect | cursect+1 |
936 * 0 | sector n+0 | sector n+1 | none |
937 * 1 | none | sector n+0 | sector n+1 |
938 * 0 | sector n+1 | sector n+2 | sector n+3 |
939 * 1 | sector n+3 | ...
940 */
941 dir_ptr = (do_fat_read_at_block + mydata->sect_size);
942 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size);
943 }
944
945 do_read = 1;
946
947 if (mydata->fatsize == 32 && buffer_blk_cnt)
948 do_read = 0;
949
950 if (do_read) {
951 read_blk = (mydata->fatsize == 32) ?
952 mydata->clust_size : PREFETCH_BLOCKS;
953
954 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
955 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200956
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100957 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200958 debug("Error: reading rootdir block\n");
959 goto exit;
960 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200961
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100962 dentptr = (dir_entry *)dir_ptr;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200963 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200964
965 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300966 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Marek Vasut47a470f2012-10-09 07:20:22 +0000967 __u8 csum;
wdenk7a428cc2003-06-15 22:40:42 +0000968
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200969 l_name[0] = '\0';
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300970 if (dentptr->name[0] == DELETED_FLAG) {
971 dentptr++;
972 continue;
973 }
Marek Vasut47a470f2012-10-09 07:20:22 +0000974
Richard Genouda43f41f2012-12-13 03:30:10 +0000975 if (vfat_enabled)
976 csum = mkcksum(dentptr->name, dentptr->ext);
977
Marek Vasut47a470f2012-10-09 07:20:22 +0000978 if (dentptr->attr & ATTR_VOLUME) {
Richard Genouda43f41f2012-12-13 03:30:10 +0000979 if (vfat_enabled &&
980 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200981 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
982 prevcksum =
983 ((dir_slot *)dentptr)->alias_checksum;
wdenk7a428cc2003-06-15 22:40:42 +0000984
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300985 get_vfatname(mydata,
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000986 root_cluster,
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100987 dir_ptr,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200988 dentptr, l_name);
989
990 if (dols == LS_ROOT) {
991 char dirc;
992 int doit = 0;
993 int isdir =
994 (dentptr->attr & ATTR_DIR);
995
996 if (isdir) {
997 dirs++;
998 dirc = '/';
999 doit = 1;
1000 } else {
1001 dirc = ' ';
1002 if (l_name[0] != 0) {
1003 files++;
1004 doit = 1;
1005 }
1006 }
1007 if (doit) {
1008 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001009 printf(" %8u %s%c\n",
1010 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001011 l_name,
1012 dirc);
1013 } else {
1014 printf(" %s%c\n",
1015 l_name,
1016 dirc);
1017 }
1018 }
1019 dentptr++;
1020 continue;
1021 }
1022 debug("Rootvfatname: |%s|\n",
1023 l_name);
Richard Genouda43f41f2012-12-13 03:30:10 +00001024 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001025 /* Volume label or VFAT entry */
1026 dentptr++;
1027 continue;
1028 }
1029 } else if (dentptr->name[0] == 0) {
1030 debug("RootDentname == NULL - %d\n", i);
1031 if (dols == LS_ROOT) {
1032 printf("\n%d file(s), %d dir(s)\n\n",
1033 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001034 ret = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001035 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001036 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001037 }
Richard Genouda43f41f2012-12-13 03:30:10 +00001038 else if (vfat_enabled &&
1039 dols == LS_ROOT && csum == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +00001040 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001041 dentptr++;
1042 continue;
1043 }
Richard Genouda43f41f2012-12-13 03:30:10 +00001044
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001045 get_name(dentptr, s_name);
wdenk7a428cc2003-06-15 22:40:42 +00001046
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001047 if (dols == LS_ROOT) {
1048 int isdir = (dentptr->attr & ATTR_DIR);
1049 char dirc;
1050 int doit = 0;
1051
1052 if (isdir) {
1053 dirc = '/';
1054 if (s_name[0] != 0) {
1055 dirs++;
1056 doit = 1;
1057 }
1058 } else {
1059 dirc = ' ';
1060 if (s_name[0] != 0) {
1061 files++;
1062 doit = 1;
1063 }
1064 }
1065 if (doit) {
1066 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001067 printf(" %8u %s%c\n",
1068 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001069 s_name, dirc);
1070 } else {
1071 printf(" %s%c\n",
1072 s_name, dirc);
1073 }
1074 }
1075 dentptr++;
1076 continue;
1077 }
1078
1079 if (strcmp(fnamecopy, s_name)
1080 && strcmp(fnamecopy, l_name)) {
1081 debug("RootMismatch: |%s|%s|\n", s_name,
1082 l_name);
1083 dentptr++;
1084 continue;
1085 }
1086
1087 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001088 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001089
1090 debug("RootName: %s", s_name);
1091 debug(", start: 0x%x", START(dentptr));
1092 debug(", size: 0x%x %s\n",
1093 FAT2CPU32(dentptr->size),
1094 isdir ? "(DIR)" : "");
1095
1096 goto rootdir_done; /* We got a match */
wdenk7a428cc2003-06-15 22:40:42 +00001097 }
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001098 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001099 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +00001100
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001101 /*
1102 * On FAT32 we must fetch the FAT entries for the next
1103 * root directory clusters when a cluster has been
1104 * completely processed.
1105 */
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001106 ++buffer_blk_cnt;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001107 int rootdir_end = 0;
1108 if (mydata->fatsize == 32) {
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001109 if (buffer_blk_cnt == mydata->clust_size) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001110 int nxtsect = 0;
1111 int nxt_clust = 0;
wdenk7a428cc2003-06-15 22:40:42 +00001112
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001113 nxt_clust = get_fatent(mydata, root_cluster);
1114 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001115
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001116 nxtsect = mydata->data_begin +
1117 (nxt_clust * mydata->clust_size);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001118
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001119 root_cluster = nxt_clust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001120
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001121 cursect = nxtsect;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001122 buffer_blk_cnt = 0;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001123 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001124 } else {
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001125 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1126 buffer_blk_cnt = 0;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001127
1128 rootdir_end = (++cursect - mydata->rootdir_sect >=
1129 rootdir_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001130 }
Erik Hansen4ea89662011-03-24 10:15:37 +01001131
1132 /* If end of rootdir reached */
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001133 if (rootdir_end) {
Erik Hansen4ea89662011-03-24 10:15:37 +01001134 if (dols == LS_ROOT) {
1135 printf("\n%d file(s), %d dir(s)\n\n",
1136 files, dirs);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001137 *size = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +01001138 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001139 goto exit;
Erik Hansen4ea89662011-03-24 10:15:37 +01001140 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001141 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001142rootdir_done:
wdenk7a428cc2003-06-15 22:40:42 +00001143
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001144 firsttime = 1;
wdenk7a428cc2003-06-15 22:40:42 +00001145
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001146 while (isdir) {
1147 int startsect = mydata->data_begin
1148 + START(dentptr) * mydata->clust_size;
1149 dir_entry dent;
1150 char *nextname = NULL;
wdenk7a428cc2003-06-15 22:40:42 +00001151
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001152 dent = *dentptr;
1153 dentptr = &dent;
wdenk7a428cc2003-06-15 22:40:42 +00001154
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001155 idx = dirdelim(subname);
wdenk7a428cc2003-06-15 22:40:42 +00001156
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001157 if (idx >= 0) {
1158 subname[idx] = '\0';
1159 nextname = subname + idx + 1;
1160 /* Handle multiple delimiters */
1161 while (ISDIRDELIM(*nextname))
1162 nextname++;
1163 if (dols && *nextname == '\0')
1164 firsttime = 0;
1165 } else {
1166 if (dols && firsttime) {
1167 firsttime = 0;
1168 } else {
1169 isdir = 0;
1170 }
1171 }
1172
1173 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1174 isdir ? 0 : dols) == NULL) {
1175 if (dols && !isdir)
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001176 *size = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001177 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001178 }
1179
Benoît Thébaudeau7ca2a8a2012-07-20 03:20:29 +00001180 if (isdir && !(dentptr->attr & ATTR_DIR))
1181 goto exit;
1182
1183 if (idx >= 0)
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001184 subname = nextname;
wdenk7a428cc2003-06-15 22:40:42 +00001185 }
wdenk7a428cc2003-06-15 22:40:42 +00001186
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001187 if (dogetsize) {
1188 *size = FAT2CPU32(dentptr->size);
1189 ret = 0;
1190 } else {
1191 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
1192 }
1193 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
wdenk7a428cc2003-06-15 22:40:42 +00001194
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001195exit:
1196 free(mydata->fatbuf);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001197 return ret;
1198}
wdenk7a428cc2003-06-15 22:40:42 +00001199
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001200int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
1201 loff_t *actread)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001202{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001203 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001204}
1205
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001206int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001207{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001208 boot_sector bs;
1209 volume_info volinfo;
1210 int fatsize;
1211 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001212
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001213 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001214 printf("No current device\n");
1215 return 1;
1216 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001217
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001218#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang853208e2008-12-09 23:20:18 -05001219 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001220 defined(CONFIG_CMD_SCSI) || \
1221 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001222 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001223 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001224 switch (cur_dev->if_type) {
1225 case IF_TYPE_IDE:
1226 printf("IDE");
1227 break;
1228 case IF_TYPE_SATA:
1229 printf("SATA");
1230 break;
1231 case IF_TYPE_SCSI:
1232 printf("SCSI");
1233 break;
1234 case IF_TYPE_ATAPI:
1235 printf("ATAPI");
1236 break;
1237 case IF_TYPE_USB:
1238 printf("USB");
1239 break;
1240 case IF_TYPE_DOC:
1241 printf("DOC");
1242 break;
1243 case IF_TYPE_MMC:
1244 printf("MMC");
1245 break;
1246 default:
1247 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001248 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001249
1250 printf("\n Device %d: ", cur_dev->dev);
wdenk2c9b05d2003-09-10 22:30:53 +00001251 dev_print(cur_dev);
1252#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001253
1254 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001255 printf("\nNo valid FAT fs found\n");
1256 return 1;
1257 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001258
1259 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001260 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001261 volinfo.fs_type[5] = '\0';
1262
Stephen Warrend398dbe2012-10-17 06:44:57 +00001263 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001264
wdenk2c9b05d2003-09-10 22:30:53 +00001265 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001266}
1267
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001268int file_fat_ls(const char *dir)
wdenk7a428cc2003-06-15 22:40:42 +00001269{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001270 loff_t size;
1271
1272 return do_fat_read(dir, NULL, 0, LS_YES, &size);
wdenk7a428cc2003-06-15 22:40:42 +00001273}
1274
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001275int fat_exists(const char *filename)
1276{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001277 int ret;
1278 loff_t size;
1279
1280 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1281 return ret == 0;
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001282}
1283
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001284int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001285{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001286 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -06001287}
1288
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001289int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1290 loff_t maxsize, loff_t *actread)
wdenk7a428cc2003-06-15 22:40:42 +00001291{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001292 printf("reading %s\n", filename);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001293 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1294 actread);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001295}
1296
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001297int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001298{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001299 loff_t actread;
1300 int ret;
1301
1302 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1303 if (ret)
1304 return ret;
1305 else
1306 return actread;
wdenk7a428cc2003-06-15 22:40:42 +00001307}
Simon Glass19e38582012-12-26 09:53:33 +00001308
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001309int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1310 loff_t *actread)
Simon Glass19e38582012-12-26 09:53:33 +00001311{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001312 int ret;
Simon Glass19e38582012-12-26 09:53:33 +00001313
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001314 ret = file_fat_read_at(filename, offset, buf, len, actread);
1315 if (ret)
Simon Glass19e38582012-12-26 09:53:33 +00001316 printf("** Unable to read file %s **\n", filename);
Simon Glass19e38582012-12-26 09:53:33 +00001317
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001318 return ret;
Simon Glass19e38582012-12-26 09:53:33 +00001319}
1320
1321void fat_close(void)
1322{
1323}