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