blob: 9f835725247eaae1b96250b3c2b14eadcd34e980 [file] [log] [blame]
wdenk7a428cc2003-06-15 22:40:42 +00001/*
2 * fat.c
3 *
4 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
5 *
6 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
7 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
29#include <config.h>
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +000030#include <exports.h>
wdenk7a428cc2003-06-15 22:40:42 +000031#include <fat.h>
32#include <asm/byteorder.h>
wdenk2c9b05d2003-09-10 22:30:53 +000033#include <part.h>
Eric Nelson014705b2012-04-11 04:08:53 +000034#include <malloc.h>
35#include <linux/compiler.h>
wdenk7a428cc2003-06-15 22:40:42 +000036
wdenk7a428cc2003-06-15 22:40:42 +000037/*
38 * Convert a string to lowercase.
39 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +020040static void downcase(char *str)
wdenk7a428cc2003-06-15 22:40:42 +000041{
42 while (*str != '\0') {
43 TOLOWER(*str);
44 str++;
45 }
46}
47
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000048static block_dev_desc_t *cur_dev;
49static unsigned int cur_part_nr;
50static disk_partition_t cur_part_info;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020051
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000052#define DOS_BOOT_MAGIC_OFFSET 0x1fe
wdenk2c9b05d2003-09-10 22:30:53 +000053#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020054#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000055
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000056static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
wdenk7a428cc2003-06-15 22:40:42 +000057{
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000058 if (!cur_dev || !cur_dev->block_read)
wdenk2c9b05d2003-09-10 22:30:53 +000059 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020060
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000061 return cur_dev->block_read(cur_dev->dev,
62 cur_part_info.start + block, nr_blocks, buf);
wdenk7a428cc2003-06-15 22:40:42 +000063}
64
Benoît Thébaudeau0a120372012-07-20 15:18:44 +020065int fat_register_device(block_dev_desc_t * dev_desc, int part_no)
wdenk7a428cc2003-06-15 22:40:42 +000066{
Eric Nelson014705b2012-04-11 04:08:53 +000067 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenk2c9b05d2003-09-10 22:30:53 +000068
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000069 /* First close any currently found FAT filesystem */
70 cur_dev = NULL;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020071
Jon Loeliger80eb47c2007-07-09 17:56:50 -050072#if (defined(CONFIG_CMD_IDE) || \
Sonic Zhang853208e2008-12-09 23:20:18 -050073 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -050074 defined(CONFIG_CMD_SCSI) || \
75 defined(CONFIG_CMD_USB) || \
Andy Fleming55b86682008-01-09 13:51:32 -060076 defined(CONFIG_MMC) || \
77 defined(CONFIG_SYSTEMACE) )
Andy Fleming55b86682008-01-09 13:51:32 -060078
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000079 /* Read the partition table, if present */
80 if (!get_partition_info(dev_desc, part_no, &cur_part_info)) {
81 cur_dev = dev_desc;
82 cur_part_nr = part_no;
83 }
84#endif
85
86 /* Otherwise it might be a superfloppy (whole-disk FAT filesystem) */
87 if (!cur_dev) {
88 if (part_no != 1) {
Wolfgang Denk8fff3542011-11-04 15:55:15 +000089 printf("** Partition %d not valid on device %d **\n",
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000090 part_no, dev_desc->dev);
Wolfgang Denk8fff3542011-11-04 15:55:15 +000091 return -1;
92 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +000093
94 cur_dev = dev_desc;
95 cur_part_nr = 1;
96 cur_part_info.start = 0;
97 cur_part_info.size = dev_desc->lba;
98 cur_part_info.blksz = dev_desc->blksz;
99 memset(cur_part_info.name, 0, sizeof(cur_part_info.name));
100 memset(cur_part_info.type, 0, sizeof(cur_part_info.type));
Wolfgang Denk8fff3542011-11-04 15:55:15 +0000101 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000102
103 /* Make sure it has a valid FAT header */
104 if (disk_read(0, 1, buffer) != 1) {
105 cur_dev = NULL;
106 return -1;
Wolfgang Denke78f2582007-08-07 16:02:13 +0200107 }
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000108
109 /* Check if it's actually a DOS volume */
110 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
111 cur_dev = NULL;
112 return -1;
113 }
114
115 /* Check for FAT12/FAT16/FAT32 filesystem */
116 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
117 return 0;
118 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
119 return 0;
120
121 cur_dev = NULL;
122 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000123}
124
Kyle Moffett1b3b4b72011-12-21 07:08:10 +0000125
wdenk7a428cc2003-06-15 22:40:42 +0000126/*
127 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
128 * Return index into string if found, -1 otherwise.
129 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200130static int dirdelim(char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000131{
132 char *start = str;
133
134 while (*str != '\0') {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200135 if (ISDIRDELIM(*str))
136 return str - start;
wdenk7a428cc2003-06-15 22:40:42 +0000137 str++;
138 }
139 return -1;
140}
141
wdenk7a428cc2003-06-15 22:40:42 +0000142/*
143 * Extract zero terminated short name from a directory entry.
144 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200145static void get_name(dir_entry *dirent, char *s_name)
wdenk7a428cc2003-06-15 22:40:42 +0000146{
147 char *ptr;
148
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200149 memcpy(s_name, dirent->name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000150 s_name[8] = '\0';
151 ptr = s_name;
152 while (*ptr && *ptr != ' ')
153 ptr++;
154 if (dirent->ext[0] && dirent->ext[0] != ' ') {
155 *ptr = '.';
156 ptr++;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200157 memcpy(ptr, dirent->ext, 3);
wdenk7a428cc2003-06-15 22:40:42 +0000158 ptr[3] = '\0';
159 while (*ptr && *ptr != ' ')
160 ptr++;
161 }
162 *ptr = '\0';
163 if (*s_name == DELETED_FLAG)
164 *s_name = '\0';
165 else if (*s_name == aRING)
Remy Bohmer7c8f2ce2008-11-27 22:30:27 +0100166 *s_name = DELETED_FLAG;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200167 downcase(s_name);
wdenk7a428cc2003-06-15 22:40:42 +0000168}
169
170/*
171 * Get the entry at index 'entry' in a FAT (12/16/32) table.
172 * On failure 0x00 is returned.
173 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200174static __u32 get_fatent(fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000175{
176 __u32 bufnum;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200177 __u32 off16, offset;
wdenk7a428cc2003-06-15 22:40:42 +0000178 __u32 ret = 0x00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200179 __u16 val1, val2;
wdenk7a428cc2003-06-15 22:40:42 +0000180
181 switch (mydata->fatsize) {
182 case 32:
183 bufnum = entry / FAT32BUFSIZE;
184 offset = entry - bufnum * FAT32BUFSIZE;
185 break;
186 case 16:
187 bufnum = entry / FAT16BUFSIZE;
188 offset = entry - bufnum * FAT16BUFSIZE;
189 break;
190 case 12:
191 bufnum = entry / FAT12BUFSIZE;
192 offset = entry - bufnum * FAT12BUFSIZE;
193 break;
194
195 default:
196 /* Unsupported FAT size */
197 return ret;
198 }
199
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200200 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
201 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200202
wdenk7a428cc2003-06-15 22:40:42 +0000203 /* Read a new block of FAT entries into the cache. */
204 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000205 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000206 __u8 *bufptr = mydata->fatbuf;
207 __u32 fatlength = mydata->fatlength;
208 __u32 startblock = bufnum * FATBUFBLOCKS;
209
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000210 if (getsize > fatlength)
211 getsize = fatlength;
212
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000213 fatlength *= mydata->sect_size; /* We want it in bytes now */
wdenk7a428cc2003-06-15 22:40:42 +0000214 startblock += mydata->fat_sect; /* Offset from start of disk */
215
wdenk7a428cc2003-06-15 22:40:42 +0000216 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200217 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000218 return ret;
219 }
220 mydata->fatbufnum = bufnum;
221 }
222
223 /* Get the actual entry from the table */
224 switch (mydata->fatsize) {
225 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200226 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000227 break;
228 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200229 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000230 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200231 case 12:
232 off16 = (offset * 3) / 4;
wdenk7a428cc2003-06-15 22:40:42 +0000233
234 switch (offset & 0x3) {
235 case 0:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200236 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000237 ret &= 0xfff;
238 break;
239 case 1:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200240 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000241 val1 &= 0xf000;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200242 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000243 val2 &= 0x00ff;
244 ret = (val2 << 4) | (val1 >> 12);
245 break;
246 case 2:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200247 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000248 val1 &= 0xff00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200249 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000250 val2 &= 0x000f;
251 ret = (val2 << 8) | (val1 >> 8);
252 break;
253 case 3:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200254 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000255 ret = (ret & 0xfff0) >> 4;
256 break;
257 default:
258 break;
259 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200260 break;
wdenk7a428cc2003-06-15 22:40:42 +0000261 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200262 debug("FAT%d: ret: %08x, offset: %04x\n",
263 mydata->fatsize, ret, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000264
265 return ret;
266}
267
wdenk7a428cc2003-06-15 22:40:42 +0000268/*
269 * Read at most 'size' bytes from the specified cluster into 'buffer'.
270 * Return 0 on success, -1 otherwise.
271 */
272static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200273get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000274{
Erik Hansen4ea89662011-03-24 10:15:37 +0100275 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000276 __u32 startsect;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000277 __u32 nr_sect;
278 int ret;
wdenk7a428cc2003-06-15 22:40:42 +0000279
280 if (clustnum > 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200281 startsect = mydata->data_begin +
282 clustnum * mydata->clust_size;
wdenk7a428cc2003-06-15 22:40:42 +0000283 } else {
284 startsect = mydata->rootdir_sect;
285 }
286
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200287 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
288
Kyle Moffett83c8a782011-12-20 07:41:13 +0000289 nr_sect = size / mydata->sect_size;
290 ret = disk_read(startsect, nr_sect, buffer);
291 if (ret != nr_sect) {
292 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000293 return -1;
294 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000295 if (size % mydata->sect_size) {
Eric Nelson014705b2012-04-11 04:08:53 +0000296 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200297
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000298 idx = size / mydata->sect_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000299 ret = disk_read(startsect + idx, 1, tmpbuf);
300 if (ret != 1) {
301 debug("Error reading data (got %d)\n", ret);
wdenk2c9b05d2003-09-10 22:30:53 +0000302 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000303 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000304 buffer += idx * mydata->sect_size;
wdenk2c9b05d2003-09-10 22:30:53 +0000305
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000306 memcpy(buffer, tmpbuf, size % mydata->sect_size);
wdenk2c9b05d2003-09-10 22:30:53 +0000307 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000308 }
309
310 return 0;
311}
312
wdenk7a428cc2003-06-15 22:40:42 +0000313/*
314 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
315 * into 'buffer'.
316 * Return the number of bytes read or -1 on fatal errors.
317 */
318static long
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200319get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
320 unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +0000321{
322 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000323 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000324 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000325 __u32 endclust, newclust;
326 unsigned long actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000327
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200328 debug("Filesize: %ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000329
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200330 if (maxsize > 0 && filesize > maxsize)
331 filesize = maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000332
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200333 debug("%ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000334
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200335 actsize = bytesperclust;
336 endclust = curclust;
337
wdenk7a428cc2003-06-15 22:40:42 +0000338 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000339 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200340 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000341 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200342 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000343 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100344 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200345 debug("curclust: 0x%x\n", newclust);
346 debug("Invalid FAT entry\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000347 return gotsize;
348 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200349 endclust = newclust;
350 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000351 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200352
wdenk2c9b05d2003-09-10 22:30:53 +0000353 /* actsize >= file size */
354 actsize -= bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200355
wdenk2c9b05d2003-09-10 22:30:53 +0000356 /* get remaining clusters */
357 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200358 printf("Error reading cluster\n");
wdenk7a428cc2003-06-15 22:40:42 +0000359 return -1;
360 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200361
wdenk2c9b05d2003-09-10 22:30:53 +0000362 /* get remaining bytes */
363 gotsize += (int)actsize;
364 filesize -= actsize;
365 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200366 actsize = filesize;
wdenk2c9b05d2003-09-10 22:30:53 +0000367 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200368 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000369 return -1;
370 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200371 gotsize += actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000372 return gotsize;
373getit:
374 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200375 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000376 return -1;
377 }
378 gotsize += (int)actsize;
379 filesize -= actsize;
380 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200381
wdenk2c9b05d2003-09-10 22:30:53 +0000382 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100383 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200384 debug("curclust: 0x%x\n", curclust);
385 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000386 return gotsize;
387 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200388 actsize = bytesperclust;
389 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000390 } while (1);
391}
392
wdenk7a428cc2003-06-15 22:40:42 +0000393#ifdef CONFIG_SUPPORT_VFAT
394/*
395 * Extract the file name information from 'slotptr' into 'l_name',
396 * starting at l_name[*idx].
397 * Return 1 if terminator (zero byte) is found, 0 otherwise.
398 */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200399static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000400{
401 int j;
402
403 for (j = 0; j <= 8; j += 2) {
404 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200405 if (l_name[*idx] == 0x00)
406 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000407 (*idx)++;
408 }
409 for (j = 0; j <= 10; j += 2) {
410 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200411 if (l_name[*idx] == 0x00)
412 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000413 (*idx)++;
414 }
415 for (j = 0; j <= 2; j += 2) {
416 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200417 if (l_name[*idx] == 0x00)
418 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000419 (*idx)++;
420 }
421
422 return 0;
423}
424
wdenk7a428cc2003-06-15 22:40:42 +0000425/*
426 * Extract the full long filename starting at 'retdent' (which is really
427 * a slot) into 'l_name'. If successful also copy the real directory entry
428 * into 'retdent'
429 * Return 0 on success, -1 otherwise.
430 */
Eric Nelson014705b2012-04-11 04:08:53 +0000431__u8 get_vfatname_block[MAX_CLUSTSIZE]
432 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200433
wdenk7a428cc2003-06-15 22:40:42 +0000434static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200435get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
436 dir_entry *retdent, char *l_name)
wdenk7a428cc2003-06-15 22:40:42 +0000437{
438 dir_entry *realdent;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200439 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000440 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
441 PREFETCH_BLOCKS :
442 mydata->clust_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200443 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk7a428cc2003-06-15 22:40:42 +0000444 int idx = 0;
445
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300446 if (counter > VFAT_MAXSEQ) {
447 debug("Error: VFAT name is too long\n");
448 return -1;
449 }
450
451 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200452 if (counter == 0)
453 break;
wdenk2ebee312004-02-23 19:30:57 +0000454 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
455 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000456 slotptr++;
457 counter--;
458 }
459
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300460 if ((__u8 *)slotptr >= buflimit) {
wdenk7a428cc2003-06-15 22:40:42 +0000461 dir_slot *slotptr2;
462
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300463 if (curclust == 0)
464 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000465 curclust = get_fatent(mydata, curclust);
michael2c4d2982008-03-02 23:33:46 +0100466 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200467 debug("curclust: 0x%x\n", curclust);
468 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000469 return -1;
470 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200471
wdenkb6c60cb32003-10-29 23:18:55 +0000472 if (get_cluster(mydata, curclust, get_vfatname_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000473 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200474 debug("Error: reading directory block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000475 return -1;
476 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200477
478 slotptr2 = (dir_slot *)get_vfatname_block;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300479 while (counter > 0) {
480 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
481 & 0xff) != counter)
482 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000483 slotptr2++;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300484 counter--;
485 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200486
wdenk7a428cc2003-06-15 22:40:42 +0000487 /* Save the real directory entry */
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300488 realdent = (dir_entry *)slotptr2;
489 while ((__u8 *)slotptr2 > get_vfatname_block) {
wdenk7a428cc2003-06-15 22:40:42 +0000490 slotptr2--;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300491 slot2str(slotptr2, l_name, &idx);
wdenk7a428cc2003-06-15 22:40:42 +0000492 }
493 } else {
494 /* Save the real directory entry */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200495 realdent = (dir_entry *)slotptr;
wdenk7a428cc2003-06-15 22:40:42 +0000496 }
497
498 do {
499 slotptr--;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200500 if (slot2str(slotptr, l_name, &idx))
501 break;
wdenk2ebee312004-02-23 19:30:57 +0000502 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk7a428cc2003-06-15 22:40:42 +0000503
504 l_name[idx] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200505 if (*l_name == DELETED_FLAG)
506 *l_name = '\0';
507 else if (*l_name == aRING)
508 *l_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000509 downcase(l_name);
510
511 /* Return the real directory entry */
512 memcpy(retdent, realdent, sizeof(dir_entry));
513
514 return 0;
515}
516
wdenk7a428cc2003-06-15 22:40:42 +0000517/* Calculate short name checksum */
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200518static __u8 mkcksum(const char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000519{
520 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200521
wdenk7a428cc2003-06-15 22:40:42 +0000522 __u8 ret = 0;
523
524 for (i = 0; i < 11; i++) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200525 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
wdenk7a428cc2003-06-15 22:40:42 +0000526 }
527
528 return ret;
529}
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200530#endif /* CONFIG_SUPPORT_VFAT */
wdenk7a428cc2003-06-15 22:40:42 +0000531
532/*
533 * Get the directory entry associated with 'filename' from the directory
534 * starting at 'startsect'
535 */
Eric Nelson014705b2012-04-11 04:08:53 +0000536__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
537 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200538
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200539static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
540 char *filename, dir_entry *retdent,
541 int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000542{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200543 __u16 prevcksum = 0xffff;
544 __u32 curclust = START(retdent);
545 int files = 0, dirs = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000546
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200547 debug("get_dentfromdir: %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +0000548
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200549 while (1) {
550 dir_entry *dentptr;
551
552 int i;
553
554 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000555 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200556 debug("Error: reading directory block\n");
557 return NULL;
558 }
wdenk7a428cc2003-06-15 22:40:42 +0000559
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200560 dentptr = (dir_entry *)get_dentfromdir_block;
561
562 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300563 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200564
565 l_name[0] = '\0';
566 if (dentptr->name[0] == DELETED_FLAG) {
567 dentptr++;
568 continue;
569 }
570 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk7a428cc2003-06-15 22:40:42 +0000571#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand575e21e2011-10-19 07:43:08 +0000572 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
573 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200574 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
575 get_vfatname(mydata, curclust,
576 get_dentfromdir_block,
577 dentptr, l_name);
578 if (dols) {
579 int isdir;
580 char dirc;
581 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000582
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200583 isdir = (dentptr->attr & ATTR_DIR);
584
585 if (isdir) {
586 dirs++;
587 dirc = '/';
588 doit = 1;
589 } else {
590 dirc = ' ';
591 if (l_name[0] != 0) {
592 files++;
593 doit = 1;
594 }
595 }
596 if (doit) {
597 if (dirc == ' ') {
598 printf(" %8ld %s%c\n",
599 (long)FAT2CPU32(dentptr->size),
600 l_name,
601 dirc);
602 } else {
603 printf(" %s%c\n",
604 l_name,
605 dirc);
606 }
607 }
608 dentptr++;
609 continue;
610 }
611 debug("vfatname: |%s|\n", l_name);
612 } else
613#endif
614 {
615 /* Volume label or VFAT entry */
616 dentptr++;
617 continue;
618 }
wdenk7a428cc2003-06-15 22:40:42 +0000619 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200620 if (dentptr->name[0] == 0) {
621 if (dols) {
622 printf("\n%d file(s), %d dir(s)\n\n",
623 files, dirs);
624 }
625 debug("Dentname == NULL - %d\n", i);
626 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000627 }
wdenk7a428cc2003-06-15 22:40:42 +0000628#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200629 if (dols && mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +0000630 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200631 dentptr++;
632 continue;
633 }
wdenk7a428cc2003-06-15 22:40:42 +0000634#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200635 get_name(dentptr, s_name);
636 if (dols) {
637 int isdir = (dentptr->attr & ATTR_DIR);
638 char dirc;
639 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000640
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200641 if (isdir) {
642 dirs++;
643 dirc = '/';
644 doit = 1;
645 } else {
646 dirc = ' ';
647 if (s_name[0] != 0) {
648 files++;
649 doit = 1;
650 }
651 }
wdenk7a428cc2003-06-15 22:40:42 +0000652
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200653 if (doit) {
654 if (dirc == ' ') {
655 printf(" %8ld %s%c\n",
656 (long)FAT2CPU32(dentptr->size),
657 s_name, dirc);
658 } else {
659 printf(" %s%c\n",
660 s_name, dirc);
661 }
662 }
wdenk7a428cc2003-06-15 22:40:42 +0000663
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200664 dentptr++;
665 continue;
666 }
667
668 if (strcmp(filename, s_name)
669 && strcmp(filename, l_name)) {
670 debug("Mismatch: |%s|%s|\n", s_name, l_name);
671 dentptr++;
672 continue;
673 }
674
675 memcpy(retdent, dentptr, sizeof(dir_entry));
676
677 debug("DentName: %s", s_name);
678 debug(", start: 0x%x", START(dentptr));
679 debug(", size: 0x%x %s\n",
680 FAT2CPU32(dentptr->size),
681 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
682
683 return retdent;
684 }
685
686 curclust = get_fatent(mydata, curclust);
687 if (CHECK_CLUST(curclust, mydata->fatsize)) {
688 debug("curclust: 0x%x\n", curclust);
689 printf("Invalid FAT entry\n");
690 return NULL;
691 }
wdenk7a428cc2003-06-15 22:40:42 +0000692 }
wdenk7a428cc2003-06-15 22:40:42 +0000693
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200694 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000695}
696
wdenk7a428cc2003-06-15 22:40:42 +0000697/*
698 * Read boot sector and volume info from a FAT filesystem
699 */
700static int
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200701read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000702{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000703 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000704 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000705 int ret = 0;
706
707 if (cur_dev == NULL) {
708 debug("Error: no device selected\n");
709 return -1;
710 }
711
Eric Nelson014705b2012-04-11 04:08:53 +0000712 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000713 if (block == NULL) {
714 debug("Error: allocating block\n");
715 return -1;
716 }
wdenk7a428cc2003-06-15 22:40:42 +0000717
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200718 if (disk_read(0, 1, block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200719 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000720 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000721 }
722
723 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200724 bs->reserved = FAT2CPU16(bs->reserved);
725 bs->fat_length = FAT2CPU16(bs->fat_length);
726 bs->secs_track = FAT2CPU16(bs->secs_track);
727 bs->heads = FAT2CPU16(bs->heads);
728 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000729
730 /* FAT32 entries */
731 if (bs->fat_length == 0) {
732 /* Assume FAT32 */
733 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200734 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000735 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200736 bs->info_sector = FAT2CPU16(bs->info_sector);
737 bs->backup_boot = FAT2CPU16(bs->backup_boot);
738 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000739 *fatsize = 32;
740 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200741 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000742 *fatsize = 0;
743 }
744 memcpy(volinfo, vistart, sizeof(volume_info));
745
wdenk7a428cc2003-06-15 22:40:42 +0000746 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200747 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000748 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000749 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500750 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000751 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000752 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000753 }
Tom Rix155abaa2009-05-20 07:55:41 -0500754 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000755 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000756 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000757 }
758 }
759
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200760 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000761fail:
762 ret = -1;
763exit:
764 free(block);
765 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000766}
767
Eric Nelson014705b2012-04-11 04:08:53 +0000768__u8 do_fat_read_block[MAX_CLUSTSIZE]
769 __aligned(ARCH_DMA_MINALIGN);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200770
stroese83082242004-12-16 17:57:26 +0000771long
Benoît Thébaudeau0a120372012-07-20 15:18:44 +0200772do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
wdenk7a428cc2003-06-15 22:40:42 +0000773{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200774 char fnamecopy[2048];
775 boot_sector bs;
776 volume_info volinfo;
777 fsdata datablock;
778 fsdata *mydata = &datablock;
779 dir_entry *dentptr;
780 __u16 prevcksum = 0xffff;
781 char *subname = "";
Erik Hansen4ea89662011-03-24 10:15:37 +0100782 __u32 cursect;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200783 int idx, isdir = 0;
784 int files = 0, dirs = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000785 long ret = -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200786 int firsttime;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000787 __u32 root_cluster = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +0100788 int rootdir_size = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200789 int j;
wdenk7a428cc2003-06-15 22:40:42 +0000790
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200791 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
792 debug("Error: reading boot sector\n");
793 return -1;
794 }
795
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000796 if (mydata->fatsize == 32) {
797 root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200798 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000799 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200800 mydata->fatlength = bs.fat_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000801 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200802
803 mydata->fat_sect = bs.reserved;
804
805 cursect = mydata->rootdir_sect
806 = mydata->fat_sect + mydata->fatlength * bs.fats;
807
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000808 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200809 mydata->clust_size = bs.cluster_size;
Kyle Moffett83c8a782011-12-20 07:41:13 +0000810 if (mydata->sect_size != cur_part_info.blksz) {
811 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
812 mydata->sect_size, cur_part_info.blksz);
813 return -1;
814 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200815
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200816 if (mydata->fatsize == 32) {
817 mydata->data_begin = mydata->rootdir_sect -
818 (mydata->clust_size * 2);
819 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200820 rootdir_size = ((bs.dir_entries[1] * (int)256 +
821 bs.dir_entries[0]) *
822 sizeof(dir_entry)) /
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000823 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200824 mydata->data_begin = mydata->rootdir_sect +
825 rootdir_size -
826 (mydata->clust_size * 2);
827 }
828
829 mydata->fatbufnum = -1;
Eric Nelson014705b2012-04-11 04:08:53 +0000830 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000831 if (mydata->fatbuf == NULL) {
832 debug("Error: allocating memory\n");
833 return -1;
834 }
wdenk7a428cc2003-06-15 22:40:42 +0000835
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200836#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200837 debug("VFAT Support enabled\n");
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200838#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200839 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
840 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
841 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
842 "Data begins at: %d\n",
843 root_cluster,
844 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000845 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
846 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
847 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000848
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200849 /* "cwd" is always the root... */
850 while (ISDIRDELIM(*filename))
851 filename++;
wdenk7a428cc2003-06-15 22:40:42 +0000852
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200853 /* Make a copy of the filename and convert it to lowercase */
854 strcpy(fnamecopy, filename);
855 downcase(fnamecopy);
wdenk7a428cc2003-06-15 22:40:42 +0000856
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200857 if (*fnamecopy == '\0') {
858 if (!dols)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000859 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200860
861 dols = LS_ROOT;
862 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
863 isdir = 1;
864 fnamecopy[idx] = '\0';
865 subname = fnamecopy + idx + 1;
866
867 /* Handle multiple delimiters */
868 while (ISDIRDELIM(*subname))
869 subname++;
870 } else if (dols) {
871 isdir = 1;
wdenk7a428cc2003-06-15 22:40:42 +0000872 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200873
874 j = 0;
875 while (1) {
876 int i;
877
Andreas Bießmann5c8f7802011-12-15 09:56:54 +0100878 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200879 cursect, mydata->clust_size, DIRENTSPERBLOCK);
880
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300881 if (disk_read(cursect,
882 (mydata->fatsize == 32) ?
883 (mydata->clust_size) :
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000884 PREFETCH_BLOCKS,
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300885 do_fat_read_block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200886 debug("Error: reading rootdir block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000887 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200888 }
889
890 dentptr = (dir_entry *) do_fat_read_block;
891
892 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300893 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
wdenk7a428cc2003-06-15 22:40:42 +0000894
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200895 l_name[0] = '\0';
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300896 if (dentptr->name[0] == DELETED_FLAG) {
897 dentptr++;
898 continue;
899 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200900 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk7a428cc2003-06-15 22:40:42 +0000901#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand575e21e2011-10-19 07:43:08 +0000902 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200903 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
904 prevcksum =
905 ((dir_slot *)dentptr)->alias_checksum;
wdenk7a428cc2003-06-15 22:40:42 +0000906
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300907 get_vfatname(mydata,
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000908 root_cluster,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200909 do_fat_read_block,
910 dentptr, l_name);
911
912 if (dols == LS_ROOT) {
913 char dirc;
914 int doit = 0;
915 int isdir =
916 (dentptr->attr & ATTR_DIR);
917
918 if (isdir) {
919 dirs++;
920 dirc = '/';
921 doit = 1;
922 } else {
923 dirc = ' ';
924 if (l_name[0] != 0) {
925 files++;
926 doit = 1;
927 }
928 }
929 if (doit) {
930 if (dirc == ' ') {
931 printf(" %8ld %s%c\n",
932 (long)FAT2CPU32(dentptr->size),
933 l_name,
934 dirc);
935 } else {
936 printf(" %s%c\n",
937 l_name,
938 dirc);
939 }
940 }
941 dentptr++;
942 continue;
943 }
944 debug("Rootvfatname: |%s|\n",
945 l_name);
946 } else
wdenk7a428cc2003-06-15 22:40:42 +0000947#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200948 {
949 /* Volume label or VFAT entry */
950 dentptr++;
951 continue;
952 }
953 } else if (dentptr->name[0] == 0) {
954 debug("RootDentname == NULL - %d\n", i);
955 if (dols == LS_ROOT) {
956 printf("\n%d file(s), %d dir(s)\n\n",
957 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000958 ret = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200959 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000960 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200961 }
wdenk7a428cc2003-06-15 22:40:42 +0000962#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200963 else if (dols == LS_ROOT &&
964 mkcksum(dentptr->name) == prevcksum) {
Sergei Shtylyov891240a2012-01-02 06:54:29 +0000965 prevcksum = 0xffff;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200966 dentptr++;
967 continue;
968 }
wdenk7a428cc2003-06-15 22:40:42 +0000969#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200970 get_name(dentptr, s_name);
wdenk7a428cc2003-06-15 22:40:42 +0000971
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200972 if (dols == LS_ROOT) {
973 int isdir = (dentptr->attr & ATTR_DIR);
974 char dirc;
975 int doit = 0;
976
977 if (isdir) {
978 dirc = '/';
979 if (s_name[0] != 0) {
980 dirs++;
981 doit = 1;
982 }
983 } else {
984 dirc = ' ';
985 if (s_name[0] != 0) {
986 files++;
987 doit = 1;
988 }
989 }
990 if (doit) {
991 if (dirc == ' ') {
992 printf(" %8ld %s%c\n",
993 (long)FAT2CPU32(dentptr->size),
994 s_name, dirc);
995 } else {
996 printf(" %s%c\n",
997 s_name, dirc);
998 }
999 }
1000 dentptr++;
1001 continue;
1002 }
1003
1004 if (strcmp(fnamecopy, s_name)
1005 && strcmp(fnamecopy, l_name)) {
1006 debug("RootMismatch: |%s|%s|\n", s_name,
1007 l_name);
1008 dentptr++;
1009 continue;
1010 }
1011
1012 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001013 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001014
1015 debug("RootName: %s", s_name);
1016 debug(", start: 0x%x", START(dentptr));
1017 debug(", size: 0x%x %s\n",
1018 FAT2CPU32(dentptr->size),
1019 isdir ? "(DIR)" : "");
1020
1021 goto rootdir_done; /* We got a match */
wdenk7a428cc2003-06-15 22:40:42 +00001022 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001023 debug("END LOOP: j=%d clust_size=%d\n", j,
1024 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +00001025
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001026 /*
1027 * On FAT32 we must fetch the FAT entries for the next
1028 * root directory clusters when a cluster has been
1029 * completely processed.
1030 */
Erik Hansen4ea89662011-03-24 10:15:37 +01001031 ++j;
1032 int fat32_end = 0;
1033 if ((mydata->fatsize == 32) && (j == mydata->clust_size)) {
1034 int nxtsect = 0;
1035 int nxt_clust = 0;
wdenk7a428cc2003-06-15 22:40:42 +00001036
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001037 nxt_clust = get_fatent(mydata, root_cluster);
Erik Hansen4ea89662011-03-24 10:15:37 +01001038 fat32_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001039
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001040 nxtsect = mydata->data_begin +
1041 (nxt_clust * mydata->clust_size);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001042
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001043 root_cluster = nxt_clust;
1044
1045 cursect = nxtsect;
1046 j = 0;
1047 } else {
1048 cursect++;
1049 }
Erik Hansen4ea89662011-03-24 10:15:37 +01001050
1051 /* If end of rootdir reached */
1052 if ((mydata->fatsize == 32 && fat32_end) ||
1053 (mydata->fatsize != 32 && j == rootdir_size)) {
1054 if (dols == LS_ROOT) {
1055 printf("\n%d file(s), %d dir(s)\n\n",
1056 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001057 ret = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +01001058 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001059 goto exit;
Erik Hansen4ea89662011-03-24 10:15:37 +01001060 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001061 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001062rootdir_done:
wdenk7a428cc2003-06-15 22:40:42 +00001063
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001064 firsttime = 1;
wdenk7a428cc2003-06-15 22:40:42 +00001065
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001066 while (isdir) {
1067 int startsect = mydata->data_begin
1068 + START(dentptr) * mydata->clust_size;
1069 dir_entry dent;
1070 char *nextname = NULL;
wdenk7a428cc2003-06-15 22:40:42 +00001071
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001072 dent = *dentptr;
1073 dentptr = &dent;
wdenk7a428cc2003-06-15 22:40:42 +00001074
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001075 idx = dirdelim(subname);
wdenk7a428cc2003-06-15 22:40:42 +00001076
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001077 if (idx >= 0) {
1078 subname[idx] = '\0';
1079 nextname = subname + idx + 1;
1080 /* Handle multiple delimiters */
1081 while (ISDIRDELIM(*nextname))
1082 nextname++;
1083 if (dols && *nextname == '\0')
1084 firsttime = 0;
1085 } else {
1086 if (dols && firsttime) {
1087 firsttime = 0;
1088 } else {
1089 isdir = 0;
1090 }
1091 }
1092
1093 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1094 isdir ? 0 : dols) == NULL) {
1095 if (dols && !isdir)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001096 ret = 0;
1097 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001098 }
1099
1100 if (idx >= 0) {
1101 if (!(dentptr->attr & ATTR_DIR))
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001102 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001103 subname = nextname;
1104 }
wdenk7a428cc2003-06-15 22:40:42 +00001105 }
wdenk7a428cc2003-06-15 22:40:42 +00001106
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001107 ret = get_contents(mydata, dentptr, buffer, maxsize);
1108 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
wdenk7a428cc2003-06-15 22:40:42 +00001109
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +00001110exit:
1111 free(mydata->fatbuf);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001112 return ret;
1113}
wdenk7a428cc2003-06-15 22:40:42 +00001114
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001115int file_fat_detectfs(void)
wdenk7a428cc2003-06-15 22:40:42 +00001116{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001117 boot_sector bs;
1118 volume_info volinfo;
1119 int fatsize;
1120 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001121
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001122 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001123 printf("No current device\n");
1124 return 1;
1125 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001126
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001127#if defined(CONFIG_CMD_IDE) || \
Sonic Zhang853208e2008-12-09 23:20:18 -05001128 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001129 defined(CONFIG_CMD_SCSI) || \
1130 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001131 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001132 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001133 switch (cur_dev->if_type) {
1134 case IF_TYPE_IDE:
1135 printf("IDE");
1136 break;
1137 case IF_TYPE_SATA:
1138 printf("SATA");
1139 break;
1140 case IF_TYPE_SCSI:
1141 printf("SCSI");
1142 break;
1143 case IF_TYPE_ATAPI:
1144 printf("ATAPI");
1145 break;
1146 case IF_TYPE_USB:
1147 printf("USB");
1148 break;
1149 case IF_TYPE_DOC:
1150 printf("DOC");
1151 break;
1152 case IF_TYPE_MMC:
1153 printf("MMC");
1154 break;
1155 default:
1156 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001157 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001158
1159 printf("\n Device %d: ", cur_dev->dev);
wdenk2c9b05d2003-09-10 22:30:53 +00001160 dev_print(cur_dev);
1161#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001162
1163 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001164 printf("\nNo valid FAT fs found\n");
1165 return 1;
1166 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001167
1168 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001169 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001170 volinfo.fs_type[5] = '\0';
1171
Kyle Moffett1b3b4b72011-12-21 07:08:10 +00001172 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part_nr,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001173 volinfo.fs_type, vol_label);
1174
wdenk2c9b05d2003-09-10 22:30:53 +00001175 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001176}
1177
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001178int file_fat_ls(const char *dir)
wdenk7a428cc2003-06-15 22:40:42 +00001179{
1180 return do_fat_read(dir, NULL, 0, LS_YES);
1181}
1182
Benoît Thébaudeau0a120372012-07-20 15:18:44 +02001183long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +00001184{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001185 printf("reading %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +00001186 return do_fat_read(filename, buffer, maxsize, LS_NO);
1187}