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