blob: fa977b35cb71cfbfe8e0fcef9324112e8530fba2 [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) {
88 if (part_no != 1) {
89 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/*
331 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
332 * into 'buffer'.
333 * Return the number of bytes read or -1 on fatal errors.
334 */
335static long
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200336get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
337 unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +0000338{
339 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000340 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000341 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000342 __u32 endclust, newclust;
343 unsigned long actsize;
wdenk71f95112003-06-15 22:40:42 +0000344
Wolfgang Denk7385c282010-07-19 11:37:00 +0200345 debug("Filesize: %ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000346
Wolfgang Denk7385c282010-07-19 11:37:00 +0200347 if (maxsize > 0 && filesize > maxsize)
348 filesize = maxsize;
wdenk71f95112003-06-15 22:40:42 +0000349
Wolfgang Denk7385c282010-07-19 11:37:00 +0200350 debug("%ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000351
Wolfgang Denk7385c282010-07-19 11:37:00 +0200352 actsize = bytesperclust;
353 endclust = curclust;
354
wdenk71f95112003-06-15 22:40:42 +0000355 do {
wdenk7205e402003-09-10 22:30:53 +0000356 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200357 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000358 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200359 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000360 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100361 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200362 debug("curclust: 0x%x\n", newclust);
363 debug("Invalid FAT entry\n");
wdenk7205e402003-09-10 22:30:53 +0000364 return gotsize;
365 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200366 endclust = newclust;
367 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000368 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200369
wdenk7205e402003-09-10 22:30:53 +0000370 /* actsize >= file size */
371 actsize -= bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200372
wdenk7205e402003-09-10 22:30:53 +0000373 /* get remaining clusters */
374 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200375 printf("Error reading cluster\n");
wdenk71f95112003-06-15 22:40:42 +0000376 return -1;
377 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200378
wdenk7205e402003-09-10 22:30:53 +0000379 /* get remaining bytes */
380 gotsize += (int)actsize;
381 filesize -= actsize;
382 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200383 actsize = filesize;
wdenk7205e402003-09-10 22:30:53 +0000384 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200385 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000386 return -1;
387 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200388 gotsize += actsize;
wdenk7205e402003-09-10 22:30:53 +0000389 return gotsize;
390getit:
391 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200392 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000393 return -1;
394 }
395 gotsize += (int)actsize;
396 filesize -= actsize;
397 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200398
wdenk7205e402003-09-10 22:30:53 +0000399 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100400 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200401 debug("curclust: 0x%x\n", curclust);
402 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000403 return gotsize;
404 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200405 actsize = bytesperclust;
406 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000407 } while (1);
408}
409
wdenk71f95112003-06-15 22:40:42 +0000410#ifdef CONFIG_SUPPORT_VFAT
411/*
412 * Extract the file name information from 'slotptr' into 'l_name',
413 * starting at l_name[*idx].
414 * Return 1 if terminator (zero byte) is found, 0 otherwise.
415 */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200416static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000417{
418 int j;
419
420 for (j = 0; j <= 8; j += 2) {
421 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200422 if (l_name[*idx] == 0x00)
423 return 1;
wdenk71f95112003-06-15 22:40:42 +0000424 (*idx)++;
425 }
426 for (j = 0; j <= 10; j += 2) {
427 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200428 if (l_name[*idx] == 0x00)
429 return 1;
wdenk71f95112003-06-15 22:40:42 +0000430 (*idx)++;
431 }
432 for (j = 0; j <= 2; j += 2) {
433 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200434 if (l_name[*idx] == 0x00)
435 return 1;
wdenk71f95112003-06-15 22:40:42 +0000436 (*idx)++;
437 }
438
439 return 0;
440}
441
wdenk71f95112003-06-15 22:40:42 +0000442/*
443 * Extract the full long filename starting at 'retdent' (which is really
444 * a slot) into 'l_name'. If successful also copy the real directory entry
445 * into 'retdent'
446 * Return 0 on success, -1 otherwise.
447 */
Eric Nelson9a800ac2012-04-11 04:08:53 +0000448__u8 get_vfatname_block[MAX_CLUSTSIZE]
449 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200450
wdenk71f95112003-06-15 22:40:42 +0000451static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200452get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
453 dir_entry *retdent, char *l_name)
wdenk71f95112003-06-15 22:40:42 +0000454{
455 dir_entry *realdent;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200456 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000457 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
458 PREFETCH_BLOCKS :
459 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200460 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000461 int idx = 0;
462
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300463 if (counter > VFAT_MAXSEQ) {
464 debug("Error: VFAT name is too long\n");
465 return -1;
466 }
467
468 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200469 if (counter == 0)
470 break;
wdenk2d1a5372004-02-23 19:30:57 +0000471 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
472 return -1;
wdenk71f95112003-06-15 22:40:42 +0000473 slotptr++;
474 counter--;
475 }
476
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300477 if ((__u8 *)slotptr >= buflimit) {
wdenk71f95112003-06-15 22:40:42 +0000478 dir_slot *slotptr2;
479
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300480 if (curclust == 0)
481 return -1;
wdenk71f95112003-06-15 22:40:42 +0000482 curclust = get_fatent(mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100483 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200484 debug("curclust: 0x%x\n", curclust);
485 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000486 return -1;
487 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200488
wdenk5fa66df2003-10-29 23:18:55 +0000489 if (get_cluster(mydata, curclust, get_vfatname_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000490 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200491 debug("Error: reading directory block\n");
wdenk71f95112003-06-15 22:40:42 +0000492 return -1;
493 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200494
495 slotptr2 = (dir_slot *)get_vfatname_block;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300496 while (counter > 0) {
497 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
498 & 0xff) != counter)
499 return -1;
wdenk71f95112003-06-15 22:40:42 +0000500 slotptr2++;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300501 counter--;
502 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200503
wdenk71f95112003-06-15 22:40:42 +0000504 /* Save the real directory entry */
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300505 realdent = (dir_entry *)slotptr2;
506 while ((__u8 *)slotptr2 > get_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000507 slotptr2--;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300508 slot2str(slotptr2, l_name, &idx);
wdenk71f95112003-06-15 22:40:42 +0000509 }
510 } else {
511 /* Save the real directory entry */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200512 realdent = (dir_entry *)slotptr;
wdenk71f95112003-06-15 22:40:42 +0000513 }
514
515 do {
516 slotptr--;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200517 if (slot2str(slotptr, l_name, &idx))
518 break;
wdenk2d1a5372004-02-23 19:30:57 +0000519 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000520
521 l_name[idx] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +0200522 if (*l_name == DELETED_FLAG)
523 *l_name = '\0';
524 else if (*l_name == aRING)
525 *l_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000526 downcase(l_name);
527
528 /* Return the real directory entry */
529 memcpy(retdent, realdent, sizeof(dir_entry));
530
531 return 0;
532}
533
wdenk71f95112003-06-15 22:40:42 +0000534/* Calculate short name checksum */
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200535static __u8 mkcksum(const char *str)
wdenk71f95112003-06-15 22:40:42 +0000536{
537 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200538
wdenk71f95112003-06-15 22:40:42 +0000539 __u8 ret = 0;
540
541 for (i = 0; i < 11; i++) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200542 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
wdenk71f95112003-06-15 22:40:42 +0000543 }
544
545 return ret;
546}
Wolfgang Denk7385c282010-07-19 11:37:00 +0200547#endif /* CONFIG_SUPPORT_VFAT */
wdenk71f95112003-06-15 22:40:42 +0000548
549/*
550 * Get the directory entry associated with 'filename' from the directory
551 * starting at 'startsect'
552 */
Eric Nelson9a800ac2012-04-11 04:08:53 +0000553__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
554 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200555
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200556static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
557 char *filename, dir_entry *retdent,
558 int dols)
wdenk71f95112003-06-15 22:40:42 +0000559{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200560 __u16 prevcksum = 0xffff;
561 __u32 curclust = START(retdent);
562 int files = 0, dirs = 0;
wdenk71f95112003-06-15 22:40:42 +0000563
Wolfgang Denk7385c282010-07-19 11:37:00 +0200564 debug("get_dentfromdir: %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +0000565
Wolfgang Denk7385c282010-07-19 11:37:00 +0200566 while (1) {
567 dir_entry *dentptr;
wdenk71f95112003-06-15 22:40:42 +0000568
Wolfgang Denk7385c282010-07-19 11:37:00 +0200569 int i;
wdenk71f95112003-06-15 22:40:42 +0000570
Wolfgang Denk7385c282010-07-19 11:37:00 +0200571 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000572 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200573 debug("Error: reading directory block\n");
574 return NULL;
575 }
576
577 dentptr = (dir_entry *)get_dentfromdir_block;
578
579 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300580 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200581
582 l_name[0] = '\0';
583 if (dentptr->name[0] == DELETED_FLAG) {
584 dentptr++;
585 continue;
wdenk71f95112003-06-15 22:40:42 +0000586 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200587 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000588#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand206d68f2011-10-19 07:43:08 +0000589 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
590 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200591 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
592 get_vfatname(mydata, curclust,
593 get_dentfromdir_block,
594 dentptr, l_name);
595 if (dols) {
596 int isdir;
597 char dirc;
598 int doit = 0;
599
600 isdir = (dentptr->attr & ATTR_DIR);
601
602 if (isdir) {
603 dirs++;
604 dirc = '/';
605 doit = 1;
606 } else {
607 dirc = ' ';
608 if (l_name[0] != 0) {
609 files++;
610 doit = 1;
611 }
612 }
613 if (doit) {
614 if (dirc == ' ') {
615 printf(" %8ld %s%c\n",
616 (long)FAT2CPU32(dentptr->size),
617 l_name,
618 dirc);
619 } else {
620 printf(" %s%c\n",
621 l_name,
622 dirc);
623 }
624 }
625 dentptr++;
626 continue;
627 }
628 debug("vfatname: |%s|\n", l_name);
629 } else
wdenk71f95112003-06-15 22:40:42 +0000630#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200631 {
632 /* Volume label or VFAT entry */
633 dentptr++;
634 continue;
635 }
636 }
637 if (dentptr->name[0] == 0) {
638 if (dols) {
639 printf("\n%d file(s), %d dir(s)\n\n",
640 files, dirs);
641 }
642 debug("Dentname == NULL - %d\n", i);
643 return NULL;
644 }
645#ifdef CONFIG_SUPPORT_VFAT
646 if (dols && mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +0000647 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200648 dentptr++;
649 continue;
650 }
651#endif
652 get_name(dentptr, s_name);
653 if (dols) {
654 int isdir = (dentptr->attr & ATTR_DIR);
655 char dirc;
656 int doit = 0;
wdenk71f95112003-06-15 22:40:42 +0000657
Wolfgang Denk7385c282010-07-19 11:37:00 +0200658 if (isdir) {
659 dirs++;
660 dirc = '/';
661 doit = 1;
662 } else {
663 dirc = ' ';
664 if (s_name[0] != 0) {
665 files++;
666 doit = 1;
667 }
668 }
669
670 if (doit) {
671 if (dirc == ' ') {
672 printf(" %8ld %s%c\n",
673 (long)FAT2CPU32(dentptr->size),
674 s_name, dirc);
675 } else {
676 printf(" %s%c\n",
677 s_name, dirc);
678 }
679 }
680
681 dentptr++;
682 continue;
683 }
684
685 if (strcmp(filename, s_name)
686 && strcmp(filename, l_name)) {
687 debug("Mismatch: |%s|%s|\n", s_name, l_name);
688 dentptr++;
689 continue;
690 }
691
692 memcpy(retdent, dentptr, sizeof(dir_entry));
693
694 debug("DentName: %s", s_name);
695 debug(", start: 0x%x", START(dentptr));
696 debug(", size: 0x%x %s\n",
697 FAT2CPU32(dentptr->size),
698 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
699
700 return retdent;
wdenk71f95112003-06-15 22:40:42 +0000701 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200702
703 curclust = get_fatent(mydata, curclust);
704 if (CHECK_CLUST(curclust, mydata->fatsize)) {
705 debug("curclust: 0x%x\n", curclust);
706 printf("Invalid FAT entry\n");
707 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000708 }
wdenk71f95112003-06-15 22:40:42 +0000709 }
wdenk71f95112003-06-15 22:40:42 +0000710
Wolfgang Denk7385c282010-07-19 11:37:00 +0200711 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000712}
713
wdenk71f95112003-06-15 22:40:42 +0000714/*
715 * Read boot sector and volume info from a FAT filesystem
716 */
717static int
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200718read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000719{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000720 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000721 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000722 int ret = 0;
723
724 if (cur_dev == NULL) {
725 debug("Error: no device selected\n");
726 return -1;
727 }
728
Eric Nelson9a800ac2012-04-11 04:08:53 +0000729 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000730 if (block == NULL) {
731 debug("Error: allocating block\n");
732 return -1;
733 }
wdenk71f95112003-06-15 22:40:42 +0000734
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200735 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200736 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000737 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000738 }
739
740 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200741 bs->reserved = FAT2CPU16(bs->reserved);
742 bs->fat_length = FAT2CPU16(bs->fat_length);
743 bs->secs_track = FAT2CPU16(bs->secs_track);
744 bs->heads = FAT2CPU16(bs->heads);
745 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000746
747 /* FAT32 entries */
748 if (bs->fat_length == 0) {
749 /* Assume FAT32 */
750 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200751 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000752 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200753 bs->info_sector = FAT2CPU16(bs->info_sector);
754 bs->backup_boot = FAT2CPU16(bs->backup_boot);
755 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000756 *fatsize = 32;
757 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200758 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000759 *fatsize = 0;
760 }
761 memcpy(volinfo, vistart, sizeof(volume_info));
762
wdenk71f95112003-06-15 22:40:42 +0000763 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200764 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000765 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000766 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500767 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000768 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000769 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000770 }
Tom Rix651351f2009-05-20 07:55:41 -0500771 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000772 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000773 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000774 }
775 }
776
Wolfgang Denk7385c282010-07-19 11:37:00 +0200777 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000778fail:
779 ret = -1;
780exit:
781 free(block);
782 return ret;
wdenk71f95112003-06-15 22:40:42 +0000783}
784
Eric Nelson9a800ac2012-04-11 04:08:53 +0000785__u8 do_fat_read_block[MAX_CLUSTSIZE]
786 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200787
stroese20cc00d2004-12-16 17:57:26 +0000788long
Benoît Thébaudeau9795e072012-07-20 15:18:44 +0200789do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
wdenk71f95112003-06-15 22:40:42 +0000790{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200791 char fnamecopy[2048];
792 boot_sector bs;
793 volume_info volinfo;
794 fsdata datablock;
795 fsdata *mydata = &datablock;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200796 dir_entry *dentptr = NULL;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200797 __u16 prevcksum = 0xffff;
798 char *subname = "";
Erik Hansen3f270f42011-03-24 10:15:37 +0100799 __u32 cursect;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200800 int idx, isdir = 0;
801 int files = 0, dirs = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000802 long ret = -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200803 int firsttime;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000804 __u32 root_cluster = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +0100805 int rootdir_size = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200806 int j;
wdenk71f95112003-06-15 22:40:42 +0000807
Wolfgang Denk7385c282010-07-19 11:37:00 +0200808 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
809 debug("Error: reading boot sector\n");
810 return -1;
811 }
812
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000813 if (mydata->fatsize == 32) {
814 root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200815 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000816 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200817 mydata->fatlength = bs.fat_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000818 }
wdenk71f95112003-06-15 22:40:42 +0000819
Wolfgang Denk7385c282010-07-19 11:37:00 +0200820 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000821
Wolfgang Denk7385c282010-07-19 11:37:00 +0200822 cursect = mydata->rootdir_sect
823 = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000824
Sergei Shtylyovac497772011-08-08 09:38:33 +0000825 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200826 mydata->clust_size = bs.cluster_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000827 if (mydata->sect_size != cur_part_info.blksz) {
828 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
829 mydata->sect_size, cur_part_info.blksz);
830 return -1;
831 }
wdenk71f95112003-06-15 22:40:42 +0000832
Wolfgang Denk7385c282010-07-19 11:37:00 +0200833 if (mydata->fatsize == 32) {
834 mydata->data_begin = mydata->rootdir_sect -
835 (mydata->clust_size * 2);
836 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200837 rootdir_size = ((bs.dir_entries[1] * (int)256 +
838 bs.dir_entries[0]) *
839 sizeof(dir_entry)) /
Sergei Shtylyovac497772011-08-08 09:38:33 +0000840 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200841 mydata->data_begin = mydata->rootdir_sect +
842 rootdir_size -
843 (mydata->clust_size * 2);
wdenk71f95112003-06-15 22:40:42 +0000844 }
wdenk71f95112003-06-15 22:40:42 +0000845
Wolfgang Denk7385c282010-07-19 11:37:00 +0200846 mydata->fatbufnum = -1;
Eric Nelson9a800ac2012-04-11 04:08:53 +0000847 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000848 if (mydata->fatbuf == NULL) {
849 debug("Error: allocating memory\n");
850 return -1;
851 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200852
wdenk71f95112003-06-15 22:40:42 +0000853#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk7385c282010-07-19 11:37:00 +0200854 debug("VFAT Support enabled\n");
wdenk71f95112003-06-15 22:40:42 +0000855#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200856 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
857 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
858 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
859 "Data begins at: %d\n",
860 root_cluster,
861 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000862 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
863 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
864 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200865
866 /* "cwd" is always the root... */
867 while (ISDIRDELIM(*filename))
868 filename++;
869
870 /* Make a copy of the filename and convert it to lowercase */
871 strcpy(fnamecopy, filename);
872 downcase(fnamecopy);
873
874 if (*fnamecopy == '\0') {
875 if (!dols)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000876 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200877
878 dols = LS_ROOT;
879 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
880 isdir = 1;
881 fnamecopy[idx] = '\0';
882 subname = fnamecopy + idx + 1;
883
884 /* Handle multiple delimiters */
885 while (ISDIRDELIM(*subname))
886 subname++;
887 } else if (dols) {
888 isdir = 1;
889 }
890
891 j = 0;
892 while (1) {
893 int i;
894
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200895 if (j == 0) {
896 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
897 cursect, mydata->clust_size, DIRENTSPERBLOCK);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200898
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +0200899 if (disk_read(cursect,
900 (mydata->fatsize == 32) ?
901 (mydata->clust_size) :
902 PREFETCH_BLOCKS,
903 do_fat_read_block) < 0) {
904 debug("Error: reading rootdir block\n");
905 goto exit;
906 }
907
908 dentptr = (dir_entry *) do_fat_read_block;
wdenk71f95112003-06-15 22:40:42 +0000909 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200910
Wolfgang Denk7385c282010-07-19 11:37:00 +0200911 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300912 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200913
914 l_name[0] = '\0';
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300915 if (dentptr->name[0] == DELETED_FLAG) {
916 dentptr++;
917 continue;
918 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200919 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000920#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand206d68f2011-10-19 07:43:08 +0000921 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk7385c282010-07-19 11:37:00 +0200922 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
923 prevcksum =
924 ((dir_slot *)dentptr)->alias_checksum;
wdenk71f95112003-06-15 22:40:42 +0000925
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300926 get_vfatname(mydata,
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000927 root_cluster,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200928 do_fat_read_block,
929 dentptr, l_name);
930
931 if (dols == LS_ROOT) {
932 char dirc;
933 int doit = 0;
934 int isdir =
935 (dentptr->attr & ATTR_DIR);
936
937 if (isdir) {
938 dirs++;
939 dirc = '/';
940 doit = 1;
941 } else {
942 dirc = ' ';
943 if (l_name[0] != 0) {
944 files++;
945 doit = 1;
946 }
947 }
948 if (doit) {
949 if (dirc == ' ') {
950 printf(" %8ld %s%c\n",
951 (long)FAT2CPU32(dentptr->size),
952 l_name,
953 dirc);
954 } else {
955 printf(" %s%c\n",
956 l_name,
957 dirc);
958 }
959 }
960 dentptr++;
961 continue;
962 }
963 debug("Rootvfatname: |%s|\n",
964 l_name);
965 } else
966#endif
967 {
968 /* Volume label or VFAT entry */
969 dentptr++;
970 continue;
971 }
972 } else if (dentptr->name[0] == 0) {
973 debug("RootDentname == NULL - %d\n", i);
974 if (dols == LS_ROOT) {
975 printf("\n%d file(s), %d dir(s)\n\n",
976 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000977 ret = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200978 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000979 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200980 }
981#ifdef CONFIG_SUPPORT_VFAT
982 else if (dols == LS_ROOT &&
983 mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +0000984 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200985 dentptr++;
986 continue;
987 }
988#endif
989 get_name(dentptr, s_name);
990
991 if (dols == LS_ROOT) {
992 int isdir = (dentptr->attr & ATTR_DIR);
993 char dirc;
994 int doit = 0;
995
996 if (isdir) {
997 dirc = '/';
998 if (s_name[0] != 0) {
999 dirs++;
1000 doit = 1;
1001 }
1002 } else {
1003 dirc = ' ';
1004 if (s_name[0] != 0) {
1005 files++;
1006 doit = 1;
1007 }
1008 }
1009 if (doit) {
1010 if (dirc == ' ') {
1011 printf(" %8ld %s%c\n",
1012 (long)FAT2CPU32(dentptr->size),
1013 s_name, dirc);
1014 } else {
1015 printf(" %s%c\n",
1016 s_name, dirc);
1017 }
1018 }
1019 dentptr++;
1020 continue;
1021 }
1022
1023 if (strcmp(fnamecopy, s_name)
1024 && strcmp(fnamecopy, l_name)) {
1025 debug("RootMismatch: |%s|%s|\n", s_name,
1026 l_name);
1027 dentptr++;
1028 continue;
1029 }
1030
1031 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001032 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001033
1034 debug("RootName: %s", s_name);
1035 debug(", start: 0x%x", START(dentptr));
1036 debug(", size: 0x%x %s\n",
1037 FAT2CPU32(dentptr->size),
1038 isdir ? "(DIR)" : "");
1039
1040 goto rootdir_done; /* We got a match */
1041 }
1042 debug("END LOOP: j=%d clust_size=%d\n", j,
1043 mydata->clust_size);
1044
1045 /*
1046 * On FAT32 we must fetch the FAT entries for the next
1047 * root directory clusters when a cluster has been
1048 * completely processed.
1049 */
Erik Hansen3f270f42011-03-24 10:15:37 +01001050 ++j;
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001051 int rootdir_end = 0;
1052 if (mydata->fatsize == 32) {
1053 if (j == mydata->clust_size) {
1054 int nxtsect = 0;
1055 int nxt_clust = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001056
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001057 nxt_clust = get_fatent(mydata, root_cluster);
1058 rootdir_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001059
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001060 nxtsect = mydata->data_begin +
1061 (nxt_clust * mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001062
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001063 root_cluster = nxt_clust;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001064
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001065 cursect = nxtsect;
1066 j = 0;
1067 }
wdenk71f95112003-06-15 22:40:42 +00001068 } else {
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001069 if (j == PREFETCH_BLOCKS)
1070 j = 0;
1071
1072 rootdir_end = (++cursect - mydata->rootdir_sect >=
1073 rootdir_size);
wdenk71f95112003-06-15 22:40:42 +00001074 }
Erik Hansen3f270f42011-03-24 10:15:37 +01001075
1076 /* If end of rootdir reached */
Benoît Thébaudeaucd1b0422012-07-20 15:20:12 +02001077 if (rootdir_end) {
Erik Hansen3f270f42011-03-24 10:15:37 +01001078 if (dols == LS_ROOT) {
1079 printf("\n%d file(s), %d dir(s)\n\n",
1080 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +00001081 ret = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +01001082 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001083 goto exit;
Erik Hansen3f270f42011-03-24 10:15:37 +01001084 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001085 }
1086rootdir_done:
1087
1088 firsttime = 1;
1089
1090 while (isdir) {
1091 int startsect = mydata->data_begin
1092 + START(dentptr) * mydata->clust_size;
1093 dir_entry dent;
1094 char *nextname = NULL;
1095
1096 dent = *dentptr;
1097 dentptr = &dent;
1098
1099 idx = dirdelim(subname);
1100
1101 if (idx >= 0) {
1102 subname[idx] = '\0';
1103 nextname = subname + idx + 1;
1104 /* Handle multiple delimiters */
1105 while (ISDIRDELIM(*nextname))
1106 nextname++;
1107 if (dols && *nextname == '\0')
1108 firsttime = 0;
1109 } else {
1110 if (dols && firsttime) {
1111 firsttime = 0;
1112 } else {
1113 isdir = 0;
1114 }
wdenk71f95112003-06-15 22:40:42 +00001115 }
wdenk71f95112003-06-15 22:40:42 +00001116
Wolfgang Denk7385c282010-07-19 11:37:00 +02001117 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1118 isdir ? 0 : dols) == NULL) {
1119 if (dols && !isdir)
Sergei Shtylyovac497772011-08-08 09:38:33 +00001120 ret = 0;
1121 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001122 }
wdenk71f95112003-06-15 22:40:42 +00001123
Wolfgang Denk7385c282010-07-19 11:37:00 +02001124 if (idx >= 0) {
1125 if (!(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001126 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001127 subname = nextname;
1128 }
wdenk71f95112003-06-15 22:40:42 +00001129 }
1130
Wolfgang Denk7385c282010-07-19 11:37:00 +02001131 ret = get_contents(mydata, dentptr, buffer, maxsize);
1132 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
wdenk71f95112003-06-15 22:40:42 +00001133
Sergei Shtylyovac497772011-08-08 09:38:33 +00001134exit:
1135 free(mydata->fatbuf);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001136 return ret;
wdenk71f95112003-06-15 22:40:42 +00001137}
1138
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001139int file_fat_detectfs(void)
wdenk71f95112003-06-15 22:40:42 +00001140{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001141 boot_sector bs;
1142 volume_info volinfo;
1143 int fatsize;
1144 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001145
Wolfgang Denk7385c282010-07-19 11:37:00 +02001146 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001147 printf("No current device\n");
1148 return 1;
1149 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001150
Jon Loeligerdd60d122007-07-09 17:56:50 -05001151#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -05001152 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001153 defined(CONFIG_CMD_SCSI) || \
1154 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001155 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001156 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001157 switch (cur_dev->if_type) {
1158 case IF_TYPE_IDE:
1159 printf("IDE");
1160 break;
1161 case IF_TYPE_SATA:
1162 printf("SATA");
1163 break;
1164 case IF_TYPE_SCSI:
1165 printf("SCSI");
1166 break;
1167 case IF_TYPE_ATAPI:
1168 printf("ATAPI");
1169 break;
1170 case IF_TYPE_USB:
1171 printf("USB");
1172 break;
1173 case IF_TYPE_DOC:
1174 printf("DOC");
1175 break;
1176 case IF_TYPE_MMC:
1177 printf("MMC");
1178 break;
1179 default:
1180 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001181 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001182
1183 printf("\n Device %d: ", cur_dev->dev);
wdenk7205e402003-09-10 22:30:53 +00001184 dev_print(cur_dev);
1185#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001186
1187 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001188 printf("\nNo valid FAT fs found\n");
1189 return 1;
1190 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001191
1192 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001193 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001194 volinfo.fs_type[5] = '\0';
1195
Kyle Moffett9813b752011-12-21 07:08:10 +00001196 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part_nr,
Wolfgang Denk7385c282010-07-19 11:37:00 +02001197 volinfo.fs_type, vol_label);
1198
wdenk7205e402003-09-10 22:30:53 +00001199 return 0;
wdenk71f95112003-06-15 22:40:42 +00001200}
1201
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001202int file_fat_ls(const char *dir)
wdenk71f95112003-06-15 22:40:42 +00001203{
1204 return do_fat_read(dir, NULL, 0, LS_YES);
1205}
1206
Benoît Thébaudeau9795e072012-07-20 15:18:44 +02001207long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +00001208{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001209 printf("reading %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +00001210 return do_fat_read(filename, buffer, maxsize, LS_NO);
1211}