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