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