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