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