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