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