wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1 | /* |
| 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> |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 32 | #include <part.h> |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 33 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 34 | /* |
| 35 | * Convert a string to lowercase. |
| 36 | */ |
| 37 | static void |
| 38 | downcase(char *str) |
| 39 | { |
| 40 | while (*str != '\0') { |
| 41 | TOLOWER(*str); |
| 42 | str++; |
| 43 | } |
| 44 | } |
| 45 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 46 | static block_dev_desc_t *cur_dev = NULL; |
| 47 | static unsigned long part_offset = 0; |
| 48 | static 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 |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 53 | |
| 54 | int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr) |
| 55 | { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 56 | startblock += part_offset; |
| 57 | if (cur_dev == NULL) |
| 58 | return -1; |
| 59 | if (cur_dev->block_read) { |
Peter Pearse | fb312c8 | 2007-05-18 16:47:03 +0100 | [diff] [blame] | 60 | return cur_dev->block_read (cur_dev->dev |
| 61 | , startblock, getsize, (unsigned long *)bufptr); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 62 | } |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | int |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 68 | fat_register_device(block_dev_desc_t *dev_desc, int part_no) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 69 | { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 70 | unsigned char buffer[SECTOR_SIZE]; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 71 | disk_partition_t info; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 72 | |
| 73 | if (!dev_desc->block_read) |
| 74 | return -1; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 75 | cur_dev = dev_desc; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 76 | /* 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 Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 86 | #if (defined(CONFIG_CMD_IDE) || \ |
Sonic Zhang | 853208e | 2008-12-09 23:20:18 -0500 | [diff] [blame] | 87 | defined(CONFIG_CMD_SATA) || \ |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 88 | defined(CONFIG_CMD_SCSI) || \ |
| 89 | defined(CONFIG_CMD_USB) || \ |
Andy Fleming | 55b8668 | 2008-01-09 13:51:32 -0600 | [diff] [blame] | 90 | 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 Denk | e78f258 | 2007-08-07 16:02:13 +0200 | [diff] [blame] | 102 | part_no, dev_desc->dev); |
Andy Fleming | 55b8668 | 2008-01-09 13:51:32 -0600 | [diff] [blame] | 103 | return -1; |
| 104 | } |
| 105 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 106 | #else |
Andy Fleming | 55b8668 | 2008-01-09 13:51:32 -0600 | [diff] [blame] | 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; |
Wolfgang Denk | e78f258 | 2007-08-07 16:02:13 +0200 | [diff] [blame] | 120 | } |
Andy Fleming | 55b8668 | 2008-01-09 13:51:32 -0600 | [diff] [blame] | 121 | #endif |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 122 | 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 | */ |
| 130 | static int |
| 131 | dirdelim(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 | */ |
| 147 | static int |
| 148 | compare_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 | */ |
| 167 | static 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 Bohmer | 7c8f2ce | 2008-11-27 22:30:27 +0100 | [diff] [blame] | 188 | *s_name = DELETED_FLAG; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 189 | 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 | */ |
| 196 | static __u32 |
| 197 | get_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 | */ |
| 291 | static int |
| 292 | get_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); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 304 | 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; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 314 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 315 | buffer += idx*FS_BLOCK_SIZE; |
| 316 | |
| 317 | memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE); |
| 318 | return 0; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 319 | } |
| 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 | */ |
| 330 | static long |
| 331 | get_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); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 337 | __u32 endclust, newclust; |
| 338 | unsigned long actsize; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 339 | |
| 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 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 346 | actsize=bytesperclust; |
| 347 | endclust=curclust; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 348 | do { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 349 | /* search for consecutive clusters */ |
| 350 | while(actsize < filesize) { |
| 351 | newclust = get_fatent(mydata, endclust); |
| 352 | if((newclust -1)!=endclust) |
| 353 | goto getit; |
michael | 2c4d298 | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 354 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 355 | 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) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 366 | FAT_ERROR("Error reading cluster\n"); |
| 367 | return -1; |
| 368 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 369 | /* 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; |
| 380 | getit: |
| 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); |
michael | 2c4d298 | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 389 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 390 | FAT_DPRINT("curclust: 0x%x\n", curclust); |
| 391 | FAT_ERROR("Invalid FAT entry\n"); |
| 392 | return gotsize; |
| 393 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 394 | actsize=bytesperclust; |
| 395 | endclust=curclust; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 396 | } 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 | */ |
| 406 | static int |
| 407 | slot2str(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 | */ |
Bryan Wu | 95f99a4 | 2009-01-02 20:47:45 -0500 | [diff] [blame^] | 437 | __attribute__ ((__aligned__(__alignof__(dir_entry)))) |
| 438 | __u8 get_vfatname_block[MAX_CLUSTSIZE]; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 439 | static int |
| 440 | get_vfatname(fsdata *mydata, int curclust, __u8 *cluster, |
| 441 | dir_entry *retdent, char *l_name) |
| 442 | { |
| 443 | dir_entry *realdent; |
| 444 | dir_slot *slotptr = (dir_slot*) retdent; |
| 445 | __u8 *nextclust = cluster + mydata->clust_size * SECTOR_SIZE; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 446 | __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 447 | int idx = 0; |
| 448 | |
| 449 | while ((__u8*)slotptr < nextclust) { |
| 450 | if (counter == 0) break; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 451 | if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter) |
| 452 | return -1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 453 | slotptr++; |
| 454 | counter--; |
| 455 | } |
| 456 | |
| 457 | if ((__u8*)slotptr >= nextclust) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 458 | dir_slot *slotptr2; |
| 459 | |
| 460 | slotptr--; |
| 461 | curclust = get_fatent(mydata, curclust); |
michael | 2c4d298 | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 462 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 463 | FAT_DPRINT("curclust: 0x%x\n", curclust); |
| 464 | FAT_ERROR("Invalid FAT entry\n"); |
| 465 | return -1; |
| 466 | } |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 467 | if (get_cluster(mydata, curclust, get_vfatname_block, |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 468 | mydata->clust_size * SECTOR_SIZE) != 0) { |
| 469 | FAT_DPRINT("Error: reading directory block\n"); |
| 470 | return -1; |
| 471 | } |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 472 | slotptr2 = (dir_slot*) get_vfatname_block; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 473 | while (slotptr2->id > 0x01) { |
| 474 | slotptr2++; |
| 475 | } |
| 476 | /* Save the real directory entry */ |
| 477 | realdent = (dir_entry*)slotptr2 + 1; |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 478 | while ((__u8*)slotptr2 >= get_vfatname_block) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 479 | slot2str(slotptr2, l_name, &idx); |
| 480 | slotptr2--; |
| 481 | } |
| 482 | } else { |
| 483 | /* Save the real directory entry */ |
| 484 | realdent = (dir_entry*)slotptr; |
| 485 | } |
| 486 | |
| 487 | do { |
| 488 | slotptr--; |
| 489 | if (slot2str(slotptr, l_name, &idx)) break; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 490 | } while (!(slotptr->id & LAST_LONG_ENTRY_MASK)); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 491 | |
| 492 | l_name[idx] = '\0'; |
| 493 | if (*l_name == DELETED_FLAG) *l_name = '\0'; |
Remy Bohmer | 7c8f2ce | 2008-11-27 22:30:27 +0100 | [diff] [blame] | 494 | else if (*l_name == aRING) *l_name = DELETED_FLAG; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 495 | downcase(l_name); |
| 496 | |
| 497 | /* Return the real directory entry */ |
| 498 | memcpy(retdent, realdent, sizeof(dir_entry)); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /* Calculate short name checksum */ |
| 505 | static __u8 |
| 506 | mkcksum(const char *str) |
| 507 | { |
| 508 | int i; |
| 509 | __u8 ret = 0; |
| 510 | |
| 511 | for (i = 0; i < 11; i++) { |
| 512 | ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i]; |
| 513 | } |
| 514 | |
| 515 | return ret; |
| 516 | } |
| 517 | #endif |
| 518 | |
| 519 | |
| 520 | /* |
| 521 | * Get the directory entry associated with 'filename' from the directory |
| 522 | * starting at 'startsect' |
| 523 | */ |
Bryan Wu | 95f99a4 | 2009-01-02 20:47:45 -0500 | [diff] [blame^] | 524 | __attribute__ ((__aligned__(__alignof__(dir_entry)))) |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 525 | __u8 get_dentfromdir_block[MAX_CLUSTSIZE]; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 526 | static dir_entry *get_dentfromdir (fsdata * mydata, int startsect, |
| 527 | char *filename, dir_entry * retdent, |
| 528 | int dols) |
| 529 | { |
| 530 | __u16 prevcksum = 0xffff; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 531 | __u32 curclust = START (retdent); |
| 532 | int files = 0, dirs = 0; |
| 533 | |
| 534 | FAT_DPRINT ("get_dentfromdir: %s\n", filename); |
| 535 | while (1) { |
| 536 | dir_entry *dentptr; |
| 537 | int i; |
| 538 | |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 539 | if (get_cluster (mydata, curclust, get_dentfromdir_block, |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 540 | mydata->clust_size * SECTOR_SIZE) != 0) { |
| 541 | FAT_DPRINT ("Error: reading directory block\n"); |
| 542 | return NULL; |
| 543 | } |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 544 | dentptr = (dir_entry *) get_dentfromdir_block; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 545 | for (i = 0; i < DIRENTSPERCLUST; i++) { |
| 546 | char s_name[14], l_name[256]; |
| 547 | |
| 548 | l_name[0] = '\0'; |
wdenk | ed2ac4b | 2004-03-14 18:23:55 +0000 | [diff] [blame] | 549 | if (dentptr->name[0] == DELETED_FLAG) { |
| 550 | dentptr++; |
| 551 | continue; |
| 552 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 553 | if ((dentptr->attr & ATTR_VOLUME)) { |
| 554 | #ifdef CONFIG_SUPPORT_VFAT |
| 555 | if ((dentptr->attr & ATTR_VFAT) && |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 556 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 557 | prevcksum = ((dir_slot *) dentptr) |
| 558 | ->alias_checksum; |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 559 | get_vfatname (mydata, curclust, get_dentfromdir_block, |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 560 | dentptr, l_name); |
| 561 | if (dols) { |
| 562 | int isdir = (dentptr->attr & ATTR_DIR); |
| 563 | char dirc; |
| 564 | int doit = 0; |
| 565 | |
| 566 | if (isdir) { |
| 567 | dirs++; |
| 568 | dirc = '/'; |
| 569 | doit = 1; |
| 570 | } else { |
| 571 | dirc = ' '; |
| 572 | if (l_name[0] != 0) { |
| 573 | files++; |
| 574 | doit = 1; |
| 575 | } |
| 576 | } |
| 577 | if (doit) { |
| 578 | if (dirc == ' ') { |
| 579 | printf (" %8ld %s%c\n", |
| 580 | (long) FAT2CPU32 (dentptr->size), |
| 581 | l_name, dirc); |
| 582 | } else { |
| 583 | printf (" %s%c\n", l_name, dirc); |
| 584 | } |
| 585 | } |
| 586 | dentptr++; |
| 587 | continue; |
| 588 | } |
| 589 | FAT_DPRINT ("vfatname: |%s|\n", l_name); |
| 590 | } else |
| 591 | #endif |
| 592 | { |
| 593 | /* Volume label or VFAT entry */ |
| 594 | dentptr++; |
| 595 | continue; |
| 596 | } |
| 597 | } |
| 598 | if (dentptr->name[0] == 0) { |
| 599 | if (dols) { |
| 600 | printf ("\n%d file(s), %d dir(s)\n\n", files, dirs); |
| 601 | } |
| 602 | FAT_DPRINT ("Dentname == NULL - %d\n", i); |
| 603 | return NULL; |
| 604 | } |
| 605 | #ifdef CONFIG_SUPPORT_VFAT |
| 606 | if (dols && mkcksum (dentptr->name) == prevcksum) { |
| 607 | dentptr++; |
| 608 | continue; |
| 609 | } |
| 610 | #endif |
| 611 | get_name (dentptr, s_name); |
| 612 | if (dols) { |
| 613 | int isdir = (dentptr->attr & ATTR_DIR); |
| 614 | char dirc; |
| 615 | int doit = 0; |
| 616 | |
| 617 | if (isdir) { |
| 618 | dirs++; |
| 619 | dirc = '/'; |
| 620 | doit = 1; |
| 621 | } else { |
| 622 | dirc = ' '; |
| 623 | if (s_name[0] != 0) { |
| 624 | files++; |
| 625 | doit = 1; |
| 626 | } |
| 627 | } |
| 628 | if (doit) { |
| 629 | if (dirc == ' ') { |
| 630 | printf (" %8ld %s%c\n", |
| 631 | (long) FAT2CPU32 (dentptr->size), s_name, |
| 632 | dirc); |
| 633 | } else { |
| 634 | printf (" %s%c\n", s_name, dirc); |
| 635 | } |
| 636 | } |
| 637 | dentptr++; |
| 638 | continue; |
| 639 | } |
| 640 | if (strcmp (filename, s_name) && strcmp (filename, l_name)) { |
| 641 | FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name); |
| 642 | dentptr++; |
| 643 | continue; |
| 644 | } |
| 645 | memcpy (retdent, dentptr, sizeof (dir_entry)); |
| 646 | |
| 647 | FAT_DPRINT ("DentName: %s", s_name); |
| 648 | FAT_DPRINT (", start: 0x%x", START (dentptr)); |
| 649 | FAT_DPRINT (", size: 0x%x %s\n", |
| 650 | FAT2CPU32 (dentptr->size), |
| 651 | (dentptr->attr & ATTR_DIR) ? "(DIR)" : ""); |
| 652 | |
| 653 | return retdent; |
| 654 | } |
| 655 | curclust = get_fatent (mydata, curclust); |
michael | 2c4d298 | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 656 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 657 | FAT_DPRINT ("curclust: 0x%x\n", curclust); |
| 658 | FAT_ERROR ("Invalid FAT entry\n"); |
| 659 | return NULL; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | return NULL; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /* |
| 668 | * Read boot sector and volume info from a FAT filesystem |
| 669 | */ |
| 670 | static int |
| 671 | read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) |
| 672 | { |
| 673 | __u8 block[FS_BLOCK_SIZE]; |
| 674 | volume_info *vistart; |
| 675 | |
| 676 | if (disk_read(0, 1, block) < 0) { |
| 677 | FAT_DPRINT("Error: reading block\n"); |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | memcpy(bs, block, sizeof(boot_sector)); |
| 682 | bs->reserved = FAT2CPU16(bs->reserved); |
| 683 | bs->fat_length = FAT2CPU16(bs->fat_length); |
| 684 | bs->secs_track = FAT2CPU16(bs->secs_track); |
| 685 | bs->heads = FAT2CPU16(bs->heads); |
| 686 | #if 0 /* UNUSED */ |
| 687 | bs->hidden = FAT2CPU32(bs->hidden); |
| 688 | #endif |
| 689 | bs->total_sect = FAT2CPU32(bs->total_sect); |
| 690 | |
| 691 | /* FAT32 entries */ |
| 692 | if (bs->fat_length == 0) { |
| 693 | /* Assume FAT32 */ |
| 694 | bs->fat32_length = FAT2CPU32(bs->fat32_length); |
| 695 | bs->flags = FAT2CPU16(bs->flags); |
| 696 | bs->root_cluster = FAT2CPU32(bs->root_cluster); |
| 697 | bs->info_sector = FAT2CPU16(bs->info_sector); |
| 698 | bs->backup_boot = FAT2CPU16(bs->backup_boot); |
| 699 | vistart = (volume_info*) (block + sizeof(boot_sector)); |
| 700 | *fatsize = 32; |
| 701 | } else { |
| 702 | vistart = (volume_info*) &(bs->fat32_length); |
| 703 | *fatsize = 0; |
| 704 | } |
| 705 | memcpy(volinfo, vistart, sizeof(volume_info)); |
| 706 | |
| 707 | /* Terminate fs_type string. Writing past the end of vistart |
| 708 | is ok - it's just the buffer. */ |
| 709 | vistart->fs_type[8] = '\0'; |
| 710 | |
| 711 | if (*fatsize == 32) { |
| 712 | if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) { |
| 713 | return 0; |
| 714 | } |
| 715 | } else { |
| 716 | if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) { |
| 717 | *fatsize = 12; |
| 718 | return 0; |
| 719 | } |
| 720 | if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) { |
| 721 | *fatsize = 16; |
| 722 | return 0; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | FAT_DPRINT("Error: broken fs_type sign\n"); |
| 727 | return -1; |
| 728 | } |
| 729 | |
Bryan Wu | 95f99a4 | 2009-01-02 20:47:45 -0500 | [diff] [blame^] | 730 | __attribute__ ((__aligned__(__alignof__(dir_entry)))) |
| 731 | __u8 do_fat_read_block[MAX_CLUSTSIZE]; |
stroese | 8308224 | 2004-12-16 17:57:26 +0000 | [diff] [blame] | 732 | long |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 733 | do_fat_read (const char *filename, void *buffer, unsigned long maxsize, |
| 734 | int dols) |
| 735 | { |
Wolfgang Denk | 9f191a1 | 2005-08-06 01:56:59 +0200 | [diff] [blame] | 736 | #if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */ |
| 737 | static |
| 738 | #endif |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 739 | char fnamecopy[2048]; |
| 740 | boot_sector bs; |
| 741 | volume_info volinfo; |
| 742 | fsdata datablock; |
| 743 | fsdata *mydata = &datablock; |
| 744 | dir_entry *dentptr; |
| 745 | __u16 prevcksum = 0xffff; |
| 746 | char *subname = ""; |
| 747 | int rootdir_size, cursect; |
| 748 | int idx, isdir = 0; |
| 749 | int files = 0, dirs = 0; |
| 750 | long ret = 0; |
| 751 | int firsttime; |
| 752 | |
| 753 | if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) { |
| 754 | FAT_DPRINT ("Error: reading boot sector\n"); |
| 755 | return -1; |
| 756 | } |
| 757 | if (mydata->fatsize == 32) { |
| 758 | mydata->fatlength = bs.fat32_length; |
| 759 | } else { |
| 760 | mydata->fatlength = bs.fat_length; |
| 761 | } |
| 762 | mydata->fat_sect = bs.reserved; |
| 763 | cursect = mydata->rootdir_sect |
| 764 | = mydata->fat_sect + mydata->fatlength * bs.fats; |
| 765 | mydata->clust_size = bs.cluster_size; |
| 766 | if (mydata->fatsize == 32) { |
| 767 | rootdir_size = mydata->clust_size; |
| 768 | mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */ |
| 769 | - (mydata->clust_size * 2); |
| 770 | } else { |
| 771 | rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0]) |
| 772 | * sizeof (dir_entry)) / SECTOR_SIZE; |
| 773 | mydata->data_begin = mydata->rootdir_sect + rootdir_size |
| 774 | - (mydata->clust_size * 2); |
| 775 | } |
| 776 | mydata->fatbufnum = -1; |
| 777 | |
| 778 | FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize, |
| 779 | mydata->fatlength); |
| 780 | FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n" |
| 781 | "Data begins at: %d\n", |
| 782 | mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE, |
| 783 | rootdir_size, mydata->data_begin); |
| 784 | FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size); |
| 785 | |
| 786 | /* "cwd" is always the root... */ |
| 787 | while (ISDIRDELIM (*filename)) |
| 788 | filename++; |
| 789 | /* Make a copy of the filename and convert it to lowercase */ |
| 790 | strcpy (fnamecopy, filename); |
| 791 | downcase (fnamecopy); |
| 792 | if (*fnamecopy == '\0') { |
| 793 | if (!dols) |
| 794 | return -1; |
| 795 | dols = LS_ROOT; |
| 796 | } else if ((idx = dirdelim (fnamecopy)) >= 0) { |
| 797 | isdir = 1; |
| 798 | fnamecopy[idx] = '\0'; |
| 799 | subname = fnamecopy + idx + 1; |
| 800 | /* Handle multiple delimiters */ |
| 801 | while (ISDIRDELIM (*subname)) |
| 802 | subname++; |
| 803 | } else if (dols) { |
| 804 | isdir = 1; |
| 805 | } |
| 806 | |
| 807 | while (1) { |
| 808 | int i; |
| 809 | |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 810 | if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 811 | FAT_DPRINT ("Error: reading rootdir block\n"); |
| 812 | return -1; |
| 813 | } |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 814 | dentptr = (dir_entry *) do_fat_read_block; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 815 | for (i = 0; i < DIRENTSPERBLOCK; i++) { |
| 816 | char s_name[14], l_name[256]; |
| 817 | |
| 818 | l_name[0] = '\0'; |
| 819 | if ((dentptr->attr & ATTR_VOLUME)) { |
| 820 | #ifdef CONFIG_SUPPORT_VFAT |
| 821 | if ((dentptr->attr & ATTR_VFAT) && |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 822 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 823 | prevcksum = ((dir_slot *) dentptr)->alias_checksum; |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 824 | get_vfatname (mydata, 0, do_fat_read_block, dentptr, l_name); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 825 | if (dols == LS_ROOT) { |
| 826 | int isdir = (dentptr->attr & ATTR_DIR); |
| 827 | char dirc; |
| 828 | int doit = 0; |
| 829 | |
| 830 | if (isdir) { |
| 831 | dirs++; |
| 832 | dirc = '/'; |
| 833 | doit = 1; |
| 834 | } else { |
| 835 | dirc = ' '; |
| 836 | if (l_name[0] != 0) { |
| 837 | files++; |
| 838 | doit = 1; |
| 839 | } |
| 840 | } |
| 841 | if (doit) { |
| 842 | if (dirc == ' ') { |
| 843 | printf (" %8ld %s%c\n", |
| 844 | (long) FAT2CPU32 (dentptr->size), |
| 845 | l_name, dirc); |
| 846 | } else { |
| 847 | printf (" %s%c\n", l_name, dirc); |
| 848 | } |
| 849 | } |
| 850 | dentptr++; |
| 851 | continue; |
| 852 | } |
| 853 | FAT_DPRINT ("Rootvfatname: |%s|\n", l_name); |
| 854 | } else |
| 855 | #endif |
| 856 | { |
| 857 | /* Volume label or VFAT entry */ |
| 858 | dentptr++; |
| 859 | continue; |
| 860 | } |
| 861 | } else if (dentptr->name[0] == 0) { |
| 862 | FAT_DPRINT ("RootDentname == NULL - %d\n", i); |
| 863 | if (dols == LS_ROOT) { |
| 864 | printf ("\n%d file(s), %d dir(s)\n\n", files, dirs); |
| 865 | return 0; |
| 866 | } |
| 867 | return -1; |
| 868 | } |
| 869 | #ifdef CONFIG_SUPPORT_VFAT |
| 870 | else if (dols == LS_ROOT |
| 871 | && mkcksum (dentptr->name) == prevcksum) { |
| 872 | dentptr++; |
| 873 | continue; |
| 874 | } |
| 875 | #endif |
| 876 | get_name (dentptr, s_name); |
| 877 | if (dols == LS_ROOT) { |
| 878 | int isdir = (dentptr->attr & ATTR_DIR); |
| 879 | char dirc; |
| 880 | int doit = 0; |
| 881 | |
| 882 | if (isdir) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 883 | dirc = '/'; |
wdenk | 934c4f8 | 2003-09-11 19:48:06 +0000 | [diff] [blame] | 884 | if (s_name[0] != 0) { |
| 885 | dirs++; |
| 886 | doit = 1; |
| 887 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 888 | } else { |
| 889 | dirc = ' '; |
| 890 | if (s_name[0] != 0) { |
| 891 | files++; |
| 892 | doit = 1; |
| 893 | } |
| 894 | } |
| 895 | if (doit) { |
| 896 | if (dirc == ' ') { |
| 897 | printf (" %8ld %s%c\n", |
| 898 | (long) FAT2CPU32 (dentptr->size), s_name, |
| 899 | dirc); |
| 900 | } else { |
| 901 | printf (" %s%c\n", s_name, dirc); |
| 902 | } |
| 903 | } |
| 904 | dentptr++; |
| 905 | continue; |
| 906 | } |
| 907 | if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) { |
| 908 | FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name); |
| 909 | dentptr++; |
| 910 | continue; |
| 911 | } |
| 912 | if (isdir && !(dentptr->attr & ATTR_DIR)) |
| 913 | return -1; |
| 914 | |
| 915 | FAT_DPRINT ("RootName: %s", s_name); |
| 916 | FAT_DPRINT (", start: 0x%x", START (dentptr)); |
| 917 | FAT_DPRINT (", size: 0x%x %s\n", |
| 918 | FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : ""); |
| 919 | |
| 920 | goto rootdir_done; /* We got a match */ |
| 921 | } |
| 922 | cursect++; |
| 923 | } |
| 924 | rootdir_done: |
| 925 | |
| 926 | firsttime = 1; |
| 927 | while (isdir) { |
| 928 | int startsect = mydata->data_begin |
| 929 | + START (dentptr) * mydata->clust_size; |
| 930 | dir_entry dent; |
| 931 | char *nextname = NULL; |
| 932 | |
| 933 | dent = *dentptr; |
| 934 | dentptr = &dent; |
| 935 | |
| 936 | idx = dirdelim (subname); |
| 937 | if (idx >= 0) { |
| 938 | subname[idx] = '\0'; |
| 939 | nextname = subname + idx + 1; |
| 940 | /* Handle multiple delimiters */ |
| 941 | while (ISDIRDELIM (*nextname)) |
| 942 | nextname++; |
| 943 | if (dols && *nextname == '\0') |
| 944 | firsttime = 0; |
| 945 | } else { |
| 946 | if (dols && firsttime) { |
| 947 | firsttime = 0; |
| 948 | } else { |
| 949 | isdir = 0; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | if (get_dentfromdir (mydata, startsect, subname, dentptr, |
| 954 | isdir ? 0 : dols) == NULL) { |
| 955 | if (dols && !isdir) |
| 956 | return 0; |
| 957 | return -1; |
| 958 | } |
| 959 | |
| 960 | if (idx >= 0) { |
| 961 | if (!(dentptr->attr & ATTR_DIR)) |
| 962 | return -1; |
| 963 | subname = nextname; |
| 964 | } |
| 965 | } |
| 966 | ret = get_contents (mydata, dentptr, buffer, maxsize); |
| 967 | FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret); |
| 968 | |
| 969 | return ret; |
| 970 | } |
| 971 | |
| 972 | |
| 973 | int |
| 974 | file_fat_detectfs(void) |
| 975 | { |
| 976 | boot_sector bs; |
| 977 | volume_info volinfo; |
| 978 | int fatsize; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 979 | char vol_label[12]; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 980 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 981 | if(cur_dev==NULL) { |
| 982 | printf("No current device\n"); |
| 983 | return 1; |
| 984 | } |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 985 | #if defined(CONFIG_CMD_IDE) || \ |
Sonic Zhang | 853208e | 2008-12-09 23:20:18 -0500 | [diff] [blame] | 986 | defined(CONFIG_CMD_SATA) || \ |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 987 | defined(CONFIG_CMD_SCSI) || \ |
| 988 | defined(CONFIG_CMD_USB) || \ |
Andy Fleming | 1efe578 | 2008-01-16 13:06:59 -0600 | [diff] [blame] | 989 | defined(CONFIG_MMC) |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 990 | printf("Interface: "); |
| 991 | switch(cur_dev->if_type) { |
| 992 | case IF_TYPE_IDE : printf("IDE"); break; |
Sonic Zhang | 853208e | 2008-12-09 23:20:18 -0500 | [diff] [blame] | 993 | case IF_TYPE_SATA : printf("SATA"); break; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 994 | case IF_TYPE_SCSI : printf("SCSI"); break; |
| 995 | case IF_TYPE_ATAPI : printf("ATAPI"); break; |
| 996 | case IF_TYPE_USB : printf("USB"); break; |
| 997 | case IF_TYPE_DOC : printf("DOC"); break; |
| 998 | case IF_TYPE_MMC : printf("MMC"); break; |
| 999 | default : printf("Unknown"); |
| 1000 | } |
| 1001 | printf("\n Device %d: ",cur_dev->dev); |
| 1002 | dev_print(cur_dev); |
| 1003 | #endif |
| 1004 | if(read_bootsectandvi(&bs, &volinfo, &fatsize)) { |
| 1005 | printf("\nNo valid FAT fs found\n"); |
| 1006 | return 1; |
| 1007 | } |
| 1008 | memcpy (vol_label, volinfo.volume_label, 11); |
| 1009 | vol_label[11] = '\0'; |
| 1010 | volinfo.fs_type[5]='\0'; |
Peter Pearse | fb312c8 | 2007-05-18 16:47:03 +0100 | [diff] [blame] | 1011 | printf("Partition %d: Filesystem: %s \"%s\"\n" |
| 1012 | ,cur_part,volinfo.fs_type,vol_label); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1013 | return 0; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | int |
| 1018 | file_fat_ls(const char *dir) |
| 1019 | { |
| 1020 | return do_fat_read(dir, NULL, 0, LS_YES); |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | long |
| 1025 | file_fat_read(const char *filename, void *buffer, unsigned long maxsize) |
| 1026 | { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1027 | printf("reading %s\n",filename); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1028 | return do_fat_read(filename, buffer, maxsize, LS_NO); |
| 1029 | } |