blob: 9a29458c6c18123a5ebd66b4d93912704f93d5d3 [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>
wdenk7a428cc2003-06-15 22:40:42 +000034
wdenk7a428cc2003-06-15 22:40:42 +000035/*
36 * Convert a string to lowercase.
37 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020038static void downcase (char *str)
wdenk7a428cc2003-06-15 22:40:42 +000039{
40 while (*str != '\0') {
41 TOLOWER(*str);
42 str++;
43 }
44}
45
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020046static block_dev_desc_t *cur_dev = NULL;
47
wdenk2c9b05d2003-09-10 22:30:53 +000048static unsigned long part_offset = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020049
wdenk2c9b05d2003-09-10 22:30:53 +000050static int cur_part = 1;
51
52#define DOS_PART_TBL_OFFSET 0x1be
53#define DOS_PART_MAGIC_OFFSET 0x1fe
54#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020055#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000056
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020057static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
wdenk7a428cc2003-06-15 22:40:42 +000058{
wdenk2c9b05d2003-09-10 22:30:53 +000059 if (cur_dev == NULL)
60 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020061
62 startblock += part_offset;
63
wdenk2c9b05d2003-09-10 22:30:53 +000064 if (cur_dev->block_read) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020065 return cur_dev->block_read(cur_dev->dev, startblock, getsize,
66 (unsigned long *) bufptr);
wdenk7a428cc2003-06-15 22:40:42 +000067 }
68 return -1;
69}
70
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020071int fat_register_device (block_dev_desc_t * dev_desc, int part_no)
wdenk7a428cc2003-06-15 22:40:42 +000072{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +000073 unsigned char buffer[dev_desc->blksz];
wdenk2c9b05d2003-09-10 22:30:53 +000074
75 if (!dev_desc->block_read)
76 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020077
Heiko Schocher633e03a2007-06-22 19:11:54 +020078 cur_dev = dev_desc;
wdenk2c9b05d2003-09-10 22:30:53 +000079 /* check if we have a MBR (on floppies we have only a PBR) */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020080 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)buffer) != 1) {
81 printf("** Can't read from device %d **\n",
82 dev_desc->dev);
wdenk2c9b05d2003-09-10 22:30:53 +000083 return -1;
84 }
85 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020086 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
wdenk2c9b05d2003-09-10 22:30:53 +000087 /* no signature found */
88 return -1;
89 }
Jon Loeliger80eb47c2007-07-09 17:56:50 -050090#if (defined(CONFIG_CMD_IDE) || \
unsik Kimf0b1bdd2009-02-25 11:31:24 +090091 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang853208e2008-12-09 23:20:18 -050092 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -050093 defined(CONFIG_CMD_SCSI) || \
94 defined(CONFIG_CMD_USB) || \
Andy Fleming55b86682008-01-09 13:51:32 -060095 defined(CONFIG_MMC) || \
96 defined(CONFIG_SYSTEMACE) )
Wolfgang Denk8fff3542011-11-04 15:55:15 +000097 {
98 disk_partition_t info;
Andy Fleming55b86682008-01-09 13:51:32 -060099
Wolfgang Denk8fff3542011-11-04 15:55:15 +0000100 /* First we assume there is a MBR */
101 if (!get_partition_info(dev_desc, part_no, &info)) {
102 part_offset = info.start;
103 cur_part = part_no;
104 } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],
105 "FAT", 3) == 0) ||
106 (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET],
107 "FAT32", 5) == 0)) {
108 /* ok, we assume we are on a PBR only */
109 cur_part = 1;
110 part_offset = 0;
111 } else {
112 printf("** Partition %d not valid on device %d **\n",
113 part_no, dev_desc->dev);
114 return -1;
115 }
116 }
wdenk2c9b05d2003-09-10 22:30:53 +0000117#else
Wolfgang Denk7b2290c2010-07-19 11:36:57 +0200118 if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
119 (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
Andy Fleming55b86682008-01-09 13:51:32 -0600120 /* ok, we assume we are on a PBR only */
121 cur_part = 1;
122 part_offset = 0;
Andy Fleming55b86682008-01-09 13:51:32 -0600123 } else {
124 /* FIXME we need to determine the start block of the
125 * partition where the DOS FS resides. This can be done
126 * by using the get_partition_info routine. For this
127 * purpose the libpart must be included.
128 */
129 part_offset = 32;
130 cur_part = 1;
Wolfgang Denke78f2582007-08-07 16:02:13 +0200131 }
Andy Fleming55b86682008-01-09 13:51:32 -0600132#endif
wdenk7a428cc2003-06-15 22:40:42 +0000133 return 0;
134}
135
wdenk7a428cc2003-06-15 22:40:42 +0000136/*
137 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
138 * Return index into string if found, -1 otherwise.
139 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200140static int dirdelim (char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000141{
142 char *start = str;
143
144 while (*str != '\0') {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200145 if (ISDIRDELIM(*str))
146 return str - start;
wdenk7a428cc2003-06-15 22:40:42 +0000147 str++;
148 }
149 return -1;
150}
151
wdenk7a428cc2003-06-15 22:40:42 +0000152/*
153 * Extract zero terminated short name from a directory entry.
154 */
155static void get_name (dir_entry *dirent, char *s_name)
156{
157 char *ptr;
158
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200159 memcpy(s_name, dirent->name, 8);
wdenk7a428cc2003-06-15 22:40:42 +0000160 s_name[8] = '\0';
161 ptr = s_name;
162 while (*ptr && *ptr != ' ')
163 ptr++;
164 if (dirent->ext[0] && dirent->ext[0] != ' ') {
165 *ptr = '.';
166 ptr++;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200167 memcpy(ptr, dirent->ext, 3);
wdenk7a428cc2003-06-15 22:40:42 +0000168 ptr[3] = '\0';
169 while (*ptr && *ptr != ' ')
170 ptr++;
171 }
172 *ptr = '\0';
173 if (*s_name == DELETED_FLAG)
174 *s_name = '\0';
175 else if (*s_name == aRING)
Remy Bohmer7c8f2ce2008-11-27 22:30:27 +0100176 *s_name = DELETED_FLAG;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200177 downcase(s_name);
wdenk7a428cc2003-06-15 22:40:42 +0000178}
179
180/*
181 * Get the entry at index 'entry' in a FAT (12/16/32) table.
182 * On failure 0x00 is returned.
183 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200184static __u32 get_fatent (fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000185{
186 __u32 bufnum;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200187 __u32 off16, offset;
wdenk7a428cc2003-06-15 22:40:42 +0000188 __u32 ret = 0x00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200189 __u16 val1, val2;
wdenk7a428cc2003-06-15 22:40:42 +0000190
191 switch (mydata->fatsize) {
192 case 32:
193 bufnum = entry / FAT32BUFSIZE;
194 offset = entry - bufnum * FAT32BUFSIZE;
195 break;
196 case 16:
197 bufnum = entry / FAT16BUFSIZE;
198 offset = entry - bufnum * FAT16BUFSIZE;
199 break;
200 case 12:
201 bufnum = entry / FAT12BUFSIZE;
202 offset = entry - bufnum * FAT12BUFSIZE;
203 break;
204
205 default:
206 /* Unsupported FAT size */
207 return ret;
208 }
209
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200210 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
211 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200212
wdenk7a428cc2003-06-15 22:40:42 +0000213 /* Read a new block of FAT entries into the cache. */
214 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000215 __u32 getsize = FATBUFBLOCKS;
wdenk7a428cc2003-06-15 22:40:42 +0000216 __u8 *bufptr = mydata->fatbuf;
217 __u32 fatlength = mydata->fatlength;
218 __u32 startblock = bufnum * FATBUFBLOCKS;
219
Sergei Shtylyovd5916532011-08-08 09:39:29 +0000220 if (getsize > fatlength)
221 getsize = fatlength;
222
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000223 fatlength *= mydata->sect_size; /* We want it in bytes now */
wdenk7a428cc2003-06-15 22:40:42 +0000224 startblock += mydata->fat_sect; /* Offset from start of disk */
225
wdenk7a428cc2003-06-15 22:40:42 +0000226 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200227 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000228 return ret;
229 }
230 mydata->fatbufnum = bufnum;
231 }
232
233 /* Get the actual entry from the table */
234 switch (mydata->fatsize) {
235 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200236 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000237 break;
238 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200239 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000240 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200241 case 12:
242 off16 = (offset * 3) / 4;
wdenk7a428cc2003-06-15 22:40:42 +0000243
244 switch (offset & 0x3) {
245 case 0:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200246 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000247 ret &= 0xfff;
248 break;
249 case 1:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200250 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000251 val1 &= 0xf000;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200252 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000253 val2 &= 0x00ff;
254 ret = (val2 << 4) | (val1 >> 12);
255 break;
256 case 2:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200257 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000258 val1 &= 0xff00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200259 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000260 val2 &= 0x000f;
261 ret = (val2 << 8) | (val1 >> 8);
262 break;
263 case 3:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200264 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000265 ret = (ret & 0xfff0) >> 4;
266 break;
267 default:
268 break;
269 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200270 break;
wdenk7a428cc2003-06-15 22:40:42 +0000271 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200272 debug("FAT%d: ret: %08x, offset: %04x\n",
273 mydata->fatsize, ret, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000274
275 return ret;
276}
277
wdenk7a428cc2003-06-15 22:40:42 +0000278/*
279 * Read at most 'size' bytes from the specified cluster into 'buffer'.
280 * Return 0 on success, -1 otherwise.
281 */
282static int
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200283get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
284 unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000285{
Erik Hansen4ea89662011-03-24 10:15:37 +0100286 __u32 idx = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000287 __u32 startsect;
288
289 if (clustnum > 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200290 startsect = mydata->data_begin +
291 clustnum * mydata->clust_size;
wdenk7a428cc2003-06-15 22:40:42 +0000292 } else {
293 startsect = mydata->rootdir_sect;
294 }
295
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200296 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
297
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000298 if (disk_read(startsect, size / mydata->sect_size, buffer) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200299 debug("Error reading data\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000300 return -1;
301 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000302 if (size % mydata->sect_size) {
303 __u8 tmpbuf[mydata->sect_size];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200304
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000305 idx = size / mydata->sect_size;
wdenk2c9b05d2003-09-10 22:30:53 +0000306 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200307 debug("Error reading data\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000308 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000309 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000310 buffer += idx * mydata->sect_size;
wdenk2c9b05d2003-09-10 22:30:53 +0000311
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000312 memcpy(buffer, tmpbuf, size % mydata->sect_size);
wdenk2c9b05d2003-09-10 22:30:53 +0000313 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000314 }
315
316 return 0;
317}
318
wdenk7a428cc2003-06-15 22:40:42 +0000319/*
320 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
321 * into 'buffer'.
322 * Return the number of bytes read or -1 on fatal errors.
323 */
324static long
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200325get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
326 unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +0000327{
328 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000329 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk7a428cc2003-06-15 22:40:42 +0000330 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000331 __u32 endclust, newclust;
332 unsigned long actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000333
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200334 debug("Filesize: %ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000335
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200336 if (maxsize > 0 && filesize > maxsize)
337 filesize = maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000338
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200339 debug("%ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000340
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200341 actsize = bytesperclust;
342 endclust = curclust;
343
wdenk7a428cc2003-06-15 22:40:42 +0000344 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000345 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200346 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000347 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200348 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000349 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100350 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200351 debug("curclust: 0x%x\n", newclust);
352 debug("Invalid FAT entry\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000353 return gotsize;
354 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200355 endclust = newclust;
356 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000357 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200358
wdenk2c9b05d2003-09-10 22:30:53 +0000359 /* actsize >= file size */
360 actsize -= bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200361
wdenk2c9b05d2003-09-10 22:30:53 +0000362 /* get remaining clusters */
363 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200364 printf("Error reading cluster\n");
wdenk7a428cc2003-06-15 22:40:42 +0000365 return -1;
366 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200367
wdenk2c9b05d2003-09-10 22:30:53 +0000368 /* get remaining bytes */
369 gotsize += (int)actsize;
370 filesize -= actsize;
371 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200372 actsize = filesize;
wdenk2c9b05d2003-09-10 22:30:53 +0000373 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200374 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000375 return -1;
376 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200377 gotsize += actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000378 return gotsize;
379getit:
380 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200381 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000382 return -1;
383 }
384 gotsize += (int)actsize;
385 filesize -= actsize;
386 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200387
wdenk2c9b05d2003-09-10 22:30:53 +0000388 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100389 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200390 debug("curclust: 0x%x\n", curclust);
391 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000392 return gotsize;
393 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200394 actsize = bytesperclust;
395 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000396 } while (1);
397}
398
wdenk7a428cc2003-06-15 22:40:42 +0000399#ifdef CONFIG_SUPPORT_VFAT
400/*
401 * Extract the file name information from 'slotptr' into 'l_name',
402 * starting at l_name[*idx].
403 * Return 1 if terminator (zero byte) is found, 0 otherwise.
404 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200405static int slot2str (dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000406{
407 int j;
408
409 for (j = 0; j <= 8; j += 2) {
410 l_name[*idx] = slotptr->name0_4[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 <= 10; j += 2) {
416 l_name[*idx] = slotptr->name5_10[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 for (j = 0; j <= 2; j += 2) {
422 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200423 if (l_name[*idx] == 0x00)
424 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000425 (*idx)++;
426 }
427
428 return 0;
429}
430
wdenk7a428cc2003-06-15 22:40:42 +0000431/*
432 * Extract the full long filename starting at 'retdent' (which is really
433 * a slot) into 'l_name'. If successful also copy the real directory entry
434 * into 'retdent'
435 * Return 0 on success, -1 otherwise.
436 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200437__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
Bryan Wu95f99a42009-01-02 20:47:45 -0500438__u8 get_vfatname_block[MAX_CLUSTSIZE];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200439
wdenk7a428cc2003-06-15 22:40:42 +0000440static int
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200441get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
442 dir_entry *retdent, char *l_name)
wdenk7a428cc2003-06-15 22:40:42 +0000443{
444 dir_entry *realdent;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200445 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000446 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
447 PREFETCH_BLOCKS :
448 mydata->clust_size);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200449 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk7a428cc2003-06-15 22:40:42 +0000450 int idx = 0;
451
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300452 if (counter > VFAT_MAXSEQ) {
453 debug("Error: VFAT name is too long\n");
454 return -1;
455 }
456
457 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200458 if (counter == 0)
459 break;
wdenk2ebee312004-02-23 19:30:57 +0000460 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
461 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000462 slotptr++;
463 counter--;
464 }
465
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300466 if ((__u8 *)slotptr >= buflimit) {
wdenk7a428cc2003-06-15 22:40:42 +0000467 dir_slot *slotptr2;
468
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300469 if (curclust == 0)
470 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000471 curclust = get_fatent(mydata, curclust);
michael2c4d2982008-03-02 23:33:46 +0100472 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200473 debug("curclust: 0x%x\n", curclust);
474 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000475 return -1;
476 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200477
wdenkb6c60cb32003-10-29 23:18:55 +0000478 if (get_cluster(mydata, curclust, get_vfatname_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000479 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200480 debug("Error: reading directory block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000481 return -1;
482 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200483
484 slotptr2 = (dir_slot *)get_vfatname_block;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300485 while (counter > 0) {
486 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
487 & 0xff) != counter)
488 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000489 slotptr2++;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300490 counter--;
491 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200492
wdenk7a428cc2003-06-15 22:40:42 +0000493 /* Save the real directory entry */
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300494 realdent = (dir_entry *)slotptr2;
495 while ((__u8 *)slotptr2 > get_vfatname_block) {
wdenk7a428cc2003-06-15 22:40:42 +0000496 slotptr2--;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300497 slot2str(slotptr2, l_name, &idx);
wdenk7a428cc2003-06-15 22:40:42 +0000498 }
499 } else {
500 /* Save the real directory entry */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200501 realdent = (dir_entry *)slotptr;
wdenk7a428cc2003-06-15 22:40:42 +0000502 }
503
504 do {
505 slotptr--;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200506 if (slot2str(slotptr, l_name, &idx))
507 break;
wdenk2ebee312004-02-23 19:30:57 +0000508 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk7a428cc2003-06-15 22:40:42 +0000509
510 l_name[idx] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200511 if (*l_name == DELETED_FLAG)
512 *l_name = '\0';
513 else if (*l_name == aRING)
514 *l_name = DELETED_FLAG;
wdenk7a428cc2003-06-15 22:40:42 +0000515 downcase(l_name);
516
517 /* Return the real directory entry */
518 memcpy(retdent, realdent, sizeof(dir_entry));
519
520 return 0;
521}
522
wdenk7a428cc2003-06-15 22:40:42 +0000523/* Calculate short name checksum */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200524static __u8 mkcksum (const char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000525{
526 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200527
wdenk7a428cc2003-06-15 22:40:42 +0000528 __u8 ret = 0;
529
530 for (i = 0; i < 11; i++) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200531 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
wdenk7a428cc2003-06-15 22:40:42 +0000532 }
533
534 return ret;
535}
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200536#endif /* CONFIG_SUPPORT_VFAT */
wdenk7a428cc2003-06-15 22:40:42 +0000537
538/*
539 * Get the directory entry associated with 'filename' from the directory
540 * starting at 'startsect'
541 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200542__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
wdenkb6c60cb32003-10-29 23:18:55 +0000543__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200544
545static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
546 char *filename, dir_entry *retdent,
wdenk7a428cc2003-06-15 22:40:42 +0000547 int dols)
548{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200549 __u16 prevcksum = 0xffff;
550 __u32 curclust = START(retdent);
551 int files = 0, dirs = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000552
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200553 debug("get_dentfromdir: %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +0000554
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200555 while (1) {
556 dir_entry *dentptr;
557
558 int i;
559
560 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000561 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200562 debug("Error: reading directory block\n");
563 return NULL;
564 }
wdenk7a428cc2003-06-15 22:40:42 +0000565
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200566 dentptr = (dir_entry *)get_dentfromdir_block;
567
568 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300569 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200570
571 l_name[0] = '\0';
572 if (dentptr->name[0] == DELETED_FLAG) {
573 dentptr++;
574 continue;
575 }
576 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk7a428cc2003-06-15 22:40:42 +0000577#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand575e21e2011-10-19 07:43:08 +0000578 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
579 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200580 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
581 get_vfatname(mydata, curclust,
582 get_dentfromdir_block,
583 dentptr, l_name);
584 if (dols) {
585 int isdir;
586 char dirc;
587 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000588
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200589 isdir = (dentptr->attr & ATTR_DIR);
590
591 if (isdir) {
592 dirs++;
593 dirc = '/';
594 doit = 1;
595 } else {
596 dirc = ' ';
597 if (l_name[0] != 0) {
598 files++;
599 doit = 1;
600 }
601 }
602 if (doit) {
603 if (dirc == ' ') {
604 printf(" %8ld %s%c\n",
605 (long)FAT2CPU32(dentptr->size),
606 l_name,
607 dirc);
608 } else {
609 printf(" %s%c\n",
610 l_name,
611 dirc);
612 }
613 }
614 dentptr++;
615 continue;
616 }
617 debug("vfatname: |%s|\n", l_name);
618 } else
619#endif
620 {
621 /* Volume label or VFAT entry */
622 dentptr++;
623 continue;
624 }
wdenk7a428cc2003-06-15 22:40:42 +0000625 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200626 if (dentptr->name[0] == 0) {
627 if (dols) {
628 printf("\n%d file(s), %d dir(s)\n\n",
629 files, dirs);
630 }
631 debug("Dentname == NULL - %d\n", i);
632 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000633 }
wdenk7a428cc2003-06-15 22:40:42 +0000634#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200635 if (dols && mkcksum(dentptr->name) == prevcksum) {
636 dentptr++;
637 continue;
638 }
wdenk7a428cc2003-06-15 22:40:42 +0000639#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200640 get_name(dentptr, s_name);
641 if (dols) {
642 int isdir = (dentptr->attr & ATTR_DIR);
643 char dirc;
644 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000645
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200646 if (isdir) {
647 dirs++;
648 dirc = '/';
649 doit = 1;
650 } else {
651 dirc = ' ';
652 if (s_name[0] != 0) {
653 files++;
654 doit = 1;
655 }
656 }
wdenk7a428cc2003-06-15 22:40:42 +0000657
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200658 if (doit) {
659 if (dirc == ' ') {
660 printf(" %8ld %s%c\n",
661 (long)FAT2CPU32(dentptr->size),
662 s_name, dirc);
663 } else {
664 printf(" %s%c\n",
665 s_name, dirc);
666 }
667 }
wdenk7a428cc2003-06-15 22:40:42 +0000668
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200669 dentptr++;
670 continue;
671 }
672
673 if (strcmp(filename, s_name)
674 && strcmp(filename, l_name)) {
675 debug("Mismatch: |%s|%s|\n", s_name, l_name);
676 dentptr++;
677 continue;
678 }
679
680 memcpy(retdent, dentptr, sizeof(dir_entry));
681
682 debug("DentName: %s", s_name);
683 debug(", start: 0x%x", START(dentptr));
684 debug(", size: 0x%x %s\n",
685 FAT2CPU32(dentptr->size),
686 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
687
688 return retdent;
689 }
690
691 curclust = get_fatent(mydata, curclust);
692 if (CHECK_CLUST(curclust, mydata->fatsize)) {
693 debug("curclust: 0x%x\n", curclust);
694 printf("Invalid FAT entry\n");
695 return NULL;
696 }
wdenk7a428cc2003-06-15 22:40:42 +0000697 }
wdenk7a428cc2003-06-15 22:40:42 +0000698
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200699 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000700}
701
wdenk7a428cc2003-06-15 22:40:42 +0000702/*
703 * Read boot sector and volume info from a FAT filesystem
704 */
705static int
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200706read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000707{
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000708 __u8 *block;
wdenk7a428cc2003-06-15 22:40:42 +0000709 volume_info *vistart;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000710 int ret = 0;
711
712 if (cur_dev == NULL) {
713 debug("Error: no device selected\n");
714 return -1;
715 }
716
717 block = malloc(cur_dev->blksz);
718 if (block == NULL) {
719 debug("Error: allocating block\n");
720 return -1;
721 }
wdenk7a428cc2003-06-15 22:40:42 +0000722
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200723 if (disk_read (0, 1, block) < 0) {
724 debug("Error: reading block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000725 goto fail;
wdenk7a428cc2003-06-15 22:40:42 +0000726 }
727
728 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200729 bs->reserved = FAT2CPU16(bs->reserved);
730 bs->fat_length = FAT2CPU16(bs->fat_length);
731 bs->secs_track = FAT2CPU16(bs->secs_track);
732 bs->heads = FAT2CPU16(bs->heads);
733 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000734
735 /* FAT32 entries */
736 if (bs->fat_length == 0) {
737 /* Assume FAT32 */
738 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200739 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000740 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200741 bs->info_sector = FAT2CPU16(bs->info_sector);
742 bs->backup_boot = FAT2CPU16(bs->backup_boot);
743 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000744 *fatsize = 32;
745 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200746 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000747 *fatsize = 0;
748 }
749 memcpy(volinfo, vistart, sizeof(volume_info));
750
wdenk7a428cc2003-06-15 22:40:42 +0000751 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200752 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000753 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000754 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500755 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000756 *fatsize = 12;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000757 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000758 }
Tom Rix155abaa2009-05-20 07:55:41 -0500759 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000760 *fatsize = 16;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000761 goto exit;
wdenk7a428cc2003-06-15 22:40:42 +0000762 }
763 }
764
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200765 debug("Error: broken fs_type sign\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000766fail:
767 ret = -1;
768exit:
769 free(block);
770 return ret;
wdenk7a428cc2003-06-15 22:40:42 +0000771}
772
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200773__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
Bryan Wu95f99a42009-01-02 20:47:45 -0500774__u8 do_fat_read_block[MAX_CLUSTSIZE];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200775
stroese83082242004-12-16 17:57:26 +0000776long
wdenk7a428cc2003-06-15 22:40:42 +0000777do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
778 int dols)
779{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200780 char fnamecopy[2048];
781 boot_sector bs;
782 volume_info volinfo;
783 fsdata datablock;
784 fsdata *mydata = &datablock;
785 dir_entry *dentptr;
786 __u16 prevcksum = 0xffff;
787 char *subname = "";
Erik Hansen4ea89662011-03-24 10:15:37 +0100788 __u32 cursect;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200789 int idx, isdir = 0;
790 int files = 0, dirs = 0;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000791 long ret = -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200792 int firsttime;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000793 __u32 root_cluster = 0;
Erik Hansen4ea89662011-03-24 10:15:37 +0100794 int rootdir_size = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200795 int j;
wdenk7a428cc2003-06-15 22:40:42 +0000796
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200797 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
798 debug("Error: reading boot sector\n");
799 return -1;
800 }
801
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000802 if (mydata->fatsize == 32) {
803 root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200804 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000805 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200806 mydata->fatlength = bs.fat_length;
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000807 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200808
809 mydata->fat_sect = bs.reserved;
810
811 cursect = mydata->rootdir_sect
812 = mydata->fat_sect + mydata->fatlength * bs.fats;
813
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000814 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200815 mydata->clust_size = bs.cluster_size;
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200816
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200817 if (mydata->fatsize == 32) {
818 mydata->data_begin = mydata->rootdir_sect -
819 (mydata->clust_size * 2);
820 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200821 rootdir_size = ((bs.dir_entries[1] * (int)256 +
822 bs.dir_entries[0]) *
823 sizeof(dir_entry)) /
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000824 mydata->sect_size;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200825 mydata->data_begin = mydata->rootdir_sect +
826 rootdir_size -
827 (mydata->clust_size * 2);
828 }
829
830 mydata->fatbufnum = -1;
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000831 mydata->fatbuf = malloc(FATBUFSIZE);
832 if (mydata->fatbuf == NULL) {
833 debug("Error: allocating memory\n");
834 return -1;
835 }
wdenk7a428cc2003-06-15 22:40:42 +0000836
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200837#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200838 debug("VFAT Support enabled\n");
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200839#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200840 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
841 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
842 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
843 "Data begins at: %d\n",
844 root_cluster,
845 mydata->rootdir_sect,
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000846 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
847 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
848 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000849
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200850 /* "cwd" is always the root... */
851 while (ISDIRDELIM(*filename))
852 filename++;
wdenk7a428cc2003-06-15 22:40:42 +0000853
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200854 /* Make a copy of the filename and convert it to lowercase */
855 strcpy(fnamecopy, filename);
856 downcase(fnamecopy);
wdenk7a428cc2003-06-15 22:40:42 +0000857
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200858 if (*fnamecopy == '\0') {
859 if (!dols)
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000860 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200861
862 dols = LS_ROOT;
863 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
864 isdir = 1;
865 fnamecopy[idx] = '\0';
866 subname = fnamecopy + idx + 1;
867
868 /* Handle multiple delimiters */
869 while (ISDIRDELIM(*subname))
870 subname++;
871 } else if (dols) {
872 isdir = 1;
wdenk7a428cc2003-06-15 22:40:42 +0000873 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200874
875 j = 0;
876 while (1) {
877 int i;
878
879 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%d\n",
880 cursect, mydata->clust_size, DIRENTSPERBLOCK);
881
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300882 if (disk_read(cursect,
883 (mydata->fatsize == 32) ?
884 (mydata->clust_size) :
Sergei Shtylyov51de6cf2011-08-19 09:37:46 +0000885 PREFETCH_BLOCKS,
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300886 do_fat_read_block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200887 debug("Error: reading rootdir block\n");
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000888 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200889 }
890
891 dentptr = (dir_entry *) do_fat_read_block;
892
893 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300894 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
wdenk7a428cc2003-06-15 22:40:42 +0000895
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200896 l_name[0] = '\0';
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300897 if (dentptr->name[0] == DELETED_FLAG) {
898 dentptr++;
899 continue;
900 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200901 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk7a428cc2003-06-15 22:40:42 +0000902#ifdef CONFIG_SUPPORT_VFAT
J. Vijayanand575e21e2011-10-19 07:43:08 +0000903 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200904 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
905 prevcksum =
906 ((dir_slot *)dentptr)->alias_checksum;
wdenk7a428cc2003-06-15 22:40:42 +0000907
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300908 get_vfatname(mydata,
Sergei Shtylyov42cdc9f2011-08-19 09:32:34 +0000909 root_cluster,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200910 do_fat_read_block,
911 dentptr, l_name);
912
913 if (dols == LS_ROOT) {
914 char dirc;
915 int doit = 0;
916 int isdir =
917 (dentptr->attr & ATTR_DIR);
918
919 if (isdir) {
920 dirs++;
921 dirc = '/';
922 doit = 1;
923 } else {
924 dirc = ' ';
925 if (l_name[0] != 0) {
926 files++;
927 doit = 1;
928 }
929 }
930 if (doit) {
931 if (dirc == ' ') {
932 printf(" %8ld %s%c\n",
933 (long)FAT2CPU32(dentptr->size),
934 l_name,
935 dirc);
936 } else {
937 printf(" %s%c\n",
938 l_name,
939 dirc);
940 }
941 }
942 dentptr++;
943 continue;
944 }
945 debug("Rootvfatname: |%s|\n",
946 l_name);
947 } else
wdenk7a428cc2003-06-15 22:40:42 +0000948#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200949 {
950 /* Volume label or VFAT entry */
951 dentptr++;
952 continue;
953 }
954 } else if (dentptr->name[0] == 0) {
955 debug("RootDentname == NULL - %d\n", i);
956 if (dols == LS_ROOT) {
957 printf("\n%d file(s), %d dir(s)\n\n",
958 files, dirs);
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000959 ret = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200960 }
Sergei Shtylyov5ab3ba72011-08-08 09:38:33 +0000961 goto exit;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200962 }
wdenk7a428cc2003-06-15 22:40:42 +0000963#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200964 else if (dols == LS_ROOT &&
965 mkcksum(dentptr->name) == prevcksum) {
966 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
Wolfgang Denk927b8ce2010-07-19 11:37:00 +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) || \
unsik Kimf0b1bdd2009-02-25 11:31:24 +09001128 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang853208e2008-12-09 23:20:18 -05001129 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001130 defined(CONFIG_CMD_SCSI) || \
1131 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001132 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001133 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001134 switch (cur_dev->if_type) {
1135 case IF_TYPE_IDE:
1136 printf("IDE");
1137 break;
1138 case IF_TYPE_SATA:
1139 printf("SATA");
1140 break;
1141 case IF_TYPE_SCSI:
1142 printf("SCSI");
1143 break;
1144 case IF_TYPE_ATAPI:
1145 printf("ATAPI");
1146 break;
1147 case IF_TYPE_USB:
1148 printf("USB");
1149 break;
1150 case IF_TYPE_DOC:
1151 printf("DOC");
1152 break;
1153 case IF_TYPE_MMC:
1154 printf("MMC");
1155 break;
1156 default:
1157 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001158 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001159
1160 printf("\n Device %d: ", cur_dev->dev);
wdenk2c9b05d2003-09-10 22:30:53 +00001161 dev_print(cur_dev);
1162#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001163
1164 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001165 printf("\nNo valid FAT fs found\n");
1166 return 1;
1167 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001168
1169 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001170 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001171 volinfo.fs_type[5] = '\0';
1172
1173 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part,
1174 volinfo.fs_type, vol_label);
1175
wdenk2c9b05d2003-09-10 22:30:53 +00001176 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001177}
1178
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001179int file_fat_ls (const char *dir)
wdenk7a428cc2003-06-15 22:40:42 +00001180{
1181 return do_fat_read(dir, NULL, 0, LS_YES);
1182}
1183
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001184long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +00001185{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001186 printf("reading %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +00001187 return do_fat_read(filename, buffer, maxsize, LS_NO);
1188}