blob: 600a90e30922621c03c1db8c65119b7fbe85f50a [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;
113#ifdef CONFIG_PARTITION_UUIDS
114 info.uuid[0] = 0;
115#endif
116 }
117
118 return fat_set_blk_dev(dev_desc, &info);
119}
Kyle 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
165/*
166 * Get the entry at index 'entry' in a FAT (12/16/32) table.
167 * On failure 0x00 is returned.
168 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200169static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000170{
171 __u32 bufnum;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200172 __u32 off16, offset;
wdenk7a428cc2003-06-15 22:40:42 +0000173 __u32 ret = 0x00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200174 __u16 val1, val2;
wdenk7a428cc2003-06-15 22:40:42 +0000175
176 switch (mydata->fatsize) {
177 case 32:
178 bufnum = entry / FAT32BUFSIZE;
179 offset = entry - bufnum * FAT32BUFSIZE;
180 break;
181 case 16:
182 bufnum = entry / FAT16BUFSIZE;
183 offset = entry - bufnum * FAT16BUFSIZE;
184 break;
185 case 12:
186 bufnum = entry / FAT12BUFSIZE;
187 offset = entry - bufnum * FAT12BUFSIZE;
188 break;
189
190 default:
191 /* Unsupported FAT size */
192 return ret;
193 }
194
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200195 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
196 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200197
wdenk7a428cc2003-06-15 22:40:42 +0000198 /* Read a new block of FAT entries into the cache. */
199 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000200 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000201 __u8 *bufptr = mydata->fatbuf;
202 __u32 fatlength = mydata->fatlength;
203 __u32 startblock = bufnum * FATBUFBLOCKS;
204
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200205 if (startblock + getsize > fatlength)
206 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000207
wdenk7a428cc2003-06-15 22:40:42 +0000208 startblock += mydata->fat_sect; /* Offset from start of disk */
209
wdenk7a428cc2003-06-15 22:40:42 +0000210 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200211 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000212 return ret;
213 }
214 mydata->fatbufnum = bufnum;
215 }
216
217 /* Get the actual entry from the table */
218 switch (mydata->fatsize) {
219 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200220 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000221 break;
222 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200223 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000224 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200225 case 12:
226 off16 = (offset * 3) / 4;
wdenk7a428cc2003-06-15 22:40:42 +0000227
228 switch (offset & 0x3) {
229 case 0:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200230 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000231 ret &= 0xfff;
232 break;
233 case 1:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200234 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000235 val1 &= 0xf000;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200236 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000237 val2 &= 0x00ff;
238 ret = (val2 << 4) | (val1 >> 12);
239 break;
240 case 2:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200241 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000242 val1 &= 0xff00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200243 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000244 val2 &= 0x000f;
245 ret = (val2 << 8) | (val1 >> 8);
246 break;
247 case 3:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200248 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000249 ret = (ret & 0xfff0) >> 4;
250 break;
251 default:
252 break;
253 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200254 break;
wdenk7a428cc2003-06-15 22:40:42 +0000255 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200256 debug("FAT%d: ret: %08x, offset: %04x\n",
257 mydata->fatsize, ret, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000258
259 return ret;
260}
261
wdenk7a428cc2003-06-15 22:40:42 +0000262/*
263 * Read at most 'size' bytes from the specified cluster into 'buffer'.
264 * Return 0 on success, -1 otherwise.
265 */
266static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200267get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000268{
Erik Hansen4ea89662011-03-24 10:15:37 +0100269 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000270 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000271 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000272
273 if (clustnum > 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200274 startsect = mydata->data_begin +
275 clustnum * mydata->clust_size;
wdenk7a428cc2003-06-15 22:40:42 +0000276 } else {
277 startsect = mydata->rootdir_sect;
278 }
279
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200280 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
281
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200282 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000283 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200284
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200285 printf("FAT: Misaligned buffer address (%p)\n", buffer);
286
287 while (size >= mydata->sect_size) {
288 ret = disk_read(startsect++, 1, tmpbuf);
289 if (ret != 1) {
290 debug("Error reading data (got %d)\n", ret);
291 return -1;
292 }
293
294 memcpy(buffer, tmpbuf, mydata->sect_size);
295 buffer += mydata->sect_size;
296 size -= mydata->sect_size;
297 }
298 } else {
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000299 idx = size / mydata->sect_size;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200300 ret = disk_read(startsect, idx, buffer);
301 if (ret != idx) {
302 debug("Error reading data (got %d)\n", ret);
303 return -1;
304 }
305 startsect += idx;
306 idx *= mydata->sect_size;
307 buffer += idx;
308 size -= idx;
309 }
310 if (size) {
311 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
312
313 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000314 if (ret != 1) {
315 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000316 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000317 }
wdenk2c9b05d2003-09-10 22:30:53 +0000318
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200319 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000320 }
321
322 return 0;
323}
324
wdenk7a428cc2003-06-15 22:40:42 +0000325/*
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000326 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk7a428cc2003-06-15 22:40:42 +0000327 * into 'buffer'.
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800328 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
wdenk7a428cc2003-06-15 22:40:42 +0000329 */
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000330__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
331 __aligned(ARCH_DMA_MINALIGN);
332
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800333static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
334 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
wdenk7a428cc2003-06-15 22:40:42 +0000335{
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800336 loff_t filesize = FAT2CPU32(dentptr->size);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000337 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000338 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000339 __u32 endclust, newclust;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800340 loff_t actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000341
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800342 *gotsize = 0;
343 debug("Filesize: %llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000344
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000345 if (pos >= filesize) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800346 debug("Read position past EOF: %llu\n", pos);
347 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000348 }
349
350 if (maxsize > 0 && filesize > pos + maxsize)
351 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000352
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800353 debug("%llu bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000354
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200355 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000356
357 /* go to cluster at pos */
358 while (actsize <= pos) {
359 curclust = get_fatent(mydata, curclust);
360 if (CHECK_CLUST(curclust, mydata->fatsize)) {
361 debug("curclust: 0x%x\n", curclust);
362 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800363 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000364 }
365 actsize += bytesperclust;
366 }
367
368 /* actsize > pos */
369 actsize -= bytesperclust;
370 filesize -= actsize;
371 pos -= actsize;
372
373 /* align to beginning of next cluster if any */
374 if (pos) {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800375 actsize = min(filesize, (loff_t)bytesperclust);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000376 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
377 (int)actsize) != 0) {
378 printf("Error reading cluster\n");
379 return -1;
380 }
381 filesize -= actsize;
382 actsize -= pos;
383 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800384 *gotsize += actsize;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000385 if (!filesize)
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800386 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000387 buffer += actsize;
388
389 curclust = get_fatent(mydata, curclust);
390 if (CHECK_CLUST(curclust, mydata->fatsize)) {
391 debug("curclust: 0x%x\n", curclust);
392 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800393 return 0;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000394 }
395 }
396
397 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200398 endclust = curclust;
399
wdenk7a428cc2003-06-15 22:40:42 +0000400 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000401 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200402 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000403 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200404 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000405 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100406 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200407 debug("curclust: 0x%x\n", newclust);
408 debug("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800409 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000410 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200411 endclust = newclust;
412 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000413 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200414
wdenk2c9b05d2003-09-10 22:30:53 +0000415 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200416 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200417 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200418 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000419 return -1;
420 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800421 *gotsize += actsize;
422 return 0;
wdenk2c9b05d2003-09-10 22:30:53 +0000423getit:
424 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200425 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000426 return -1;
427 }
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800428 *gotsize += (int)actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000429 filesize -= actsize;
430 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200431
wdenk2c9b05d2003-09-10 22:30:53 +0000432 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100433 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200434 debug("curclust: 0x%x\n", curclust);
435 printf("Invalid FAT entry\n");
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800436 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000437 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200438 actsize = bytesperclust;
439 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000440 } while (1);
441}
442
wdenk7a428cc2003-06-15 22:40:42 +0000443/*
444 * Extract the file name information from 'slotptr' into 'l_name',
445 * starting at l_name[*idx].
446 * Return 1 if terminator (zero byte) is found, 0 otherwise.
447 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200448static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000449{
450 int j;
451
452 for (j = 0; j <= 8; j += 2) {
453 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200454 if (l_name[*idx] == 0x00)
455 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000456 (*idx)++;
457 }
458 for (j = 0; j <= 10; j += 2) {
459 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200460 if (l_name[*idx] == 0x00)
461 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000462 (*idx)++;
463 }
464 for (j = 0; j <= 2; j += 2) {
465 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200466 if (l_name[*idx] == 0x00)
467 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000468 (*idx)++;
469 }
470
471 return 0;
472}
473
wdenk7a428cc2003-06-15 22:40:42 +0000474/*
475 * Extract the full long filename starting at 'retdent' (which is really
476 * a slot) into 'l_name'. If successful also copy the real directory entry
477 * into 'retdent'
478 * Return 0 on success, -1 otherwise.
479 */
480static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200481get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
482 dir_entry *retdent, char *l_name)
wdenk7a428cc2003-06-15 22:40:42 +0000483{
484 dir_entry *realdent;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200485 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000486 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
487 PREFETCH_BLOCKS :
488 mydata->clust_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200489 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk7a428cc2003-06-15 22:40:42 +0000490 int idx = 0;
491
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300492 if (counter > VFAT_MAXSEQ) {
493 debug("Error: VFAT name is too long\n");
494 return -1;
495 }
496
497 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200498 if (counter == 0)
499 break;
wdenk2ebee312004-02-23 19:30:57 +0000500 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
501 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000502 slotptr++;
503 counter--;
504 }
505
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300506 if ((__u8 *)slotptr >= buflimit) {
wdenk7a428cc2003-06-15 22:40:42 +0000507 dir_slot *slotptr2;
508
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300509 if (curclust == 0)
510 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000511 curclust = get_fatent(mydata, curclust);
michael2c4d2982008-03-02 23:33:46 +0100512 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200513 debug("curclust: 0x%x\n", curclust);
514 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000515 return -1;
516 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200517
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000518 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000519 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200520 debug("Error: reading directory block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000521 return -1;
522 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200523
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000524 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300525 while (counter > 0) {
526 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
527 & 0xff) != counter)
528 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000529 slotptr2++;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300530 counter--;
531 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200532
wdenk7a428cc2003-06-15 22:40:42 +0000533 /* Save the real directory entry */
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300534 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000535 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
wdenk7a428cc2003-06-15 22:40:42 +0000536 slotptr2--;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300537 slot2str(slotptr2, l_name, &idx);
wdenk7a428cc2003-06-15 22:40:42 +0000538 }
539 } else {
540 /* Save the real directory entry */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200541 realdent = (dir_entry *)slotptr;
wdenk7a428cc2003-06-15 22:40:42 +0000542 }
543
544 do {
545 slotptr--;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200546 if (slot2str(slotptr, l_name, &idx))
547 break;
wdenk2ebee312004-02-23 19:30:57 +0000548 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk7a428cc2003-06-15 22:40:42 +0000549
550 l_name[idx] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200551 if (*l_name == DELETED_FLAG)
552 *l_name = '\0';
553 else if (*l_name == aRING)
554 *l_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000555 downcase(l_name);
556
557 /* Return the real directory entry */
558 memcpy(retdent, realdent, sizeof(dir_entry));
559
560 return 0;
561}
562
wdenk7a428cc2003-06-15 22:40:42 +0000563/* Calculate short name checksum */
Marek Vasut47a470f2012-10-09 07:20:22 +0000564static __u8 mkcksum(const char name[8], const char ext[3])
wdenk7a428cc2003-06-15 22:40:42 +0000565{
566 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200567
wdenk7a428cc2003-06-15 22:40:42 +0000568 __u8 ret = 0;
569
Marek Vasut4ab1f642013-01-11 03:35:48 +0000570 for (i = 0; i < 8; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000571 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
Marek Vasut4ab1f642013-01-11 03:35:48 +0000572 for (i = 0; i < 3; i++)
Marek Vasut47a470f2012-10-09 07:20:22 +0000573 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk7a428cc2003-06-15 22:40:42 +0000574
575 return ret;
576}
wdenk7a428cc2003-06-15 22:40:42 +0000577
578/*
579 * Get the directory entry associated with 'filename' from the directory
580 * starting at 'startsect'
581 */
Eric Nelson014705b2012-04-11 04:08:53 +0000582__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
583 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200584
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200585static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
586 char *filename, dir_entry *retdent,
587 int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000588{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200589 __u16 prevcksum = 0xffff;
590 __u32 curclust = START(retdent);
591 int files = 0, dirs = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000592
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200593 debug("get_dentfromdir: %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +0000594
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200595 while (1) {
596 dir_entry *dentptr;
597
598 int i;
599
600 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000601 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200602 debug("Error: reading directory block\n");
603 return NULL;
604 }
wdenk7a428cc2003-06-15 22:40:42 +0000605
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200606 dentptr = (dir_entry *)get_dentfromdir_block;
607
608 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300609 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200610
611 l_name[0] = '\0';
612 if (dentptr->name[0] == DELETED_FLAG) {
613 dentptr++;
614 continue;
615 }
616 if ((dentptr->attr & ATTR_VOLUME)) {
Richard Genouda43f41f2012-12-13 03:30:10 +0000617 if (vfat_enabled &&
618 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
J. Vijayanand575e21e2011-10-19 07:43:08 +0000619 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200620 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
621 get_vfatname(mydata, curclust,
622 get_dentfromdir_block,
623 dentptr, l_name);
624 if (dols) {
625 int isdir;
626 char dirc;
627 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000628
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200629 isdir = (dentptr->attr & ATTR_DIR);
630
631 if (isdir) {
632 dirs++;
633 dirc = '/';
634 doit = 1;
635 } else {
636 dirc = ' ';
637 if (l_name[0] != 0) {
638 files++;
639 doit = 1;
640 }
641 }
642 if (doit) {
643 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800644 printf(" %8u %s%c\n",
645 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200646 l_name,
647 dirc);
648 } else {
649 printf(" %s%c\n",
650 l_name,
651 dirc);
652 }
653 }
654 dentptr++;
655 continue;
656 }
657 debug("vfatname: |%s|\n", l_name);
Richard Genouda43f41f2012-12-13 03:30:10 +0000658 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200659 /* Volume label or VFAT entry */
660 dentptr++;
661 continue;
662 }
wdenk7a428cc2003-06-15 22:40:42 +0000663 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200664 if (dentptr->name[0] == 0) {
665 if (dols) {
666 printf("\n%d file(s), %d dir(s)\n\n",
667 files, dirs);
668 }
669 debug("Dentname == NULL - %d\n", i);
670 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000671 }
Richard Genouda43f41f2012-12-13 03:30:10 +0000672 if (vfat_enabled) {
673 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
674 if (dols && csum == prevcksum) {
675 prevcksum = 0xffff;
676 dentptr++;
677 continue;
678 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200679 }
Richard Genouda43f41f2012-12-13 03:30:10 +0000680
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200681 get_name(dentptr, s_name);
682 if (dols) {
683 int isdir = (dentptr->attr & ATTR_DIR);
684 char dirc;
685 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000686
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200687 if (isdir) {
688 dirs++;
689 dirc = '/';
690 doit = 1;
691 } else {
692 dirc = ' ';
693 if (s_name[0] != 0) {
694 files++;
695 doit = 1;
696 }
697 }
wdenk7a428cc2003-06-15 22:40:42 +0000698
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200699 if (doit) {
700 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800701 printf(" %8u %s%c\n",
702 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200703 s_name, dirc);
704 } else {
705 printf(" %s%c\n",
706 s_name, dirc);
707 }
708 }
wdenk7a428cc2003-06-15 22:40:42 +0000709
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200710 dentptr++;
711 continue;
712 }
713
714 if (strcmp(filename, s_name)
715 && strcmp(filename, l_name)) {
716 debug("Mismatch: |%s|%s|\n", s_name, l_name);
717 dentptr++;
718 continue;
719 }
720
721 memcpy(retdent, dentptr, sizeof(dir_entry));
722
723 debug("DentName: %s", s_name);
724 debug(", start: 0x%x", START(dentptr));
725 debug(", size: 0x%x %s\n",
726 FAT2CPU32(dentptr->size),
727 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
728
729 return retdent;
730 }
731
732 curclust = get_fatent(mydata, curclust);
733 if (CHECK_CLUST(curclust, mydata->fatsize)) {
734 debug("curclust: 0x%x\n", curclust);
735 printf("Invalid FAT entry\n");
736 return NULL;
737 }
wdenk7a428cc2003-06-15 22:40:42 +0000738 }
wdenk7a428cc2003-06-15 22:40:42 +0000739
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200740 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000741}
742
wdenk7a428cc2003-06-15 22:40:42 +0000743/*
744 * Read boot sector and volume info from a FAT filesystem
745 */
746static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200747read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000748{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000749 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000750 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000751 int ret = 0;
752
753 if (cur_dev == NULL) {
754 debug("Error: no device selected\n");
755 return -1;
756 }
757
Eric Nelson014705b2012-04-11 04:08:53 +0000758 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000759 if (block == NULL) {
760 debug("Error: allocating block\n");
761 return -1;
762 }
wdenk7a428cc2003-06-15 22:40:42 +0000763
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200764 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200765 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000766 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000767 }
768
769 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200770 bs->reserved = FAT2CPU16(bs->reserved);
771 bs->fat_length = FAT2CPU16(bs->fat_length);
772 bs->secs_track = FAT2CPU16(bs->secs_track);
773 bs->heads = FAT2CPU16(bs->heads);
774 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000775
776 /* FAT32 entries */
777 if (bs->fat_length == 0) {
778 /* Assume FAT32 */
779 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200780 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000781 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200782 bs->info_sector = FAT2CPU16(bs->info_sector);
783 bs->backup_boot = FAT2CPU16(bs->backup_boot);
784 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000785 *fatsize = 32;
786 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200787 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000788 *fatsize = 0;
789 }
790 memcpy(volinfo, vistart, sizeof(volume_info));
791
wdenk7a428cc2003-06-15 22:40:42 +0000792 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200793 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000794 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000795 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500796 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000797 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000798 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000799 }
Tom Rix155abaa2009-05-20 07:55:41 -0500800 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000801 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000802 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000803 }
804 }
805
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200806 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000807fail:
808 ret = -1;
809exit:
810 free(block);
811 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000812}
813
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000814__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
Eric Nelson014705b2012-04-11 04:08:53 +0000815 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200816
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800817int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
818 loff_t maxsize, int dols, int dogetsize, loff_t *size)
wdenk7a428cc2003-06-15 22:40:42 +0000819{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200820 char fnamecopy[2048];
821 boot_sector bs;
822 volume_info volinfo;
823 fsdata datablock;
824 fsdata *mydata = &datablock;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200825 dir_entry *dentptr = NULL;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200826 __u16 prevcksum = 0xffff;
827 char *subname = "";
Erik Hansen4ea89662011-03-24 10:15:37 +0100828 __u32 cursect;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200829 int idx, isdir = 0;
830 int files = 0, dirs = 0;
Suriyan Ramasami441c2232014-11-17 14:39:35 -0800831 int ret = -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200832 int firsttime;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000833 __u32 root_cluster = 0;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100834 __u32 read_blk;
Erik Hansen4ea89662011-03-24 10:15:37 +0100835 int rootdir_size = 0;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100836 int buffer_blk_cnt;
837 int do_read;
838 __u8 *dir_ptr;
wdenk7a428cc2003-06-15 22:40:42 +0000839
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200840 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
841 debug("Error: reading boot sector\n");
842 return -1;
843 }
844
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000845 if (mydata->fatsize == 32) {
846 root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200847 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000848 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200849 mydata->fatlength = bs.fat_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000850 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200851
852 mydata->fat_sect = bs.reserved;
853
854 cursect = mydata->rootdir_sect
855 = mydata->fat_sect + mydata->fatlength * bs.fats;
856
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000857 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200858 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000859 if (mydata->sect_size != cur_part_info.blksz) {
860 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
861 mydata->sect_size, cur_part_info.blksz);
862 return -1;
863 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200864
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200865 if (mydata->fatsize == 32) {
866 mydata->data_begin = mydata->rootdir_sect -
867 (mydata->clust_size * 2);
868 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200869 rootdir_size = ((bs.dir_entries[1] * (int)256 +
870 bs.dir_entries[0]) *
871 sizeof(dir_entry)) /
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000872 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200873 mydata->data_begin = mydata->rootdir_sect +
874 rootdir_size -
875 (mydata->clust_size * 2);
876 }
877
878 mydata->fatbufnum = -1;
Eric Nelson014705b2012-04-11 04:08:53 +0000879 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000880 if (mydata->fatbuf == NULL) {
881 debug("Error: allocating memory\n");
882 return -1;
883 }
wdenk7a428cc2003-06-15 22:40:42 +0000884
Richard Genouda43f41f2012-12-13 03:30:10 +0000885 if (vfat_enabled)
886 debug("VFAT Support enabled\n");
887
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200888 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
889 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
890 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
891 "Data begins at: %d\n",
892 root_cluster,
893 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000894 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
895 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
896 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000897
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200898 /* "cwd" is always the root... */
899 while (ISDIRDELIM(*filename))
900 filename++;
wdenk7a428cc2003-06-15 22:40:42 +0000901
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200902 /* Make a copy of the filename and convert it to lowercase */
903 strcpy(fnamecopy, filename);
904 downcase(fnamecopy);
wdenk7a428cc2003-06-15 22:40:42 +0000905
Stephen Warren8bdf9612015-07-28 21:55:03 -0600906root_reparse:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200907 if (*fnamecopy == '\0') {
908 if (!dols)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000909 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200910
911 dols = LS_ROOT;
912 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
913 isdir = 1;
914 fnamecopy[idx] = '\0';
915 subname = fnamecopy + idx + 1;
916
917 /* Handle multiple delimiters */
918 while (ISDIRDELIM(*subname))
919 subname++;
920 } else if (dols) {
921 isdir = 1;
wdenk7a428cc2003-06-15 22:40:42 +0000922 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200923
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100924 buffer_blk_cnt = 0;
925 firsttime = 1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200926 while (1) {
927 int i;
928
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100929 if (mydata->fatsize == 32 || firsttime) {
930 dir_ptr = do_fat_read_at_block;
931 firsttime = 0;
932 } else {
933 /**
934 * FAT16 sector buffer modification:
935 * Each loop, the second buffered block is moved to
936 * the buffer begin, and two next sectors are read
937 * next to the previously moved one. So the sector
938 * buffer keeps always 3 sectors for fat16.
939 * And the current sector is the buffer second sector
940 * beside the "firsttime" read, when it is the first one.
941 *
942 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
943 * n = computed root dir sector
944 * loop | cursect-1 | cursect | cursect+1 |
945 * 0 | sector n+0 | sector n+1 | none |
946 * 1 | none | sector n+0 | sector n+1 |
947 * 0 | sector n+1 | sector n+2 | sector n+3 |
948 * 1 | sector n+3 | ...
949 */
950 dir_ptr = (do_fat_read_at_block + mydata->sect_size);
951 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size);
952 }
953
954 do_read = 1;
955
956 if (mydata->fatsize == 32 && buffer_blk_cnt)
957 do_read = 0;
958
959 if (do_read) {
960 read_blk = (mydata->fatsize == 32) ?
961 mydata->clust_size : PREFETCH_BLOCKS;
962
963 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
964 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200965
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100966 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200967 debug("Error: reading rootdir block\n");
968 goto exit;
969 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200970
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100971 dentptr = (dir_entry *)dir_ptr;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200972 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200973
974 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300975 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Marek Vasut47a470f2012-10-09 07:20:22 +0000976 __u8 csum;
wdenk7a428cc2003-06-15 22:40:42 +0000977
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200978 l_name[0] = '\0';
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300979 if (dentptr->name[0] == DELETED_FLAG) {
980 dentptr++;
981 continue;
982 }
Marek Vasut47a470f2012-10-09 07:20:22 +0000983
Richard Genouda43f41f2012-12-13 03:30:10 +0000984 if (vfat_enabled)
985 csum = mkcksum(dentptr->name, dentptr->ext);
986
Marek Vasut47a470f2012-10-09 07:20:22 +0000987 if (dentptr->attr & ATTR_VOLUME) {
Richard Genouda43f41f2012-12-13 03:30:10 +0000988 if (vfat_enabled &&
989 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200990 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
991 prevcksum =
992 ((dir_slot *)dentptr)->alias_checksum;
wdenk7a428cc2003-06-15 22:40:42 +0000993
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300994 get_vfatname(mydata,
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000995 root_cluster,
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +0100996 dir_ptr,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200997 dentptr, l_name);
998
999 if (dols == LS_ROOT) {
1000 char dirc;
1001 int doit = 0;
1002 int isdir =
1003 (dentptr->attr & ATTR_DIR);
1004
1005 if (isdir) {
1006 dirs++;
1007 dirc = '/';
1008 doit = 1;
1009 } else {
1010 dirc = ' ';
1011 if (l_name[0] != 0) {
1012 files++;
1013 doit = 1;
1014 }
1015 }
1016 if (doit) {
1017 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001018 printf(" %8u %s%c\n",
1019 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001020 l_name,
1021 dirc);
1022 } else {
1023 printf(" %s%c\n",
1024 l_name,
1025 dirc);
1026 }
1027 }
1028 dentptr++;
1029 continue;
1030 }
1031 debug("Rootvfatname: |%s|\n",
1032 l_name);
Richard Genouda43f41f2012-12-13 03:30:10 +00001033 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001034 /* Volume label or VFAT entry */
1035 dentptr++;
1036 continue;
1037 }
1038 } else if (dentptr->name[0] == 0) {
1039 debug("RootDentname == NULL - %d\n", i);
1040 if (dols == LS_ROOT) {
1041 printf("\n%d file(s), %d dir(s)\n\n",
1042 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001043 ret = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001044 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001045 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001046 }
Richard Genouda43f41f2012-12-13 03:30:10 +00001047 else if (vfat_enabled &&
1048 dols == LS_ROOT && csum == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +00001049 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001050 dentptr++;
1051 continue;
1052 }
Richard Genouda43f41f2012-12-13 03:30:10 +00001053
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001054 get_name(dentptr, s_name);
wdenk7a428cc2003-06-15 22:40:42 +00001055
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001056 if (dols == LS_ROOT) {
1057 int isdir = (dentptr->attr & ATTR_DIR);
1058 char dirc;
1059 int doit = 0;
1060
1061 if (isdir) {
1062 dirc = '/';
1063 if (s_name[0] != 0) {
1064 dirs++;
1065 doit = 1;
1066 }
1067 } else {
1068 dirc = ' ';
1069 if (s_name[0] != 0) {
1070 files++;
1071 doit = 1;
1072 }
1073 }
1074 if (doit) {
1075 if (dirc == ' ') {
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001076 printf(" %8u %s%c\n",
1077 FAT2CPU32(dentptr->size),
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001078 s_name, dirc);
1079 } else {
1080 printf(" %s%c\n",
1081 s_name, dirc);
1082 }
1083 }
1084 dentptr++;
1085 continue;
1086 }
1087
1088 if (strcmp(fnamecopy, s_name)
1089 && strcmp(fnamecopy, l_name)) {
1090 debug("RootMismatch: |%s|%s|\n", s_name,
1091 l_name);
1092 dentptr++;
1093 continue;
1094 }
1095
1096 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001097 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001098
1099 debug("RootName: %s", s_name);
1100 debug(", start: 0x%x", START(dentptr));
1101 debug(", size: 0x%x %s\n",
1102 FAT2CPU32(dentptr->size),
1103 isdir ? "(DIR)" : "");
1104
1105 goto rootdir_done; /* We got a match */
wdenk7a428cc2003-06-15 22:40:42 +00001106 }
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001107 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001108 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +00001109
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001110 /*
1111 * On FAT32 we must fetch the FAT entries for the next
1112 * root directory clusters when a cluster has been
1113 * completely processed.
1114 */
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001115 ++buffer_blk_cnt;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001116 int rootdir_end = 0;
1117 if (mydata->fatsize == 32) {
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001118 if (buffer_blk_cnt == mydata->clust_size) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001119 int nxtsect = 0;
1120 int nxt_clust = 0;
wdenk7a428cc2003-06-15 22:40:42 +00001121
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001122 nxt_clust = get_fatent(mydata, root_cluster);
1123 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001124
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001125 nxtsect = mydata->data_begin +
1126 (nxt_clust * mydata->clust_size);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001127
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001128 root_cluster = nxt_clust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001129
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001130 cursect = nxtsect;
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001131 buffer_blk_cnt = 0;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001132 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001133 } else {
Przemyslaw Marczakcd486d12014-12-18 17:14:17 +01001134 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1135 buffer_blk_cnt = 0;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001136
1137 rootdir_end = (++cursect - mydata->rootdir_sect >=
1138 rootdir_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001139 }
Erik Hansen4ea89662011-03-24 10:15:37 +01001140
1141 /* If end of rootdir reached */
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001142 if (rootdir_end) {
Erik Hansen4ea89662011-03-24 10:15:37 +01001143 if (dols == LS_ROOT) {
1144 printf("\n%d file(s), %d dir(s)\n\n",
1145 files, dirs);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001146 *size = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +01001147 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001148 goto exit;
Erik Hansen4ea89662011-03-24 10:15:37 +01001149 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001150 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001151rootdir_done:
wdenk7a428cc2003-06-15 22:40:42 +00001152
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001153 firsttime = 1;
wdenk7a428cc2003-06-15 22:40:42 +00001154
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001155 while (isdir) {
1156 int startsect = mydata->data_begin
1157 + START(dentptr) * mydata->clust_size;
1158 dir_entry dent;
1159 char *nextname = NULL;
wdenk7a428cc2003-06-15 22:40:42 +00001160
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001161 dent = *dentptr;
1162 dentptr = &dent;
wdenk7a428cc2003-06-15 22:40:42 +00001163
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001164 idx = dirdelim(subname);
wdenk7a428cc2003-06-15 22:40:42 +00001165
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001166 if (idx >= 0) {
1167 subname[idx] = '\0';
1168 nextname = subname + idx + 1;
1169 /* Handle multiple delimiters */
1170 while (ISDIRDELIM(*nextname))
1171 nextname++;
1172 if (dols && *nextname == '\0')
1173 firsttime = 0;
1174 } else {
1175 if (dols && firsttime) {
1176 firsttime = 0;
1177 } else {
1178 isdir = 0;
1179 }
1180 }
1181
1182 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1183 isdir ? 0 : dols) == NULL) {
1184 if (dols && !isdir)
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001185 *size = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001186 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001187 }
1188
Benoît Thébaudeau7ca2a8a2012-07-20 03:20:29 +00001189 if (isdir && !(dentptr->attr & ATTR_DIR))
1190 goto exit;
1191
Stephen Warren8bdf9612015-07-28 21:55:03 -06001192 /*
1193 * If we are looking for a directory, and found a directory
1194 * type entry, and the entry is for the root directory (as
1195 * denoted by a cluster number of 0), jump back to the start
1196 * of the function, since at least on FAT12/16, the root dir
1197 * lives in a hard-coded location and needs special handling
1198 * to parse, rather than simply following the cluster linked
1199 * list in the FAT, like other directories.
1200 */
1201 if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) {
1202 /*
1203 * Modify the filename to remove the prefix that gets
1204 * back to the root directory, so the initial root dir
1205 * parsing code can continue from where we are without
1206 * confusion.
1207 */
1208 strcpy(fnamecopy, nextname ?: "");
1209 /*
1210 * Set up state the same way as the function does when
1211 * first started. This is required for the root dir
1212 * parsing code operates in its expected environment.
1213 */
1214 subname = "";
1215 cursect = mydata->rootdir_sect;
1216 isdir = 0;
1217 goto root_reparse;
1218 }
1219
Benoît Thébaudeau7ca2a8a2012-07-20 03:20:29 +00001220 if (idx >= 0)
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001221 subname = nextname;
wdenk7a428cc2003-06-15 22:40:42 +00001222 }
wdenk7a428cc2003-06-15 22:40:42 +00001223
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001224 if (dogetsize) {
1225 *size = FAT2CPU32(dentptr->size);
1226 ret = 0;
1227 } else {
1228 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
1229 }
1230 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
wdenk7a428cc2003-06-15 22:40:42 +00001231
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001232exit:
1233 free(mydata->fatbuf);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001234 return ret;
1235}
wdenk7a428cc2003-06-15 22:40:42 +00001236
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001237int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
1238 loff_t *actread)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001239{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001240 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001241}
1242
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001243int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001244{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001245 boot_sector bs;
1246 volume_info volinfo;
1247 int fatsize;
1248 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001249
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001250 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001251 printf("No current device\n");
1252 return 1;
1253 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001254
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001255#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang853208e2008-12-09 23:20:18 -05001256 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001257 defined(CONFIG_CMD_SCSI) || \
1258 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001259 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001260 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001261 switch (cur_dev->if_type) {
1262 case IF_TYPE_IDE:
1263 printf("IDE");
1264 break;
1265 case IF_TYPE_SATA:
1266 printf("SATA");
1267 break;
1268 case IF_TYPE_SCSI:
1269 printf("SCSI");
1270 break;
1271 case IF_TYPE_ATAPI:
1272 printf("ATAPI");
1273 break;
1274 case IF_TYPE_USB:
1275 printf("USB");
1276 break;
1277 case IF_TYPE_DOC:
1278 printf("DOC");
1279 break;
1280 case IF_TYPE_MMC:
1281 printf("MMC");
1282 break;
1283 default:
1284 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001285 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001286
Simon Glass2f26fff2016-02-29 15:25:51 -07001287 printf("\n Device %d: ", cur_dev->devnum);
wdenk2c9b05d2003-09-10 22:30:53 +00001288 dev_print(cur_dev);
1289#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001290
1291 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001292 printf("\nNo valid FAT fs found\n");
1293 return 1;
1294 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001295
1296 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001297 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001298 volinfo.fs_type[5] = '\0';
1299
Stephen Warrend398dbe2012-10-17 06:44:57 +00001300 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001301
wdenk2c9b05d2003-09-10 22:30:53 +00001302 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001303}
1304
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001305int file_fat_ls(const char *dir)
wdenk7a428cc2003-06-15 22:40:42 +00001306{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001307 loff_t size;
1308
1309 return do_fat_read(dir, NULL, 0, LS_YES, &size);
wdenk7a428cc2003-06-15 22:40:42 +00001310}
1311
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001312int fat_exists(const char *filename)
1313{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001314 int ret;
1315 loff_t size;
1316
1317 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1318 return ret == 0;
Stephen Warren1d2f9a02014-02-03 13:21:10 -07001319}
1320
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001321int fat_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -06001322{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001323 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -06001324}
1325
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001326int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1327 loff_t maxsize, loff_t *actread)
wdenk7a428cc2003-06-15 22:40:42 +00001328{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001329 printf("reading %s\n", filename);
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001330 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1331 actread);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001332}
1333
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001334int file_fat_read(const char *filename, void *buffer, int maxsize)
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001335{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001336 loff_t actread;
1337 int ret;
1338
1339 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1340 if (ret)
1341 return ret;
1342 else
1343 return actread;
wdenk7a428cc2003-06-15 22:40:42 +00001344}
Simon Glass19e38582012-12-26 09:53:33 +00001345
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001346int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1347 loff_t *actread)
Simon Glass19e38582012-12-26 09:53:33 +00001348{
Suriyan Ramasami441c2232014-11-17 14:39:35 -08001349 int ret;
Simon Glass19e38582012-12-26 09:53:33 +00001350
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001351 ret = file_fat_read_at(filename, offset, buf, len, actread);
1352 if (ret)
Simon Glass19e38582012-12-26 09:53:33 +00001353 printf("** Unable to read file %s **\n", filename);
Simon Glass19e38582012-12-26 09:53:33 +00001354
Suriyan Ramasami96171fb2014-11-17 14:39:38 -08001355 return ret;
Simon Glass19e38582012-12-26 09:53:33 +00001356}
1357
1358void fat_close(void)
1359{
1360}