Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * fat.c |
| 4 | * |
| 5 | * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg |
| 6 | * |
| 7 | * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6 |
| 8 | * 2003-03-10 - kharris@nexus-tech.net - ported to uboot |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 12 | #include <blk.h> |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 13 | #include <config.h> |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 14 | #include <exports.h> |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 15 | #include <fat.h> |
Rob Clark | 1f40366 | 2017-09-09 13:15:56 -0400 | [diff] [blame] | 16 | #include <fs.h> |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 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 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 24 | /* |
Rob Clark | 21a24c3 | 2017-09-09 13:15:59 -0400 | [diff] [blame] | 25 | * Convert a string to lowercase. Converts at most 'len' characters, |
| 26 | * 'len' may be larger than the length of 'str' if 'str' is NULL |
| 27 | * terminated. |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 28 | */ |
Rob Clark | 21a24c3 | 2017-09-09 13:15:59 -0400 | [diff] [blame] | 29 | static void downcase(char *str, size_t len) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 30 | { |
Rob Clark | 21a24c3 | 2017-09-09 13:15:59 -0400 | [diff] [blame] | 31 | while (*str != '\0' && len--) { |
Richard Genoud | fb7e16c | 2012-12-13 00:47:36 +0000 | [diff] [blame] | 32 | *str = tolower(*str); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 33 | str++; |
| 34 | } |
| 35 | } |
| 36 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 37 | static struct blk_desc *cur_dev; |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 38 | static disk_partition_t cur_part_info; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 39 | |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 40 | #define DOS_BOOT_MAGIC_OFFSET 0x1fe |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 41 | #define DOS_FS_TYPE_OFFSET 0x36 |
Wolfgang Denk | 66c2d73 | 2010-07-19 11:36:57 +0200 | [diff] [blame] | 42 | #define DOS_FS32_TYPE_OFFSET 0x52 |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 43 | |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 44 | static int disk_read(__u32 block, __u32 nr_blocks, void *buf) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 45 | { |
Łukasz Majewski | 0a04ed8 | 2015-09-03 14:21:39 +0200 | [diff] [blame] | 46 | ulong ret; |
| 47 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 48 | if (!cur_dev) |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 49 | return -1; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 50 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 51 | 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] | 52 | |
Tom Rini | 42a9f14 | 2017-08-14 21:02:08 -0400 | [diff] [blame] | 53 | if (ret != nr_blocks) |
Łukasz Majewski | 0a04ed8 | 2015-09-03 14:21:39 +0200 | [diff] [blame] | 54 | return -1; |
| 55 | |
| 56 | return ret; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 59 | 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] | 60 | { |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 61 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 62 | |
Stephen Warren | 5e8f983 | 2012-10-17 06:44:59 +0000 | [diff] [blame] | 63 | cur_dev = dev_desc; |
| 64 | cur_part_info = *info; |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 65 | |
| 66 | /* Make sure it has a valid FAT header */ |
| 67 | if (disk_read(0, 1, buffer) != 1) { |
| 68 | cur_dev = NULL; |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | /* Check if it's actually a DOS volume */ |
| 73 | if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) { |
| 74 | cur_dev = NULL; |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | /* Check for FAT12/FAT16/FAT32 filesystem */ |
| 79 | if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3)) |
| 80 | return 0; |
| 81 | if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5)) |
| 82 | return 0; |
| 83 | |
| 84 | cur_dev = NULL; |
| 85 | return -1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 88 | int fat_register_device(struct blk_desc *dev_desc, int part_no) |
Stephen Warren | 5e8f983 | 2012-10-17 06:44:59 +0000 | [diff] [blame] | 89 | { |
| 90 | disk_partition_t info; |
| 91 | |
| 92 | /* First close any currently found FAT filesystem */ |
| 93 | cur_dev = NULL; |
| 94 | |
| 95 | /* Read the partition table, if present */ |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 96 | if (part_get_info(dev_desc, part_no, &info)) { |
Stephen Warren | 5e8f983 | 2012-10-17 06:44:59 +0000 | [diff] [blame] | 97 | if (part_no != 0) { |
| 98 | printf("** Partition %d not valid on device %d **\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 99 | part_no, dev_desc->devnum); |
Stephen Warren | 5e8f983 | 2012-10-17 06:44:59 +0000 | [diff] [blame] | 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | info.start = 0; |
| 104 | info.size = dev_desc->lba; |
| 105 | info.blksz = dev_desc->blksz; |
| 106 | info.name[0] = 0; |
| 107 | info.type[0] = 0; |
| 108 | info.bootable = 0; |
Patrick Delaunay | b331cd6 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 109 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Stephen Warren | 5e8f983 | 2012-10-17 06:44:59 +0000 | [diff] [blame] | 110 | info.uuid[0] = 0; |
| 111 | #endif |
| 112 | } |
| 113 | |
| 114 | return fat_set_blk_dev(dev_desc, &info); |
| 115 | } |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 116 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 117 | /* |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 118 | * Extract zero terminated short name from a directory entry. |
| 119 | */ |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 120 | static void get_name(dir_entry *dirent, char *s_name) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 121 | { |
| 122 | char *ptr; |
| 123 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 124 | memcpy(s_name, dirent->name, 8); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 125 | s_name[8] = '\0'; |
| 126 | ptr = s_name; |
| 127 | while (*ptr && *ptr != ' ') |
| 128 | ptr++; |
Rob Clark | 21a24c3 | 2017-09-09 13:15:59 -0400 | [diff] [blame] | 129 | if (dirent->lcase & CASE_LOWER_BASE) |
| 130 | downcase(s_name, (unsigned)(ptr - s_name)); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 131 | if (dirent->ext[0] && dirent->ext[0] != ' ') { |
Rob Clark | 21a24c3 | 2017-09-09 13:15:59 -0400 | [diff] [blame] | 132 | *ptr++ = '.'; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 133 | memcpy(ptr, dirent->ext, 3); |
Rob Clark | 21a24c3 | 2017-09-09 13:15:59 -0400 | [diff] [blame] | 134 | if (dirent->lcase & CASE_LOWER_EXT) |
| 135 | downcase(ptr, 3); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 136 | ptr[3] = '\0'; |
| 137 | while (*ptr && *ptr != ' ') |
| 138 | ptr++; |
| 139 | } |
| 140 | *ptr = '\0'; |
| 141 | if (*s_name == DELETED_FLAG) |
| 142 | *s_name = '\0'; |
| 143 | else if (*s_name == aRING) |
Remy Bohmer | 3c2c2f4 | 2008-11-27 22:30:27 +0100 | [diff] [blame] | 144 | *s_name = DELETED_FLAG; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Stefan Brüns | b8948d2 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 147 | static int flush_dirty_fat_buffer(fsdata *mydata); |
| 148 | #if !defined(CONFIG_FAT_WRITE) |
| 149 | /* Stub for read only operation */ |
| 150 | int flush_dirty_fat_buffer(fsdata *mydata) |
| 151 | { |
| 152 | (void)(mydata); |
| 153 | return 0; |
| 154 | } |
| 155 | #endif |
| 156 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 157 | /* |
| 158 | * Get the entry at index 'entry' in a FAT (12/16/32) table. |
| 159 | * On failure 0x00 is returned. |
| 160 | */ |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 161 | static __u32 get_fatent(fsdata *mydata, __u32 entry) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 162 | { |
| 163 | __u32 bufnum; |
Stefan Brüns | b352cae | 2017-01-26 20:22:36 +0000 | [diff] [blame] | 164 | __u32 offset, off8; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 165 | __u32 ret = 0x00; |
| 166 | |
Stefan Brüns | b8948d2 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 167 | if (CHECK_CLUST(entry, mydata->fatsize)) { |
| 168 | printf("Error: Invalid FAT entry: 0x%08x\n", entry); |
| 169 | return ret; |
| 170 | } |
| 171 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 172 | switch (mydata->fatsize) { |
| 173 | case 32: |
| 174 | bufnum = entry / FAT32BUFSIZE; |
| 175 | offset = entry - bufnum * FAT32BUFSIZE; |
| 176 | break; |
| 177 | case 16: |
| 178 | bufnum = entry / FAT16BUFSIZE; |
| 179 | offset = entry - bufnum * FAT16BUFSIZE; |
| 180 | break; |
| 181 | case 12: |
| 182 | bufnum = entry / FAT12BUFSIZE; |
| 183 | offset = entry - bufnum * FAT12BUFSIZE; |
| 184 | break; |
| 185 | |
| 186 | default: |
| 187 | /* Unsupported FAT size */ |
| 188 | return ret; |
| 189 | } |
| 190 | |
Stefan Brüns | b8948d2 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 191 | debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n", |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 192 | mydata->fatsize, entry, entry, offset, offset); |
Wolfgang Denk | 2aa98c6 | 2010-07-19 11:36:58 +0200 | [diff] [blame] | 193 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 194 | /* Read a new block of FAT entries into the cache. */ |
| 195 | if (bufnum != mydata->fatbufnum) { |
Sergei Shtylyov | 60b36f0 | 2011-08-08 09:39:29 +0000 | [diff] [blame] | 196 | __u32 getsize = FATBUFBLOCKS; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 197 | __u8 *bufptr = mydata->fatbuf; |
| 198 | __u32 fatlength = mydata->fatlength; |
| 199 | __u32 startblock = bufnum * FATBUFBLOCKS; |
| 200 | |
Stefan Brüns | 6c1a808 | 2016-12-17 00:27:50 +0100 | [diff] [blame] | 201 | /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */ |
Benoît Thébaudeau | 8006dd2 | 2012-07-20 15:19:29 +0200 | [diff] [blame] | 202 | if (startblock + getsize > fatlength) |
| 203 | getsize = fatlength - startblock; |
Sergei Shtylyov | 60b36f0 | 2011-08-08 09:39:29 +0000 | [diff] [blame] | 204 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 205 | startblock += mydata->fat_sect; /* Offset from start of disk */ |
| 206 | |
Stefan Brüns | b8948d2 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 207 | /* Write back the fatbuf to the disk */ |
| 208 | if (flush_dirty_fat_buffer(mydata) < 0) |
| 209 | return -1; |
| 210 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 211 | if (disk_read(startblock, getsize, bufptr) < 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 212 | debug("Error reading FAT blocks\n"); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | mydata->fatbufnum = bufnum; |
| 216 | } |
| 217 | |
| 218 | /* Get the actual entry from the table */ |
| 219 | switch (mydata->fatsize) { |
| 220 | case 32: |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 221 | ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 222 | break; |
| 223 | case 16: |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 224 | ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 225 | break; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 226 | case 12: |
Stefan Brüns | b352cae | 2017-01-26 20:22:36 +0000 | [diff] [blame] | 227 | off8 = (offset * 3) / 2; |
| 228 | /* fatbut + off8 may be unaligned, read in byte granularity */ |
| 229 | ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 230 | |
Stefan Brüns | 8d48c92 | 2016-12-17 03:55:10 +0100 | [diff] [blame] | 231 | if (offset & 0x1) |
| 232 | ret >>= 4; |
| 233 | ret &= 0xfff; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 234 | } |
Stefan Brüns | b8948d2 | 2016-12-17 00:27:51 +0100 | [diff] [blame] | 235 | debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n", |
| 236 | mydata->fatsize, ret, entry, offset); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 237 | |
| 238 | return ret; |
| 239 | } |
| 240 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 241 | /* |
| 242 | * Read at most 'size' bytes from the specified cluster into 'buffer'. |
| 243 | * Return 0 on success, -1 otherwise. |
| 244 | */ |
| 245 | static int |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 246 | get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 247 | { |
Erik Hansen | 3f270f4 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 248 | __u32 idx = 0; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 249 | __u32 startsect; |
Kyle Moffett | 46236b1 | 2011-12-20 07:41:13 +0000 | [diff] [blame] | 250 | int ret; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 251 | |
| 252 | if (clustnum > 0) { |
Rob Clark | 265edc0 | 2017-09-09 13:16:00 -0400 | [diff] [blame] | 253 | startsect = clust_to_sect(mydata, clustnum); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 254 | } else { |
| 255 | startsect = mydata->rootdir_sect; |
| 256 | } |
| 257 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 258 | debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect); |
| 259 | |
Benoît Thébaudeau | cc63b25 | 2012-07-20 15:21:08 +0200 | [diff] [blame] | 260 | if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) { |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 261 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 262 | |
Heinrich Schuchardt | 1c381ce | 2018-09-13 19:42:47 +0200 | [diff] [blame] | 263 | debug("FAT: Misaligned buffer address (%p)\n", buffer); |
Benoît Thébaudeau | cc63b25 | 2012-07-20 15:21:08 +0200 | [diff] [blame] | 264 | |
| 265 | while (size >= mydata->sect_size) { |
| 266 | ret = disk_read(startsect++, 1, tmpbuf); |
| 267 | if (ret != 1) { |
| 268 | debug("Error reading data (got %d)\n", ret); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | memcpy(buffer, tmpbuf, mydata->sect_size); |
| 273 | buffer += mydata->sect_size; |
| 274 | size -= mydata->sect_size; |
| 275 | } |
| 276 | } else { |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 277 | idx = size / mydata->sect_size; |
Benoît Thébaudeau | cc63b25 | 2012-07-20 15:21:08 +0200 | [diff] [blame] | 278 | ret = disk_read(startsect, idx, buffer); |
| 279 | if (ret != idx) { |
| 280 | debug("Error reading data (got %d)\n", ret); |
| 281 | return -1; |
| 282 | } |
| 283 | startsect += idx; |
| 284 | idx *= mydata->sect_size; |
| 285 | buffer += idx; |
| 286 | size -= idx; |
| 287 | } |
| 288 | if (size) { |
| 289 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
| 290 | |
| 291 | ret = disk_read(startsect, 1, tmpbuf); |
Kyle Moffett | 46236b1 | 2011-12-20 07:41:13 +0000 | [diff] [blame] | 292 | if (ret != 1) { |
| 293 | debug("Error reading data (got %d)\n", ret); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 294 | return -1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 295 | } |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 296 | |
Benoît Thébaudeau | cc63b25 | 2012-07-20 15:21:08 +0200 | [diff] [blame] | 297 | memcpy(buffer, tmpbuf, size); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 303 | /* |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 304 | * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr' |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 305 | * into 'buffer'. |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 306 | * 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] | 307 | */ |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 308 | __u8 get_contents_vfatname_block[MAX_CLUSTSIZE] |
| 309 | __aligned(ARCH_DMA_MINALIGN); |
| 310 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 311 | static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, |
| 312 | __u8 *buffer, loff_t maxsize, loff_t *gotsize) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 313 | { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 314 | loff_t filesize = FAT2CPU32(dentptr->size); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 315 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 316 | __u32 curclust = START(dentptr); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 317 | __u32 endclust, newclust; |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 318 | loff_t actsize; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 319 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 320 | *gotsize = 0; |
| 321 | debug("Filesize: %llu bytes\n", filesize); |
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 | if (pos >= filesize) { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 324 | debug("Read position past EOF: %llu\n", pos); |
| 325 | return 0; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | if (maxsize > 0 && filesize > pos + maxsize) |
| 329 | filesize = pos + maxsize; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 330 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 331 | debug("%llu bytes\n", filesize); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 332 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 333 | actsize = bytesperclust; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 334 | |
| 335 | /* go to cluster at pos */ |
| 336 | while (actsize <= pos) { |
| 337 | curclust = get_fatent(mydata, curclust); |
| 338 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
| 339 | debug("curclust: 0x%x\n", curclust); |
| 340 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 341 | return 0; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 342 | } |
| 343 | actsize += bytesperclust; |
| 344 | } |
| 345 | |
| 346 | /* actsize > pos */ |
| 347 | actsize -= bytesperclust; |
| 348 | filesize -= actsize; |
| 349 | pos -= actsize; |
| 350 | |
| 351 | /* align to beginning of next cluster if any */ |
| 352 | if (pos) { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 353 | actsize = min(filesize, (loff_t)bytesperclust); |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 354 | if (get_cluster(mydata, curclust, get_contents_vfatname_block, |
| 355 | (int)actsize) != 0) { |
| 356 | printf("Error reading cluster\n"); |
| 357 | return -1; |
| 358 | } |
| 359 | filesize -= actsize; |
| 360 | actsize -= pos; |
| 361 | memcpy(buffer, get_contents_vfatname_block + pos, actsize); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 362 | *gotsize += actsize; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 363 | if (!filesize) |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 364 | return 0; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 365 | buffer += actsize; |
| 366 | |
| 367 | curclust = get_fatent(mydata, curclust); |
| 368 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
| 369 | debug("curclust: 0x%x\n", curclust); |
| 370 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 371 | return 0; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | actsize = bytesperclust; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 376 | endclust = curclust; |
| 377 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 378 | do { |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 379 | /* search for consecutive clusters */ |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 380 | while (actsize < filesize) { |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 381 | newclust = get_fatent(mydata, endclust); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 382 | if ((newclust - 1) != endclust) |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 383 | goto getit; |
michael | 8ce4e5c | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 384 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 385 | debug("curclust: 0x%x\n", newclust); |
| 386 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 387 | return 0; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 388 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 389 | endclust = newclust; |
| 390 | actsize += bytesperclust; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 391 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 392 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 393 | /* get remaining bytes */ |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 394 | actsize = filesize; |
Benoît Thébaudeau | 0880e5b | 2012-07-20 15:21:37 +0200 | [diff] [blame] | 395 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 396 | printf("Error reading cluster\n"); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 397 | return -1; |
| 398 | } |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 399 | *gotsize += actsize; |
| 400 | return 0; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 401 | getit: |
| 402 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 403 | printf("Error reading cluster\n"); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 404 | return -1; |
| 405 | } |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 406 | *gotsize += (int)actsize; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 407 | filesize -= actsize; |
| 408 | buffer += actsize; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 409 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 410 | curclust = get_fatent(mydata, endclust); |
michael | 8ce4e5c | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 411 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 412 | debug("curclust: 0x%x\n", curclust); |
| 413 | printf("Invalid FAT entry\n"); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 414 | return 0; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 415 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 416 | actsize = bytesperclust; |
| 417 | endclust = curclust; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 418 | } while (1); |
| 419 | } |
| 420 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 421 | /* |
| 422 | * Extract the file name information from 'slotptr' into 'l_name', |
| 423 | * starting at l_name[*idx]. |
| 424 | * Return 1 if terminator (zero byte) is found, 0 otherwise. |
| 425 | */ |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 426 | static int slot2str(dir_slot *slotptr, char *l_name, int *idx) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 427 | { |
| 428 | int j; |
| 429 | |
| 430 | for (j = 0; j <= 8; j += 2) { |
| 431 | l_name[*idx] = slotptr->name0_4[j]; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 432 | if (l_name[*idx] == 0x00) |
| 433 | return 1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 434 | (*idx)++; |
| 435 | } |
| 436 | for (j = 0; j <= 10; j += 2) { |
| 437 | l_name[*idx] = slotptr->name5_10[j]; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 438 | if (l_name[*idx] == 0x00) |
| 439 | return 1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 440 | (*idx)++; |
| 441 | } |
| 442 | for (j = 0; j <= 2; j += 2) { |
| 443 | l_name[*idx] = slotptr->name11_12[j]; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 444 | if (l_name[*idx] == 0x00) |
| 445 | return 1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 446 | (*idx)++; |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 452 | /* Calculate short name checksum */ |
Marek Vasut | ff04f6d | 2012-10-09 07:20:22 +0000 | [diff] [blame] | 453 | static __u8 mkcksum(const char name[8], const char ext[3]) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 454 | { |
| 455 | int i; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 456 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 457 | __u8 ret = 0; |
| 458 | |
Marek Vasut | 6ad77d8 | 2013-01-11 03:35:48 +0000 | [diff] [blame] | 459 | for (i = 0; i < 8; i++) |
Marek Vasut | ff04f6d | 2012-10-09 07:20:22 +0000 | [diff] [blame] | 460 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i]; |
Marek Vasut | 6ad77d8 | 2013-01-11 03:35:48 +0000 | [diff] [blame] | 461 | for (i = 0; i < 3; i++) |
Marek Vasut | ff04f6d | 2012-10-09 07:20:22 +0000 | [diff] [blame] | 462 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i]; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 463 | |
| 464 | return ret; |
| 465 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 466 | |
| 467 | /* |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 468 | * Read boot sector and volume info from a FAT filesystem |
| 469 | */ |
| 470 | static int |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 471 | read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 472 | { |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 473 | __u8 *block; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 474 | volume_info *vistart; |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 475 | int ret = 0; |
| 476 | |
| 477 | if (cur_dev == NULL) { |
| 478 | debug("Error: no device selected\n"); |
| 479 | return -1; |
| 480 | } |
| 481 | |
Tuomas Tynkkynen | 09fa964 | 2017-10-01 02:25:21 +0300 | [diff] [blame] | 482 | block = malloc_cache_aligned(cur_dev->blksz); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 483 | if (block == NULL) { |
| 484 | debug("Error: allocating block\n"); |
| 485 | return -1; |
| 486 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 487 | |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 488 | if (disk_read(0, 1, block) < 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 489 | debug("Error: reading block\n"); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 490 | goto fail; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | memcpy(bs, block, sizeof(boot_sector)); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 494 | bs->reserved = FAT2CPU16(bs->reserved); |
| 495 | bs->fat_length = FAT2CPU16(bs->fat_length); |
| 496 | bs->secs_track = FAT2CPU16(bs->secs_track); |
| 497 | bs->heads = FAT2CPU16(bs->heads); |
| 498 | bs->total_sect = FAT2CPU32(bs->total_sect); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 499 | |
| 500 | /* FAT32 entries */ |
| 501 | if (bs->fat_length == 0) { |
| 502 | /* Assume FAT32 */ |
| 503 | bs->fat32_length = FAT2CPU32(bs->fat32_length); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 504 | bs->flags = FAT2CPU16(bs->flags); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 505 | bs->root_cluster = FAT2CPU32(bs->root_cluster); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 506 | bs->info_sector = FAT2CPU16(bs->info_sector); |
| 507 | bs->backup_boot = FAT2CPU16(bs->backup_boot); |
| 508 | vistart = (volume_info *)(block + sizeof(boot_sector)); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 509 | *fatsize = 32; |
| 510 | } else { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 511 | vistart = (volume_info *)&(bs->fat32_length); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 512 | *fatsize = 0; |
| 513 | } |
| 514 | memcpy(volinfo, vistart, sizeof(volume_info)); |
| 515 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 516 | if (*fatsize == 32) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 517 | if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 518 | goto exit; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 519 | } else { |
Tom Rix | 651351f | 2009-05-20 07:55:41 -0500 | [diff] [blame] | 520 | if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 521 | *fatsize = 12; |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 522 | goto exit; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 523 | } |
Tom Rix | 651351f | 2009-05-20 07:55:41 -0500 | [diff] [blame] | 524 | if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 525 | *fatsize = 16; |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 526 | goto exit; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 530 | debug("Error: broken fs_type sign\n"); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 531 | fail: |
| 532 | ret = -1; |
| 533 | exit: |
| 534 | free(block); |
| 535 | return ret; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Rob Clark | 4544998 | 2017-09-09 13:15:52 -0400 | [diff] [blame] | 538 | static int get_fs_info(fsdata *mydata) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 539 | { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 540 | boot_sector bs; |
| 541 | volume_info volinfo; |
Rob Clark | 4544998 | 2017-09-09 13:15:52 -0400 | [diff] [blame] | 542 | int ret; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 543 | |
Rob Clark | 4544998 | 2017-09-09 13:15:52 -0400 | [diff] [blame] | 544 | ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize); |
| 545 | if (ret) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 546 | debug("Error: reading boot sector\n"); |
Rob Clark | 4544998 | 2017-09-09 13:15:52 -0400 | [diff] [blame] | 547 | return ret; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Sergei Shtylyov | 40e2191 | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 550 | if (mydata->fatsize == 32) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 551 | mydata->fatlength = bs.fat32_length; |
AKASHI Takahiro | f23101f | 2018-09-11 15:58:58 +0900 | [diff] [blame] | 552 | mydata->total_sect = bs.total_sect; |
Sergei Shtylyov | 40e2191 | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 553 | } else { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 554 | mydata->fatlength = bs.fat_length; |
AKASHI Takahiro | f23101f | 2018-09-11 15:58:58 +0900 | [diff] [blame] | 555 | mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0]; |
| 556 | if (!mydata->total_sect) |
| 557 | mydata->total_sect = bs.total_sect; |
Sergei Shtylyov | 40e2191 | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 558 | } |
AKASHI Takahiro | f23101f | 2018-09-11 15:58:58 +0900 | [diff] [blame] | 559 | if (!mydata->total_sect) /* unlikely */ |
| 560 | mydata->total_sect = (u32)cur_part_info.size; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 561 | |
AKASHI Takahiro | f23101f | 2018-09-11 15:58:58 +0900 | [diff] [blame] | 562 | mydata->fats = bs.fats; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 563 | mydata->fat_sect = bs.reserved; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 564 | |
Rob Clark | 4544998 | 2017-09-09 13:15:52 -0400 | [diff] [blame] | 565 | mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 566 | |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 567 | mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 568 | mydata->clust_size = bs.cluster_size; |
Kyle Moffett | 46236b1 | 2011-12-20 07:41:13 +0000 | [diff] [blame] | 569 | if (mydata->sect_size != cur_part_info.blksz) { |
| 570 | printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n", |
| 571 | mydata->sect_size, cur_part_info.blksz); |
| 572 | return -1; |
| 573 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 574 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 575 | if (mydata->fatsize == 32) { |
| 576 | mydata->data_begin = mydata->rootdir_sect - |
| 577 | (mydata->clust_size * 2); |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 578 | mydata->root_cluster = bs.root_cluster; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 579 | } else { |
Rob Clark | 4544998 | 2017-09-09 13:15:52 -0400 | [diff] [blame] | 580 | mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 + |
| 581 | bs.dir_entries[0]) * |
| 582 | sizeof(dir_entry)) / |
| 583 | mydata->sect_size; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 584 | mydata->data_begin = mydata->rootdir_sect + |
Rob Clark | 4544998 | 2017-09-09 13:15:52 -0400 | [diff] [blame] | 585 | mydata->rootdir_size - |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 586 | (mydata->clust_size * 2); |
Rob Clark | 265edc0 | 2017-09-09 13:16:00 -0400 | [diff] [blame] | 587 | mydata->root_cluster = |
| 588 | sect_to_clust(mydata, mydata->rootdir_sect); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 589 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 590 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 591 | mydata->fatbufnum = -1; |
Stefan Brüns | 3c0ed9c | 2016-09-11 22:51:40 +0200 | [diff] [blame] | 592 | mydata->fat_dirty = 0; |
Tuomas Tynkkynen | 09fa964 | 2017-10-01 02:25:21 +0300 | [diff] [blame] | 593 | mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 594 | if (mydata->fatbuf == NULL) { |
| 595 | debug("Error: allocating memory\n"); |
| 596 | return -1; |
| 597 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 598 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 599 | debug("FAT%d, fat_sect: %d, fatlength: %d\n", |
| 600 | mydata->fatsize, mydata->fat_sect, mydata->fatlength); |
| 601 | debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n" |
| 602 | "Data begins at: %d\n", |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 603 | mydata->root_cluster, |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 604 | mydata->rootdir_sect, |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 605 | mydata->rootdir_sect * mydata->sect_size, mydata->data_begin); |
| 606 | debug("Sector size: %d, cluster size: %d\n", mydata->sect_size, |
| 607 | mydata->clust_size); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 608 | |
Rob Clark | 4544998 | 2017-09-09 13:15:52 -0400 | [diff] [blame] | 609 | return 0; |
| 610 | } |
| 611 | |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * Directory iterator, to simplify filesystem traversal |
| 615 | * |
| 616 | * Implements an iterator pattern to traverse directory tables, |
| 617 | * transparently handling directory tables split across multiple |
| 618 | * clusters, and the difference between FAT12/FAT16 root directory |
| 619 | * (contiguous) and subdirectories + FAT32 root (chained). |
| 620 | * |
| 621 | * Rough usage: |
| 622 | * |
| 623 | * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) { |
| 624 | * // to traverse down to a subdirectory pointed to by |
| 625 | * // current iterator position: |
| 626 | * fat_itr_child(&itr, &itr); |
| 627 | * } |
| 628 | * |
| 629 | * For more complete example, see fat_itr_resolve() |
| 630 | */ |
| 631 | |
| 632 | typedef struct { |
| 633 | fsdata *fsdata; /* filesystem parameters */ |
AKASHI Takahiro | 3a10e07 | 2018-09-11 15:59:09 +0900 | [diff] [blame] | 634 | unsigned start_clust; /* first cluster */ |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 635 | unsigned clust; /* current cluster */ |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 636 | unsigned next_clust; /* next cluster if remaining == 0 */ |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 637 | int last_cluster; /* set once we've read last cluster */ |
| 638 | int is_root; /* is iterator at root directory */ |
| 639 | int remaining; /* remaining dent's in current cluster */ |
| 640 | |
| 641 | /* current iterator position values: */ |
| 642 | dir_entry *dent; /* current directory entry */ |
| 643 | char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */ |
| 644 | char s_name[14]; /* short 8.3 name */ |
| 645 | char *name; /* l_name if there is one, else s_name */ |
| 646 | |
| 647 | /* storage for current cluster in memory: */ |
| 648 | u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN); |
| 649 | } fat_itr; |
| 650 | |
| 651 | static int fat_itr_isdir(fat_itr *itr); |
| 652 | |
| 653 | /** |
| 654 | * fat_itr_root() - initialize an iterator to start at the root |
| 655 | * directory |
| 656 | * |
| 657 | * @itr: iterator to initialize |
| 658 | * @fsdata: filesystem data for the partition |
| 659 | * @return 0 on success, else -errno |
| 660 | */ |
| 661 | static int fat_itr_root(fat_itr *itr, fsdata *fsdata) |
| 662 | { |
| 663 | if (get_fs_info(fsdata)) |
| 664 | return -ENXIO; |
| 665 | |
| 666 | itr->fsdata = fsdata; |
AKASHI Takahiro | 3a10e07 | 2018-09-11 15:59:09 +0900 | [diff] [blame] | 667 | itr->start_clust = 0; |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 668 | itr->clust = fsdata->root_cluster; |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 669 | itr->next_clust = fsdata->root_cluster; |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 670 | itr->dent = NULL; |
| 671 | itr->remaining = 0; |
| 672 | itr->last_cluster = 0; |
| 673 | itr->is_root = 1; |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * fat_itr_child() - initialize an iterator to descend into a sub- |
| 680 | * directory |
| 681 | * |
| 682 | * Initializes 'itr' to iterate the contents of the directory at |
| 683 | * the current cursor position of 'parent'. It is an error to |
| 684 | * call this if the current cursor of 'parent' is pointing at a |
| 685 | * regular file. |
| 686 | * |
| 687 | * Note that 'itr' and 'parent' can be the same pointer if you do |
| 688 | * not need to preserve 'parent' after this call, which is useful |
| 689 | * for traversing directory structure to resolve a file/directory. |
| 690 | * |
| 691 | * @itr: iterator to initialize |
| 692 | * @parent: the iterator pointing at a directory entry in the |
| 693 | * parent directory of the directory to iterate |
| 694 | */ |
| 695 | static void fat_itr_child(fat_itr *itr, fat_itr *parent) |
| 696 | { |
| 697 | fsdata *mydata = parent->fsdata; /* for silly macros */ |
| 698 | unsigned clustnum = START(parent->dent); |
| 699 | |
| 700 | assert(fat_itr_isdir(parent)); |
| 701 | |
| 702 | itr->fsdata = parent->fsdata; |
AKASHI Takahiro | 3a10e07 | 2018-09-11 15:59:09 +0900 | [diff] [blame] | 703 | itr->start_clust = clustnum; |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 704 | if (clustnum > 0) { |
| 705 | itr->clust = clustnum; |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 706 | itr->next_clust = clustnum; |
Tuomas Tynkkynen | 8df8731 | 2017-09-25 22:06:33 +0300 | [diff] [blame] | 707 | itr->is_root = 0; |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 708 | } else { |
| 709 | itr->clust = parent->fsdata->root_cluster; |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 710 | itr->next_clust = parent->fsdata->root_cluster; |
Tuomas Tynkkynen | 8df8731 | 2017-09-25 22:06:33 +0300 | [diff] [blame] | 711 | itr->is_root = 1; |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 712 | } |
| 713 | itr->dent = NULL; |
| 714 | itr->remaining = 0; |
| 715 | itr->last_cluster = 0; |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static void *next_cluster(fat_itr *itr) |
| 719 | { |
| 720 | fsdata *mydata = itr->fsdata; /* for silly macros */ |
| 721 | int ret; |
| 722 | u32 sect; |
| 723 | |
| 724 | /* have we reached the end? */ |
| 725 | if (itr->last_cluster) |
| 726 | return NULL; |
| 727 | |
Thomas RIENOESSL | a68b0e1 | 2018-11-13 14:00:59 +0100 | [diff] [blame] | 728 | if (itr->fsdata->fatsize != 32 && itr->is_root) |
| 729 | sect = mydata->rootdir_sect; |
| 730 | else |
| 731 | sect = clust_to_sect(itr->fsdata, itr->next_clust); |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 732 | |
| 733 | debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n", |
| 734 | sect, itr->fsdata->clust_size, DIRENTSPERBLOCK); |
| 735 | |
| 736 | /* |
| 737 | * NOTE: do_fat_read_at() had complicated logic to deal w/ |
| 738 | * vfat names that span multiple clusters in the fat16 case, |
| 739 | * which get_dentfromdir() probably also needed (and was |
| 740 | * missing). And not entirely sure what fat32 didn't have |
| 741 | * the same issue.. We solve that by only caring about one |
| 742 | * dent at a time and iteratively constructing the vfat long |
| 743 | * name. |
| 744 | */ |
| 745 | ret = disk_read(sect, itr->fsdata->clust_size, |
| 746 | itr->block); |
| 747 | if (ret < 0) { |
| 748 | debug("Error: reading block\n"); |
| 749 | return NULL; |
| 750 | } |
| 751 | |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 752 | itr->clust = itr->next_clust; |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 753 | if (itr->is_root && itr->fsdata->fatsize != 32) { |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 754 | itr->next_clust++; |
| 755 | sect = clust_to_sect(itr->fsdata, itr->next_clust); |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 756 | if (sect - itr->fsdata->rootdir_sect >= |
| 757 | itr->fsdata->rootdir_size) { |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 758 | debug("nextclust: 0x%x\n", itr->next_clust); |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 759 | itr->last_cluster = 1; |
| 760 | } |
| 761 | } else { |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 762 | itr->next_clust = get_fatent(itr->fsdata, itr->next_clust); |
| 763 | if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) { |
| 764 | debug("nextclust: 0x%x\n", itr->next_clust); |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 765 | itr->last_cluster = 1; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | return itr->block; |
| 770 | } |
| 771 | |
| 772 | static dir_entry *next_dent(fat_itr *itr) |
| 773 | { |
| 774 | if (itr->remaining == 0) { |
| 775 | struct dir_entry *dent = next_cluster(itr); |
| 776 | unsigned nbytes = itr->fsdata->sect_size * |
| 777 | itr->fsdata->clust_size; |
| 778 | |
| 779 | /* have we reached the last cluster? */ |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 780 | if (!dent) { |
| 781 | /* a sign for no more entries left */ |
| 782 | itr->dent = NULL; |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 783 | return NULL; |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 784 | } |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 785 | |
| 786 | itr->remaining = nbytes / sizeof(dir_entry) - 1; |
| 787 | itr->dent = dent; |
| 788 | } else { |
| 789 | itr->remaining--; |
| 790 | itr->dent++; |
| 791 | } |
| 792 | |
| 793 | /* have we reached the last valid entry? */ |
| 794 | if (itr->dent->name[0] == 0) |
| 795 | return NULL; |
| 796 | |
| 797 | return itr->dent; |
| 798 | } |
| 799 | |
| 800 | static dir_entry *extract_vfat_name(fat_itr *itr) |
| 801 | { |
| 802 | struct dir_entry *dent = itr->dent; |
| 803 | int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK; |
| 804 | u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum; |
| 805 | int n = 0; |
| 806 | |
| 807 | while (seqn--) { |
| 808 | char buf[13]; |
| 809 | int idx = 0; |
| 810 | |
| 811 | slot2str((dir_slot *)dent, buf, &idx); |
| 812 | |
| 813 | /* shift accumulated long-name up and copy new part in: */ |
| 814 | memmove(itr->l_name + idx, itr->l_name, n); |
| 815 | memcpy(itr->l_name, buf, idx); |
| 816 | n += idx; |
| 817 | |
| 818 | dent = next_dent(itr); |
| 819 | if (!dent) |
| 820 | return NULL; |
| 821 | } |
| 822 | |
| 823 | itr->l_name[n] = '\0'; |
| 824 | |
| 825 | chksum = mkcksum(dent->name, dent->ext); |
| 826 | |
| 827 | /* checksum mismatch could mean deleted file, etc.. skip it: */ |
| 828 | if (chksum != alias_checksum) { |
| 829 | debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n", |
| 830 | chksum, alias_checksum, itr->l_name, dent->name, dent->ext); |
| 831 | return NULL; |
| 832 | } |
| 833 | |
| 834 | return dent; |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * fat_itr_next() - step to the next entry in a directory |
| 839 | * |
| 840 | * Must be called once on a new iterator before the cursor is valid. |
| 841 | * |
| 842 | * @itr: the iterator to iterate |
| 843 | * @return boolean, 1 if success or 0 if no more entries in the |
| 844 | * current directory |
| 845 | */ |
| 846 | static int fat_itr_next(fat_itr *itr) |
| 847 | { |
| 848 | dir_entry *dent; |
| 849 | |
| 850 | itr->name = NULL; |
| 851 | |
| 852 | while (1) { |
| 853 | dent = next_dent(itr); |
| 854 | if (!dent) |
| 855 | return 0; |
| 856 | |
| 857 | if (dent->name[0] == DELETED_FLAG || |
| 858 | dent->name[0] == aRING) |
| 859 | continue; |
| 860 | |
| 861 | if (dent->attr & ATTR_VOLUME) { |
Tuomas Tynkkynen | 8bad6cb | 2018-01-05 02:45:21 +0200 | [diff] [blame] | 862 | if ((dent->attr & ATTR_VFAT) == ATTR_VFAT && |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 863 | (dent->name[0] & LAST_LONG_ENTRY_MASK)) { |
| 864 | dent = extract_vfat_name(itr); |
| 865 | if (!dent) |
| 866 | continue; |
| 867 | itr->name = itr->l_name; |
| 868 | break; |
| 869 | } else { |
| 870 | /* Volume label or VFAT entry, skip */ |
| 871 | continue; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | break; |
| 876 | } |
| 877 | |
| 878 | get_name(dent, itr->s_name); |
| 879 | if (!itr->name) |
| 880 | itr->name = itr->s_name; |
| 881 | |
| 882 | return 1; |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * fat_itr_isdir() - is current cursor position pointing to a directory |
| 887 | * |
| 888 | * @itr: the iterator |
| 889 | * @return true if cursor is at a directory |
| 890 | */ |
| 891 | static int fat_itr_isdir(fat_itr *itr) |
| 892 | { |
| 893 | return !!(itr->dent->attr & ATTR_DIR); |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Helpers: |
| 898 | */ |
| 899 | |
| 900 | #define TYPE_FILE 0x1 |
| 901 | #define TYPE_DIR 0x2 |
| 902 | #define TYPE_ANY (TYPE_FILE | TYPE_DIR) |
| 903 | |
| 904 | /** |
| 905 | * fat_itr_resolve() - traverse directory structure to resolve the |
| 906 | * requested path. |
| 907 | * |
| 908 | * Traverse directory structure to the requested path. If the specified |
| 909 | * path is to a directory, this will descend into the directory and |
| 910 | * leave it iterator at the start of the directory. If the path is to a |
| 911 | * file, it will leave the iterator in the parent directory with current |
| 912 | * cursor at file's entry in the directory. |
| 913 | * |
| 914 | * @itr: iterator initialized to root |
| 915 | * @path: the requested path |
| 916 | * @type: bitmask of allowable file types |
| 917 | * @return 0 on success or -errno |
| 918 | */ |
| 919 | static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type) |
| 920 | { |
| 921 | const char *next; |
| 922 | |
| 923 | /* chomp any extra leading slashes: */ |
| 924 | while (path[0] && ISDIRDELIM(path[0])) |
| 925 | path++; |
| 926 | |
| 927 | /* are we at the end? */ |
| 928 | if (strlen(path) == 0) { |
| 929 | if (!(type & TYPE_DIR)) |
| 930 | return -ENOENT; |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | /* find length of next path entry: */ |
| 935 | next = path; |
| 936 | while (next[0] && !ISDIRDELIM(next[0])) |
| 937 | next++; |
| 938 | |
AKASHI Takahiro | b94b6be | 2018-09-11 15:58:59 +0900 | [diff] [blame] | 939 | if (itr->is_root) { |
| 940 | /* root dir doesn't have "." nor ".." */ |
| 941 | if ((((next - path) == 1) && !strncmp(path, ".", 1)) || |
| 942 | (((next - path) == 2) && !strncmp(path, "..", 2))) { |
| 943 | /* point back to itself */ |
| 944 | itr->clust = itr->fsdata->root_cluster; |
AKASHI Takahiro | f528c14 | 2018-09-11 15:59:00 +0900 | [diff] [blame] | 945 | itr->next_clust = itr->fsdata->root_cluster; |
AKASHI Takahiro | b94b6be | 2018-09-11 15:58:59 +0900 | [diff] [blame] | 946 | itr->dent = NULL; |
| 947 | itr->remaining = 0; |
| 948 | itr->last_cluster = 0; |
| 949 | |
| 950 | if (next[0] == 0) { |
| 951 | if (type & TYPE_DIR) |
| 952 | return 0; |
| 953 | else |
| 954 | return -ENOENT; |
| 955 | } |
| 956 | |
| 957 | return fat_itr_resolve(itr, next, type); |
| 958 | } |
| 959 | } |
| 960 | |
Rob Clark | c6e3baa | 2017-09-09 13:15:53 -0400 | [diff] [blame] | 961 | while (fat_itr_next(itr)) { |
| 962 | int match = 0; |
| 963 | unsigned n = max(strlen(itr->name), (size_t)(next - path)); |
| 964 | |
| 965 | /* check both long and short name: */ |
| 966 | if (!strncasecmp(path, itr->name, n)) |
| 967 | match = 1; |
| 968 | else if (itr->name != itr->s_name && |
| 969 | !strncasecmp(path, itr->s_name, n)) |
| 970 | match = 1; |
| 971 | |
| 972 | if (!match) |
| 973 | continue; |
| 974 | |
| 975 | if (fat_itr_isdir(itr)) { |
| 976 | /* recurse into directory: */ |
| 977 | fat_itr_child(itr, itr); |
| 978 | return fat_itr_resolve(itr, next, type); |
| 979 | } else if (next[0]) { |
| 980 | /* |
| 981 | * If next is not empty then we have a case |
| 982 | * like: /path/to/realfile/nonsense |
| 983 | */ |
| 984 | debug("bad trailing path: %s\n", next); |
| 985 | return -ENOENT; |
| 986 | } else if (!(type & TYPE_FILE)) { |
| 987 | return -ENOTDIR; |
| 988 | } else { |
| 989 | return 0; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | return -ENOENT; |
| 994 | } |
| 995 | |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 996 | int file_fat_detectfs(void) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 997 | { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 998 | boot_sector bs; |
| 999 | volume_info volinfo; |
| 1000 | int fatsize; |
| 1001 | char vol_label[12]; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1002 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1003 | if (cur_dev == NULL) { |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1004 | printf("No current device\n"); |
| 1005 | return 1; |
| 1006 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1007 | |
Simon Glass | fc843a0 | 2017-05-17 03:25:30 -0600 | [diff] [blame] | 1008 | #if defined(CONFIG_IDE) || \ |
Simon Glass | 10e40d5 | 2017-06-14 21:28:25 -0600 | [diff] [blame] | 1009 | defined(CONFIG_SATA) || \ |
Simon Glass | c649e3c | 2016-05-01 11:36:02 -0600 | [diff] [blame] | 1010 | defined(CONFIG_SCSI) || \ |
Jon Loeliger | dd60d12 | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 1011 | defined(CONFIG_CMD_USB) || \ |
Andy Fleming | 21f6f96 | 2008-01-16 13:06:59 -0600 | [diff] [blame] | 1012 | defined(CONFIG_MMC) |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1013 | printf("Interface: "); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1014 | switch (cur_dev->if_type) { |
| 1015 | case IF_TYPE_IDE: |
| 1016 | printf("IDE"); |
| 1017 | break; |
| 1018 | case IF_TYPE_SATA: |
| 1019 | printf("SATA"); |
| 1020 | break; |
| 1021 | case IF_TYPE_SCSI: |
| 1022 | printf("SCSI"); |
| 1023 | break; |
| 1024 | case IF_TYPE_ATAPI: |
| 1025 | printf("ATAPI"); |
| 1026 | break; |
| 1027 | case IF_TYPE_USB: |
| 1028 | printf("USB"); |
| 1029 | break; |
| 1030 | case IF_TYPE_DOC: |
| 1031 | printf("DOC"); |
| 1032 | break; |
| 1033 | case IF_TYPE_MMC: |
| 1034 | printf("MMC"); |
| 1035 | break; |
| 1036 | default: |
| 1037 | printf("Unknown"); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1038 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1039 | |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 1040 | printf("\n Device %d: ", cur_dev->devnum); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1041 | dev_print(cur_dev); |
| 1042 | #endif |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1043 | |
| 1044 | if (read_bootsectandvi(&bs, &volinfo, &fatsize)) { |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1045 | printf("\nNo valid FAT fs found\n"); |
| 1046 | return 1; |
| 1047 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1048 | |
| 1049 | memcpy(vol_label, volinfo.volume_label, 11); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1050 | vol_label[11] = '\0'; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1051 | volinfo.fs_type[5] = '\0'; |
| 1052 | |
Stephen Warren | 461f86e | 2012-10-17 06:44:57 +0000 | [diff] [blame] | 1053 | printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1054 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1055 | return 0; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Stephen Warren | b7b5f31 | 2014-02-03 13:21:10 -0700 | [diff] [blame] | 1058 | int fat_exists(const char *filename) |
| 1059 | { |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1060 | fsdata fsdata; |
Tom Rini | 2460098 | 2017-09-22 07:37:43 -0400 | [diff] [blame] | 1061 | fat_itr *itr; |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1062 | int ret; |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1063 | |
Tuomas Tynkkynen | 09fa964 | 2017-10-01 02:25:21 +0300 | [diff] [blame] | 1064 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1065 | if (!itr) |
| 1066 | return 0; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1067 | ret = fat_itr_root(itr, &fsdata); |
| 1068 | if (ret) |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1069 | goto out; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1070 | |
| 1071 | ret = fat_itr_resolve(itr, filename, TYPE_ANY); |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1072 | free(fsdata.fatbuf); |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1073 | out: |
Tom Rini | 2460098 | 2017-09-22 07:37:43 -0400 | [diff] [blame] | 1074 | free(itr); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1075 | return ret == 0; |
Stephen Warren | b7b5f31 | 2014-02-03 13:21:10 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1078 | int fat_size(const char *filename, loff_t *size) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 1079 | { |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1080 | fsdata fsdata; |
Tom Rini | 2460098 | 2017-09-22 07:37:43 -0400 | [diff] [blame] | 1081 | fat_itr *itr; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1082 | int ret; |
| 1083 | |
Tuomas Tynkkynen | 09fa964 | 2017-10-01 02:25:21 +0300 | [diff] [blame] | 1084 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1085 | if (!itr) |
| 1086 | return -ENOMEM; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1087 | ret = fat_itr_root(itr, &fsdata); |
| 1088 | if (ret) |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1089 | goto out_free_itr; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1090 | |
| 1091 | ret = fat_itr_resolve(itr, filename, TYPE_FILE); |
| 1092 | if (ret) { |
| 1093 | /* |
| 1094 | * Directories don't have size, but fs_size() is not |
| 1095 | * expected to fail if passed a directory path: |
| 1096 | */ |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1097 | free(fsdata.fatbuf); |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1098 | fat_itr_root(itr, &fsdata); |
| 1099 | if (!fat_itr_resolve(itr, filename, TYPE_DIR)) { |
| 1100 | *size = 0; |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1101 | ret = 0; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1102 | } |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1103 | goto out_free_both; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | *size = FAT2CPU32(itr->dent->size); |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1107 | out_free_both: |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1108 | free(fsdata.fatbuf); |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1109 | out_free_itr: |
Tom Rini | 2460098 | 2017-09-22 07:37:43 -0400 | [diff] [blame] | 1110 | free(itr); |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1111 | return ret; |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 1112 | } |
| 1113 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1114 | int file_fat_read_at(const char *filename, loff_t pos, void *buffer, |
| 1115 | loff_t maxsize, loff_t *actread) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1116 | { |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1117 | fsdata fsdata; |
Tom Rini | 2460098 | 2017-09-22 07:37:43 -0400 | [diff] [blame] | 1118 | fat_itr *itr; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1119 | int ret; |
| 1120 | |
Tuomas Tynkkynen | 09fa964 | 2017-10-01 02:25:21 +0300 | [diff] [blame] | 1121 | itr = malloc_cache_aligned(sizeof(fat_itr)); |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1122 | if (!itr) |
| 1123 | return -ENOMEM; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1124 | ret = fat_itr_root(itr, &fsdata); |
| 1125 | if (ret) |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1126 | goto out_free_itr; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1127 | |
| 1128 | ret = fat_itr_resolve(itr, filename, TYPE_FILE); |
| 1129 | if (ret) |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1130 | goto out_free_both; |
Rob Clark | 8eafae2 | 2017-09-09 13:15:54 -0400 | [diff] [blame] | 1131 | |
Andreas Dannenberg | 287c04e | 2018-08-13 21:35:15 -0500 | [diff] [blame] | 1132 | debug("reading %s at pos %llu\n", filename, pos); |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1133 | ret = get_contents(&fsdata, itr->dent, pos, buffer, maxsize, actread); |
| 1134 | |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1135 | out_free_both: |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1136 | free(fsdata.fatbuf); |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1137 | out_free_itr: |
Tom Rini | 2460098 | 2017-09-22 07:37:43 -0400 | [diff] [blame] | 1138 | free(itr); |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1139 | return ret; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1142 | 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] | 1143 | { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1144 | loff_t actread; |
| 1145 | int ret; |
| 1146 | |
| 1147 | ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread); |
| 1148 | if (ret) |
| 1149 | return ret; |
| 1150 | else |
| 1151 | return actread; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1152 | } |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1153 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1154 | int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len, |
| 1155 | loff_t *actread) |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1156 | { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1157 | int ret; |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1158 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1159 | ret = file_fat_read_at(filename, offset, buf, len, actread); |
| 1160 | if (ret) |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1161 | printf("** Unable to read file %s **\n", filename); |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1162 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1163 | return ret; |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Rob Clark | 1f40366 | 2017-09-09 13:15:56 -0400 | [diff] [blame] | 1166 | typedef struct { |
| 1167 | struct fs_dir_stream parent; |
| 1168 | struct fs_dirent dirent; |
| 1169 | fsdata fsdata; |
| 1170 | fat_itr itr; |
| 1171 | } fat_dir; |
| 1172 | |
| 1173 | int fat_opendir(const char *filename, struct fs_dir_stream **dirsp) |
| 1174 | { |
Neil Armstrong | 34dd853 | 2017-11-24 09:54:41 +0100 | [diff] [blame] | 1175 | fat_dir *dir; |
Rob Clark | 1f40366 | 2017-09-09 13:15:56 -0400 | [diff] [blame] | 1176 | int ret; |
| 1177 | |
Neil Armstrong | 34dd853 | 2017-11-24 09:54:41 +0100 | [diff] [blame] | 1178 | dir = malloc_cache_aligned(sizeof(*dir)); |
Rob Clark | 1f40366 | 2017-09-09 13:15:56 -0400 | [diff] [blame] | 1179 | if (!dir) |
| 1180 | return -ENOMEM; |
Neil Armstrong | 34dd853 | 2017-11-24 09:54:41 +0100 | [diff] [blame] | 1181 | memset(dir, 0, sizeof(*dir)); |
Rob Clark | 1f40366 | 2017-09-09 13:15:56 -0400 | [diff] [blame] | 1182 | |
| 1183 | ret = fat_itr_root(&dir->itr, &dir->fsdata); |
| 1184 | if (ret) |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1185 | goto fail_free_dir; |
Rob Clark | 1f40366 | 2017-09-09 13:15:56 -0400 | [diff] [blame] | 1186 | |
| 1187 | ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR); |
| 1188 | if (ret) |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1189 | goto fail_free_both; |
Rob Clark | 1f40366 | 2017-09-09 13:15:56 -0400 | [diff] [blame] | 1190 | |
| 1191 | *dirsp = (struct fs_dir_stream *)dir; |
| 1192 | return 0; |
| 1193 | |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1194 | fail_free_both: |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1195 | free(dir->fsdata.fatbuf); |
Tuomas Tynkkynen | af609e3 | 2017-10-01 02:25:22 +0300 | [diff] [blame] | 1196 | fail_free_dir: |
Rob Clark | 1f40366 | 2017-09-09 13:15:56 -0400 | [diff] [blame] | 1197 | free(dir); |
| 1198 | return ret; |
| 1199 | } |
| 1200 | |
| 1201 | int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp) |
| 1202 | { |
| 1203 | fat_dir *dir = (fat_dir *)dirs; |
| 1204 | struct fs_dirent *dent = &dir->dirent; |
| 1205 | |
| 1206 | if (!fat_itr_next(&dir->itr)) |
| 1207 | return -ENOENT; |
| 1208 | |
| 1209 | memset(dent, 0, sizeof(*dent)); |
| 1210 | strcpy(dent->name, dir->itr.name); |
| 1211 | |
| 1212 | if (fat_itr_isdir(&dir->itr)) { |
| 1213 | dent->type = FS_DT_DIR; |
| 1214 | } else { |
| 1215 | dent->type = FS_DT_REG; |
| 1216 | dent->size = FAT2CPU32(dir->itr.dent->size); |
| 1217 | } |
| 1218 | |
| 1219 | *dentp = dent; |
| 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | void fat_closedir(struct fs_dir_stream *dirs) |
| 1225 | { |
| 1226 | fat_dir *dir = (fat_dir *)dirs; |
Rob Clark | 725ffdb | 2017-09-12 16:40:01 -0400 | [diff] [blame] | 1227 | free(dir->fsdata.fatbuf); |
Rob Clark | 1f40366 | 2017-09-09 13:15:56 -0400 | [diff] [blame] | 1228 | free(dir); |
| 1229 | } |
| 1230 | |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1231 | void fat_close(void) |
| 1232 | { |
| 1233 | } |