blob: a75e4f258aa1ec768f28d3fe3f8ac2a1bcc9a87d [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>
30#include <fat.h>
31#include <asm/byteorder.h>
wdenk2c9b05d2003-09-10 22:30:53 +000032#include <part.h>
wdenk7a428cc2003-06-15 22:40:42 +000033
wdenk7a428cc2003-06-15 22:40:42 +000034/*
35 * Convert a string to lowercase.
36 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020037static void downcase (char *str)
wdenk7a428cc2003-06-15 22:40:42 +000038{
39 while (*str != '\0') {
40 TOLOWER(*str);
41 str++;
42 }
43}
44
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020045static block_dev_desc_t *cur_dev = NULL;
46
wdenk2c9b05d2003-09-10 22:30:53 +000047static unsigned long part_offset = 0;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020048
wdenk2c9b05d2003-09-10 22:30:53 +000049static int cur_part = 1;
50
51#define DOS_PART_TBL_OFFSET 0x1be
52#define DOS_PART_MAGIC_OFFSET 0x1fe
53#define DOS_FS_TYPE_OFFSET 0x36
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020054#define DOS_FS32_TYPE_OFFSET 0x52
wdenk7a428cc2003-06-15 22:40:42 +000055
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020056static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
wdenk7a428cc2003-06-15 22:40:42 +000057{
wdenk2c9b05d2003-09-10 22:30:53 +000058 if (cur_dev == NULL)
59 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020060
61 startblock += part_offset;
62
wdenk2c9b05d2003-09-10 22:30:53 +000063 if (cur_dev->block_read) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020064 return cur_dev->block_read(cur_dev->dev, startblock, getsize,
65 (unsigned long *) bufptr);
wdenk7a428cc2003-06-15 22:40:42 +000066 }
67 return -1;
68}
69
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020070int fat_register_device (block_dev_desc_t * dev_desc, int part_no)
wdenk7a428cc2003-06-15 22:40:42 +000071{
wdenk2c9b05d2003-09-10 22:30:53 +000072 unsigned char buffer[SECTOR_SIZE];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020073
Heiko Schocher633e03a2007-06-22 19:11:54 +020074 disk_partition_t info;
wdenk2c9b05d2003-09-10 22:30:53 +000075
76 if (!dev_desc->block_read)
77 return -1;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020078
Heiko Schocher633e03a2007-06-22 19:11:54 +020079 cur_dev = dev_desc;
wdenk2c9b05d2003-09-10 22:30:53 +000080 /* check if we have a MBR (on floppies we have only a PBR) */
Wolfgang Denk927b8ce2010-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);
wdenk2c9b05d2003-09-10 22:30:53 +000084 return -1;
85 }
86 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020087 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
wdenk2c9b05d2003-09-10 22:30:53 +000088 /* no signature found */
89 return -1;
90 }
Jon Loeliger80eb47c2007-07-09 17:56:50 -050091#if (defined(CONFIG_CMD_IDE) || \
unsik Kimf0b1bdd2009-02-25 11:31:24 +090092 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang853208e2008-12-09 23:20:18 -050093 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -050094 defined(CONFIG_CMD_SCSI) || \
95 defined(CONFIG_CMD_USB) || \
Andy Fleming55b86682008-01-09 13:51:32 -060096 defined(CONFIG_MMC) || \
97 defined(CONFIG_SYSTEMACE) )
Wolfgang Denk927b8ce2010-07-19 11:37:00 +020098 /* First we assume there is a MBR */
99 if (!get_partition_info(dev_desc, part_no, &info)) {
Andy Fleming55b86682008-01-09 13:51:32 -0600100 part_offset = info.start;
101 cur_part = part_no;
Wolfgang Denk927b8ce2010-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 Fleming55b86682008-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 Denk927b8ce2010-07-19 11:37:00 +0200108 printf("** Partition %d not valid on device %d **\n",
109 part_no, dev_desc->dev);
Andy Fleming55b86682008-01-09 13:51:32 -0600110 return -1;
111 }
112
wdenk2c9b05d2003-09-10 22:30:53 +0000113#else
Wolfgang Denk7b2290c2010-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 Fleming55b86682008-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;
Wolfgang Denke78f2582007-08-07 16:02:13 +0200128 }
Andy Fleming55b86682008-01-09 13:51:32 -0600129#endif
wdenk7a428cc2003-06-15 22:40:42 +0000130 return 0;
131}
132
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200137static int dirdelim (char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000138{
139 char *start = str;
140
141 while (*str != '\0') {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200142 if (ISDIRDELIM(*str))
143 return str - start;
wdenk7a428cc2003-06-15 22:40:42 +0000144 str++;
145 }
146 return -1;
147}
148
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200156 memcpy(s_name, dirent->name, 8);
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200164 memcpy(ptr, dirent->ext, 3);
wdenk7a428cc2003-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 Bohmer7c8f2ce2008-11-27 22:30:27 +0100173 *s_name = DELETED_FLAG;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200174 downcase(s_name);
wdenk7a428cc2003-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 Denk927b8ce2010-07-19 11:37:00 +0200181static __u32 get_fatent (fsdata *mydata, __u32 entry)
wdenk7a428cc2003-06-15 22:40:42 +0000182{
183 __u32 bufnum;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200184 __u32 off16, offset;
wdenk7a428cc2003-06-15 22:40:42 +0000185 __u32 ret = 0x00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200186 __u16 val1, val2;
wdenk7a428cc2003-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 Denk927b8ce2010-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 Denka48d0a32010-07-19 11:36:58 +0200209
wdenk7a428cc2003-06-15 22:40:42 +0000210 /* Read a new block of FAT entries into the cache. */
211 if (bufnum != mydata->fatbufnum) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200212 int getsize = FATBUFSIZE / FS_BLOCK_SIZE;
wdenk7a428cc2003-06-15 22:40:42 +0000213 __u8 *bufptr = mydata->fatbuf;
214 __u32 fatlength = mydata->fatlength;
215 __u32 startblock = bufnum * FATBUFBLOCKS;
216
217 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
218 startblock += mydata->fat_sect; /* Offset from start of disk */
219
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200220 if (getsize > fatlength)
221 getsize = fatlength;
wdenk7a428cc2003-06-15 22:40:42 +0000222 if (disk_read(startblock, getsize, bufptr) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200223 debug("Error reading FAT blocks\n");
wdenk7a428cc2003-06-15 22:40:42 +0000224 return ret;
225 }
226 mydata->fatbufnum = bufnum;
227 }
228
229 /* Get the actual entry from the table */
230 switch (mydata->fatsize) {
231 case 32:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200232 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000233 break;
234 case 16:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200235 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
wdenk7a428cc2003-06-15 22:40:42 +0000236 break;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200237 case 12:
238 off16 = (offset * 3) / 4;
wdenk7a428cc2003-06-15 22:40:42 +0000239
240 switch (offset & 0x3) {
241 case 0:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200242 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000243 ret &= 0xfff;
244 break;
245 case 1:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200246 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000247 val1 &= 0xf000;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200248 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000249 val2 &= 0x00ff;
250 ret = (val2 << 4) | (val1 >> 12);
251 break;
252 case 2:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200253 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000254 val1 &= 0xff00;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200255 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
wdenk7a428cc2003-06-15 22:40:42 +0000256 val2 &= 0x000f;
257 ret = (val2 << 8) | (val1 >> 8);
258 break;
259 case 3:
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200260 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
wdenk7a428cc2003-06-15 22:40:42 +0000261 ret = (ret & 0xfff0) >> 4;
262 break;
263 default:
264 break;
265 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200266 break;
wdenk7a428cc2003-06-15 22:40:42 +0000267 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200268 debug("FAT%d: ret: %08x, offset: %04x\n",
269 mydata->fatsize, ret, offset);
wdenk7a428cc2003-06-15 22:40:42 +0000270
271 return ret;
272}
273
wdenk7a428cc2003-06-15 22:40:42 +0000274/*
275 * Read at most 'size' bytes from the specified cluster into 'buffer'.
276 * Return 0 on success, -1 otherwise.
277 */
278static int
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200279get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
280 unsigned long size)
wdenk7a428cc2003-06-15 22:40:42 +0000281{
282 int idx = 0;
283 __u32 startsect;
284
285 if (clustnum > 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200286 startsect = mydata->data_begin +
287 clustnum * mydata->clust_size;
wdenk7a428cc2003-06-15 22:40:42 +0000288 } else {
289 startsect = mydata->rootdir_sect;
290 }
291
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200292 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
293
294 if (disk_read(startsect, size / FS_BLOCK_SIZE, buffer) < 0) {
295 debug("Error reading data\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000296 return -1;
297 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200298 if (size % FS_BLOCK_SIZE) {
wdenk2c9b05d2003-09-10 22:30:53 +0000299 __u8 tmpbuf[FS_BLOCK_SIZE];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200300
301 idx = size / FS_BLOCK_SIZE;
wdenk2c9b05d2003-09-10 22:30:53 +0000302 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200303 debug("Error reading data\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000304 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000305 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200306 buffer += idx * FS_BLOCK_SIZE;
wdenk2c9b05d2003-09-10 22:30:53 +0000307
308 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
309 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000310 }
311
312 return 0;
313}
314
wdenk7a428cc2003-06-15 22:40:42 +0000315/*
316 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
317 * into 'buffer'.
318 * Return the number of bytes read or -1 on fatal errors.
319 */
320static long
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200321get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
322 unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +0000323{
324 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
325 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
326 __u32 curclust = START(dentptr);
wdenk2c9b05d2003-09-10 22:30:53 +0000327 __u32 endclust, newclust;
328 unsigned long actsize;
wdenk7a428cc2003-06-15 22:40:42 +0000329
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200330 debug("Filesize: %ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000331
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200332 if (maxsize > 0 && filesize > maxsize)
333 filesize = maxsize;
wdenk7a428cc2003-06-15 22:40:42 +0000334
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200335 debug("%ld bytes\n", filesize);
wdenk7a428cc2003-06-15 22:40:42 +0000336
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200337 actsize = bytesperclust;
338 endclust = curclust;
339
wdenk7a428cc2003-06-15 22:40:42 +0000340 do {
wdenk2c9b05d2003-09-10 22:30:53 +0000341 /* search for consecutive clusters */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200342 while (actsize < filesize) {
wdenk2c9b05d2003-09-10 22:30:53 +0000343 newclust = get_fatent(mydata, endclust);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200344 if ((newclust - 1) != endclust)
wdenk2c9b05d2003-09-10 22:30:53 +0000345 goto getit;
michael2c4d2982008-03-02 23:33:46 +0100346 if (CHECK_CLUST(newclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200347 debug("curclust: 0x%x\n", newclust);
348 debug("Invalid FAT entry\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000349 return gotsize;
350 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200351 endclust = newclust;
352 actsize += bytesperclust;
wdenk2c9b05d2003-09-10 22:30:53 +0000353 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200354
wdenk2c9b05d2003-09-10 22:30:53 +0000355 /* actsize >= file size */
356 actsize -= bytesperclust;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200357
wdenk2c9b05d2003-09-10 22:30:53 +0000358 /* get remaining clusters */
359 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200360 printf("Error reading cluster\n");
wdenk7a428cc2003-06-15 22:40:42 +0000361 return -1;
362 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200363
wdenk2c9b05d2003-09-10 22:30:53 +0000364 /* get remaining bytes */
365 gotsize += (int)actsize;
366 filesize -= actsize;
367 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200368 actsize = filesize;
wdenk2c9b05d2003-09-10 22:30:53 +0000369 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200370 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000371 return -1;
372 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200373 gotsize += actsize;
wdenk2c9b05d2003-09-10 22:30:53 +0000374 return gotsize;
375getit:
376 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200377 printf("Error reading cluster\n");
wdenk2c9b05d2003-09-10 22:30:53 +0000378 return -1;
379 }
380 gotsize += (int)actsize;
381 filesize -= actsize;
382 buffer += actsize;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200383
wdenk2c9b05d2003-09-10 22:30:53 +0000384 curclust = get_fatent(mydata, endclust);
michael2c4d2982008-03-02 23:33:46 +0100385 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200386 debug("curclust: 0x%x\n", curclust);
387 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000388 return gotsize;
389 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200390 actsize = bytesperclust;
391 endclust = curclust;
wdenk7a428cc2003-06-15 22:40:42 +0000392 } while (1);
393}
394
wdenk7a428cc2003-06-15 22:40:42 +0000395#ifdef CONFIG_SUPPORT_VFAT
396/*
397 * Extract the file name information from 'slotptr' into 'l_name',
398 * starting at l_name[*idx].
399 * Return 1 if terminator (zero byte) is found, 0 otherwise.
400 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200401static int slot2str (dir_slot *slotptr, char *l_name, int *idx)
wdenk7a428cc2003-06-15 22:40:42 +0000402{
403 int j;
404
405 for (j = 0; j <= 8; j += 2) {
406 l_name[*idx] = slotptr->name0_4[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200407 if (l_name[*idx] == 0x00)
408 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000409 (*idx)++;
410 }
411 for (j = 0; j <= 10; j += 2) {
412 l_name[*idx] = slotptr->name5_10[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200413 if (l_name[*idx] == 0x00)
414 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000415 (*idx)++;
416 }
417 for (j = 0; j <= 2; j += 2) {
418 l_name[*idx] = slotptr->name11_12[j];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200419 if (l_name[*idx] == 0x00)
420 return 1;
wdenk7a428cc2003-06-15 22:40:42 +0000421 (*idx)++;
422 }
423
424 return 0;
425}
426
wdenk7a428cc2003-06-15 22:40:42 +0000427/*
428 * Extract the full long filename starting at 'retdent' (which is really
429 * a slot) into 'l_name'. If successful also copy the real directory entry
430 * into 'retdent'
431 * Return 0 on success, -1 otherwise.
432 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200433__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
Bryan Wu95f99a42009-01-02 20:47:45 -0500434__u8 get_vfatname_block[MAX_CLUSTSIZE];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200435
wdenk7a428cc2003-06-15 22:40:42 +0000436static int
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200437get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
438 dir_entry *retdent, char *l_name)
wdenk7a428cc2003-06-15 22:40:42 +0000439{
440 dir_entry *realdent;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200441 dir_slot *slotptr = (dir_slot *)retdent;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300442 __u8 *buflimit = cluster + ((curclust == 0) ?
443 LINEAR_PREFETCH_SIZE :
444 (mydata->clust_size * SECTOR_SIZE)
445 );
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200446 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk7a428cc2003-06-15 22:40:42 +0000447 int idx = 0;
448
Mikhail Zolotaryov45d513e2010-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 Denk927b8ce2010-07-19 11:37:00 +0200455 if (counter == 0)
456 break;
wdenk2ebee312004-02-23 19:30:57 +0000457 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
458 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000459 slotptr++;
460 counter--;
461 }
462
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300463 if ((__u8 *)slotptr >= buflimit) {
wdenk7a428cc2003-06-15 22:40:42 +0000464 dir_slot *slotptr2;
465
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300466 if (curclust == 0)
467 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000468 curclust = get_fatent(mydata, curclust);
michael2c4d2982008-03-02 23:33:46 +0100469 if (CHECK_CLUST(curclust, mydata->fatsize)) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200470 debug("curclust: 0x%x\n", curclust);
471 printf("Invalid FAT entry\n");
wdenk7a428cc2003-06-15 22:40:42 +0000472 return -1;
473 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200474
wdenkb6c60cb32003-10-29 23:18:55 +0000475 if (get_cluster(mydata, curclust, get_vfatname_block,
wdenk7a428cc2003-06-15 22:40:42 +0000476 mydata->clust_size * SECTOR_SIZE) != 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200477 debug("Error: reading directory block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000478 return -1;
479 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200480
481 slotptr2 = (dir_slot *)get_vfatname_block;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300482 while (counter > 0) {
483 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
484 & 0xff) != counter)
485 return -1;
wdenk7a428cc2003-06-15 22:40:42 +0000486 slotptr2++;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300487 counter--;
488 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200489
wdenk7a428cc2003-06-15 22:40:42 +0000490 /* Save the real directory entry */
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300491 realdent = (dir_entry *)slotptr2;
492 while ((__u8 *)slotptr2 > get_vfatname_block) {
wdenk7a428cc2003-06-15 22:40:42 +0000493 slotptr2--;
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300494 slot2str(slotptr2, l_name, &idx);
wdenk7a428cc2003-06-15 22:40:42 +0000495 }
496 } else {
497 /* Save the real directory entry */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200498 realdent = (dir_entry *)slotptr;
wdenk7a428cc2003-06-15 22:40:42 +0000499 }
500
501 do {
502 slotptr--;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200503 if (slot2str(slotptr, l_name, &idx))
504 break;
wdenk2ebee312004-02-23 19:30:57 +0000505 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk7a428cc2003-06-15 22:40:42 +0000506
507 l_name[idx] = '\0';
Wolfgang Denk927b8ce2010-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;
wdenk7a428cc2003-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
wdenk7a428cc2003-06-15 22:40:42 +0000520/* Calculate short name checksum */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200521static __u8 mkcksum (const char *str)
wdenk7a428cc2003-06-15 22:40:42 +0000522{
523 int i;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200524
wdenk7a428cc2003-06-15 22:40:42 +0000525 __u8 ret = 0;
526
527 for (i = 0; i < 11; i++) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200528 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
wdenk7a428cc2003-06-15 22:40:42 +0000529 }
530
531 return ret;
532}
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200533#endif /* CONFIG_SUPPORT_VFAT */
wdenk7a428cc2003-06-15 22:40:42 +0000534
535/*
536 * Get the directory entry associated with 'filename' from the directory
537 * starting at 'startsect'
538 */
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200539__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
wdenkb6c60cb32003-10-29 23:18:55 +0000540__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200541
542static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
543 char *filename, dir_entry *retdent,
wdenk7a428cc2003-06-15 22:40:42 +0000544 int dols)
545{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200546 __u16 prevcksum = 0xffff;
547 __u32 curclust = START(retdent);
548 int files = 0, dirs = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000549
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200550 debug("get_dentfromdir: %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +0000551
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200552 while (1) {
553 dir_entry *dentptr;
554
555 int i;
556
557 if (get_cluster(mydata, curclust, get_dentfromdir_block,
558 mydata->clust_size * SECTOR_SIZE) != 0) {
559 debug("Error: reading directory block\n");
560 return NULL;
561 }
wdenk7a428cc2003-06-15 22:40:42 +0000562
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200563 dentptr = (dir_entry *)get_dentfromdir_block;
564
565 for (i = 0; i < DIRENTSPERCLUST; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300566 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200567
568 l_name[0] = '\0';
569 if (dentptr->name[0] == DELETED_FLAG) {
570 dentptr++;
571 continue;
572 }
573 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk7a428cc2003-06-15 22:40:42 +0000574#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-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;
wdenk7a428cc2003-06-15 22:40:42 +0000585
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200586 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
616#endif
617 {
618 /* Volume label or VFAT entry */
619 dentptr++;
620 continue;
621 }
wdenk7a428cc2003-06-15 22:40:42 +0000622 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200623 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;
wdenk7a428cc2003-06-15 22:40:42 +0000630 }
wdenk7a428cc2003-06-15 22:40:42 +0000631#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200632 if (dols && mkcksum(dentptr->name) == prevcksum) {
633 dentptr++;
634 continue;
635 }
wdenk7a428cc2003-06-15 22:40:42 +0000636#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200637 get_name(dentptr, s_name);
638 if (dols) {
639 int isdir = (dentptr->attr & ATTR_DIR);
640 char dirc;
641 int doit = 0;
wdenk7a428cc2003-06-15 22:40:42 +0000642
Wolfgang Denk927b8ce2010-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 }
wdenk7a428cc2003-06-15 22:40:42 +0000654
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200655 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 }
wdenk7a428cc2003-06-15 22:40:42 +0000665
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200666 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;
686 }
687
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;
693 }
wdenk7a428cc2003-06-15 22:40:42 +0000694 }
wdenk7a428cc2003-06-15 22:40:42 +0000695
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200696 return NULL;
wdenk7a428cc2003-06-15 22:40:42 +0000697}
698
wdenk7a428cc2003-06-15 22:40:42 +0000699/*
700 * Read boot sector and volume info from a FAT filesystem
701 */
702static int
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200703read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
wdenk7a428cc2003-06-15 22:40:42 +0000704{
705 __u8 block[FS_BLOCK_SIZE];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200706
wdenk7a428cc2003-06-15 22:40:42 +0000707 volume_info *vistart;
708
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200709 if (disk_read (0, 1, block) < 0) {
710 debug("Error: reading block\n");
wdenk7a428cc2003-06-15 22:40:42 +0000711 return -1;
712 }
713
714 memcpy(bs, block, sizeof(boot_sector));
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200715 bs->reserved = FAT2CPU16(bs->reserved);
716 bs->fat_length = FAT2CPU16(bs->fat_length);
717 bs->secs_track = FAT2CPU16(bs->secs_track);
718 bs->heads = FAT2CPU16(bs->heads);
719 bs->total_sect = FAT2CPU32(bs->total_sect);
wdenk7a428cc2003-06-15 22:40:42 +0000720
721 /* FAT32 entries */
722 if (bs->fat_length == 0) {
723 /* Assume FAT32 */
724 bs->fat32_length = FAT2CPU32(bs->fat32_length);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200725 bs->flags = FAT2CPU16(bs->flags);
wdenk7a428cc2003-06-15 22:40:42 +0000726 bs->root_cluster = FAT2CPU32(bs->root_cluster);
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200727 bs->info_sector = FAT2CPU16(bs->info_sector);
728 bs->backup_boot = FAT2CPU16(bs->backup_boot);
729 vistart = (volume_info *)(block + sizeof(boot_sector));
wdenk7a428cc2003-06-15 22:40:42 +0000730 *fatsize = 32;
731 } else {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200732 vistart = (volume_info *)&(bs->fat32_length);
wdenk7a428cc2003-06-15 22:40:42 +0000733 *fatsize = 0;
734 }
735 memcpy(volinfo, vistart, sizeof(volume_info));
736
wdenk7a428cc2003-06-15 22:40:42 +0000737 if (*fatsize == 32) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200738 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
wdenk7a428cc2003-06-15 22:40:42 +0000739 return 0;
wdenk7a428cc2003-06-15 22:40:42 +0000740 } else {
Tom Rix155abaa2009-05-20 07:55:41 -0500741 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000742 *fatsize = 12;
743 return 0;
744 }
Tom Rix155abaa2009-05-20 07:55:41 -0500745 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk7a428cc2003-06-15 22:40:42 +0000746 *fatsize = 16;
747 return 0;
748 }
749 }
750
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200751 debug("Error: broken fs_type sign\n");
wdenk7a428cc2003-06-15 22:40:42 +0000752 return -1;
753}
754
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200755__attribute__ ((__aligned__ (__alignof__ (dir_entry))))
Bryan Wu95f99a42009-01-02 20:47:45 -0500756__u8 do_fat_read_block[MAX_CLUSTSIZE];
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200757
stroese83082242004-12-16 17:57:26 +0000758long
wdenk7a428cc2003-06-15 22:40:42 +0000759do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
760 int dols)
761{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200762 char fnamecopy[2048];
763 boot_sector bs;
764 volume_info volinfo;
765 fsdata datablock;
766 fsdata *mydata = &datablock;
767 dir_entry *dentptr;
768 __u16 prevcksum = 0xffff;
769 char *subname = "";
770 int cursect;
771 int idx, isdir = 0;
772 int files = 0, dirs = 0;
773 long ret = 0;
774 int firsttime;
775 int root_cluster;
776 int j;
wdenk7a428cc2003-06-15 22:40:42 +0000777
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200778 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
779 debug("Error: reading boot sector\n");
780 return -1;
781 }
782
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200783 root_cluster = bs.root_cluster;
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200784
785 if (mydata->fatsize == 32)
786 mydata->fatlength = bs.fat32_length;
787 else
788 mydata->fatlength = bs.fat_length;
789
790 mydata->fat_sect = bs.reserved;
791
792 cursect = mydata->rootdir_sect
793 = mydata->fat_sect + mydata->fatlength * bs.fats;
794
795 mydata->clust_size = bs.cluster_size;
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200796
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200797 if (mydata->fatsize == 32) {
798 mydata->data_begin = mydata->rootdir_sect -
799 (mydata->clust_size * 2);
800 } else {
801 int rootdir_size;
802
803 rootdir_size = ((bs.dir_entries[1] * (int)256 +
804 bs.dir_entries[0]) *
805 sizeof(dir_entry)) /
806 SECTOR_SIZE;
807 mydata->data_begin = mydata->rootdir_sect +
808 rootdir_size -
809 (mydata->clust_size * 2);
810 }
811
812 mydata->fatbufnum = -1;
wdenk7a428cc2003-06-15 22:40:42 +0000813
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200814#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200815 debug("VFAT Support enabled\n");
Wolfgang Denka48d0a32010-07-19 11:36:58 +0200816#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200817 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
818 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
819 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
820 "Data begins at: %d\n",
821 root_cluster,
822 mydata->rootdir_sect,
823 mydata->rootdir_sect * SECTOR_SIZE, mydata->data_begin);
824 debug("Cluster size: %d\n", mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +0000825
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200826 /* "cwd" is always the root... */
827 while (ISDIRDELIM(*filename))
828 filename++;
wdenk7a428cc2003-06-15 22:40:42 +0000829
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200830 /* Make a copy of the filename and convert it to lowercase */
831 strcpy(fnamecopy, filename);
832 downcase(fnamecopy);
wdenk7a428cc2003-06-15 22:40:42 +0000833
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200834 if (*fnamecopy == '\0') {
835 if (!dols)
836 return -1;
837
838 dols = LS_ROOT;
839 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
840 isdir = 1;
841 fnamecopy[idx] = '\0';
842 subname = fnamecopy + idx + 1;
843
844 /* Handle multiple delimiters */
845 while (ISDIRDELIM(*subname))
846 subname++;
847 } else if (dols) {
848 isdir = 1;
wdenk7a428cc2003-06-15 22:40:42 +0000849 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200850
851 j = 0;
852 while (1) {
853 int i;
854
855 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%d\n",
856 cursect, mydata->clust_size, DIRENTSPERBLOCK);
857
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300858 if (disk_read(cursect,
859 (mydata->fatsize == 32) ?
860 (mydata->clust_size) :
Stefano Babic3676f802010-10-20 08:51:45 +0200861 LINEAR_PREFETCH_SIZE / SECTOR_SIZE,
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300862 do_fat_read_block) < 0) {
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200863 debug("Error: reading rootdir block\n");
864 return -1;
865 }
866
867 dentptr = (dir_entry *) do_fat_read_block;
868
869 for (i = 0; i < DIRENTSPERBLOCK; i++) {
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300870 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
wdenk7a428cc2003-06-15 22:40:42 +0000871
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200872 l_name[0] = '\0';
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300873 if (dentptr->name[0] == DELETED_FLAG) {
874 dentptr++;
875 continue;
876 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200877 if ((dentptr->attr & ATTR_VOLUME)) {
wdenk7a428cc2003-06-15 22:40:42 +0000878#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200879 if ((dentptr->attr & ATTR_VFAT) &&
880 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
881 prevcksum =
882 ((dir_slot *)dentptr)->alias_checksum;
wdenk7a428cc2003-06-15 22:40:42 +0000883
Mikhail Zolotaryov45d513e2010-09-08 17:06:03 +0300884 get_vfatname(mydata,
885 (mydata->fatsize == 32) ?
886 root_cluster :
887 0,
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200888 do_fat_read_block,
889 dentptr, l_name);
890
891 if (dols == LS_ROOT) {
892 char dirc;
893 int doit = 0;
894 int isdir =
895 (dentptr->attr & ATTR_DIR);
896
897 if (isdir) {
898 dirs++;
899 dirc = '/';
900 doit = 1;
901 } else {
902 dirc = ' ';
903 if (l_name[0] != 0) {
904 files++;
905 doit = 1;
906 }
907 }
908 if (doit) {
909 if (dirc == ' ') {
910 printf(" %8ld %s%c\n",
911 (long)FAT2CPU32(dentptr->size),
912 l_name,
913 dirc);
914 } else {
915 printf(" %s%c\n",
916 l_name,
917 dirc);
918 }
919 }
920 dentptr++;
921 continue;
922 }
923 debug("Rootvfatname: |%s|\n",
924 l_name);
925 } else
wdenk7a428cc2003-06-15 22:40:42 +0000926#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200927 {
928 /* Volume label or VFAT entry */
929 dentptr++;
930 continue;
931 }
932 } else if (dentptr->name[0] == 0) {
933 debug("RootDentname == NULL - %d\n", i);
934 if (dols == LS_ROOT) {
935 printf("\n%d file(s), %d dir(s)\n\n",
936 files, dirs);
937 return 0;
938 }
939 return -1;
940 }
wdenk7a428cc2003-06-15 22:40:42 +0000941#ifdef CONFIG_SUPPORT_VFAT
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200942 else if (dols == LS_ROOT &&
943 mkcksum(dentptr->name) == prevcksum) {
944 dentptr++;
945 continue;
946 }
wdenk7a428cc2003-06-15 22:40:42 +0000947#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200948 get_name(dentptr, s_name);
wdenk7a428cc2003-06-15 22:40:42 +0000949
Wolfgang Denk927b8ce2010-07-19 11:37:00 +0200950 if (dols == LS_ROOT) {
951 int isdir = (dentptr->attr & ATTR_DIR);
952 char dirc;
953 int doit = 0;
954
955 if (isdir) {
956 dirc = '/';
957 if (s_name[0] != 0) {
958 dirs++;
959 doit = 1;
960 }
961 } else {
962 dirc = ' ';
963 if (s_name[0] != 0) {
964 files++;
965 doit = 1;
966 }
967 }
968 if (doit) {
969 if (dirc == ' ') {
970 printf(" %8ld %s%c\n",
971 (long)FAT2CPU32(dentptr->size),
972 s_name, dirc);
973 } else {
974 printf(" %s%c\n",
975 s_name, dirc);
976 }
977 }
978 dentptr++;
979 continue;
980 }
981
982 if (strcmp(fnamecopy, s_name)
983 && strcmp(fnamecopy, l_name)) {
984 debug("RootMismatch: |%s|%s|\n", s_name,
985 l_name);
986 dentptr++;
987 continue;
988 }
989
990 if (isdir && !(dentptr->attr & ATTR_DIR))
991 return -1;
992
993 debug("RootName: %s", s_name);
994 debug(", start: 0x%x", START(dentptr));
995 debug(", size: 0x%x %s\n",
996 FAT2CPU32(dentptr->size),
997 isdir ? "(DIR)" : "");
998
999 goto rootdir_done; /* We got a match */
wdenk7a428cc2003-06-15 22:40:42 +00001000 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001001 debug("END LOOP: j=%d clust_size=%d\n", j,
1002 mydata->clust_size);
wdenk7a428cc2003-06-15 22:40:42 +00001003
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001004 /*
1005 * On FAT32 we must fetch the FAT entries for the next
1006 * root directory clusters when a cluster has been
1007 * completely processed.
1008 */
1009 if ((mydata->fatsize == 32) && (++j == mydata->clust_size)) {
1010 int nxtsect;
1011 int nxt_clust;
wdenk7a428cc2003-06-15 22:40:42 +00001012
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001013 nxt_clust = get_fatent(mydata, root_cluster);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001014
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001015 nxtsect = mydata->data_begin +
1016 (nxt_clust * mydata->clust_size);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001017
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001018 debug("END LOOP: sect=%d, root_clust=%d, "
1019 "n_sect=%d, n_clust=%d\n",
1020 cursect, root_cluster,
1021 nxtsect, nxt_clust);
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001022
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001023 root_cluster = nxt_clust;
1024
1025 cursect = nxtsect;
1026 j = 0;
1027 } else {
1028 cursect++;
1029 }
Wolfgang Denka48d0a32010-07-19 11:36:58 +02001030 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001031rootdir_done:
wdenk7a428cc2003-06-15 22:40:42 +00001032
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001033 firsttime = 1;
wdenk7a428cc2003-06-15 22:40:42 +00001034
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001035 while (isdir) {
1036 int startsect = mydata->data_begin
1037 + START(dentptr) * mydata->clust_size;
1038 dir_entry dent;
1039 char *nextname = NULL;
wdenk7a428cc2003-06-15 22:40:42 +00001040
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001041 dent = *dentptr;
1042 dentptr = &dent;
wdenk7a428cc2003-06-15 22:40:42 +00001043
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001044 idx = dirdelim(subname);
wdenk7a428cc2003-06-15 22:40:42 +00001045
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001046 if (idx >= 0) {
1047 subname[idx] = '\0';
1048 nextname = subname + idx + 1;
1049 /* Handle multiple delimiters */
1050 while (ISDIRDELIM(*nextname))
1051 nextname++;
1052 if (dols && *nextname == '\0')
1053 firsttime = 0;
1054 } else {
1055 if (dols && firsttime) {
1056 firsttime = 0;
1057 } else {
1058 isdir = 0;
1059 }
1060 }
1061
1062 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1063 isdir ? 0 : dols) == NULL) {
1064 if (dols && !isdir)
1065 return 0;
1066 return -1;
1067 }
1068
1069 if (idx >= 0) {
1070 if (!(dentptr->attr & ATTR_DIR))
1071 return -1;
1072 subname = nextname;
1073 }
wdenk7a428cc2003-06-15 22:40:42 +00001074 }
wdenk7a428cc2003-06-15 22:40:42 +00001075
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001076 ret = get_contents(mydata, dentptr, buffer, maxsize);
1077 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
wdenk7a428cc2003-06-15 22:40:42 +00001078
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001079 return ret;
1080}
wdenk7a428cc2003-06-15 22:40:42 +00001081
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001082int file_fat_detectfs (void)
wdenk7a428cc2003-06-15 22:40:42 +00001083{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001084 boot_sector bs;
1085 volume_info volinfo;
1086 int fatsize;
1087 char vol_label[12];
wdenk7a428cc2003-06-15 22:40:42 +00001088
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001089 if (cur_dev == NULL) {
wdenk2c9b05d2003-09-10 22:30:53 +00001090 printf("No current device\n");
1091 return 1;
1092 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001093
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001094#if defined(CONFIG_CMD_IDE) || \
unsik Kimf0b1bdd2009-02-25 11:31:24 +09001095 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang853208e2008-12-09 23:20:18 -05001096 defined(CONFIG_CMD_SATA) || \
Jon Loeliger80eb47c2007-07-09 17:56:50 -05001097 defined(CONFIG_CMD_SCSI) || \
1098 defined(CONFIG_CMD_USB) || \
Andy Fleming1efe5782008-01-16 13:06:59 -06001099 defined(CONFIG_MMC)
wdenk2c9b05d2003-09-10 22:30:53 +00001100 printf("Interface: ");
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001101 switch (cur_dev->if_type) {
1102 case IF_TYPE_IDE:
1103 printf("IDE");
1104 break;
1105 case IF_TYPE_SATA:
1106 printf("SATA");
1107 break;
1108 case IF_TYPE_SCSI:
1109 printf("SCSI");
1110 break;
1111 case IF_TYPE_ATAPI:
1112 printf("ATAPI");
1113 break;
1114 case IF_TYPE_USB:
1115 printf("USB");
1116 break;
1117 case IF_TYPE_DOC:
1118 printf("DOC");
1119 break;
1120 case IF_TYPE_MMC:
1121 printf("MMC");
1122 break;
1123 default:
1124 printf("Unknown");
wdenk2c9b05d2003-09-10 22:30:53 +00001125 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001126
1127 printf("\n Device %d: ", cur_dev->dev);
wdenk2c9b05d2003-09-10 22:30:53 +00001128 dev_print(cur_dev);
1129#endif
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001130
1131 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
wdenk2c9b05d2003-09-10 22:30:53 +00001132 printf("\nNo valid FAT fs found\n");
1133 return 1;
1134 }
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001135
1136 memcpy(vol_label, volinfo.volume_label, 11);
wdenk2c9b05d2003-09-10 22:30:53 +00001137 vol_label[11] = '\0';
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001138 volinfo.fs_type[5] = '\0';
1139
1140 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part,
1141 volinfo.fs_type, vol_label);
1142
wdenk2c9b05d2003-09-10 22:30:53 +00001143 return 0;
wdenk7a428cc2003-06-15 22:40:42 +00001144}
1145
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001146int file_fat_ls (const char *dir)
wdenk7a428cc2003-06-15 22:40:42 +00001147{
1148 return do_fat_read(dir, NULL, 0, LS_YES);
1149}
1150
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001151long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
wdenk7a428cc2003-06-15 22:40:42 +00001152{
Wolfgang Denk927b8ce2010-07-19 11:37:00 +02001153 printf("reading %s\n", filename);
wdenk7a428cc2003-06-15 22:40:42 +00001154 return do_fat_read(filename, buffer, maxsize, LS_NO);
1155}