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