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