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