blob: 244ee091917fa01e8e7b9a61fc2ab51b92fbe90f [file] [log] [blame]
wdenk7a428cc2003-06-15 22:40:42 +00001/*
2 * fat.c
3 *
4 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
5 *
6 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
7 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenk7a428cc2003-06-15 22:40:42 +000010 */
11
12#include <common.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -070013#include <blk.h>
wdenk7a428cc2003-06-15 22:40:42 +000014#include <config.h>
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +000015#include <exports.h>
wdenk7a428cc2003-06-15 22:40:42 +000016#include <fat.h>
17#include <asm/byteorder.h>
wdenk2c9b05d2003-09-10 22:30:53 +000018#include <part.h>
Eric Nelson014705b2012-04-11 04:08:53 +000019#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060020#include <memalign.h>
Eric Nelson014705b2012-04-11 04:08:53 +000021#include <linux/compiler.h>
Richard Genoud2e372022012-12-13 00:47:36 +000022#include <linux/ctype.h>
wdenk7a428cc2003-06-15 22:40:42 +000023
Richard Genouda43f41f2012-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
wdenk7a428cc2003-06-15 22:40:42 +000030/*
31 * Convert a string to lowercase.
32 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +020033static void downcase(char *str)
wdenk7a428cc2003-06-15 22:40:42 +000034{
35 while (*str != '\0') {
Richard Genoud2e372022012-12-13 00:47:36 +000036 *str = tolower(*str);
wdenk7a428cc2003-06-15 22:40:42 +000037 str++;
38 }
39}
40
Simon Glasse3394752016-02-29 15:25:34 -070041static struct blk_desc *cur_dev;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000042static disk_partition_t cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020043
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000044#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000045#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020046#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000047
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000048static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000049{
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020050 ulong ret;
51
Simon Glass2ee8ada2016-02-29 15:25:52 -070052 if (!cur_dev)
wdenk2c9b05d2003-09-10 22:30:53 +000053 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020054
Simon Glass2ee8ada2016-02-29 15:25:52 -070055 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020056
57 if (nr_blocks && ret == 0)
58 return -1;
59
60 return ret;
wdenk7a428cc2003-06-15 22:40:42 +000061}
62
Simon Glasse3394752016-02-29 15:25:34 -070063int fat_set_blk_dev(struct blk_desc *dev_desc, disk_partition_t *info)
wdenk7a428cc2003-06-15 22:40:42 +000064{
Eric Nelson014705b2012-04-11 04:08:53 +000065 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000066
Stephen Warren288904d2012-10-17 06:44:59 +000067 cur_dev = dev_desc;
68 cur_part_info = *info;
Kyle Moffett1b3b4b72011-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;
Wolfgang Denke78f2582007-08-07 16:02:13 +020074 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000075
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;
wdenk7a428cc2003-06-15 22:40:42 +000090}
91
Simon Glasse3394752016-02-29 15:25:34 -070092int fat_register_device(struct blk_desc *dev_desc, int part_no)
Stephen Warren288904d2012-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 Glassb89a8442016-02-29 15:25:48 -0700100 if (part_get_info(dev_desc, part_no, &info)) {
Stephen Warren288904d2012-10-17 06:44:59 +0000101 if (part_no != 0) {
102 printf("** Partition %d not valid on device %d **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700103 part_no, dev_desc->devnum);
Stephen Warren288904d2012-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;
Patrick Delaunay73287092017-01-27 11:00:42 +0100113#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren288904d2012-10-17 06:44:59 +0000114 info.uuid[0] = 0;
115#endif
116 }
117
118 return fat_set_blk_dev(dev_desc, &info);
119}
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000120
wdenk7a428cc2003-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ébaudeau0a120372012-07-20 15:18:44 +0200125static int dirdelim(char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000126{
127 char *start = str;
128
129 while (*str != '\0') {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200130 if (ISDIRDELIM(*str))
131 return str - start;
wdenk7a428cc2003-06-15 22:40:42 +0000132 str++;
133 }
134 return -1;
135}
136
wdenk7a428cc2003-06-15 22:40:42 +0000137/*
138 * Extract zero terminated short name from a directory entry.
139 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200140static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000141{
142 char *ptr;
143
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200144 memcpy(s_name, dirent->name, 8);
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200152 memcpy(ptr, dirent->ext, 3);
wdenk7a428cc2003-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 Bohmer7c8f2ce2008-11-27 22:30:27 +0100161 *s_name = DELETED_FLAG;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200162 downcase(s_name);
wdenk7a428cc2003-06-15 22:40:42 +0000163}
164
Stefan Brüns9f95d812016-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
wdenk7a428cc2003-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ébaudeau0a120372012-07-20 15:18:44 +0200179static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000180{
181 __u32 bufnum;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200182 __u32 off16, offset;
wdenk7a428cc2003-06-15 22:40:42 +0000183 __u32 ret = 0x00;
184
Stefan Brüns9f95d812016-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
wdenk7a428cc2003-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üns9f95d812016-12-17 00:27:51 +0100209 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200210 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200211
wdenk7a428cc2003-06-15 22:40:42 +0000212 /* Read a new block of FAT entries into the cache. */
213 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000214 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000215 __u8 *bufptr = mydata->fatbuf;
216 __u32 fatlength = mydata->fatlength;
217 __u32 startblock = bufnum * FATBUFBLOCKS;
218
Stefan Brüns58bf86f2016-12-17 00:27:50 +0100219 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200220 if (startblock + getsize > fatlength)
221 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000222
wdenk7a428cc2003-06-15 22:40:42 +0000223 startblock += mydata->fat_sect; /* Offset from start of disk */
224
Stefan Brüns9f95d812016-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
wdenk7a428cc2003-06-15 22:40:42 +0000229 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200230 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200239 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000240 break;
241 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200242 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000243 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200244 case 12:
Stefan Brüns15f40632016-12-17 03:55:10 +0100245 off16 = (offset * 3) / 2;
246 ret = FAT2CPU16(*(__u16 *)(mydata->fatbuf + off16));
wdenk7a428cc2003-06-15 22:40:42 +0000247
Stefan Brüns15f40632016-12-17 03:55:10 +0100248 if (offset & 0x1)
249 ret >>= 4;
250 ret &= 0xfff;
wdenk7a428cc2003-06-15 22:40:42 +0000251 }
Stefan Brüns9f95d812016-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);
wdenk7a428cc2003-06-15 22:40:42 +0000254
255 return ret;
256}
257
wdenk7a428cc2003-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ébaudeau0a120372012-07-20 15:18:44 +0200263get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000264{
Erik Hansen4ea89662011-03-24 10:15:37 +0100265 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000266 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000267 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000268
269 if (clustnum > 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200270 startsect = mydata->data_begin +
271 clustnum * mydata->clust_size;
wdenk7a428cc2003-06-15 22:40:42 +0000272 } else {
273 startsect = mydata->rootdir_sect;
274 }
275
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200276 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
277
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200278 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000279 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200280
Benoît Thébaudeaue957c072012-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 Shtylyov5ab3ba72011-08-08 09:38:33 +0000295 idx = size / mydata->sect_size;
Benoît Thébaudeaue957c072012-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 Moffett83c8a782011-12-20 07:41:13 +0000310 if (ret != 1) {
311 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000312 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000313 }
wdenk2c9b05d2003-09-10 22:30:53 +0000314
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200315 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000316 }
317
318 return 0;
319}
320
wdenk7a428cc2003-06-15 22:40:42 +0000321/*
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000322 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk7a428cc2003-06-15 22:40:42 +0000323 * into 'buffer'.
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800324 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
wdenk7a428cc2003-06-15 22:40:42 +0000325 */
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000326__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
327 __aligned(ARCH_DMA_MINALIGN);
328
Suriyan Ramasami441c2232014-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)
wdenk7a428cc2003-06-15 22:40:42 +0000331{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800332 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000333 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000334 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000335 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800336 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000337
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800338 *gotsize = 0;
339 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000340
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000341 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800342 debug("Read position past EOF: %llu\n", pos);
343 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000344 }
345
346 if (maxsize > 0 && filesize > pos + maxsize)
347 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000348
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800349 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000350
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200351 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-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 Ramasami441c2232014-11-17 14:39:35 -0800359 return 0;
Benoît Thébaudeau5295aa82012-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 Ramasami441c2232014-11-17 14:39:35 -0800371 actsize = min(filesize, (loff_t)bytesperclust);
Benoît Thébaudeau5295aa82012-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 Ramasami441c2232014-11-17 14:39:35 -0800380 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000381 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800382 return 0;
Benoît Thébaudeau5295aa82012-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 Ramasami441c2232014-11-17 14:39:35 -0800389 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000390 }
391 }
392
393 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200394 endclust = curclust;
395
wdenk7a428cc2003-06-15 22:40:42 +0000396 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000397 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200398 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000399 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200400 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000401 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100402 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200403 debug("curclust: 0x%x\n", newclust);
404 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800405 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000406 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200407 endclust = newclust;
408 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000409 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200410
wdenk2c9b05d2003-09-10 22:30:53 +0000411 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200412 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200413 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200414 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000415 return -1;
416 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800417 *gotsize += actsize;
418 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000419getit:
420 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200421 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000422 return -1;
423 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800424 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000425 filesize -= actsize;
426 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200427
wdenk2c9b05d2003-09-10 22:30:53 +0000428 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100429 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200430 debug("curclust: 0x%x\n", curclust);
431 printf("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800432 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000433 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200434 actsize = bytesperclust;
435 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000436 } while (1);
437}
438
wdenk7a428cc2003-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ébaudeau0a120372012-07-20 15:18:44 +0200444static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200450 if (l_name[*idx] == 0x00)
451 return 1;
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200456 if (l_name[*idx] == 0x00)
457 return 1;
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200462 if (l_name[*idx] == 0x00)
463 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000464 (*idx)++;
465 }
466
467 return 0;
468}
469
wdenk7a428cc2003-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ébaudeau0a120372012-07-20 15:18:44 +0200477get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
478 dir_entry *retdent, char *l_name)
wdenk7a428cc2003-06-15 22:40:42 +0000479{
480 dir_entry *realdent;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200481 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000482 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
483 PREFETCH_BLOCKS :
484 mydata->clust_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200485 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk7a428cc2003-06-15 22:40:42 +0000486 int idx = 0;
487
Mikhail Zolotaryov45d513e2010-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 Denk927b8ce2010-07-19 11:37:00 +0200494 if (counter == 0)
495 break;
wdenk2ebee312004-02-23 19:30:57 +0000496 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
497 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000498 slotptr++;
499 counter--;
500 }
501
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300502 if ((__u8 *)slotptr >= buflimit) {
wdenk7a428cc2003-06-15 22:40:42 +0000503 dir_slot *slotptr2;
504
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300505 if (curclust == 0)
506 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000507 curclust = get_fatent(mydata, curclust);
michael2c4d2982008-03-02 23:33:46 +0100508 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200509 debug("curclust: 0x%x\n", curclust);
510 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000511 return -1;
512 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200513
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000514 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000515 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200516 debug("Error: reading directory block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000517 return -1;
518 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200519
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000520 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300521 while (counter > 0) {
522 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
523 & 0xff) != counter)
524 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000525 slotptr2++;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300526 counter--;
527 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200528
wdenk7a428cc2003-06-15 22:40:42 +0000529 /* Save the real directory entry */
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300530 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000531 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
wdenk7a428cc2003-06-15 22:40:42 +0000532 slotptr2--;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300533 slot2str(slotptr2, l_name, &idx);
wdenk7a428cc2003-06-15 22:40:42 +0000534 }
535 } else {
536 /* Save the real directory entry */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200537 realdent = (dir_entry *)slotptr;
wdenk7a428cc2003-06-15 22:40:42 +0000538 }
539
540 do {
541 slotptr--;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200542 if (slot2str(slotptr, l_name, &idx))
543 break;
wdenk2ebee312004-02-23 19:30:57 +0000544 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk7a428cc2003-06-15 22:40:42 +0000545
546 l_name[idx] = '\0';
Wolfgang Denk927b8ce2010-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;
wdenk7a428cc2003-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
wdenk7a428cc2003-06-15 22:40:42 +0000559/* Calculate short name checksum */
Marek Vasut47a470f2012-10-09 07:20:22 +0000560static __u8 mkcksum(const char name[8], const char ext[3])
wdenk7a428cc2003-06-15 22:40:42 +0000561{
562 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200563
wdenk7a428cc2003-06-15 22:40:42 +0000564 __u8 ret = 0;
565
Marek Vasut4ab1f642013-01-11 03:35:48 +0000566 for (i = 0; i < 8; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000567 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut4ab1f642013-01-11 03:35:48 +0000568 for (i = 0; i < 3; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000569 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk7a428cc2003-06-15 22:40:42 +0000570
571 return ret;
572}
wdenk7a428cc2003-06-15 22:40:42 +0000573
574/*
575 * Get the directory entry associated with 'filename' from the directory
576 * starting at 'startsect'
577 */
Eric Nelson014705b2012-04-11 04:08:53 +0000578__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
579 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200580
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200581static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
582 char *filename, dir_entry *retdent,
583 int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000584{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200585 __u16 prevcksum = 0xffff;
586 __u32 curclust = START(retdent);
587 int files = 0, dirs = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000588
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200589 debug("get_dentfromdir: %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +0000590
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200591 while (1) {
592 dir_entry *dentptr;
593
594 int i;
595
596 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000597 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200598 debug("Error: reading directory block\n");
599 return NULL;
600 }
wdenk7a428cc2003-06-15 22:40:42 +0000601
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200602 dentptr = (dir_entry *)get_dentfromdir_block;
603
604 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300605 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200606
607 l_name[0] = '\0';
608 if (dentptr->name[0] == DELETED_FLAG) {
609 dentptr++;
610 continue;
611 }
612 if ((dentptr->attr & ATTR_VOLUME)) {
Richard Genouda43f41f2012-12-13 03:30:10 +0000613 if (vfat_enabled &&
614 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
J. Vijayanand575e21e2011-10-19 07:43:08 +0000615 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk927b8ce2010-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;
wdenk7a428cc2003-06-15 22:40:42 +0000624
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200625 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 Ramasami441c2232014-11-17 14:39:35 -0800640 printf(" %8u %s%c\n",
641 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-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 Genouda43f41f2012-12-13 03:30:10 +0000654 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200655 /* Volume label or VFAT entry */
656 dentptr++;
657 continue;
658 }
wdenk7a428cc2003-06-15 22:40:42 +0000659 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200660 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;
wdenk7a428cc2003-06-15 22:40:42 +0000667 }
Richard Genouda43f41f2012-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 Denk927b8ce2010-07-19 11:37:00 +0200675 }
Richard Genouda43f41f2012-12-13 03:30:10 +0000676
Wolfgang Denk927b8ce2010-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;
wdenk7a428cc2003-06-15 22:40:42 +0000682
Wolfgang Denk927b8ce2010-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 }
wdenk7a428cc2003-06-15 22:40:42 +0000694
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200695 if (doit) {
696 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800697 printf(" %8u %s%c\n",
698 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200699 s_name, dirc);
700 } else {
701 printf(" %s%c\n",
702 s_name, dirc);
703 }
704 }
wdenk7a428cc2003-06-15 22:40:42 +0000705
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200706 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;
726 }
727
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;
733 }
wdenk7a428cc2003-06-15 22:40:42 +0000734 }
wdenk7a428cc2003-06-15 22:40:42 +0000735
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200736 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000737}
738
wdenk7a428cc2003-06-15 22:40:42 +0000739/*
740 * Read boot sector and volume info from a FAT filesystem
741 */
742static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200743read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000744{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000745 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000746 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-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 Nelson014705b2012-04-11 04:08:53 +0000754 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000755 if (block == NULL) {
756 debug("Error: allocating block\n");
757 return -1;
758 }
wdenk7a428cc2003-06-15 22:40:42 +0000759
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200760 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200761 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000762 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000763 }
764
765 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-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);
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200776 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000777 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-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));
wdenk7a428cc2003-06-15 22:40:42 +0000781 *fatsize = 32;
782 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200783 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000784 *fatsize = 0;
785 }
786 memcpy(volinfo, vistart, sizeof(volume_info));
787
wdenk7a428cc2003-06-15 22:40:42 +0000788 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200789 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000790 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000791 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500792 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000793 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000794 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000795 }
Tom Rix155abaa2009-05-20 07:55:41 -0500796 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000797 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000798 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000799 }
800 }
801
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200802 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000803fail:
804 ret = -1;
805exit:
806 free(block);
807 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000808}
809
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000810__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
Eric Nelson014705b2012-04-11 04:08:53 +0000811 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200812
Suriyan Ramasami441c2232014-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)
wdenk7a428cc2003-06-15 22:40:42 +0000815{
Wolfgang Denk927b8ce2010-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ébaudeau2b89d3e2012-07-20 15:20:12 +0200821 dir_entry *dentptr = NULL;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200822 __u16 prevcksum = 0xffff;
823 char *subname = "";
Erik Hansen4ea89662011-03-24 10:15:37 +0100824 __u32 cursect;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200825 int idx, isdir = 0;
826 int files = 0, dirs = 0;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800827 int ret = -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200828 int firsttime;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000829 __u32 root_cluster = 0;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100830 __u32 read_blk;
Erik Hansen4ea89662011-03-24 10:15:37 +0100831 int rootdir_size = 0;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100832 int buffer_blk_cnt;
833 int do_read;
834 __u8 *dir_ptr;
wdenk7a428cc2003-06-15 22:40:42 +0000835
Wolfgang Denk927b8ce2010-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 Shtylyov42cdc9f2011-08-19 09:32:34 +0000841 if (mydata->fatsize == 32) {
842 root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200843 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000844 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200845 mydata->fatlength = bs.fat_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000846 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200847
848 mydata->fat_sect = bs.reserved;
849
850 cursect = mydata->rootdir_sect
851 = mydata->fat_sect + mydata->fatlength * bs.fats;
852
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000853 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200854 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-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 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200860
Wolfgang Denk927b8ce2010-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 Denk927b8ce2010-07-19 11:37:00 +0200865 rootdir_size = ((bs.dir_entries[1] * (int)256 +
866 bs.dir_entries[0]) *
867 sizeof(dir_entry)) /
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000868 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200869 mydata->data_begin = mydata->rootdir_sect +
870 rootdir_size -
871 (mydata->clust_size * 2);
872 }
873
874 mydata->fatbufnum = -1;
Stefan Brüns751b31d2016-09-11 22:51:40 +0200875 mydata->fat_dirty = 0;
Eric Nelson014705b2012-04-11 04:08:53 +0000876 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000877 if (mydata->fatbuf == NULL) {
878 debug("Error: allocating memory\n");
879 return -1;
880 }
wdenk7a428cc2003-06-15 22:40:42 +0000881
Richard Genouda43f41f2012-12-13 03:30:10 +0000882 if (vfat_enabled)
883 debug("VFAT Support enabled\n");
884
Wolfgang Denk927b8ce2010-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 Shtylyov5ab3ba72011-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);
wdenk7a428cc2003-06-15 22:40:42 +0000894
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200895 /* "cwd" is always the root... */
896 while (ISDIRDELIM(*filename))
897 filename++;
wdenk7a428cc2003-06-15 22:40:42 +0000898
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200899 /* Make a copy of the filename and convert it to lowercase */
900 strcpy(fnamecopy, filename);
901 downcase(fnamecopy);
wdenk7a428cc2003-06-15 22:40:42 +0000902
Stephen Warren8bdf9612015-07-28 21:55:03 -0600903root_reparse:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200904 if (*fnamecopy == '\0') {
905 if (!dols)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000906 goto exit;
Wolfgang Denk927b8ce2010-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;
wdenk7a428cc2003-06-15 22:40:42 +0000919 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200920
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100921 buffer_blk_cnt = 0;
922 firsttime = 1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200923 while (1) {
924 int i;
925
Przemyslaw Marczakcd486d12014-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 }
950
951 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);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200962
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100963 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200964 debug("Error: reading rootdir block\n");
965 goto exit;
966 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200967
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100968 dentptr = (dir_entry *)dir_ptr;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200969 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200970
971 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300972 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Marek Vasut47a470f2012-10-09 07:20:22 +0000973 __u8 csum;
wdenk7a428cc2003-06-15 22:40:42 +0000974
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200975 l_name[0] = '\0';
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300976 if (dentptr->name[0] == DELETED_FLAG) {
977 dentptr++;
978 continue;
979 }
Marek Vasut47a470f2012-10-09 07:20:22 +0000980
Richard Genouda43f41f2012-12-13 03:30:10 +0000981 if (vfat_enabled)
982 csum = mkcksum(dentptr->name, dentptr->ext);
983
Marek Vasut47a470f2012-10-09 07:20:22 +0000984 if (dentptr->attr & ATTR_VOLUME) {
Richard Genouda43f41f2012-12-13 03:30:10 +0000985 if (vfat_enabled &&
986 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200987 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
988 prevcksum =
989 ((dir_slot *)dentptr)->alias_checksum;
wdenk7a428cc2003-06-15 22:40:42 +0000990
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300991 get_vfatname(mydata,
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000992 root_cluster,
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100993 dir_ptr,
Wolfgang Denk927b8ce2010-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 Ramasami441c2232014-11-17 14:39:35 -08001015 printf(" %8u %s%c\n",
1016 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-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 Genouda43f41f2012-12-13 03:30:10 +00001030 } else {
Wolfgang Denk927b8ce2010-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 Shtylyov5ab3ba72011-08-08 09:38:33 +00001040 ret = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001041 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001042 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001043 }
Richard Genouda43f41f2012-12-13 03:30:10 +00001044 else if (vfat_enabled &&
1045 dols == LS_ROOT && csum == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +00001046 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001047 dentptr++;
1048 continue;
1049 }
Richard Genouda43f41f2012-12-13 03:30:10 +00001050
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001051 get_name(dentptr, s_name);
wdenk7a428cc2003-06-15 22:40:42 +00001052
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001053 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 Ramasami441c2232014-11-17 14:39:35 -08001073 printf(" %8u %s%c\n",
1074 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-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 Shtylyov5ab3ba72011-08-08 09:38:33 +00001094 goto exit;
Wolfgang Denk927b8ce2010-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 */
wdenk7a428cc2003-06-15 22:40:42 +00001103 }
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001104 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001105 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +00001106
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001107 /*
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 Marczakcd486d12014-12-18 17:14:17 +01001112 ++buffer_blk_cnt;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001113 int rootdir_end = 0;
1114 if (mydata->fatsize == 32) {
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001115 if (buffer_blk_cnt == mydata->clust_size) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001116 int nxtsect = 0;
1117 int nxt_clust = 0;
wdenk7a428cc2003-06-15 22:40:42 +00001118
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001119 nxt_clust = get_fatent(mydata, root_cluster);
1120 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001121
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001122 nxtsect = mydata->data_begin +
1123 (nxt_clust * mydata->clust_size);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001124
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001125 root_cluster = nxt_clust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001126
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001127 cursect = nxtsect;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001128 buffer_blk_cnt = 0;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001129 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001130 } else {
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001131 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1132 buffer_blk_cnt = 0;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001133
1134 rootdir_end = (++cursect - mydata->rootdir_sect >=
1135 rootdir_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001136 }
Erik Hansen4ea89662011-03-24 10:15:37 +01001137
1138 /* If end of rootdir reached */
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001139 if (rootdir_end) {
Erik Hansen4ea89662011-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 Ramasami441c2232014-11-17 14:39:35 -08001143 *size = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +01001144 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001145 goto exit;
Erik Hansen4ea89662011-03-24 10:15:37 +01001146 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001147 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001148rootdir_done:
wdenk7a428cc2003-06-15 22:40:42 +00001149
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001150 firsttime = 1;
wdenk7a428cc2003-06-15 22:40:42 +00001151
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001152 while (isdir) {
1153 int startsect = mydata->data_begin
1154 + START(dentptr) * mydata->clust_size;
1155 dir_entry dent;
1156 char *nextname = NULL;
wdenk7a428cc2003-06-15 22:40:42 +00001157
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001158 dent = *dentptr;
1159 dentptr = &dent;
wdenk7a428cc2003-06-15 22:40:42 +00001160
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001161 idx = dirdelim(subname);
wdenk7a428cc2003-06-15 22:40:42 +00001162
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001163 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 }
1177 }
1178
1179 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1180 isdir ? 0 : dols) == NULL) {
1181 if (dols && !isdir)
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001182 *size = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001183 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001184 }
1185
Benoît Thébaudeau7ca2a8a2012-07-20 03:20:29 +00001186 if (isdir && !(dentptr->attr & ATTR_DIR))
1187 goto exit;
1188
Stephen Warren8bdf9612015-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ébaudeau7ca2a8a2012-07-20 03:20:29 +00001217 if (idx >= 0)
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001218 subname = nextname;
wdenk7a428cc2003-06-15 22:40:42 +00001219 }
wdenk7a428cc2003-06-15 22:40:42 +00001220
Suriyan Ramasami441c2232014-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);
wdenk7a428cc2003-06-15 22:40:42 +00001228
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001229exit:
1230 free(mydata->fatbuf);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001231 return ret;
1232}
wdenk7a428cc2003-06-15 22:40:42 +00001233
Suriyan Ramasami441c2232014-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ébaudeau5295aa82012-09-18 08:14:56 +00001236{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001237 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001238}
1239
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001240int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001241{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001242 boot_sector bs;
1243 volume_info volinfo;
1244 int fatsize;
1245 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001246
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001247 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001248 printf("No current device\n");
1249 return 1;
1250 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001251
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001252#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang853208e2008-12-09 23:20:18 -05001253 defined(CONFIG_CMD_SATA) || \
Simon Glass8706b812016-05-01 11:36:02 -06001254 defined(CONFIG_SCSI) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001255 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001256 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001257 printf("Interface: ");
Wolfgang Denk927b8ce2010-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");
wdenk2c9b05d2003-09-10 22:30:53 +00001282 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001283
Simon Glass2f26fff2016-02-29 15:25:51 -07001284 printf("\n Device %d: ", cur_dev->devnum);
wdenk2c9b05d2003-09-10 22:30:53 +00001285 dev_print(cur_dev);
1286#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001287
1288 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001289 printf("\nNo valid FAT fs found\n");
1290 return 1;
1291 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001292
1293 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001294 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001295 volinfo.fs_type[5] = '\0';
1296
Stephen Warrend398dbe2012-10-17 06:44:57 +00001297 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001298
wdenk2c9b05d2003-09-10 22:30:53 +00001299 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001300}
1301
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001302int file_fat_ls(const char *dir)
wdenk7a428cc2003-06-15 22:40:42 +00001303{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001304 loff_t size;
1305
1306 return do_fat_read(dir, NULL, 0, LS_YES, &size);
wdenk7a428cc2003-06-15 22:40:42 +00001307}
1308
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001309int fat_exists(const char *filename)
1310{
Suriyan Ramasami441c2232014-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 Warren1d2f9a02014-02-03 13:21:10 -07001316}
1317
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001318int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001319{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001320 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -06001321}
1322
Suriyan Ramasami441c2232014-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)
wdenk7a428cc2003-06-15 22:40:42 +00001325{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001326 printf("reading %s\n", filename);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001327 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1328 actread);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001329}
1330
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001331int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001332{
Suriyan Ramasami441c2232014-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;
wdenk7a428cc2003-06-15 22:40:42 +00001341}
Simon Glass19e38582012-12-26 09:53:33 +00001342
Suriyan Ramasami96171fb2014-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 Glass19e38582012-12-26 09:53:33 +00001345{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001346 int ret;
Simon Glass19e38582012-12-26 09:53:33 +00001347
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001348 ret = file_fat_read_at(filename, offset, buf, len, actread);
1349 if (ret)
Simon Glass19e38582012-12-26 09:53:33 +00001350 printf("** Unable to read file %s **\n", filename);
Simon Glass19e38582012-12-26 09:53:33 +00001351
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001352 return ret;
Simon Glass19e38582012-12-26 09:53:33 +00001353}
1354
1355void fat_close(void)
1356{
1357}