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