blob: 06dcbb70057488b46b4a2d85afb08bbfeb7e2b4c [file] [log] [blame]
wdenk71f95112003-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 Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenk71f95112003-06-15 22:40:42 +000010 */
11
12#include <common.h>
13#include <config.h>
Sergei Shtylyovac497772011-08-08 09:38:33 +000014#include <exports.h>
wdenk71f95112003-06-15 22:40:42 +000015#include <fat.h>
16#include <asm/byteorder.h>
wdenk7205e402003-09-10 22:30:53 +000017#include <part.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000018#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060019#include <memalign.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000020#include <linux/compiler.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000021#include <linux/ctype.h>
wdenk71f95112003-06-15 22:40:42 +000022
Richard Genoudcb940c72012-12-13 03:30:10 +000023#ifdef CONFIG_SUPPORT_VFAT
24static const int vfat_enabled = 1;
25#else
26static const int vfat_enabled = 0;
27#endif
28
wdenk71f95112003-06-15 22:40:42 +000029/*
30 * Convert a string to lowercase.
31 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +020032static void downcase(char *str)
wdenk71f95112003-06-15 22:40:42 +000033{
34 while (*str != '\0') {
Richard Genoudfb7e16c2012-12-13 00:47:36 +000035 *str = tolower(*str);
wdenk71f95112003-06-15 22:40:42 +000036 str++;
37 }
38}
39
Kyle Moffett9813b752011-12-21 07:08:10 +000040static block_dev_desc_t *cur_dev;
Kyle Moffett9813b752011-12-21 07:08:10 +000041static disk_partition_t cur_part_info;
Wolfgang Denk7385c282010-07-19 11:37:00 +020042
Kyle Moffett9813b752011-12-21 07:08:10 +000043#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk7205e402003-09-10 22:30:53 +000044#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk66c2d732010-07-19 11:36:57 +020045#define DOS_FS32_TYPE_OFFSET 0x52
wdenk71f95112003-06-15 22:40:42 +000046
Kyle Moffett9813b752011-12-21 07:08:10 +000047static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk71f95112003-06-15 22:40:42 +000048{
Kyle Moffett9813b752011-12-21 07:08:10 +000049 if (!cur_dev || !cur_dev->block_read)
wdenk7205e402003-09-10 22:30:53 +000050 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020051
Kyle Moffett9813b752011-12-21 07:08:10 +000052 return cur_dev->block_read(cur_dev->dev,
53 cur_part_info.start + block, nr_blocks, buf);
wdenk71f95112003-06-15 22:40:42 +000054}
55
Stephen Warren5e8f9832012-10-17 06:44:59 +000056int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info)
wdenk71f95112003-06-15 22:40:42 +000057{
Eric Nelson9a800ac2012-04-11 04:08:53 +000058 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk7205e402003-09-10 22:30:53 +000059
Stephen Warren5e8f9832012-10-17 06:44:59 +000060 cur_dev = dev_desc;
61 cur_part_info = *info;
Kyle Moffett9813b752011-12-21 07:08:10 +000062
63 /* Make sure it has a valid FAT header */
64 if (disk_read(0, 1, buffer) != 1) {
65 cur_dev = NULL;
66 return -1;
67 }
68
69 /* Check if it's actually a DOS volume */
70 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
71 cur_dev = NULL;
72 return -1;
73 }
74
75 /* Check for FAT12/FAT16/FAT32 filesystem */
76 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
77 return 0;
78 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
79 return 0;
80
81 cur_dev = NULL;
82 return -1;
wdenk71f95112003-06-15 22:40:42 +000083}
84
Stephen Warren5e8f9832012-10-17 06:44:59 +000085int fat_register_device(block_dev_desc_t *dev_desc, int part_no)
86{
87 disk_partition_t info;
88
89 /* First close any currently found FAT filesystem */
90 cur_dev = NULL;
91
92 /* Read the partition table, if present */
93 if (get_partition_info(dev_desc, part_no, &info)) {
94 if (part_no != 0) {
95 printf("** Partition %d not valid on device %d **\n",
96 part_no, dev_desc->dev);
97 return -1;
98 }
99
100 info.start = 0;
101 info.size = dev_desc->lba;
102 info.blksz = dev_desc->blksz;
103 info.name[0] = 0;
104 info.type[0] = 0;
105 info.bootable = 0;
106#ifdef CONFIG_PARTITION_UUIDS
107 info.uuid[0] = 0;
108#endif
109 }
110
111 return fat_set_blk_dev(dev_desc, &info);
112}
Kyle Moffett9813b752011-12-21 07:08:10 +0000113
wdenk71f95112003-06-15 22:40:42 +0000114/*
115 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
116 * Return index into string if found, -1 otherwise.
117 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200118static int dirdelim(char *str)
wdenk71f95112003-06-15 22:40:42 +0000119{
120 char *start = str;
121
122 while (*str != '\0') {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200123 if (ISDIRDELIM(*str))
124 return str - start;
wdenk71f95112003-06-15 22:40:42 +0000125 str++;
126 }
127 return -1;
128}
129
wdenk71f95112003-06-15 22:40:42 +0000130/*
131 * Extract zero terminated short name from a directory entry.
132 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200133static void get_name(dir_entry *dirent, char *s_name)
wdenk71f95112003-06-15 22:40:42 +0000134{
135 char *ptr;
136
Wolfgang Denk7385c282010-07-19 11:37:00 +0200137 memcpy(s_name, dirent->name, 8);
wdenk71f95112003-06-15 22:40:42 +0000138 s_name[8] = '\0';
139 ptr = s_name;
140 while (*ptr && *ptr != ' ')
141 ptr++;
142 if (dirent->ext[0] && dirent->ext[0] != ' ') {
143 *ptr = '.';
144 ptr++;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200145 memcpy(ptr, dirent->ext, 3);
wdenk71f95112003-06-15 22:40:42 +0000146 ptr[3] = '\0';
147 while (*ptr && *ptr != ' ')
148 ptr++;
149 }
150 *ptr = '\0';
151 if (*s_name == DELETED_FLAG)
152 *s_name = '\0';
153 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100154 *s_name = DELETED_FLAG;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200155 downcase(s_name);
wdenk71f95112003-06-15 22:40:42 +0000156}
157
158/*
159 * Get the entry at index 'entry' in a FAT (12/16/32) table.
160 * On failure 0x00 is returned.
161 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200162static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000163{
164 __u32 bufnum;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200165 __u32 off16, offset;
wdenk71f95112003-06-15 22:40:42 +0000166 __u32 ret = 0x00;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200167 __u16 val1, val2;
wdenk71f95112003-06-15 22:40:42 +0000168
169 switch (mydata->fatsize) {
170 case 32:
171 bufnum = entry / FAT32BUFSIZE;
172 offset = entry - bufnum * FAT32BUFSIZE;
173 break;
174 case 16:
175 bufnum = entry / FAT16BUFSIZE;
176 offset = entry - bufnum * FAT16BUFSIZE;
177 break;
178 case 12:
179 bufnum = entry / FAT12BUFSIZE;
180 offset = entry - bufnum * FAT12BUFSIZE;
181 break;
182
183 default:
184 /* Unsupported FAT size */
185 return ret;
186 }
187
Wolfgang Denk7385c282010-07-19 11:37:00 +0200188 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
189 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200190
wdenk71f95112003-06-15 22:40:42 +0000191 /* Read a new block of FAT entries into the cache. */
192 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000193 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000194 __u8 *bufptr = mydata->fatbuf;
195 __u32 fatlength = mydata->fatlength;
196 __u32 startblock = bufnum * FATBUFBLOCKS;
197
Benoît Thébaudeau8006dd22012-07-20 15:19:29 +0200198 if (startblock + getsize > fatlength)
199 getsize = fatlength - startblock;
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000200
wdenk71f95112003-06-15 22:40:42 +0000201 startblock += mydata->fat_sect; /* Offset from start of disk */
202
wdenk71f95112003-06-15 22:40:42 +0000203 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200204 debug("Error reading FAT blocks\n");
wdenk71f95112003-06-15 22:40:42 +0000205 return ret;
206 }
207 mydata->fatbufnum = bufnum;
208 }
209
210 /* Get the actual entry from the table */
211 switch (mydata->fatsize) {
212 case 32:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200213 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000214 break;
215 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200216 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000217 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200218 case 12:
219 off16 = (offset * 3) / 4;
wdenk71f95112003-06-15 22:40:42 +0000220
221 switch (offset & 0x3) {
222 case 0:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200223 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000224 ret &= 0xfff;
225 break;
226 case 1:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200227 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000228 val1 &= 0xf000;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200229 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk71f95112003-06-15 22:40:42 +0000230 val2 &= 0x00ff;
231 ret = (val2 << 4) | (val1 >> 12);
232 break;
233 case 2:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200234 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000235 val1 &= 0xff00;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200236 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk71f95112003-06-15 22:40:42 +0000237 val2 &= 0x000f;
238 ret = (val2 << 8) | (val1 >> 8);
239 break;
240 case 3:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200241 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000242 ret = (ret & 0xfff0) >> 4;
243 break;
244 default:
245 break;
246 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200247 break;
wdenk71f95112003-06-15 22:40:42 +0000248 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200249 debug("FAT%d: ret: %08x, offset: %04x\n",
250 mydata->fatsize, ret, offset);
wdenk71f95112003-06-15 22:40:42 +0000251
252 return ret;
253}
254
wdenk71f95112003-06-15 22:40:42 +0000255/*
256 * Read at most 'size' bytes from the specified cluster into 'buffer'.
257 * Return 0 on success, -1 otherwise.
258 */
259static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200260get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000261{
Erik Hansen3f270f42011-03-24 10:15:37 +0100262 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000263 __u32 startsect;
Kyle Moffett46236b12011-12-20 07:41:13 +0000264 int ret;
wdenk71f95112003-06-15 22:40:42 +0000265
266 if (clustnum > 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200267 startsect = mydata->data_begin +
268 clustnum * mydata->clust_size;
wdenk71f95112003-06-15 22:40:42 +0000269 } else {
270 startsect = mydata->rootdir_sect;
271 }
272
Wolfgang Denk7385c282010-07-19 11:37:00 +0200273 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
274
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200275 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson9a800ac2012-04-11 04:08:53 +0000276 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200277
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200278 printf("FAT: Misaligned buffer address (%p)\n", buffer);
279
280 while (size >= mydata->sect_size) {
281 ret = disk_read(startsect++, 1, tmpbuf);
282 if (ret != 1) {
283 debug("Error reading data (got %d)\n", ret);
284 return -1;
285 }
286
287 memcpy(buffer, tmpbuf, mydata->sect_size);
288 buffer += mydata->sect_size;
289 size -= mydata->sect_size;
290 }
291 } else {
Sergei Shtylyovac497772011-08-08 09:38:33 +0000292 idx = size / mydata->sect_size;
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200293 ret = disk_read(startsect, idx, buffer);
294 if (ret != idx) {
295 debug("Error reading data (got %d)\n", ret);
296 return -1;
297 }
298 startsect += idx;
299 idx *= mydata->sect_size;
300 buffer += idx;
301 size -= idx;
302 }
303 if (size) {
304 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
305
306 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett46236b12011-12-20 07:41:13 +0000307 if (ret != 1) {
308 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000309 return -1;
wdenk71f95112003-06-15 22:40:42 +0000310 }
wdenk7205e402003-09-10 22:30:53 +0000311
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200312 memcpy(buffer, tmpbuf, size);
wdenk71f95112003-06-15 22:40:42 +0000313 }
314
315 return 0;
316}
317
wdenk71f95112003-06-15 22:40:42 +0000318/*
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000319 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk71f95112003-06-15 22:40:42 +0000320 * into 'buffer'.
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800321 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
wdenk71f95112003-06-15 22:40:42 +0000322 */
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000323__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
324 __aligned(ARCH_DMA_MINALIGN);
325
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800326static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
327 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk71f95112003-06-15 22:40:42 +0000328{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800329 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000330 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000331 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000332 __u32 endclust, newclust;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800333 loff_t actsize;
wdenk71f95112003-06-15 22:40:42 +0000334
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800335 *gotsize = 0;
336 debug("Filesize: %llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000337
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000338 if (pos >= filesize) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800339 debug("Read position past EOF: %llu\n", pos);
340 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000341 }
342
343 if (maxsize > 0 && filesize > pos + maxsize)
344 filesize = pos + maxsize;
wdenk71f95112003-06-15 22:40:42 +0000345
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800346 debug("%llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000347
Wolfgang Denk7385c282010-07-19 11:37:00 +0200348 actsize = bytesperclust;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000349
350 /* go to cluster at pos */
351 while (actsize <= pos) {
352 curclust = get_fatent(mydata, curclust);
353 if (CHECK_CLUST(curclust, mydata->fatsize)) {
354 debug("curclust: 0x%x\n", curclust);
355 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800356 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000357 }
358 actsize += bytesperclust;
359 }
360
361 /* actsize > pos */
362 actsize -= bytesperclust;
363 filesize -= actsize;
364 pos -= actsize;
365
366 /* align to beginning of next cluster if any */
367 if (pos) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800368 actsize = min(filesize, (loff_t)bytesperclust);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000369 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
370 (int)actsize) != 0) {
371 printf("Error reading cluster\n");
372 return -1;
373 }
374 filesize -= actsize;
375 actsize -= pos;
376 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800377 *gotsize += actsize;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000378 if (!filesize)
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800379 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000380 buffer += actsize;
381
382 curclust = get_fatent(mydata, curclust);
383 if (CHECK_CLUST(curclust, mydata->fatsize)) {
384 debug("curclust: 0x%x\n", curclust);
385 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800386 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000387 }
388 }
389
390 actsize = bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200391 endclust = curclust;
392
wdenk71f95112003-06-15 22:40:42 +0000393 do {
wdenk7205e402003-09-10 22:30:53 +0000394 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200395 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000396 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200397 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000398 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100399 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200400 debug("curclust: 0x%x\n", newclust);
401 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800402 return 0;
wdenk7205e402003-09-10 22:30:53 +0000403 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200404 endclust = newclust;
405 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000406 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200407
wdenk7205e402003-09-10 22:30:53 +0000408 /* get remaining bytes */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200409 actsize = filesize;
Benoît Thébaudeau0880e5b2012-07-20 15:21:37 +0200410 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200411 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000412 return -1;
413 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800414 *gotsize += actsize;
415 return 0;
wdenk7205e402003-09-10 22:30:53 +0000416getit:
417 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200418 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000419 return -1;
420 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800421 *gotsize += (int)actsize;
wdenk7205e402003-09-10 22:30:53 +0000422 filesize -= actsize;
423 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200424
wdenk7205e402003-09-10 22:30:53 +0000425 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100426 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200427 debug("curclust: 0x%x\n", curclust);
428 printf("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800429 return 0;
wdenk71f95112003-06-15 22:40:42 +0000430 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200431 actsize = bytesperclust;
432 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000433 } while (1);
434}
435
wdenk71f95112003-06-15 22:40:42 +0000436/*
437 * Extract the file name information from 'slotptr' into 'l_name',
438 * starting at l_name[*idx].
439 * Return 1 if terminator (zero byte) is found, 0 otherwise.
440 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200441static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000442{
443 int j;
444
445 for (j = 0; j <= 8; j += 2) {
446 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200447 if (l_name[*idx] == 0x00)
448 return 1;
wdenk71f95112003-06-15 22:40:42 +0000449 (*idx)++;
450 }
451 for (j = 0; j <= 10; j += 2) {
452 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200453 if (l_name[*idx] == 0x00)
454 return 1;
wdenk71f95112003-06-15 22:40:42 +0000455 (*idx)++;
456 }
457 for (j = 0; j <= 2; j += 2) {
458 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200459 if (l_name[*idx] == 0x00)
460 return 1;
wdenk71f95112003-06-15 22:40:42 +0000461 (*idx)++;
462 }
463
464 return 0;
465}
466
wdenk71f95112003-06-15 22:40:42 +0000467/*
468 * Extract the full long filename starting at 'retdent' (which is really
469 * a slot) into 'l_name'. If successful also copy the real directory entry
470 * into 'retdent'
471 * Return 0 on success, -1 otherwise.
472 */
473static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200474get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
475 dir_entry *retdent, char *l_name)
wdenk71f95112003-06-15 22:40:42 +0000476{
477 dir_entry *realdent;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200478 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000479 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
480 PREFETCH_BLOCKS :
481 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200482 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000483 int idx = 0;
484
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300485 if (counter > VFAT_MAXSEQ) {
486 debug("Error: VFAT name is too long\n");
487 return -1;
488 }
489
490 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200491 if (counter == 0)
492 break;
wdenk2d1a5372004-02-23 19:30:57 +0000493 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
494 return -1;
wdenk71f95112003-06-15 22:40:42 +0000495 slotptr++;
496 counter--;
497 }
498
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300499 if ((__u8 *)slotptr >= buflimit) {
wdenk71f95112003-06-15 22:40:42 +0000500 dir_slot *slotptr2;
501
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300502 if (curclust == 0)
503 return -1;
wdenk71f95112003-06-15 22:40:42 +0000504 curclust = get_fatent(mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100505 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200506 debug("curclust: 0x%x\n", curclust);
507 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000508 return -1;
509 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200510
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000511 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000512 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200513 debug("Error: reading directory block\n");
wdenk71f95112003-06-15 22:40:42 +0000514 return -1;
515 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200516
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000517 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300518 while (counter > 0) {
519 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
520 & 0xff) != counter)
521 return -1;
wdenk71f95112003-06-15 22:40:42 +0000522 slotptr2++;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300523 counter--;
524 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200525
wdenk71f95112003-06-15 22:40:42 +0000526 /* Save the real directory entry */
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300527 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000528 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000529 slotptr2--;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300530 slot2str(slotptr2, l_name, &idx);
wdenk71f95112003-06-15 22:40:42 +0000531 }
532 } else {
533 /* Save the real directory entry */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200534 realdent = (dir_entry *)slotptr;
wdenk71f95112003-06-15 22:40:42 +0000535 }
536
537 do {
538 slotptr--;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200539 if (slot2str(slotptr, l_name, &idx))
540 break;
wdenk2d1a5372004-02-23 19:30:57 +0000541 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000542
543 l_name[idx] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +0200544 if (*l_name == DELETED_FLAG)
545 *l_name = '\0';
546 else if (*l_name == aRING)
547 *l_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000548 downcase(l_name);
549
550 /* Return the real directory entry */
551 memcpy(retdent, realdent, sizeof(dir_entry));
552
553 return 0;
554}
555
wdenk71f95112003-06-15 22:40:42 +0000556/* Calculate short name checksum */
Marek Vasutff04f6d2012-10-09 07:20:22 +0000557static __u8 mkcksum(const char name[8], const char ext[3])
wdenk71f95112003-06-15 22:40:42 +0000558{
559 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200560
wdenk71f95112003-06-15 22:40:42 +0000561 __u8 ret = 0;
562
Marek Vasut6ad77d82013-01-11 03:35:48 +0000563 for (i = 0; i < 8; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000564 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut6ad77d82013-01-11 03:35:48 +0000565 for (i = 0; i < 3; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000566 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk71f95112003-06-15 22:40:42 +0000567
568 return ret;
569}
wdenk71f95112003-06-15 22:40:42 +0000570
571/*
572 * Get the directory entry associated with 'filename' from the directory
573 * starting at 'startsect'
574 */
Eric Nelson9a800ac2012-04-11 04:08:53 +0000575__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
576 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200577
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200578static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
579 char *filename, dir_entry *retdent,
580 int dols)
wdenk71f95112003-06-15 22:40:42 +0000581{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200582 __u16 prevcksum = 0xffff;
583 __u32 curclust = START(retdent);
584 int files = 0, dirs = 0;
wdenk71f95112003-06-15 22:40:42 +0000585
Wolfgang Denk7385c282010-07-19 11:37:00 +0200586 debug("get_dentfromdir: %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +0000587
Wolfgang Denk7385c282010-07-19 11:37:00 +0200588 while (1) {
589 dir_entry *dentptr;
wdenk71f95112003-06-15 22:40:42 +0000590
Wolfgang Denk7385c282010-07-19 11:37:00 +0200591 int i;
wdenk71f95112003-06-15 22:40:42 +0000592
Wolfgang Denk7385c282010-07-19 11:37:00 +0200593 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000594 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200595 debug("Error: reading directory block\n");
596 return NULL;
597 }
598
599 dentptr = (dir_entry *)get_dentfromdir_block;
600
601 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300602 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200603
604 l_name[0] = '\0';
605 if (dentptr->name[0] == DELETED_FLAG) {
606 dentptr++;
607 continue;
wdenk71f95112003-06-15 22:40:42 +0000608 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200609 if ((dentptr->attr & ATTR_VOLUME)) {
Richard Genoudcb940c72012-12-13 03:30:10 +0000610 if (vfat_enabled &&
611 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
J. Vijayanand206d68f2011-10-19 07:43:08 +0000612 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200613 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
614 get_vfatname(mydata, curclust,
615 get_dentfromdir_block,
616 dentptr, l_name);
617 if (dols) {
618 int isdir;
619 char dirc;
620 int doit = 0;
621
622 isdir = (dentptr->attr & ATTR_DIR);
623
624 if (isdir) {
625 dirs++;
626 dirc = '/';
627 doit = 1;
628 } else {
629 dirc = ' ';
630 if (l_name[0] != 0) {
631 files++;
632 doit = 1;
633 }
634 }
635 if (doit) {
636 if (dirc == ' ') {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800637 printf(" %8u %s%c\n",
638 FAT2CPU32(dentptr->size),
Wolfgang Denk7385c282010-07-19 11:37:00 +0200639 l_name,
640 dirc);
641 } else {
642 printf(" %s%c\n",
643 l_name,
644 dirc);
645 }
646 }
647 dentptr++;
648 continue;
649 }
650 debug("vfatname: |%s|\n", l_name);
Richard Genoudcb940c72012-12-13 03:30:10 +0000651 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200652 /* Volume label or VFAT entry */
653 dentptr++;
654 continue;
655 }
656 }
657 if (dentptr->name[0] == 0) {
658 if (dols) {
659 printf("\n%d file(s), %d dir(s)\n\n",
660 files, dirs);
661 }
662 debug("Dentname == NULL - %d\n", i);
663 return NULL;
664 }
Richard Genoudcb940c72012-12-13 03:30:10 +0000665 if (vfat_enabled) {
666 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
667 if (dols && csum == prevcksum) {
668 prevcksum = 0xffff;
669 dentptr++;
670 continue;
671 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200672 }
Richard Genoudcb940c72012-12-13 03:30:10 +0000673
Wolfgang Denk7385c282010-07-19 11:37:00 +0200674 get_name(dentptr, s_name);
675 if (dols) {
676 int isdir = (dentptr->attr & ATTR_DIR);
677 char dirc;
678 int doit = 0;
wdenk71f95112003-06-15 22:40:42 +0000679
Wolfgang Denk7385c282010-07-19 11:37:00 +0200680 if (isdir) {
681 dirs++;
682 dirc = '/';
683 doit = 1;
684 } else {
685 dirc = ' ';
686 if (s_name[0] != 0) {
687 files++;
688 doit = 1;
689 }
690 }
691
692 if (doit) {
693 if (dirc == ' ') {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800694 printf(" %8u %s%c\n",
695 FAT2CPU32(dentptr->size),
Wolfgang Denk7385c282010-07-19 11:37:00 +0200696 s_name, dirc);
697 } else {
698 printf(" %s%c\n",
699 s_name, dirc);
700 }
701 }
702
703 dentptr++;
704 continue;
705 }
706
707 if (strcmp(filename, s_name)
708 && strcmp(filename, l_name)) {
709 debug("Mismatch: |%s|%s|\n", s_name, l_name);
710 dentptr++;
711 continue;
712 }
713
714 memcpy(retdent, dentptr, sizeof(dir_entry));
715
716 debug("DentName: %s", s_name);
717 debug(", start: 0x%x", START(dentptr));
718 debug(", size: 0x%x %s\n",
719 FAT2CPU32(dentptr->size),
720 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
721
722 return retdent;
wdenk71f95112003-06-15 22:40:42 +0000723 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200724
725 curclust = get_fatent(mydata, curclust);
726 if (CHECK_CLUST(curclust, mydata->fatsize)) {
727 debug("curclust: 0x%x\n", curclust);
728 printf("Invalid FAT entry\n");
729 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000730 }
wdenk71f95112003-06-15 22:40:42 +0000731 }
wdenk71f95112003-06-15 22:40:42 +0000732
Wolfgang Denk7385c282010-07-19 11:37:00 +0200733 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000734}
735
wdenk71f95112003-06-15 22:40:42 +0000736/*
737 * Read boot sector and volume info from a FAT filesystem
738 */
739static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200740read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000741{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000742 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000743 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000744 int ret = 0;
745
746 if (cur_dev == NULL) {
747 debug("Error: no device selected\n");
748 return -1;
749 }
750
Eric Nelson9a800ac2012-04-11 04:08:53 +0000751 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000752 if (block == NULL) {
753 debug("Error: allocating block\n");
754 return -1;
755 }
wdenk71f95112003-06-15 22:40:42 +0000756
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200757 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200758 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000759 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000760 }
761
762 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200763 bs->reserved = FAT2CPU16(bs->reserved);
764 bs->fat_length = FAT2CPU16(bs->fat_length);
765 bs->secs_track = FAT2CPU16(bs->secs_track);
766 bs->heads = FAT2CPU16(bs->heads);
767 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000768
769 /* FAT32 entries */
770 if (bs->fat_length == 0) {
771 /* Assume FAT32 */
772 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200773 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000774 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200775 bs->info_sector = FAT2CPU16(bs->info_sector);
776 bs->backup_boot = FAT2CPU16(bs->backup_boot);
777 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000778 *fatsize = 32;
779 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200780 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000781 *fatsize = 0;
782 }
783 memcpy(volinfo, vistart, sizeof(volume_info));
784
wdenk71f95112003-06-15 22:40:42 +0000785 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200786 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000787 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000788 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500789 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000790 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000791 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000792 }
Tom Rix651351f2009-05-20 07:55:41 -0500793 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000794 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000795 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000796 }
797 }
798
Wolfgang Denk7385c282010-07-19 11:37:00 +0200799 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000800fail:
801 ret = -1;
802exit:
803 free(block);
804 return ret;
wdenk71f95112003-06-15 22:40:42 +0000805}
806
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000807__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
Eric Nelson9a800ac2012-04-11 04:08:53 +0000808 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200809
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800810int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
811 loff_t maxsize, int dols, int dogetsize, loff_t *size)
wdenk71f95112003-06-15 22:40:42 +0000812{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200813 char fnamecopy[2048];
814 boot_sector bs;
815 volume_info volinfo;
816 fsdata datablock;
817 fsdata *mydata = &datablock;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200818 dir_entry *dentptr = NULL;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200819 __u16 prevcksum = 0xffff;
820 char *subname = "";
Erik Hansen3f270f42011-03-24 10:15:37 +0100821 __u32 cursect;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200822 int idx, isdir = 0;
823 int files = 0, dirs = 0;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800824 int ret = -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200825 int firsttime;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000826 __u32 root_cluster = 0;
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100827 __u32 read_blk;
Erik Hansen3f270f42011-03-24 10:15:37 +0100828 int rootdir_size = 0;
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100829 int buffer_blk_cnt;
830 int do_read;
831 __u8 *dir_ptr;
wdenk71f95112003-06-15 22:40:42 +0000832
Wolfgang Denk7385c282010-07-19 11:37:00 +0200833 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
834 debug("Error: reading boot sector\n");
835 return -1;
836 }
837
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000838 if (mydata->fatsize == 32) {
839 root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200840 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000841 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200842 mydata->fatlength = bs.fat_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000843 }
wdenk71f95112003-06-15 22:40:42 +0000844
Wolfgang Denk7385c282010-07-19 11:37:00 +0200845 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000846
Wolfgang Denk7385c282010-07-19 11:37:00 +0200847 cursect = mydata->rootdir_sect
848 = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000849
Sergei Shtylyovac497772011-08-08 09:38:33 +0000850 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200851 mydata->clust_size = bs.cluster_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000852 if (mydata->sect_size != cur_part_info.blksz) {
853 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
854 mydata->sect_size, cur_part_info.blksz);
855 return -1;
856 }
wdenk71f95112003-06-15 22:40:42 +0000857
Wolfgang Denk7385c282010-07-19 11:37:00 +0200858 if (mydata->fatsize == 32) {
859 mydata->data_begin = mydata->rootdir_sect -
860 (mydata->clust_size * 2);
861 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200862 rootdir_size = ((bs.dir_entries[1] * (int)256 +
863 bs.dir_entries[0]) *
864 sizeof(dir_entry)) /
Sergei Shtylyovac497772011-08-08 09:38:33 +0000865 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200866 mydata->data_begin = mydata->rootdir_sect +
867 rootdir_size -
868 (mydata->clust_size * 2);
wdenk71f95112003-06-15 22:40:42 +0000869 }
wdenk71f95112003-06-15 22:40:42 +0000870
Wolfgang Denk7385c282010-07-19 11:37:00 +0200871 mydata->fatbufnum = -1;
Eric Nelson9a800ac2012-04-11 04:08:53 +0000872 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000873 if (mydata->fatbuf == NULL) {
874 debug("Error: allocating memory\n");
875 return -1;
876 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200877
Richard Genoudcb940c72012-12-13 03:30:10 +0000878 if (vfat_enabled)
879 debug("VFAT Support enabled\n");
880
Wolfgang Denk7385c282010-07-19 11:37:00 +0200881 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
882 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
883 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
884 "Data begins at: %d\n",
885 root_cluster,
886 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000887 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
888 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
889 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200890
891 /* "cwd" is always the root... */
892 while (ISDIRDELIM(*filename))
893 filename++;
894
895 /* Make a copy of the filename and convert it to lowercase */
896 strcpy(fnamecopy, filename);
897 downcase(fnamecopy);
898
Stephen Warren18a10d42015-07-28 21:55:03 -0600899root_reparse:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200900 if (*fnamecopy == '\0') {
901 if (!dols)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000902 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200903
904 dols = LS_ROOT;
905 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
906 isdir = 1;
907 fnamecopy[idx] = '\0';
908 subname = fnamecopy + idx + 1;
909
910 /* Handle multiple delimiters */
911 while (ISDIRDELIM(*subname))
912 subname++;
913 } else if (dols) {
914 isdir = 1;
915 }
916
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100917 buffer_blk_cnt = 0;
918 firsttime = 1;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200919 while (1) {
920 int i;
921
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100922 if (mydata->fatsize == 32 || firsttime) {
923 dir_ptr = do_fat_read_at_block;
924 firsttime = 0;
925 } else {
926 /**
927 * FAT16 sector buffer modification:
928 * Each loop, the second buffered block is moved to
929 * the buffer begin, and two next sectors are read
930 * next to the previously moved one. So the sector
931 * buffer keeps always 3 sectors for fat16.
932 * And the current sector is the buffer second sector
933 * beside the "firsttime" read, when it is the first one.
934 *
935 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
936 * n = computed root dir sector
937 * loop | cursect-1 | cursect | cursect+1 |
938 * 0 | sector n+0 | sector n+1 | none |
939 * 1 | none | sector n+0 | sector n+1 |
940 * 0 | sector n+1 | sector n+2 | sector n+3 |
941 * 1 | sector n+3 | ...
942 */
943 dir_ptr = (do_fat_read_at_block + mydata->sect_size);
944 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size);
945 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200946
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100947 do_read = 1;
948
949 if (mydata->fatsize == 32 && buffer_blk_cnt)
950 do_read = 0;
951
952 if (do_read) {
953 read_blk = (mydata->fatsize == 32) ?
954 mydata->clust_size : PREFETCH_BLOCKS;
955
956 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
957 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK);
958
959 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200960 debug("Error: reading rootdir block\n");
961 goto exit;
962 }
963
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100964 dentptr = (dir_entry *)dir_ptr;
wdenk71f95112003-06-15 22:40:42 +0000965 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200966
Wolfgang Denk7385c282010-07-19 11:37:00 +0200967 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300968 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Marek Vasutff04f6d2012-10-09 07:20:22 +0000969 __u8 csum;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200970
971 l_name[0] = '\0';
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300972 if (dentptr->name[0] == DELETED_FLAG) {
973 dentptr++;
974 continue;
975 }
Marek Vasutff04f6d2012-10-09 07:20:22 +0000976
Richard Genoudcb940c72012-12-13 03:30:10 +0000977 if (vfat_enabled)
978 csum = mkcksum(dentptr->name, dentptr->ext);
979
Marek Vasutff04f6d2012-10-09 07:20:22 +0000980 if (dentptr->attr & ATTR_VOLUME) {
Richard Genoudcb940c72012-12-13 03:30:10 +0000981 if (vfat_enabled &&
982 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk7385c282010-07-19 11:37:00 +0200983 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
984 prevcksum =
985 ((dir_slot *)dentptr)->alias_checksum;
wdenk71f95112003-06-15 22:40:42 +0000986
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300987 get_vfatname(mydata,
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000988 root_cluster,
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100989 dir_ptr,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200990 dentptr, l_name);
991
992 if (dols == LS_ROOT) {
993 char dirc;
994 int doit = 0;
995 int isdir =
996 (dentptr->attr & ATTR_DIR);
997
998 if (isdir) {
999 dirs++;
1000 dirc = '/';
1001 doit = 1;
1002 } else {
1003 dirc = ' ';
1004 if (l_name[0] != 0) {
1005 files++;
1006 doit = 1;
1007 }
1008 }
1009 if (doit) {
1010 if (dirc == ' ') {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001011 printf(" %8u %s%c\n",
1012 FAT2CPU32(dentptr->size),
Wolfgang Denk7385c282010-07-19 11:37:00 +02001013 l_name,
1014 dirc);
1015 } else {
1016 printf(" %s%c\n",
1017 l_name,
1018 dirc);
1019 }
1020 }
1021 dentptr++;
1022 continue;
1023 }
1024 debug("Rootvfatname: |%s|\n",
1025 l_name);
Richard Genoudcb940c72012-12-13 03:30:10 +00001026 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +02001027 /* Volume label or VFAT entry */
1028 dentptr++;
1029 continue;
1030 }
1031 } else if (dentptr->name[0] == 0) {
1032 debug("RootDentname == NULL - %d\n", i);
1033 if (dols == LS_ROOT) {
1034 printf("\n%d file(s), %d dir(s)\n\n",
1035 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +00001036 ret = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001037 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001038 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001039 }
Richard Genoudcb940c72012-12-13 03:30:10 +00001040 else if (vfat_enabled &&
1041 dols == LS_ROOT && csum == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +00001042 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001043 dentptr++;
1044 continue;
1045 }
Richard Genoudcb940c72012-12-13 03:30:10 +00001046
Wolfgang Denk7385c282010-07-19 11:37:00 +02001047 get_name(dentptr, s_name);
1048
1049 if (dols == LS_ROOT) {
1050 int isdir = (dentptr->attr & ATTR_DIR);
1051 char dirc;
1052 int doit = 0;
1053
1054 if (isdir) {
1055 dirc = '/';
1056 if (s_name[0] != 0) {
1057 dirs++;
1058 doit = 1;
1059 }
1060 } else {
1061 dirc = ' ';
1062 if (s_name[0] != 0) {
1063 files++;
1064 doit = 1;
1065 }
1066 }
1067 if (doit) {
1068 if (dirc == ' ') {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001069 printf(" %8u %s%c\n",
1070 FAT2CPU32(dentptr->size),
Wolfgang Denk7385c282010-07-19 11:37:00 +02001071 s_name, dirc);
1072 } else {
1073 printf(" %s%c\n",
1074 s_name, dirc);
1075 }
1076 }
1077 dentptr++;
1078 continue;
1079 }
1080
1081 if (strcmp(fnamecopy, s_name)
1082 && strcmp(fnamecopy, l_name)) {
1083 debug("RootMismatch: |%s|%s|\n", s_name,
1084 l_name);
1085 dentptr++;
1086 continue;
1087 }
1088
1089 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001090 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001091
1092 debug("RootName: %s", s_name);
1093 debug(", start: 0x%x", START(dentptr));
1094 debug(", size: 0x%x %s\n",
1095 FAT2CPU32(dentptr->size),
1096 isdir ? "(DIR)" : "");
1097
1098 goto rootdir_done; /* We got a match */
1099 }
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001100 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
Wolfgang Denk7385c282010-07-19 11:37:00 +02001101 mydata->clust_size);
1102
1103 /*
1104 * On FAT32 we must fetch the FAT entries for the next
1105 * root directory clusters when a cluster has been
1106 * completely processed.
1107 */
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001108 ++buffer_blk_cnt;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001109 int rootdir_end = 0;
1110 if (mydata->fatsize == 32) {
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001111 if (buffer_blk_cnt == mydata->clust_size) {
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001112 int nxtsect = 0;
1113 int nxt_clust = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001114
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001115 nxt_clust = get_fatent(mydata, root_cluster);
1116 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001117
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001118 nxtsect = mydata->data_begin +
1119 (nxt_clust * mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001120
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001121 root_cluster = nxt_clust;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001122
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001123 cursect = nxtsect;
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001124 buffer_blk_cnt = 0;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001125 }
wdenk71f95112003-06-15 22:40:42 +00001126 } else {
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001127 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1128 buffer_blk_cnt = 0;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001129
1130 rootdir_end = (++cursect - mydata->rootdir_sect >=
1131 rootdir_size);
wdenk71f95112003-06-15 22:40:42 +00001132 }
Erik Hansen3f270f42011-03-24 10:15:37 +01001133
1134 /* If end of rootdir reached */
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001135 if (rootdir_end) {
Erik Hansen3f270f42011-03-24 10:15:37 +01001136 if (dols == LS_ROOT) {
1137 printf("\n%d file(s), %d dir(s)\n\n",
1138 files, dirs);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001139 *size = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +01001140 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001141 goto exit;
Erik Hansen3f270f42011-03-24 10:15:37 +01001142 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001143 }
1144rootdir_done:
1145
1146 firsttime = 1;
1147
1148 while (isdir) {
1149 int startsect = mydata->data_begin
1150 + START(dentptr) * mydata->clust_size;
1151 dir_entry dent;
1152 char *nextname = NULL;
1153
1154 dent = *dentptr;
1155 dentptr = &dent;
1156
1157 idx = dirdelim(subname);
1158
1159 if (idx >= 0) {
1160 subname[idx] = '\0';
1161 nextname = subname + idx + 1;
1162 /* Handle multiple delimiters */
1163 while (ISDIRDELIM(*nextname))
1164 nextname++;
1165 if (dols && *nextname == '\0')
1166 firsttime = 0;
1167 } else {
1168 if (dols && firsttime) {
1169 firsttime = 0;
1170 } else {
1171 isdir = 0;
1172 }
wdenk71f95112003-06-15 22:40:42 +00001173 }
wdenk71f95112003-06-15 22:40:42 +00001174
Wolfgang Denk7385c282010-07-19 11:37:00 +02001175 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1176 isdir ? 0 : dols) == NULL) {
1177 if (dols && !isdir)
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001178 *size = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +00001179 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001180 }
wdenk71f95112003-06-15 22:40:42 +00001181
Benoît Thébaudeau7ee46ce2012-07-20 03:20:29 +00001182 if (isdir && !(dentptr->attr & ATTR_DIR))
1183 goto exit;
1184
Stephen Warren18a10d42015-07-28 21:55:03 -06001185 /*
1186 * If we are looking for a directory, and found a directory
1187 * type entry, and the entry is for the root directory (as
1188 * denoted by a cluster number of 0), jump back to the start
1189 * of the function, since at least on FAT12/16, the root dir
1190 * lives in a hard-coded location and needs special handling
1191 * to parse, rather than simply following the cluster linked
1192 * list in the FAT, like other directories.
1193 */
1194 if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) {
1195 /*
1196 * Modify the filename to remove the prefix that gets
1197 * back to the root directory, so the initial root dir
1198 * parsing code can continue from where we are without
1199 * confusion.
1200 */
1201 strcpy(fnamecopy, nextname ?: "");
1202 /*
1203 * Set up state the same way as the function does when
1204 * first started. This is required for the root dir
1205 * parsing code operates in its expected environment.
1206 */
1207 subname = "";
1208 cursect = mydata->rootdir_sect;
1209 isdir = 0;
1210 goto root_reparse;
1211 }
1212
Benoît Thébaudeau7ee46ce2012-07-20 03:20:29 +00001213 if (idx >= 0)
Wolfgang Denk7385c282010-07-19 11:37:00 +02001214 subname = nextname;
wdenk71f95112003-06-15 22:40:42 +00001215 }
1216
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001217 if (dogetsize) {
1218 *size = FAT2CPU32(dentptr->size);
1219 ret = 0;
1220 } else {
1221 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
1222 }
1223 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
wdenk71f95112003-06-15 22:40:42 +00001224
Sergei Shtylyovac497772011-08-08 09:38:33 +00001225exit:
1226 free(mydata->fatbuf);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001227 return ret;
wdenk71f95112003-06-15 22:40:42 +00001228}
1229
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001230int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
1231 loff_t *actread)
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001232{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001233 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001234}
1235
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001236int file_fat_detectfs(void)
wdenk71f95112003-06-15 22:40:42 +00001237{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001238 boot_sector bs;
1239 volume_info volinfo;
1240 int fatsize;
1241 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001242
Wolfgang Denk7385c282010-07-19 11:37:00 +02001243 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001244 printf("No current device\n");
1245 return 1;
1246 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001247
Jon Loeligerdd60d122007-07-09 17:56:50 -05001248#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -05001249 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001250 defined(CONFIG_CMD_SCSI) || \
1251 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001252 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001253 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001254 switch (cur_dev->if_type) {
1255 case IF_TYPE_IDE:
1256 printf("IDE");
1257 break;
1258 case IF_TYPE_SATA:
1259 printf("SATA");
1260 break;
1261 case IF_TYPE_SCSI:
1262 printf("SCSI");
1263 break;
1264 case IF_TYPE_ATAPI:
1265 printf("ATAPI");
1266 break;
1267 case IF_TYPE_USB:
1268 printf("USB");
1269 break;
1270 case IF_TYPE_DOC:
1271 printf("DOC");
1272 break;
1273 case IF_TYPE_MMC:
1274 printf("MMC");
1275 break;
1276 default:
1277 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001278 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001279
1280 printf("\n Device %d: ", cur_dev->dev);
wdenk7205e402003-09-10 22:30:53 +00001281 dev_print(cur_dev);
1282#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001283
1284 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001285 printf("\nNo valid FAT fs found\n");
1286 return 1;
1287 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001288
1289 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001290 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001291 volinfo.fs_type[5] = '\0';
1292
Stephen Warren461f86e2012-10-17 06:44:57 +00001293 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001294
wdenk7205e402003-09-10 22:30:53 +00001295 return 0;
wdenk71f95112003-06-15 22:40:42 +00001296}
1297
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001298int file_fat_ls(const char *dir)
wdenk71f95112003-06-15 22:40:42 +00001299{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001300 loff_t size;
1301
1302 return do_fat_read(dir, NULL, 0, LS_YES, &size);
wdenk71f95112003-06-15 22:40:42 +00001303}
1304
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001305int fat_exists(const char *filename)
1306{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001307 int ret;
1308 loff_t size;
1309
1310 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1311 return ret == 0;
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001312}
1313
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001314int fat_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -06001315{
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001316 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
Stephen Warrencf659812014-06-11 12:47:26 -06001317}
1318
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001319int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1320 loff_t maxsize, loff_t *actread)
wdenk71f95112003-06-15 22:40:42 +00001321{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001322 printf("reading %s\n", filename);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001323 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1324 actread);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001325}
1326
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001327int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001328{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001329 loff_t actread;
1330 int ret;
1331
1332 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1333 if (ret)
1334 return ret;
1335 else
1336 return actread;
wdenk71f95112003-06-15 22:40:42 +00001337}
Simon Glasse6d52412012-12-26 09:53:33 +00001338
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001339int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1340 loff_t *actread)
Simon Glasse6d52412012-12-26 09:53:33 +00001341{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001342 int ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001343
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001344 ret = file_fat_read_at(filename, offset, buf, len, actread);
1345 if (ret)
Simon Glasse6d52412012-12-26 09:53:33 +00001346 printf("** Unable to read file %s **\n", filename);
Simon Glasse6d52412012-12-26 09:53:33 +00001347
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001348 return ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001349}
1350
1351void fat_close(void)
1352{
1353}