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