blob: a3085b51d99851b4db746368196d08ef664b0bdb [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 */
Wolfgang Denk7385c282010-07-19 11:37:00 +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
Wolfgang Denk7385c282010-07-19 11:37:00 +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) || \
unsik Kim75eb82e2009-02-25 11:31:24 +090073 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -050074 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -050075 defined(CONFIG_CMD_SCSI) || \
76 defined(CONFIG_CMD_USB) || \
Andy Fleming02df4a22008-01-09 13:51:32 -060077 defined(CONFIG_MMC) || \
78 defined(CONFIG_SYSTEMACE) )
Andy Fleming02df4a22008-01-09 13:51:32 -060079
Kyle Moffett9813b752011-12-21 07:08:10 +000080 /* Read the partition table, if present */
81 if (!get_partition_info(dev_desc, part_no, &cur_part_info)) {
82 cur_dev = dev_desc;
83 cur_part_nr = part_no;
Andy Fleming02df4a22008-01-09 13:51:32 -060084 }
85#endif
Kyle Moffett9813b752011-12-21 07:08:10 +000086
87 /* Otherwise it might be a superfloppy (whole-disk FAT filesystem) */
88 if (!cur_dev) {
89 if (part_no != 1) {
90 printf("** Partition %d not valid on device %d **\n",
91 part_no, dev_desc->dev);
92 return -1;
93 }
94
95 cur_dev = dev_desc;
96 cur_part_nr = 1;
97 cur_part_info.start = 0;
98 cur_part_info.size = dev_desc->lba;
99 cur_part_info.blksz = dev_desc->blksz;
100 memset(cur_part_info.name, 0, sizeof(cur_part_info.name));
101 memset(cur_part_info.type, 0, sizeof(cur_part_info.type));
102 }
103
104 /* Make sure it has a valid FAT header */
105 if (disk_read(0, 1, buffer) != 1) {
106 cur_dev = NULL;
107 return -1;
108 }
109
110 /* Check if it's actually a DOS volume */
111 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
112 cur_dev = NULL;
113 return -1;
114 }
115
116 /* Check for FAT12/FAT16/FAT32 filesystem */
117 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
118 return 0;
119 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
120 return 0;
121
122 cur_dev = NULL;
123 return -1;
wdenk71f95112003-06-15 22:40:42 +0000124}
125
Kyle Moffett9813b752011-12-21 07:08:10 +0000126
wdenk71f95112003-06-15 22:40:42 +0000127/*
128 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
129 * Return index into string if found, -1 otherwise.
130 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200131static int dirdelim (char *str)
wdenk71f95112003-06-15 22:40:42 +0000132{
133 char *start = str;
134
135 while (*str != '\0') {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200136 if (ISDIRDELIM(*str))
137 return str - start;
wdenk71f95112003-06-15 22:40:42 +0000138 str++;
139 }
140 return -1;
141}
142
wdenk71f95112003-06-15 22:40:42 +0000143/*
144 * Extract zero terminated short name from a directory entry.
145 */
146static void get_name (dir_entry *dirent, char *s_name)
147{
148 char *ptr;
149
Wolfgang Denk7385c282010-07-19 11:37:00 +0200150 memcpy(s_name, dirent->name, 8);
wdenk71f95112003-06-15 22:40:42 +0000151 s_name[8] = '\0';
152 ptr = s_name;
153 while (*ptr && *ptr != ' ')
154 ptr++;
155 if (dirent->ext[0] && dirent->ext[0] != ' ') {
156 *ptr = '.';
157 ptr++;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200158 memcpy(ptr, dirent->ext, 3);
wdenk71f95112003-06-15 22:40:42 +0000159 ptr[3] = '\0';
160 while (*ptr && *ptr != ' ')
161 ptr++;
162 }
163 *ptr = '\0';
164 if (*s_name == DELETED_FLAG)
165 *s_name = '\0';
166 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100167 *s_name = DELETED_FLAG;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200168 downcase(s_name);
wdenk71f95112003-06-15 22:40:42 +0000169}
170
171/*
172 * Get the entry at index 'entry' in a FAT (12/16/32) table.
173 * On failure 0x00 is returned.
174 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200175static __u32 get_fatent (fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000176{
177 __u32 bufnum;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200178 __u32 off16, offset;
wdenk71f95112003-06-15 22:40:42 +0000179 __u32 ret = 0x00;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200180 __u16 val1, val2;
wdenk71f95112003-06-15 22:40:42 +0000181
182 switch (mydata->fatsize) {
183 case 32:
184 bufnum = entry / FAT32BUFSIZE;
185 offset = entry - bufnum * FAT32BUFSIZE;
186 break;
187 case 16:
188 bufnum = entry / FAT16BUFSIZE;
189 offset = entry - bufnum * FAT16BUFSIZE;
190 break;
191 case 12:
192 bufnum = entry / FAT12BUFSIZE;
193 offset = entry - bufnum * FAT12BUFSIZE;
194 break;
195
196 default:
197 /* Unsupported FAT size */
198 return ret;
199 }
200
Wolfgang Denk7385c282010-07-19 11:37:00 +0200201 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
202 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200203
wdenk71f95112003-06-15 22:40:42 +0000204 /* Read a new block of FAT entries into the cache. */
205 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000206 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000207 __u8 *bufptr = mydata->fatbuf;
208 __u32 fatlength = mydata->fatlength;
209 __u32 startblock = bufnum * FATBUFBLOCKS;
210
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000211 if (getsize > fatlength)
212 getsize = fatlength;
213
Sergei Shtylyovac497772011-08-08 09:38:33 +0000214 fatlength *= mydata->sect_size; /* We want it in bytes now */
wdenk71f95112003-06-15 22:40:42 +0000215 startblock += mydata->fat_sect; /* Offset from start of disk */
216
wdenk71f95112003-06-15 22:40:42 +0000217 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200218 debug("Error reading FAT blocks\n");
wdenk71f95112003-06-15 22:40:42 +0000219 return ret;
220 }
221 mydata->fatbufnum = bufnum;
222 }
223
224 /* Get the actual entry from the table */
225 switch (mydata->fatsize) {
226 case 32:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200227 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000228 break;
229 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200230 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000231 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200232 case 12:
233 off16 = (offset * 3) / 4;
wdenk71f95112003-06-15 22:40:42 +0000234
235 switch (offset & 0x3) {
236 case 0:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200237 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000238 ret &= 0xfff;
239 break;
240 case 1:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200241 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000242 val1 &= 0xf000;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200243 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk71f95112003-06-15 22:40:42 +0000244 val2 &= 0x00ff;
245 ret = (val2 << 4) | (val1 >> 12);
246 break;
247 case 2:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200248 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000249 val1 &= 0xff00;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200250 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk71f95112003-06-15 22:40:42 +0000251 val2 &= 0x000f;
252 ret = (val2 << 8) | (val1 >> 8);
253 break;
254 case 3:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200255 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000256 ret = (ret & 0xfff0) >> 4;
257 break;
258 default:
259 break;
260 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200261 break;
wdenk71f95112003-06-15 22:40:42 +0000262 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200263 debug("FAT%d: ret: %08x, offset: %04x\n",
264 mydata->fatsize, ret, offset);
wdenk71f95112003-06-15 22:40:42 +0000265
266 return ret;
267}
268
wdenk71f95112003-06-15 22:40:42 +0000269/*
270 * Read at most 'size' bytes from the specified cluster into 'buffer'.
271 * Return 0 on success, -1 otherwise.
272 */
273static int
Wolfgang Denk7385c282010-07-19 11:37:00 +0200274get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
275 unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000276{
Erik Hansen3f270f42011-03-24 10:15:37 +0100277 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000278 __u32 startsect;
Kyle Moffett46236b12011-12-20 07:41:13 +0000279 __u32 nr_sect;
280 int ret;
wdenk71f95112003-06-15 22:40:42 +0000281
282 if (clustnum > 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200283 startsect = mydata->data_begin +
284 clustnum * mydata->clust_size;
wdenk71f95112003-06-15 22:40:42 +0000285 } else {
286 startsect = mydata->rootdir_sect;
287 }
288
Wolfgang Denk7385c282010-07-19 11:37:00 +0200289 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
290
Kyle Moffett46236b12011-12-20 07:41:13 +0000291 nr_sect = size / mydata->sect_size;
292 ret = disk_read(startsect, nr_sect, buffer);
293 if (ret != nr_sect) {
294 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000295 return -1;
296 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000297 if (size % mydata->sect_size) {
Eric Nelson9a800ac2012-04-11 04:08:53 +0000298 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200299
Sergei Shtylyovac497772011-08-08 09:38:33 +0000300 idx = size / mydata->sect_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000301 ret = disk_read(startsect + idx, 1, tmpbuf);
302 if (ret != 1) {
303 debug("Error reading data (got %d)\n", ret);
wdenk7205e402003-09-10 22:30:53 +0000304 return -1;
wdenk71f95112003-06-15 22:40:42 +0000305 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000306 buffer += idx * mydata->sect_size;
wdenk7205e402003-09-10 22:30:53 +0000307
Sergei Shtylyovac497772011-08-08 09:38:33 +0000308 memcpy(buffer, tmpbuf, size % mydata->sect_size);
wdenk7205e402003-09-10 22:30:53 +0000309 return 0;
wdenk71f95112003-06-15 22:40:42 +0000310 }
311
312 return 0;
313}
314
wdenk71f95112003-06-15 22:40:42 +0000315/*
316 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
317 * into 'buffer'.
318 * Return the number of bytes read or -1 on fatal errors.
319 */
320static long
Wolfgang Denk7385c282010-07-19 11:37:00 +0200321get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
322 unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +0000323{
324 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000325 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000326 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000327 __u32 endclust, newclust;
328 unsigned long actsize;
wdenk71f95112003-06-15 22:40:42 +0000329
Wolfgang Denk7385c282010-07-19 11:37:00 +0200330 debug("Filesize: %ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000331
Wolfgang Denk7385c282010-07-19 11:37:00 +0200332 if (maxsize > 0 && filesize > maxsize)
333 filesize = maxsize;
wdenk71f95112003-06-15 22:40:42 +0000334
Wolfgang Denk7385c282010-07-19 11:37:00 +0200335 debug("%ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000336
Wolfgang Denk7385c282010-07-19 11:37:00 +0200337 actsize = bytesperclust;
338 endclust = curclust;
339
wdenk71f95112003-06-15 22:40:42 +0000340 do {
wdenk7205e402003-09-10 22:30:53 +0000341 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200342 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000343 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200344 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000345 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100346 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200347 debug("curclust: 0x%x\n", newclust);
348 debug("Invalid FAT entry\n");
wdenk7205e402003-09-10 22:30:53 +0000349 return gotsize;
350 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200351 endclust = newclust;
352 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000353 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200354
wdenk7205e402003-09-10 22:30:53 +0000355 /* actsize >= file size */
356 actsize -= bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200357
wdenk7205e402003-09-10 22:30:53 +0000358 /* get remaining clusters */
359 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200360 printf("Error reading cluster\n");
wdenk71f95112003-06-15 22:40:42 +0000361 return -1;
362 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200363
wdenk7205e402003-09-10 22:30:53 +0000364 /* get remaining bytes */
365 gotsize += (int)actsize;
366 filesize -= actsize;
367 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200368 actsize = filesize;
wdenk7205e402003-09-10 22:30:53 +0000369 if (get_cluster(mydata, endclust, 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 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200373 gotsize += actsize;
wdenk7205e402003-09-10 22:30:53 +0000374 return gotsize;
375getit:
376 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200377 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000378 return -1;
379 }
380 gotsize += (int)actsize;
381 filesize -= actsize;
382 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200383
wdenk7205e402003-09-10 22:30:53 +0000384 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100385 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200386 debug("curclust: 0x%x\n", curclust);
387 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000388 return gotsize;
389 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200390 actsize = bytesperclust;
391 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000392 } while (1);
393}
394
wdenk71f95112003-06-15 22:40:42 +0000395#ifdef CONFIG_SUPPORT_VFAT
396/*
397 * Extract the file name information from 'slotptr' into 'l_name',
398 * starting at l_name[*idx].
399 * Return 1 if terminator (zero byte) is found, 0 otherwise.
400 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200401static int slot2str (dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000402{
403 int j;
404
405 for (j = 0; j <= 8; j += 2) {
406 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200407 if (l_name[*idx] == 0x00)
408 return 1;
wdenk71f95112003-06-15 22:40:42 +0000409 (*idx)++;
410 }
411 for (j = 0; j <= 10; j += 2) {
412 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200413 if (l_name[*idx] == 0x00)
414 return 1;
wdenk71f95112003-06-15 22:40:42 +0000415 (*idx)++;
416 }
417 for (j = 0; j <= 2; j += 2) {
418 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200419 if (l_name[*idx] == 0x00)
420 return 1;
wdenk71f95112003-06-15 22:40:42 +0000421 (*idx)++;
422 }
423
424 return 0;
425}
426
wdenk71f95112003-06-15 22:40:42 +0000427/*
428 * Extract the full long filename starting at 'retdent' (which is really
429 * a slot) into 'l_name'. If successful also copy the real directory entry
430 * into 'retdent'
431 * Return 0 on success, -1 otherwise.
432 */
Eric Nelson9a800ac2012-04-11 04:08:53 +0000433__u8 get_vfatname_block[MAX_CLUSTSIZE]
434 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200435
wdenk71f95112003-06-15 22:40:42 +0000436static int
Wolfgang Denk7385c282010-07-19 11:37:00 +0200437get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
438 dir_entry *retdent, char *l_name)
wdenk71f95112003-06-15 22:40:42 +0000439{
440 dir_entry *realdent;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200441 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000442 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
443 PREFETCH_BLOCKS :
444 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200445 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000446 int idx = 0;
447
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300448 if (counter > VFAT_MAXSEQ) {
449 debug("Error: VFAT name is too long\n");
450 return -1;
451 }
452
453 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200454 if (counter == 0)
455 break;
wdenk2d1a5372004-02-23 19:30:57 +0000456 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
457 return -1;
wdenk71f95112003-06-15 22:40:42 +0000458 slotptr++;
459 counter--;
460 }
461
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300462 if ((__u8 *)slotptr >= buflimit) {
wdenk71f95112003-06-15 22:40:42 +0000463 dir_slot *slotptr2;
464
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300465 if (curclust == 0)
466 return -1;
wdenk71f95112003-06-15 22:40:42 +0000467 curclust = get_fatent(mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100468 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200469 debug("curclust: 0x%x\n", curclust);
470 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000471 return -1;
472 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200473
wdenk5fa66df2003-10-29 23:18:55 +0000474 if (get_cluster(mydata, curclust, get_vfatname_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000475 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200476 debug("Error: reading directory block\n");
wdenk71f95112003-06-15 22:40:42 +0000477 return -1;
478 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200479
480 slotptr2 = (dir_slot *)get_vfatname_block;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300481 while (counter > 0) {
482 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
483 & 0xff) != counter)
484 return -1;
wdenk71f95112003-06-15 22:40:42 +0000485 slotptr2++;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300486 counter--;
487 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200488
wdenk71f95112003-06-15 22:40:42 +0000489 /* Save the real directory entry */
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300490 realdent = (dir_entry *)slotptr2;
491 while ((__u8 *)slotptr2 > get_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000492 slotptr2--;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300493 slot2str(slotptr2, l_name, &idx);
wdenk71f95112003-06-15 22:40:42 +0000494 }
495 } else {
496 /* Save the real directory entry */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200497 realdent = (dir_entry *)slotptr;
wdenk71f95112003-06-15 22:40:42 +0000498 }
499
500 do {
501 slotptr--;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200502 if (slot2str(slotptr, l_name, &idx))
503 break;
wdenk2d1a5372004-02-23 19:30:57 +0000504 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000505
506 l_name[idx] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +0200507 if (*l_name == DELETED_FLAG)
508 *l_name = '\0';
509 else if (*l_name == aRING)
510 *l_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000511 downcase(l_name);
512
513 /* Return the real directory entry */
514 memcpy(retdent, realdent, sizeof(dir_entry));
515
516 return 0;
517}
518
wdenk71f95112003-06-15 22:40:42 +0000519/* Calculate short name checksum */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200520static __u8 mkcksum (const char *str)
wdenk71f95112003-06-15 22:40:42 +0000521{
522 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200523
wdenk71f95112003-06-15 22:40:42 +0000524 __u8 ret = 0;
525
526 for (i = 0; i < 11; i++) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200527 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
wdenk71f95112003-06-15 22:40:42 +0000528 }
529
530 return ret;
531}
Wolfgang Denk7385c282010-07-19 11:37:00 +0200532#endif /* CONFIG_SUPPORT_VFAT */
wdenk71f95112003-06-15 22:40:42 +0000533
534/*
535 * Get the directory entry associated with 'filename' from the directory
536 * starting at 'startsect'
537 */
Eric Nelson9a800ac2012-04-11 04:08:53 +0000538__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
539 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200540
541static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
542 char *filename, dir_entry *retdent,
wdenk71f95112003-06-15 22:40:42 +0000543 int dols)
544{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200545 __u16 prevcksum = 0xffff;
546 __u32 curclust = START(retdent);
547 int files = 0, dirs = 0;
wdenk71f95112003-06-15 22:40:42 +0000548
Wolfgang Denk7385c282010-07-19 11:37:00 +0200549 debug("get_dentfromdir: %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +0000550
Wolfgang Denk7385c282010-07-19 11:37:00 +0200551 while (1) {
552 dir_entry *dentptr;
wdenk71f95112003-06-15 22:40:42 +0000553
Wolfgang Denk7385c282010-07-19 11:37:00 +0200554 int i;
wdenk71f95112003-06-15 22:40:42 +0000555
Wolfgang Denk7385c282010-07-19 11:37:00 +0200556 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000557 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200558 debug("Error: reading directory block\n");
559 return NULL;
560 }
561
562 dentptr = (dir_entry *)get_dentfromdir_block;
563
564 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300565 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200566
567 l_name[0] = '\0';
568 if (dentptr->name[0] == DELETED_FLAG) {
569 dentptr++;
570 continue;
wdenk71f95112003-06-15 22:40:42 +0000571 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200572 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000573#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand206d68f2011-10-19 07:43:08 +0000574 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
575 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200576 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
577 get_vfatname(mydata, curclust,
578 get_dentfromdir_block,
579 dentptr, l_name);
580 if (dols) {
581 int isdir;
582 char dirc;
583 int doit = 0;
584
585 isdir = (dentptr->attr & ATTR_DIR);
586
587 if (isdir) {
588 dirs++;
589 dirc = '/';
590 doit = 1;
591 } else {
592 dirc = ' ';
593 if (l_name[0] != 0) {
594 files++;
595 doit = 1;
596 }
597 }
598 if (doit) {
599 if (dirc == ' ') {
600 printf(" %8ld %s%c\n",
601 (long)FAT2CPU32(dentptr->size),
602 l_name,
603 dirc);
604 } else {
605 printf(" %s%c\n",
606 l_name,
607 dirc);
608 }
609 }
610 dentptr++;
611 continue;
612 }
613 debug("vfatname: |%s|\n", l_name);
614 } else
wdenk71f95112003-06-15 22:40:42 +0000615#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200616 {
617 /* Volume label or VFAT entry */
618 dentptr++;
619 continue;
620 }
621 }
622 if (dentptr->name[0] == 0) {
623 if (dols) {
624 printf("\n%d file(s), %d dir(s)\n\n",
625 files, dirs);
626 }
627 debug("Dentname == NULL - %d\n", i);
628 return NULL;
629 }
630#ifdef CONFIG_SUPPORT_VFAT
631 if (dols && mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +0000632 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200633 dentptr++;
634 continue;
635 }
636#endif
637 get_name(dentptr, s_name);
638 if (dols) {
639 int isdir = (dentptr->attr & ATTR_DIR);
640 char dirc;
641 int doit = 0;
wdenk71f95112003-06-15 22:40:42 +0000642
Wolfgang Denk7385c282010-07-19 11:37:00 +0200643 if (isdir) {
644 dirs++;
645 dirc = '/';
646 doit = 1;
647 } else {
648 dirc = ' ';
649 if (s_name[0] != 0) {
650 files++;
651 doit = 1;
652 }
653 }
654
655 if (doit) {
656 if (dirc == ' ') {
657 printf(" %8ld %s%c\n",
658 (long)FAT2CPU32(dentptr->size),
659 s_name, dirc);
660 } else {
661 printf(" %s%c\n",
662 s_name, dirc);
663 }
664 }
665
666 dentptr++;
667 continue;
668 }
669
670 if (strcmp(filename, s_name)
671 && strcmp(filename, l_name)) {
672 debug("Mismatch: |%s|%s|\n", s_name, l_name);
673 dentptr++;
674 continue;
675 }
676
677 memcpy(retdent, dentptr, sizeof(dir_entry));
678
679 debug("DentName: %s", s_name);
680 debug(", start: 0x%x", START(dentptr));
681 debug(", size: 0x%x %s\n",
682 FAT2CPU32(dentptr->size),
683 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
684
685 return retdent;
wdenk71f95112003-06-15 22:40:42 +0000686 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200687
688 curclust = get_fatent(mydata, curclust);
689 if (CHECK_CLUST(curclust, mydata->fatsize)) {
690 debug("curclust: 0x%x\n", curclust);
691 printf("Invalid FAT entry\n");
692 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000693 }
wdenk71f95112003-06-15 22:40:42 +0000694 }
wdenk71f95112003-06-15 22:40:42 +0000695
Wolfgang Denk7385c282010-07-19 11:37:00 +0200696 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000697}
698
wdenk71f95112003-06-15 22:40:42 +0000699/*
700 * Read boot sector and volume info from a FAT filesystem
701 */
702static int
Wolfgang Denk7385c282010-07-19 11:37:00 +0200703read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000704{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000705 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000706 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000707 int ret = 0;
708
709 if (cur_dev == NULL) {
710 debug("Error: no device selected\n");
711 return -1;
712 }
713
Eric Nelson9a800ac2012-04-11 04:08:53 +0000714 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000715 if (block == NULL) {
716 debug("Error: allocating block\n");
717 return -1;
718 }
wdenk71f95112003-06-15 22:40:42 +0000719
Wolfgang Denk7385c282010-07-19 11:37:00 +0200720 if (disk_read (0, 1, block) < 0) {
721 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000722 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000723 }
724
725 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200726 bs->reserved = FAT2CPU16(bs->reserved);
727 bs->fat_length = FAT2CPU16(bs->fat_length);
728 bs->secs_track = FAT2CPU16(bs->secs_track);
729 bs->heads = FAT2CPU16(bs->heads);
730 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000731
732 /* FAT32 entries */
733 if (bs->fat_length == 0) {
734 /* Assume FAT32 */
735 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200736 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000737 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200738 bs->info_sector = FAT2CPU16(bs->info_sector);
739 bs->backup_boot = FAT2CPU16(bs->backup_boot);
740 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000741 *fatsize = 32;
742 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200743 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000744 *fatsize = 0;
745 }
746 memcpy(volinfo, vistart, sizeof(volume_info));
747
wdenk71f95112003-06-15 22:40:42 +0000748 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200749 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000750 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000751 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500752 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000753 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000754 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000755 }
Tom Rix651351f2009-05-20 07:55:41 -0500756 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000757 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000758 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000759 }
760 }
761
Wolfgang Denk7385c282010-07-19 11:37:00 +0200762 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000763fail:
764 ret = -1;
765exit:
766 free(block);
767 return ret;
wdenk71f95112003-06-15 22:40:42 +0000768}
769
Eric Nelson9a800ac2012-04-11 04:08:53 +0000770__u8 do_fat_read_block[MAX_CLUSTSIZE]
771 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200772
stroese20cc00d2004-12-16 17:57:26 +0000773long
wdenk71f95112003-06-15 22:40:42 +0000774do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
775 int dols)
776{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200777 char fnamecopy[2048];
778 boot_sector bs;
779 volume_info volinfo;
780 fsdata datablock;
781 fsdata *mydata = &datablock;
782 dir_entry *dentptr;
783 __u16 prevcksum = 0xffff;
784 char *subname = "";
Erik Hansen3f270f42011-03-24 10:15:37 +0100785 __u32 cursect;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200786 int idx, isdir = 0;
787 int files = 0, dirs = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000788 long ret = -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200789 int firsttime;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000790 __u32 root_cluster = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +0100791 int rootdir_size = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200792 int j;
wdenk71f95112003-06-15 22:40:42 +0000793
Wolfgang Denk7385c282010-07-19 11:37:00 +0200794 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
795 debug("Error: reading boot sector\n");
796 return -1;
797 }
798
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000799 if (mydata->fatsize == 32) {
800 root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200801 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000802 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200803 mydata->fatlength = bs.fat_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000804 }
wdenk71f95112003-06-15 22:40:42 +0000805
Wolfgang Denk7385c282010-07-19 11:37:00 +0200806 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000807
Wolfgang Denk7385c282010-07-19 11:37:00 +0200808 cursect = mydata->rootdir_sect
809 = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000810
Sergei Shtylyovac497772011-08-08 09:38:33 +0000811 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200812 mydata->clust_size = bs.cluster_size;
Kyle Moffett46236b12011-12-20 07:41:13 +0000813 if (mydata->sect_size != cur_part_info.blksz) {
814 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
815 mydata->sect_size, cur_part_info.blksz);
816 return -1;
817 }
wdenk71f95112003-06-15 22:40:42 +0000818
Wolfgang Denk7385c282010-07-19 11:37:00 +0200819 if (mydata->fatsize == 32) {
820 mydata->data_begin = mydata->rootdir_sect -
821 (mydata->clust_size * 2);
822 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200823 rootdir_size = ((bs.dir_entries[1] * (int)256 +
824 bs.dir_entries[0]) *
825 sizeof(dir_entry)) /
Sergei Shtylyovac497772011-08-08 09:38:33 +0000826 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200827 mydata->data_begin = mydata->rootdir_sect +
828 rootdir_size -
829 (mydata->clust_size * 2);
wdenk71f95112003-06-15 22:40:42 +0000830 }
wdenk71f95112003-06-15 22:40:42 +0000831
Wolfgang Denk7385c282010-07-19 11:37:00 +0200832 mydata->fatbufnum = -1;
Eric Nelson9a800ac2012-04-11 04:08:53 +0000833 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000834 if (mydata->fatbuf == NULL) {
835 debug("Error: allocating memory\n");
836 return -1;
837 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200838
wdenk71f95112003-06-15 22:40:42 +0000839#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk7385c282010-07-19 11:37:00 +0200840 debug("VFAT Support enabled\n");
wdenk71f95112003-06-15 22:40:42 +0000841#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200842 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
843 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
844 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
845 "Data begins at: %d\n",
846 root_cluster,
847 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000848 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
849 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
850 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200851
852 /* "cwd" is always the root... */
853 while (ISDIRDELIM(*filename))
854 filename++;
855
856 /* Make a copy of the filename and convert it to lowercase */
857 strcpy(fnamecopy, filename);
858 downcase(fnamecopy);
859
860 if (*fnamecopy == '\0') {
861 if (!dols)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000862 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200863
864 dols = LS_ROOT;
865 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
866 isdir = 1;
867 fnamecopy[idx] = '\0';
868 subname = fnamecopy + idx + 1;
869
870 /* Handle multiple delimiters */
871 while (ISDIRDELIM(*subname))
872 subname++;
873 } else if (dols) {
874 isdir = 1;
875 }
876
877 j = 0;
878 while (1) {
879 int i;
880
Andreas Bießmann2d1b83b2011-12-15 09:56:54 +0100881 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
Wolfgang Denk7385c282010-07-19 11:37:00 +0200882 cursect, mydata->clust_size, DIRENTSPERBLOCK);
883
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300884 if (disk_read(cursect,
885 (mydata->fatsize == 32) ?
886 (mydata->clust_size) :
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000887 PREFETCH_BLOCKS,
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300888 do_fat_read_block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200889 debug("Error: reading rootdir block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000890 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000891 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200892
893 dentptr = (dir_entry *) do_fat_read_block;
894
895 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300896 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200897
898 l_name[0] = '\0';
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300899 if (dentptr->name[0] == DELETED_FLAG) {
900 dentptr++;
901 continue;
902 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200903 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000904#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand206d68f2011-10-19 07:43:08 +0000905 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk7385c282010-07-19 11:37:00 +0200906 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
907 prevcksum =
908 ((dir_slot *)dentptr)->alias_checksum;
wdenk71f95112003-06-15 22:40:42 +0000909
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300910 get_vfatname(mydata,
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000911 root_cluster,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200912 do_fat_read_block,
913 dentptr, l_name);
914
915 if (dols == LS_ROOT) {
916 char dirc;
917 int doit = 0;
918 int isdir =
919 (dentptr->attr & ATTR_DIR);
920
921 if (isdir) {
922 dirs++;
923 dirc = '/';
924 doit = 1;
925 } else {
926 dirc = ' ';
927 if (l_name[0] != 0) {
928 files++;
929 doit = 1;
930 }
931 }
932 if (doit) {
933 if (dirc == ' ') {
934 printf(" %8ld %s%c\n",
935 (long)FAT2CPU32(dentptr->size),
936 l_name,
937 dirc);
938 } else {
939 printf(" %s%c\n",
940 l_name,
941 dirc);
942 }
943 }
944 dentptr++;
945 continue;
946 }
947 debug("Rootvfatname: |%s|\n",
948 l_name);
949 } else
950#endif
951 {
952 /* Volume label or VFAT entry */
953 dentptr++;
954 continue;
955 }
956 } else if (dentptr->name[0] == 0) {
957 debug("RootDentname == NULL - %d\n", i);
958 if (dols == LS_ROOT) {
959 printf("\n%d file(s), %d dir(s)\n\n",
960 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000961 ret = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200962 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000963 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200964 }
965#ifdef CONFIG_SUPPORT_VFAT
966 else if (dols == LS_ROOT &&
967 mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyovbf34e7d2012-01-02 06:54:29 +0000968 prevcksum = 0xffff;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200969 dentptr++;
970 continue;
971 }
972#endif
973 get_name(dentptr, s_name);
974
975 if (dols == LS_ROOT) {
976 int isdir = (dentptr->attr & ATTR_DIR);
977 char dirc;
978 int doit = 0;
979
980 if (isdir) {
981 dirc = '/';
982 if (s_name[0] != 0) {
983 dirs++;
984 doit = 1;
985 }
986 } else {
987 dirc = ' ';
988 if (s_name[0] != 0) {
989 files++;
990 doit = 1;
991 }
992 }
993 if (doit) {
994 if (dirc == ' ') {
995 printf(" %8ld %s%c\n",
996 (long)FAT2CPU32(dentptr->size),
997 s_name, dirc);
998 } else {
999 printf(" %s%c\n",
1000 s_name, dirc);
1001 }
1002 }
1003 dentptr++;
1004 continue;
1005 }
1006
1007 if (strcmp(fnamecopy, s_name)
1008 && strcmp(fnamecopy, l_name)) {
1009 debug("RootMismatch: |%s|%s|\n", s_name,
1010 l_name);
1011 dentptr++;
1012 continue;
1013 }
1014
1015 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001016 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001017
1018 debug("RootName: %s", s_name);
1019 debug(", start: 0x%x", START(dentptr));
1020 debug(", size: 0x%x %s\n",
1021 FAT2CPU32(dentptr->size),
1022 isdir ? "(DIR)" : "");
1023
1024 goto rootdir_done; /* We got a match */
1025 }
1026 debug("END LOOP: j=%d clust_size=%d\n", j,
1027 mydata->clust_size);
1028
1029 /*
1030 * On FAT32 we must fetch the FAT entries for the next
1031 * root directory clusters when a cluster has been
1032 * completely processed.
1033 */
Erik Hansen3f270f42011-03-24 10:15:37 +01001034 ++j;
1035 int fat32_end = 0;
1036 if ((mydata->fatsize == 32) && (j == mydata->clust_size)) {
1037 int nxtsect = 0;
1038 int nxt_clust = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001039
1040 nxt_clust = get_fatent(mydata, root_cluster);
Erik Hansen3f270f42011-03-24 10:15:37 +01001041 fat32_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001042
1043 nxtsect = mydata->data_begin +
1044 (nxt_clust * mydata->clust_size);
1045
Wolfgang Denk7385c282010-07-19 11:37:00 +02001046 root_cluster = nxt_clust;
1047
1048 cursect = nxtsect;
1049 j = 0;
wdenk71f95112003-06-15 22:40:42 +00001050 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +02001051 cursect++;
wdenk71f95112003-06-15 22:40:42 +00001052 }
Erik Hansen3f270f42011-03-24 10:15:37 +01001053
1054 /* If end of rootdir reached */
1055 if ((mydata->fatsize == 32 && fat32_end) ||
1056 (mydata->fatsize != 32 && j == rootdir_size)) {
1057 if (dols == LS_ROOT) {
1058 printf("\n%d file(s), %d dir(s)\n\n",
1059 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +00001060 ret = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +01001061 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001062 goto exit;
Erik Hansen3f270f42011-03-24 10:15:37 +01001063 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001064 }
1065rootdir_done:
1066
1067 firsttime = 1;
1068
1069 while (isdir) {
1070 int startsect = mydata->data_begin
1071 + START(dentptr) * mydata->clust_size;
1072 dir_entry dent;
1073 char *nextname = NULL;
1074
1075 dent = *dentptr;
1076 dentptr = &dent;
1077
1078 idx = dirdelim(subname);
1079
1080 if (idx >= 0) {
1081 subname[idx] = '\0';
1082 nextname = subname + idx + 1;
1083 /* Handle multiple delimiters */
1084 while (ISDIRDELIM(*nextname))
1085 nextname++;
1086 if (dols && *nextname == '\0')
1087 firsttime = 0;
1088 } else {
1089 if (dols && firsttime) {
1090 firsttime = 0;
1091 } else {
1092 isdir = 0;
1093 }
wdenk71f95112003-06-15 22:40:42 +00001094 }
wdenk71f95112003-06-15 22:40:42 +00001095
Wolfgang Denk7385c282010-07-19 11:37:00 +02001096 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1097 isdir ? 0 : dols) == NULL) {
1098 if (dols && !isdir)
Sergei Shtylyovac497772011-08-08 09:38:33 +00001099 ret = 0;
1100 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001101 }
wdenk71f95112003-06-15 22:40:42 +00001102
Wolfgang Denk7385c282010-07-19 11:37:00 +02001103 if (idx >= 0) {
1104 if (!(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001105 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001106 subname = nextname;
1107 }
wdenk71f95112003-06-15 22:40:42 +00001108 }
1109
Wolfgang Denk7385c282010-07-19 11:37:00 +02001110 ret = get_contents(mydata, dentptr, buffer, maxsize);
1111 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
wdenk71f95112003-06-15 22:40:42 +00001112
Sergei Shtylyovac497772011-08-08 09:38:33 +00001113exit:
1114 free(mydata->fatbuf);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001115 return ret;
wdenk71f95112003-06-15 22:40:42 +00001116}
1117
Wolfgang Denk7385c282010-07-19 11:37:00 +02001118int file_fat_detectfs (void)
wdenk71f95112003-06-15 22:40:42 +00001119{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001120 boot_sector bs;
1121 volume_info volinfo;
1122 int fatsize;
1123 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001124
Wolfgang Denk7385c282010-07-19 11:37:00 +02001125 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001126 printf("No current device\n");
1127 return 1;
1128 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001129
Jon Loeligerdd60d122007-07-09 17:56:50 -05001130#if defined(CONFIG_CMD_IDE) || \
unsik Kim75eb82e2009-02-25 11:31:24 +09001131 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -05001132 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001133 defined(CONFIG_CMD_SCSI) || \
1134 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001135 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001136 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001137 switch (cur_dev->if_type) {
1138 case IF_TYPE_IDE:
1139 printf("IDE");
1140 break;
1141 case IF_TYPE_SATA:
1142 printf("SATA");
1143 break;
1144 case IF_TYPE_SCSI:
1145 printf("SCSI");
1146 break;
1147 case IF_TYPE_ATAPI:
1148 printf("ATAPI");
1149 break;
1150 case IF_TYPE_USB:
1151 printf("USB");
1152 break;
1153 case IF_TYPE_DOC:
1154 printf("DOC");
1155 break;
1156 case IF_TYPE_MMC:
1157 printf("MMC");
1158 break;
1159 default:
1160 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001161 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001162
1163 printf("\n Device %d: ", cur_dev->dev);
wdenk7205e402003-09-10 22:30:53 +00001164 dev_print(cur_dev);
1165#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001166
1167 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001168 printf("\nNo valid FAT fs found\n");
1169 return 1;
1170 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001171
1172 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001173 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001174 volinfo.fs_type[5] = '\0';
1175
Kyle Moffett9813b752011-12-21 07:08:10 +00001176 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part_nr,
Wolfgang Denk7385c282010-07-19 11:37:00 +02001177 volinfo.fs_type, vol_label);
1178
wdenk7205e402003-09-10 22:30:53 +00001179 return 0;
wdenk71f95112003-06-15 22:40:42 +00001180}
1181
Wolfgang Denk7385c282010-07-19 11:37:00 +02001182int file_fat_ls (const char *dir)
wdenk71f95112003-06-15 22:40:42 +00001183{
1184 return do_fat_read(dir, NULL, 0, LS_YES);
1185}
1186
Wolfgang Denk7385c282010-07-19 11:37:00 +02001187long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +00001188{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001189 printf("reading %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +00001190 return do_fat_read(filename, buffer, maxsize, LS_NO);
1191}