blob: 80156c815f8e392bc01809cbcc31b87ed3757e9a [file] [log] [blame]
wdenk71f95112003-06-15 22:40:42 +00001/*
2 * fat.c
3 *
4 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
5 *
6 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
7 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
8 *
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 Shtylyovac497772011-08-08 09:38:33 +000030#include <exports.h>
wdenk71f95112003-06-15 22:40:42 +000031#include <fat.h>
32#include <asm/byteorder.h>
wdenk7205e402003-09-10 22:30:53 +000033#include <part.h>
Eric Nelson9a800ac2012-04-11 04:08:53 +000034#include <malloc.h>
35#include <linux/compiler.h>
wdenk71f95112003-06-15 22:40:42 +000036
wdenk71f95112003-06-15 22:40:42 +000037/*
38 * Convert a string to lowercase.
39 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +020040static void downcase(char *str)
wdenk71f95112003-06-15 22:40:42 +000041{
42 while (*str != '\0') {
43 TOLOWER(*str);
44 str++;
45 }
46}
47
Kyle Moffett9813b752011-12-21 07:08:10 +000048static block_dev_desc_t *cur_dev;
49static unsigned int cur_part_nr;
50static disk_partition_t cur_part_info;
Wolfgang Denk7385c282010-07-19 11:37:00 +020051
Kyle Moffett9813b752011-12-21 07:08:10 +000052#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk7205e402003-09-10 22:30:53 +000053#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk66c2d732010-07-19 11:36:57 +020054#define DOS_FS32_TYPE_OFFSET 0x52
wdenk71f95112003-06-15 22:40:42 +000055
Kyle Moffett9813b752011-12-21 07:08:10 +000056static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk71f95112003-06-15 22:40:42 +000057{
Kyle Moffett9813b752011-12-21 07:08:10 +000058 if (!cur_dev || !cur_dev->block_read)
wdenk7205e402003-09-10 22:30:53 +000059 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020060
Kyle Moffett9813b752011-12-21 07:08:10 +000061 return cur_dev->block_read(cur_dev->dev,
62 cur_part_info.start + block, nr_blocks, buf);
wdenk71f95112003-06-15 22:40:42 +000063}
64
Benoît Thébaudeau9795e072012-07-20 15:18:44 +020065int fat_register_device(block_dev_desc_t * dev_desc, int part_no)
wdenk71f95112003-06-15 22:40:42 +000066{
Eric Nelson9a800ac2012-04-11 04:08:53 +000067 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk7205e402003-09-10 22:30:53 +000068
Kyle Moffett9813b752011-12-21 07:08:10 +000069 /* First close any currently found FAT filesystem */
70 cur_dev = NULL;
Wolfgang Denk7385c282010-07-19 11:37:00 +020071
Jon Loeligerdd60d122007-07-09 17:56:50 -050072#if (defined(CONFIG_CMD_IDE) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -050073 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -050074 defined(CONFIG_CMD_SCSI) || \
75 defined(CONFIG_CMD_USB) || \
Andy Fleming02df4a22008-01-09 13:51:32 -060076 defined(CONFIG_MMC) || \
77 defined(CONFIG_SYSTEMACE) )
Andy Fleming02df4a22008-01-09 13:51:32 -060078
Kyle Moffett9813b752011-12-21 07:08:10 +000079 /* Read the partition table, if present */
80 if (!get_partition_info(dev_desc, part_no, &cur_part_info)) {
81 cur_dev = dev_desc;
82 cur_part_nr = part_no;
Andy Fleming02df4a22008-01-09 13:51:32 -060083 }
84#endif
Kyle Moffett9813b752011-12-21 07:08:10 +000085
86 /* Otherwise it might be a superfloppy (whole-disk FAT filesystem) */
87 if (!cur_dev) {
Stephen Warrenbd1a7e32012-10-05 13:17:39 +000088 if (part_no != 0) {
Kyle Moffett9813b752011-12-21 07:08:10 +000089 printf("** Partition %d not valid on device %d **\n",
90 part_no, dev_desc->dev);
91 return -1;
92 }
93
94 cur_dev = dev_desc;
95 cur_part_nr = 1;
96 cur_part_info.start = 0;
97 cur_part_info.size = dev_desc->lba;
98 cur_part_info.blksz = dev_desc->blksz;
99 memset(cur_part_info.name, 0, sizeof(cur_part_info.name));
100 memset(cur_part_info.type, 0, sizeof(cur_part_info.type));
101 }
102
103 /* Make sure it has a valid FAT header */
104 if (disk_read(0, 1, buffer) != 1) {
105 cur_dev = NULL;
106 return -1;
107 }
108
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;
wdenk71f95112003-06-15 22:40:42 +0000123}
124
Kyle Moffett9813b752011-12-21 07:08:10 +0000125
wdenk71f95112003-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ébaudeau9795e072012-07-20 15:18:44 +0200130static int dirdelim(char *str)
wdenk71f95112003-06-15 22:40:42 +0000131{
132 char *start = str;
133
134 while (*str != '\0') {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200135 if (ISDIRDELIM(*str))
136 return str - start;
wdenk71f95112003-06-15 22:40:42 +0000137 str++;
138 }
139 return -1;
140}
141
wdenk71f95112003-06-15 22:40:42 +0000142/*
143 * Extract zero terminated short name from a directory entry.
144 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200145static void get_name(dir_entry *dirent, char *s_name)
wdenk71f95112003-06-15 22:40:42 +0000146{
147 char *ptr;
148
Wolfgang Denk7385c282010-07-19 11:37:00 +0200149 memcpy(s_name, dirent->name, 8);
wdenk71f95112003-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 Denk7385c282010-07-19 11:37:00 +0200157 memcpy(ptr, dirent->ext, 3);
wdenk71f95112003-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 Bohmer3c2c2f42008-11-27 22:30:27 +0100166 *s_name = DELETED_FLAG;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200167 downcase(s_name);
wdenk71f95112003-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ébaudeau9795e072012-07-20 15:18:44 +0200174static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000175{
176 __u32 bufnum;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200177 __u32 off16, offset;
wdenk71f95112003-06-15 22:40:42 +0000178 __u32 ret = 0x00;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200179 __u16 val1, val2;
wdenk71f95112003-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 Denk7385c282010-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 Denk2aa98c62010-07-19 11:36:58 +0200202
wdenk71f95112003-06-15 22:40:42 +0000203 /* Read a new block of FAT entries into the cache. */
204 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000205 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000206 __u8 *bufptr = mydata->fatbuf;
207 __u32 fatlength = mydata->fatlength;
208 __u32 startblock = bufnum * FATBUFBLOCKS;
209
Benoît Thébaudeau8006dd22012-07-20 15:19:29 +0200210 if (startblock + getsize > fatlength)
211 getsize = fatlength - startblock;
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000212
wdenk71f95112003-06-15 22:40:42 +0000213 startblock += mydata->fat_sect; /* Offset from start of disk */
214
wdenk71f95112003-06-15 22:40:42 +0000215 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200216 debug("Error reading FAT blocks\n");
wdenk71f95112003-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 Denk7385c282010-07-19 11:37:00 +0200225 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000226 break;
227 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200228 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000229 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200230 case 12:
231 off16 = (offset * 3) / 4;
wdenk71f95112003-06-15 22:40:42 +0000232
233 switch (offset & 0x3) {
234 case 0:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200235 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000236 ret &= 0xfff;
237 break;
238 case 1:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200239 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000240 val1 &= 0xf000;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200241 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk71f95112003-06-15 22:40:42 +0000242 val2 &= 0x00ff;
243 ret = (val2 << 4) | (val1 >> 12);
244 break;
245 case 2:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200246 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000247 val1 &= 0xff00;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200248 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk71f95112003-06-15 22:40:42 +0000249 val2 &= 0x000f;
250 ret = (val2 << 8) | (val1 >> 8);
251 break;
252 case 3:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200253 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000254 ret = (ret & 0xfff0) >> 4;
255 break;
256 default:
257 break;
258 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200259 break;
wdenk71f95112003-06-15 22:40:42 +0000260 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200261 debug("FAT%d: ret: %08x, offset: %04x\n",
262 mydata->fatsize, ret, offset);
wdenk71f95112003-06-15 22:40:42 +0000263
264 return ret;
265}
266
wdenk71f95112003-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ébaudeau9795e072012-07-20 15:18:44 +0200272get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000273{
Erik Hansen3f270f42011-03-24 10:15:37 +0100274 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000275 __u32 startsect;
Kyle Moffett46236b12011-12-20 07:41:13 +0000276 int ret;
wdenk71f95112003-06-15 22:40:42 +0000277
278 if (clustnum > 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200279 startsect = mydata->data_begin +
280 clustnum * mydata->clust_size;
wdenk71f95112003-06-15 22:40:42 +0000281 } else {
282 startsect = mydata->rootdir_sect;
283 }
284
Wolfgang Denk7385c282010-07-19 11:37:00 +0200285 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
286
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200287 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
Eric Nelson9a800ac2012-04-11 04:08:53 +0000288 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200289
Benoît Thébaudeaucc63b252012-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 Shtylyovac497772011-08-08 09:38:33 +0000304 idx = size / mydata->sect_size;
Benoît Thébaudeaucc63b252012-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 Moffett46236b12011-12-20 07:41:13 +0000319 if (ret != 1) {
320 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000321 return -1;
wdenk71f95112003-06-15 22:40:42 +0000322 }
wdenk7205e402003-09-10 22:30:53 +0000323
Benoît Thébaudeaucc63b252012-07-20 15:21:08 +0200324 memcpy(buffer, tmpbuf, size);
wdenk71f95112003-06-15 22:40:42 +0000325 }
326
327 return 0;
328}
329
wdenk71f95112003-06-15 22:40:42 +0000330/*
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000331 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
wdenk71f95112003-06-15 22:40:42 +0000332 * into 'buffer'.
333 * Return the number of bytes read or -1 on fatal errors.
334 */
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000335__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
336 __aligned(ARCH_DMA_MINALIGN);
337
wdenk71f95112003-06-15 22:40:42 +0000338static long
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000339get_contents(fsdata *mydata, dir_entry *dentptr, unsigned long pos,
340 __u8 *buffer, unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +0000341{
342 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000343 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000344 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000345 __u32 endclust, newclust;
346 unsigned long actsize;
wdenk71f95112003-06-15 22:40:42 +0000347
Wolfgang Denk7385c282010-07-19 11:37:00 +0200348 debug("Filesize: %ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000349
Benoît Thébaudeau1170e632012-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;
wdenk71f95112003-06-15 22:40:42 +0000357
Wolfgang Denk7385c282010-07-19 11:37:00 +0200358 debug("%ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000359
Wolfgang Denk7385c282010-07-19 11:37:00 +0200360 actsize = bytesperclust;
Benoît Thébaudeau1170e632012-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 Denk7385c282010-07-19 11:37:00 +0200403 endclust = curclust;
404
wdenk71f95112003-06-15 22:40:42 +0000405 do {
wdenk7205e402003-09-10 22:30:53 +0000406 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200407 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000408 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200409 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000410 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100411 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200412 debug("curclust: 0x%x\n", newclust);
413 debug("Invalid FAT entry\n");
wdenk7205e402003-09-10 22:30:53 +0000414 return gotsize;
415 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200416 endclust = newclust;
417 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000418 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200419
wdenk7205e402003-09-10 22:30:53 +0000420 /* get remaining bytes */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200421 actsize = filesize;
Benoît Thébaudeau0880e5b2012-07-20 15:21:37 +0200422 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200423 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000424 return -1;
425 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200426 gotsize += actsize;
wdenk7205e402003-09-10 22:30:53 +0000427 return gotsize;
428getit:
429 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200430 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000431 return -1;
432 }
433 gotsize += (int)actsize;
434 filesize -= actsize;
435 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200436
wdenk7205e402003-09-10 22:30:53 +0000437 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100438 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200439 debug("curclust: 0x%x\n", curclust);
440 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000441 return gotsize;
442 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200443 actsize = bytesperclust;
444 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000445 } while (1);
446}
447
wdenk71f95112003-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ébaudeau9795e072012-07-20 15:18:44 +0200454static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-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 Denk7385c282010-07-19 11:37:00 +0200460 if (l_name[*idx] == 0x00)
461 return 1;
wdenk71f95112003-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 Denk7385c282010-07-19 11:37:00 +0200466 if (l_name[*idx] == 0x00)
467 return 1;
wdenk71f95112003-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 Denk7385c282010-07-19 11:37:00 +0200472 if (l_name[*idx] == 0x00)
473 return 1;
wdenk71f95112003-06-15 22:40:42 +0000474 (*idx)++;
475 }
476
477 return 0;
478}
479
wdenk71f95112003-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ébaudeau9795e072012-07-20 15:18:44 +0200487get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
488 dir_entry *retdent, char *l_name)
wdenk71f95112003-06-15 22:40:42 +0000489{
490 dir_entry *realdent;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200491 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000492 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
493 PREFETCH_BLOCKS :
494 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200495 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000496 int idx = 0;
497
Mikhail Zolotaryov38315302010-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 Denk7385c282010-07-19 11:37:00 +0200504 if (counter == 0)
505 break;
wdenk2d1a5372004-02-23 19:30:57 +0000506 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
507 return -1;
wdenk71f95112003-06-15 22:40:42 +0000508 slotptr++;
509 counter--;
510 }
511
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300512 if ((__u8 *)slotptr >= buflimit) {
wdenk71f95112003-06-15 22:40:42 +0000513 dir_slot *slotptr2;
514
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300515 if (curclust == 0)
516 return -1;
wdenk71f95112003-06-15 22:40:42 +0000517 curclust = get_fatent(mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100518 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200519 debug("curclust: 0x%x\n", curclust);
520 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000521 return -1;
522 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200523
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000524 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000525 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200526 debug("Error: reading directory block\n");
wdenk71f95112003-06-15 22:40:42 +0000527 return -1;
528 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200529
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000530 slotptr2 = (dir_slot *)get_contents_vfatname_block;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300531 while (counter > 0) {
532 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
533 & 0xff) != counter)
534 return -1;
wdenk71f95112003-06-15 22:40:42 +0000535 slotptr2++;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300536 counter--;
537 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200538
wdenk71f95112003-06-15 22:40:42 +0000539 /* Save the real directory entry */
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300540 realdent = (dir_entry *)slotptr2;
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000541 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000542 slotptr2--;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300543 slot2str(slotptr2, l_name, &idx);
wdenk71f95112003-06-15 22:40:42 +0000544 }
545 } else {
546 /* Save the real directory entry */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200547 realdent = (dir_entry *)slotptr;
wdenk71f95112003-06-15 22:40:42 +0000548 }
549
550 do {
551 slotptr--;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200552 if (slot2str(slotptr, l_name, &idx))
553 break;
wdenk2d1a5372004-02-23 19:30:57 +0000554 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000555
556 l_name[idx] = '\0';
Wolfgang Denk7385c282010-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;
wdenk71f95112003-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
wdenk71f95112003-06-15 22:40:42 +0000569/* Calculate short name checksum */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200570static __u8 mkcksum(const char *str)
wdenk71f95112003-06-15 22:40:42 +0000571{
572 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200573
wdenk71f95112003-06-15 22:40:42 +0000574 __u8 ret = 0;
575
576 for (i = 0; i < 11; i++) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200577 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
wdenk71f95112003-06-15 22:40:42 +0000578 }
579
580 return ret;
581}
Wolfgang Denk7385c282010-07-19 11:37:00 +0200582#endif /* CONFIG_SUPPORT_VFAT */
wdenk71f95112003-06-15 22:40:42 +0000583
584/*
585 * Get the directory entry associated with 'filename' from the directory
586 * starting at 'startsect'
587 */
Eric Nelson9a800ac2012-04-11 04:08:53 +0000588__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
589 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200590
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200591static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
592 char *filename, dir_entry *retdent,
593 int dols)
wdenk71f95112003-06-15 22:40:42 +0000594{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200595 __u16 prevcksum = 0xffff;
596 __u32 curclust = START(retdent);
597 int files = 0, dirs = 0;
wdenk71f95112003-06-15 22:40:42 +0000598
Wolfgang Denk7385c282010-07-19 11:37:00 +0200599 debug("get_dentfromdir: %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +0000600
Wolfgang Denk7385c282010-07-19 11:37:00 +0200601 while (1) {
602 dir_entry *dentptr;
wdenk71f95112003-06-15 22:40:42 +0000603
Wolfgang Denk7385c282010-07-19 11:37:00 +0200604 int i;
wdenk71f95112003-06-15 22:40:42 +0000605
Wolfgang Denk7385c282010-07-19 11:37:00 +0200606 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000607 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200608 debug("Error: reading directory block\n");
609 return NULL;
610 }
611
612 dentptr = (dir_entry *)get_dentfromdir_block;
613
614 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300615 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200616
617 l_name[0] = '\0';
618 if (dentptr->name[0] == DELETED_FLAG) {
619 dentptr++;
620 continue;
wdenk71f95112003-06-15 22:40:42 +0000621 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200622 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000623#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand206d68f2011-10-19 07:43:08 +0000624 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
625 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200626 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
627 get_vfatname(mydata, curclust,
628 get_dentfromdir_block,
629 dentptr, l_name);
630 if (dols) {
631 int isdir;
632 char dirc;
633 int doit = 0;
634
635 isdir = (dentptr->attr & ATTR_DIR);
636
637 if (isdir) {
638 dirs++;
639 dirc = '/';
640 doit = 1;
641 } else {
642 dirc = ' ';
643 if (l_name[0] != 0) {
644 files++;
645 doit = 1;
646 }
647 }
648 if (doit) {
649 if (dirc == ' ') {
650 printf(" %8ld %s%c\n",
651 (long)FAT2CPU32(dentptr->size),
652 l_name,
653 dirc);
654 } else {
655 printf(" %s%c\n",
656 l_name,
657 dirc);
658 }
659 }
660 dentptr++;
661 continue;
662 }
663 debug("vfatname: |%s|\n", l_name);
664 } else
wdenk71f95112003-06-15 22:40:42 +0000665#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200666 {
667 /* Volume label or VFAT entry */
668 dentptr++;
669 continue;
670 }
671 }
672 if (dentptr->name[0] == 0) {
673 if (dols) {
674 printf("\n%d file(s), %d dir(s)\n\n",
675 files, dirs);
676 }
677 debug("Dentname == NULL - %d\n", i);
678 return NULL;
679 }
680#ifdef CONFIG_SUPPORT_VFAT
681 if (dols && mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +0000682 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200683 dentptr++;
684 continue;
685 }
686#endif
687 get_name(dentptr, s_name);
688 if (dols) {
689 int isdir = (dentptr->attr & ATTR_DIR);
690 char dirc;
691 int doit = 0;
wdenk71f95112003-06-15 22:40:42 +0000692
Wolfgang Denk7385c282010-07-19 11:37:00 +0200693 if (isdir) {
694 dirs++;
695 dirc = '/';
696 doit = 1;
697 } else {
698 dirc = ' ';
699 if (s_name[0] != 0) {
700 files++;
701 doit = 1;
702 }
703 }
704
705 if (doit) {
706 if (dirc == ' ') {
707 printf(" %8ld %s%c\n",
708 (long)FAT2CPU32(dentptr->size),
709 s_name, dirc);
710 } else {
711 printf(" %s%c\n",
712 s_name, dirc);
713 }
714 }
715
716 dentptr++;
717 continue;
718 }
719
720 if (strcmp(filename, s_name)
721 && strcmp(filename, l_name)) {
722 debug("Mismatch: |%s|%s|\n", s_name, l_name);
723 dentptr++;
724 continue;
725 }
726
727 memcpy(retdent, dentptr, sizeof(dir_entry));
728
729 debug("DentName: %s", s_name);
730 debug(", start: 0x%x", START(dentptr));
731 debug(", size: 0x%x %s\n",
732 FAT2CPU32(dentptr->size),
733 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
734
735 return retdent;
wdenk71f95112003-06-15 22:40:42 +0000736 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200737
738 curclust = get_fatent(mydata, curclust);
739 if (CHECK_CLUST(curclust, mydata->fatsize)) {
740 debug("curclust: 0x%x\n", curclust);
741 printf("Invalid FAT entry\n");
742 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000743 }
wdenk71f95112003-06-15 22:40:42 +0000744 }
wdenk71f95112003-06-15 22:40:42 +0000745
Wolfgang Denk7385c282010-07-19 11:37:00 +0200746 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000747}
748
wdenk71f95112003-06-15 22:40:42 +0000749/*
750 * Read boot sector and volume info from a FAT filesystem
751 */
752static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200753read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000754{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000755 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000756 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000757 int ret = 0;
758
759 if (cur_dev == NULL) {
760 debug("Error: no device selected\n");
761 return -1;
762 }
763
Eric Nelson9a800ac2012-04-11 04:08:53 +0000764 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000765 if (block == NULL) {
766 debug("Error: allocating block\n");
767 return -1;
768 }
wdenk71f95112003-06-15 22:40:42 +0000769
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200770 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200771 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000772 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000773 }
774
775 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200776 bs->reserved = FAT2CPU16(bs->reserved);
777 bs->fat_length = FAT2CPU16(bs->fat_length);
778 bs->secs_track = FAT2CPU16(bs->secs_track);
779 bs->heads = FAT2CPU16(bs->heads);
780 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000781
782 /* FAT32 entries */
783 if (bs->fat_length == 0) {
784 /* Assume FAT32 */
785 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200786 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000787 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200788 bs->info_sector = FAT2CPU16(bs->info_sector);
789 bs->backup_boot = FAT2CPU16(bs->backup_boot);
790 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000791 *fatsize = 32;
792 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200793 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000794 *fatsize = 0;
795 }
796 memcpy(volinfo, vistart, sizeof(volume_info));
797
wdenk71f95112003-06-15 22:40:42 +0000798 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200799 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000800 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000801 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500802 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000803 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000804 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000805 }
Tom Rix651351f2009-05-20 07:55:41 -0500806 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000807 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000808 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000809 }
810 }
811
Wolfgang Denk7385c282010-07-19 11:37:00 +0200812 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000813fail:
814 ret = -1;
815exit:
816 free(block);
817 return ret;
wdenk71f95112003-06-15 22:40:42 +0000818}
819
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000820__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
Eric Nelson9a800ac2012-04-11 04:08:53 +0000821 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200822
stroese20cc00d2004-12-16 17:57:26 +0000823long
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000824do_fat_read_at(const char *filename, unsigned long pos, void *buffer,
825 unsigned long maxsize, int dols)
wdenk71f95112003-06-15 22:40:42 +0000826{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200827 char fnamecopy[2048];
828 boot_sector bs;
829 volume_info volinfo;
830 fsdata datablock;
831 fsdata *mydata = &datablock;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200832 dir_entry *dentptr = NULL;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200833 __u16 prevcksum = 0xffff;
834 char *subname = "";
Erik Hansen3f270f42011-03-24 10:15:37 +0100835 __u32 cursect;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200836 int idx, isdir = 0;
837 int files = 0, dirs = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000838 long ret = -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200839 int firsttime;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000840 __u32 root_cluster = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +0100841 int rootdir_size = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200842 int j;
wdenk71f95112003-06-15 22:40:42 +0000843
Wolfgang Denk7385c282010-07-19 11:37:00 +0200844 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
845 debug("Error: reading boot sector\n");
846 return -1;
847 }
848
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000849 if (mydata->fatsize == 32) {
850 root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200851 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000852 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200853 mydata->fatlength = bs.fat_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000854 }
wdenk71f95112003-06-15 22:40:42 +0000855
Wolfgang Denk7385c282010-07-19 11:37:00 +0200856 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000857
Wolfgang Denk7385c282010-07-19 11:37:00 +0200858 cursect = mydata->rootdir_sect
859 = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000860
Sergei Shtylyovac497772011-08-08 09:38:33 +0000861 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200862 mydata->clust_size = bs.cluster_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000863 if (mydata->sect_size != cur_part_info.blksz) {
864 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
865 mydata->sect_size, cur_part_info.blksz);
866 return -1;
867 }
wdenk71f95112003-06-15 22:40:42 +0000868
Wolfgang Denk7385c282010-07-19 11:37:00 +0200869 if (mydata->fatsize == 32) {
870 mydata->data_begin = mydata->rootdir_sect -
871 (mydata->clust_size * 2);
872 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200873 rootdir_size = ((bs.dir_entries[1] * (int)256 +
874 bs.dir_entries[0]) *
875 sizeof(dir_entry)) /
Sergei Shtylyovac497772011-08-08 09:38:33 +0000876 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200877 mydata->data_begin = mydata->rootdir_sect +
878 rootdir_size -
879 (mydata->clust_size * 2);
wdenk71f95112003-06-15 22:40:42 +0000880 }
wdenk71f95112003-06-15 22:40:42 +0000881
Wolfgang Denk7385c282010-07-19 11:37:00 +0200882 mydata->fatbufnum = -1;
Eric Nelson9a800ac2012-04-11 04:08:53 +0000883 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000884 if (mydata->fatbuf == NULL) {
885 debug("Error: allocating memory\n");
886 return -1;
887 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200888
wdenk71f95112003-06-15 22:40:42 +0000889#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk7385c282010-07-19 11:37:00 +0200890 debug("VFAT Support enabled\n");
wdenk71f95112003-06-15 22:40:42 +0000891#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200892 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
893 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
894 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
895 "Data begins at: %d\n",
896 root_cluster,
897 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000898 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
899 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
900 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200901
902 /* "cwd" is always the root... */
903 while (ISDIRDELIM(*filename))
904 filename++;
905
906 /* Make a copy of the filename and convert it to lowercase */
907 strcpy(fnamecopy, filename);
908 downcase(fnamecopy);
909
910 if (*fnamecopy == '\0') {
911 if (!dols)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000912 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200913
914 dols = LS_ROOT;
915 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
916 isdir = 1;
917 fnamecopy[idx] = '\0';
918 subname = fnamecopy + idx + 1;
919
920 /* Handle multiple delimiters */
921 while (ISDIRDELIM(*subname))
922 subname++;
923 } else if (dols) {
924 isdir = 1;
925 }
926
927 j = 0;
928 while (1) {
929 int i;
930
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200931 if (j == 0) {
932 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
933 cursect, mydata->clust_size, DIRENTSPERBLOCK);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200934
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200935 if (disk_read(cursect,
936 (mydata->fatsize == 32) ?
937 (mydata->clust_size) :
938 PREFETCH_BLOCKS,
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000939 do_fat_read_at_block) < 0) {
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200940 debug("Error: reading rootdir block\n");
941 goto exit;
942 }
943
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000944 dentptr = (dir_entry *) do_fat_read_at_block;
wdenk71f95112003-06-15 22:40:42 +0000945 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200946
Wolfgang Denk7385c282010-07-19 11:37:00 +0200947 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300948 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200949
950 l_name[0] = '\0';
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300951 if (dentptr->name[0] == DELETED_FLAG) {
952 dentptr++;
953 continue;
954 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200955 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000956#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand206d68f2011-10-19 07:43:08 +0000957 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk7385c282010-07-19 11:37:00 +0200958 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
959 prevcksum =
960 ((dir_slot *)dentptr)->alias_checksum;
wdenk71f95112003-06-15 22:40:42 +0000961
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300962 get_vfatname(mydata,
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000963 root_cluster,
Benoît Thébaudeau1170e632012-09-18 08:14:56 +0000964 do_fat_read_at_block,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200965 dentptr, l_name);
966
967 if (dols == LS_ROOT) {
968 char dirc;
969 int doit = 0;
970 int isdir =
971 (dentptr->attr & ATTR_DIR);
972
973 if (isdir) {
974 dirs++;
975 dirc = '/';
976 doit = 1;
977 } else {
978 dirc = ' ';
979 if (l_name[0] != 0) {
980 files++;
981 doit = 1;
982 }
983 }
984 if (doit) {
985 if (dirc == ' ') {
986 printf(" %8ld %s%c\n",
987 (long)FAT2CPU32(dentptr->size),
988 l_name,
989 dirc);
990 } else {
991 printf(" %s%c\n",
992 l_name,
993 dirc);
994 }
995 }
996 dentptr++;
997 continue;
998 }
999 debug("Rootvfatname: |%s|\n",
1000 l_name);
1001 } else
1002#endif
1003 {
1004 /* Volume label or VFAT entry */
1005 dentptr++;
1006 continue;
1007 }
1008 } else if (dentptr->name[0] == 0) {
1009 debug("RootDentname == NULL - %d\n", i);
1010 if (dols == LS_ROOT) {
1011 printf("\n%d file(s), %d dir(s)\n\n",
1012 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +00001013 ret = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001014 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001015 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001016 }
1017#ifdef CONFIG_SUPPORT_VFAT
1018 else if (dols == LS_ROOT &&
1019 mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +00001020 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001021 dentptr++;
1022 continue;
1023 }
1024#endif
1025 get_name(dentptr, s_name);
1026
1027 if (dols == LS_ROOT) {
1028 int isdir = (dentptr->attr & ATTR_DIR);
1029 char dirc;
1030 int doit = 0;
1031
1032 if (isdir) {
1033 dirc = '/';
1034 if (s_name[0] != 0) {
1035 dirs++;
1036 doit = 1;
1037 }
1038 } else {
1039 dirc = ' ';
1040 if (s_name[0] != 0) {
1041 files++;
1042 doit = 1;
1043 }
1044 }
1045 if (doit) {
1046 if (dirc == ' ') {
1047 printf(" %8ld %s%c\n",
1048 (long)FAT2CPU32(dentptr->size),
1049 s_name, dirc);
1050 } else {
1051 printf(" %s%c\n",
1052 s_name, dirc);
1053 }
1054 }
1055 dentptr++;
1056 continue;
1057 }
1058
1059 if (strcmp(fnamecopy, s_name)
1060 && strcmp(fnamecopy, l_name)) {
1061 debug("RootMismatch: |%s|%s|\n", s_name,
1062 l_name);
1063 dentptr++;
1064 continue;
1065 }
1066
1067 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001068 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001069
1070 debug("RootName: %s", s_name);
1071 debug(", start: 0x%x", START(dentptr));
1072 debug(", size: 0x%x %s\n",
1073 FAT2CPU32(dentptr->size),
1074 isdir ? "(DIR)" : "");
1075
1076 goto rootdir_done; /* We got a match */
1077 }
1078 debug("END LOOP: j=%d clust_size=%d\n", j,
1079 mydata->clust_size);
1080
1081 /*
1082 * On FAT32 we must fetch the FAT entries for the next
1083 * root directory clusters when a cluster has been
1084 * completely processed.
1085 */
Erik Hansen3f270f42011-03-24 10:15:37 +01001086 ++j;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001087 int rootdir_end = 0;
1088 if (mydata->fatsize == 32) {
1089 if (j == mydata->clust_size) {
1090 int nxtsect = 0;
1091 int nxt_clust = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001092
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001093 nxt_clust = get_fatent(mydata, root_cluster);
1094 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001095
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001096 nxtsect = mydata->data_begin +
1097 (nxt_clust * mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001098
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001099 root_cluster = nxt_clust;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001100
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001101 cursect = nxtsect;
1102 j = 0;
1103 }
wdenk71f95112003-06-15 22:40:42 +00001104 } else {
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001105 if (j == PREFETCH_BLOCKS)
1106 j = 0;
1107
1108 rootdir_end = (++cursect - mydata->rootdir_sect >=
1109 rootdir_size);
wdenk71f95112003-06-15 22:40:42 +00001110 }
Erik Hansen3f270f42011-03-24 10:15:37 +01001111
1112 /* If end of rootdir reached */
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001113 if (rootdir_end) {
Erik Hansen3f270f42011-03-24 10:15:37 +01001114 if (dols == LS_ROOT) {
1115 printf("\n%d file(s), %d dir(s)\n\n",
1116 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +00001117 ret = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +01001118 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001119 goto exit;
Erik Hansen3f270f42011-03-24 10:15:37 +01001120 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001121 }
1122rootdir_done:
1123
1124 firsttime = 1;
1125
1126 while (isdir) {
1127 int startsect = mydata->data_begin
1128 + START(dentptr) * mydata->clust_size;
1129 dir_entry dent;
1130 char *nextname = NULL;
1131
1132 dent = *dentptr;
1133 dentptr = &dent;
1134
1135 idx = dirdelim(subname);
1136
1137 if (idx >= 0) {
1138 subname[idx] = '\0';
1139 nextname = subname + idx + 1;
1140 /* Handle multiple delimiters */
1141 while (ISDIRDELIM(*nextname))
1142 nextname++;
1143 if (dols && *nextname == '\0')
1144 firsttime = 0;
1145 } else {
1146 if (dols && firsttime) {
1147 firsttime = 0;
1148 } else {
1149 isdir = 0;
1150 }
wdenk71f95112003-06-15 22:40:42 +00001151 }
wdenk71f95112003-06-15 22:40:42 +00001152
Wolfgang Denk7385c282010-07-19 11:37:00 +02001153 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1154 isdir ? 0 : dols) == NULL) {
1155 if (dols && !isdir)
Sergei Shtylyovac497772011-08-08 09:38:33 +00001156 ret = 0;
1157 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001158 }
wdenk71f95112003-06-15 22:40:42 +00001159
Benoît Thébaudeau7ee46ce2012-07-20 03:20:29 +00001160 if (isdir && !(dentptr->attr & ATTR_DIR))
1161 goto exit;
1162
1163 if (idx >= 0)
Wolfgang Denk7385c282010-07-19 11:37:00 +02001164 subname = nextname;
wdenk71f95112003-06-15 22:40:42 +00001165 }
1166
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001167 ret = get_contents(mydata, dentptr, pos, buffer, maxsize);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001168 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
wdenk71f95112003-06-15 22:40:42 +00001169
Sergei Shtylyovac497772011-08-08 09:38:33 +00001170exit:
1171 free(mydata->fatbuf);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001172 return ret;
wdenk71f95112003-06-15 22:40:42 +00001173}
1174
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001175long
1176do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
1177{
1178 return do_fat_read_at(filename, 0, buffer, maxsize, dols);
1179}
1180
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001181int file_fat_detectfs(void)
wdenk71f95112003-06-15 22:40:42 +00001182{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001183 boot_sector bs;
1184 volume_info volinfo;
1185 int fatsize;
1186 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001187
Wolfgang Denk7385c282010-07-19 11:37:00 +02001188 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001189 printf("No current device\n");
1190 return 1;
1191 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001192
Jon Loeligerdd60d122007-07-09 17:56:50 -05001193#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -05001194 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001195 defined(CONFIG_CMD_SCSI) || \
1196 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001197 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001198 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001199 switch (cur_dev->if_type) {
1200 case IF_TYPE_IDE:
1201 printf("IDE");
1202 break;
1203 case IF_TYPE_SATA:
1204 printf("SATA");
1205 break;
1206 case IF_TYPE_SCSI:
1207 printf("SCSI");
1208 break;
1209 case IF_TYPE_ATAPI:
1210 printf("ATAPI");
1211 break;
1212 case IF_TYPE_USB:
1213 printf("USB");
1214 break;
1215 case IF_TYPE_DOC:
1216 printf("DOC");
1217 break;
1218 case IF_TYPE_MMC:
1219 printf("MMC");
1220 break;
1221 default:
1222 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001223 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001224
1225 printf("\n Device %d: ", cur_dev->dev);
wdenk7205e402003-09-10 22:30:53 +00001226 dev_print(cur_dev);
1227#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001228
1229 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001230 printf("\nNo valid FAT fs found\n");
1231 return 1;
1232 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001233
1234 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001235 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001236 volinfo.fs_type[5] = '\0';
1237
Kyle Moffett9813b752011-12-21 07:08:10 +00001238 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part_nr,
Wolfgang Denk7385c282010-07-19 11:37:00 +02001239 volinfo.fs_type, vol_label);
1240
wdenk7205e402003-09-10 22:30:53 +00001241 return 0;
wdenk71f95112003-06-15 22:40:42 +00001242}
1243
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001244int file_fat_ls(const char *dir)
wdenk71f95112003-06-15 22:40:42 +00001245{
1246 return do_fat_read(dir, NULL, 0, LS_YES);
1247}
1248
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001249long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
1250 unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +00001251{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001252 printf("reading %s\n", filename);
Benoît Thébaudeau1170e632012-09-18 08:14:56 +00001253 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO);
1254}
1255
1256long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1257{
1258 return file_fat_read_at(filename, 0, buffer, maxsize);
wdenk71f95112003-06-15 22:40:42 +00001259}