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