blob: 756ac64a8b7b98287788702728134947f22b15ce [file] [log] [blame]
wdenk71f95112003-06-15 22:40:42 +00001/*
2 * fat.c
3 *
4 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
5 *
6 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
7 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
8 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
28#include <common.h>
29#include <config.h>
Sergei Shtylyovac497772011-08-08 09:38:33 +000030#include <exports.h>
wdenk71f95112003-06-15 22:40:42 +000031#include <fat.h>
32#include <asm/byteorder.h>
wdenk7205e402003-09-10 22:30:53 +000033#include <part.h>
wdenk71f95112003-06-15 22:40:42 +000034
wdenk71f95112003-06-15 22:40:42 +000035/*
36 * Convert a string to lowercase.
37 */
Wolfgang Denk7385c282010-07-19 11:37:00 +020038static void downcase (char *str)
wdenk71f95112003-06-15 22:40:42 +000039{
40 while (*str != '\0') {
41 TOLOWER(*str);
42 str++;
43 }
44}
45
Wolfgang Denk7385c282010-07-19 11:37:00 +020046static block_dev_desc_t *cur_dev = NULL;
47
wdenk7205e402003-09-10 22:30:53 +000048static unsigned long part_offset = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +020049
wdenk7205e402003-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 Denk66c2d732010-07-19 11:36:57 +020055#define DOS_FS32_TYPE_OFFSET 0x52
wdenk71f95112003-06-15 22:40:42 +000056
Wolfgang Denk7385c282010-07-19 11:37:00 +020057static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
wdenk71f95112003-06-15 22:40:42 +000058{
wdenk7205e402003-09-10 22:30:53 +000059 if (cur_dev == NULL)
60 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020061
62 startblock += part_offset;
63
wdenk7205e402003-09-10 22:30:53 +000064 if (cur_dev->block_read) {
Wolfgang Denk7385c282010-07-19 11:37:00 +020065 return cur_dev->block_read(cur_dev->dev, startblock, getsize,
66 (unsigned long *) bufptr);
wdenk71f95112003-06-15 22:40:42 +000067 }
68 return -1;
69}
70
Wolfgang Denk7385c282010-07-19 11:37:00 +020071int fat_register_device (block_dev_desc_t * dev_desc, int part_no)
wdenk71f95112003-06-15 22:40:42 +000072{
Sergei Shtylyovac497772011-08-08 09:38:33 +000073 unsigned char buffer[dev_desc->blksz];
Heiko Schocher566a4942007-06-22 19:11:54 +020074 disk_partition_t info;
wdenk7205e402003-09-10 22:30:53 +000075
76 if (!dev_desc->block_read)
77 return -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +020078
Heiko Schocher566a4942007-06-22 19:11:54 +020079 cur_dev = dev_desc;
wdenk7205e402003-09-10 22:30:53 +000080 /* check if we have a MBR (on floppies we have only a PBR) */
Wolfgang Denk7385c282010-07-19 11:37:00 +020081 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)buffer) != 1) {
82 printf("** Can't read from device %d **\n",
83 dev_desc->dev);
wdenk7205e402003-09-10 22:30:53 +000084 return -1;
85 }
86 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
Wolfgang Denk7385c282010-07-19 11:37:00 +020087 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
wdenk7205e402003-09-10 22:30:53 +000088 /* no signature found */
89 return -1;
90 }
Jon Loeligerdd60d122007-07-09 17:56:50 -050091#if (defined(CONFIG_CMD_IDE) || \
unsik Kim75eb82e2009-02-25 11:31:24 +090092 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -050093 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -050094 defined(CONFIG_CMD_SCSI) || \
95 defined(CONFIG_CMD_USB) || \
Andy Fleming02df4a22008-01-09 13:51:32 -060096 defined(CONFIG_MMC) || \
97 defined(CONFIG_SYSTEMACE) )
Wolfgang Denk7385c282010-07-19 11:37:00 +020098 /* First we assume there is a MBR */
99 if (!get_partition_info(dev_desc, part_no, &info)) {
Andy Fleming02df4a22008-01-09 13:51:32 -0600100 part_offset = info.start;
101 cur_part = part_no;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200102 } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
103 (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
Andy Fleming02df4a22008-01-09 13:51:32 -0600104 /* ok, we assume we are on a PBR only */
105 cur_part = 1;
106 part_offset = 0;
107 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200108 printf("** Partition %d not valid on device %d **\n",
109 part_no, dev_desc->dev);
Andy Fleming02df4a22008-01-09 13:51:32 -0600110 return -1;
Wolfgang Denkbf1060e2007-08-07 16:02:13 +0200111 }
Andy Fleming02df4a22008-01-09 13:51:32 -0600112
113#else
Wolfgang Denk66c2d732010-07-19 11:36:57 +0200114 if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
115 (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
Andy Fleming02df4a22008-01-09 13:51:32 -0600116 /* ok, we assume we are on a PBR only */
117 cur_part = 1;
118 part_offset = 0;
119 info.start = part_offset;
120 } else {
121 /* FIXME we need to determine the start block of the
122 * partition where the DOS FS resides. This can be done
123 * by using the get_partition_info routine. For this
124 * purpose the libpart must be included.
125 */
126 part_offset = 32;
127 cur_part = 1;
128 }
129#endif
wdenk71f95112003-06-15 22:40:42 +0000130 return 0;
131}
132
wdenk71f95112003-06-15 22:40:42 +0000133/*
134 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
135 * Return index into string if found, -1 otherwise.
136 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200137static int dirdelim (char *str)
wdenk71f95112003-06-15 22:40:42 +0000138{
139 char *start = str;
140
141 while (*str != '\0') {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200142 if (ISDIRDELIM(*str))
143 return str - start;
wdenk71f95112003-06-15 22:40:42 +0000144 str++;
145 }
146 return -1;
147}
148
wdenk71f95112003-06-15 22:40:42 +0000149/*
150 * Extract zero terminated short name from a directory entry.
151 */
152static void get_name (dir_entry *dirent, char *s_name)
153{
154 char *ptr;
155
Wolfgang Denk7385c282010-07-19 11:37:00 +0200156 memcpy(s_name, dirent->name, 8);
wdenk71f95112003-06-15 22:40:42 +0000157 s_name[8] = '\0';
158 ptr = s_name;
159 while (*ptr && *ptr != ' ')
160 ptr++;
161 if (dirent->ext[0] && dirent->ext[0] != ' ') {
162 *ptr = '.';
163 ptr++;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200164 memcpy(ptr, dirent->ext, 3);
wdenk71f95112003-06-15 22:40:42 +0000165 ptr[3] = '\0';
166 while (*ptr && *ptr != ' ')
167 ptr++;
168 }
169 *ptr = '\0';
170 if (*s_name == DELETED_FLAG)
171 *s_name = '\0';
172 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100173 *s_name = DELETED_FLAG;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200174 downcase(s_name);
wdenk71f95112003-06-15 22:40:42 +0000175}
176
177/*
178 * Get the entry at index 'entry' in a FAT (12/16/32) table.
179 * On failure 0x00 is returned.
180 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200181static __u32 get_fatent (fsdata *mydata, __u32 entry)
wdenk71f95112003-06-15 22:40:42 +0000182{
183 __u32 bufnum;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200184 __u32 off16, offset;
wdenk71f95112003-06-15 22:40:42 +0000185 __u32 ret = 0x00;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200186 __u16 val1, val2;
wdenk71f95112003-06-15 22:40:42 +0000187
188 switch (mydata->fatsize) {
189 case 32:
190 bufnum = entry / FAT32BUFSIZE;
191 offset = entry - bufnum * FAT32BUFSIZE;
192 break;
193 case 16:
194 bufnum = entry / FAT16BUFSIZE;
195 offset = entry - bufnum * FAT16BUFSIZE;
196 break;
197 case 12:
198 bufnum = entry / FAT12BUFSIZE;
199 offset = entry - bufnum * FAT12BUFSIZE;
200 break;
201
202 default:
203 /* Unsupported FAT size */
204 return ret;
205 }
206
Wolfgang Denk7385c282010-07-19 11:37:00 +0200207 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
208 mydata->fatsize, entry, entry, offset, offset);
Wolfgang Denk2aa98c62010-07-19 11:36:58 +0200209
wdenk71f95112003-06-15 22:40:42 +0000210 /* Read a new block of FAT entries into the cache. */
211 if (bufnum != mydata->fatbufnum) {
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000212 __u32 getsize = FATBUFBLOCKS;
wdenk71f95112003-06-15 22:40:42 +0000213 __u8 *bufptr = mydata->fatbuf;
214 __u32 fatlength = mydata->fatlength;
215 __u32 startblock = bufnum * FATBUFBLOCKS;
216
Sergei Shtylyov60b36f02011-08-08 09:39:29 +0000217 if (getsize > fatlength)
218 getsize = fatlength;
219
Sergei Shtylyovac497772011-08-08 09:38:33 +0000220 fatlength *= mydata->sect_size; /* We want it in bytes now */
wdenk71f95112003-06-15 22:40:42 +0000221 startblock += mydata->fat_sect; /* Offset from start of disk */
222
wdenk71f95112003-06-15 22:40:42 +0000223 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200224 debug("Error reading FAT blocks\n");
wdenk71f95112003-06-15 22:40:42 +0000225 return ret;
226 }
227 mydata->fatbufnum = bufnum;
228 }
229
230 /* Get the actual entry from the table */
231 switch (mydata->fatsize) {
232 case 32:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200233 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000234 break;
235 case 16:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200236 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk71f95112003-06-15 22:40:42 +0000237 break;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200238 case 12:
239 off16 = (offset * 3) / 4;
wdenk71f95112003-06-15 22:40:42 +0000240
241 switch (offset & 0x3) {
242 case 0:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200243 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000244 ret &= 0xfff;
245 break;
246 case 1:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200247 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000248 val1 &= 0xf000;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200249 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk71f95112003-06-15 22:40:42 +0000250 val2 &= 0x00ff;
251 ret = (val2 << 4) | (val1 >> 12);
252 break;
253 case 2:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200254 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000255 val1 &= 0xff00;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200256 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk71f95112003-06-15 22:40:42 +0000257 val2 &= 0x000f;
258 ret = (val2 << 8) | (val1 >> 8);
259 break;
260 case 3:
Wolfgang Denk7385c282010-07-19 11:37:00 +0200261 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk71f95112003-06-15 22:40:42 +0000262 ret = (ret & 0xfff0) >> 4;
263 break;
264 default:
265 break;
266 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200267 break;
wdenk71f95112003-06-15 22:40:42 +0000268 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200269 debug("FAT%d: ret: %08x, offset: %04x\n",
270 mydata->fatsize, ret, offset);
wdenk71f95112003-06-15 22:40:42 +0000271
272 return ret;
273}
274
wdenk71f95112003-06-15 22:40:42 +0000275/*
276 * Read at most 'size' bytes from the specified cluster into 'buffer'.
277 * Return 0 on success, -1 otherwise.
278 */
279static int
Wolfgang Denk7385c282010-07-19 11:37:00 +0200280get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
281 unsigned long size)
wdenk71f95112003-06-15 22:40:42 +0000282{
Erik Hansen3f270f42011-03-24 10:15:37 +0100283 __u32 idx = 0;
wdenk71f95112003-06-15 22:40:42 +0000284 __u32 startsect;
285
286 if (clustnum > 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200287 startsect = mydata->data_begin +
288 clustnum * mydata->clust_size;
wdenk71f95112003-06-15 22:40:42 +0000289 } else {
290 startsect = mydata->rootdir_sect;
291 }
292
Wolfgang Denk7385c282010-07-19 11:37:00 +0200293 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
294
Sergei Shtylyovac497772011-08-08 09:38:33 +0000295 if (disk_read(startsect, size / mydata->sect_size, buffer) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200296 debug("Error reading data\n");
wdenk7205e402003-09-10 22:30:53 +0000297 return -1;
298 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000299 if (size % mydata->sect_size) {
300 __u8 tmpbuf[mydata->sect_size];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200301
Sergei Shtylyovac497772011-08-08 09:38:33 +0000302 idx = size / mydata->sect_size;
wdenk7205e402003-09-10 22:30:53 +0000303 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200304 debug("Error reading data\n");
wdenk7205e402003-09-10 22:30:53 +0000305 return -1;
wdenk71f95112003-06-15 22:40:42 +0000306 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000307 buffer += idx * mydata->sect_size;
wdenk7205e402003-09-10 22:30:53 +0000308
Sergei Shtylyovac497772011-08-08 09:38:33 +0000309 memcpy(buffer, tmpbuf, size % mydata->sect_size);
wdenk7205e402003-09-10 22:30:53 +0000310 return 0;
wdenk71f95112003-06-15 22:40:42 +0000311 }
312
313 return 0;
314}
315
wdenk71f95112003-06-15 22:40:42 +0000316/*
317 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
318 * into 'buffer'.
319 * Return the number of bytes read or -1 on fatal errors.
320 */
321static long
Wolfgang Denk7385c282010-07-19 11:37:00 +0200322get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
323 unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +0000324{
325 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000326 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
wdenk71f95112003-06-15 22:40:42 +0000327 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000328 __u32 endclust, newclust;
329 unsigned long actsize;
wdenk71f95112003-06-15 22:40:42 +0000330
Wolfgang Denk7385c282010-07-19 11:37:00 +0200331 debug("Filesize: %ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000332
Wolfgang Denk7385c282010-07-19 11:37:00 +0200333 if (maxsize > 0 && filesize > maxsize)
334 filesize = maxsize;
wdenk71f95112003-06-15 22:40:42 +0000335
Wolfgang Denk7385c282010-07-19 11:37:00 +0200336 debug("%ld bytes\n", filesize);
wdenk71f95112003-06-15 22:40:42 +0000337
Wolfgang Denk7385c282010-07-19 11:37:00 +0200338 actsize = bytesperclust;
339 endclust = curclust;
340
wdenk71f95112003-06-15 22:40:42 +0000341 do {
wdenk7205e402003-09-10 22:30:53 +0000342 /* search for consecutive clusters */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200343 while (actsize < filesize) {
wdenk7205e402003-09-10 22:30:53 +0000344 newclust = get_fatent(mydata, endclust);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200345 if ((newclust - 1) != endclust)
wdenk7205e402003-09-10 22:30:53 +0000346 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100347 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200348 debug("curclust: 0x%x\n", newclust);
349 debug("Invalid FAT entry\n");
wdenk7205e402003-09-10 22:30:53 +0000350 return gotsize;
351 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200352 endclust = newclust;
353 actsize += bytesperclust;
wdenk7205e402003-09-10 22:30:53 +0000354 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200355
wdenk7205e402003-09-10 22:30:53 +0000356 /* actsize >= file size */
357 actsize -= bytesperclust;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200358
wdenk7205e402003-09-10 22:30:53 +0000359 /* get remaining clusters */
360 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200361 printf("Error reading cluster\n");
wdenk71f95112003-06-15 22:40:42 +0000362 return -1;
363 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200364
wdenk7205e402003-09-10 22:30:53 +0000365 /* get remaining bytes */
366 gotsize += (int)actsize;
367 filesize -= actsize;
368 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200369 actsize = filesize;
wdenk7205e402003-09-10 22:30:53 +0000370 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200371 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000372 return -1;
373 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200374 gotsize += actsize;
wdenk7205e402003-09-10 22:30:53 +0000375 return gotsize;
376getit:
377 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200378 printf("Error reading cluster\n");
wdenk7205e402003-09-10 22:30:53 +0000379 return -1;
380 }
381 gotsize += (int)actsize;
382 filesize -= actsize;
383 buffer += actsize;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200384
wdenk7205e402003-09-10 22:30:53 +0000385 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100386 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200387 debug("curclust: 0x%x\n", curclust);
388 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000389 return gotsize;
390 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200391 actsize = bytesperclust;
392 endclust = curclust;
wdenk71f95112003-06-15 22:40:42 +0000393 } while (1);
394}
395
wdenk71f95112003-06-15 22:40:42 +0000396#ifdef CONFIG_SUPPORT_VFAT
397/*
398 * Extract the file name information from 'slotptr' into 'l_name',
399 * starting at l_name[*idx].
400 * Return 1 if terminator (zero byte) is found, 0 otherwise.
401 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200402static int slot2str (dir_slot *slotptr, char *l_name, int *idx)
wdenk71f95112003-06-15 22:40:42 +0000403{
404 int j;
405
406 for (j = 0; j <= 8; j += 2) {
407 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200408 if (l_name[*idx] == 0x00)
409 return 1;
wdenk71f95112003-06-15 22:40:42 +0000410 (*idx)++;
411 }
412 for (j = 0; j <= 10; j += 2) {
413 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200414 if (l_name[*idx] == 0x00)
415 return 1;
wdenk71f95112003-06-15 22:40:42 +0000416 (*idx)++;
417 }
418 for (j = 0; j <= 2; j += 2) {
419 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200420 if (l_name[*idx] == 0x00)
421 return 1;
wdenk71f95112003-06-15 22:40:42 +0000422 (*idx)++;
423 }
424
425 return 0;
426}
427
wdenk71f95112003-06-15 22:40:42 +0000428/*
429 * Extract the full long filename starting at 'retdent' (which is really
430 * a slot) into 'l_name'. If successful also copy the real directory entry
431 * into 'retdent'
432 * Return 0 on success, -1 otherwise.
433 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200434__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
Bryan Wu7e4b9b42009-01-02 20:47:45 -0500435__u8 get_vfatname_block[MAX_CLUSTSIZE];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200436
wdenk71f95112003-06-15 22:40:42 +0000437static int
Wolfgang Denk7385c282010-07-19 11:37:00 +0200438get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
439 dir_entry *retdent, char *l_name)
wdenk71f95112003-06-15 22:40:42 +0000440{
441 dir_entry *realdent;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200442 dir_slot *slotptr = (dir_slot *)retdent;
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000443 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
444 PREFETCH_BLOCKS :
445 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200446 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000447 int idx = 0;
448
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300449 if (counter > VFAT_MAXSEQ) {
450 debug("Error: VFAT name is too long\n");
451 return -1;
452 }
453
454 while ((__u8 *)slotptr < buflimit) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200455 if (counter == 0)
456 break;
wdenk2d1a5372004-02-23 19:30:57 +0000457 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
458 return -1;
wdenk71f95112003-06-15 22:40:42 +0000459 slotptr++;
460 counter--;
461 }
462
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300463 if ((__u8 *)slotptr >= buflimit) {
wdenk71f95112003-06-15 22:40:42 +0000464 dir_slot *slotptr2;
465
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300466 if (curclust == 0)
467 return -1;
wdenk71f95112003-06-15 22:40:42 +0000468 curclust = get_fatent(mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100469 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200470 debug("curclust: 0x%x\n", curclust);
471 printf("Invalid FAT entry\n");
wdenk71f95112003-06-15 22:40:42 +0000472 return -1;
473 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200474
wdenk5fa66df2003-10-29 23:18:55 +0000475 if (get_cluster(mydata, curclust, get_vfatname_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000476 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200477 debug("Error: reading directory block\n");
wdenk71f95112003-06-15 22:40:42 +0000478 return -1;
479 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200480
481 slotptr2 = (dir_slot *)get_vfatname_block;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300482 while (counter > 0) {
483 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
484 & 0xff) != counter)
485 return -1;
wdenk71f95112003-06-15 22:40:42 +0000486 slotptr2++;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300487 counter--;
488 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200489
wdenk71f95112003-06-15 22:40:42 +0000490 /* Save the real directory entry */
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300491 realdent = (dir_entry *)slotptr2;
492 while ((__u8 *)slotptr2 > get_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000493 slotptr2--;
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300494 slot2str(slotptr2, l_name, &idx);
wdenk71f95112003-06-15 22:40:42 +0000495 }
496 } else {
497 /* Save the real directory entry */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200498 realdent = (dir_entry *)slotptr;
wdenk71f95112003-06-15 22:40:42 +0000499 }
500
501 do {
502 slotptr--;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200503 if (slot2str(slotptr, l_name, &idx))
504 break;
wdenk2d1a5372004-02-23 19:30:57 +0000505 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000506
507 l_name[idx] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +0200508 if (*l_name == DELETED_FLAG)
509 *l_name = '\0';
510 else if (*l_name == aRING)
511 *l_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000512 downcase(l_name);
513
514 /* Return the real directory entry */
515 memcpy(retdent, realdent, sizeof(dir_entry));
516
517 return 0;
518}
519
wdenk71f95112003-06-15 22:40:42 +0000520/* Calculate short name checksum */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200521static __u8 mkcksum (const char *str)
wdenk71f95112003-06-15 22:40:42 +0000522{
523 int i;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200524
wdenk71f95112003-06-15 22:40:42 +0000525 __u8 ret = 0;
526
527 for (i = 0; i < 11; i++) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200528 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
wdenk71f95112003-06-15 22:40:42 +0000529 }
530
531 return ret;
532}
Wolfgang Denk7385c282010-07-19 11:37:00 +0200533#endif /* CONFIG_SUPPORT_VFAT */
wdenk71f95112003-06-15 22:40:42 +0000534
535/*
536 * Get the directory entry associated with 'filename' from the directory
537 * starting at 'startsect'
538 */
Wolfgang Denk7385c282010-07-19 11:37:00 +0200539__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
wdenk5fa66df2003-10-29 23:18:55 +0000540__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200541
542static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
543 char *filename, dir_entry *retdent,
wdenk71f95112003-06-15 22:40:42 +0000544 int dols)
545{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200546 __u16 prevcksum = 0xffff;
547 __u32 curclust = START(retdent);
548 int files = 0, dirs = 0;
wdenk71f95112003-06-15 22:40:42 +0000549
Wolfgang Denk7385c282010-07-19 11:37:00 +0200550 debug("get_dentfromdir: %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +0000551
Wolfgang Denk7385c282010-07-19 11:37:00 +0200552 while (1) {
553 dir_entry *dentptr;
wdenk71f95112003-06-15 22:40:42 +0000554
Wolfgang Denk7385c282010-07-19 11:37:00 +0200555 int i;
wdenk71f95112003-06-15 22:40:42 +0000556
Wolfgang Denk7385c282010-07-19 11:37:00 +0200557 if (get_cluster(mydata, curclust, get_dentfromdir_block,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000558 mydata->clust_size * mydata->sect_size) != 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200559 debug("Error: reading directory block\n");
560 return NULL;
561 }
562
563 dentptr = (dir_entry *)get_dentfromdir_block;
564
565 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300566 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200567
568 l_name[0] = '\0';
569 if (dentptr->name[0] == DELETED_FLAG) {
570 dentptr++;
571 continue;
wdenk71f95112003-06-15 22:40:42 +0000572 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200573 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000574#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk7385c282010-07-19 11:37:00 +0200575 if ((dentptr->attr & ATTR_VFAT) &&
576 (dentptr-> name[0] & LAST_LONG_ENTRY_MASK)) {
577 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
578 get_vfatname(mydata, curclust,
579 get_dentfromdir_block,
580 dentptr, l_name);
581 if (dols) {
582 int isdir;
583 char dirc;
584 int doit = 0;
585
586 isdir = (dentptr->attr & ATTR_DIR);
587
588 if (isdir) {
589 dirs++;
590 dirc = '/';
591 doit = 1;
592 } else {
593 dirc = ' ';
594 if (l_name[0] != 0) {
595 files++;
596 doit = 1;
597 }
598 }
599 if (doit) {
600 if (dirc == ' ') {
601 printf(" %8ld %s%c\n",
602 (long)FAT2CPU32(dentptr->size),
603 l_name,
604 dirc);
605 } else {
606 printf(" %s%c\n",
607 l_name,
608 dirc);
609 }
610 }
611 dentptr++;
612 continue;
613 }
614 debug("vfatname: |%s|\n", l_name);
615 } else
wdenk71f95112003-06-15 22:40:42 +0000616#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200617 {
618 /* Volume label or VFAT entry */
619 dentptr++;
620 continue;
621 }
622 }
623 if (dentptr->name[0] == 0) {
624 if (dols) {
625 printf("\n%d file(s), %d dir(s)\n\n",
626 files, dirs);
627 }
628 debug("Dentname == NULL - %d\n", i);
629 return NULL;
630 }
631#ifdef CONFIG_SUPPORT_VFAT
632 if (dols && mkcksum(dentptr->name) == prevcksum) {
633 dentptr++;
634 continue;
635 }
636#endif
637 get_name(dentptr, s_name);
638 if (dols) {
639 int isdir = (dentptr->attr & ATTR_DIR);
640 char dirc;
641 int doit = 0;
wdenk71f95112003-06-15 22:40:42 +0000642
Wolfgang Denk7385c282010-07-19 11:37:00 +0200643 if (isdir) {
644 dirs++;
645 dirc = '/';
646 doit = 1;
647 } else {
648 dirc = ' ';
649 if (s_name[0] != 0) {
650 files++;
651 doit = 1;
652 }
653 }
654
655 if (doit) {
656 if (dirc == ' ') {
657 printf(" %8ld %s%c\n",
658 (long)FAT2CPU32(dentptr->size),
659 s_name, dirc);
660 } else {
661 printf(" %s%c\n",
662 s_name, dirc);
663 }
664 }
665
666 dentptr++;
667 continue;
668 }
669
670 if (strcmp(filename, s_name)
671 && strcmp(filename, l_name)) {
672 debug("Mismatch: |%s|%s|\n", s_name, l_name);
673 dentptr++;
674 continue;
675 }
676
677 memcpy(retdent, dentptr, sizeof(dir_entry));
678
679 debug("DentName: %s", s_name);
680 debug(", start: 0x%x", START(dentptr));
681 debug(", size: 0x%x %s\n",
682 FAT2CPU32(dentptr->size),
683 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
684
685 return retdent;
wdenk71f95112003-06-15 22:40:42 +0000686 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200687
688 curclust = get_fatent(mydata, curclust);
689 if (CHECK_CLUST(curclust, mydata->fatsize)) {
690 debug("curclust: 0x%x\n", curclust);
691 printf("Invalid FAT entry\n");
692 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000693 }
wdenk71f95112003-06-15 22:40:42 +0000694 }
wdenk71f95112003-06-15 22:40:42 +0000695
Wolfgang Denk7385c282010-07-19 11:37:00 +0200696 return NULL;
wdenk71f95112003-06-15 22:40:42 +0000697}
698
wdenk71f95112003-06-15 22:40:42 +0000699/*
700 * Read boot sector and volume info from a FAT filesystem
701 */
702static int
Wolfgang Denk7385c282010-07-19 11:37:00 +0200703read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk71f95112003-06-15 22:40:42 +0000704{
Sergei Shtylyovac497772011-08-08 09:38:33 +0000705 __u8 *block;
wdenk71f95112003-06-15 22:40:42 +0000706 volume_info *vistart;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000707 int ret = 0;
708
709 if (cur_dev == NULL) {
710 debug("Error: no device selected\n");
711 return -1;
712 }
713
714 block = malloc(cur_dev->blksz);
715 if (block == NULL) {
716 debug("Error: allocating block\n");
717 return -1;
718 }
wdenk71f95112003-06-15 22:40:42 +0000719
Wolfgang Denk7385c282010-07-19 11:37:00 +0200720 if (disk_read (0, 1, block) < 0) {
721 debug("Error: reading block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000722 goto fail;
wdenk71f95112003-06-15 22:40:42 +0000723 }
724
725 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk7385c282010-07-19 11:37:00 +0200726 bs->reserved = FAT2CPU16(bs->reserved);
727 bs->fat_length = FAT2CPU16(bs->fat_length);
728 bs->secs_track = FAT2CPU16(bs->secs_track);
729 bs->heads = FAT2CPU16(bs->heads);
730 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk71f95112003-06-15 22:40:42 +0000731
732 /* FAT32 entries */
733 if (bs->fat_length == 0) {
734 /* Assume FAT32 */
735 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200736 bs->flags = FAT2CPU16(bs->flags);
wdenk71f95112003-06-15 22:40:42 +0000737 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200738 bs->info_sector = FAT2CPU16(bs->info_sector);
739 bs->backup_boot = FAT2CPU16(bs->backup_boot);
740 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk71f95112003-06-15 22:40:42 +0000741 *fatsize = 32;
742 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200743 vistart = (volume_info *)&(bs->fat32_length);
wdenk71f95112003-06-15 22:40:42 +0000744 *fatsize = 0;
745 }
746 memcpy(volinfo, vistart, sizeof(volume_info));
747
wdenk71f95112003-06-15 22:40:42 +0000748 if (*fatsize == 32) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200749 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000750 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000751 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500752 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000753 *fatsize = 12;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000754 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000755 }
Tom Rix651351f2009-05-20 07:55:41 -0500756 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000757 *fatsize = 16;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000758 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000759 }
760 }
761
Wolfgang Denk7385c282010-07-19 11:37:00 +0200762 debug("Error: broken fs_type sign\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000763fail:
764 ret = -1;
765exit:
766 free(block);
767 return ret;
wdenk71f95112003-06-15 22:40:42 +0000768}
769
Wolfgang Denk7385c282010-07-19 11:37:00 +0200770__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
Bryan Wu7e4b9b42009-01-02 20:47:45 -0500771__u8 do_fat_read_block[MAX_CLUSTSIZE];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200772
stroese20cc00d2004-12-16 17:57:26 +0000773long
wdenk71f95112003-06-15 22:40:42 +0000774do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
775 int dols)
776{
Wolfgang Denk7385c282010-07-19 11:37:00 +0200777 char fnamecopy[2048];
778 boot_sector bs;
779 volume_info volinfo;
780 fsdata datablock;
781 fsdata *mydata = &datablock;
782 dir_entry *dentptr;
783 __u16 prevcksum = 0xffff;
784 char *subname = "";
Erik Hansen3f270f42011-03-24 10:15:37 +0100785 __u32 cursect;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200786 int idx, isdir = 0;
787 int files = 0, dirs = 0;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000788 long ret = -1;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200789 int firsttime;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000790 __u32 root_cluster = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +0100791 int rootdir_size = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200792 int j;
wdenk71f95112003-06-15 22:40:42 +0000793
Wolfgang Denk7385c282010-07-19 11:37:00 +0200794 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
795 debug("Error: reading boot sector\n");
796 return -1;
797 }
798
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000799 if (mydata->fatsize == 32) {
800 root_cluster = bs.root_cluster;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200801 mydata->fatlength = bs.fat32_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000802 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200803 mydata->fatlength = bs.fat_length;
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000804 }
wdenk71f95112003-06-15 22:40:42 +0000805
Wolfgang Denk7385c282010-07-19 11:37:00 +0200806 mydata->fat_sect = bs.reserved;
wdenk71f95112003-06-15 22:40:42 +0000807
Wolfgang Denk7385c282010-07-19 11:37:00 +0200808 cursect = mydata->rootdir_sect
809 = mydata->fat_sect + mydata->fatlength * bs.fats;
wdenk71f95112003-06-15 22:40:42 +0000810
Sergei Shtylyovac497772011-08-08 09:38:33 +0000811 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200812 mydata->clust_size = bs.cluster_size;
wdenk71f95112003-06-15 22:40:42 +0000813
Wolfgang Denk7385c282010-07-19 11:37:00 +0200814 if (mydata->fatsize == 32) {
815 mydata->data_begin = mydata->rootdir_sect -
816 (mydata->clust_size * 2);
817 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200818 rootdir_size = ((bs.dir_entries[1] * (int)256 +
819 bs.dir_entries[0]) *
820 sizeof(dir_entry)) /
Sergei Shtylyovac497772011-08-08 09:38:33 +0000821 mydata->sect_size;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200822 mydata->data_begin = mydata->rootdir_sect +
823 rootdir_size -
824 (mydata->clust_size * 2);
wdenk71f95112003-06-15 22:40:42 +0000825 }
wdenk71f95112003-06-15 22:40:42 +0000826
Wolfgang Denk7385c282010-07-19 11:37:00 +0200827 mydata->fatbufnum = -1;
Sergei Shtylyovac497772011-08-08 09:38:33 +0000828 mydata->fatbuf = malloc(FATBUFSIZE);
829 if (mydata->fatbuf == NULL) {
830 debug("Error: allocating memory\n");
831 return -1;
832 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200833
wdenk71f95112003-06-15 22:40:42 +0000834#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk7385c282010-07-19 11:37:00 +0200835 debug("VFAT Support enabled\n");
wdenk71f95112003-06-15 22:40:42 +0000836#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +0200837 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
838 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
839 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
840 "Data begins at: %d\n",
841 root_cluster,
842 mydata->rootdir_sect,
Sergei Shtylyovac497772011-08-08 09:38:33 +0000843 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
844 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
845 mydata->clust_size);
Wolfgang Denk7385c282010-07-19 11:37:00 +0200846
847 /* "cwd" is always the root... */
848 while (ISDIRDELIM(*filename))
849 filename++;
850
851 /* Make a copy of the filename and convert it to lowercase */
852 strcpy(fnamecopy, filename);
853 downcase(fnamecopy);
854
855 if (*fnamecopy == '\0') {
856 if (!dols)
Sergei Shtylyovac497772011-08-08 09:38:33 +0000857 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200858
859 dols = LS_ROOT;
860 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
861 isdir = 1;
862 fnamecopy[idx] = '\0';
863 subname = fnamecopy + idx + 1;
864
865 /* Handle multiple delimiters */
866 while (ISDIRDELIM(*subname))
867 subname++;
868 } else if (dols) {
869 isdir = 1;
870 }
871
872 j = 0;
873 while (1) {
874 int i;
875
876 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%d\n",
877 cursect, mydata->clust_size, DIRENTSPERBLOCK);
878
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300879 if (disk_read(cursect,
880 (mydata->fatsize == 32) ?
881 (mydata->clust_size) :
Sergei Shtylyov025421e2011-08-19 09:37:46 +0000882 PREFETCH_BLOCKS,
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300883 do_fat_read_block) < 0) {
Wolfgang Denk7385c282010-07-19 11:37:00 +0200884 debug("Error: reading rootdir block\n");
Sergei Shtylyovac497772011-08-08 09:38:33 +0000885 goto exit;
wdenk71f95112003-06-15 22:40:42 +0000886 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200887
888 dentptr = (dir_entry *) do_fat_read_block;
889
890 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300891 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk7385c282010-07-19 11:37:00 +0200892
893 l_name[0] = '\0';
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300894 if (dentptr->name[0] == DELETED_FLAG) {
895 dentptr++;
896 continue;
897 }
Wolfgang Denk7385c282010-07-19 11:37:00 +0200898 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk71f95112003-06-15 22:40:42 +0000899#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk7385c282010-07-19 11:37:00 +0200900 if ((dentptr->attr & ATTR_VFAT) &&
901 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
902 prevcksum =
903 ((dir_slot *)dentptr)->alias_checksum;
wdenk71f95112003-06-15 22:40:42 +0000904
Mikhail Zolotaryov38315302010-09-08 17:06:03 +0300905 get_vfatname(mydata,
Sergei Shtylyov40e21912011-08-19 09:32:34 +0000906 root_cluster,
Wolfgang Denk7385c282010-07-19 11:37:00 +0200907 do_fat_read_block,
908 dentptr, l_name);
909
910 if (dols == LS_ROOT) {
911 char dirc;
912 int doit = 0;
913 int isdir =
914 (dentptr->attr & ATTR_DIR);
915
916 if (isdir) {
917 dirs++;
918 dirc = '/';
919 doit = 1;
920 } else {
921 dirc = ' ';
922 if (l_name[0] != 0) {
923 files++;
924 doit = 1;
925 }
926 }
927 if (doit) {
928 if (dirc == ' ') {
929 printf(" %8ld %s%c\n",
930 (long)FAT2CPU32(dentptr->size),
931 l_name,
932 dirc);
933 } else {
934 printf(" %s%c\n",
935 l_name,
936 dirc);
937 }
938 }
939 dentptr++;
940 continue;
941 }
942 debug("Rootvfatname: |%s|\n",
943 l_name);
944 } else
945#endif
946 {
947 /* Volume label or VFAT entry */
948 dentptr++;
949 continue;
950 }
951 } else if (dentptr->name[0] == 0) {
952 debug("RootDentname == NULL - %d\n", i);
953 if (dols == LS_ROOT) {
954 printf("\n%d file(s), %d dir(s)\n\n",
955 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +0000956 ret = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200957 }
Sergei Shtylyovac497772011-08-08 09:38:33 +0000958 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +0200959 }
960#ifdef CONFIG_SUPPORT_VFAT
961 else if (dols == LS_ROOT &&
962 mkcksum(dentptr->name) == prevcksum) {
963 dentptr++;
964 continue;
965 }
966#endif
967 get_name(dentptr, s_name);
968
969 if (dols == LS_ROOT) {
970 int isdir = (dentptr->attr & ATTR_DIR);
971 char dirc;
972 int doit = 0;
973
974 if (isdir) {
975 dirc = '/';
976 if (s_name[0] != 0) {
977 dirs++;
978 doit = 1;
979 }
980 } else {
981 dirc = ' ';
982 if (s_name[0] != 0) {
983 files++;
984 doit = 1;
985 }
986 }
987 if (doit) {
988 if (dirc == ' ') {
989 printf(" %8ld %s%c\n",
990 (long)FAT2CPU32(dentptr->size),
991 s_name, dirc);
992 } else {
993 printf(" %s%c\n",
994 s_name, dirc);
995 }
996 }
997 dentptr++;
998 continue;
999 }
1000
1001 if (strcmp(fnamecopy, s_name)
1002 && strcmp(fnamecopy, l_name)) {
1003 debug("RootMismatch: |%s|%s|\n", s_name,
1004 l_name);
1005 dentptr++;
1006 continue;
1007 }
1008
1009 if (isdir && !(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001010 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001011
1012 debug("RootName: %s", s_name);
1013 debug(", start: 0x%x", START(dentptr));
1014 debug(", size: 0x%x %s\n",
1015 FAT2CPU32(dentptr->size),
1016 isdir ? "(DIR)" : "");
1017
1018 goto rootdir_done; /* We got a match */
1019 }
1020 debug("END LOOP: j=%d clust_size=%d\n", j,
1021 mydata->clust_size);
1022
1023 /*
1024 * On FAT32 we must fetch the FAT entries for the next
1025 * root directory clusters when a cluster has been
1026 * completely processed.
1027 */
Erik Hansen3f270f42011-03-24 10:15:37 +01001028 ++j;
1029 int fat32_end = 0;
1030 if ((mydata->fatsize == 32) && (j == mydata->clust_size)) {
1031 int nxtsect = 0;
1032 int nxt_clust = 0;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001033
1034 nxt_clust = get_fatent(mydata, root_cluster);
Erik Hansen3f270f42011-03-24 10:15:37 +01001035 fat32_end = CHECK_CLUST(nxt_clust, 32);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001036
1037 nxtsect = mydata->data_begin +
1038 (nxt_clust * mydata->clust_size);
1039
Wolfgang Denk7385c282010-07-19 11:37:00 +02001040 root_cluster = nxt_clust;
1041
1042 cursect = nxtsect;
1043 j = 0;
wdenk71f95112003-06-15 22:40:42 +00001044 } else {
Wolfgang Denk7385c282010-07-19 11:37:00 +02001045 cursect++;
wdenk71f95112003-06-15 22:40:42 +00001046 }
Erik Hansen3f270f42011-03-24 10:15:37 +01001047
1048 /* If end of rootdir reached */
1049 if ((mydata->fatsize == 32 && fat32_end) ||
1050 (mydata->fatsize != 32 && j == rootdir_size)) {
1051 if (dols == LS_ROOT) {
1052 printf("\n%d file(s), %d dir(s)\n\n",
1053 files, dirs);
Sergei Shtylyovac497772011-08-08 09:38:33 +00001054 ret = 0;
Erik Hansen3f270f42011-03-24 10:15:37 +01001055 }
Sergei Shtylyovac497772011-08-08 09:38:33 +00001056 goto exit;
Erik Hansen3f270f42011-03-24 10:15:37 +01001057 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001058 }
1059rootdir_done:
1060
1061 firsttime = 1;
1062
1063 while (isdir) {
1064 int startsect = mydata->data_begin
1065 + START(dentptr) * mydata->clust_size;
1066 dir_entry dent;
1067 char *nextname = NULL;
1068
1069 dent = *dentptr;
1070 dentptr = &dent;
1071
1072 idx = dirdelim(subname);
1073
1074 if (idx >= 0) {
1075 subname[idx] = '\0';
1076 nextname = subname + idx + 1;
1077 /* Handle multiple delimiters */
1078 while (ISDIRDELIM(*nextname))
1079 nextname++;
1080 if (dols && *nextname == '\0')
1081 firsttime = 0;
1082 } else {
1083 if (dols && firsttime) {
1084 firsttime = 0;
1085 } else {
1086 isdir = 0;
1087 }
wdenk71f95112003-06-15 22:40:42 +00001088 }
wdenk71f95112003-06-15 22:40:42 +00001089
Wolfgang Denk7385c282010-07-19 11:37:00 +02001090 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1091 isdir ? 0 : dols) == NULL) {
1092 if (dols && !isdir)
Sergei Shtylyovac497772011-08-08 09:38:33 +00001093 ret = 0;
1094 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001095 }
wdenk71f95112003-06-15 22:40:42 +00001096
Wolfgang Denk7385c282010-07-19 11:37:00 +02001097 if (idx >= 0) {
1098 if (!(dentptr->attr & ATTR_DIR))
Sergei Shtylyovac497772011-08-08 09:38:33 +00001099 goto exit;
Wolfgang Denk7385c282010-07-19 11:37:00 +02001100 subname = nextname;
1101 }
wdenk71f95112003-06-15 22:40:42 +00001102 }
1103
Wolfgang Denk7385c282010-07-19 11:37:00 +02001104 ret = get_contents(mydata, dentptr, buffer, maxsize);
1105 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
wdenk71f95112003-06-15 22:40:42 +00001106
Sergei Shtylyovac497772011-08-08 09:38:33 +00001107exit:
1108 free(mydata->fatbuf);
Wolfgang Denk7385c282010-07-19 11:37:00 +02001109 return ret;
wdenk71f95112003-06-15 22:40:42 +00001110}
1111
Wolfgang Denk7385c282010-07-19 11:37:00 +02001112int file_fat_detectfs (void)
wdenk71f95112003-06-15 22:40:42 +00001113{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001114 boot_sector bs;
1115 volume_info volinfo;
1116 int fatsize;
1117 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +00001118
Wolfgang Denk7385c282010-07-19 11:37:00 +02001119 if (cur_dev == NULL) {
wdenk7205e402003-09-10 22:30:53 +00001120 printf("No current device\n");
1121 return 1;
1122 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001123
Jon Loeligerdd60d122007-07-09 17:56:50 -05001124#if defined(CONFIG_CMD_IDE) || \
unsik Kim75eb82e2009-02-25 11:31:24 +09001125 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -05001126 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -05001127 defined(CONFIG_CMD_SCSI) || \
1128 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -06001129 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +00001130 printf("Interface: ");
Wolfgang Denk7385c282010-07-19 11:37:00 +02001131 switch (cur_dev->if_type) {
1132 case IF_TYPE_IDE:
1133 printf("IDE");
1134 break;
1135 case IF_TYPE_SATA:
1136 printf("SATA");
1137 break;
1138 case IF_TYPE_SCSI:
1139 printf("SCSI");
1140 break;
1141 case IF_TYPE_ATAPI:
1142 printf("ATAPI");
1143 break;
1144 case IF_TYPE_USB:
1145 printf("USB");
1146 break;
1147 case IF_TYPE_DOC:
1148 printf("DOC");
1149 break;
1150 case IF_TYPE_MMC:
1151 printf("MMC");
1152 break;
1153 default:
1154 printf("Unknown");
wdenk7205e402003-09-10 22:30:53 +00001155 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001156
1157 printf("\n Device %d: ", cur_dev->dev);
wdenk7205e402003-09-10 22:30:53 +00001158 dev_print(cur_dev);
1159#endif
Wolfgang Denk7385c282010-07-19 11:37:00 +02001160
1161 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk7205e402003-09-10 22:30:53 +00001162 printf("\nNo valid FAT fs found\n");
1163 return 1;
1164 }
Wolfgang Denk7385c282010-07-19 11:37:00 +02001165
1166 memcpy(vol_label, volinfo.volume_label, 11);
wdenk7205e402003-09-10 22:30:53 +00001167 vol_label[11] = '\0';
Wolfgang Denk7385c282010-07-19 11:37:00 +02001168 volinfo.fs_type[5] = '\0';
1169
1170 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part,
1171 volinfo.fs_type, vol_label);
1172
wdenk7205e402003-09-10 22:30:53 +00001173 return 0;
wdenk71f95112003-06-15 22:40:42 +00001174}
1175
Wolfgang Denk7385c282010-07-19 11:37:00 +02001176int file_fat_ls (const char *dir)
wdenk71f95112003-06-15 22:40:42 +00001177{
1178 return do_fat_read(dir, NULL, 0, LS_YES);
1179}
1180
Wolfgang Denk7385c282010-07-19 11:37:00 +02001181long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
wdenk71f95112003-06-15 22:40:42 +00001182{
Wolfgang Denk7385c282010-07-19 11:37:00 +02001183 printf("reading %s\n", filename);
wdenk71f95112003-06-15 22:40:42 +00001184 return do_fat_read(filename, buffer, maxsize, LS_NO);
1185}