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