blob: 2f0bd8c14d2147e85cdfd426b3046c92dff3deb4 [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) || \
87 defined(CONFIG_CMD_SCSI) || \
88 defined(CONFIG_CMD_USB) || \
Andy Fleming02df4a22008-01-09 13:51:32 -060089 defined(CONFIG_MMC) || \
90 defined(CONFIG_SYSTEMACE) )
91 /* First we assume, there is a MBR */
92 if (!get_partition_info (dev_desc, part_no, &info)) {
93 part_offset = info.start;
94 cur_part = part_no;
95 } else if (!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3)) {
96 /* ok, we assume we are on a PBR only */
97 cur_part = 1;
98 part_offset = 0;
99 } else {
100 printf ("** Partition %d not valid on device %d **\n",
Wolfgang Denkbf1060e2007-08-07 16:02:13 +0200101 part_no, dev_desc->dev);
Andy Fleming02df4a22008-01-09 13:51:32 -0600102 return -1;
Wolfgang Denkbf1060e2007-08-07 16:02:13 +0200103 }
Andy Fleming02df4a22008-01-09 13:51:32 -0600104
105#else
106 if (!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
107 /* ok, we assume we are on a PBR only */
108 cur_part = 1;
109 part_offset = 0;
110 info.start = part_offset;
111 } else {
112 /* FIXME we need to determine the start block of the
113 * partition where the DOS FS resides. This can be done
114 * by using the get_partition_info routine. For this
115 * purpose the libpart must be included.
116 */
117 part_offset = 32;
118 cur_part = 1;
119 }
120#endif
wdenk71f95112003-06-15 22:40:42 +0000121 return 0;
122}
123
124
125/*
126 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
127 * Return index into string if found, -1 otherwise.
128 */
129static int
130dirdelim(char *str)
131{
132 char *start = str;
133
134 while (*str != '\0') {
135 if (ISDIRDELIM(*str)) return str - start;
136 str++;
137 }
138 return -1;
139}
140
141
142/*
143 * Match volume_info fs_type strings.
144 * Return 0 on match, -1 otherwise.
145 */
146static int
147compare_sign(char *str1, char *str2)
148{
149 char *end = str1+SIGNLEN;
150
151 while (str1 != end) {
152 if (*str1 != *str2) {
153 return -1;
154 }
155 str1++;
156 str2++;
157 }
158
159 return 0;
160}
161
162
163/*
164 * Extract zero terminated short name from a directory entry.
165 */
166static void get_name (dir_entry *dirent, char *s_name)
167{
168 char *ptr;
169
170 memcpy (s_name, dirent->name, 8);
171 s_name[8] = '\0';
172 ptr = s_name;
173 while (*ptr && *ptr != ' ')
174 ptr++;
175 if (dirent->ext[0] && dirent->ext[0] != ' ') {
176 *ptr = '.';
177 ptr++;
178 memcpy (ptr, dirent->ext, 3);
179 ptr[3] = '\0';
180 while (*ptr && *ptr != ' ')
181 ptr++;
182 }
183 *ptr = '\0';
184 if (*s_name == DELETED_FLAG)
185 *s_name = '\0';
186 else if (*s_name == aRING)
187 *s_name = 'Ã¥';
188 downcase (s_name);
189}
190
191/*
192 * Get the entry at index 'entry' in a FAT (12/16/32) table.
193 * On failure 0x00 is returned.
194 */
195static __u32
196get_fatent(fsdata *mydata, __u32 entry)
197{
198 __u32 bufnum;
199 __u32 offset;
200 __u32 ret = 0x00;
201
202 switch (mydata->fatsize) {
203 case 32:
204 bufnum = entry / FAT32BUFSIZE;
205 offset = entry - bufnum * FAT32BUFSIZE;
206 break;
207 case 16:
208 bufnum = entry / FAT16BUFSIZE;
209 offset = entry - bufnum * FAT16BUFSIZE;
210 break;
211 case 12:
212 bufnum = entry / FAT12BUFSIZE;
213 offset = entry - bufnum * FAT12BUFSIZE;
214 break;
215
216 default:
217 /* Unsupported FAT size */
218 return ret;
219 }
220
221 /* Read a new block of FAT entries into the cache. */
222 if (bufnum != mydata->fatbufnum) {
223 int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
224 __u8 *bufptr = mydata->fatbuf;
225 __u32 fatlength = mydata->fatlength;
226 __u32 startblock = bufnum * FATBUFBLOCKS;
227
228 fatlength *= SECTOR_SIZE; /* We want it in bytes now */
229 startblock += mydata->fat_sect; /* Offset from start of disk */
230
231 if (getsize > fatlength) getsize = fatlength;
232 if (disk_read(startblock, getsize, bufptr) < 0) {
233 FAT_DPRINT("Error reading FAT blocks\n");
234 return ret;
235 }
236 mydata->fatbufnum = bufnum;
237 }
238
239 /* Get the actual entry from the table */
240 switch (mydata->fatsize) {
241 case 32:
242 ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
243 break;
244 case 16:
245 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
246 break;
247 case 12: {
248 __u32 off16 = (offset*3)/4;
249 __u16 val1, val2;
250
251 switch (offset & 0x3) {
252 case 0:
253 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
254 ret &= 0xfff;
255 break;
256 case 1:
257 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
258 val1 &= 0xf000;
259 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
260 val2 &= 0x00ff;
261 ret = (val2 << 4) | (val1 >> 12);
262 break;
263 case 2:
264 val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
265 val1 &= 0xff00;
266 val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
267 val2 &= 0x000f;
268 ret = (val2 << 8) | (val1 >> 8);
269 break;
270 case 3:
271 ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
272 ret = (ret & 0xfff0) >> 4;
273 break;
274 default:
275 break;
276 }
277 }
278 break;
279 }
280 FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
281
282 return ret;
283}
284
285
286/*
287 * Read at most 'size' bytes from the specified cluster into 'buffer'.
288 * Return 0 on success, -1 otherwise.
289 */
290static int
291get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
292{
293 int idx = 0;
294 __u32 startsect;
295
296 if (clustnum > 0) {
297 startsect = mydata->data_begin + clustnum*mydata->clust_size;
298 } else {
299 startsect = mydata->rootdir_sect;
300 }
301
302 FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
wdenk7205e402003-09-10 22:30:53 +0000303 if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
304 FAT_DPRINT("Error reading data\n");
305 return -1;
306 }
307 if(size % FS_BLOCK_SIZE) {
308 __u8 tmpbuf[FS_BLOCK_SIZE];
309 idx= size/FS_BLOCK_SIZE;
310 if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
311 FAT_DPRINT("Error reading data\n");
312 return -1;
wdenk71f95112003-06-15 22:40:42 +0000313 }
wdenk7205e402003-09-10 22:30:53 +0000314 buffer += idx*FS_BLOCK_SIZE;
315
316 memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
317 return 0;
wdenk71f95112003-06-15 22:40:42 +0000318 }
319
320 return 0;
321}
322
323
324/*
325 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
326 * into 'buffer'.
327 * Return the number of bytes read or -1 on fatal errors.
328 */
329static long
330get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
331 unsigned long maxsize)
332{
333 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
334 unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
335 __u32 curclust = START(dentptr);
wdenk7205e402003-09-10 22:30:53 +0000336 __u32 endclust, newclust;
337 unsigned long actsize;
wdenk71f95112003-06-15 22:40:42 +0000338
339 FAT_DPRINT("Filesize: %ld bytes\n", filesize);
340
341 if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
342
343 FAT_DPRINT("Reading: %ld bytes\n", filesize);
344
wdenk7205e402003-09-10 22:30:53 +0000345 actsize=bytesperclust;
346 endclust=curclust;
wdenk71f95112003-06-15 22:40:42 +0000347 do {
wdenk7205e402003-09-10 22:30:53 +0000348 /* search for consecutive clusters */
349 while(actsize < filesize) {
350 newclust = get_fatent(mydata, endclust);
351 if((newclust -1)!=endclust)
352 goto getit;
michael8ce4e5c2008-03-02 23:33:46 +0100353 if (CHECK_CLUST(newclust, mydata->fatsize)) {
wdenk7205e402003-09-10 22:30:53 +0000354 FAT_DPRINT("curclust: 0x%x\n", newclust);
355 FAT_DPRINT("Invalid FAT entry\n");
356 return gotsize;
357 }
358 endclust=newclust;
359 actsize+= bytesperclust;
360 }
361 /* actsize >= file size */
362 actsize -= bytesperclust;
363 /* get remaining clusters */
364 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
wdenk71f95112003-06-15 22:40:42 +0000365 FAT_ERROR("Error reading cluster\n");
366 return -1;
367 }
wdenk7205e402003-09-10 22:30:53 +0000368 /* get remaining bytes */
369 gotsize += (int)actsize;
370 filesize -= actsize;
371 buffer += actsize;
372 actsize= filesize;
373 if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
374 FAT_ERROR("Error reading cluster\n");
375 return -1;
376 }
377 gotsize+=actsize;
378 return gotsize;
379getit:
380 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
381 FAT_ERROR("Error reading cluster\n");
382 return -1;
383 }
384 gotsize += (int)actsize;
385 filesize -= actsize;
386 buffer += actsize;
387 curclust = get_fatent(mydata, endclust);
michael8ce4e5c2008-03-02 23:33:46 +0100388 if (CHECK_CLUST(curclust, mydata->fatsize)) {
wdenk71f95112003-06-15 22:40:42 +0000389 FAT_DPRINT("curclust: 0x%x\n", curclust);
390 FAT_ERROR("Invalid FAT entry\n");
391 return gotsize;
392 }
wdenk7205e402003-09-10 22:30:53 +0000393 actsize=bytesperclust;
394 endclust=curclust;
wdenk71f95112003-06-15 22:40:42 +0000395 } while (1);
396}
397
398
399#ifdef CONFIG_SUPPORT_VFAT
400/*
401 * Extract the file name information from 'slotptr' into 'l_name',
402 * starting at l_name[*idx].
403 * Return 1 if terminator (zero byte) is found, 0 otherwise.
404 */
405static int
406slot2str(dir_slot *slotptr, char *l_name, int *idx)
407{
408 int j;
409
410 for (j = 0; j <= 8; j += 2) {
411 l_name[*idx] = slotptr->name0_4[j];
412 if (l_name[*idx] == 0x00) return 1;
413 (*idx)++;
414 }
415 for (j = 0; j <= 10; j += 2) {
416 l_name[*idx] = slotptr->name5_10[j];
417 if (l_name[*idx] == 0x00) return 1;
418 (*idx)++;
419 }
420 for (j = 0; j <= 2; j += 2) {
421 l_name[*idx] = slotptr->name11_12[j];
422 if (l_name[*idx] == 0x00) return 1;
423 (*idx)++;
424 }
425
426 return 0;
427}
428
429
430/*
431 * Extract the full long filename starting at 'retdent' (which is really
432 * a slot) into 'l_name'. If successful also copy the real directory entry
433 * into 'retdent'
434 * Return 0 on success, -1 otherwise.
435 */
wdenk5fa66df2003-10-29 23:18:55 +0000436__u8 get_vfatname_block[MAX_CLUSTSIZE];
wdenk71f95112003-06-15 22:40:42 +0000437static int
438get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
439 dir_entry *retdent, char *l_name)
440{
441 dir_entry *realdent;
442 dir_slot *slotptr = (dir_slot*) retdent;
443 __u8 *nextclust = cluster + mydata->clust_size * SECTOR_SIZE;
wdenk2d1a5372004-02-23 19:30:57 +0000444 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
wdenk71f95112003-06-15 22:40:42 +0000445 int idx = 0;
446
447 while ((__u8*)slotptr < nextclust) {
448 if (counter == 0) break;
wdenk2d1a5372004-02-23 19:30:57 +0000449 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
450 return -1;
wdenk71f95112003-06-15 22:40:42 +0000451 slotptr++;
452 counter--;
453 }
454
455 if ((__u8*)slotptr >= nextclust) {
wdenk71f95112003-06-15 22:40:42 +0000456 dir_slot *slotptr2;
457
458 slotptr--;
459 curclust = get_fatent(mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100460 if (CHECK_CLUST(curclust, mydata->fatsize)) {
wdenk71f95112003-06-15 22:40:42 +0000461 FAT_DPRINT("curclust: 0x%x\n", curclust);
462 FAT_ERROR("Invalid FAT entry\n");
463 return -1;
464 }
wdenk5fa66df2003-10-29 23:18:55 +0000465 if (get_cluster(mydata, curclust, get_vfatname_block,
wdenk71f95112003-06-15 22:40:42 +0000466 mydata->clust_size * SECTOR_SIZE) != 0) {
467 FAT_DPRINT("Error: reading directory block\n");
468 return -1;
469 }
wdenk5fa66df2003-10-29 23:18:55 +0000470 slotptr2 = (dir_slot*) get_vfatname_block;
wdenk71f95112003-06-15 22:40:42 +0000471 while (slotptr2->id > 0x01) {
472 slotptr2++;
473 }
474 /* Save the real directory entry */
475 realdent = (dir_entry*)slotptr2 + 1;
wdenk5fa66df2003-10-29 23:18:55 +0000476 while ((__u8*)slotptr2 >= get_vfatname_block) {
wdenk71f95112003-06-15 22:40:42 +0000477 slot2str(slotptr2, l_name, &idx);
478 slotptr2--;
479 }
480 } else {
481 /* Save the real directory entry */
482 realdent = (dir_entry*)slotptr;
483 }
484
485 do {
486 slotptr--;
487 if (slot2str(slotptr, l_name, &idx)) break;
wdenk2d1a5372004-02-23 19:30:57 +0000488 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
wdenk71f95112003-06-15 22:40:42 +0000489
490 l_name[idx] = '\0';
491 if (*l_name == DELETED_FLAG) *l_name = '\0';
492 else if (*l_name == aRING) *l_name = 'Ã¥';
493 downcase(l_name);
494
495 /* Return the real directory entry */
496 memcpy(retdent, realdent, sizeof(dir_entry));
497
498 return 0;
499}
500
501
502/* Calculate short name checksum */
503static __u8
504mkcksum(const char *str)
505{
506 int i;
507 __u8 ret = 0;
508
509 for (i = 0; i < 11; i++) {
510 ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
511 }
512
513 return ret;
514}
515#endif
516
517
518/*
519 * Get the directory entry associated with 'filename' from the directory
520 * starting at 'startsect'
521 */
wdenk5fa66df2003-10-29 23:18:55 +0000522__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
wdenk71f95112003-06-15 22:40:42 +0000523static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
524 char *filename, dir_entry * retdent,
525 int dols)
526{
527 __u16 prevcksum = 0xffff;
wdenk71f95112003-06-15 22:40:42 +0000528 __u32 curclust = START (retdent);
529 int files = 0, dirs = 0;
530
531 FAT_DPRINT ("get_dentfromdir: %s\n", filename);
532 while (1) {
533 dir_entry *dentptr;
534 int i;
535
wdenk5fa66df2003-10-29 23:18:55 +0000536 if (get_cluster (mydata, curclust, get_dentfromdir_block,
wdenk71f95112003-06-15 22:40:42 +0000537 mydata->clust_size * SECTOR_SIZE) != 0) {
538 FAT_DPRINT ("Error: reading directory block\n");
539 return NULL;
540 }
wdenk5fa66df2003-10-29 23:18:55 +0000541 dentptr = (dir_entry *) get_dentfromdir_block;
wdenk71f95112003-06-15 22:40:42 +0000542 for (i = 0; i < DIRENTSPERCLUST; i++) {
543 char s_name[14], l_name[256];
544
545 l_name[0] = '\0';
wdenk855a4962004-03-14 18:23:55 +0000546 if (dentptr->name[0] == DELETED_FLAG) {
547 dentptr++;
548 continue;
549 }
wdenk71f95112003-06-15 22:40:42 +0000550 if ((dentptr->attr & ATTR_VOLUME)) {
551#ifdef CONFIG_SUPPORT_VFAT
552 if ((dentptr->attr & ATTR_VFAT) &&
wdenk2d1a5372004-02-23 19:30:57 +0000553 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
wdenk71f95112003-06-15 22:40:42 +0000554 prevcksum = ((dir_slot *) dentptr)
555 ->alias_checksum;
wdenk5fa66df2003-10-29 23:18:55 +0000556 get_vfatname (mydata, curclust, get_dentfromdir_block,
wdenk71f95112003-06-15 22:40:42 +0000557 dentptr, l_name);
558 if (dols) {
559 int isdir = (dentptr->attr & ATTR_DIR);
560 char dirc;
561 int doit = 0;
562
563 if (isdir) {
564 dirs++;
565 dirc = '/';
566 doit = 1;
567 } else {
568 dirc = ' ';
569 if (l_name[0] != 0) {
570 files++;
571 doit = 1;
572 }
573 }
574 if (doit) {
575 if (dirc == ' ') {
576 printf (" %8ld %s%c\n",
577 (long) FAT2CPU32 (dentptr->size),
578 l_name, dirc);
579 } else {
580 printf (" %s%c\n", l_name, dirc);
581 }
582 }
583 dentptr++;
584 continue;
585 }
586 FAT_DPRINT ("vfatname: |%s|\n", l_name);
587 } else
588#endif
589 {
590 /* Volume label or VFAT entry */
591 dentptr++;
592 continue;
593 }
594 }
595 if (dentptr->name[0] == 0) {
596 if (dols) {
597 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
598 }
599 FAT_DPRINT ("Dentname == NULL - %d\n", i);
600 return NULL;
601 }
602#ifdef CONFIG_SUPPORT_VFAT
603 if (dols && mkcksum (dentptr->name) == prevcksum) {
604 dentptr++;
605 continue;
606 }
607#endif
608 get_name (dentptr, s_name);
609 if (dols) {
610 int isdir = (dentptr->attr & ATTR_DIR);
611 char dirc;
612 int doit = 0;
613
614 if (isdir) {
615 dirs++;
616 dirc = '/';
617 doit = 1;
618 } else {
619 dirc = ' ';
620 if (s_name[0] != 0) {
621 files++;
622 doit = 1;
623 }
624 }
625 if (doit) {
626 if (dirc == ' ') {
627 printf (" %8ld %s%c\n",
628 (long) FAT2CPU32 (dentptr->size), s_name,
629 dirc);
630 } else {
631 printf (" %s%c\n", s_name, dirc);
632 }
633 }
634 dentptr++;
635 continue;
636 }
637 if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
638 FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
639 dentptr++;
640 continue;
641 }
642 memcpy (retdent, dentptr, sizeof (dir_entry));
643
644 FAT_DPRINT ("DentName: %s", s_name);
645 FAT_DPRINT (", start: 0x%x", START (dentptr));
646 FAT_DPRINT (", size: 0x%x %s\n",
647 FAT2CPU32 (dentptr->size),
648 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
649
650 return retdent;
651 }
652 curclust = get_fatent (mydata, curclust);
michael8ce4e5c2008-03-02 23:33:46 +0100653 if (CHECK_CLUST(curclust, mydata->fatsize)) {
wdenk71f95112003-06-15 22:40:42 +0000654 FAT_DPRINT ("curclust: 0x%x\n", curclust);
655 FAT_ERROR ("Invalid FAT entry\n");
656 return NULL;
657 }
658 }
659
660 return NULL;
661}
662
663
664/*
665 * Read boot sector and volume info from a FAT filesystem
666 */
667static int
668read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
669{
670 __u8 block[FS_BLOCK_SIZE];
671 volume_info *vistart;
672
673 if (disk_read(0, 1, block) < 0) {
674 FAT_DPRINT("Error: reading block\n");
675 return -1;
676 }
677
678 memcpy(bs, block, sizeof(boot_sector));
679 bs->reserved = FAT2CPU16(bs->reserved);
680 bs->fat_length = FAT2CPU16(bs->fat_length);
681 bs->secs_track = FAT2CPU16(bs->secs_track);
682 bs->heads = FAT2CPU16(bs->heads);
683#if 0 /* UNUSED */
684 bs->hidden = FAT2CPU32(bs->hidden);
685#endif
686 bs->total_sect = FAT2CPU32(bs->total_sect);
687
688 /* FAT32 entries */
689 if (bs->fat_length == 0) {
690 /* Assume FAT32 */
691 bs->fat32_length = FAT2CPU32(bs->fat32_length);
692 bs->flags = FAT2CPU16(bs->flags);
693 bs->root_cluster = FAT2CPU32(bs->root_cluster);
694 bs->info_sector = FAT2CPU16(bs->info_sector);
695 bs->backup_boot = FAT2CPU16(bs->backup_boot);
696 vistart = (volume_info*) (block + sizeof(boot_sector));
697 *fatsize = 32;
698 } else {
699 vistart = (volume_info*) &(bs->fat32_length);
700 *fatsize = 0;
701 }
702 memcpy(volinfo, vistart, sizeof(volume_info));
703
704 /* Terminate fs_type string. Writing past the end of vistart
705 is ok - it's just the buffer. */
706 vistart->fs_type[8] = '\0';
707
708 if (*fatsize == 32) {
709 if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
710 return 0;
711 }
712 } else {
713 if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
714 *fatsize = 12;
715 return 0;
716 }
717 if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
718 *fatsize = 16;
719 return 0;
720 }
721 }
722
723 FAT_DPRINT("Error: broken fs_type sign\n");
724 return -1;
725}
726
727
wdenk5fa66df2003-10-29 23:18:55 +0000728__u8 do_fat_read_block[MAX_CLUSTSIZE]; /* Block buffer */
stroese20cc00d2004-12-16 17:57:26 +0000729long
wdenk71f95112003-06-15 22:40:42 +0000730do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
731 int dols)
732{
Wolfgang Denkd06a5f72005-08-06 01:56:59 +0200733#if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
734 static
735#endif
wdenk71f95112003-06-15 22:40:42 +0000736 char fnamecopy[2048];
737 boot_sector bs;
738 volume_info volinfo;
739 fsdata datablock;
740 fsdata *mydata = &datablock;
741 dir_entry *dentptr;
742 __u16 prevcksum = 0xffff;
743 char *subname = "";
744 int rootdir_size, cursect;
745 int idx, isdir = 0;
746 int files = 0, dirs = 0;
747 long ret = 0;
748 int firsttime;
749
750 if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
751 FAT_DPRINT ("Error: reading boot sector\n");
752 return -1;
753 }
754 if (mydata->fatsize == 32) {
755 mydata->fatlength = bs.fat32_length;
756 } else {
757 mydata->fatlength = bs.fat_length;
758 }
759 mydata->fat_sect = bs.reserved;
760 cursect = mydata->rootdir_sect
761 = mydata->fat_sect + mydata->fatlength * bs.fats;
762 mydata->clust_size = bs.cluster_size;
763 if (mydata->fatsize == 32) {
764 rootdir_size = mydata->clust_size;
765 mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
766 - (mydata->clust_size * 2);
767 } else {
768 rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
769 * sizeof (dir_entry)) / SECTOR_SIZE;
770 mydata->data_begin = mydata->rootdir_sect + rootdir_size
771 - (mydata->clust_size * 2);
772 }
773 mydata->fatbufnum = -1;
774
775 FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
776 mydata->fatlength);
777 FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
778 "Data begins at: %d\n",
779 mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
780 rootdir_size, mydata->data_begin);
781 FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
782
783 /* "cwd" is always the root... */
784 while (ISDIRDELIM (*filename))
785 filename++;
786 /* Make a copy of the filename and convert it to lowercase */
787 strcpy (fnamecopy, filename);
788 downcase (fnamecopy);
789 if (*fnamecopy == '\0') {
790 if (!dols)
791 return -1;
792 dols = LS_ROOT;
793 } else if ((idx = dirdelim (fnamecopy)) >= 0) {
794 isdir = 1;
795 fnamecopy[idx] = '\0';
796 subname = fnamecopy + idx + 1;
797 /* Handle multiple delimiters */
798 while (ISDIRDELIM (*subname))
799 subname++;
800 } else if (dols) {
801 isdir = 1;
802 }
803
804 while (1) {
805 int i;
806
wdenk5fa66df2003-10-29 23:18:55 +0000807 if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
wdenk71f95112003-06-15 22:40:42 +0000808 FAT_DPRINT ("Error: reading rootdir block\n");
809 return -1;
810 }
wdenk5fa66df2003-10-29 23:18:55 +0000811 dentptr = (dir_entry *) do_fat_read_block;
wdenk71f95112003-06-15 22:40:42 +0000812 for (i = 0; i < DIRENTSPERBLOCK; i++) {
813 char s_name[14], l_name[256];
814
815 l_name[0] = '\0';
816 if ((dentptr->attr & ATTR_VOLUME)) {
817#ifdef CONFIG_SUPPORT_VFAT
818 if ((dentptr->attr & ATTR_VFAT) &&
wdenk2d1a5372004-02-23 19:30:57 +0000819 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
wdenk71f95112003-06-15 22:40:42 +0000820 prevcksum = ((dir_slot *) dentptr)->alias_checksum;
wdenk5fa66df2003-10-29 23:18:55 +0000821 get_vfatname (mydata, 0, do_fat_read_block, dentptr, l_name);
wdenk71f95112003-06-15 22:40:42 +0000822 if (dols == LS_ROOT) {
823 int isdir = (dentptr->attr & ATTR_DIR);
824 char dirc;
825 int doit = 0;
826
827 if (isdir) {
828 dirs++;
829 dirc = '/';
830 doit = 1;
831 } else {
832 dirc = ' ';
833 if (l_name[0] != 0) {
834 files++;
835 doit = 1;
836 }
837 }
838 if (doit) {
839 if (dirc == ' ') {
840 printf (" %8ld %s%c\n",
841 (long) FAT2CPU32 (dentptr->size),
842 l_name, dirc);
843 } else {
844 printf (" %s%c\n", l_name, dirc);
845 }
846 }
847 dentptr++;
848 continue;
849 }
850 FAT_DPRINT ("Rootvfatname: |%s|\n", l_name);
851 } else
852#endif
853 {
854 /* Volume label or VFAT entry */
855 dentptr++;
856 continue;
857 }
858 } else if (dentptr->name[0] == 0) {
859 FAT_DPRINT ("RootDentname == NULL - %d\n", i);
860 if (dols == LS_ROOT) {
861 printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
862 return 0;
863 }
864 return -1;
865 }
866#ifdef CONFIG_SUPPORT_VFAT
867 else if (dols == LS_ROOT
868 && mkcksum (dentptr->name) == prevcksum) {
869 dentptr++;
870 continue;
871 }
872#endif
873 get_name (dentptr, s_name);
874 if (dols == LS_ROOT) {
875 int isdir = (dentptr->attr & ATTR_DIR);
876 char dirc;
877 int doit = 0;
878
879 if (isdir) {
wdenk71f95112003-06-15 22:40:42 +0000880 dirc = '/';
wdenka43278a2003-09-11 19:48:06 +0000881 if (s_name[0] != 0) {
882 dirs++;
883 doit = 1;
884 }
wdenk71f95112003-06-15 22:40:42 +0000885 } else {
886 dirc = ' ';
887 if (s_name[0] != 0) {
888 files++;
889 doit = 1;
890 }
891 }
892 if (doit) {
893 if (dirc == ' ') {
894 printf (" %8ld %s%c\n",
895 (long) FAT2CPU32 (dentptr->size), s_name,
896 dirc);
897 } else {
898 printf (" %s%c\n", s_name, dirc);
899 }
900 }
901 dentptr++;
902 continue;
903 }
904 if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
905 FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
906 dentptr++;
907 continue;
908 }
909 if (isdir && !(dentptr->attr & ATTR_DIR))
910 return -1;
911
912 FAT_DPRINT ("RootName: %s", s_name);
913 FAT_DPRINT (", start: 0x%x", START (dentptr));
914 FAT_DPRINT (", size: 0x%x %s\n",
915 FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
916
917 goto rootdir_done; /* We got a match */
918 }
919 cursect++;
920 }
921 rootdir_done:
922
923 firsttime = 1;
924 while (isdir) {
925 int startsect = mydata->data_begin
926 + START (dentptr) * mydata->clust_size;
927 dir_entry dent;
928 char *nextname = NULL;
929
930 dent = *dentptr;
931 dentptr = &dent;
932
933 idx = dirdelim (subname);
934 if (idx >= 0) {
935 subname[idx] = '\0';
936 nextname = subname + idx + 1;
937 /* Handle multiple delimiters */
938 while (ISDIRDELIM (*nextname))
939 nextname++;
940 if (dols && *nextname == '\0')
941 firsttime = 0;
942 } else {
943 if (dols && firsttime) {
944 firsttime = 0;
945 } else {
946 isdir = 0;
947 }
948 }
949
950 if (get_dentfromdir (mydata, startsect, subname, dentptr,
951 isdir ? 0 : dols) == NULL) {
952 if (dols && !isdir)
953 return 0;
954 return -1;
955 }
956
957 if (idx >= 0) {
958 if (!(dentptr->attr & ATTR_DIR))
959 return -1;
960 subname = nextname;
961 }
962 }
963 ret = get_contents (mydata, dentptr, buffer, maxsize);
964 FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
965
966 return ret;
967}
968
969
970int
971file_fat_detectfs(void)
972{
973 boot_sector bs;
974 volume_info volinfo;
975 int fatsize;
wdenk7205e402003-09-10 22:30:53 +0000976 char vol_label[12];
wdenk71f95112003-06-15 22:40:42 +0000977
wdenk7205e402003-09-10 22:30:53 +0000978 if(cur_dev==NULL) {
979 printf("No current device\n");
980 return 1;
981 }
Jon Loeligerdd60d122007-07-09 17:56:50 -0500982#if defined(CONFIG_CMD_IDE) || \
983 defined(CONFIG_CMD_SCSI) || \
984 defined(CONFIG_CMD_USB) || \
Andy Fleming21f6f962008-01-16 13:06:59 -0600985 defined(CONFIG_MMC)
wdenk7205e402003-09-10 22:30:53 +0000986 printf("Interface: ");
987 switch(cur_dev->if_type) {
988 case IF_TYPE_IDE : printf("IDE"); break;
989 case IF_TYPE_SCSI : printf("SCSI"); break;
990 case IF_TYPE_ATAPI : printf("ATAPI"); break;
991 case IF_TYPE_USB : printf("USB"); break;
992 case IF_TYPE_DOC : printf("DOC"); break;
993 case IF_TYPE_MMC : printf("MMC"); break;
994 default : printf("Unknown");
995 }
996 printf("\n Device %d: ",cur_dev->dev);
997 dev_print(cur_dev);
998#endif
999 if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1000 printf("\nNo valid FAT fs found\n");
1001 return 1;
1002 }
1003 memcpy (vol_label, volinfo.volume_label, 11);
1004 vol_label[11] = '\0';
1005 volinfo.fs_type[5]='\0';
Peter Pearse3e3b9562007-05-18 16:47:03 +01001006 printf("Partition %d: Filesystem: %s \"%s\"\n"
1007 ,cur_part,volinfo.fs_type,vol_label);
wdenk7205e402003-09-10 22:30:53 +00001008 return 0;
wdenk71f95112003-06-15 22:40:42 +00001009}
1010
1011
1012int
1013file_fat_ls(const char *dir)
1014{
1015 return do_fat_read(dir, NULL, 0, LS_YES);
1016}
1017
1018
1019long
1020file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1021{
wdenk7205e402003-09-10 22:30:53 +00001022 printf("reading %s\n",filename);
wdenk71f95112003-06-15 22:40:42 +00001023 return do_fat_read(filename, buffer, maxsize, LS_NO);
1024}