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