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