blob: 465a6875edf509a17054adeefa6af73985543bdc [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
Tom Rini240124c2017-08-14 21:02:08 -040057 if (ret != nr_blocks)
Łukasz Majewskid6f23d82015-09-03 14:21:39 +020058 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;
Stefan Brüns0f3b6972017-01-26 20:22:36 +0000182 __u32 offset, off8;
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üns0f3b6972017-01-26 20:22:36 +0000245 off8 = (offset * 3) / 2;
246 /* fatbut + off8 may be unaligned, read in byte granularity */
247 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
wdenk7a428cc2003-06-15 22:40:42 +0000248
Stefan Brüns15f40632016-12-17 03:55:10 +0100249 if (offset & 0x1)
250 ret >>= 4;
251 ret &= 0xfff;
wdenk7a428cc2003-06-15 22:40:42 +0000252 }
Stefan Brüns9f95d812016-12-17 00:27:51 +0100253 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
254 mydata->fatsize, ret, entry, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000255
256 return ret;
257}
258
wdenk7a428cc2003-06-15 22:40:42 +0000259/*
260 * Read at most 'size' bytes from the specified cluster into 'buffer'.
261 * Return 0 on success, -1 otherwise.
262 */
263static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200264get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000265{
Erik Hansen4ea89662011-03-24 10:15:37 +0100266 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000267 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000268 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000269
270 if (clustnum > 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200271 startsect = mydata->data_begin +
272 clustnum * mydata->clust_size;
wdenk7a428cc2003-06-15 22:40:42 +0000273 } else {
274 startsect = mydata->rootdir_sect;
275 }
276
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200277 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
278
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200279 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000280 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200281
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200282 printf("FAT: Misaligned buffer address (%p)\n", buffer);
283
284 while (size >= mydata->sect_size) {
285 ret = disk_read(startsect++, 1, tmpbuf);
286 if (ret != 1) {
287 debug("Error reading data (got %d)\n", ret);
288 return -1;
289 }
290
291 memcpy(buffer, tmpbuf, mydata->sect_size);
292 buffer += mydata->sect_size;
293 size -= mydata->sect_size;
294 }
295 } else {
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000296 idx = size / mydata->sect_size;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200297 ret = disk_read(startsect, idx, buffer);
298 if (ret != idx) {
299 debug("Error reading data (got %d)\n", ret);
300 return -1;
301 }
302 startsect += idx;
303 idx *= mydata->sect_size;
304 buffer += idx;
305 size -= idx;
306 }
307 if (size) {
308 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
309
310 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000311 if (ret != 1) {
312 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000313 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000314 }
wdenk2c9b05d2003-09-10 22:30:53 +0000315
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200316 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000317 }
318
319 return 0;
320}
321
wdenk7a428cc2003-06-15 22:40:42 +0000322/*
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000323 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk7a428cc2003-06-15 22:40:42 +0000324 * into 'buffer'.
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800325 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
wdenk7a428cc2003-06-15 22:40:42 +0000326 */
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000327__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
328 __aligned(ARCH_DMA_MINALIGN);
329
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800330static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
331 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk7a428cc2003-06-15 22:40:42 +0000332{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800333 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000334 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000335 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000336 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800337 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000338
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800339 *gotsize = 0;
340 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000341
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000342 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800343 debug("Read position past EOF: %llu\n", pos);
344 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000345 }
346
347 if (maxsize > 0 && filesize > pos + maxsize)
348 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000349
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800350 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000351
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200352 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000353
354 /* go to cluster at pos */
355 while (actsize <= pos) {
356 curclust = get_fatent(mydata, curclust);
357 if (CHECK_CLUST(curclust, mydata->fatsize)) {
358 debug("curclust: 0x%x\n", curclust);
359 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800360 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000361 }
362 actsize += bytesperclust;
363 }
364
365 /* actsize > pos */
366 actsize -= bytesperclust;
367 filesize -= actsize;
368 pos -= actsize;
369
370 /* align to beginning of next cluster if any */
371 if (pos) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800372 actsize = min(filesize, (loff_t)bytesperclust);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000373 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
374 (int)actsize) != 0) {
375 printf("Error reading cluster\n");
376 return -1;
377 }
378 filesize -= actsize;
379 actsize -= pos;
380 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800381 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000382 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800383 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000384 buffer += actsize;
385
386 curclust = get_fatent(mydata, curclust);
387 if (CHECK_CLUST(curclust, mydata->fatsize)) {
388 debug("curclust: 0x%x\n", curclust);
389 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800390 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000391 }
392 }
393
394 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200395 endclust = curclust;
396
wdenk7a428cc2003-06-15 22:40:42 +0000397 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000398 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200399 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000400 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200401 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000402 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100403 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200404 debug("curclust: 0x%x\n", newclust);
405 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800406 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000407 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200408 endclust = newclust;
409 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000410 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200411
wdenk2c9b05d2003-09-10 22:30:53 +0000412 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200413 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200414 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200415 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000416 return -1;
417 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800418 *gotsize += actsize;
419 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000420getit:
421 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200422 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000423 return -1;
424 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800425 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000426 filesize -= actsize;
427 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200428
wdenk2c9b05d2003-09-10 22:30:53 +0000429 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100430 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200431 debug("curclust: 0x%x\n", curclust);
432 printf("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800433 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000434 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200435 actsize = bytesperclust;
436 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000437 } while (1);
438}
439
wdenk7a428cc2003-06-15 22:40:42 +0000440/*
441 * Extract the file name information from 'slotptr' into 'l_name',
442 * starting at l_name[*idx].
443 * Return 1 if terminator (zero byte) is found, 0 otherwise.
444 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200445static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000446{
447 int j;
448
449 for (j = 0; j <= 8; j += 2) {
450 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200451 if (l_name[*idx] == 0x00)
452 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000453 (*idx)++;
454 }
455 for (j = 0; j <= 10; j += 2) {
456 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200457 if (l_name[*idx] == 0x00)
458 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000459 (*idx)++;
460 }
461 for (j = 0; j <= 2; j += 2) {
462 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200463 if (l_name[*idx] == 0x00)
464 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000465 (*idx)++;
466 }
467
468 return 0;
469}
470
wdenk7a428cc2003-06-15 22:40:42 +0000471/*
472 * Extract the full long filename starting at 'retdent' (which is really
473 * a slot) into 'l_name'. If successful also copy the real directory entry
474 * into 'retdent'
475 * Return 0 on success, -1 otherwise.
476 */
477static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200478get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
479 dir_entry *retdent, char *l_name)
wdenk7a428cc2003-06-15 22:40:42 +0000480{
481 dir_entry *realdent;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200482 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000483 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
484 PREFETCH_BLOCKS :
485 mydata->clust_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200486 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk7a428cc2003-06-15 22:40:42 +0000487 int idx = 0;
488
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300489 if (counter > VFAT_MAXSEQ) {
490 debug("Error: VFAT name is too long\n");
491 return -1;
492 }
493
494 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200495 if (counter == 0)
496 break;
wdenk2ebee312004-02-23 19:30:57 +0000497 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
498 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000499 slotptr++;
500 counter--;
501 }
502
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300503 if ((__u8 *)slotptr >= buflimit) {
wdenk7a428cc2003-06-15 22:40:42 +0000504 dir_slot *slotptr2;
505
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300506 if (curclust == 0)
507 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000508 curclust = get_fatent(mydata, curclust);
michael2c4d2982008-03-02 23:33:46 +0100509 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200510 debug("curclust: 0x%x\n", curclust);
511 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000512 return -1;
513 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200514
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000515 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000516 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200517 debug("Error: reading directory block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000518 return -1;
519 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200520
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000521 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300522 while (counter > 0) {
523 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
524 & 0xff) != counter)
525 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000526 slotptr2++;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300527 counter--;
528 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200529
wdenk7a428cc2003-06-15 22:40:42 +0000530 /* Save the real directory entry */
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300531 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000532 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
wdenk7a428cc2003-06-15 22:40:42 +0000533 slotptr2--;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300534 slot2str(slotptr2, l_name, &idx);
wdenk7a428cc2003-06-15 22:40:42 +0000535 }
536 } else {
537 /* Save the real directory entry */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200538 realdent = (dir_entry *)slotptr;
wdenk7a428cc2003-06-15 22:40:42 +0000539 }
540
541 do {
542 slotptr--;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200543 if (slot2str(slotptr, l_name, &idx))
544 break;
wdenk2ebee312004-02-23 19:30:57 +0000545 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk7a428cc2003-06-15 22:40:42 +0000546
547 l_name[idx] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200548 if (*l_name == DELETED_FLAG)
549 *l_name = '\0';
550 else if (*l_name == aRING)
551 *l_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000552 downcase(l_name);
553
554 /* Return the real directory entry */
555 memcpy(retdent, realdent, sizeof(dir_entry));
556
557 return 0;
558}
559
wdenk7a428cc2003-06-15 22:40:42 +0000560/* Calculate short name checksum */
Marek Vasut47a470f2012-10-09 07:20:22 +0000561static __u8 mkcksum(const char name[8], const char ext[3])
wdenk7a428cc2003-06-15 22:40:42 +0000562{
563 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200564
wdenk7a428cc2003-06-15 22:40:42 +0000565 __u8 ret = 0;
566
Marek Vasut4ab1f642013-01-11 03:35:48 +0000567 for (i = 0; i < 8; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000568 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut4ab1f642013-01-11 03:35:48 +0000569 for (i = 0; i < 3; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000570 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk7a428cc2003-06-15 22:40:42 +0000571
572 return ret;
573}
wdenk7a428cc2003-06-15 22:40:42 +0000574
575/*
576 * Get the directory entry associated with 'filename' from the directory
577 * starting at 'startsect'
578 */
Eric Nelson014705b2012-04-11 04:08:53 +0000579__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
580 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200581
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200582static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
583 char *filename, dir_entry *retdent,
584 int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000585{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200586 __u16 prevcksum = 0xffff;
587 __u32 curclust = START(retdent);
588 int files = 0, dirs = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000589
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200590 debug("get_dentfromdir: %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +0000591
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200592 while (1) {
593 dir_entry *dentptr;
594
595 int i;
596
597 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000598 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200599 debug("Error: reading directory block\n");
600 return NULL;
601 }
wdenk7a428cc2003-06-15 22:40:42 +0000602
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200603 dentptr = (dir_entry *)get_dentfromdir_block;
604
605 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300606 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200607
608 l_name[0] = '\0';
609 if (dentptr->name[0] == DELETED_FLAG) {
610 dentptr++;
611 continue;
612 }
613 if ((dentptr->attr & ATTR_VOLUME)) {
Richard Genouda43f41f2012-12-13 03:30:10 +0000614 if (vfat_enabled &&
615 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
J. Vijayanand575e21e2011-10-19 07:43:08 +0000616 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200617 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
618 get_vfatname(mydata, curclust,
619 get_dentfromdir_block,
620 dentptr, l_name);
621 if (dols) {
622 int isdir;
623 char dirc;
624 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000625
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200626 isdir = (dentptr->attr & ATTR_DIR);
627
628 if (isdir) {
629 dirs++;
630 dirc = '/';
631 doit = 1;
632 } else {
633 dirc = ' ';
634 if (l_name[0] != 0) {
635 files++;
636 doit = 1;
637 }
638 }
639 if (doit) {
640 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800641 printf(" %8u %s%c\n",
642 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200643 l_name,
644 dirc);
645 } else {
646 printf(" %s%c\n",
647 l_name,
648 dirc);
649 }
650 }
651 dentptr++;
652 continue;
653 }
654 debug("vfatname: |%s|\n", l_name);
Richard Genouda43f41f2012-12-13 03:30:10 +0000655 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200656 /* Volume label or VFAT entry */
657 dentptr++;
658 continue;
659 }
wdenk7a428cc2003-06-15 22:40:42 +0000660 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200661 if (dentptr->name[0] == 0) {
662 if (dols) {
663 printf("\n%d file(s), %d dir(s)\n\n",
664 files, dirs);
665 }
666 debug("Dentname == NULL - %d\n", i);
667 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000668 }
Richard Genouda43f41f2012-12-13 03:30:10 +0000669 if (vfat_enabled) {
670 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
671 if (dols && csum == prevcksum) {
672 prevcksum = 0xffff;
673 dentptr++;
674 continue;
675 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200676 }
Richard Genouda43f41f2012-12-13 03:30:10 +0000677
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200678 get_name(dentptr, s_name);
679 if (dols) {
680 int isdir = (dentptr->attr & ATTR_DIR);
681 char dirc;
682 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000683
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200684 if (isdir) {
685 dirs++;
686 dirc = '/';
687 doit = 1;
688 } else {
689 dirc = ' ';
690 if (s_name[0] != 0) {
691 files++;
692 doit = 1;
693 }
694 }
wdenk7a428cc2003-06-15 22:40:42 +0000695
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200696 if (doit) {
697 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800698 printf(" %8u %s%c\n",
699 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200700 s_name, dirc);
701 } else {
702 printf(" %s%c\n",
703 s_name, dirc);
704 }
705 }
wdenk7a428cc2003-06-15 22:40:42 +0000706
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200707 dentptr++;
708 continue;
709 }
710
711 if (strcmp(filename, s_name)
712 && strcmp(filename, l_name)) {
713 debug("Mismatch: |%s|%s|\n", s_name, l_name);
714 dentptr++;
715 continue;
716 }
717
718 memcpy(retdent, dentptr, sizeof(dir_entry));
719
720 debug("DentName: %s", s_name);
721 debug(", start: 0x%x", START(dentptr));
722 debug(", size: 0x%x %s\n",
723 FAT2CPU32(dentptr->size),
724 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
725
726 return retdent;
727 }
728
729 curclust = get_fatent(mydata, curclust);
730 if (CHECK_CLUST(curclust, mydata->fatsize)) {
731 debug("curclust: 0x%x\n", curclust);
732 printf("Invalid FAT entry\n");
733 return NULL;
734 }
wdenk7a428cc2003-06-15 22:40:42 +0000735 }
wdenk7a428cc2003-06-15 22:40:42 +0000736
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200737 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000738}
739
wdenk7a428cc2003-06-15 22:40:42 +0000740/*
741 * Read boot sector and volume info from a FAT filesystem
742 */
743static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200744read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000745{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000746 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000747 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000748 int ret = 0;
749
750 if (cur_dev == NULL) {
751 debug("Error: no device selected\n");
752 return -1;
753 }
754
Eric Nelson014705b2012-04-11 04:08:53 +0000755 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000756 if (block == NULL) {
757 debug("Error: allocating block\n");
758 return -1;
759 }
wdenk7a428cc2003-06-15 22:40:42 +0000760
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200761 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200762 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000763 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000764 }
765
766 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200767 bs->reserved = FAT2CPU16(bs->reserved);
768 bs->fat_length = FAT2CPU16(bs->fat_length);
769 bs->secs_track = FAT2CPU16(bs->secs_track);
770 bs->heads = FAT2CPU16(bs->heads);
771 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000772
773 /* FAT32 entries */
774 if (bs->fat_length == 0) {
775 /* Assume FAT32 */
776 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200777 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000778 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200779 bs->info_sector = FAT2CPU16(bs->info_sector);
780 bs->backup_boot = FAT2CPU16(bs->backup_boot);
781 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000782 *fatsize = 32;
783 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200784 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000785 *fatsize = 0;
786 }
787 memcpy(volinfo, vistart, sizeof(volume_info));
788
wdenk7a428cc2003-06-15 22:40:42 +0000789 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200790 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000791 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000792 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500793 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000794 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000795 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000796 }
Tom Rix155abaa2009-05-20 07:55:41 -0500797 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000798 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000799 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000800 }
801 }
802
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200803 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000804fail:
805 ret = -1;
806exit:
807 free(block);
808 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000809}
810
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000811__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
Eric Nelson014705b2012-04-11 04:08:53 +0000812 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200813
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800814int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
815 loff_t maxsize, int dols, int dogetsize, loff_t *size)
wdenk7a428cc2003-06-15 22:40:42 +0000816{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200817 char fnamecopy[2048];
818 boot_sector bs;
819 volume_info volinfo;
820 fsdata datablock;
821 fsdata *mydata = &datablock;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200822 dir_entry *dentptr = NULL;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200823 __u16 prevcksum = 0xffff;
824 char *subname = "";
Erik Hansen4ea89662011-03-24 10:15:37 +0100825 __u32 cursect;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200826 int idx, isdir = 0;
827 int files = 0, dirs = 0;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800828 int ret = -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200829 int firsttime;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000830 __u32 root_cluster = 0;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100831 __u32 read_blk;
Erik Hansen4ea89662011-03-24 10:15:37 +0100832 int rootdir_size = 0;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100833 int buffer_blk_cnt;
834 int do_read;
835 __u8 *dir_ptr;
wdenk7a428cc2003-06-15 22:40:42 +0000836
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200837 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
838 debug("Error: reading boot sector\n");
839 return -1;
840 }
841
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000842 if (mydata->fatsize == 32) {
843 root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200844 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000845 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200846 mydata->fatlength = bs.fat_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000847 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200848
849 mydata->fat_sect = bs.reserved;
850
851 cursect = mydata->rootdir_sect
852 = mydata->fat_sect + mydata->fatlength * bs.fats;
853
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000854 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200855 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000856 if (mydata->sect_size != cur_part_info.blksz) {
857 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
858 mydata->sect_size, cur_part_info.blksz);
859 return -1;
860 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200861
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200862 if (mydata->fatsize == 32) {
863 mydata->data_begin = mydata->rootdir_sect -
864 (mydata->clust_size * 2);
865 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200866 rootdir_size = ((bs.dir_entries[1] * (int)256 +
867 bs.dir_entries[0]) *
868 sizeof(dir_entry)) /
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000869 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200870 mydata->data_begin = mydata->rootdir_sect +
871 rootdir_size -
872 (mydata->clust_size * 2);
873 }
874
875 mydata->fatbufnum = -1;
Stefan Brüns751b31d2016-09-11 22:51:40 +0200876 mydata->fat_dirty = 0;
Eric Nelson014705b2012-04-11 04:08:53 +0000877 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000878 if (mydata->fatbuf == NULL) {
879 debug("Error: allocating memory\n");
880 return -1;
881 }
wdenk7a428cc2003-06-15 22:40:42 +0000882
Richard Genouda43f41f2012-12-13 03:30:10 +0000883 if (vfat_enabled)
884 debug("VFAT Support enabled\n");
885
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200886 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
887 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
888 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
889 "Data begins at: %d\n",
890 root_cluster,
891 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000892 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
893 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
894 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000895
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200896 /* "cwd" is always the root... */
897 while (ISDIRDELIM(*filename))
898 filename++;
wdenk7a428cc2003-06-15 22:40:42 +0000899
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200900 /* Make a copy of the filename and convert it to lowercase */
901 strcpy(fnamecopy, filename);
902 downcase(fnamecopy);
wdenk7a428cc2003-06-15 22:40:42 +0000903
Stephen Warren8bdf9612015-07-28 21:55:03 -0600904root_reparse:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200905 if (*fnamecopy == '\0') {
906 if (!dols)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000907 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200908
909 dols = LS_ROOT;
910 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
911 isdir = 1;
912 fnamecopy[idx] = '\0';
913 subname = fnamecopy + idx + 1;
914
915 /* Handle multiple delimiters */
916 while (ISDIRDELIM(*subname))
917 subname++;
918 } else if (dols) {
919 isdir = 1;
wdenk7a428cc2003-06-15 22:40:42 +0000920 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200921
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100922 buffer_blk_cnt = 0;
923 firsttime = 1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200924 while (1) {
925 int i;
926
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100927 if (mydata->fatsize == 32 || firsttime) {
928 dir_ptr = do_fat_read_at_block;
929 firsttime = 0;
930 } else {
931 /**
932 * FAT16 sector buffer modification:
933 * Each loop, the second buffered block is moved to
934 * the buffer begin, and two next sectors are read
935 * next to the previously moved one. So the sector
936 * buffer keeps always 3 sectors for fat16.
937 * And the current sector is the buffer second sector
938 * beside the "firsttime" read, when it is the first one.
939 *
940 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
941 * n = computed root dir sector
942 * loop | cursect-1 | cursect | cursect+1 |
943 * 0 | sector n+0 | sector n+1 | none |
944 * 1 | none | sector n+0 | sector n+1 |
945 * 0 | sector n+1 | sector n+2 | sector n+3 |
946 * 1 | sector n+3 | ...
947 */
948 dir_ptr = (do_fat_read_at_block + mydata->sect_size);
949 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size);
950 }
951
952 do_read = 1;
953
954 if (mydata->fatsize == 32 && buffer_blk_cnt)
955 do_read = 0;
956
957 if (do_read) {
958 read_blk = (mydata->fatsize == 32) ?
959 mydata->clust_size : PREFETCH_BLOCKS;
960
961 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
962 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200963
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100964 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200965 debug("Error: reading rootdir block\n");
966 goto exit;
967 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200968
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100969 dentptr = (dir_entry *)dir_ptr;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200970 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200971
972 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300973 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Marek Vasut47a470f2012-10-09 07:20:22 +0000974 __u8 csum;
wdenk7a428cc2003-06-15 22:40:42 +0000975
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200976 l_name[0] = '\0';
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300977 if (dentptr->name[0] == DELETED_FLAG) {
978 dentptr++;
979 continue;
980 }
Marek Vasut47a470f2012-10-09 07:20:22 +0000981
Richard Genouda43f41f2012-12-13 03:30:10 +0000982 if (vfat_enabled)
983 csum = mkcksum(dentptr->name, dentptr->ext);
984
Marek Vasut47a470f2012-10-09 07:20:22 +0000985 if (dentptr->attr & ATTR_VOLUME) {
Richard Genouda43f41f2012-12-13 03:30:10 +0000986 if (vfat_enabled &&
987 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200988 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
989 prevcksum =
990 ((dir_slot *)dentptr)->alias_checksum;
wdenk7a428cc2003-06-15 22:40:42 +0000991
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300992 get_vfatname(mydata,
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000993 root_cluster,
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100994 dir_ptr,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200995 dentptr, l_name);
996
997 if (dols == LS_ROOT) {
998 char dirc;
999 int doit = 0;
1000 int isdir =
1001 (dentptr->attr & ATTR_DIR);
1002
1003 if (isdir) {
1004 dirs++;
1005 dirc = '/';
1006 doit = 1;
1007 } else {
1008 dirc = ' ';
1009 if (l_name[0] != 0) {
1010 files++;
1011 doit = 1;
1012 }
1013 }
1014 if (doit) {
1015 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001016 printf(" %8u %s%c\n",
1017 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001018 l_name,
1019 dirc);
1020 } else {
1021 printf(" %s%c\n",
1022 l_name,
1023 dirc);
1024 }
1025 }
1026 dentptr++;
1027 continue;
1028 }
1029 debug("Rootvfatname: |%s|\n",
1030 l_name);
Richard Genouda43f41f2012-12-13 03:30:10 +00001031 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001032 /* Volume label or VFAT entry */
1033 dentptr++;
1034 continue;
1035 }
1036 } else if (dentptr->name[0] == 0) {
1037 debug("RootDentname == NULL - %d\n", i);
1038 if (dols == LS_ROOT) {
1039 printf("\n%d file(s), %d dir(s)\n\n",
1040 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001041 ret = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001042 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001043 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001044 }
Richard Genouda43f41f2012-12-13 03:30:10 +00001045 else if (vfat_enabled &&
1046 dols == LS_ROOT && csum == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +00001047 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001048 dentptr++;
1049 continue;
1050 }
Richard Genouda43f41f2012-12-13 03:30:10 +00001051
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001052 get_name(dentptr, s_name);
wdenk7a428cc2003-06-15 22:40:42 +00001053
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001054 if (dols == LS_ROOT) {
1055 int isdir = (dentptr->attr & ATTR_DIR);
1056 char dirc;
1057 int doit = 0;
1058
1059 if (isdir) {
1060 dirc = '/';
1061 if (s_name[0] != 0) {
1062 dirs++;
1063 doit = 1;
1064 }
1065 } else {
1066 dirc = ' ';
1067 if (s_name[0] != 0) {
1068 files++;
1069 doit = 1;
1070 }
1071 }
1072 if (doit) {
1073 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001074 printf(" %8u %s%c\n",
1075 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001076 s_name, dirc);
1077 } else {
1078 printf(" %s%c\n",
1079 s_name, dirc);
1080 }
1081 }
1082 dentptr++;
1083 continue;
1084 }
1085
1086 if (strcmp(fnamecopy, s_name)
1087 && strcmp(fnamecopy, l_name)) {
1088 debug("RootMismatch: |%s|%s|\n", s_name,
1089 l_name);
1090 dentptr++;
1091 continue;
1092 }
1093
1094 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001095 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001096
1097 debug("RootName: %s", s_name);
1098 debug(", start: 0x%x", START(dentptr));
1099 debug(", size: 0x%x %s\n",
1100 FAT2CPU32(dentptr->size),
1101 isdir ? "(DIR)" : "");
1102
1103 goto rootdir_done; /* We got a match */
wdenk7a428cc2003-06-15 22:40:42 +00001104 }
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001105 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001106 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +00001107
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001108 /*
1109 * On FAT32 we must fetch the FAT entries for the next
1110 * root directory clusters when a cluster has been
1111 * completely processed.
1112 */
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001113 ++buffer_blk_cnt;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001114 int rootdir_end = 0;
1115 if (mydata->fatsize == 32) {
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001116 if (buffer_blk_cnt == mydata->clust_size) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001117 int nxtsect = 0;
1118 int nxt_clust = 0;
wdenk7a428cc2003-06-15 22:40:42 +00001119
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001120 nxt_clust = get_fatent(mydata, root_cluster);
1121 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001122
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001123 nxtsect = mydata->data_begin +
1124 (nxt_clust * mydata->clust_size);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001125
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001126 root_cluster = nxt_clust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001127
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001128 cursect = nxtsect;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001129 buffer_blk_cnt = 0;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001130 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001131 } else {
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001132 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1133 buffer_blk_cnt = 0;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001134
1135 rootdir_end = (++cursect - mydata->rootdir_sect >=
1136 rootdir_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001137 }
Erik Hansen4ea89662011-03-24 10:15:37 +01001138
1139 /* If end of rootdir reached */
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001140 if (rootdir_end) {
Erik Hansen4ea89662011-03-24 10:15:37 +01001141 if (dols == LS_ROOT) {
1142 printf("\n%d file(s), %d dir(s)\n\n",
1143 files, dirs);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001144 *size = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +01001145 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001146 goto exit;
Erik Hansen4ea89662011-03-24 10:15:37 +01001147 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001148 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001149rootdir_done:
wdenk7a428cc2003-06-15 22:40:42 +00001150
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001151 firsttime = 1;
wdenk7a428cc2003-06-15 22:40:42 +00001152
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001153 while (isdir) {
1154 int startsect = mydata->data_begin
1155 + START(dentptr) * mydata->clust_size;
1156 dir_entry dent;
1157 char *nextname = NULL;
wdenk7a428cc2003-06-15 22:40:42 +00001158
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001159 dent = *dentptr;
1160 dentptr = &dent;
wdenk7a428cc2003-06-15 22:40:42 +00001161
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001162 idx = dirdelim(subname);
wdenk7a428cc2003-06-15 22:40:42 +00001163
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001164 if (idx >= 0) {
1165 subname[idx] = '\0';
1166 nextname = subname + idx + 1;
1167 /* Handle multiple delimiters */
1168 while (ISDIRDELIM(*nextname))
1169 nextname++;
1170 if (dols && *nextname == '\0')
1171 firsttime = 0;
1172 } else {
1173 if (dols && firsttime) {
1174 firsttime = 0;
1175 } else {
1176 isdir = 0;
1177 }
1178 }
1179
1180 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1181 isdir ? 0 : dols) == NULL) {
1182 if (dols && !isdir)
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001183 *size = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001184 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001185 }
1186
Benoît Thébaudeau7ca2a8a2012-07-20 03:20:29 +00001187 if (isdir && !(dentptr->attr & ATTR_DIR))
1188 goto exit;
1189
Stephen Warren8bdf9612015-07-28 21:55:03 -06001190 /*
1191 * If we are looking for a directory, and found a directory
1192 * type entry, and the entry is for the root directory (as
1193 * denoted by a cluster number of 0), jump back to the start
1194 * of the function, since at least on FAT12/16, the root dir
1195 * lives in a hard-coded location and needs special handling
1196 * to parse, rather than simply following the cluster linked
1197 * list in the FAT, like other directories.
1198 */
1199 if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) {
1200 /*
1201 * Modify the filename to remove the prefix that gets
1202 * back to the root directory, so the initial root dir
1203 * parsing code can continue from where we are without
1204 * confusion.
1205 */
1206 strcpy(fnamecopy, nextname ?: "");
1207 /*
1208 * Set up state the same way as the function does when
1209 * first started. This is required for the root dir
1210 * parsing code operates in its expected environment.
1211 */
1212 subname = "";
1213 cursect = mydata->rootdir_sect;
1214 isdir = 0;
1215 goto root_reparse;
1216 }
1217
Benoît Thébaudeau7ca2a8a2012-07-20 03:20:29 +00001218 if (idx >= 0)
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001219 subname = nextname;
wdenk7a428cc2003-06-15 22:40:42 +00001220 }
wdenk7a428cc2003-06-15 22:40:42 +00001221
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001222 if (dogetsize) {
1223 *size = FAT2CPU32(dentptr->size);
1224 ret = 0;
1225 } else {
1226 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
1227 }
1228 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
wdenk7a428cc2003-06-15 22:40:42 +00001229
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001230exit:
1231 free(mydata->fatbuf);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001232 return ret;
1233}
wdenk7a428cc2003-06-15 22:40:42 +00001234
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001235int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
1236 loff_t *actread)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001237{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001238 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001239}
1240
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001241int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001242{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001243 boot_sector bs;
1244 volume_info volinfo;
1245 int fatsize;
1246 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001247
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001248 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001249 printf("No current device\n");
1250 return 1;
1251 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001252
Simon Glassb569a012017-05-17 03:25:30 -06001253#if defined(CONFIG_IDE) || \
Simon Glassab3055a2017-06-14 21:28:25 -06001254 defined(CONFIG_SATA) || \
Simon Glass8706b812016-05-01 11:36:02 -06001255 defined(CONFIG_SCSI) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001256 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001257 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001258 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001259 switch (cur_dev->if_type) {
1260 case IF_TYPE_IDE:
1261 printf("IDE");
1262 break;
1263 case IF_TYPE_SATA:
1264 printf("SATA");
1265 break;
1266 case IF_TYPE_SCSI:
1267 printf("SCSI");
1268 break;
1269 case IF_TYPE_ATAPI:
1270 printf("ATAPI");
1271 break;
1272 case IF_TYPE_USB:
1273 printf("USB");
1274 break;
1275 case IF_TYPE_DOC:
1276 printf("DOC");
1277 break;
1278 case IF_TYPE_MMC:
1279 printf("MMC");
1280 break;
1281 default:
1282 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001283 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001284
Simon Glass2f26fff2016-02-29 15:25:51 -07001285 printf("\n Device %d: ", cur_dev->devnum);
wdenk2c9b05d2003-09-10 22:30:53 +00001286 dev_print(cur_dev);
1287#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001288
1289 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001290 printf("\nNo valid FAT fs found\n");
1291 return 1;
1292 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001293
1294 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001295 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001296 volinfo.fs_type[5] = '\0';
1297
Stephen Warrend398dbe2012-10-17 06:44:57 +00001298 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001299
wdenk2c9b05d2003-09-10 22:30:53 +00001300 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001301}
1302
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001303int file_fat_ls(const char *dir)
wdenk7a428cc2003-06-15 22:40:42 +00001304{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001305 loff_t size;
1306
1307 return do_fat_read(dir, NULL, 0, LS_YES, &size);
wdenk7a428cc2003-06-15 22:40:42 +00001308}
1309
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001310int fat_exists(const char *filename)
1311{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001312 int ret;
1313 loff_t size;
1314
1315 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1316 return ret == 0;
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001317}
1318
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001319int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001320{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001321 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -06001322}
1323
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001324int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1325 loff_t maxsize, loff_t *actread)
wdenk7a428cc2003-06-15 22:40:42 +00001326{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001327 printf("reading %s\n", filename);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001328 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1329 actread);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001330}
1331
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001332int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001333{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001334 loff_t actread;
1335 int ret;
1336
1337 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1338 if (ret)
1339 return ret;
1340 else
1341 return actread;
wdenk7a428cc2003-06-15 22:40:42 +00001342}
Simon Glass19e38582012-12-26 09:53:33 +00001343
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001344int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1345 loff_t *actread)
Simon Glass19e38582012-12-26 09:53:33 +00001346{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001347 int ret;
Simon Glass19e38582012-12-26 09:53:33 +00001348
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001349 ret = file_fat_read_at(filename, offset, buf, len, actread);
1350 if (ret)
Simon Glass19e38582012-12-26 09:53:33 +00001351 printf("** Unable to read file %s **\n", filename);
Simon Glass19e38582012-12-26 09:53:33 +00001352
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001353 return ret;
Simon Glass19e38582012-12-26 09:53:33 +00001354}
1355
1356void fat_close(void)
1357{
1358}