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