blob: 31042e535787e059536bf16522799c7307096774 [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 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
29#include <config.h>
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +000030#include <exports.h>
wdenk7a428cc2003-06-15 22:40:42 +000031#include <fat.h>
32#include <asm/byteorder.h>
wdenk2c9b05d2003-09-10 22:30:53 +000033#include <part.h>
Eric Nelson014705b2012-04-11 04:08:53 +000034#include <malloc.h>
35#include <linux/compiler.h>
wdenk7a428cc2003-06-15 22:40:42 +000036
wdenk7a428cc2003-06-15 22:40:42 +000037/*
38 * Convert a string to lowercase.
39 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +020040static void downcase(char *str)
wdenk7a428cc2003-06-15 22:40:42 +000041{
42 while (*str != '\0') {
43 TOLOWER(*str);
44 str++;
45 }
46}
47
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000048static block_dev_desc_t *cur_dev;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000049static disk_partition_t cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020050
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000051#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000052#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020053#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000054
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000055static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000056{
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000057 if (!cur_dev || !cur_dev->block_read)
wdenk2c9b05d2003-09-10 22:30:53 +000058 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020059
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000060 return cur_dev->block_read(cur_dev->dev,
61 cur_part_info.start + block, nr_blocks, buf);
wdenk7a428cc2003-06-15 22:40:42 +000062}
63
Benoît Thébaudeau0a120372012-07-20 15:18:44 +020064int fat_register_device(block_dev_desc_t * dev_desc, int part_no)
wdenk7a428cc2003-06-15 22:40:42 +000065{
Eric Nelson014705b2012-04-11 04:08:53 +000066 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000067
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000068 /* First close any currently found FAT filesystem */
69 cur_dev = NULL;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020070
Jon Loeliger80eb47c2007-07-09 17:56:50 -050071#if (defined(CONFIG_CMD_IDE) || \
Sonic Zhang853208e2008-12-09 23:20:18 -050072 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -050073 defined(CONFIG_CMD_SCSI) || \
74 defined(CONFIG_CMD_USB) || \
Andy Fleming55b86682008-01-09 13:51:32 -060075 defined(CONFIG_MMC) || \
76 defined(CONFIG_SYSTEMACE) )
Andy Fleming55b86682008-01-09 13:51:32 -060077
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000078 /* Read the partition table, if present */
Stephen Warrend398dbe2012-10-17 06:44:57 +000079 if (!get_partition_info(dev_desc, part_no, &cur_part_info))
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000080 cur_dev = dev_desc;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000081#endif
82
83 /* Otherwise it might be a superfloppy (whole-disk FAT filesystem) */
84 if (!cur_dev) {
Stephen Warren2e1b3572012-10-05 13:17:39 +000085 if (part_no != 0) {
Wolfgang Denk8fff3542011-11-04 15:55:15 +000086 printf("** Partition %d not valid on device %d **\n",
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000087 part_no, dev_desc->dev);
Wolfgang Denk8fff3542011-11-04 15:55:15 +000088 return -1;
89 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000090
91 cur_dev = dev_desc;
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000092 cur_part_info.start = 0;
93 cur_part_info.size = dev_desc->lba;
94 cur_part_info.blksz = dev_desc->blksz;
Stephen Warren63e53b52012-10-17 06:44:58 +000095 cur_part_info.name[0] = 0;
96 cur_part_info.type[0] = 0;
97 cur_part_info.bootable = 0;
98#ifdef CONFIG_PARTITION_UUIDS
99 cur_part_info.uuid[0] = 0;
100#endif
Wolfgang Denk8fff3542011-11-04 15:55:15 +0000101 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000102
103 /* Make sure it has a valid FAT header */
104 if (disk_read(0, 1, buffer) != 1) {
105 cur_dev = NULL;
106 return -1;
Wolfgang Denke78f2582007-08-07 16:02:13 +0200107 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000108
109 /* Check if it's actually a DOS volume */
110 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
111 cur_dev = NULL;
112 return -1;
113 }
114
115 /* Check for FAT12/FAT16/FAT32 filesystem */
116 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
117 return 0;
118 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
119 return 0;
120
121 cur_dev = NULL;
122 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000123}
124
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000125
wdenk7a428cc2003-06-15 22:40:42 +0000126/*
127 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
128 * Return index into string if found, -1 otherwise.
129 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200130static int dirdelim(char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000131{
132 char *start = str;
133
134 while (*str != '\0') {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200135 if (ISDIRDELIM(*str))
136 return str - start;
wdenk7a428cc2003-06-15 22:40:42 +0000137 str++;
138 }
139 return -1;
140}
141
wdenk7a428cc2003-06-15 22:40:42 +0000142/*
143 * Extract zero terminated short name from a directory entry.
144 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200145static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000146{
147 char *ptr;
148
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200149 memcpy(s_name, dirent->name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000150 s_name[8] = '\0';
151 ptr = s_name;
152 while (*ptr && *ptr != ' ')
153 ptr++;
154 if (dirent->ext[0] && dirent->ext[0] != ' ') {
155 *ptr = '.';
156 ptr++;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200157 memcpy(ptr, dirent->ext, 3);
wdenk7a428cc2003-06-15 22:40:42 +0000158 ptr[3] = '\0';
159 while (*ptr && *ptr != ' ')
160 ptr++;
161 }
162 *ptr = '\0';
163 if (*s_name == DELETED_FLAG)
164 *s_name = '\0';
165 else if (*s_name == aRING)
Remy Bohmer7c8f2ce2008-11-27 22:30:27 +0100166 *s_name = DELETED_FLAG;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200167 downcase(s_name);
wdenk7a428cc2003-06-15 22:40:42 +0000168}
169
170/*
171 * Get the entry at index 'entry' in a FAT (12/16/32) table.
172 * On failure 0x00 is returned.
173 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200174static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000175{
176 __u32 bufnum;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200177 __u32 off16, offset;
wdenk7a428cc2003-06-15 22:40:42 +0000178 __u32 ret = 0x00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200179 __u16 val1, val2;
wdenk7a428cc2003-06-15 22:40:42 +0000180
181 switch (mydata->fatsize) {
182 case 32:
183 bufnum = entry / FAT32BUFSIZE;
184 offset = entry - bufnum * FAT32BUFSIZE;
185 break;
186 case 16:
187 bufnum = entry / FAT16BUFSIZE;
188 offset = entry - bufnum * FAT16BUFSIZE;
189 break;
190 case 12:
191 bufnum = entry / FAT12BUFSIZE;
192 offset = entry - bufnum * FAT12BUFSIZE;
193 break;
194
195 default:
196 /* Unsupported FAT size */
197 return ret;
198 }
199
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200200 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
201 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200202
wdenk7a428cc2003-06-15 22:40:42 +0000203 /* Read a new block of FAT entries into the cache. */
204 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000205 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000206 __u8 *bufptr = mydata->fatbuf;
207 __u32 fatlength = mydata->fatlength;
208 __u32 startblock = bufnum * FATBUFBLOCKS;
209
Benoît Thébaudeau808a5fd2012-07-20 15:19:29 +0200210 if (startblock + getsize > fatlength)
211 getsize = fatlength - startblock;
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000212
wdenk7a428cc2003-06-15 22:40:42 +0000213 startblock += mydata->fat_sect; /* Offset from start of disk */
214
wdenk7a428cc2003-06-15 22:40:42 +0000215 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200216 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000217 return ret;
218 }
219 mydata->fatbufnum = bufnum;
220 }
221
222 /* Get the actual entry from the table */
223 switch (mydata->fatsize) {
224 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200225 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000226 break;
227 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200228 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000229 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200230 case 12:
231 off16 = (offset * 3) / 4;
wdenk7a428cc2003-06-15 22:40:42 +0000232
233 switch (offset & 0x3) {
234 case 0:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200235 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000236 ret &= 0xfff;
237 break;
238 case 1:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200239 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000240 val1 &= 0xf000;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200241 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000242 val2 &= 0x00ff;
243 ret = (val2 << 4) | (val1 >> 12);
244 break;
245 case 2:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200246 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000247 val1 &= 0xff00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200248 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000249 val2 &= 0x000f;
250 ret = (val2 << 8) | (val1 >> 8);
251 break;
252 case 3:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200253 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000254 ret = (ret & 0xfff0) >> 4;
255 break;
256 default:
257 break;
258 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200259 break;
wdenk7a428cc2003-06-15 22:40:42 +0000260 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200261 debug("FAT%d: ret: %08x, offset: %04x\n",
262 mydata->fatsize, ret, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000263
264 return ret;
265}
266
wdenk7a428cc2003-06-15 22:40:42 +0000267/*
268 * Read at most 'size' bytes from the specified cluster into 'buffer'.
269 * Return 0 on success, -1 otherwise.
270 */
271static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200272get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000273{
Erik Hansen4ea89662011-03-24 10:15:37 +0100274 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000275 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000276 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000277
278 if (clustnum > 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200279 startsect = mydata->data_begin +
280 clustnum * mydata->clust_size;
wdenk7a428cc2003-06-15 22:40:42 +0000281 } else {
282 startsect = mydata->rootdir_sect;
283 }
284
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200285 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
286
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200287 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson014705b2012-04-11 04:08:53 +0000288 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200289
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200290 printf("FAT: Misaligned buffer address (%p)\n", buffer);
291
292 while (size >= mydata->sect_size) {
293 ret = disk_read(startsect++, 1, tmpbuf);
294 if (ret != 1) {
295 debug("Error reading data (got %d)\n", ret);
296 return -1;
297 }
298
299 memcpy(buffer, tmpbuf, mydata->sect_size);
300 buffer += mydata->sect_size;
301 size -= mydata->sect_size;
302 }
303 } else {
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000304 idx = size / mydata->sect_size;
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200305 ret = disk_read(startsect, idx, buffer);
306 if (ret != idx) {
307 debug("Error reading data (got %d)\n", ret);
308 return -1;
309 }
310 startsect += idx;
311 idx *= mydata->sect_size;
312 buffer += idx;
313 size -= idx;
314 }
315 if (size) {
316 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
317
318 ret = disk_read(startsect, 1, tmpbuf);
Kyle Moffett83c8a782011-12-20 07:41:13 +0000319 if (ret != 1) {
320 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000321 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000322 }
wdenk2c9b05d2003-09-10 22:30:53 +0000323
Benoît Thébaudeaue957c072012-07-20 15:21:08 +0200324 memcpy(buffer, tmpbuf, size);
wdenk7a428cc2003-06-15 22:40:42 +0000325 }
326
327 return 0;
328}
329
wdenk7a428cc2003-06-15 22:40:42 +0000330/*
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000331 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk7a428cc2003-06-15 22:40:42 +0000332 * into 'buffer'.
333 * Return the number of bytes read or -1 on fatal errors.
334 */
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000335__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
336 __aligned(ARCH_DMA_MINALIGN);
337
wdenk7a428cc2003-06-15 22:40:42 +0000338static long
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000339get_contents(fsdata *mydata, dir_entry *dentptr, unsigned long pos,
340 __u8 *buffer, unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +0000341{
342 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000343 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000344 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000345 __u32 endclust, newclust;
346 unsigned long actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000347
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200348 debug("Filesize: %ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000349
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000350 if (pos >= filesize) {
351 debug("Read position past EOF: %lu\n", pos);
352 return gotsize;
353 }
354
355 if (maxsize > 0 && filesize > pos + maxsize)
356 filesize = pos + maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000357
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200358 debug("%ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000359
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200360 actsize = bytesperclust;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000361
362 /* go to cluster at pos */
363 while (actsize <= pos) {
364 curclust = get_fatent(mydata, curclust);
365 if (CHECK_CLUST(curclust, mydata->fatsize)) {
366 debug("curclust: 0x%x\n", curclust);
367 debug("Invalid FAT entry\n");
368 return gotsize;
369 }
370 actsize += bytesperclust;
371 }
372
373 /* actsize > pos */
374 actsize -= bytesperclust;
375 filesize -= actsize;
376 pos -= actsize;
377
378 /* align to beginning of next cluster if any */
379 if (pos) {
380 actsize = min(filesize, bytesperclust);
381 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
382 (int)actsize) != 0) {
383 printf("Error reading cluster\n");
384 return -1;
385 }
386 filesize -= actsize;
387 actsize -= pos;
388 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
389 gotsize += actsize;
390 if (!filesize)
391 return gotsize;
392 buffer += actsize;
393
394 curclust = get_fatent(mydata, curclust);
395 if (CHECK_CLUST(curclust, mydata->fatsize)) {
396 debug("curclust: 0x%x\n", curclust);
397 debug("Invalid FAT entry\n");
398 return gotsize;
399 }
400 }
401
402 actsize = bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200403 endclust = curclust;
404
wdenk7a428cc2003-06-15 22:40:42 +0000405 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000406 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200407 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000408 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200409 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000410 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100411 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200412 debug("curclust: 0x%x\n", newclust);
413 debug("Invalid FAT entry\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000414 return gotsize;
415 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200416 endclust = newclust;
417 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000418 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200419
wdenk2c9b05d2003-09-10 22:30:53 +0000420 /* get remaining bytes */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200421 actsize = filesize;
Benoît Thébaudeau01938d82012-07-20 15:21:37 +0200422 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200423 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000424 return -1;
425 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200426 gotsize += actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000427 return gotsize;
428getit:
429 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200430 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000431 return -1;
432 }
433 gotsize += (int)actsize;
434 filesize -= actsize;
435 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200436
wdenk2c9b05d2003-09-10 22:30:53 +0000437 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100438 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200439 debug("curclust: 0x%x\n", curclust);
440 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000441 return gotsize;
442 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200443 actsize = bytesperclust;
444 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000445 } while (1);
446}
447
wdenk7a428cc2003-06-15 22:40:42 +0000448#ifdef CONFIG_SUPPORT_VFAT
449/*
450 * Extract the file name information from 'slotptr' into 'l_name',
451 * starting at l_name[*idx].
452 * Return 1 if terminator (zero byte) is found, 0 otherwise.
453 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200454static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000455{
456 int j;
457
458 for (j = 0; j <= 8; j += 2) {
459 l_name[*idx] = slotptr->name0_4[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 <= 10; j += 2) {
465 l_name[*idx] = slotptr->name5_10[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 for (j = 0; j <= 2; j += 2) {
471 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200472 if (l_name[*idx] == 0x00)
473 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000474 (*idx)++;
475 }
476
477 return 0;
478}
479
wdenk7a428cc2003-06-15 22:40:42 +0000480/*
481 * Extract the full long filename starting at 'retdent' (which is really
482 * a slot) into 'l_name'. If successful also copy the real directory entry
483 * into 'retdent'
484 * Return 0 on success, -1 otherwise.
485 */
486static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200487get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
488 dir_entry *retdent, char *l_name)
wdenk7a428cc2003-06-15 22:40:42 +0000489{
490 dir_entry *realdent;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200491 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000492 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
493 PREFETCH_BLOCKS :
494 mydata->clust_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200495 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk7a428cc2003-06-15 22:40:42 +0000496 int idx = 0;
497
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300498 if (counter > VFAT_MAXSEQ) {
499 debug("Error: VFAT name is too long\n");
500 return -1;
501 }
502
503 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200504 if (counter == 0)
505 break;
wdenk2ebee312004-02-23 19:30:57 +0000506 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
507 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000508 slotptr++;
509 counter--;
510 }
511
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300512 if ((__u8 *)slotptr >= buflimit) {
wdenk7a428cc2003-06-15 22:40:42 +0000513 dir_slot *slotptr2;
514
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300515 if (curclust == 0)
516 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000517 curclust = get_fatent(mydata, curclust);
michael2c4d2982008-03-02 23:33:46 +0100518 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200519 debug("curclust: 0x%x\n", curclust);
520 printf("Invalid FAT entry\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 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000525 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200526 debug("Error: reading directory block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000527 return -1;
528 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200529
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000530 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300531 while (counter > 0) {
532 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
533 & 0xff) != counter)
534 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000535 slotptr2++;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300536 counter--;
537 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200538
wdenk7a428cc2003-06-15 22:40:42 +0000539 /* Save the real directory entry */
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300540 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000541 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
wdenk7a428cc2003-06-15 22:40:42 +0000542 slotptr2--;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300543 slot2str(slotptr2, l_name, &idx);
wdenk7a428cc2003-06-15 22:40:42 +0000544 }
545 } else {
546 /* Save the real directory entry */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200547 realdent = (dir_entry *)slotptr;
wdenk7a428cc2003-06-15 22:40:42 +0000548 }
549
550 do {
551 slotptr--;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200552 if (slot2str(slotptr, l_name, &idx))
553 break;
wdenk2ebee312004-02-23 19:30:57 +0000554 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk7a428cc2003-06-15 22:40:42 +0000555
556 l_name[idx] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200557 if (*l_name == DELETED_FLAG)
558 *l_name = '\0';
559 else if (*l_name == aRING)
560 *l_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000561 downcase(l_name);
562
563 /* Return the real directory entry */
564 memcpy(retdent, realdent, sizeof(dir_entry));
565
566 return 0;
567}
568
wdenk7a428cc2003-06-15 22:40:42 +0000569/* Calculate short name checksum */
Marek Vasut47a470f2012-10-09 07:20:22 +0000570static __u8 mkcksum(const char name[8], const char ext[3])
wdenk7a428cc2003-06-15 22:40:42 +0000571{
572 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200573
wdenk7a428cc2003-06-15 22:40:42 +0000574 __u8 ret = 0;
575
Marek Vasut47a470f2012-10-09 07:20:22 +0000576 for (i = 0; i < sizeof(name); i++)
577 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
578 for (i = 0; i < sizeof(ext); i++)
579 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
wdenk7a428cc2003-06-15 22:40:42 +0000580
581 return ret;
582}
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200583#endif /* CONFIG_SUPPORT_VFAT */
wdenk7a428cc2003-06-15 22:40:42 +0000584
585/*
586 * Get the directory entry associated with 'filename' from the directory
587 * starting at 'startsect'
588 */
Eric Nelson014705b2012-04-11 04:08:53 +0000589__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
590 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200591
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200592static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
593 char *filename, dir_entry *retdent,
594 int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000595{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200596 __u16 prevcksum = 0xffff;
597 __u32 curclust = START(retdent);
598 int files = 0, dirs = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000599
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200600 debug("get_dentfromdir: %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +0000601
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200602 while (1) {
603 dir_entry *dentptr;
604
605 int i;
606
607 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000608 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200609 debug("Error: reading directory block\n");
610 return NULL;
611 }
wdenk7a428cc2003-06-15 22:40:42 +0000612
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200613 dentptr = (dir_entry *)get_dentfromdir_block;
614
615 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300616 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200617
618 l_name[0] = '\0';
619 if (dentptr->name[0] == DELETED_FLAG) {
620 dentptr++;
621 continue;
622 }
623 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk7a428cc2003-06-15 22:40:42 +0000624#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand575e21e2011-10-19 07:43:08 +0000625 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
626 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200627 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
628 get_vfatname(mydata, curclust,
629 get_dentfromdir_block,
630 dentptr, l_name);
631 if (dols) {
632 int isdir;
633 char dirc;
634 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000635
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200636 isdir = (dentptr->attr & ATTR_DIR);
637
638 if (isdir) {
639 dirs++;
640 dirc = '/';
641 doit = 1;
642 } else {
643 dirc = ' ';
644 if (l_name[0] != 0) {
645 files++;
646 doit = 1;
647 }
648 }
649 if (doit) {
650 if (dirc == ' ') {
651 printf(" %8ld %s%c\n",
652 (long)FAT2CPU32(dentptr->size),
653 l_name,
654 dirc);
655 } else {
656 printf(" %s%c\n",
657 l_name,
658 dirc);
659 }
660 }
661 dentptr++;
662 continue;
663 }
664 debug("vfatname: |%s|\n", l_name);
665 } else
666#endif
667 {
668 /* Volume label or VFAT entry */
669 dentptr++;
670 continue;
671 }
wdenk7a428cc2003-06-15 22:40:42 +0000672 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200673 if (dentptr->name[0] == 0) {
674 if (dols) {
675 printf("\n%d file(s), %d dir(s)\n\n",
676 files, dirs);
677 }
678 debug("Dentname == NULL - %d\n", i);
679 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000680 }
wdenk7a428cc2003-06-15 22:40:42 +0000681#ifdef CONFIG_SUPPORT_VFAT
Marek Vasut47a470f2012-10-09 07:20:22 +0000682 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
683 if (dols && csum == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +0000684 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200685 dentptr++;
686 continue;
687 }
wdenk7a428cc2003-06-15 22:40:42 +0000688#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200689 get_name(dentptr, s_name);
690 if (dols) {
691 int isdir = (dentptr->attr & ATTR_DIR);
692 char dirc;
693 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000694
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200695 if (isdir) {
696 dirs++;
697 dirc = '/';
698 doit = 1;
699 } else {
700 dirc = ' ';
701 if (s_name[0] != 0) {
702 files++;
703 doit = 1;
704 }
705 }
wdenk7a428cc2003-06-15 22:40:42 +0000706
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200707 if (doit) {
708 if (dirc == ' ') {
709 printf(" %8ld %s%c\n",
710 (long)FAT2CPU32(dentptr->size),
711 s_name, dirc);
712 } else {
713 printf(" %s%c\n",
714 s_name, dirc);
715 }
716 }
wdenk7a428cc2003-06-15 22:40:42 +0000717
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200718 dentptr++;
719 continue;
720 }
721
722 if (strcmp(filename, s_name)
723 && strcmp(filename, l_name)) {
724 debug("Mismatch: |%s|%s|\n", s_name, l_name);
725 dentptr++;
726 continue;
727 }
728
729 memcpy(retdent, dentptr, sizeof(dir_entry));
730
731 debug("DentName: %s", s_name);
732 debug(", start: 0x%x", START(dentptr));
733 debug(", size: 0x%x %s\n",
734 FAT2CPU32(dentptr->size),
735 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
736
737 return retdent;
738 }
739
740 curclust = get_fatent(mydata, curclust);
741 if (CHECK_CLUST(curclust, mydata->fatsize)) {
742 debug("curclust: 0x%x\n", curclust);
743 printf("Invalid FAT entry\n");
744 return NULL;
745 }
wdenk7a428cc2003-06-15 22:40:42 +0000746 }
wdenk7a428cc2003-06-15 22:40:42 +0000747
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200748 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000749}
750
wdenk7a428cc2003-06-15 22:40:42 +0000751/*
752 * Read boot sector and volume info from a FAT filesystem
753 */
754static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200755read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000756{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000757 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000758 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000759 int ret = 0;
760
761 if (cur_dev == NULL) {
762 debug("Error: no device selected\n");
763 return -1;
764 }
765
Eric Nelson014705b2012-04-11 04:08:53 +0000766 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000767 if (block == NULL) {
768 debug("Error: allocating block\n");
769 return -1;
770 }
wdenk7a428cc2003-06-15 22:40:42 +0000771
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200772 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200773 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000774 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000775 }
776
777 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200778 bs->reserved = FAT2CPU16(bs->reserved);
779 bs->fat_length = FAT2CPU16(bs->fat_length);
780 bs->secs_track = FAT2CPU16(bs->secs_track);
781 bs->heads = FAT2CPU16(bs->heads);
782 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000783
784 /* FAT32 entries */
785 if (bs->fat_length == 0) {
786 /* Assume FAT32 */
787 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200788 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000789 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200790 bs->info_sector = FAT2CPU16(bs->info_sector);
791 bs->backup_boot = FAT2CPU16(bs->backup_boot);
792 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000793 *fatsize = 32;
794 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200795 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000796 *fatsize = 0;
797 }
798 memcpy(volinfo, vistart, sizeof(volume_info));
799
wdenk7a428cc2003-06-15 22:40:42 +0000800 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200801 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000802 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000803 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500804 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000805 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000806 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000807 }
Tom Rix155abaa2009-05-20 07:55:41 -0500808 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000809 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000810 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000811 }
812 }
813
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200814 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000815fail:
816 ret = -1;
817exit:
818 free(block);
819 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000820}
821
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000822__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
Eric Nelson014705b2012-04-11 04:08:53 +0000823 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200824
stroese83082242004-12-16 17:57:26 +0000825long
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000826do_fat_read_at(const char *filename, unsigned long pos, void *buffer,
827 unsigned long maxsize, int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000828{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200829 char fnamecopy[2048];
830 boot_sector bs;
831 volume_info volinfo;
832 fsdata datablock;
833 fsdata *mydata = &datablock;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200834 dir_entry *dentptr = NULL;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200835 __u16 prevcksum = 0xffff;
836 char *subname = "";
Erik Hansen4ea89662011-03-24 10:15:37 +0100837 __u32 cursect;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200838 int idx, isdir = 0;
839 int files = 0, dirs = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000840 long ret = -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200841 int firsttime;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000842 __u32 root_cluster = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +0100843 int rootdir_size = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200844 int j;
wdenk7a428cc2003-06-15 22:40:42 +0000845
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200846 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
847 debug("Error: reading boot sector\n");
848 return -1;
849 }
850
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000851 if (mydata->fatsize == 32) {
852 root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200853 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000854 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200855 mydata->fatlength = bs.fat_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000856 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200857
858 mydata->fat_sect = bs.reserved;
859
860 cursect = mydata->rootdir_sect
861 = mydata->fat_sect + mydata->fatlength * bs.fats;
862
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000863 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200864 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000865 if (mydata->sect_size != cur_part_info.blksz) {
866 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
867 mydata->sect_size, cur_part_info.blksz);
868 return -1;
869 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200870
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200871 if (mydata->fatsize == 32) {
872 mydata->data_begin = mydata->rootdir_sect -
873 (mydata->clust_size * 2);
874 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200875 rootdir_size = ((bs.dir_entries[1] * (int)256 +
876 bs.dir_entries[0]) *
877 sizeof(dir_entry)) /
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000878 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200879 mydata->data_begin = mydata->rootdir_sect +
880 rootdir_size -
881 (mydata->clust_size * 2);
882 }
883
884 mydata->fatbufnum = -1;
Eric Nelson014705b2012-04-11 04:08:53 +0000885 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000886 if (mydata->fatbuf == NULL) {
887 debug("Error: allocating memory\n");
888 return -1;
889 }
wdenk7a428cc2003-06-15 22:40:42 +0000890
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200891#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200892 debug("VFAT Support enabled\n");
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200893#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200894 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
895 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
896 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
897 "Data begins at: %d\n",
898 root_cluster,
899 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000900 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
901 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
902 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000903
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200904 /* "cwd" is always the root... */
905 while (ISDIRDELIM(*filename))
906 filename++;
wdenk7a428cc2003-06-15 22:40:42 +0000907
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200908 /* Make a copy of the filename and convert it to lowercase */
909 strcpy(fnamecopy, filename);
910 downcase(fnamecopy);
wdenk7a428cc2003-06-15 22:40:42 +0000911
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200912 if (*fnamecopy == '\0') {
913 if (!dols)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000914 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200915
916 dols = LS_ROOT;
917 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
918 isdir = 1;
919 fnamecopy[idx] = '\0';
920 subname = fnamecopy + idx + 1;
921
922 /* Handle multiple delimiters */
923 while (ISDIRDELIM(*subname))
924 subname++;
925 } else if (dols) {
926 isdir = 1;
wdenk7a428cc2003-06-15 22:40:42 +0000927 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200928
929 j = 0;
930 while (1) {
931 int i;
932
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200933 if (j == 0) {
934 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
935 cursect, mydata->clust_size, DIRENTSPERBLOCK);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200936
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200937 if (disk_read(cursect,
938 (mydata->fatsize == 32) ?
939 (mydata->clust_size) :
940 PREFETCH_BLOCKS,
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000941 do_fat_read_at_block) < 0) {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200942 debug("Error: reading rootdir block\n");
943 goto exit;
944 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200945
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000946 dentptr = (dir_entry *) do_fat_read_at_block;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +0200947 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200948
949 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300950 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Marek Vasut47a470f2012-10-09 07:20:22 +0000951 __u8 csum;
wdenk7a428cc2003-06-15 22:40:42 +0000952
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200953 l_name[0] = '\0';
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300954 if (dentptr->name[0] == DELETED_FLAG) {
955 dentptr++;
956 continue;
957 }
Marek Vasut47a470f2012-10-09 07:20:22 +0000958
959 csum = mkcksum(dentptr->name, dentptr->ext);
960 if (dentptr->attr & ATTR_VOLUME) {
wdenk7a428cc2003-06-15 22:40:42 +0000961#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand575e21e2011-10-19 07:43:08 +0000962 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200963 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
964 prevcksum =
965 ((dir_slot *)dentptr)->alias_checksum;
wdenk7a428cc2003-06-15 22:40:42 +0000966
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300967 get_vfatname(mydata,
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000968 root_cluster,
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +0000969 do_fat_read_at_block,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200970 dentptr, l_name);
971
972 if (dols == LS_ROOT) {
973 char dirc;
974 int doit = 0;
975 int isdir =
976 (dentptr->attr & ATTR_DIR);
977
978 if (isdir) {
979 dirs++;
980 dirc = '/';
981 doit = 1;
982 } else {
983 dirc = ' ';
984 if (l_name[0] != 0) {
985 files++;
986 doit = 1;
987 }
988 }
989 if (doit) {
990 if (dirc == ' ') {
991 printf(" %8ld %s%c\n",
992 (long)FAT2CPU32(dentptr->size),
993 l_name,
994 dirc);
995 } else {
996 printf(" %s%c\n",
997 l_name,
998 dirc);
999 }
1000 }
1001 dentptr++;
1002 continue;
1003 }
1004 debug("Rootvfatname: |%s|\n",
1005 l_name);
1006 } else
wdenk7a428cc2003-06-15 22:40:42 +00001007#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001008 {
1009 /* Volume label or VFAT entry */
1010 dentptr++;
1011 continue;
1012 }
1013 } else if (dentptr->name[0] == 0) {
1014 debug("RootDentname == NULL - %d\n", i);
1015 if (dols == LS_ROOT) {
1016 printf("\n%d file(s), %d dir(s)\n\n",
1017 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001018 ret = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001019 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001020 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001021 }
wdenk7a428cc2003-06-15 22:40:42 +00001022#ifdef CONFIG_SUPPORT_VFAT
Marek Vasut47a470f2012-10-09 07:20:22 +00001023 else if (dols == LS_ROOT && csum == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +00001024 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001025 dentptr++;
1026 continue;
1027 }
wdenk7a428cc2003-06-15 22:40:42 +00001028#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001029 get_name(dentptr, s_name);
wdenk7a428cc2003-06-15 22:40:42 +00001030
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001031 if (dols == LS_ROOT) {
1032 int isdir = (dentptr->attr & ATTR_DIR);
1033 char dirc;
1034 int doit = 0;
1035
1036 if (isdir) {
1037 dirc = '/';
1038 if (s_name[0] != 0) {
1039 dirs++;
1040 doit = 1;
1041 }
1042 } else {
1043 dirc = ' ';
1044 if (s_name[0] != 0) {
1045 files++;
1046 doit = 1;
1047 }
1048 }
1049 if (doit) {
1050 if (dirc == ' ') {
1051 printf(" %8ld %s%c\n",
1052 (long)FAT2CPU32(dentptr->size),
1053 s_name, dirc);
1054 } else {
1055 printf(" %s%c\n",
1056 s_name, dirc);
1057 }
1058 }
1059 dentptr++;
1060 continue;
1061 }
1062
1063 if (strcmp(fnamecopy, s_name)
1064 && strcmp(fnamecopy, l_name)) {
1065 debug("RootMismatch: |%s|%s|\n", s_name,
1066 l_name);
1067 dentptr++;
1068 continue;
1069 }
1070
1071 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001072 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001073
1074 debug("RootName: %s", s_name);
1075 debug(", start: 0x%x", START(dentptr));
1076 debug(", size: 0x%x %s\n",
1077 FAT2CPU32(dentptr->size),
1078 isdir ? "(DIR)" : "");
1079
1080 goto rootdir_done; /* We got a match */
wdenk7a428cc2003-06-15 22:40:42 +00001081 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001082 debug("END LOOP: j=%d clust_size=%d\n", j,
1083 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +00001084
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001085 /*
1086 * On FAT32 we must fetch the FAT entries for the next
1087 * root directory clusters when a cluster has been
1088 * completely processed.
1089 */
Erik Hansen4ea89662011-03-24 10:15:37 +01001090 ++j;
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001091 int rootdir_end = 0;
1092 if (mydata->fatsize == 32) {
1093 if (j == mydata->clust_size) {
1094 int nxtsect = 0;
1095 int nxt_clust = 0;
wdenk7a428cc2003-06-15 22:40:42 +00001096
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001097 nxt_clust = get_fatent(mydata, root_cluster);
1098 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001099
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001100 nxtsect = mydata->data_begin +
1101 (nxt_clust * mydata->clust_size);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001102
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001103 root_cluster = nxt_clust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001104
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001105 cursect = nxtsect;
1106 j = 0;
1107 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001108 } else {
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001109 if (j == PREFETCH_BLOCKS)
1110 j = 0;
1111
1112 rootdir_end = (++cursect - mydata->rootdir_sect >=
1113 rootdir_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001114 }
Erik Hansen4ea89662011-03-24 10:15:37 +01001115
1116 /* If end of rootdir reached */
Benoît Thébaudeau2b89d3e2012-07-20 15:20:12 +02001117 if (rootdir_end) {
Erik Hansen4ea89662011-03-24 10:15:37 +01001118 if (dols == LS_ROOT) {
1119 printf("\n%d file(s), %d dir(s)\n\n",
1120 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001121 ret = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +01001122 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001123 goto exit;
Erik Hansen4ea89662011-03-24 10:15:37 +01001124 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001125 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001126rootdir_done:
wdenk7a428cc2003-06-15 22:40:42 +00001127
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001128 firsttime = 1;
wdenk7a428cc2003-06-15 22:40:42 +00001129
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001130 while (isdir) {
1131 int startsect = mydata->data_begin
1132 + START(dentptr) * mydata->clust_size;
1133 dir_entry dent;
1134 char *nextname = NULL;
wdenk7a428cc2003-06-15 22:40:42 +00001135
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001136 dent = *dentptr;
1137 dentptr = &dent;
wdenk7a428cc2003-06-15 22:40:42 +00001138
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001139 idx = dirdelim(subname);
wdenk7a428cc2003-06-15 22:40:42 +00001140
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001141 if (idx >= 0) {
1142 subname[idx] = '\0';
1143 nextname = subname + idx + 1;
1144 /* Handle multiple delimiters */
1145 while (ISDIRDELIM(*nextname))
1146 nextname++;
1147 if (dols && *nextname == '\0')
1148 firsttime = 0;
1149 } else {
1150 if (dols && firsttime) {
1151 firsttime = 0;
1152 } else {
1153 isdir = 0;
1154 }
1155 }
1156
1157 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1158 isdir ? 0 : dols) == NULL) {
1159 if (dols && !isdir)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001160 ret = 0;
1161 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001162 }
1163
Benoît Thébaudeau7ca2a8a2012-07-20 03:20:29 +00001164 if (isdir && !(dentptr->attr & ATTR_DIR))
1165 goto exit;
1166
1167 if (idx >= 0)
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001168 subname = nextname;
wdenk7a428cc2003-06-15 22:40:42 +00001169 }
wdenk7a428cc2003-06-15 22:40:42 +00001170
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001171 ret = get_contents(mydata, dentptr, pos, buffer, maxsize);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001172 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
wdenk7a428cc2003-06-15 22:40:42 +00001173
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001174exit:
1175 free(mydata->fatbuf);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001176 return ret;
1177}
wdenk7a428cc2003-06-15 22:40:42 +00001178
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001179long
1180do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
1181{
1182 return do_fat_read_at(filename, 0, buffer, maxsize, dols);
1183}
1184
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001185int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001186{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001187 boot_sector bs;
1188 volume_info volinfo;
1189 int fatsize;
1190 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001191
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001192 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001193 printf("No current device\n");
1194 return 1;
1195 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001196
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001197#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang853208e2008-12-09 23:20:18 -05001198 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001199 defined(CONFIG_CMD_SCSI) || \
1200 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001201 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001202 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001203 switch (cur_dev->if_type) {
1204 case IF_TYPE_IDE:
1205 printf("IDE");
1206 break;
1207 case IF_TYPE_SATA:
1208 printf("SATA");
1209 break;
1210 case IF_TYPE_SCSI:
1211 printf("SCSI");
1212 break;
1213 case IF_TYPE_ATAPI:
1214 printf("ATAPI");
1215 break;
1216 case IF_TYPE_USB:
1217 printf("USB");
1218 break;
1219 case IF_TYPE_DOC:
1220 printf("DOC");
1221 break;
1222 case IF_TYPE_MMC:
1223 printf("MMC");
1224 break;
1225 default:
1226 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001227 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001228
1229 printf("\n Device %d: ", cur_dev->dev);
wdenk2c9b05d2003-09-10 22:30:53 +00001230 dev_print(cur_dev);
1231#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001232
1233 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001234 printf("\nNo valid FAT fs found\n");
1235 return 1;
1236 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001237
1238 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001239 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001240 volinfo.fs_type[5] = '\0';
1241
Stephen Warrend398dbe2012-10-17 06:44:57 +00001242 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001243
wdenk2c9b05d2003-09-10 22:30:53 +00001244 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001245}
1246
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001247int file_fat_ls(const char *dir)
wdenk7a428cc2003-06-15 22:40:42 +00001248{
1249 return do_fat_read(dir, NULL, 0, LS_YES);
1250}
1251
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001252long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
1253 unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +00001254{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001255 printf("reading %s\n", filename);
Benoît Thébaudeau5295aa82012-09-18 08:14:56 +00001256 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO);
1257}
1258
1259long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1260{
1261 return file_fat_read_at(filename, 0, buffer, maxsize);
wdenk7a428cc2003-06-15 22:40:42 +00001262}