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