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