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