blob: 2445f1e784d322ec70ff7412db1c60615ada4cc3 [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>
30#include <fat.h>
31#include <asm/byteorder.h>
wdenk7205e402003-09-10 22:30:53 +000032#include <part.h>
wdenk71f95112003-06-15 22:40:42 +000033
wdenk71f95112003-06-15 22:40:42 +000034/*
35 * Convert a string to lowercase.
36 */
37static void
38downcase(char *str)
39{
40 while (*str != '\0') {
41 TOLOWER(*str);
42 str++;
43 }
44}
45
wdenk7205e402003-09-10 22:30:53 +000046static block_dev_desc_t *cur_dev = NULL;
47static unsigned long part_offset = 0;
48static int cur_part = 1;
49
50#define DOS_PART_TBL_OFFSET 0x1be
51#define DOS_PART_MAGIC_OFFSET 0x1fe
52#define DOS_FS_TYPE_OFFSET 0x36
wdenk71f95112003-06-15 22:40:42 +000053
54int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
55{
wdenk7205e402003-09-10 22:30:53 +000056 startblock += part_offset;
57 if (cur_dev == NULL)
58 return -1;
59 if (cur_dev->block_read) {
Peter Pearse3e3b9562007-05-18 16:47:03 +010060 return cur_dev->block_read (cur_dev->dev
61 , startblock, getsize, (unsigned long *)bufptr);
wdenk71f95112003-06-15 22:40:42 +000062 }
63 return -1;
64}
65
66
67int
wdenk7205e402003-09-10 22:30:53 +000068fat_register_device(block_dev_desc_t *dev_desc, int part_no)
wdenk71f95112003-06-15 22:40:42 +000069{
wdenk7205e402003-09-10 22:30:53 +000070 unsigned char buffer[SECTOR_SIZE];
Heiko Schocher566a4942007-06-22 19:11:54 +020071 disk_partition_t info;
wdenk7205e402003-09-10 22:30:53 +000072
73 if (!dev_desc->block_read)
74 return -1;
Heiko Schocher566a4942007-06-22 19:11:54 +020075 cur_dev = dev_desc;
wdenk7205e402003-09-10 22:30:53 +000076 /* check if we have a MBR (on floppies we have only a PBR) */
77 if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
78 printf ("** Can't read from device %d **\n", dev_desc->dev);
79 return -1;
80 }
81 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
82 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
83 /* no signature found */
84 return -1;
85 }
Jon Loeligerdd60d122007-07-09 17:56:50 -050086#if (defined(CONFIG_CMD_IDE) || \
unsik Kim75eb82e2009-02-25 11:31:24 +090087 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -050088 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -050089 defined(CONFIG_CMD_SCSI) || \
90 defined(CONFIG_CMD_USB) || \
Andy Fleming02df4a22008-01-09 13:51:32 -060091 defined(CONFIG_MMC) || \
92 defined(CONFIG_SYSTEMACE) )
93 /* First we assume, there is a MBR */
94 if (!get_partition_info (dev_desc, part_no, &info)) {
95 part_offset = info.start;
96 cur_part = part_no;
97 } else if (!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3)) {
98 /* ok, we assume we are on a PBR only */
99 cur_part = 1;
100 part_offset = 0;
101 } else {
102 printf ("** Partition %d not valid on device %d **\n",
Wolfgang Denkbf1060e2007-08-07 16:02:13 +0200103 part_no, dev_desc->dev);
Andy Fleming02df4a22008-01-09 13:51:32 -0600104 return -1;
Wolfgang Denkbf1060e2007-08-07 16:02:13 +0200105 }
Andy Fleming02df4a22008-01-09 13:51:32 -0600106
107#else
108 if (!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
109 /* ok, we assume we are on a PBR only */
110 cur_part = 1;
111 part_offset = 0;
112 info.start = part_offset;
113 } else {
114 /* FIXME we need to determine the start block of the
115 * partition where the DOS FS resides. This can be done
116 * by using the get_partition_info routine. For this
117 * purpose the libpart must be included.
118 */
119 part_offset = 32;
120 cur_part = 1;
121 }
122#endif
wdenk71f95112003-06-15 22:40:42 +0000123 return 0;
124}
125
126
127/*
128 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
129 * Return index into string if found, -1 otherwise.
130 */
131static int
132dirdelim(char *str)
133{
134 char *start = str;
135
136 while (*str != '\0') {
137 if (ISDIRDELIM(*str)) return str - start;
138 str++;
139 }
140 return -1;
141}
142
wdenk71f95112003-06-15 22:40:42 +0000143/*
144 * Extract zero terminated short name from a directory entry.
145 */
146static void get_name (dir_entry *dirent, char *s_name)
147{
148 char *ptr;
149
150 memcpy (s_name, dirent->name, 8);
151 s_name[8] = '\0';
152 ptr = s_name;
153 while (*ptr && *ptr != ' ')
154 ptr++;
155 if (dirent->ext[0] && dirent->ext[0] != ' ') {
156 *ptr = '.';
157 ptr++;
158 memcpy (ptr, dirent->ext, 3);
159 ptr[3] = '\0';
160 while (*ptr && *ptr != ' ')
161 ptr++;
162 }
163 *ptr = '\0';
164 if (*s_name == DELETED_FLAG)
165 *s_name = '\0';
166 else if (*s_name == aRING)
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100167 *s_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000168 downcase (s_name);
169}
170
171/*
172 * Get the entry at index 'entry' in a FAT (12/16/32) table.
173 * On failure 0x00 is returned.
174 */
175static __u32
176get_fatent(fsdata *mydata, __u32 entry)
177{
178 __u32 bufnum;
179 __u32 offset;
180 __u32 ret = 0x00;
181
182 switch (mydata->fatsize) {
183 case 32:
184 bufnum = entry / FAT32BUFSIZE;
185 offset = entry - bufnum * FAT32BUFSIZE;
186 break;
187 case 16:
188 bufnum = entry / FAT16BUFSIZE;
189 offset = entry - bufnum * FAT16BUFSIZE;
190 break;
191 case 12:
192 bufnum = entry / FAT12BUFSIZE;
193 offset = entry - bufnum * FAT12BUFSIZE;
194 break;
195
196 default:
197 /* Unsupported FAT size */
198 return ret;
199 }
200
201 /* Read a new block of FAT entries into the cache. */
202 if (bufnum != mydata->fatbufnum) {
203 int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
204 __u8 *bufptr = mydata->fatbuf;
205 __u32 fatlength = mydata->fatlength;
206 __u32 startblock = bufnum * FATBUFBLOCKS;
207
208 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
209 startblock += mydata->fat_sect; /* Offset from start of disk */
210
211 if (getsize > fatlength) getsize = fatlength;
212 if (disk_read(startblock, getsize, bufptr) < 0) {
213 FAT_DPRINT("Error reading FAT blocks\n");
214 return ret;
215 }
216 mydata->fatbufnum = bufnum;
217 }
218
219 /* Get the actual entry from the table */
220 switch (mydata->fatsize) {
221 case 32:
222 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
223 break;
224 case 16:
225 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
226 break;
227 case 12: {
228 __u32 off16 = (offset*3)/4;
229 __u16 val1, val2;
230
231 switch (offset & 0x3) {
232 case 0:
233 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
234 ret &= 0xfff;
235 break;
236 case 1:
237 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
238 val1 &= 0xf000;
239 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
240 val2 &= 0x00ff;
241 ret = (val2 << 4) | (val1 >> 12);
242 break;
243 case 2:
244 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
245 val1 &= 0xff00;
246 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
247 val2 &= 0x000f;
248 ret = (val2 << 8) | (val1 >> 8);
249 break;
250 case 3:
251 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
252 ret = (ret & 0xfff0) >> 4;
253 break;
254 default:
255 break;
256 }
257 }
258 break;
259 }
260 FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
261
262 return ret;
263}
264
265
266/*
267 * Read at most 'size' bytes from the specified cluster into 'buffer'.
268 * Return 0 on success, -1 otherwise.
269 */
270static int
271get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
272{
273 int idx = 0;
274 __u32 startsect;
275
276 if (clustnum > 0) {
277 startsect = mydata->data_begin + clustnum*mydata->clust_size;
278 } else {
279 startsect = mydata->rootdir_sect;
280 }
281
282 FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
wdenk7205e402003-09-10 22:30:53 +0000283 if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
284 FAT_DPRINT("Error reading data\n");
285 return -1;
286 }
287 if(size % FS_BLOCK_SIZE) {
288 __u8 tmpbuf[FS_BLOCK_SIZE];
289 idx= size/FS_BLOCK_SIZE;
290 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
291 FAT_DPRINT("Error reading data\n");
292 return -1;
wdenk71f95112003-06-15 22:40:42 +0000293 }
wdenk7205e402003-09-10 22:30:53 +0000294 buffer += idx*FS_BLOCK_SIZE;
295
296 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
297 return 0;
wdenk71f95112003-06-15 22:40:42 +0000298 }
299
300 return 0;
301}
302
303
304/*
305 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
306 * into 'buffer'.
307 * Return the number of bytes read or -1 on fatal errors.
308 */
309static long
310get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
311 unsigned long maxsize)
312{
313 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
314 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
315 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000316 __u32 endclust, newclust;
317 unsigned long actsize;
wdenk71f95112003-06-15 22:40:42 +0000318
319 FAT_DPRINT("Filesize: %ld bytes\n", filesize);
320
321 if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
322
323 FAT_DPRINT("Reading: %ld bytes\n", filesize);
324
wdenk7205e402003-09-10 22:30:53 +0000325 actsize=bytesperclust;
326 endclust=curclust;
wdenk71f95112003-06-15 22:40:42 +0000327 do {
wdenk7205e402003-09-10 22:30:53 +0000328 /* search for consecutive clusters */
329 while(actsize < filesize) {
330 newclust = get_fatent(mydata, endclust);
331 if((newclust -1)!=endclust)
332 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100333 if (CHECK_CLUST(newclust, mydata->fatsize)) {
wdenk7205e402003-09-10 22:30:53 +0000334 FAT_DPRINT("curclust: 0x%x\n", newclust);
335 FAT_DPRINT("Invalid FAT entry\n");
336 return gotsize;
337 }
338 endclust=newclust;
339 actsize+= bytesperclust;
340 }
341 /* actsize >= file size */
342 actsize -= bytesperclust;
343 /* get remaining clusters */
344 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
wdenk71f95112003-06-15 22:40:42 +0000345 FAT_ERROR("Error reading cluster\n");
346 return -1;
347 }
wdenk7205e402003-09-10 22:30:53 +0000348 /* get remaining bytes */
349 gotsize += (int)actsize;
350 filesize -= actsize;
351 buffer += actsize;
352 actsize= filesize;
353 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
354 FAT_ERROR("Error reading cluster\n");
355 return -1;
356 }
357 gotsize+=actsize;
358 return gotsize;
359getit:
360 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
361 FAT_ERROR("Error reading cluster\n");
362 return -1;
363 }
364 gotsize += (int)actsize;
365 filesize -= actsize;
366 buffer += actsize;
367 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100368 if (CHECK_CLUST(curclust, mydata->fatsize)) {
wdenk71f95112003-06-15 22:40:42 +0000369 FAT_DPRINT("curclust: 0x%x\n", curclust);
370 FAT_ERROR("Invalid FAT entry\n");
371 return gotsize;
372 }
wdenk7205e402003-09-10 22:30:53 +0000373 actsize=bytesperclust;
374 endclust=curclust;
wdenk71f95112003-06-15 22:40:42 +0000375 } while (1);
376}
377
378
379#ifdef CONFIG_SUPPORT_VFAT
380/*
381 * Extract the file name information from 'slotptr' into 'l_name',
382 * starting at l_name[*idx].
383 * Return 1 if terminator (zero byte) is found, 0 otherwise.
384 */
385static int
386slot2str(dir_slot *slotptr, char *l_name, int *idx)
387{
388 int j;
389
390 for (j = 0; j <= 8; j += 2) {
391 l_name[*idx] = slotptr->name0_4[j];
392 if (l_name[*idx] == 0x00) return 1;
393 (*idx)++;
394 }
395 for (j = 0; j <= 10; j += 2) {
396 l_name[*idx] = slotptr->name5_10[j];
397 if (l_name[*idx] == 0x00) return 1;
398 (*idx)++;
399 }
400 for (j = 0; j <= 2; j += 2) {
401 l_name[*idx] = slotptr->name11_12[j];
402 if (l_name[*idx] == 0x00) return 1;
403 (*idx)++;
404 }
405
406 return 0;
407}
408
409
410/*
411 * Extract the full long filename starting at 'retdent' (which is really
412 * a slot) into 'l_name'. If successful also copy the real directory entry
413 * into 'retdent'
414 * Return 0 on success, -1 otherwise.
415 */
Bryan Wu7e4b9b42009-01-02 20:47:45 -0500416__attribute__ ((__aligned__(__alignof__(dir_entry))))
417__u8 get_vfatname_block[MAX_CLUSTSIZE];
wdenk71f95112003-06-15 22:40:42 +0000418static int
419get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
420 dir_entry *retdent, char *l_name)
421{
422 dir_entry *realdent;
423 dir_slot *slotptr = (dir_slot*) retdent;
424 __u8 *nextclust = cluster + mydata->clust_size * SECTOR_SIZE;
wdenk2d1a5372004-02-23 19:30:57 +0000425 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000426 int idx = 0;
427
428 while ((__u8*)slotptr < nextclust) {
429 if (counter == 0) break;
wdenk2d1a5372004-02-23 19:30:57 +0000430 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
431 return -1;
wdenk71f95112003-06-15 22:40:42 +0000432 slotptr++;
433 counter--;
434 }
435
436 if ((__u8*)slotptr >= nextclust) {
wdenk71f95112003-06-15 22:40:42 +0000437 dir_slot *slotptr2;
438
439 slotptr--;
440 curclust = get_fatent(mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100441 if (CHECK_CLUST(curclust, mydata->fatsize)) {
wdenk71f95112003-06-15 22:40:42 +0000442 FAT_DPRINT("curclust: 0x%x\n", curclust);
443 FAT_ERROR("Invalid FAT entry\n");
444 return -1;
445 }
wdenk5fa66df2003-10-29 23:18:55 +0000446 if (get_cluster(mydata, curclust, get_vfatname_block,
wdenk71f95112003-06-15 22:40:42 +0000447 mydata->clust_size * SECTOR_SIZE) != 0) {
448 FAT_DPRINT("Error: reading directory block\n");
449 return -1;
450 }
wdenk5fa66df2003-10-29 23:18:55 +0000451 slotptr2 = (dir_slot*) get_vfatname_block;
wdenk71f95112003-06-15 22:40:42 +0000452 while (slotptr2->id > 0x01) {
453 slotptr2++;
454 }
455 /* Save the real directory entry */
456 realdent = (dir_entry*)slotptr2 + 1;
wdenk5fa66df2003-10-29 23:18:55 +0000457 while ((__u8*)slotptr2 >= get_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000458 slot2str(slotptr2, l_name, &idx);
459 slotptr2--;
460 }
461 } else {
462 /* Save the real directory entry */
463 realdent = (dir_entry*)slotptr;
464 }
465
466 do {
467 slotptr--;
468 if (slot2str(slotptr, l_name, &idx)) break;
wdenk2d1a5372004-02-23 19:30:57 +0000469 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000470
471 l_name[idx] = '\0';
472 if (*l_name == DELETED_FLAG) *l_name = '\0';
Remy Bohmer3c2c2f42008-11-27 22:30:27 +0100473 else if (*l_name == aRING) *l_name = DELETED_FLAG;
wdenk71f95112003-06-15 22:40:42 +0000474 downcase(l_name);
475
476 /* Return the real directory entry */
477 memcpy(retdent, realdent, sizeof(dir_entry));
478
479 return 0;
480}
481
482
483/* Calculate short name checksum */
484static __u8
485mkcksum(const char *str)
486{
487 int i;
488 __u8 ret = 0;
489
490 for (i = 0; i < 11; i++) {
491 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
492 }
493
494 return ret;
495}
496#endif
497
498
499/*
500 * Get the directory entry associated with 'filename' from the directory
501 * starting at 'startsect'
502 */
Bryan Wu7e4b9b42009-01-02 20:47:45 -0500503__attribute__ ((__aligned__(__alignof__(dir_entry))))
wdenk5fa66df2003-10-29 23:18:55 +0000504__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
wdenk71f95112003-06-15 22:40:42 +0000505static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
506 char *filename, dir_entry * retdent,
507 int dols)
508{
509 __u16 prevcksum = 0xffff;
wdenk71f95112003-06-15 22:40:42 +0000510 __u32 curclust = START (retdent);
511 int files = 0, dirs = 0;
512
513 FAT_DPRINT ("get_dentfromdir: %s\n", filename);
514 while (1) {
515 dir_entry *dentptr;
516 int i;
517
wdenk5fa66df2003-10-29 23:18:55 +0000518 if (get_cluster (mydata, curclust, get_dentfromdir_block,
wdenk71f95112003-06-15 22:40:42 +0000519 mydata->clust_size * SECTOR_SIZE) != 0) {
520 FAT_DPRINT ("Error: reading directory block\n");
521 return NULL;
522 }
wdenk5fa66df2003-10-29 23:18:55 +0000523 dentptr = (dir_entry *) get_dentfromdir_block;
wdenk71f95112003-06-15 22:40:42 +0000524 for (i = 0; i < DIRENTSPERCLUST; i++) {
525 char s_name[14], l_name[256];
526
527 l_name[0] = '\0';
wdenk855a4962004-03-14 18:23:55 +0000528 if (dentptr->name[0] == DELETED_FLAG) {
529 dentptr++;
530 continue;
531 }
wdenk71f95112003-06-15 22:40:42 +0000532 if ((dentptr->attr & ATTR_VOLUME)) {
533#ifdef CONFIG_SUPPORT_VFAT
534 if ((dentptr->attr & ATTR_VFAT) &&
wdenk2d1a5372004-02-23 19:30:57 +0000535 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
wdenk71f95112003-06-15 22:40:42 +0000536 prevcksum = ((dir_slot *) dentptr)
537 ->alias_checksum;
wdenk5fa66df2003-10-29 23:18:55 +0000538 get_vfatname (mydata, curclust, get_dentfromdir_block,
wdenk71f95112003-06-15 22:40:42 +0000539 dentptr, l_name);
540 if (dols) {
541 int isdir = (dentptr->attr & ATTR_DIR);
542 char dirc;
543 int doit = 0;
544
545 if (isdir) {
546 dirs++;
547 dirc = '/';
548 doit = 1;
549 } else {
550 dirc = ' ';
551 if (l_name[0] != 0) {
552 files++;
553 doit = 1;
554 }
555 }
556 if (doit) {
557 if (dirc == ' ') {
558 printf (" %8ld %s%c\n",
559 (long) FAT2CPU32 (dentptr->size),
560 l_name, dirc);
561 } else {
562 printf (" %s%c\n", l_name, dirc);
563 }
564 }
565 dentptr++;
566 continue;
567 }
568 FAT_DPRINT ("vfatname: |%s|\n", l_name);
569 } else
570#endif
571 {
572 /* Volume label or VFAT entry */
573 dentptr++;
574 continue;
575 }
576 }
577 if (dentptr->name[0] == 0) {
578 if (dols) {
579 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
580 }
581 FAT_DPRINT ("Dentname == NULL - %d\n", i);
582 return NULL;
583 }
584#ifdef CONFIG_SUPPORT_VFAT
585 if (dols && mkcksum (dentptr->name) == prevcksum) {
586 dentptr++;
587 continue;
588 }
589#endif
590 get_name (dentptr, s_name);
591 if (dols) {
592 int isdir = (dentptr->attr & ATTR_DIR);
593 char dirc;
594 int doit = 0;
595
596 if (isdir) {
597 dirs++;
598 dirc = '/';
599 doit = 1;
600 } else {
601 dirc = ' ';
602 if (s_name[0] != 0) {
603 files++;
604 doit = 1;
605 }
606 }
607 if (doit) {
608 if (dirc == ' ') {
609 printf (" %8ld %s%c\n",
610 (long) FAT2CPU32 (dentptr->size), s_name,
611 dirc);
612 } else {
613 printf (" %s%c\n", s_name, dirc);
614 }
615 }
616 dentptr++;
617 continue;
618 }
619 if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
620 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
621 dentptr++;
622 continue;
623 }
624 memcpy (retdent, dentptr, sizeof (dir_entry));
625
626 FAT_DPRINT ("DentName: %s", s_name);
627 FAT_DPRINT (", start: 0x%x", START (dentptr));
628 FAT_DPRINT (", size: 0x%x %s\n",
629 FAT2CPU32 (dentptr->size),
630 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
631
632 return retdent;
633 }
634 curclust = get_fatent (mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100635 if (CHECK_CLUST(curclust, mydata->fatsize)) {
wdenk71f95112003-06-15 22:40:42 +0000636 FAT_DPRINT ("curclust: 0x%x\n", curclust);
637 FAT_ERROR ("Invalid FAT entry\n");
638 return NULL;
639 }
640 }
641
642 return NULL;
643}
644
645
646/*
647 * Read boot sector and volume info from a FAT filesystem
648 */
649static int
650read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
651{
652 __u8 block[FS_BLOCK_SIZE];
653 volume_info *vistart;
654
655 if (disk_read(0, 1, block) < 0) {
656 FAT_DPRINT("Error: reading block\n");
657 return -1;
658 }
659
660 memcpy(bs, block, sizeof(boot_sector));
661 bs->reserved = FAT2CPU16(bs->reserved);
662 bs->fat_length = FAT2CPU16(bs->fat_length);
663 bs->secs_track = FAT2CPU16(bs->secs_track);
664 bs->heads = FAT2CPU16(bs->heads);
665#if 0 /* UNUSED */
666 bs->hidden = FAT2CPU32(bs->hidden);
667#endif
668 bs->total_sect = FAT2CPU32(bs->total_sect);
669
670 /* FAT32 entries */
671 if (bs->fat_length == 0) {
672 /* Assume FAT32 */
673 bs->fat32_length = FAT2CPU32(bs->fat32_length);
674 bs->flags = FAT2CPU16(bs->flags);
675 bs->root_cluster = FAT2CPU32(bs->root_cluster);
676 bs->info_sector = FAT2CPU16(bs->info_sector);
677 bs->backup_boot = FAT2CPU16(bs->backup_boot);
678 vistart = (volume_info*) (block + sizeof(boot_sector));
679 *fatsize = 32;
680 } else {
681 vistart = (volume_info*) &(bs->fat32_length);
682 *fatsize = 0;
683 }
684 memcpy(volinfo, vistart, sizeof(volume_info));
685
wdenk71f95112003-06-15 22:40:42 +0000686 if (*fatsize == 32) {
Tom Rix651351f2009-05-20 07:55:41 -0500687 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000688 return 0;
689 }
690 } else {
Tom Rix651351f2009-05-20 07:55:41 -0500691 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000692 *fatsize = 12;
693 return 0;
694 }
Tom Rix651351f2009-05-20 07:55:41 -0500695 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
wdenk71f95112003-06-15 22:40:42 +0000696 *fatsize = 16;
697 return 0;
698 }
699 }
700
701 FAT_DPRINT("Error: broken fs_type sign\n");
702 return -1;
703}
704
Bryan Wu7e4b9b42009-01-02 20:47:45 -0500705__attribute__ ((__aligned__(__alignof__(dir_entry))))
706__u8 do_fat_read_block[MAX_CLUSTSIZE];
stroese20cc00d2004-12-16 17:57:26 +0000707long
wdenk71f95112003-06-15 22:40:42 +0000708do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
709 int dols)
710{
Wolfgang Denkd06a5f72005-08-06 01:56:59 +0200711#if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
712 static
713#endif
wdenk71f95112003-06-15 22:40:42 +0000714 char fnamecopy[2048];
715 boot_sector bs;
716 volume_info volinfo;
717 fsdata datablock;
718 fsdata *mydata = &datablock;
719 dir_entry *dentptr;
720 __u16 prevcksum = 0xffff;
721 char *subname = "";
722 int rootdir_size, cursect;
723 int idx, isdir = 0;
724 int files = 0, dirs = 0;
725 long ret = 0;
726 int firsttime;
727
728 if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
729 FAT_DPRINT ("Error: reading boot sector\n");
730 return -1;
731 }
732 if (mydata->fatsize == 32) {
733 mydata->fatlength = bs.fat32_length;
734 } else {
735 mydata->fatlength = bs.fat_length;
736 }
737 mydata->fat_sect = bs.reserved;
738 cursect = mydata->rootdir_sect
739 = mydata->fat_sect + mydata->fatlength * bs.fats;
740 mydata->clust_size = bs.cluster_size;
741 if (mydata->fatsize == 32) {
742 rootdir_size = mydata->clust_size;
743 mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
744 - (mydata->clust_size * 2);
745 } else {
746 rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
747 * sizeof (dir_entry)) / SECTOR_SIZE;
748 mydata->data_begin = mydata->rootdir_sect + rootdir_size
749 - (mydata->clust_size * 2);
750 }
751 mydata->fatbufnum = -1;
752
753 FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
754 mydata->fatlength);
755 FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
756 "Data begins at: %d\n",
757 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
758 rootdir_size, mydata->data_begin);
759 FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
760
761 /* "cwd" is always the root... */
762 while (ISDIRDELIM (*filename))
763 filename++;
764 /* Make a copy of the filename and convert it to lowercase */
765 strcpy (fnamecopy, filename);
766 downcase (fnamecopy);
767 if (*fnamecopy == '\0') {
768 if (!dols)
769 return -1;
770 dols = LS_ROOT;
771 } else if ((idx = dirdelim (fnamecopy)) >= 0) {
772 isdir = 1;
773 fnamecopy[idx] = '\0';
774 subname = fnamecopy + idx + 1;
775 /* Handle multiple delimiters */
776 while (ISDIRDELIM (*subname))
777 subname++;
778 } else if (dols) {
779 isdir = 1;
780 }
781
782 while (1) {
783 int i;
784
wdenk5fa66df2003-10-29 23:18:55 +0000785 if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
wdenk71f95112003-06-15 22:40:42 +0000786 FAT_DPRINT ("Error: reading rootdir block\n");
787 return -1;
788 }
wdenk5fa66df2003-10-29 23:18:55 +0000789 dentptr = (dir_entry *) do_fat_read_block;
wdenk71f95112003-06-15 22:40:42 +0000790 for (i = 0; i < DIRENTSPERBLOCK; i++) {
791 char s_name[14], l_name[256];
792
793 l_name[0] = '\0';
794 if ((dentptr->attr & ATTR_VOLUME)) {
795#ifdef CONFIG_SUPPORT_VFAT
796 if ((dentptr->attr & ATTR_VFAT) &&
wdenk2d1a5372004-02-23 19:30:57 +0000797 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
wdenk71f95112003-06-15 22:40:42 +0000798 prevcksum = ((dir_slot *) dentptr)->alias_checksum;
wdenk5fa66df2003-10-29 23:18:55 +0000799 get_vfatname (mydata, 0, do_fat_read_block, dentptr, l_name);
wdenk71f95112003-06-15 22:40:42 +0000800 if (dols == LS_ROOT) {
801 int isdir = (dentptr->attr & ATTR_DIR);
802 char dirc;
803 int doit = 0;
804
805 if (isdir) {
806 dirs++;
807 dirc = '/';
808 doit = 1;
809 } else {
810 dirc = ' ';
811 if (l_name[0] != 0) {
812 files++;
813 doit = 1;
814 }
815 }
816 if (doit) {
817 if (dirc == ' ') {
818 printf (" %8ld %s%c\n",
819 (long) FAT2CPU32 (dentptr->size),
820 l_name, dirc);
821 } else {
822 printf (" %s%c\n", l_name, dirc);
823 }
824 }
825 dentptr++;
826 continue;
827 }
828 FAT_DPRINT ("Rootvfatname: |%s|\n", l_name);
829 } else
830#endif
831 {
832 /* Volume label or VFAT entry */
833 dentptr++;
834 continue;
835 }
836 } else if (dentptr->name[0] == 0) {
837 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
838 if (dols == LS_ROOT) {
839 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
840 return 0;
841 }
842 return -1;
843 }
844#ifdef CONFIG_SUPPORT_VFAT
845 else if (dols == LS_ROOT
846 && mkcksum (dentptr->name) == prevcksum) {
847 dentptr++;
848 continue;
849 }
850#endif
851 get_name (dentptr, s_name);
852 if (dols == LS_ROOT) {
853 int isdir = (dentptr->attr & ATTR_DIR);
854 char dirc;
855 int doit = 0;
856
857 if (isdir) {
wdenk71f95112003-06-15 22:40:42 +0000858 dirc = '/';
wdenka43278a2003-09-11 19:48:06 +0000859 if (s_name[0] != 0) {
860 dirs++;
861 doit = 1;
862 }
wdenk71f95112003-06-15 22:40:42 +0000863 } else {
864 dirc = ' ';
865 if (s_name[0] != 0) {
866 files++;
867 doit = 1;
868 }
869 }
870 if (doit) {
871 if (dirc == ' ') {
872 printf (" %8ld %s%c\n",
873 (long) FAT2CPU32 (dentptr->size), s_name,
874 dirc);
875 } else {
876 printf (" %s%c\n", s_name, dirc);
877 }
878 }
879 dentptr++;
880 continue;
881 }
882 if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
883 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
884 dentptr++;
885 continue;
886 }
887 if (isdir && !(dentptr->attr & ATTR_DIR))
888 return -1;
889
890 FAT_DPRINT ("RootName: %s", s_name);
891 FAT_DPRINT (", start: 0x%x", START (dentptr));
892 FAT_DPRINT (", size: 0x%x %s\n",
893 FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
894
895 goto rootdir_done; /* We got a match */
896 }
897 cursect++;
898 }
899 rootdir_done:
900
901 firsttime = 1;
902 while (isdir) {
903 int startsect = mydata->data_begin
904 + START (dentptr) * mydata->clust_size;
905 dir_entry dent;
906 char *nextname = NULL;
907
908 dent = *dentptr;
909 dentptr = &dent;
910
911 idx = dirdelim (subname);
912 if (idx >= 0) {
913 subname[idx] = '\0';
914 nextname = subname + idx + 1;
915 /* Handle multiple delimiters */
916 while (ISDIRDELIM (*nextname))
917 nextname++;
918 if (dols && *nextname == '\0')
919 firsttime = 0;
920 } else {
921 if (dols && firsttime) {
922 firsttime = 0;
923 } else {
924 isdir = 0;
925 }
926 }
927
928 if (get_dentfromdir (mydata, startsect, subname, dentptr,
929 isdir ? 0 : dols) == NULL) {
930 if (dols && !isdir)
931 return 0;
932 return -1;
933 }
934
935 if (idx >= 0) {
936 if (!(dentptr->attr & ATTR_DIR))
937 return -1;
938 subname = nextname;
939 }
940 }
941 ret = get_contents (mydata, dentptr, buffer, maxsize);
942 FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
943
944 return ret;
945}
946
947
948int
949file_fat_detectfs(void)
950{
951 boot_sector bs;
952 volume_info volinfo;
953 int fatsize;
wdenk7205e402003-09-10 22:30:53 +0000954 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +0000955
wdenk7205e402003-09-10 22:30:53 +0000956 if(cur_dev==NULL) {
957 printf("No current device\n");
958 return 1;
959 }
Jon Loeligerdd60d122007-07-09 17:56:50 -0500960#if defined(CONFIG_CMD_IDE) || \
unsik Kim75eb82e2009-02-25 11:31:24 +0900961 defined(CONFIG_CMD_MG_DISK) || \
Sonic Zhang8c5170a2008-12-09 23:20:18 -0500962 defined(CONFIG_CMD_SATA) || \
Jon Loeligerdd60d122007-07-09 17:56:50 -0500963 defined(CONFIG_CMD_SCSI) || \
964 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -0600965 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +0000966 printf("Interface: ");
967 switch(cur_dev->if_type) {
968 case IF_TYPE_IDE : printf("IDE"); break;
Sonic Zhang8c5170a2008-12-09 23:20:18 -0500969 case IF_TYPE_SATA : printf("SATA"); break;
wdenk7205e402003-09-10 22:30:53 +0000970 case IF_TYPE_SCSI : printf("SCSI"); break;
971 case IF_TYPE_ATAPI : printf("ATAPI"); break;
972 case IF_TYPE_USB : printf("USB"); break;
973 case IF_TYPE_DOC : printf("DOC"); break;
974 case IF_TYPE_MMC : printf("MMC"); break;
975 default : printf("Unknown");
976 }
977 printf("\n Device %d: ",cur_dev->dev);
978 dev_print(cur_dev);
979#endif
980 if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
981 printf("\nNo valid FAT fs found\n");
982 return 1;
983 }
984 memcpy (vol_label, volinfo.volume_label, 11);
985 vol_label[11] = '\0';
986 volinfo.fs_type[5]='\0';
Peter Pearse3e3b9562007-05-18 16:47:03 +0100987 printf("Partition %d: Filesystem: %s \"%s\"\n"
988 ,cur_part,volinfo.fs_type,vol_label);
wdenk7205e402003-09-10 22:30:53 +0000989 return 0;
wdenk71f95112003-06-15 22:40:42 +0000990}
991
992
993int
994file_fat_ls(const char *dir)
995{
996 return do_fat_read(dir, NULL, 0, LS_YES);
997}
998
999
1000long
1001file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1002{
wdenk7205e402003-09-10 22:30:53 +00001003 printf("reading %s\n",filename);
wdenk71f95112003-06-15 22:40:42 +00001004 return do_fat_read(filename, buffer, maxsize, LS_NO);
1005}