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> |
| 13 | #include <config.h> |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 14 | #include <exports.h> |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 15 | #include <fat.h> |
| 16 | #include <asm/byteorder.h> |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 17 | #include <part.h> |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 18 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 19 | #include <memalign.h> |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 20 | #include <linux/compiler.h> |
Richard Genoud | fb7e16c | 2012-12-13 00:47:36 +0000 | [diff] [blame] | 21 | #include <linux/ctype.h> |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 22 | |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 23 | #ifdef CONFIG_SUPPORT_VFAT |
| 24 | static const int vfat_enabled = 1; |
| 25 | #else |
| 26 | static const int vfat_enabled = 0; |
| 27 | #endif |
| 28 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 29 | /* |
| 30 | * Convert a string to lowercase. |
| 31 | */ |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 32 | static void downcase(char *str) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 33 | { |
| 34 | while (*str != '\0') { |
Richard Genoud | fb7e16c | 2012-12-13 00:47:36 +0000 | [diff] [blame] | 35 | *str = tolower(*str); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 36 | str++; |
| 37 | } |
| 38 | } |
| 39 | |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 40 | static block_dev_desc_t *cur_dev; |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 41 | static disk_partition_t cur_part_info; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 42 | |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 43 | #define DOS_BOOT_MAGIC_OFFSET 0x1fe |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 44 | #define DOS_FS_TYPE_OFFSET 0x36 |
Wolfgang Denk | 66c2d73 | 2010-07-19 11:36:57 +0200 | [diff] [blame] | 45 | #define DOS_FS32_TYPE_OFFSET 0x52 |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 46 | |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 47 | static int disk_read(__u32 block, __u32 nr_blocks, void *buf) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 48 | { |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 49 | if (!cur_dev || !cur_dev->block_read) |
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 | |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 52 | return cur_dev->block_read(cur_dev->dev, |
| 53 | cur_part_info.start + block, nr_blocks, buf); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Stephen Warren | 5e8f983 | 2012-10-17 06:44:59 +0000 | [diff] [blame] | 56 | int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 57 | { |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 58 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 59 | |
Stephen Warren | 5e8f983 | 2012-10-17 06:44:59 +0000 | [diff] [blame] | 60 | cur_dev = dev_desc; |
| 61 | cur_part_info = *info; |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 62 | |
| 63 | /* Make sure it has a valid FAT header */ |
| 64 | if (disk_read(0, 1, buffer) != 1) { |
| 65 | cur_dev = NULL; |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | /* Check if it's actually a DOS volume */ |
| 70 | if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) { |
| 71 | cur_dev = NULL; |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | /* Check for FAT12/FAT16/FAT32 filesystem */ |
| 76 | if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3)) |
| 77 | return 0; |
| 78 | if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5)) |
| 79 | return 0; |
| 80 | |
| 81 | cur_dev = NULL; |
| 82 | return -1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Stephen Warren | 5e8f983 | 2012-10-17 06:44:59 +0000 | [diff] [blame] | 85 | int fat_register_device(block_dev_desc_t *dev_desc, int part_no) |
| 86 | { |
| 87 | disk_partition_t info; |
| 88 | |
| 89 | /* First close any currently found FAT filesystem */ |
| 90 | cur_dev = NULL; |
| 91 | |
| 92 | /* Read the partition table, if present */ |
| 93 | if (get_partition_info(dev_desc, part_no, &info)) { |
| 94 | if (part_no != 0) { |
| 95 | printf("** Partition %d not valid on device %d **\n", |
| 96 | part_no, dev_desc->dev); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | info.start = 0; |
| 101 | info.size = dev_desc->lba; |
| 102 | info.blksz = dev_desc->blksz; |
| 103 | info.name[0] = 0; |
| 104 | info.type[0] = 0; |
| 105 | info.bootable = 0; |
| 106 | #ifdef CONFIG_PARTITION_UUIDS |
| 107 | info.uuid[0] = 0; |
| 108 | #endif |
| 109 | } |
| 110 | |
| 111 | return fat_set_blk_dev(dev_desc, &info); |
| 112 | } |
Kyle Moffett | 9813b75 | 2011-12-21 07:08:10 +0000 | [diff] [blame] | 113 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 114 | /* |
| 115 | * Get the first occurence of a directory delimiter ('/' or '\') in a string. |
| 116 | * Return index into string if found, -1 otherwise. |
| 117 | */ |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 118 | static int dirdelim(char *str) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 119 | { |
| 120 | char *start = str; |
| 121 | |
| 122 | while (*str != '\0') { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 123 | if (ISDIRDELIM(*str)) |
| 124 | return str - start; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 125 | str++; |
| 126 | } |
| 127 | return -1; |
| 128 | } |
| 129 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 130 | /* |
| 131 | * Extract zero terminated short name from a directory entry. |
| 132 | */ |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 133 | static void get_name(dir_entry *dirent, char *s_name) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 134 | { |
| 135 | char *ptr; |
| 136 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 137 | memcpy(s_name, dirent->name, 8); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 138 | s_name[8] = '\0'; |
| 139 | ptr = s_name; |
| 140 | while (*ptr && *ptr != ' ') |
| 141 | ptr++; |
| 142 | if (dirent->ext[0] && dirent->ext[0] != ' ') { |
| 143 | *ptr = '.'; |
| 144 | ptr++; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 145 | memcpy(ptr, dirent->ext, 3); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 146 | ptr[3] = '\0'; |
| 147 | while (*ptr && *ptr != ' ') |
| 148 | ptr++; |
| 149 | } |
| 150 | *ptr = '\0'; |
| 151 | if (*s_name == DELETED_FLAG) |
| 152 | *s_name = '\0'; |
| 153 | else if (*s_name == aRING) |
Remy Bohmer | 3c2c2f4 | 2008-11-27 22:30:27 +0100 | [diff] [blame] | 154 | *s_name = DELETED_FLAG; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 155 | downcase(s_name); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 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; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 165 | __u32 off16, offset; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 166 | __u32 ret = 0x00; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 167 | __u16 val1, val2; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 168 | |
| 169 | switch (mydata->fatsize) { |
| 170 | case 32: |
| 171 | bufnum = entry / FAT32BUFSIZE; |
| 172 | offset = entry - bufnum * FAT32BUFSIZE; |
| 173 | break; |
| 174 | case 16: |
| 175 | bufnum = entry / FAT16BUFSIZE; |
| 176 | offset = entry - bufnum * FAT16BUFSIZE; |
| 177 | break; |
| 178 | case 12: |
| 179 | bufnum = entry / FAT12BUFSIZE; |
| 180 | offset = entry - bufnum * FAT12BUFSIZE; |
| 181 | break; |
| 182 | |
| 183 | default: |
| 184 | /* Unsupported FAT size */ |
| 185 | return ret; |
| 186 | } |
| 187 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 188 | debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n", |
| 189 | mydata->fatsize, entry, entry, offset, offset); |
Wolfgang Denk | 2aa98c6 | 2010-07-19 11:36:58 +0200 | [diff] [blame] | 190 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 191 | /* Read a new block of FAT entries into the cache. */ |
| 192 | if (bufnum != mydata->fatbufnum) { |
Sergei Shtylyov | 60b36f0 | 2011-08-08 09:39:29 +0000 | [diff] [blame] | 193 | __u32 getsize = FATBUFBLOCKS; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 194 | __u8 *bufptr = mydata->fatbuf; |
| 195 | __u32 fatlength = mydata->fatlength; |
| 196 | __u32 startblock = bufnum * FATBUFBLOCKS; |
| 197 | |
Benoît Thébaudeau | 8006dd2 | 2012-07-20 15:19:29 +0200 | [diff] [blame] | 198 | if (startblock + getsize > fatlength) |
| 199 | getsize = fatlength - startblock; |
Sergei Shtylyov | 60b36f0 | 2011-08-08 09:39:29 +0000 | [diff] [blame] | 200 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 201 | startblock += mydata->fat_sect; /* Offset from start of disk */ |
| 202 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 203 | if (disk_read(startblock, getsize, bufptr) < 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 204 | debug("Error reading FAT blocks\n"); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 205 | return ret; |
| 206 | } |
| 207 | mydata->fatbufnum = bufnum; |
| 208 | } |
| 209 | |
| 210 | /* Get the actual entry from the table */ |
| 211 | switch (mydata->fatsize) { |
| 212 | case 32: |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 213 | ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 214 | break; |
| 215 | case 16: |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 216 | ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 217 | break; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 218 | case 12: |
| 219 | off16 = (offset * 3) / 4; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 220 | |
| 221 | switch (offset & 0x3) { |
| 222 | case 0: |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 223 | ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 224 | ret &= 0xfff; |
| 225 | break; |
| 226 | case 1: |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 227 | val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 228 | val1 &= 0xf000; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 229 | val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 230 | val2 &= 0x00ff; |
| 231 | ret = (val2 << 4) | (val1 >> 12); |
| 232 | break; |
| 233 | case 2: |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 234 | val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 235 | val1 &= 0xff00; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 236 | val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 237 | val2 &= 0x000f; |
| 238 | ret = (val2 << 8) | (val1 >> 8); |
| 239 | break; |
| 240 | case 3: |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 241 | ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 242 | ret = (ret & 0xfff0) >> 4; |
| 243 | break; |
| 244 | default: |
| 245 | break; |
| 246 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 247 | break; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 248 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 249 | debug("FAT%d: ret: %08x, offset: %04x\n", |
| 250 | mydata->fatsize, ret, offset); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 255 | /* |
| 256 | * Read at most 'size' bytes from the specified cluster into 'buffer'. |
| 257 | * Return 0 on success, -1 otherwise. |
| 258 | */ |
| 259 | static int |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 260 | get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 261 | { |
Erik Hansen | 3f270f4 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 262 | __u32 idx = 0; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 263 | __u32 startsect; |
Kyle Moffett | 46236b1 | 2011-12-20 07:41:13 +0000 | [diff] [blame] | 264 | int ret; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 265 | |
| 266 | if (clustnum > 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 267 | startsect = mydata->data_begin + |
| 268 | clustnum * mydata->clust_size; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 269 | } else { |
| 270 | startsect = mydata->rootdir_sect; |
| 271 | } |
| 272 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 273 | debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect); |
| 274 | |
Benoît Thébaudeau | cc63b25 | 2012-07-20 15:21:08 +0200 | [diff] [blame] | 275 | if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) { |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 276 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 277 | |
Benoît Thébaudeau | cc63b25 | 2012-07-20 15:21:08 +0200 | [diff] [blame] | 278 | printf("FAT: Misaligned buffer address (%p)\n", buffer); |
| 279 | |
| 280 | while (size >= mydata->sect_size) { |
| 281 | ret = disk_read(startsect++, 1, tmpbuf); |
| 282 | if (ret != 1) { |
| 283 | debug("Error reading data (got %d)\n", ret); |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | memcpy(buffer, tmpbuf, mydata->sect_size); |
| 288 | buffer += mydata->sect_size; |
| 289 | size -= mydata->sect_size; |
| 290 | } |
| 291 | } else { |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 292 | idx = size / mydata->sect_size; |
Benoît Thébaudeau | cc63b25 | 2012-07-20 15:21:08 +0200 | [diff] [blame] | 293 | ret = disk_read(startsect, idx, buffer); |
| 294 | if (ret != idx) { |
| 295 | debug("Error reading data (got %d)\n", ret); |
| 296 | return -1; |
| 297 | } |
| 298 | startsect += idx; |
| 299 | idx *= mydata->sect_size; |
| 300 | buffer += idx; |
| 301 | size -= idx; |
| 302 | } |
| 303 | if (size) { |
| 304 | ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size); |
| 305 | |
| 306 | ret = disk_read(startsect, 1, tmpbuf); |
Kyle Moffett | 46236b1 | 2011-12-20 07:41:13 +0000 | [diff] [blame] | 307 | if (ret != 1) { |
| 308 | debug("Error reading data (got %d)\n", ret); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 309 | return -1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 310 | } |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 311 | |
Benoît Thébaudeau | cc63b25 | 2012-07-20 15:21:08 +0200 | [diff] [blame] | 312 | memcpy(buffer, tmpbuf, size); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 318 | /* |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 319 | * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr' |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 320 | * into 'buffer'. |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 321 | * 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] | 322 | */ |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 323 | __u8 get_contents_vfatname_block[MAX_CLUSTSIZE] |
| 324 | __aligned(ARCH_DMA_MINALIGN); |
| 325 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 326 | static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, |
| 327 | __u8 *buffer, loff_t maxsize, loff_t *gotsize) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 328 | { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 329 | loff_t filesize = FAT2CPU32(dentptr->size); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 330 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 331 | __u32 curclust = START(dentptr); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 332 | __u32 endclust, newclust; |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 333 | loff_t actsize; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 334 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 335 | *gotsize = 0; |
| 336 | debug("Filesize: %llu bytes\n", filesize); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 337 | |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 338 | if (pos >= filesize) { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 339 | debug("Read position past EOF: %llu\n", pos); |
| 340 | return 0; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | if (maxsize > 0 && filesize > pos + maxsize) |
| 344 | filesize = pos + maxsize; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 345 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 346 | debug("%llu bytes\n", filesize); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 347 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 348 | actsize = bytesperclust; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 349 | |
| 350 | /* go to cluster at pos */ |
| 351 | while (actsize <= pos) { |
| 352 | curclust = get_fatent(mydata, curclust); |
| 353 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
| 354 | debug("curclust: 0x%x\n", curclust); |
| 355 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 356 | return 0; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 357 | } |
| 358 | actsize += bytesperclust; |
| 359 | } |
| 360 | |
| 361 | /* actsize > pos */ |
| 362 | actsize -= bytesperclust; |
| 363 | filesize -= actsize; |
| 364 | pos -= actsize; |
| 365 | |
| 366 | /* align to beginning of next cluster if any */ |
| 367 | if (pos) { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 368 | actsize = min(filesize, (loff_t)bytesperclust); |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 369 | if (get_cluster(mydata, curclust, get_contents_vfatname_block, |
| 370 | (int)actsize) != 0) { |
| 371 | printf("Error reading cluster\n"); |
| 372 | return -1; |
| 373 | } |
| 374 | filesize -= actsize; |
| 375 | actsize -= pos; |
| 376 | memcpy(buffer, get_contents_vfatname_block + pos, actsize); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 377 | *gotsize += actsize; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 378 | if (!filesize) |
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 | buffer += actsize; |
| 381 | |
| 382 | curclust = get_fatent(mydata, curclust); |
| 383 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
| 384 | debug("curclust: 0x%x\n", curclust); |
| 385 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 386 | return 0; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | actsize = bytesperclust; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 391 | endclust = curclust; |
| 392 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 393 | do { |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 394 | /* search for consecutive clusters */ |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 395 | while (actsize < filesize) { |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 396 | newclust = get_fatent(mydata, endclust); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 397 | if ((newclust - 1) != endclust) |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 398 | goto getit; |
michael | 8ce4e5c | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 399 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 400 | debug("curclust: 0x%x\n", newclust); |
| 401 | debug("Invalid FAT entry\n"); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 402 | return 0; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 403 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 404 | endclust = newclust; |
| 405 | actsize += bytesperclust; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 406 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 407 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 408 | /* get remaining bytes */ |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 409 | actsize = filesize; |
Benoît Thébaudeau | 0880e5b | 2012-07-20 15:21:37 +0200 | [diff] [blame] | 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 += actsize; |
| 415 | return 0; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 416 | getit: |
| 417 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 418 | printf("Error reading cluster\n"); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 419 | return -1; |
| 420 | } |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 421 | *gotsize += (int)actsize; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 422 | filesize -= actsize; |
| 423 | buffer += actsize; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 424 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 425 | curclust = get_fatent(mydata, endclust); |
michael | 8ce4e5c | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 426 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 427 | debug("curclust: 0x%x\n", curclust); |
| 428 | printf("Invalid FAT entry\n"); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 429 | return 0; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 430 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 431 | actsize = bytesperclust; |
| 432 | endclust = curclust; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 433 | } while (1); |
| 434 | } |
| 435 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 436 | /* |
| 437 | * Extract the file name information from 'slotptr' into 'l_name', |
| 438 | * starting at l_name[*idx]. |
| 439 | * Return 1 if terminator (zero byte) is found, 0 otherwise. |
| 440 | */ |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 441 | static int slot2str(dir_slot *slotptr, char *l_name, int *idx) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 442 | { |
| 443 | int j; |
| 444 | |
| 445 | for (j = 0; j <= 8; j += 2) { |
| 446 | l_name[*idx] = slotptr->name0_4[j]; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 447 | if (l_name[*idx] == 0x00) |
| 448 | return 1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 449 | (*idx)++; |
| 450 | } |
| 451 | for (j = 0; j <= 10; j += 2) { |
| 452 | l_name[*idx] = slotptr->name5_10[j]; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 453 | if (l_name[*idx] == 0x00) |
| 454 | return 1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 455 | (*idx)++; |
| 456 | } |
| 457 | for (j = 0; j <= 2; j += 2) { |
| 458 | l_name[*idx] = slotptr->name11_12[j]; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 459 | if (l_name[*idx] == 0x00) |
| 460 | return 1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 461 | (*idx)++; |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 467 | /* |
| 468 | * Extract the full long filename starting at 'retdent' (which is really |
| 469 | * a slot) into 'l_name'. If successful also copy the real directory entry |
| 470 | * into 'retdent' |
| 471 | * Return 0 on success, -1 otherwise. |
| 472 | */ |
| 473 | static int |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 474 | get_vfatname(fsdata *mydata, int curclust, __u8 *cluster, |
| 475 | dir_entry *retdent, char *l_name) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 476 | { |
| 477 | dir_entry *realdent; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 478 | dir_slot *slotptr = (dir_slot *)retdent; |
Sergei Shtylyov | 025421e | 2011-08-19 09:37:46 +0000 | [diff] [blame] | 479 | __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ? |
| 480 | PREFETCH_BLOCKS : |
| 481 | mydata->clust_size); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 482 | __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 483 | int idx = 0; |
| 484 | |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 485 | if (counter > VFAT_MAXSEQ) { |
| 486 | debug("Error: VFAT name is too long\n"); |
| 487 | return -1; |
| 488 | } |
| 489 | |
| 490 | while ((__u8 *)slotptr < buflimit) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 491 | if (counter == 0) |
| 492 | break; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 493 | if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter) |
| 494 | return -1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 495 | slotptr++; |
| 496 | counter--; |
| 497 | } |
| 498 | |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 499 | if ((__u8 *)slotptr >= buflimit) { |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 500 | dir_slot *slotptr2; |
| 501 | |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 502 | if (curclust == 0) |
| 503 | return -1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 504 | curclust = get_fatent(mydata, curclust); |
michael | 8ce4e5c | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 505 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 506 | debug("curclust: 0x%x\n", curclust); |
| 507 | printf("Invalid FAT entry\n"); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 508 | return -1; |
| 509 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 510 | |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 511 | if (get_cluster(mydata, curclust, get_contents_vfatname_block, |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 512 | mydata->clust_size * mydata->sect_size) != 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 513 | debug("Error: reading directory block\n"); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 514 | return -1; |
| 515 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 516 | |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 517 | slotptr2 = (dir_slot *)get_contents_vfatname_block; |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 518 | while (counter > 0) { |
| 519 | if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK) |
| 520 | & 0xff) != counter) |
| 521 | return -1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 522 | slotptr2++; |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 523 | counter--; |
| 524 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 525 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 526 | /* Save the real directory entry */ |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 527 | realdent = (dir_entry *)slotptr2; |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 528 | while ((__u8 *)slotptr2 > get_contents_vfatname_block) { |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 529 | slotptr2--; |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 530 | slot2str(slotptr2, l_name, &idx); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 531 | } |
| 532 | } else { |
| 533 | /* Save the real directory entry */ |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 534 | realdent = (dir_entry *)slotptr; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | do { |
| 538 | slotptr--; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 539 | if (slot2str(slotptr, l_name, &idx)) |
| 540 | break; |
wdenk | 2d1a537 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 541 | } while (!(slotptr->id & LAST_LONG_ENTRY_MASK)); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 542 | |
| 543 | l_name[idx] = '\0'; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 544 | if (*l_name == DELETED_FLAG) |
| 545 | *l_name = '\0'; |
| 546 | else if (*l_name == aRING) |
| 547 | *l_name = DELETED_FLAG; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 548 | downcase(l_name); |
| 549 | |
| 550 | /* Return the real directory entry */ |
| 551 | memcpy(retdent, realdent, sizeof(dir_entry)); |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 556 | /* Calculate short name checksum */ |
Marek Vasut | ff04f6d | 2012-10-09 07:20:22 +0000 | [diff] [blame] | 557 | static __u8 mkcksum(const char name[8], const char ext[3]) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 558 | { |
| 559 | int i; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 560 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 561 | __u8 ret = 0; |
| 562 | |
Marek Vasut | 6ad77d8 | 2013-01-11 03:35:48 +0000 | [diff] [blame] | 563 | for (i = 0; i < 8; i++) |
Marek Vasut | ff04f6d | 2012-10-09 07:20:22 +0000 | [diff] [blame] | 564 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i]; |
Marek Vasut | 6ad77d8 | 2013-01-11 03:35:48 +0000 | [diff] [blame] | 565 | for (i = 0; i < 3; i++) |
Marek Vasut | ff04f6d | 2012-10-09 07:20:22 +0000 | [diff] [blame] | 566 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i]; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 567 | |
| 568 | return ret; |
| 569 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 570 | |
| 571 | /* |
| 572 | * Get the directory entry associated with 'filename' from the directory |
| 573 | * starting at 'startsect' |
| 574 | */ |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 575 | __u8 get_dentfromdir_block[MAX_CLUSTSIZE] |
| 576 | __aligned(ARCH_DMA_MINALIGN); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 577 | |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 578 | static dir_entry *get_dentfromdir(fsdata *mydata, int startsect, |
| 579 | char *filename, dir_entry *retdent, |
| 580 | int dols) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 581 | { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 582 | __u16 prevcksum = 0xffff; |
| 583 | __u32 curclust = START(retdent); |
| 584 | int files = 0, dirs = 0; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 585 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 586 | debug("get_dentfromdir: %s\n", filename); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 587 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 588 | while (1) { |
| 589 | dir_entry *dentptr; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 590 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 591 | int i; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 592 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 593 | if (get_cluster(mydata, curclust, get_dentfromdir_block, |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 594 | mydata->clust_size * mydata->sect_size) != 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 595 | debug("Error: reading directory block\n"); |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | dentptr = (dir_entry *)get_dentfromdir_block; |
| 600 | |
| 601 | for (i = 0; i < DIRENTSPERCLUST; i++) { |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 602 | char s_name[14], l_name[VFAT_MAXLEN_BYTES]; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 603 | |
| 604 | l_name[0] = '\0'; |
| 605 | if (dentptr->name[0] == DELETED_FLAG) { |
| 606 | dentptr++; |
| 607 | continue; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 608 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 609 | if ((dentptr->attr & ATTR_VOLUME)) { |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 610 | if (vfat_enabled && |
| 611 | (dentptr->attr & ATTR_VFAT) == ATTR_VFAT && |
J. Vijayanand | 206d68f | 2011-10-19 07:43:08 +0000 | [diff] [blame] | 612 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 613 | prevcksum = ((dir_slot *)dentptr)->alias_checksum; |
| 614 | get_vfatname(mydata, curclust, |
| 615 | get_dentfromdir_block, |
| 616 | dentptr, l_name); |
| 617 | if (dols) { |
| 618 | int isdir; |
| 619 | char dirc; |
| 620 | int doit = 0; |
| 621 | |
| 622 | isdir = (dentptr->attr & ATTR_DIR); |
| 623 | |
| 624 | if (isdir) { |
| 625 | dirs++; |
| 626 | dirc = '/'; |
| 627 | doit = 1; |
| 628 | } else { |
| 629 | dirc = ' '; |
| 630 | if (l_name[0] != 0) { |
| 631 | files++; |
| 632 | doit = 1; |
| 633 | } |
| 634 | } |
| 635 | if (doit) { |
| 636 | if (dirc == ' ') { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 637 | printf(" %8u %s%c\n", |
| 638 | FAT2CPU32(dentptr->size), |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 639 | l_name, |
| 640 | dirc); |
| 641 | } else { |
| 642 | printf(" %s%c\n", |
| 643 | l_name, |
| 644 | dirc); |
| 645 | } |
| 646 | } |
| 647 | dentptr++; |
| 648 | continue; |
| 649 | } |
| 650 | debug("vfatname: |%s|\n", l_name); |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 651 | } else { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 652 | /* Volume label or VFAT entry */ |
| 653 | dentptr++; |
| 654 | continue; |
| 655 | } |
| 656 | } |
| 657 | if (dentptr->name[0] == 0) { |
| 658 | if (dols) { |
| 659 | printf("\n%d file(s), %d dir(s)\n\n", |
| 660 | files, dirs); |
| 661 | } |
| 662 | debug("Dentname == NULL - %d\n", i); |
| 663 | return NULL; |
| 664 | } |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 665 | if (vfat_enabled) { |
| 666 | __u8 csum = mkcksum(dentptr->name, dentptr->ext); |
| 667 | if (dols && csum == prevcksum) { |
| 668 | prevcksum = 0xffff; |
| 669 | dentptr++; |
| 670 | continue; |
| 671 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 672 | } |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 673 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 674 | get_name(dentptr, s_name); |
| 675 | if (dols) { |
| 676 | int isdir = (dentptr->attr & ATTR_DIR); |
| 677 | char dirc; |
| 678 | int doit = 0; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 679 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 680 | if (isdir) { |
| 681 | dirs++; |
| 682 | dirc = '/'; |
| 683 | doit = 1; |
| 684 | } else { |
| 685 | dirc = ' '; |
| 686 | if (s_name[0] != 0) { |
| 687 | files++; |
| 688 | doit = 1; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | if (doit) { |
| 693 | if (dirc == ' ') { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 694 | printf(" %8u %s%c\n", |
| 695 | FAT2CPU32(dentptr->size), |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 696 | s_name, dirc); |
| 697 | } else { |
| 698 | printf(" %s%c\n", |
| 699 | s_name, dirc); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | dentptr++; |
| 704 | continue; |
| 705 | } |
| 706 | |
| 707 | if (strcmp(filename, s_name) |
| 708 | && strcmp(filename, l_name)) { |
| 709 | debug("Mismatch: |%s|%s|\n", s_name, l_name); |
| 710 | dentptr++; |
| 711 | continue; |
| 712 | } |
| 713 | |
| 714 | memcpy(retdent, dentptr, sizeof(dir_entry)); |
| 715 | |
| 716 | debug("DentName: %s", s_name); |
| 717 | debug(", start: 0x%x", START(dentptr)); |
| 718 | debug(", size: 0x%x %s\n", |
| 719 | FAT2CPU32(dentptr->size), |
| 720 | (dentptr->attr & ATTR_DIR) ? "(DIR)" : ""); |
| 721 | |
| 722 | return retdent; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 723 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 724 | |
| 725 | curclust = get_fatent(mydata, curclust); |
| 726 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
| 727 | debug("curclust: 0x%x\n", curclust); |
| 728 | printf("Invalid FAT entry\n"); |
| 729 | return NULL; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 730 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 731 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 732 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 733 | return NULL; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 734 | } |
| 735 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 736 | /* |
| 737 | * Read boot sector and volume info from a FAT filesystem |
| 738 | */ |
| 739 | static int |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 740 | read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 741 | { |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 742 | __u8 *block; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 743 | volume_info *vistart; |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 744 | int ret = 0; |
| 745 | |
| 746 | if (cur_dev == NULL) { |
| 747 | debug("Error: no device selected\n"); |
| 748 | return -1; |
| 749 | } |
| 750 | |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 751 | block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 752 | if (block == NULL) { |
| 753 | debug("Error: allocating block\n"); |
| 754 | return -1; |
| 755 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 756 | |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 757 | if (disk_read(0, 1, block) < 0) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 758 | debug("Error: reading block\n"); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 759 | goto fail; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | memcpy(bs, block, sizeof(boot_sector)); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 763 | bs->reserved = FAT2CPU16(bs->reserved); |
| 764 | bs->fat_length = FAT2CPU16(bs->fat_length); |
| 765 | bs->secs_track = FAT2CPU16(bs->secs_track); |
| 766 | bs->heads = FAT2CPU16(bs->heads); |
| 767 | bs->total_sect = FAT2CPU32(bs->total_sect); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 768 | |
| 769 | /* FAT32 entries */ |
| 770 | if (bs->fat_length == 0) { |
| 771 | /* Assume FAT32 */ |
| 772 | bs->fat32_length = FAT2CPU32(bs->fat32_length); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 773 | bs->flags = FAT2CPU16(bs->flags); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 774 | bs->root_cluster = FAT2CPU32(bs->root_cluster); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 775 | bs->info_sector = FAT2CPU16(bs->info_sector); |
| 776 | bs->backup_boot = FAT2CPU16(bs->backup_boot); |
| 777 | vistart = (volume_info *)(block + sizeof(boot_sector)); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 778 | *fatsize = 32; |
| 779 | } else { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 780 | vistart = (volume_info *)&(bs->fat32_length); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 781 | *fatsize = 0; |
| 782 | } |
| 783 | memcpy(volinfo, vistart, sizeof(volume_info)); |
| 784 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 785 | if (*fatsize == 32) { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 786 | if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 787 | goto exit; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 788 | } else { |
Tom Rix | 651351f | 2009-05-20 07:55:41 -0500 | [diff] [blame] | 789 | if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 790 | *fatsize = 12; |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 791 | goto exit; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 792 | } |
Tom Rix | 651351f | 2009-05-20 07:55:41 -0500 | [diff] [blame] | 793 | if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 794 | *fatsize = 16; |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 795 | goto exit; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 799 | debug("Error: broken fs_type sign\n"); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 800 | fail: |
| 801 | ret = -1; |
| 802 | exit: |
| 803 | free(block); |
| 804 | return ret; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 807 | __u8 do_fat_read_at_block[MAX_CLUSTSIZE] |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 808 | __aligned(ARCH_DMA_MINALIGN); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 809 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 810 | int do_fat_read_at(const char *filename, loff_t pos, void *buffer, |
| 811 | loff_t maxsize, int dols, int dogetsize, loff_t *size) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 812 | { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 813 | char fnamecopy[2048]; |
| 814 | boot_sector bs; |
| 815 | volume_info volinfo; |
| 816 | fsdata datablock; |
| 817 | fsdata *mydata = &datablock; |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 818 | dir_entry *dentptr = NULL; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 819 | __u16 prevcksum = 0xffff; |
| 820 | char *subname = ""; |
Erik Hansen | 3f270f4 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 821 | __u32 cursect; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 822 | int idx, isdir = 0; |
| 823 | int files = 0, dirs = 0; |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 824 | int ret = -1; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 825 | int firsttime; |
Sergei Shtylyov | 40e2191 | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 826 | __u32 root_cluster = 0; |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 827 | __u32 read_blk; |
Erik Hansen | 3f270f4 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 828 | int rootdir_size = 0; |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 829 | int buffer_blk_cnt; |
| 830 | int do_read; |
| 831 | __u8 *dir_ptr; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 832 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 833 | if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) { |
| 834 | debug("Error: reading boot sector\n"); |
| 835 | return -1; |
| 836 | } |
| 837 | |
Sergei Shtylyov | 40e2191 | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 838 | if (mydata->fatsize == 32) { |
| 839 | root_cluster = bs.root_cluster; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 840 | mydata->fatlength = bs.fat32_length; |
Sergei Shtylyov | 40e2191 | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 841 | } else { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 842 | mydata->fatlength = bs.fat_length; |
Sergei Shtylyov | 40e2191 | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 843 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 844 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 845 | mydata->fat_sect = bs.reserved; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 846 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 847 | cursect = mydata->rootdir_sect |
| 848 | = mydata->fat_sect + mydata->fatlength * bs.fats; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 849 | |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 850 | mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 851 | mydata->clust_size = bs.cluster_size; |
Kyle Moffett | 46236b1 | 2011-12-20 07:41:13 +0000 | [diff] [blame] | 852 | if (mydata->sect_size != cur_part_info.blksz) { |
| 853 | printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n", |
| 854 | mydata->sect_size, cur_part_info.blksz); |
| 855 | return -1; |
| 856 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 857 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 858 | if (mydata->fatsize == 32) { |
| 859 | mydata->data_begin = mydata->rootdir_sect - |
| 860 | (mydata->clust_size * 2); |
| 861 | } else { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 862 | rootdir_size = ((bs.dir_entries[1] * (int)256 + |
| 863 | bs.dir_entries[0]) * |
| 864 | sizeof(dir_entry)) / |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 865 | mydata->sect_size; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 866 | mydata->data_begin = mydata->rootdir_sect + |
| 867 | rootdir_size - |
| 868 | (mydata->clust_size * 2); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 869 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 870 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 871 | mydata->fatbufnum = -1; |
Eric Nelson | 9a800ac | 2012-04-11 04:08:53 +0000 | [diff] [blame] | 872 | mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 873 | if (mydata->fatbuf == NULL) { |
| 874 | debug("Error: allocating memory\n"); |
| 875 | return -1; |
| 876 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 877 | |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 878 | if (vfat_enabled) |
| 879 | debug("VFAT Support enabled\n"); |
| 880 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 881 | debug("FAT%d, fat_sect: %d, fatlength: %d\n", |
| 882 | mydata->fatsize, mydata->fat_sect, mydata->fatlength); |
| 883 | debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n" |
| 884 | "Data begins at: %d\n", |
| 885 | root_cluster, |
| 886 | mydata->rootdir_sect, |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 887 | mydata->rootdir_sect * mydata->sect_size, mydata->data_begin); |
| 888 | debug("Sector size: %d, cluster size: %d\n", mydata->sect_size, |
| 889 | mydata->clust_size); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 890 | |
| 891 | /* "cwd" is always the root... */ |
| 892 | while (ISDIRDELIM(*filename)) |
| 893 | filename++; |
| 894 | |
| 895 | /* Make a copy of the filename and convert it to lowercase */ |
| 896 | strcpy(fnamecopy, filename); |
| 897 | downcase(fnamecopy); |
| 898 | |
Stephen Warren | 18a10d4 | 2015-07-28 21:55:03 -0600 | [diff] [blame] | 899 | root_reparse: |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 900 | if (*fnamecopy == '\0') { |
| 901 | if (!dols) |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 902 | goto exit; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 903 | |
| 904 | dols = LS_ROOT; |
| 905 | } else if ((idx = dirdelim(fnamecopy)) >= 0) { |
| 906 | isdir = 1; |
| 907 | fnamecopy[idx] = '\0'; |
| 908 | subname = fnamecopy + idx + 1; |
| 909 | |
| 910 | /* Handle multiple delimiters */ |
| 911 | while (ISDIRDELIM(*subname)) |
| 912 | subname++; |
| 913 | } else if (dols) { |
| 914 | isdir = 1; |
| 915 | } |
| 916 | |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 917 | buffer_blk_cnt = 0; |
| 918 | firsttime = 1; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 919 | while (1) { |
| 920 | int i; |
| 921 | |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 922 | if (mydata->fatsize == 32 || firsttime) { |
| 923 | dir_ptr = do_fat_read_at_block; |
| 924 | firsttime = 0; |
| 925 | } else { |
| 926 | /** |
| 927 | * FAT16 sector buffer modification: |
| 928 | * Each loop, the second buffered block is moved to |
| 929 | * the buffer begin, and two next sectors are read |
| 930 | * next to the previously moved one. So the sector |
| 931 | * buffer keeps always 3 sectors for fat16. |
| 932 | * And the current sector is the buffer second sector |
| 933 | * beside the "firsttime" read, when it is the first one. |
| 934 | * |
| 935 | * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1] |
| 936 | * n = computed root dir sector |
| 937 | * loop | cursect-1 | cursect | cursect+1 | |
| 938 | * 0 | sector n+0 | sector n+1 | none | |
| 939 | * 1 | none | sector n+0 | sector n+1 | |
| 940 | * 0 | sector n+1 | sector n+2 | sector n+3 | |
| 941 | * 1 | sector n+3 | ... |
| 942 | */ |
| 943 | dir_ptr = (do_fat_read_at_block + mydata->sect_size); |
| 944 | memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size); |
| 945 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 946 | |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 947 | do_read = 1; |
| 948 | |
| 949 | if (mydata->fatsize == 32 && buffer_blk_cnt) |
| 950 | do_read = 0; |
| 951 | |
| 952 | if (do_read) { |
| 953 | read_blk = (mydata->fatsize == 32) ? |
| 954 | mydata->clust_size : PREFETCH_BLOCKS; |
| 955 | |
| 956 | debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n", |
| 957 | cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK); |
| 958 | |
| 959 | if (disk_read(cursect, read_blk, dir_ptr) < 0) { |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 960 | debug("Error: reading rootdir block\n"); |
| 961 | goto exit; |
| 962 | } |
| 963 | |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 964 | dentptr = (dir_entry *)dir_ptr; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 965 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 966 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 967 | for (i = 0; i < DIRENTSPERBLOCK; i++) { |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 968 | char s_name[14], l_name[VFAT_MAXLEN_BYTES]; |
Marek Vasut | ff04f6d | 2012-10-09 07:20:22 +0000 | [diff] [blame] | 969 | __u8 csum; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 970 | |
| 971 | l_name[0] = '\0'; |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 972 | if (dentptr->name[0] == DELETED_FLAG) { |
| 973 | dentptr++; |
| 974 | continue; |
| 975 | } |
Marek Vasut | ff04f6d | 2012-10-09 07:20:22 +0000 | [diff] [blame] | 976 | |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 977 | if (vfat_enabled) |
| 978 | csum = mkcksum(dentptr->name, dentptr->ext); |
| 979 | |
Marek Vasut | ff04f6d | 2012-10-09 07:20:22 +0000 | [diff] [blame] | 980 | if (dentptr->attr & ATTR_VOLUME) { |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 981 | if (vfat_enabled && |
| 982 | (dentptr->attr & ATTR_VFAT) == ATTR_VFAT && |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 983 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
| 984 | prevcksum = |
| 985 | ((dir_slot *)dentptr)->alias_checksum; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 986 | |
Mikhail Zolotaryov | 3831530 | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 987 | get_vfatname(mydata, |
Sergei Shtylyov | 40e2191 | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 988 | root_cluster, |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 989 | dir_ptr, |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 990 | dentptr, l_name); |
| 991 | |
| 992 | if (dols == LS_ROOT) { |
| 993 | char dirc; |
| 994 | int doit = 0; |
| 995 | int isdir = |
| 996 | (dentptr->attr & ATTR_DIR); |
| 997 | |
| 998 | if (isdir) { |
| 999 | dirs++; |
| 1000 | dirc = '/'; |
| 1001 | doit = 1; |
| 1002 | } else { |
| 1003 | dirc = ' '; |
| 1004 | if (l_name[0] != 0) { |
| 1005 | files++; |
| 1006 | doit = 1; |
| 1007 | } |
| 1008 | } |
| 1009 | if (doit) { |
| 1010 | if (dirc == ' ') { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1011 | printf(" %8u %s%c\n", |
| 1012 | FAT2CPU32(dentptr->size), |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1013 | l_name, |
| 1014 | dirc); |
| 1015 | } else { |
| 1016 | printf(" %s%c\n", |
| 1017 | l_name, |
| 1018 | dirc); |
| 1019 | } |
| 1020 | } |
| 1021 | dentptr++; |
| 1022 | continue; |
| 1023 | } |
| 1024 | debug("Rootvfatname: |%s|\n", |
| 1025 | l_name); |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 1026 | } else { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1027 | /* Volume label or VFAT entry */ |
| 1028 | dentptr++; |
| 1029 | continue; |
| 1030 | } |
| 1031 | } else if (dentptr->name[0] == 0) { |
| 1032 | debug("RootDentname == NULL - %d\n", i); |
| 1033 | if (dols == LS_ROOT) { |
| 1034 | printf("\n%d file(s), %d dir(s)\n\n", |
| 1035 | files, dirs); |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1036 | ret = 0; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1037 | } |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1038 | goto exit; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1039 | } |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 1040 | else if (vfat_enabled && |
| 1041 | dols == LS_ROOT && csum == prevcksum) { |
Sergei Shtylyov | bf34e7d | 2012-01-02 06:54:29 +0000 | [diff] [blame] | 1042 | prevcksum = 0xffff; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1043 | dentptr++; |
| 1044 | continue; |
| 1045 | } |
Richard Genoud | cb940c7 | 2012-12-13 03:30:10 +0000 | [diff] [blame] | 1046 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1047 | get_name(dentptr, s_name); |
| 1048 | |
| 1049 | if (dols == LS_ROOT) { |
| 1050 | int isdir = (dentptr->attr & ATTR_DIR); |
| 1051 | char dirc; |
| 1052 | int doit = 0; |
| 1053 | |
| 1054 | if (isdir) { |
| 1055 | dirc = '/'; |
| 1056 | if (s_name[0] != 0) { |
| 1057 | dirs++; |
| 1058 | doit = 1; |
| 1059 | } |
| 1060 | } else { |
| 1061 | dirc = ' '; |
| 1062 | if (s_name[0] != 0) { |
| 1063 | files++; |
| 1064 | doit = 1; |
| 1065 | } |
| 1066 | } |
| 1067 | if (doit) { |
| 1068 | if (dirc == ' ') { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1069 | printf(" %8u %s%c\n", |
| 1070 | FAT2CPU32(dentptr->size), |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1071 | s_name, dirc); |
| 1072 | } else { |
| 1073 | printf(" %s%c\n", |
| 1074 | s_name, dirc); |
| 1075 | } |
| 1076 | } |
| 1077 | dentptr++; |
| 1078 | continue; |
| 1079 | } |
| 1080 | |
| 1081 | if (strcmp(fnamecopy, s_name) |
| 1082 | && strcmp(fnamecopy, l_name)) { |
| 1083 | debug("RootMismatch: |%s|%s|\n", s_name, |
| 1084 | l_name); |
| 1085 | dentptr++; |
| 1086 | continue; |
| 1087 | } |
| 1088 | |
| 1089 | if (isdir && !(dentptr->attr & ATTR_DIR)) |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1090 | goto exit; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1091 | |
| 1092 | debug("RootName: %s", s_name); |
| 1093 | debug(", start: 0x%x", START(dentptr)); |
| 1094 | debug(", size: 0x%x %s\n", |
| 1095 | FAT2CPU32(dentptr->size), |
| 1096 | isdir ? "(DIR)" : ""); |
| 1097 | |
| 1098 | goto rootdir_done; /* We got a match */ |
| 1099 | } |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 1100 | debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt, |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1101 | mydata->clust_size); |
| 1102 | |
| 1103 | /* |
| 1104 | * On FAT32 we must fetch the FAT entries for the next |
| 1105 | * root directory clusters when a cluster has been |
| 1106 | * completely processed. |
| 1107 | */ |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 1108 | ++buffer_blk_cnt; |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 1109 | int rootdir_end = 0; |
| 1110 | if (mydata->fatsize == 32) { |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 1111 | if (buffer_blk_cnt == mydata->clust_size) { |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 1112 | int nxtsect = 0; |
| 1113 | int nxt_clust = 0; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1114 | |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 1115 | nxt_clust = get_fatent(mydata, root_cluster); |
| 1116 | rootdir_end = CHECK_CLUST(nxt_clust, 32); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1117 | |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 1118 | nxtsect = mydata->data_begin + |
| 1119 | (nxt_clust * mydata->clust_size); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1120 | |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 1121 | root_cluster = nxt_clust; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1122 | |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 1123 | cursect = nxtsect; |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 1124 | buffer_blk_cnt = 0; |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 1125 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1126 | } else { |
Przemyslaw Marczak | 64f65e1 | 2014-12-18 17:14:17 +0100 | [diff] [blame] | 1127 | if (buffer_blk_cnt == PREFETCH_BLOCKS) |
| 1128 | buffer_blk_cnt = 0; |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 1129 | |
| 1130 | rootdir_end = (++cursect - mydata->rootdir_sect >= |
| 1131 | rootdir_size); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1132 | } |
Erik Hansen | 3f270f4 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 1133 | |
| 1134 | /* If end of rootdir reached */ |
Benoît Thébaudeau | cd1b042 | 2012-07-20 15:20:12 +0200 | [diff] [blame] | 1135 | if (rootdir_end) { |
Erik Hansen | 3f270f4 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 1136 | if (dols == LS_ROOT) { |
| 1137 | printf("\n%d file(s), %d dir(s)\n\n", |
| 1138 | files, dirs); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1139 | *size = 0; |
Erik Hansen | 3f270f4 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 1140 | } |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1141 | goto exit; |
Erik Hansen | 3f270f4 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 1142 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1143 | } |
| 1144 | rootdir_done: |
| 1145 | |
| 1146 | firsttime = 1; |
| 1147 | |
| 1148 | while (isdir) { |
| 1149 | int startsect = mydata->data_begin |
| 1150 | + START(dentptr) * mydata->clust_size; |
| 1151 | dir_entry dent; |
| 1152 | char *nextname = NULL; |
| 1153 | |
| 1154 | dent = *dentptr; |
| 1155 | dentptr = &dent; |
| 1156 | |
| 1157 | idx = dirdelim(subname); |
| 1158 | |
| 1159 | if (idx >= 0) { |
| 1160 | subname[idx] = '\0'; |
| 1161 | nextname = subname + idx + 1; |
| 1162 | /* Handle multiple delimiters */ |
| 1163 | while (ISDIRDELIM(*nextname)) |
| 1164 | nextname++; |
| 1165 | if (dols && *nextname == '\0') |
| 1166 | firsttime = 0; |
| 1167 | } else { |
| 1168 | if (dols && firsttime) { |
| 1169 | firsttime = 0; |
| 1170 | } else { |
| 1171 | isdir = 0; |
| 1172 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1173 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1174 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1175 | if (get_dentfromdir(mydata, startsect, subname, dentptr, |
| 1176 | isdir ? 0 : dols) == NULL) { |
| 1177 | if (dols && !isdir) |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1178 | *size = 0; |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1179 | goto exit; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1180 | } |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1181 | |
Benoît Thébaudeau | 7ee46ce | 2012-07-20 03:20:29 +0000 | [diff] [blame] | 1182 | if (isdir && !(dentptr->attr & ATTR_DIR)) |
| 1183 | goto exit; |
| 1184 | |
Stephen Warren | 18a10d4 | 2015-07-28 21:55:03 -0600 | [diff] [blame] | 1185 | /* |
| 1186 | * If we are looking for a directory, and found a directory |
| 1187 | * type entry, and the entry is for the root directory (as |
| 1188 | * denoted by a cluster number of 0), jump back to the start |
| 1189 | * of the function, since at least on FAT12/16, the root dir |
| 1190 | * lives in a hard-coded location and needs special handling |
| 1191 | * to parse, rather than simply following the cluster linked |
| 1192 | * list in the FAT, like other directories. |
| 1193 | */ |
| 1194 | if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) { |
| 1195 | /* |
| 1196 | * Modify the filename to remove the prefix that gets |
| 1197 | * back to the root directory, so the initial root dir |
| 1198 | * parsing code can continue from where we are without |
| 1199 | * confusion. |
| 1200 | */ |
| 1201 | strcpy(fnamecopy, nextname ?: ""); |
| 1202 | /* |
| 1203 | * Set up state the same way as the function does when |
| 1204 | * first started. This is required for the root dir |
| 1205 | * parsing code operates in its expected environment. |
| 1206 | */ |
| 1207 | subname = ""; |
| 1208 | cursect = mydata->rootdir_sect; |
| 1209 | isdir = 0; |
| 1210 | goto root_reparse; |
| 1211 | } |
| 1212 | |
Benoît Thébaudeau | 7ee46ce | 2012-07-20 03:20:29 +0000 | [diff] [blame] | 1213 | if (idx >= 0) |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1214 | subname = nextname; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1217 | if (dogetsize) { |
| 1218 | *size = FAT2CPU32(dentptr->size); |
| 1219 | ret = 0; |
| 1220 | } else { |
| 1221 | ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size); |
| 1222 | } |
| 1223 | debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1224 | |
Sergei Shtylyov | ac49777 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1225 | exit: |
| 1226 | free(mydata->fatbuf); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1227 | return ret; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1230 | int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols, |
| 1231 | loff_t *actread) |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 1232 | { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1233 | return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread); |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 1236 | int file_fat_detectfs(void) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1237 | { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1238 | boot_sector bs; |
| 1239 | volume_info volinfo; |
| 1240 | int fatsize; |
| 1241 | char vol_label[12]; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1242 | |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1243 | if (cur_dev == NULL) { |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1244 | printf("No current device\n"); |
| 1245 | return 1; |
| 1246 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1247 | |
Jon Loeliger | dd60d12 | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 1248 | #if defined(CONFIG_CMD_IDE) || \ |
Sonic Zhang | 8c5170a | 2008-12-09 23:20:18 -0500 | [diff] [blame] | 1249 | defined(CONFIG_CMD_SATA) || \ |
Jon Loeliger | dd60d12 | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 1250 | defined(CONFIG_CMD_SCSI) || \ |
| 1251 | defined(CONFIG_CMD_USB) || \ |
Andy Fleming | 21f6f96 | 2008-01-16 13:06:59 -0600 | [diff] [blame] | 1252 | defined(CONFIG_MMC) |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1253 | printf("Interface: "); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1254 | switch (cur_dev->if_type) { |
| 1255 | case IF_TYPE_IDE: |
| 1256 | printf("IDE"); |
| 1257 | break; |
| 1258 | case IF_TYPE_SATA: |
| 1259 | printf("SATA"); |
| 1260 | break; |
| 1261 | case IF_TYPE_SCSI: |
| 1262 | printf("SCSI"); |
| 1263 | break; |
| 1264 | case IF_TYPE_ATAPI: |
| 1265 | printf("ATAPI"); |
| 1266 | break; |
| 1267 | case IF_TYPE_USB: |
| 1268 | printf("USB"); |
| 1269 | break; |
| 1270 | case IF_TYPE_DOC: |
| 1271 | printf("DOC"); |
| 1272 | break; |
| 1273 | case IF_TYPE_MMC: |
| 1274 | printf("MMC"); |
| 1275 | break; |
| 1276 | default: |
| 1277 | printf("Unknown"); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1278 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1279 | |
| 1280 | printf("\n Device %d: ", cur_dev->dev); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1281 | dev_print(cur_dev); |
| 1282 | #endif |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1283 | |
| 1284 | if (read_bootsectandvi(&bs, &volinfo, &fatsize)) { |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1285 | printf("\nNo valid FAT fs found\n"); |
| 1286 | return 1; |
| 1287 | } |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1288 | |
| 1289 | memcpy(vol_label, volinfo.volume_label, 11); |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1290 | vol_label[11] = '\0'; |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1291 | volinfo.fs_type[5] = '\0'; |
| 1292 | |
Stephen Warren | 461f86e | 2012-10-17 06:44:57 +0000 | [diff] [blame] | 1293 | printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label); |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1294 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1295 | return 0; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Benoît Thébaudeau | 9795e07 | 2012-07-20 15:18:44 +0200 | [diff] [blame] | 1298 | int file_fat_ls(const char *dir) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1299 | { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1300 | loff_t size; |
| 1301 | |
| 1302 | return do_fat_read(dir, NULL, 0, LS_YES, &size); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Stephen Warren | b7b5f31 | 2014-02-03 13:21:10 -0700 | [diff] [blame] | 1305 | int fat_exists(const char *filename) |
| 1306 | { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1307 | int ret; |
| 1308 | loff_t size; |
| 1309 | |
| 1310 | ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size); |
| 1311 | return ret == 0; |
Stephen Warren | b7b5f31 | 2014-02-03 13:21:10 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1314 | int fat_size(const char *filename, loff_t *size) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 1315 | { |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1316 | return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size); |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 1317 | } |
| 1318 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1319 | int file_fat_read_at(const char *filename, loff_t pos, void *buffer, |
| 1320 | loff_t maxsize, loff_t *actread) |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1321 | { |
Wolfgang Denk | 7385c28 | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1322 | printf("reading %s\n", filename); |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1323 | return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0, |
| 1324 | actread); |
Benoît Thébaudeau | 1170e63 | 2012-09-18 08:14:56 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1327 | 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] | 1328 | { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1329 | loff_t actread; |
| 1330 | int ret; |
| 1331 | |
| 1332 | ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread); |
| 1333 | if (ret) |
| 1334 | return ret; |
| 1335 | else |
| 1336 | return actread; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1337 | } |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1338 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1339 | int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len, |
| 1340 | loff_t *actread) |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1341 | { |
Suriyan Ramasami | 1ad0b98 | 2014-11-17 14:39:35 -0800 | [diff] [blame] | 1342 | int ret; |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1343 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1344 | ret = file_fat_read_at(filename, offset, buf, len, actread); |
| 1345 | if (ret) |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1346 | printf("** Unable to read file %s **\n", filename); |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1347 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 1348 | return ret; |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | void fat_close(void) |
| 1352 | { |
| 1353 | } |