blob: fe899d0442d614fa93dd49a0a7cefc4f7ecdbb41 [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>
Simon Glass2a981dc2016-02-29 15:25:52 -070013#include <blk.h>
wdenk71f95112003-06-15 22:40:42 +000014#include <config.h>
Sergei Shtylyovac497772011-08-08 09:38:33 +000015#include <exports.h>
wdenk71f95112003-06-15 22:40:42 +000016#include <fat.h>
17#include <asm/byteorder.h>
wdenk7205e402003-09-10 22:30:53 +000018#include <part.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000019#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060020#include <memalign.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000021#include <linux/compiler.h>
Richard Genoudfb7e16c2012-12-13 00:47:36 +000022#include <linux/ctype.h>
wdenk71f95112003-06-15 22:40:42 +000023
Richard Genoudcb940c72012-12-13 03:30:10 +000024#ifdef CONFIG_SUPPORT_VFAT
25static const int vfat_enabled = 1;
26#else
27static const int vfat_enabled = 0;
28#endif
29
wdenk71f95112003-06-15 22:40:42 +000030/*
31 * Convert a string to lowercase.
32 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +020033static void downcase(char *str)
wdenk71f95112003-06-15 22:40:42 +000034{
35 while (*str != '\0') {
Richard Genoudfb7e16c2012-12-13 00:47:36 +000036 *str = tolower(*str);
wdenk71f95112003-06-15 22:40:42 +000037 str++;
38 }
39}
40
Simon Glass4101f682016-02-29 15:25:34 -070041static struct blk_desc *cur_dev;
Kyle Moffett9813b752011-12-21 07:08:10 +000042static disk_partition_t cur_part_info;
Wolfgang Denk7385c282010-07-19 11:37:00 +020043
Kyle Moffett9813b752011-12-21 07:08:10 +000044#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk7205e402003-09-10 22:30:53 +000045#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk66c2d732010-07-19 11:36:57 +020046#define DOS_FS32_TYPE_OFFSET 0x52
wdenk71f95112003-06-15 22:40:42 +000047
Kyle Moffett9813b752011-12-21 07:08:10 +000048static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk71f95112003-06-15 22:40:42 +000049{
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020050 ulong ret;
51
Simon Glass2a981dc2016-02-29 15:25:52 -070052 if (!cur_dev)
wdenk7205e402003-09-10 22:30:53 +000053 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020054
Simon Glass2a981dc2016-02-29 15:25:52 -070055 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewski0a04ed82015-09-03 14:21:39 +020056
57 if (nr_blocks && ret == 0)
58 return -1;
59
60 return ret;
wdenk71f95112003-06-15 22:40:42 +000061}
62
Simon Glass4101f682016-02-29 15:25:34 -070063int fat_set_blk_dev(struct blk_desc *dev_desc, disk_partition_t *info)
wdenk71f95112003-06-15 22:40:42 +000064{
Eric Nelson9a800ac2012-04-11 04:08:53 +000065 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk7205e402003-09-10 22:30:53 +000066
Stephen Warren5e8f9832012-10-17 06:44:59 +000067 cur_dev = dev_desc;
68 cur_part_info = *info;
Kyle Moffett9813b752011-12-21 07:08:10 +000069
70 /* Make sure it has a valid FAT header */
71 if (disk_read(0, 1, buffer) != 1) {
72 cur_dev = NULL;
73 return -1;
74 }
75
76 /* Check if it's actually a DOS volume */
77 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
78 cur_dev = NULL;
79 return -1;
80 }
81
82 /* Check for FAT12/FAT16/FAT32 filesystem */
83 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
84 return 0;
85 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
86 return 0;
87
88 cur_dev = NULL;
89 return -1;
wdenk71f95112003-06-15 22:40:42 +000090}
91
Simon Glass4101f682016-02-29 15:25:34 -070092int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren5e8f9832012-10-17 06:44:59 +000093{
94 disk_partition_t info;
95
96 /* First close any currently found FAT filesystem */
97 cur_dev = NULL;
98
99 /* Read the partition table, if present */
Simon Glass3e8bd462016-02-29 15:25:48 -0700100 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren5e8f9832012-10-17 06:44:59 +0000101 if (part_no != 0) {
102 printf("** Partition %d not valid on device %d **\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700103 part_no, dev_desc->devnum);
Stephen Warren5e8f9832012-10-17 06:44:59 +0000104 return -1;
105 }
106
107 info.start = 0;
108 info.size = dev_desc->lba;
109 info.blksz = dev_desc->blksz;
110 info.name[0] = 0;
111 info.type[0] = 0;
112 info.bootable = 0;
113#ifdef CONFIG_PARTITION_UUIDS
114 info.uuid[0] = 0;
115#endif
116 }
117
118 return fat_set_blk_dev(dev_desc, &info);
119}
Kyle Moffett9813b752011-12-21 07:08:10 +0000120
wdenk71f95112003-06-15 22:40:42 +0000121/*
122 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
123 * Return index into string if found, -1 otherwise.
124 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200125static int dirdelim(char *str)
wdenk71f95112003-06-15 22:40:42 +0000126{
127 char *start = str;
128
129 while (*str != '\0') {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200130 if (ISDIRDELIM(*str))
131 return str - start;
wdenk71f95112003-06-15 22:40:42 +0000132 str++;
133 }
134 return -1;
135}
136
wdenk71f95112003-06-15 22:40:42 +0000137/*
138 * Extract zero terminated short name from a directory entry.
139 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200140static void get_name(dir_entry *dirent, char *s_name)
wdenk71f95112003-06-15 22:40:42 +0000141{
142 char *ptr;
143
Wolfgang Denk7385c282010-07-19 11:37:00 +0200144 memcpy(s_name, dirent->name, 8);
wdenk71f95112003-06-15 22:40:42 +0000145 s_name[8] = '\0';
146 ptr = s_name;
147 while (*ptr && *ptr != ' ')
148 ptr++;
149 if (dirent->ext[0] && dirent->ext[0] != ' ') {
150 *ptr = '.';
151 ptr++;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200152 memcpy(ptr, dirent->ext, 3);
wdenk71f95112003-06-15 22:40:42 +0000153 ptr[3] = '\0';
154 while (*ptr && *ptr != ' ')
155 ptr++;
156 }
157 *ptr = '\0';
158 if (*s_name == DELETED_FLAG)
159 *s_name = '\0';
160 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100161 *s_name = DELETED_FLAG;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200162 downcase(s_name);
wdenk71f95112003-06-15 22:40:42 +0000163}
164
Stefan Brünsb8948d22016-12-17 00:27:51 +0100165static int flush_dirty_fat_buffer(fsdata *mydata);
166#if !defined(CONFIG_FAT_WRITE)
167/* Stub for read only operation */
168int flush_dirty_fat_buffer(fsdata *mydata)
169{
170 (void)(mydata);
171 return 0;
172}
173#endif
174
wdenk71f95112003-06-15 22:40:42 +0000175/*
176 * Get the entry at index 'entry' in a FAT (12/16/32) table.
177 * On failure 0x00 is returned.
178 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200179static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000180{
181 __u32 bufnum;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200182 __u32 off16, offset;
wdenk71f95112003-06-15 22:40:42 +0000183 __u32 ret = 0x00;
184
Stefan Brünsb8948d22016-12-17 00:27:51 +0100185 if (CHECK_CLUST(entry, mydata->fatsize)) {
186 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
187 return ret;
188 }
189
wdenk71f95112003-06-15 22:40:42 +0000190 switch (mydata->fatsize) {
191 case 32:
192 bufnum = entry / FAT32BUFSIZE;
193 offset = entry - bufnum * FAT32BUFSIZE;
194 break;
195 case 16:
196 bufnum = entry / FAT16BUFSIZE;
197 offset = entry - bufnum * FAT16BUFSIZE;
198 break;
199 case 12:
200 bufnum = entry / FAT12BUFSIZE;
201 offset = entry - bufnum * FAT12BUFSIZE;
202 break;
203
204 default:
205 /* Unsupported FAT size */
206 return ret;
207 }
208
Stefan Brünsb8948d22016-12-17 00:27:51 +0100209 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk7385c282010-07-19 11:37:00 +0200210 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200211
wdenk71f95112003-06-15 22:40:42 +0000212 /* Read a new block of FAT entries into the cache. */
213 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000214 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000215 __u8 *bufptr = mydata->fatbuf;
216 __u32 fatlength = mydata->fatlength;
217 __u32 startblock = bufnum * FATBUFBLOCKS;
218
Stefan Brüns6c1a8082016-12-17 00:27:50 +0100219 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau8006dd22012-07-20 15:19:29 +0200220 if (startblock + getsize > fatlength)
221 getsize = fatlength - startblock;
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000222
wdenk71f95112003-06-15 22:40:42 +0000223 startblock += mydata->fat_sect; /* Offset from start of disk */
224
Stefan Brünsb8948d22016-12-17 00:27:51 +0100225 /* Write back the fatbuf to the disk */
226 if (flush_dirty_fat_buffer(mydata) < 0)
227 return -1;
228
wdenk71f95112003-06-15 22:40:42 +0000229 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200230 debug("Error reading FAT blocks\n");
wdenk71f95112003-06-15 22:40:42 +0000231 return ret;
232 }
233 mydata->fatbufnum = bufnum;
234 }
235
236 /* Get the actual entry from the table */
237 switch (mydata->fatsize) {
238 case 32:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200239 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000240 break;
241 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200242 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000243 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200244 case 12:
Stefan Brüns8d48c922016-12-17 03:55:10 +0100245 off16 = (offset * 3) / 2;
246 ret = FAT2CPU16(*(__u16 *)(mydata->fatbuf + off16));
wdenk71f95112003-06-15 22:40:42 +0000247
Stefan Brüns8d48c922016-12-17 03:55:10 +0100248 if (offset & 0x1)
249 ret >>= 4;
250 ret &= 0xfff;
wdenk71f95112003-06-15 22:40:42 +0000251 }
Stefan Brünsb8948d22016-12-17 00:27:51 +0100252 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
253 mydata->fatsize, ret, entry, offset);
wdenk71f95112003-06-15 22:40:42 +0000254
255 return ret;
256}
257
wdenk71f95112003-06-15 22:40:42 +0000258/*
259 * Read at most 'size' bytes from the specified cluster into 'buffer'.
260 * Return 0 on success, -1 otherwise.
261 */
262static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200263get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000264{
Erik Hansen3f270f42011-03-24 10:15:37 +0100265 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000266 __u32 startsect;
Kyle Moffett46236b12011-12-20 07:41:13 +0000267 int ret;
wdenk71f95112003-06-15 22:40:42 +0000268
269 if (clustnum > 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200270 startsect = mydata->data_begin +
271 clustnum * mydata->clust_size;
wdenk71f95112003-06-15 22:40:42 +0000272 } else {
273 startsect = mydata->rootdir_sect;
274 }
275
Wolfgang Denk7385c282010-07-19 11:37:00 +0200276 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
277
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200278 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson9a800ac2012-04-11 04:08:53 +0000279 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200280
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200281 printf("FAT: Misaligned buffer address (%p)\n", buffer);
282
283 while (size >= mydata->sect_size) {
284 ret = disk_read(startsect++, 1, tmpbuf);
285 if (ret != 1) {
286 debug("Error reading data (got %d)\n", ret);
287 return -1;
288 }
289
290 memcpy(buffer, tmpbuf, mydata->sect_size);
291 buffer += mydata->sect_size;
292 size -= mydata->sect_size;
293 }
294 } else {
Sergei Shtylyovac497772011-08-08 09:38:33 +0000295 idx = size / mydata->sect_size;
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200296 ret = disk_read(startsect, idx, buffer);
297 if (ret != idx) {
298 debug("Error reading data (got %d)\n", ret);
299 return -1;
300 }
301 startsect += idx;
302 idx *= mydata->sect_size;
303 buffer += idx;
304 size -= idx;
305 }
306 if (size) {
307 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
308
309 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett46236b12011-12-20 07:41:13 +0000310 if (ret != 1) {
311 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000312 return -1;
wdenk71f95112003-06-15 22:40:42 +0000313 }
wdenk7205e402003-09-10 22:30:53 +0000314
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200315 memcpy(buffer, tmpbuf, size);
wdenk71f95112003-06-15 22:40:42 +0000316 }
317
318 return 0;
319}
320
wdenk71f95112003-06-15 22:40:42 +0000321/*
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000322 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk71f95112003-06-15 22:40:42 +0000323 * into 'buffer'.
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800324 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
wdenk71f95112003-06-15 22:40:42 +0000325 */
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000326__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
327 __aligned(ARCH_DMA_MINALIGN);
328
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800329static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
330 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk71f95112003-06-15 22:40:42 +0000331{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800332 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000333 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000334 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000335 __u32 endclust, newclust;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800336 loff_t actsize;
wdenk71f95112003-06-15 22:40:42 +0000337
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800338 *gotsize = 0;
339 debug("Filesize: %llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000340
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000341 if (pos >= filesize) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800342 debug("Read position past EOF: %llu\n", pos);
343 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000344 }
345
346 if (maxsize > 0 && filesize > pos + maxsize)
347 filesize = pos + maxsize;
wdenk71f95112003-06-15 22:40:42 +0000348
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800349 debug("%llu bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000350
Wolfgang Denk7385c282010-07-19 11:37:00 +0200351 actsize = bytesperclust;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000352
353 /* go to cluster at pos */
354 while (actsize <= pos) {
355 curclust = get_fatent(mydata, curclust);
356 if (CHECK_CLUST(curclust, mydata->fatsize)) {
357 debug("curclust: 0x%x\n", curclust);
358 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800359 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000360 }
361 actsize += bytesperclust;
362 }
363
364 /* actsize > pos */
365 actsize -= bytesperclust;
366 filesize -= actsize;
367 pos -= actsize;
368
369 /* align to beginning of next cluster if any */
370 if (pos) {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800371 actsize = min(filesize, (loff_t)bytesperclust);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000372 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
373 (int)actsize) != 0) {
374 printf("Error reading cluster\n");
375 return -1;
376 }
377 filesize -= actsize;
378 actsize -= pos;
379 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800380 *gotsize += actsize;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000381 if (!filesize)
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800382 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000383 buffer += actsize;
384
385 curclust = get_fatent(mydata, curclust);
386 if (CHECK_CLUST(curclust, mydata->fatsize)) {
387 debug("curclust: 0x%x\n", curclust);
388 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800389 return 0;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000390 }
391 }
392
393 actsize = bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200394 endclust = curclust;
395
wdenk71f95112003-06-15 22:40:42 +0000396 do {
wdenk7205e402003-09-10 22:30:53 +0000397 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200398 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000399 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200400 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000401 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100402 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200403 debug("curclust: 0x%x\n", newclust);
404 debug("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800405 return 0;
wdenk7205e402003-09-10 22:30:53 +0000406 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200407 endclust = newclust;
408 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000409 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200410
wdenk7205e402003-09-10 22:30:53 +0000411 /* get remaining bytes */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200412 actsize = filesize;
Benoît Thébaudeau0880e5b2012-07-20 15:21:37 +0200413 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200414 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000415 return -1;
416 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800417 *gotsize += actsize;
418 return 0;
wdenk7205e402003-09-10 22:30:53 +0000419getit:
420 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200421 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000422 return -1;
423 }
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800424 *gotsize += (int)actsize;
wdenk7205e402003-09-10 22:30:53 +0000425 filesize -= actsize;
426 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200427
wdenk7205e402003-09-10 22:30:53 +0000428 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100429 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200430 debug("curclust: 0x%x\n", curclust);
431 printf("Invalid FAT entry\n");
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800432 return 0;
wdenk71f95112003-06-15 22:40:42 +0000433 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200434 actsize = bytesperclust;
435 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000436 } while (1);
437}
438
wdenk71f95112003-06-15 22:40:42 +0000439/*
440 * Extract the file name information from 'slotptr' into 'l_name',
441 * starting at l_name[*idx].
442 * Return 1 if terminator (zero byte) is found, 0 otherwise.
443 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200444static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000445{
446 int j;
447
448 for (j = 0; j <= 8; j += 2) {
449 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200450 if (l_name[*idx] == 0x00)
451 return 1;
wdenk71f95112003-06-15 22:40:42 +0000452 (*idx)++;
453 }
454 for (j = 0; j <= 10; j += 2) {
455 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200456 if (l_name[*idx] == 0x00)
457 return 1;
wdenk71f95112003-06-15 22:40:42 +0000458 (*idx)++;
459 }
460 for (j = 0; j <= 2; j += 2) {
461 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200462 if (l_name[*idx] == 0x00)
463 return 1;
wdenk71f95112003-06-15 22:40:42 +0000464 (*idx)++;
465 }
466
467 return 0;
468}
469
wdenk71f95112003-06-15 22:40:42 +0000470/*
471 * Extract the full long filename starting at 'retdent' (which is really
472 * a slot) into 'l_name'. If successful also copy the real directory entry
473 * into 'retdent'
474 * Return 0 on success, -1 otherwise.
475 */
476static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200477get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
478 dir_entry *retdent, char *l_name)
wdenk71f95112003-06-15 22:40:42 +0000479{
480 dir_entry *realdent;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200481 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000482 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
483 PREFETCH_BLOCKS :
484 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200485 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000486 int idx = 0;
487
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300488 if (counter > VFAT_MAXSEQ) {
489 debug("Error: VFAT name is too long\n");
490 return -1;
491 }
492
493 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200494 if (counter == 0)
495 break;
wdenk2d1a5372004-02-23 19:30:57 +0000496 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
497 return -1;
wdenk71f95112003-06-15 22:40:42 +0000498 slotptr++;
499 counter--;
500 }
501
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300502 if ((__u8 *)slotptr >= buflimit) {
wdenk71f95112003-06-15 22:40:42 +0000503 dir_slot *slotptr2;
504
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300505 if (curclust == 0)
506 return -1;
wdenk71f95112003-06-15 22:40:42 +0000507 curclust = get_fatent(mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100508 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200509 debug("curclust: 0x%x\n", curclust);
510 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000511 return -1;
512 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200513
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000514 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000515 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200516 debug("Error: reading directory block\n");
wdenk71f95112003-06-15 22:40:42 +0000517 return -1;
518 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200519
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000520 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300521 while (counter > 0) {
522 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
523 & 0xff) != counter)
524 return -1;
wdenk71f95112003-06-15 22:40:42 +0000525 slotptr2++;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300526 counter--;
527 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200528
wdenk71f95112003-06-15 22:40:42 +0000529 /* Save the real directory entry */
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300530 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000531 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000532 slotptr2--;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300533 slot2str(slotptr2, l_name, &idx);
wdenk71f95112003-06-15 22:40:42 +0000534 }
535 } else {
536 /* Save the real directory entry */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200537 realdent = (dir_entry *)slotptr;
wdenk71f95112003-06-15 22:40:42 +0000538 }
539
540 do {
541 slotptr--;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200542 if (slot2str(slotptr, l_name, &idx))
543 break;
wdenk2d1a5372004-02-23 19:30:57 +0000544 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000545
546 l_name[idx] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +0200547 if (*l_name == DELETED_FLAG)
548 *l_name = '\0';
549 else if (*l_name == aRING)
550 *l_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000551 downcase(l_name);
552
553 /* Return the real directory entry */
554 memcpy(retdent, realdent, sizeof(dir_entry));
555
556 return 0;
557}
558
wdenk71f95112003-06-15 22:40:42 +0000559/* Calculate short name checksum */
Marek Vasutff04f6d2012-10-09 07:20:22 +0000560static __u8 mkcksum(const char name[8], const char ext[3])
wdenk71f95112003-06-15 22:40:42 +0000561{
562 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200563
wdenk71f95112003-06-15 22:40:42 +0000564 __u8 ret = 0;
565
Marek Vasut6ad77d82013-01-11 03:35:48 +0000566 for (i = 0; i < 8; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000567 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut6ad77d82013-01-11 03:35:48 +0000568 for (i = 0; i < 3; i++)
Marek Vasutff04f6d2012-10-09 07:20:22 +0000569 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk71f95112003-06-15 22:40:42 +0000570
571 return ret;
572}
wdenk71f95112003-06-15 22:40:42 +0000573
574/*
575 * Get the directory entry associated with 'filename' from the directory
576 * starting at 'startsect'
577 */
Eric Nelson9a800ac2012-04-11 04:08:53 +0000578__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
579 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200580
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200581static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
582 char *filename, dir_entry *retdent,
583 int dols)
wdenk71f95112003-06-15 22:40:42 +0000584{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200585 __u16 prevcksum = 0xffff;
586 __u32 curclust = START(retdent);
587 int files = 0, dirs = 0;
wdenk71f95112003-06-15 22:40:42 +0000588
Wolfgang Denk7385c282010-07-19 11:37:00 +0200589 debug("get_dentfromdir: %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +0000590
Wolfgang Denk7385c282010-07-19 11:37:00 +0200591 while (1) {
592 dir_entry *dentptr;
wdenk71f95112003-06-15 22:40:42 +0000593
Wolfgang Denk7385c282010-07-19 11:37:00 +0200594 int i;
wdenk71f95112003-06-15 22:40:42 +0000595
Wolfgang Denk7385c282010-07-19 11:37:00 +0200596 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000597 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200598 debug("Error: reading directory block\n");
599 return NULL;
600 }
601
602 dentptr = (dir_entry *)get_dentfromdir_block;
603
604 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300605 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200606
607 l_name[0] = '\0';
608 if (dentptr->name[0] == DELETED_FLAG) {
609 dentptr++;
610 continue;
wdenk71f95112003-06-15 22:40:42 +0000611 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200612 if ((dentptr->attr & ATTR_VOLUME)) {
Richard Genoudcb940c72012-12-13 03:30:10 +0000613 if (vfat_enabled &&
614 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
J. Vijayanand206d68f2011-10-19 07:43:08 +0000615 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200616 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
617 get_vfatname(mydata, curclust,
618 get_dentfromdir_block,
619 dentptr, l_name);
620 if (dols) {
621 int isdir;
622 char dirc;
623 int doit = 0;
624
625 isdir = (dentptr->attr & ATTR_DIR);
626
627 if (isdir) {
628 dirs++;
629 dirc = '/';
630 doit = 1;
631 } else {
632 dirc = ' ';
633 if (l_name[0] != 0) {
634 files++;
635 doit = 1;
636 }
637 }
638 if (doit) {
639 if (dirc == ' ') {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800640 printf(" %8u %s%c\n",
641 FAT2CPU32(dentptr->size),
Wolfgang Denk7385c282010-07-19 11:37:00 +0200642 l_name,
643 dirc);
644 } else {
645 printf(" %s%c\n",
646 l_name,
647 dirc);
648 }
649 }
650 dentptr++;
651 continue;
652 }
653 debug("vfatname: |%s|\n", l_name);
Richard Genoudcb940c72012-12-13 03:30:10 +0000654 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200655 /* Volume label or VFAT entry */
656 dentptr++;
657 continue;
658 }
659 }
660 if (dentptr->name[0] == 0) {
661 if (dols) {
662 printf("\n%d file(s), %d dir(s)\n\n",
663 files, dirs);
664 }
665 debug("Dentname == NULL - %d\n", i);
666 return NULL;
667 }
Richard Genoudcb940c72012-12-13 03:30:10 +0000668 if (vfat_enabled) {
669 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
670 if (dols && csum == prevcksum) {
671 prevcksum = 0xffff;
672 dentptr++;
673 continue;
674 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200675 }
Richard Genoudcb940c72012-12-13 03:30:10 +0000676
Wolfgang Denk7385c282010-07-19 11:37:00 +0200677 get_name(dentptr, s_name);
678 if (dols) {
679 int isdir = (dentptr->attr & ATTR_DIR);
680 char dirc;
681 int doit = 0;
wdenk71f95112003-06-15 22:40:42 +0000682
Wolfgang Denk7385c282010-07-19 11:37:00 +0200683 if (isdir) {
684 dirs++;
685 dirc = '/';
686 doit = 1;
687 } else {
688 dirc = ' ';
689 if (s_name[0] != 0) {
690 files++;
691 doit = 1;
692 }
693 }
694
695 if (doit) {
696 if (dirc == ' ') {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800697 printf(" %8u %s%c\n",
698 FAT2CPU32(dentptr->size),
Wolfgang Denk7385c282010-07-19 11:37:00 +0200699 s_name, dirc);
700 } else {
701 printf(" %s%c\n",
702 s_name, dirc);
703 }
704 }
705
706 dentptr++;
707 continue;
708 }
709
710 if (strcmp(filename, s_name)
711 && strcmp(filename, l_name)) {
712 debug("Mismatch: |%s|%s|\n", s_name, l_name);
713 dentptr++;
714 continue;
715 }
716
717 memcpy(retdent, dentptr, sizeof(dir_entry));
718
719 debug("DentName: %s", s_name);
720 debug(", start: 0x%x", START(dentptr));
721 debug(", size: 0x%x %s\n",
722 FAT2CPU32(dentptr->size),
723 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
724
725 return retdent;
wdenk71f95112003-06-15 22:40:42 +0000726 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200727
728 curclust = get_fatent(mydata, curclust);
729 if (CHECK_CLUST(curclust, mydata->fatsize)) {
730 debug("curclust: 0x%x\n", curclust);
731 printf("Invalid FAT entry\n");
732 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000733 }
wdenk71f95112003-06-15 22:40:42 +0000734 }
wdenk71f95112003-06-15 22:40:42 +0000735
Wolfgang Denk7385c282010-07-19 11:37:00 +0200736 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000737}
738
wdenk71f95112003-06-15 22:40:42 +0000739/*
740 * Read boot sector and volume info from a FAT filesystem
741 */
742static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200743read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000744{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000745 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000746 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000747 int ret = 0;
748
749 if (cur_dev == NULL) {
750 debug("Error: no device selected\n");
751 return -1;
752 }
753
Eric Nelson9a800ac2012-04-11 04:08:53 +0000754 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000755 if (block == NULL) {
756 debug("Error: allocating block\n");
757 return -1;
758 }
wdenk71f95112003-06-15 22:40:42 +0000759
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200760 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200761 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000762 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000763 }
764
765 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200766 bs->reserved = FAT2CPU16(bs->reserved);
767 bs->fat_length = FAT2CPU16(bs->fat_length);
768 bs->secs_track = FAT2CPU16(bs->secs_track);
769 bs->heads = FAT2CPU16(bs->heads);
770 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000771
772 /* FAT32 entries */
773 if (bs->fat_length == 0) {
774 /* Assume FAT32 */
775 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200776 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000777 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200778 bs->info_sector = FAT2CPU16(bs->info_sector);
779 bs->backup_boot = FAT2CPU16(bs->backup_boot);
780 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000781 *fatsize = 32;
782 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200783 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000784 *fatsize = 0;
785 }
786 memcpy(volinfo, vistart, sizeof(volume_info));
787
wdenk71f95112003-06-15 22:40:42 +0000788 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200789 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000790 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000791 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500792 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000793 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000794 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000795 }
Tom Rix651351f2009-05-20 07:55:41 -0500796 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000797 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000798 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000799 }
800 }
801
Wolfgang Denk7385c282010-07-19 11:37:00 +0200802 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000803fail:
804 ret = -1;
805exit:
806 free(block);
807 return ret;
wdenk71f95112003-06-15 22:40:42 +0000808}
809
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000810__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
Eric Nelson9a800ac2012-04-11 04:08:53 +0000811 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200812
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800813int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
814 loff_t maxsize, int dols, int dogetsize, loff_t *size)
wdenk71f95112003-06-15 22:40:42 +0000815{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200816 char fnamecopy[2048];
817 boot_sector bs;
818 volume_info volinfo;
819 fsdata datablock;
820 fsdata *mydata = &datablock;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200821 dir_entry *dentptr = NULL;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200822 __u16 prevcksum = 0xffff;
823 char *subname = "";
Erik Hansen3f270f42011-03-24 10:15:37 +0100824 __u32 cursect;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200825 int idx, isdir = 0;
826 int files = 0, dirs = 0;
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -0800827 int ret = -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200828 int firsttime;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000829 __u32 root_cluster = 0;
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100830 __u32 read_blk;
Erik Hansen3f270f42011-03-24 10:15:37 +0100831 int rootdir_size = 0;
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100832 int buffer_blk_cnt;
833 int do_read;
834 __u8 *dir_ptr;
wdenk71f95112003-06-15 22:40:42 +0000835
Wolfgang Denk7385c282010-07-19 11:37:00 +0200836 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
837 debug("Error: reading boot sector\n");
838 return -1;
839 }
840
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000841 if (mydata->fatsize == 32) {
842 root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200843 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000844 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200845 mydata->fatlength = bs.fat_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000846 }
wdenk71f95112003-06-15 22:40:42 +0000847
Wolfgang Denk7385c282010-07-19 11:37:00 +0200848 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000849
Wolfgang Denk7385c282010-07-19 11:37:00 +0200850 cursect = mydata->rootdir_sect
851 = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000852
Sergei Shtylyovac497772011-08-08 09:38:33 +0000853 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200854 mydata->clust_size = bs.cluster_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000855 if (mydata->sect_size != cur_part_info.blksz) {
856 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
857 mydata->sect_size, cur_part_info.blksz);
858 return -1;
859 }
wdenk71f95112003-06-15 22:40:42 +0000860
Wolfgang Denk7385c282010-07-19 11:37:00 +0200861 if (mydata->fatsize == 32) {
862 mydata->data_begin = mydata->rootdir_sect -
863 (mydata->clust_size * 2);
864 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200865 rootdir_size = ((bs.dir_entries[1] * (int)256 +
866 bs.dir_entries[0]) *
867 sizeof(dir_entry)) /
Sergei Shtylyovac497772011-08-08 09:38:33 +0000868 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200869 mydata->data_begin = mydata->rootdir_sect +
870 rootdir_size -
871 (mydata->clust_size * 2);
wdenk71f95112003-06-15 22:40:42 +0000872 }
wdenk71f95112003-06-15 22:40:42 +0000873
Wolfgang Denk7385c282010-07-19 11:37:00 +0200874 mydata->fatbufnum = -1;
Stefan Brüns3c0ed9c2016-09-11 22:51:40 +0200875 mydata->fat_dirty = 0;
Eric Nelson9a800ac2012-04-11 04:08:53 +0000876 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000877 if (mydata->fatbuf == NULL) {
878 debug("Error: allocating memory\n");
879 return -1;
880 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200881
Richard Genoudcb940c72012-12-13 03:30:10 +0000882 if (vfat_enabled)
883 debug("VFAT Support enabled\n");
884
Wolfgang Denk7385c282010-07-19 11:37:00 +0200885 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
886 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
887 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
888 "Data begins at: %d\n",
889 root_cluster,
890 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000891 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
892 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
893 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200894
895 /* "cwd" is always the root... */
896 while (ISDIRDELIM(*filename))
897 filename++;
898
899 /* Make a copy of the filename and convert it to lowercase */
900 strcpy(fnamecopy, filename);
901 downcase(fnamecopy);
902
Stephen Warren18a10d42015-07-28 21:55:03 -0600903root_reparse:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200904 if (*fnamecopy == '\0') {
905 if (!dols)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000906 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200907
908 dols = LS_ROOT;
909 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
910 isdir = 1;
911 fnamecopy[idx] = '\0';
912 subname = fnamecopy + idx + 1;
913
914 /* Handle multiple delimiters */
915 while (ISDIRDELIM(*subname))
916 subname++;
917 } else if (dols) {
918 isdir = 1;
919 }
920
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100921 buffer_blk_cnt = 0;
922 firsttime = 1;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200923 while (1) {
924 int i;
925
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100926 if (mydata->fatsize == 32 || firsttime) {
927 dir_ptr = do_fat_read_at_block;
928 firsttime = 0;
929 } else {
930 /**
931 * FAT16 sector buffer modification:
932 * Each loop, the second buffered block is moved to
933 * the buffer begin, and two next sectors are read
934 * next to the previously moved one. So the sector
935 * buffer keeps always 3 sectors for fat16.
936 * And the current sector is the buffer second sector
937 * beside the "firsttime" read, when it is the first one.
938 *
939 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
940 * n = computed root dir sector
941 * loop | cursect-1 | cursect | cursect+1 |
942 * 0 | sector n+0 | sector n+1 | none |
943 * 1 | none | sector n+0 | sector n+1 |
944 * 0 | sector n+1 | sector n+2 | sector n+3 |
945 * 1 | sector n+3 | ...
946 */
947 dir_ptr = (do_fat_read_at_block + mydata->sect_size);
948 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size);
949 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200950
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100951 do_read = 1;
952
953 if (mydata->fatsize == 32 && buffer_blk_cnt)
954 do_read = 0;
955
956 if (do_read) {
957 read_blk = (mydata->fatsize == 32) ?
958 mydata->clust_size : PREFETCH_BLOCKS;
959
960 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
961 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK);
962
963 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200964 debug("Error: reading rootdir block\n");
965 goto exit;
966 }
967
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100968 dentptr = (dir_entry *)dir_ptr;
wdenk71f95112003-06-15 22:40:42 +0000969 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200970
Wolfgang Denk7385c282010-07-19 11:37:00 +0200971 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300972 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Marek Vasutff04f6d2012-10-09 07:20:22 +0000973 __u8 csum;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200974
975 l_name[0] = '\0';
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300976 if (dentptr->name[0] == DELETED_FLAG) {
977 dentptr++;
978 continue;
979 }
Marek Vasutff04f6d2012-10-09 07:20:22 +0000980
Richard Genoudcb940c72012-12-13 03:30:10 +0000981 if (vfat_enabled)
982 csum = mkcksum(dentptr->name, dentptr->ext);
983
Marek Vasutff04f6d2012-10-09 07:20:22 +0000984 if (dentptr->attr & ATTR_VOLUME) {
Richard Genoudcb940c72012-12-13 03:30:10 +0000985 if (vfat_enabled &&
986 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk7385c282010-07-19 11:37:00 +0200987 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
988 prevcksum =
989 ((dir_slot *)dentptr)->alias_checksum;
wdenk71f95112003-06-15 22:40:42 +0000990
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300991 get_vfatname(mydata,
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000992 root_cluster,
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +0100993 dir_ptr,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200994 dentptr, l_name);
995
996 if (dols == LS_ROOT) {
997 char dirc;
998 int doit = 0;
999 int isdir =
1000 (dentptr->attr & ATTR_DIR);
1001
1002 if (isdir) {
1003 dirs++;
1004 dirc = '/';
1005 doit = 1;
1006 } else {
1007 dirc = ' ';
1008 if (l_name[0] != 0) {
1009 files++;
1010 doit = 1;
1011 }
1012 }
1013 if (doit) {
1014 if (dirc == ' ') {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001015 printf(" %8u %s%c\n",
1016 FAT2CPU32(dentptr->size),
Wolfgang Denk7385c282010-07-19 11:37:00 +02001017 l_name,
1018 dirc);
1019 } else {
1020 printf(" %s%c\n",
1021 l_name,
1022 dirc);
1023 }
1024 }
1025 dentptr++;
1026 continue;
1027 }
1028 debug("Rootvfatname: |%s|\n",
1029 l_name);
Richard Genoudcb940c72012-12-13 03:30:10 +00001030 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +02001031 /* Volume label or VFAT entry */
1032 dentptr++;
1033 continue;
1034 }
1035 } else if (dentptr->name[0] == 0) {
1036 debug("RootDentname == NULL - %d\n", i);
1037 if (dols == LS_ROOT) {
1038 printf("\n%d file(s), %d dir(s)\n\n",
1039 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +00001040 ret = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001041 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001042 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001043 }
Richard Genoudcb940c72012-12-13 03:30:10 +00001044 else if (vfat_enabled &&
1045 dols == LS_ROOT && csum == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +00001046 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001047 dentptr++;
1048 continue;
1049 }
Richard Genoudcb940c72012-12-13 03:30:10 +00001050
Wolfgang Denk7385c282010-07-19 11:37:00 +02001051 get_name(dentptr, s_name);
1052
1053 if (dols == LS_ROOT) {
1054 int isdir = (dentptr->attr & ATTR_DIR);
1055 char dirc;
1056 int doit = 0;
1057
1058 if (isdir) {
1059 dirc = '/';
1060 if (s_name[0] != 0) {
1061 dirs++;
1062 doit = 1;
1063 }
1064 } else {
1065 dirc = ' ';
1066 if (s_name[0] != 0) {
1067 files++;
1068 doit = 1;
1069 }
1070 }
1071 if (doit) {
1072 if (dirc == ' ') {
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001073 printf(" %8u %s%c\n",
1074 FAT2CPU32(dentptr->size),
Wolfgang Denk7385c282010-07-19 11:37:00 +02001075 s_name, dirc);
1076 } else {
1077 printf(" %s%c\n",
1078 s_name, dirc);
1079 }
1080 }
1081 dentptr++;
1082 continue;
1083 }
1084
1085 if (strcmp(fnamecopy, s_name)
1086 && strcmp(fnamecopy, l_name)) {
1087 debug("RootMismatch: |%s|%s|\n", s_name,
1088 l_name);
1089 dentptr++;
1090 continue;
1091 }
1092
1093 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001094 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001095
1096 debug("RootName: %s", s_name);
1097 debug(", start: 0x%x", START(dentptr));
1098 debug(", size: 0x%x %s\n",
1099 FAT2CPU32(dentptr->size),
1100 isdir ? "(DIR)" : "");
1101
1102 goto rootdir_done; /* We got a match */
1103 }
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001104 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
Wolfgang Denk7385c282010-07-19 11:37:00 +02001105 mydata->clust_size);
1106
1107 /*
1108 * On FAT32 we must fetch the FAT entries for the next
1109 * root directory clusters when a cluster has been
1110 * completely processed.
1111 */
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001112 ++buffer_blk_cnt;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001113 int rootdir_end = 0;
1114 if (mydata->fatsize == 32) {
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001115 if (buffer_blk_cnt == mydata->clust_size) {
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001116 int nxtsect = 0;
1117 int nxt_clust = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001118
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001119 nxt_clust = get_fatent(mydata, root_cluster);
1120 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001121
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001122 nxtsect = mydata->data_begin +
1123 (nxt_clust * mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001124
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001125 root_cluster = nxt_clust;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001126
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001127 cursect = nxtsect;
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001128 buffer_blk_cnt = 0;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001129 }
wdenk71f95112003-06-15 22:40:42 +00001130 } else {
Przemyslaw Marczak64f65e12014-12-18 17:14:17 +01001131 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1132 buffer_blk_cnt = 0;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001133
1134 rootdir_end = (++cursect - mydata->rootdir_sect >=
1135 rootdir_size);
wdenk71f95112003-06-15 22:40:42 +00001136 }
Erik Hansen3f270f42011-03-24 10:15:37 +01001137
1138 /* If end of rootdir reached */
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001139 if (rootdir_end) {
Erik Hansen3f270f42011-03-24 10:15:37 +01001140 if (dols == LS_ROOT) {
1141 printf("\n%d file(s), %d dir(s)\n\n",
1142 files, dirs);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001143 *size = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +01001144 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001145 goto exit;
Erik Hansen3f270f42011-03-24 10:15:37 +01001146 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001147 }
1148rootdir_done:
1149
1150 firsttime = 1;
1151
1152 while (isdir) {
1153 int startsect = mydata->data_begin
1154 + START(dentptr) * mydata->clust_size;
1155 dir_entry dent;
1156 char *nextname = NULL;
1157
1158 dent = *dentptr;
1159 dentptr = &dent;
1160
1161 idx = dirdelim(subname);
1162
1163 if (idx >= 0) {
1164 subname[idx] = '\0';
1165 nextname = subname + idx + 1;
1166 /* Handle multiple delimiters */
1167 while (ISDIRDELIM(*nextname))
1168 nextname++;
1169 if (dols && *nextname == '\0')
1170 firsttime = 0;
1171 } else {
1172 if (dols && firsttime) {
1173 firsttime = 0;
1174 } else {
1175 isdir = 0;
1176 }
wdenk71f95112003-06-15 22:40:42 +00001177 }
wdenk71f95112003-06-15 22:40:42 +00001178
Wolfgang Denk7385c282010-07-19 11:37:00 +02001179 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1180 isdir ? 0 : dols) == NULL) {
1181 if (dols && !isdir)
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001182 *size = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +00001183 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001184 }
wdenk71f95112003-06-15 22:40:42 +00001185
Benoît Thébaudeau7ee46ce2012-07-20 03:20:29 +00001186 if (isdir && !(dentptr->attr & ATTR_DIR))
1187 goto exit;
1188
Stephen Warren18a10d42015-07-28 21:55:03 -06001189 /*
1190 * If we are looking for a directory, and found a directory
1191 * type entry, and the entry is for the root directory (as
1192 * denoted by a cluster number of 0), jump back to the start
1193 * of the function, since at least on FAT12/16, the root dir
1194 * lives in a hard-coded location and needs special handling
1195 * to parse, rather than simply following the cluster linked
1196 * list in the FAT, like other directories.
1197 */
1198 if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) {
1199 /*
1200 * Modify the filename to remove the prefix that gets
1201 * back to the root directory, so the initial root dir
1202 * parsing code can continue from where we are without
1203 * confusion.
1204 */
1205 strcpy(fnamecopy, nextname ?: "");
1206 /*
1207 * Set up state the same way as the function does when
1208 * first started. This is required for the root dir
1209 * parsing code operates in its expected environment.
1210 */
1211 subname = "";
1212 cursect = mydata->rootdir_sect;
1213 isdir = 0;
1214 goto root_reparse;
1215 }
1216
Benoît Thébaudeau7ee46ce2012-07-20 03:20:29 +00001217 if (idx >= 0)
Wolfgang Denk7385c282010-07-19 11:37:00 +02001218 subname = nextname;
wdenk71f95112003-06-15 22:40:42 +00001219 }
1220
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001221 if (dogetsize) {
1222 *size = FAT2CPU32(dentptr->size);
1223 ret = 0;
1224 } else {
1225 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
1226 }
1227 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
wdenk71f95112003-06-15 22:40:42 +00001228
Sergei Shtylyovac497772011-08-08 09:38:33 +00001229exit:
1230 free(mydata->fatbuf);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001231 return ret;
wdenk71f95112003-06-15 22:40:42 +00001232}
1233
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001234int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
1235 loff_t *actread)
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001236{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001237 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001238}
1239
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001240int file_fat_detectfs(void)
wdenk71f95112003-06-15 22:40:42 +00001241{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001242 boot_sector bs;
1243 volume_info volinfo;
1244 int fatsize;
1245 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001246
Wolfgang Denk7385c282010-07-19 11:37:00 +02001247 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001248 printf("No current device\n");
1249 return 1;
1250 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001251
Jon Loeligerdd60d122007-07-09 17:56:50 -05001252#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -05001253 defined(CONFIG_CMD_SATA) || \
Simon Glassc649e3c2016-05-01 11:36:02 -06001254 defined(CONFIG_SCSI) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001255 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001256 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001257 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001258 switch (cur_dev->if_type) {
1259 case IF_TYPE_IDE:
1260 printf("IDE");
1261 break;
1262 case IF_TYPE_SATA:
1263 printf("SATA");
1264 break;
1265 case IF_TYPE_SCSI:
1266 printf("SCSI");
1267 break;
1268 case IF_TYPE_ATAPI:
1269 printf("ATAPI");
1270 break;
1271 case IF_TYPE_USB:
1272 printf("USB");
1273 break;
1274 case IF_TYPE_DOC:
1275 printf("DOC");
1276 break;
1277 case IF_TYPE_MMC:
1278 printf("MMC");
1279 break;
1280 default:
1281 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001282 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001283
Simon Glassbcce53d2016-02-29 15:25:51 -07001284 printf("\n Device %d: ", cur_dev->devnum);
wdenk7205e402003-09-10 22:30:53 +00001285 dev_print(cur_dev);
1286#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001287
1288 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001289 printf("\nNo valid FAT fs found\n");
1290 return 1;
1291 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001292
1293 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001294 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001295 volinfo.fs_type[5] = '\0';
1296
Stephen Warren461f86e2012-10-17 06:44:57 +00001297 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001298
wdenk7205e402003-09-10 22:30:53 +00001299 return 0;
wdenk71f95112003-06-15 22:40:42 +00001300}
1301
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001302int file_fat_ls(const char *dir)
wdenk71f95112003-06-15 22:40:42 +00001303{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001304 loff_t size;
1305
1306 return do_fat_read(dir, NULL, 0, LS_YES, &size);
wdenk71f95112003-06-15 22:40:42 +00001307}
1308
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001309int fat_exists(const char *filename)
1310{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001311 int ret;
1312 loff_t size;
1313
1314 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1315 return ret == 0;
Stephen Warrenb7b5f312014-02-03 13:21:10 -07001316}
1317
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001318int fat_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -06001319{
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001320 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
Stephen Warrencf659812014-06-11 12:47:26 -06001321}
1322
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001323int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1324 loff_t maxsize, loff_t *actread)
wdenk71f95112003-06-15 22:40:42 +00001325{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001326 printf("reading %s\n", filename);
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001327 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1328 actread);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001329}
1330
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001331int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001332{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001333 loff_t actread;
1334 int ret;
1335
1336 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1337 if (ret)
1338 return ret;
1339 else
1340 return actread;
wdenk71f95112003-06-15 22:40:42 +00001341}
Simon Glasse6d52412012-12-26 09:53:33 +00001342
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001343int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1344 loff_t *actread)
Simon Glasse6d52412012-12-26 09:53:33 +00001345{
Suriyan Ramasami1ad0b982014-11-17 14:39:35 -08001346 int ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001347
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001348 ret = file_fat_read_at(filename, offset, buf, len, actread);
1349 if (ret)
Simon Glasse6d52412012-12-26 09:53:33 +00001350 printf("** Unable to read file %s **\n", filename);
Simon Glasse6d52412012-12-26 09:53:33 +00001351
Suriyan Ramasamid455d872014-11-17 14:39:38 -08001352 return ret;
Simon Glasse6d52412012-12-26 09:53:33 +00001353}
1354
1355void fat_close(void)
1356{
1357}