blob: 2bb7adfcf156424e48b9df634473c6fb6d6bca54 [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>
wdenk71f95112003-06-15 22:40:42 +000034
wdenk71f95112003-06-15 22:40:42 +000035/*
36 * Convert a string to lowercase.
37 */
Wolfgang Denk7385c282010-07-19 11:37:00 +020038static void downcase (char *str)
wdenk71f95112003-06-15 22:40:42 +000039{
40 while (*str != '\0') {
41 TOLOWER(*str);
42 str++;
43 }
44}
45
Kyle Moffett9813b752011-12-21 07:08:10 +000046static block_dev_desc_t *cur_dev;
47static unsigned int cur_part_nr;
48static disk_partition_t cur_part_info;
Wolfgang Denk7385c282010-07-19 11:37:00 +020049
Kyle Moffett9813b752011-12-21 07:08:10 +000050#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk7205e402003-09-10 22:30:53 +000051#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk66c2d732010-07-19 11:36:57 +020052#define DOS_FS32_TYPE_OFFSET 0x52
wdenk71f95112003-06-15 22:40:42 +000053
Kyle Moffett9813b752011-12-21 07:08:10 +000054static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk71f95112003-06-15 22:40:42 +000055{
Kyle Moffett9813b752011-12-21 07:08:10 +000056 if (!cur_dev || !cur_dev->block_read)
wdenk7205e402003-09-10 22:30:53 +000057 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020058
Kyle Moffett9813b752011-12-21 07:08:10 +000059 return cur_dev->block_read(cur_dev->dev,
60 cur_part_info.start + block, nr_blocks, buf);
wdenk71f95112003-06-15 22:40:42 +000061}
62
Wolfgang Denk7385c282010-07-19 11:37:00 +020063int fat_register_device (block_dev_desc_t * dev_desc, int part_no)
wdenk71f95112003-06-15 22:40:42 +000064{
Sergei Shtylyovac497772011-08-08 09:38:33 +000065 unsigned char buffer[dev_desc->blksz];
wdenk7205e402003-09-10 22:30:53 +000066
Kyle Moffett9813b752011-12-21 07:08:10 +000067 /* First close any currently found FAT filesystem */
68 cur_dev = NULL;
Wolfgang Denk7385c282010-07-19 11:37:00 +020069
Jon Loeligerdd60d122007-07-09 17:56:50 -050070#if (defined(CONFIG_CMD_IDE) || \
unsik Kim75eb82e2009-02-25 11:31:24 +090071 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -050072 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -050073 defined(CONFIG_CMD_SCSI) || \
74 defined(CONFIG_CMD_USB) || \
Andy Fleming02df4a22008-01-09 13:51:32 -060075 defined(CONFIG_MMC) || \
76 defined(CONFIG_SYSTEMACE) )
Andy Fleming02df4a22008-01-09 13:51:32 -060077
Kyle Moffett9813b752011-12-21 07:08:10 +000078 /* Read the partition table, if present */
79 if (!get_partition_info(dev_desc, part_no, &cur_part_info)) {
80 cur_dev = dev_desc;
81 cur_part_nr = part_no;
Andy Fleming02df4a22008-01-09 13:51:32 -060082 }
83#endif
Kyle Moffett9813b752011-12-21 07:08:10 +000084
85 /* Otherwise it might be a superfloppy (whole-disk FAT filesystem) */
86 if (!cur_dev) {
87 if (part_no != 1) {
88 printf("** Partition %d not valid on device %d **\n",
89 part_no, dev_desc->dev);
90 return -1;
91 }
92
93 cur_dev = dev_desc;
94 cur_part_nr = 1;
95 cur_part_info.start = 0;
96 cur_part_info.size = dev_desc->lba;
97 cur_part_info.blksz = dev_desc->blksz;
98 memset(cur_part_info.name, 0, sizeof(cur_part_info.name));
99 memset(cur_part_info.type, 0, sizeof(cur_part_info.type));
100 }
101
102 /* Make sure it has a valid FAT header */
103 if (disk_read(0, 1, buffer) != 1) {
104 cur_dev = NULL;
105 return -1;
106 }
107
108 /* Check if it's actually a DOS volume */
109 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
110 cur_dev = NULL;
111 return -1;
112 }
113
114 /* Check for FAT12/FAT16/FAT32 filesystem */
115 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
116 return 0;
117 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
118 return 0;
119
120 cur_dev = NULL;
121 return -1;
wdenk71f95112003-06-15 22:40:42 +0000122}
123
Kyle Moffett9813b752011-12-21 07:08:10 +0000124
wdenk71f95112003-06-15 22:40:42 +0000125/*
126 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
127 * Return index into string if found, -1 otherwise.
128 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200129static int dirdelim (char *str)
wdenk71f95112003-06-15 22:40:42 +0000130{
131 char *start = str;
132
133 while (*str != '\0') {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200134 if (ISDIRDELIM(*str))
135 return str - start;
wdenk71f95112003-06-15 22:40:42 +0000136 str++;
137 }
138 return -1;
139}
140
wdenk71f95112003-06-15 22:40:42 +0000141/*
142 * Extract zero terminated short name from a directory entry.
143 */
144static void get_name (dir_entry *dirent, char *s_name)
145{
146 char *ptr;
147
Wolfgang Denk7385c282010-07-19 11:37:00 +0200148 memcpy(s_name, dirent->name, 8);
wdenk71f95112003-06-15 22:40:42 +0000149 s_name[8] = '\0';
150 ptr = s_name;
151 while (*ptr && *ptr != ' ')
152 ptr++;
153 if (dirent->ext[0] && dirent->ext[0] != ' ') {
154 *ptr = '.';
155 ptr++;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200156 memcpy(ptr, dirent->ext, 3);
wdenk71f95112003-06-15 22:40:42 +0000157 ptr[3] = '\0';
158 while (*ptr && *ptr != ' ')
159 ptr++;
160 }
161 *ptr = '\0';
162 if (*s_name == DELETED_FLAG)
163 *s_name = '\0';
164 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100165 *s_name = DELETED_FLAG;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200166 downcase(s_name);
wdenk71f95112003-06-15 22:40:42 +0000167}
168
169/*
170 * Get the entry at index 'entry' in a FAT (12/16/32) table.
171 * On failure 0x00 is returned.
172 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200173static __u32 get_fatent (fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000174{
175 __u32 bufnum;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200176 __u32 off16, offset;
wdenk71f95112003-06-15 22:40:42 +0000177 __u32 ret = 0x00;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200178 __u16 val1, val2;
wdenk71f95112003-06-15 22:40:42 +0000179
180 switch (mydata->fatsize) {
181 case 32:
182 bufnum = entry / FAT32BUFSIZE;
183 offset = entry - bufnum * FAT32BUFSIZE;
184 break;
185 case 16:
186 bufnum = entry / FAT16BUFSIZE;
187 offset = entry - bufnum * FAT16BUFSIZE;
188 break;
189 case 12:
190 bufnum = entry / FAT12BUFSIZE;
191 offset = entry - bufnum * FAT12BUFSIZE;
192 break;
193
194 default:
195 /* Unsupported FAT size */
196 return ret;
197 }
198
Wolfgang Denk7385c282010-07-19 11:37:00 +0200199 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
200 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200201
wdenk71f95112003-06-15 22:40:42 +0000202 /* Read a new block of FAT entries into the cache. */
203 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000204 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000205 __u8 *bufptr = mydata->fatbuf;
206 __u32 fatlength = mydata->fatlength;
207 __u32 startblock = bufnum * FATBUFBLOCKS;
208
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000209 if (getsize > fatlength)
210 getsize = fatlength;
211
Sergei Shtylyovac497772011-08-08 09:38:33 +0000212 fatlength *= mydata->sect_size; /* We want it in bytes now */
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
Wolfgang Denk7385c282010-07-19 11:37:00 +0200272get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
273 unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000274{
Erik Hansen3f270f42011-03-24 10:15:37 +0100275 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000276 __u32 startsect;
277
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
Sergei Shtylyovac497772011-08-08 09:38:33 +0000287 if (disk_read(startsect, size / mydata->sect_size, buffer) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200288 debug("Error reading data\n");
wdenk7205e402003-09-10 22:30:53 +0000289 return -1;
290 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000291 if (size % mydata->sect_size) {
292 __u8 tmpbuf[mydata->sect_size];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200293
Sergei Shtylyovac497772011-08-08 09:38:33 +0000294 idx = size / mydata->sect_size;
wdenk7205e402003-09-10 22:30:53 +0000295 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200296 debug("Error reading data\n");
wdenk7205e402003-09-10 22:30:53 +0000297 return -1;
wdenk71f95112003-06-15 22:40:42 +0000298 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000299 buffer += idx * mydata->sect_size;
wdenk7205e402003-09-10 22:30:53 +0000300
Sergei Shtylyovac497772011-08-08 09:38:33 +0000301 memcpy(buffer, tmpbuf, size % mydata->sect_size);
wdenk7205e402003-09-10 22:30:53 +0000302 return 0;
wdenk71f95112003-06-15 22:40:42 +0000303 }
304
305 return 0;
306}
307
wdenk71f95112003-06-15 22:40:42 +0000308/*
309 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
310 * into 'buffer'.
311 * Return the number of bytes read or -1 on fatal errors.
312 */
313static long
Wolfgang Denk7385c282010-07-19 11:37:00 +0200314get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
315 unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +0000316{
317 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000318 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000319 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000320 __u32 endclust, newclust;
321 unsigned long actsize;
wdenk71f95112003-06-15 22:40:42 +0000322
Wolfgang Denk7385c282010-07-19 11:37:00 +0200323 debug("Filesize: %ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000324
Wolfgang Denk7385c282010-07-19 11:37:00 +0200325 if (maxsize > 0 && filesize > maxsize)
326 filesize = maxsize;
wdenk71f95112003-06-15 22:40:42 +0000327
Wolfgang Denk7385c282010-07-19 11:37:00 +0200328 debug("%ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000329
Wolfgang Denk7385c282010-07-19 11:37:00 +0200330 actsize = bytesperclust;
331 endclust = curclust;
332
wdenk71f95112003-06-15 22:40:42 +0000333 do {
wdenk7205e402003-09-10 22:30:53 +0000334 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200335 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000336 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200337 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000338 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100339 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200340 debug("curclust: 0x%x\n", newclust);
341 debug("Invalid FAT entry\n");
wdenk7205e402003-09-10 22:30:53 +0000342 return gotsize;
343 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200344 endclust = newclust;
345 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000346 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200347
wdenk7205e402003-09-10 22:30:53 +0000348 /* actsize >= file size */
349 actsize -= bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200350
wdenk7205e402003-09-10 22:30:53 +0000351 /* get remaining clusters */
352 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200353 printf("Error reading cluster\n");
wdenk71f95112003-06-15 22:40:42 +0000354 return -1;
355 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200356
wdenk7205e402003-09-10 22:30:53 +0000357 /* get remaining bytes */
358 gotsize += (int)actsize;
359 filesize -= actsize;
360 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200361 actsize = filesize;
wdenk7205e402003-09-10 22:30:53 +0000362 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200363 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000364 return -1;
365 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200366 gotsize += actsize;
wdenk7205e402003-09-10 22:30:53 +0000367 return gotsize;
368getit:
369 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200370 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000371 return -1;
372 }
373 gotsize += (int)actsize;
374 filesize -= actsize;
375 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200376
wdenk7205e402003-09-10 22:30:53 +0000377 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100378 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200379 debug("curclust: 0x%x\n", curclust);
380 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000381 return gotsize;
382 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200383 actsize = bytesperclust;
384 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000385 } while (1);
386}
387
wdenk71f95112003-06-15 22:40:42 +0000388#ifdef CONFIG_SUPPORT_VFAT
389/*
390 * Extract the file name information from 'slotptr' into 'l_name',
391 * starting at l_name[*idx].
392 * Return 1 if terminator (zero byte) is found, 0 otherwise.
393 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200394static int slot2str (dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000395{
396 int j;
397
398 for (j = 0; j <= 8; j += 2) {
399 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200400 if (l_name[*idx] == 0x00)
401 return 1;
wdenk71f95112003-06-15 22:40:42 +0000402 (*idx)++;
403 }
404 for (j = 0; j <= 10; j += 2) {
405 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200406 if (l_name[*idx] == 0x00)
407 return 1;
wdenk71f95112003-06-15 22:40:42 +0000408 (*idx)++;
409 }
410 for (j = 0; j <= 2; j += 2) {
411 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200412 if (l_name[*idx] == 0x00)
413 return 1;
wdenk71f95112003-06-15 22:40:42 +0000414 (*idx)++;
415 }
416
417 return 0;
418}
419
wdenk71f95112003-06-15 22:40:42 +0000420/*
421 * Extract the full long filename starting at 'retdent' (which is really
422 * a slot) into 'l_name'. If successful also copy the real directory entry
423 * into 'retdent'
424 * Return 0 on success, -1 otherwise.
425 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200426__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
Bryan Wu7e4b9b42009-01-02 20:47:45 -0500427__u8 get_vfatname_block[MAX_CLUSTSIZE];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200428
wdenk71f95112003-06-15 22:40:42 +0000429static int
Wolfgang Denk7385c282010-07-19 11:37:00 +0200430get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
431 dir_entry *retdent, char *l_name)
wdenk71f95112003-06-15 22:40:42 +0000432{
433 dir_entry *realdent;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200434 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000435 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
436 PREFETCH_BLOCKS :
437 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200438 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000439 int idx = 0;
440
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300441 if (counter > VFAT_MAXSEQ) {
442 debug("Error: VFAT name is too long\n");
443 return -1;
444 }
445
446 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200447 if (counter == 0)
448 break;
wdenk2d1a5372004-02-23 19:30:57 +0000449 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
450 return -1;
wdenk71f95112003-06-15 22:40:42 +0000451 slotptr++;
452 counter--;
453 }
454
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300455 if ((__u8 *)slotptr >= buflimit) {
wdenk71f95112003-06-15 22:40:42 +0000456 dir_slot *slotptr2;
457
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300458 if (curclust == 0)
459 return -1;
wdenk71f95112003-06-15 22:40:42 +0000460 curclust = get_fatent(mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100461 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200462 debug("curclust: 0x%x\n", curclust);
463 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000464 return -1;
465 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200466
wdenk5fa66df2003-10-29 23:18:55 +0000467 if (get_cluster(mydata, curclust, get_vfatname_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000468 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200469 debug("Error: reading directory block\n");
wdenk71f95112003-06-15 22:40:42 +0000470 return -1;
471 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200472
473 slotptr2 = (dir_slot *)get_vfatname_block;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300474 while (counter > 0) {
475 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
476 & 0xff) != counter)
477 return -1;
wdenk71f95112003-06-15 22:40:42 +0000478 slotptr2++;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300479 counter--;
480 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200481
wdenk71f95112003-06-15 22:40:42 +0000482 /* Save the real directory entry */
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300483 realdent = (dir_entry *)slotptr2;
484 while ((__u8 *)slotptr2 > get_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000485 slotptr2--;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300486 slot2str(slotptr2, l_name, &idx);
wdenk71f95112003-06-15 22:40:42 +0000487 }
488 } else {
489 /* Save the real directory entry */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200490 realdent = (dir_entry *)slotptr;
wdenk71f95112003-06-15 22:40:42 +0000491 }
492
493 do {
494 slotptr--;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200495 if (slot2str(slotptr, l_name, &idx))
496 break;
wdenk2d1a5372004-02-23 19:30:57 +0000497 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000498
499 l_name[idx] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +0200500 if (*l_name == DELETED_FLAG)
501 *l_name = '\0';
502 else if (*l_name == aRING)
503 *l_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000504 downcase(l_name);
505
506 /* Return the real directory entry */
507 memcpy(retdent, realdent, sizeof(dir_entry));
508
509 return 0;
510}
511
wdenk71f95112003-06-15 22:40:42 +0000512/* Calculate short name checksum */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200513static __u8 mkcksum (const char *str)
wdenk71f95112003-06-15 22:40:42 +0000514{
515 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200516
wdenk71f95112003-06-15 22:40:42 +0000517 __u8 ret = 0;
518
519 for (i = 0; i < 11; i++) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200520 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
wdenk71f95112003-06-15 22:40:42 +0000521 }
522
523 return ret;
524}
Wolfgang Denk7385c282010-07-19 11:37:00 +0200525#endif /* CONFIG_SUPPORT_VFAT */
wdenk71f95112003-06-15 22:40:42 +0000526
527/*
528 * Get the directory entry associated with 'filename' from the directory
529 * starting at 'startsect'
530 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200531__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
wdenk5fa66df2003-10-29 23:18:55 +0000532__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200533
534static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
535 char *filename, dir_entry *retdent,
wdenk71f95112003-06-15 22:40:42 +0000536 int dols)
537{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200538 __u16 prevcksum = 0xffff;
539 __u32 curclust = START(retdent);
540 int files = 0, dirs = 0;
wdenk71f95112003-06-15 22:40:42 +0000541
Wolfgang Denk7385c282010-07-19 11:37:00 +0200542 debug("get_dentfromdir: %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +0000543
Wolfgang Denk7385c282010-07-19 11:37:00 +0200544 while (1) {
545 dir_entry *dentptr;
wdenk71f95112003-06-15 22:40:42 +0000546
Wolfgang Denk7385c282010-07-19 11:37:00 +0200547 int i;
wdenk71f95112003-06-15 22:40:42 +0000548
Wolfgang Denk7385c282010-07-19 11:37:00 +0200549 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000550 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200551 debug("Error: reading directory block\n");
552 return NULL;
553 }
554
555 dentptr = (dir_entry *)get_dentfromdir_block;
556
557 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300558 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200559
560 l_name[0] = '\0';
561 if (dentptr->name[0] == DELETED_FLAG) {
562 dentptr++;
563 continue;
wdenk71f95112003-06-15 22:40:42 +0000564 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200565 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000566#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand206d68f2011-10-19 07:43:08 +0000567 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
568 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200569 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
570 get_vfatname(mydata, curclust,
571 get_dentfromdir_block,
572 dentptr, l_name);
573 if (dols) {
574 int isdir;
575 char dirc;
576 int doit = 0;
577
578 isdir = (dentptr->attr & ATTR_DIR);
579
580 if (isdir) {
581 dirs++;
582 dirc = '/';
583 doit = 1;
584 } else {
585 dirc = ' ';
586 if (l_name[0] != 0) {
587 files++;
588 doit = 1;
589 }
590 }
591 if (doit) {
592 if (dirc == ' ') {
593 printf(" %8ld %s%c\n",
594 (long)FAT2CPU32(dentptr->size),
595 l_name,
596 dirc);
597 } else {
598 printf(" %s%c\n",
599 l_name,
600 dirc);
601 }
602 }
603 dentptr++;
604 continue;
605 }
606 debug("vfatname: |%s|\n", l_name);
607 } else
wdenk71f95112003-06-15 22:40:42 +0000608#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200609 {
610 /* Volume label or VFAT entry */
611 dentptr++;
612 continue;
613 }
614 }
615 if (dentptr->name[0] == 0) {
616 if (dols) {
617 printf("\n%d file(s), %d dir(s)\n\n",
618 files, dirs);
619 }
620 debug("Dentname == NULL - %d\n", i);
621 return NULL;
622 }
623#ifdef CONFIG_SUPPORT_VFAT
624 if (dols && mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +0000625 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200626 dentptr++;
627 continue;
628 }
629#endif
630 get_name(dentptr, s_name);
631 if (dols) {
632 int isdir = (dentptr->attr & ATTR_DIR);
633 char dirc;
634 int doit = 0;
wdenk71f95112003-06-15 22:40:42 +0000635
Wolfgang Denk7385c282010-07-19 11:37:00 +0200636 if (isdir) {
637 dirs++;
638 dirc = '/';
639 doit = 1;
640 } else {
641 dirc = ' ';
642 if (s_name[0] != 0) {
643 files++;
644 doit = 1;
645 }
646 }
647
648 if (doit) {
649 if (dirc == ' ') {
650 printf(" %8ld %s%c\n",
651 (long)FAT2CPU32(dentptr->size),
652 s_name, dirc);
653 } else {
654 printf(" %s%c\n",
655 s_name, dirc);
656 }
657 }
658
659 dentptr++;
660 continue;
661 }
662
663 if (strcmp(filename, s_name)
664 && strcmp(filename, l_name)) {
665 debug("Mismatch: |%s|%s|\n", s_name, l_name);
666 dentptr++;
667 continue;
668 }
669
670 memcpy(retdent, dentptr, sizeof(dir_entry));
671
672 debug("DentName: %s", s_name);
673 debug(", start: 0x%x", START(dentptr));
674 debug(", size: 0x%x %s\n",
675 FAT2CPU32(dentptr->size),
676 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
677
678 return retdent;
wdenk71f95112003-06-15 22:40:42 +0000679 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200680
681 curclust = get_fatent(mydata, curclust);
682 if (CHECK_CLUST(curclust, mydata->fatsize)) {
683 debug("curclust: 0x%x\n", curclust);
684 printf("Invalid FAT entry\n");
685 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000686 }
wdenk71f95112003-06-15 22:40:42 +0000687 }
wdenk71f95112003-06-15 22:40:42 +0000688
Wolfgang Denk7385c282010-07-19 11:37:00 +0200689 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000690}
691
wdenk71f95112003-06-15 22:40:42 +0000692/*
693 * Read boot sector and volume info from a FAT filesystem
694 */
695static int
Wolfgang Denk7385c282010-07-19 11:37:00 +0200696read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000697{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000698 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000699 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000700 int ret = 0;
701
702 if (cur_dev == NULL) {
703 debug("Error: no device selected\n");
704 return -1;
705 }
706
707 block = malloc(cur_dev->blksz);
708 if (block == NULL) {
709 debug("Error: allocating block\n");
710 return -1;
711 }
wdenk71f95112003-06-15 22:40:42 +0000712
Wolfgang Denk7385c282010-07-19 11:37:00 +0200713 if (disk_read (0, 1, block) < 0) {
714 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000715 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000716 }
717
718 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200719 bs->reserved = FAT2CPU16(bs->reserved);
720 bs->fat_length = FAT2CPU16(bs->fat_length);
721 bs->secs_track = FAT2CPU16(bs->secs_track);
722 bs->heads = FAT2CPU16(bs->heads);
723 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000724
725 /* FAT32 entries */
726 if (bs->fat_length == 0) {
727 /* Assume FAT32 */
728 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200729 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000730 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200731 bs->info_sector = FAT2CPU16(bs->info_sector);
732 bs->backup_boot = FAT2CPU16(bs->backup_boot);
733 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000734 *fatsize = 32;
735 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200736 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000737 *fatsize = 0;
738 }
739 memcpy(volinfo, vistart, sizeof(volume_info));
740
wdenk71f95112003-06-15 22:40:42 +0000741 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200742 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000743 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000744 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500745 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000746 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000747 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000748 }
Tom Rix651351f2009-05-20 07:55:41 -0500749 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000750 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000751 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000752 }
753 }
754
Wolfgang Denk7385c282010-07-19 11:37:00 +0200755 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000756fail:
757 ret = -1;
758exit:
759 free(block);
760 return ret;
wdenk71f95112003-06-15 22:40:42 +0000761}
762
Wolfgang Denk7385c282010-07-19 11:37:00 +0200763__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
Bryan Wu7e4b9b42009-01-02 20:47:45 -0500764__u8 do_fat_read_block[MAX_CLUSTSIZE];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200765
stroese20cc00d2004-12-16 17:57:26 +0000766long
wdenk71f95112003-06-15 22:40:42 +0000767do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
768 int dols)
769{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200770 char fnamecopy[2048];
771 boot_sector bs;
772 volume_info volinfo;
773 fsdata datablock;
774 fsdata *mydata = &datablock;
775 dir_entry *dentptr;
776 __u16 prevcksum = 0xffff;
777 char *subname = "";
Erik Hansen3f270f42011-03-24 10:15:37 +0100778 __u32 cursect;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200779 int idx, isdir = 0;
780 int files = 0, dirs = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000781 long ret = -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200782 int firsttime;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000783 __u32 root_cluster = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +0100784 int rootdir_size = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200785 int j;
wdenk71f95112003-06-15 22:40:42 +0000786
Wolfgang Denk7385c282010-07-19 11:37:00 +0200787 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
788 debug("Error: reading boot sector\n");
789 return -1;
790 }
791
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000792 if (mydata->fatsize == 32) {
793 root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200794 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000795 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200796 mydata->fatlength = bs.fat_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000797 }
wdenk71f95112003-06-15 22:40:42 +0000798
Wolfgang Denk7385c282010-07-19 11:37:00 +0200799 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000800
Wolfgang Denk7385c282010-07-19 11:37:00 +0200801 cursect = mydata->rootdir_sect
802 = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000803
Sergei Shtylyovac497772011-08-08 09:38:33 +0000804 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200805 mydata->clust_size = bs.cluster_size;
wdenk71f95112003-06-15 22:40:42 +0000806
Wolfgang Denk7385c282010-07-19 11:37:00 +0200807 if (mydata->fatsize == 32) {
808 mydata->data_begin = mydata->rootdir_sect -
809 (mydata->clust_size * 2);
810 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200811 rootdir_size = ((bs.dir_entries[1] * (int)256 +
812 bs.dir_entries[0]) *
813 sizeof(dir_entry)) /
Sergei Shtylyovac497772011-08-08 09:38:33 +0000814 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200815 mydata->data_begin = mydata->rootdir_sect +
816 rootdir_size -
817 (mydata->clust_size * 2);
wdenk71f95112003-06-15 22:40:42 +0000818 }
wdenk71f95112003-06-15 22:40:42 +0000819
Wolfgang Denk7385c282010-07-19 11:37:00 +0200820 mydata->fatbufnum = -1;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000821 mydata->fatbuf = malloc(FATBUFSIZE);
822 if (mydata->fatbuf == NULL) {
823 debug("Error: allocating memory\n");
824 return -1;
825 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200826
wdenk71f95112003-06-15 22:40:42 +0000827#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk7385c282010-07-19 11:37:00 +0200828 debug("VFAT Support enabled\n");
wdenk71f95112003-06-15 22:40:42 +0000829#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200830 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
831 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
832 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
833 "Data begins at: %d\n",
834 root_cluster,
835 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000836 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
837 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
838 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200839
840 /* "cwd" is always the root... */
841 while (ISDIRDELIM(*filename))
842 filename++;
843
844 /* Make a copy of the filename and convert it to lowercase */
845 strcpy(fnamecopy, filename);
846 downcase(fnamecopy);
847
848 if (*fnamecopy == '\0') {
849 if (!dols)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000850 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200851
852 dols = LS_ROOT;
853 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
854 isdir = 1;
855 fnamecopy[idx] = '\0';
856 subname = fnamecopy + idx + 1;
857
858 /* Handle multiple delimiters */
859 while (ISDIRDELIM(*subname))
860 subname++;
861 } else if (dols) {
862 isdir = 1;
863 }
864
865 j = 0;
866 while (1) {
867 int i;
868
Andreas Bießmann2d1b83b2011-12-15 09:56:54 +0100869 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
Wolfgang Denk7385c282010-07-19 11:37:00 +0200870 cursect, mydata->clust_size, DIRENTSPERBLOCK);
871
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300872 if (disk_read(cursect,
873 (mydata->fatsize == 32) ?
874 (mydata->clust_size) :
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000875 PREFETCH_BLOCKS,
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300876 do_fat_read_block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200877 debug("Error: reading rootdir block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000878 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000879 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200880
881 dentptr = (dir_entry *) do_fat_read_block;
882
883 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300884 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200885
886 l_name[0] = '\0';
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300887 if (dentptr->name[0] == DELETED_FLAG) {
888 dentptr++;
889 continue;
890 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200891 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000892#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand206d68f2011-10-19 07:43:08 +0000893 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk7385c282010-07-19 11:37:00 +0200894 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
895 prevcksum =
896 ((dir_slot *)dentptr)->alias_checksum;
wdenk71f95112003-06-15 22:40:42 +0000897
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300898 get_vfatname(mydata,
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000899 root_cluster,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200900 do_fat_read_block,
901 dentptr, l_name);
902
903 if (dols == LS_ROOT) {
904 char dirc;
905 int doit = 0;
906 int isdir =
907 (dentptr->attr & ATTR_DIR);
908
909 if (isdir) {
910 dirs++;
911 dirc = '/';
912 doit = 1;
913 } else {
914 dirc = ' ';
915 if (l_name[0] != 0) {
916 files++;
917 doit = 1;
918 }
919 }
920 if (doit) {
921 if (dirc == ' ') {
922 printf(" %8ld %s%c\n",
923 (long)FAT2CPU32(dentptr->size),
924 l_name,
925 dirc);
926 } else {
927 printf(" %s%c\n",
928 l_name,
929 dirc);
930 }
931 }
932 dentptr++;
933 continue;
934 }
935 debug("Rootvfatname: |%s|\n",
936 l_name);
937 } else
938#endif
939 {
940 /* Volume label or VFAT entry */
941 dentptr++;
942 continue;
943 }
944 } else if (dentptr->name[0] == 0) {
945 debug("RootDentname == NULL - %d\n", i);
946 if (dols == LS_ROOT) {
947 printf("\n%d file(s), %d dir(s)\n\n",
948 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000949 ret = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200950 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000951 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200952 }
953#ifdef CONFIG_SUPPORT_VFAT
954 else if (dols == LS_ROOT &&
955 mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +0000956 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200957 dentptr++;
958 continue;
959 }
960#endif
961 get_name(dentptr, s_name);
962
963 if (dols == LS_ROOT) {
964 int isdir = (dentptr->attr & ATTR_DIR);
965 char dirc;
966 int doit = 0;
967
968 if (isdir) {
969 dirc = '/';
970 if (s_name[0] != 0) {
971 dirs++;
972 doit = 1;
973 }
974 } else {
975 dirc = ' ';
976 if (s_name[0] != 0) {
977 files++;
978 doit = 1;
979 }
980 }
981 if (doit) {
982 if (dirc == ' ') {
983 printf(" %8ld %s%c\n",
984 (long)FAT2CPU32(dentptr->size),
985 s_name, dirc);
986 } else {
987 printf(" %s%c\n",
988 s_name, dirc);
989 }
990 }
991 dentptr++;
992 continue;
993 }
994
995 if (strcmp(fnamecopy, s_name)
996 && strcmp(fnamecopy, l_name)) {
997 debug("RootMismatch: |%s|%s|\n", s_name,
998 l_name);
999 dentptr++;
1000 continue;
1001 }
1002
1003 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001004 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001005
1006 debug("RootName: %s", s_name);
1007 debug(", start: 0x%x", START(dentptr));
1008 debug(", size: 0x%x %s\n",
1009 FAT2CPU32(dentptr->size),
1010 isdir ? "(DIR)" : "");
1011
1012 goto rootdir_done; /* We got a match */
1013 }
1014 debug("END LOOP: j=%d clust_size=%d\n", j,
1015 mydata->clust_size);
1016
1017 /*
1018 * On FAT32 we must fetch the FAT entries for the next
1019 * root directory clusters when a cluster has been
1020 * completely processed.
1021 */
Erik Hansen3f270f42011-03-24 10:15:37 +01001022 ++j;
1023 int fat32_end = 0;
1024 if ((mydata->fatsize == 32) && (j == mydata->clust_size)) {
1025 int nxtsect = 0;
1026 int nxt_clust = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001027
1028 nxt_clust = get_fatent(mydata, root_cluster);
Erik Hansen3f270f42011-03-24 10:15:37 +01001029 fat32_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001030
1031 nxtsect = mydata->data_begin +
1032 (nxt_clust * mydata->clust_size);
1033
Wolfgang Denk7385c282010-07-19 11:37:00 +02001034 root_cluster = nxt_clust;
1035
1036 cursect = nxtsect;
1037 j = 0;
wdenk71f95112003-06-15 22:40:42 +00001038 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +02001039 cursect++;
wdenk71f95112003-06-15 22:40:42 +00001040 }
Erik Hansen3f270f42011-03-24 10:15:37 +01001041
1042 /* If end of rootdir reached */
1043 if ((mydata->fatsize == 32 && fat32_end) ||
1044 (mydata->fatsize != 32 && j == rootdir_size)) {
1045 if (dols == LS_ROOT) {
1046 printf("\n%d file(s), %d dir(s)\n\n",
1047 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +00001048 ret = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +01001049 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001050 goto exit;
Erik Hansen3f270f42011-03-24 10:15:37 +01001051 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001052 }
1053rootdir_done:
1054
1055 firsttime = 1;
1056
1057 while (isdir) {
1058 int startsect = mydata->data_begin
1059 + START(dentptr) * mydata->clust_size;
1060 dir_entry dent;
1061 char *nextname = NULL;
1062
1063 dent = *dentptr;
1064 dentptr = &dent;
1065
1066 idx = dirdelim(subname);
1067
1068 if (idx >= 0) {
1069 subname[idx] = '\0';
1070 nextname = subname + idx + 1;
1071 /* Handle multiple delimiters */
1072 while (ISDIRDELIM(*nextname))
1073 nextname++;
1074 if (dols && *nextname == '\0')
1075 firsttime = 0;
1076 } else {
1077 if (dols && firsttime) {
1078 firsttime = 0;
1079 } else {
1080 isdir = 0;
1081 }
wdenk71f95112003-06-15 22:40:42 +00001082 }
wdenk71f95112003-06-15 22:40:42 +00001083
Wolfgang Denk7385c282010-07-19 11:37:00 +02001084 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1085 isdir ? 0 : dols) == NULL) {
1086 if (dols && !isdir)
Sergei Shtylyovac497772011-08-08 09:38:33 +00001087 ret = 0;
1088 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001089 }
wdenk71f95112003-06-15 22:40:42 +00001090
Wolfgang Denk7385c282010-07-19 11:37:00 +02001091 if (idx >= 0) {
1092 if (!(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001093 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001094 subname = nextname;
1095 }
wdenk71f95112003-06-15 22:40:42 +00001096 }
1097
Wolfgang Denk7385c282010-07-19 11:37:00 +02001098 ret = get_contents(mydata, dentptr, buffer, maxsize);
1099 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
wdenk71f95112003-06-15 22:40:42 +00001100
Sergei Shtylyovac497772011-08-08 09:38:33 +00001101exit:
1102 free(mydata->fatbuf);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001103 return ret;
wdenk71f95112003-06-15 22:40:42 +00001104}
1105
Wolfgang Denk7385c282010-07-19 11:37:00 +02001106int file_fat_detectfs (void)
wdenk71f95112003-06-15 22:40:42 +00001107{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001108 boot_sector bs;
1109 volume_info volinfo;
1110 int fatsize;
1111 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001112
Wolfgang Denk7385c282010-07-19 11:37:00 +02001113 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001114 printf("No current device\n");
1115 return 1;
1116 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001117
Jon Loeligerdd60d122007-07-09 17:56:50 -05001118#if defined(CONFIG_CMD_IDE) || \
unsik Kim75eb82e2009-02-25 11:31:24 +09001119 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -05001120 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001121 defined(CONFIG_CMD_SCSI) || \
1122 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001123 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001124 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001125 switch (cur_dev->if_type) {
1126 case IF_TYPE_IDE:
1127 printf("IDE");
1128 break;
1129 case IF_TYPE_SATA:
1130 printf("SATA");
1131 break;
1132 case IF_TYPE_SCSI:
1133 printf("SCSI");
1134 break;
1135 case IF_TYPE_ATAPI:
1136 printf("ATAPI");
1137 break;
1138 case IF_TYPE_USB:
1139 printf("USB");
1140 break;
1141 case IF_TYPE_DOC:
1142 printf("DOC");
1143 break;
1144 case IF_TYPE_MMC:
1145 printf("MMC");
1146 break;
1147 default:
1148 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001149 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001150
1151 printf("\n Device %d: ", cur_dev->dev);
wdenk7205e402003-09-10 22:30:53 +00001152 dev_print(cur_dev);
1153#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001154
1155 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001156 printf("\nNo valid FAT fs found\n");
1157 return 1;
1158 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001159
1160 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001161 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001162 volinfo.fs_type[5] = '\0';
1163
Kyle Moffett9813b752011-12-21 07:08:10 +00001164 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part_nr,
Wolfgang Denk7385c282010-07-19 11:37:00 +02001165 volinfo.fs_type, vol_label);
1166
wdenk7205e402003-09-10 22:30:53 +00001167 return 0;
wdenk71f95112003-06-15 22:40:42 +00001168}
1169
Wolfgang Denk7385c282010-07-19 11:37:00 +02001170int file_fat_ls (const char *dir)
wdenk71f95112003-06-15 22:40:42 +00001171{
1172 return do_fat_read(dir, NULL, 0, LS_YES);
1173}
1174
Wolfgang Denk7385c282010-07-19 11:37:00 +02001175long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +00001176{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001177 printf("reading %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +00001178 return do_fat_read(filename, buffer, maxsize, LS_NO);
1179}