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