wdenk | 7a428cc | 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 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <config.h> |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 30 | #include <exports.h> |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 31 | #include <fat.h> |
| 32 | #include <asm/byteorder.h> |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 33 | #include <part.h> |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 34 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 35 | /* |
| 36 | * Convert a string to lowercase. |
| 37 | */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 38 | static void downcase (char *str) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 39 | { |
| 40 | while (*str != '\0') { |
| 41 | TOLOWER(*str); |
| 42 | str++; |
| 43 | } |
| 44 | } |
| 45 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 46 | static block_dev_desc_t *cur_dev = NULL; |
| 47 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 48 | static unsigned long part_offset = 0; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 49 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 50 | static int cur_part = 1; |
| 51 | |
| 52 | #define DOS_PART_TBL_OFFSET 0x1be |
| 53 | #define DOS_PART_MAGIC_OFFSET 0x1fe |
| 54 | #define DOS_FS_TYPE_OFFSET 0x36 |
Wolfgang Denk | 7b2290c | 2010-07-19 11:36:57 +0200 | [diff] [blame] | 55 | #define DOS_FS32_TYPE_OFFSET 0x52 |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 56 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 57 | static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 58 | { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 59 | if (cur_dev == NULL) |
| 60 | return -1; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 61 | |
| 62 | startblock += part_offset; |
| 63 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 64 | if (cur_dev->block_read) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 65 | return cur_dev->block_read(cur_dev->dev, startblock, getsize, |
| 66 | (unsigned long *) bufptr); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 67 | } |
| 68 | return -1; |
| 69 | } |
| 70 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 71 | int fat_register_device (block_dev_desc_t * dev_desc, int part_no) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 72 | { |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 73 | unsigned char buffer[dev_desc->blksz]; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 74 | |
| 75 | if (!dev_desc->block_read) |
| 76 | return -1; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 77 | |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 78 | cur_dev = dev_desc; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 79 | /* check if we have a MBR (on floppies we have only a PBR) */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 80 | if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)buffer) != 1) { |
| 81 | printf("** Can't read from device %d **\n", |
| 82 | dev_desc->dev); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 83 | return -1; |
| 84 | } |
| 85 | if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 86 | buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 87 | /* no signature found */ |
| 88 | return -1; |
| 89 | } |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 90 | #if (defined(CONFIG_CMD_IDE) || \ |
unsik Kim | f0b1bdd | 2009-02-25 11:31:24 +0900 | [diff] [blame] | 91 | defined(CONFIG_CMD_MG_DISK) || \ |
Sonic Zhang | 853208e | 2008-12-09 23:20:18 -0500 | [diff] [blame] | 92 | defined(CONFIG_CMD_SATA) || \ |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 93 | defined(CONFIG_CMD_SCSI) || \ |
| 94 | defined(CONFIG_CMD_USB) || \ |
Andy Fleming | 55b8668 | 2008-01-09 13:51:32 -0600 | [diff] [blame] | 95 | defined(CONFIG_MMC) || \ |
| 96 | defined(CONFIG_SYSTEMACE) ) |
Wolfgang Denk | 8fff354 | 2011-11-04 15:55:15 +0000 | [diff] [blame] | 97 | { |
| 98 | disk_partition_t info; |
Andy Fleming | 55b8668 | 2008-01-09 13:51:32 -0600 | [diff] [blame] | 99 | |
Wolfgang Denk | 8fff354 | 2011-11-04 15:55:15 +0000 | [diff] [blame] | 100 | /* First we assume there is a MBR */ |
| 101 | if (!get_partition_info(dev_desc, part_no, &info)) { |
| 102 | part_offset = info.start; |
| 103 | cur_part = part_no; |
| 104 | } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], |
| 105 | "FAT", 3) == 0) || |
| 106 | (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], |
| 107 | "FAT32", 5) == 0)) { |
| 108 | /* ok, we assume we are on a PBR only */ |
| 109 | cur_part = 1; |
| 110 | part_offset = 0; |
| 111 | } else { |
| 112 | printf("** Partition %d not valid on device %d **\n", |
| 113 | part_no, dev_desc->dev); |
| 114 | return -1; |
| 115 | } |
| 116 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 117 | #else |
Wolfgang Denk | 7b2290c | 2010-07-19 11:36:57 +0200 | [diff] [blame] | 118 | if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) || |
| 119 | (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) { |
Andy Fleming | 55b8668 | 2008-01-09 13:51:32 -0600 | [diff] [blame] | 120 | /* ok, we assume we are on a PBR only */ |
| 121 | cur_part = 1; |
| 122 | part_offset = 0; |
Andy Fleming | 55b8668 | 2008-01-09 13:51:32 -0600 | [diff] [blame] | 123 | } else { |
| 124 | /* FIXME we need to determine the start block of the |
| 125 | * partition where the DOS FS resides. This can be done |
| 126 | * by using the get_partition_info routine. For this |
| 127 | * purpose the libpart must be included. |
| 128 | */ |
| 129 | part_offset = 32; |
| 130 | cur_part = 1; |
Wolfgang Denk | e78f258 | 2007-08-07 16:02:13 +0200 | [diff] [blame] | 131 | } |
Andy Fleming | 55b8668 | 2008-01-09 13:51:32 -0600 | [diff] [blame] | 132 | #endif |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Get the first occurence of a directory delimiter ('/' or '\') in a string. |
| 138 | * Return index into string if found, -1 otherwise. |
| 139 | */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 140 | static int dirdelim (char *str) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 141 | { |
| 142 | char *start = str; |
| 143 | |
| 144 | while (*str != '\0') { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 145 | if (ISDIRDELIM(*str)) |
| 146 | return str - start; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 147 | str++; |
| 148 | } |
| 149 | return -1; |
| 150 | } |
| 151 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 152 | /* |
| 153 | * Extract zero terminated short name from a directory entry. |
| 154 | */ |
| 155 | static void get_name (dir_entry *dirent, char *s_name) |
| 156 | { |
| 157 | char *ptr; |
| 158 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 159 | memcpy(s_name, dirent->name, 8); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 160 | s_name[8] = '\0'; |
| 161 | ptr = s_name; |
| 162 | while (*ptr && *ptr != ' ') |
| 163 | ptr++; |
| 164 | if (dirent->ext[0] && dirent->ext[0] != ' ') { |
| 165 | *ptr = '.'; |
| 166 | ptr++; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 167 | memcpy(ptr, dirent->ext, 3); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 168 | ptr[3] = '\0'; |
| 169 | while (*ptr && *ptr != ' ') |
| 170 | ptr++; |
| 171 | } |
| 172 | *ptr = '\0'; |
| 173 | if (*s_name == DELETED_FLAG) |
| 174 | *s_name = '\0'; |
| 175 | else if (*s_name == aRING) |
Remy Bohmer | 7c8f2ce | 2008-11-27 22:30:27 +0100 | [diff] [blame] | 176 | *s_name = DELETED_FLAG; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 177 | downcase(s_name); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Get the entry at index 'entry' in a FAT (12/16/32) table. |
| 182 | * On failure 0x00 is returned. |
| 183 | */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 184 | static __u32 get_fatent (fsdata *mydata, __u32 entry) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 185 | { |
| 186 | __u32 bufnum; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 187 | __u32 off16, offset; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 188 | __u32 ret = 0x00; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 189 | __u16 val1, val2; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 190 | |
| 191 | switch (mydata->fatsize) { |
| 192 | case 32: |
| 193 | bufnum = entry / FAT32BUFSIZE; |
| 194 | offset = entry - bufnum * FAT32BUFSIZE; |
| 195 | break; |
| 196 | case 16: |
| 197 | bufnum = entry / FAT16BUFSIZE; |
| 198 | offset = entry - bufnum * FAT16BUFSIZE; |
| 199 | break; |
| 200 | case 12: |
| 201 | bufnum = entry / FAT12BUFSIZE; |
| 202 | offset = entry - bufnum * FAT12BUFSIZE; |
| 203 | break; |
| 204 | |
| 205 | default: |
| 206 | /* Unsupported FAT size */ |
| 207 | return ret; |
| 208 | } |
| 209 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 210 | debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n", |
| 211 | mydata->fatsize, entry, entry, offset, offset); |
Wolfgang Denk | a48d0a3 | 2010-07-19 11:36:58 +0200 | [diff] [blame] | 212 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 213 | /* Read a new block of FAT entries into the cache. */ |
| 214 | if (bufnum != mydata->fatbufnum) { |
Sergei Shtylyov | d591653 | 2011-08-08 09:39:29 +0000 | [diff] [blame] | 215 | __u32 getsize = FATBUFBLOCKS; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 216 | __u8 *bufptr = mydata->fatbuf; |
| 217 | __u32 fatlength = mydata->fatlength; |
| 218 | __u32 startblock = bufnum * FATBUFBLOCKS; |
| 219 | |
Sergei Shtylyov | d591653 | 2011-08-08 09:39:29 +0000 | [diff] [blame] | 220 | if (getsize > fatlength) |
| 221 | getsize = fatlength; |
| 222 | |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 223 | fatlength *= mydata->sect_size; /* We want it in bytes now */ |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 224 | startblock += mydata->fat_sect; /* Offset from start of disk */ |
| 225 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 226 | if (disk_read(startblock, getsize, bufptr) < 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 227 | debug("Error reading FAT blocks\n"); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 228 | return ret; |
| 229 | } |
| 230 | mydata->fatbufnum = bufnum; |
| 231 | } |
| 232 | |
| 233 | /* Get the actual entry from the table */ |
| 234 | switch (mydata->fatsize) { |
| 235 | case 32: |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 236 | ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 237 | break; |
| 238 | case 16: |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 239 | ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 240 | break; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 241 | case 12: |
| 242 | off16 = (offset * 3) / 4; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 243 | |
| 244 | switch (offset & 0x3) { |
| 245 | case 0: |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 246 | ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 247 | ret &= 0xfff; |
| 248 | break; |
| 249 | case 1: |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 250 | val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 251 | val1 &= 0xf000; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 252 | val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 253 | val2 &= 0x00ff; |
| 254 | ret = (val2 << 4) | (val1 >> 12); |
| 255 | break; |
| 256 | case 2: |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 257 | val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 258 | val1 &= 0xff00; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 259 | val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 260 | val2 &= 0x000f; |
| 261 | ret = (val2 << 8) | (val1 >> 8); |
| 262 | break; |
| 263 | case 3: |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 264 | ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 265 | ret = (ret & 0xfff0) >> 4; |
| 266 | break; |
| 267 | default: |
| 268 | break; |
| 269 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 270 | break; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 271 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 272 | debug("FAT%d: ret: %08x, offset: %04x\n", |
| 273 | mydata->fatsize, ret, offset); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 278 | /* |
| 279 | * Read at most 'size' bytes from the specified cluster into 'buffer'. |
| 280 | * Return 0 on success, -1 otherwise. |
| 281 | */ |
| 282 | static int |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 283 | get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer, |
| 284 | unsigned long size) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 285 | { |
Erik Hansen | 4ea8966 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 286 | __u32 idx = 0; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 287 | __u32 startsect; |
| 288 | |
| 289 | if (clustnum > 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 290 | startsect = mydata->data_begin + |
| 291 | clustnum * mydata->clust_size; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 292 | } else { |
| 293 | startsect = mydata->rootdir_sect; |
| 294 | } |
| 295 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 296 | debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect); |
| 297 | |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 298 | if (disk_read(startsect, size / mydata->sect_size, buffer) < 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 299 | debug("Error reading data\n"); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 300 | return -1; |
| 301 | } |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 302 | if (size % mydata->sect_size) { |
| 303 | __u8 tmpbuf[mydata->sect_size]; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 304 | |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 305 | idx = size / mydata->sect_size; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 306 | if (disk_read(startsect + idx, 1, tmpbuf) < 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 307 | debug("Error reading data\n"); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 308 | return -1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 309 | } |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 310 | buffer += idx * mydata->sect_size; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 311 | |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 312 | memcpy(buffer, tmpbuf, size % mydata->sect_size); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 313 | return 0; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 319 | /* |
| 320 | * Read at most 'maxsize' bytes from the file associated with 'dentptr' |
| 321 | * into 'buffer'. |
| 322 | * Return the number of bytes read or -1 on fatal errors. |
| 323 | */ |
| 324 | static long |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 325 | get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer, |
| 326 | unsigned long maxsize) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 327 | { |
| 328 | unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0; |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 329 | unsigned int bytesperclust = mydata->clust_size * mydata->sect_size; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 330 | __u32 curclust = START(dentptr); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 331 | __u32 endclust, newclust; |
| 332 | unsigned long actsize; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 333 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 334 | debug("Filesize: %ld bytes\n", filesize); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 335 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 336 | if (maxsize > 0 && filesize > maxsize) |
| 337 | filesize = maxsize; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 338 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 339 | debug("%ld bytes\n", filesize); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 340 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 341 | actsize = bytesperclust; |
| 342 | endclust = curclust; |
| 343 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 344 | do { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 345 | /* search for consecutive clusters */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 346 | while (actsize < filesize) { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 347 | newclust = get_fatent(mydata, endclust); |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 348 | if ((newclust - 1) != endclust) |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 349 | goto getit; |
michael | 2c4d298 | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 350 | if (CHECK_CLUST(newclust, mydata->fatsize)) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 351 | debug("curclust: 0x%x\n", newclust); |
| 352 | debug("Invalid FAT entry\n"); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 353 | return gotsize; |
| 354 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 355 | endclust = newclust; |
| 356 | actsize += bytesperclust; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 357 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 358 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 359 | /* actsize >= file size */ |
| 360 | actsize -= bytesperclust; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 361 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 362 | /* get remaining clusters */ |
| 363 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 364 | printf("Error reading cluster\n"); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 365 | return -1; |
| 366 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 367 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 368 | /* get remaining bytes */ |
| 369 | gotsize += (int)actsize; |
| 370 | filesize -= actsize; |
| 371 | buffer += actsize; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 372 | actsize = filesize; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 373 | if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 374 | printf("Error reading cluster\n"); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 375 | return -1; |
| 376 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 377 | gotsize += actsize; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 378 | return gotsize; |
| 379 | getit: |
| 380 | if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 381 | printf("Error reading cluster\n"); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 382 | return -1; |
| 383 | } |
| 384 | gotsize += (int)actsize; |
| 385 | filesize -= actsize; |
| 386 | buffer += actsize; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 387 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 388 | curclust = get_fatent(mydata, endclust); |
michael | 2c4d298 | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 389 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 390 | debug("curclust: 0x%x\n", curclust); |
| 391 | printf("Invalid FAT entry\n"); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 392 | return gotsize; |
| 393 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 394 | actsize = bytesperclust; |
| 395 | endclust = curclust; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 396 | } while (1); |
| 397 | } |
| 398 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 399 | #ifdef CONFIG_SUPPORT_VFAT |
| 400 | /* |
| 401 | * Extract the file name information from 'slotptr' into 'l_name', |
| 402 | * starting at l_name[*idx]. |
| 403 | * Return 1 if terminator (zero byte) is found, 0 otherwise. |
| 404 | */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 405 | static int slot2str (dir_slot *slotptr, char *l_name, int *idx) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 406 | { |
| 407 | int j; |
| 408 | |
| 409 | for (j = 0; j <= 8; j += 2) { |
| 410 | l_name[*idx] = slotptr->name0_4[j]; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 411 | if (l_name[*idx] == 0x00) |
| 412 | return 1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 413 | (*idx)++; |
| 414 | } |
| 415 | for (j = 0; j <= 10; j += 2) { |
| 416 | l_name[*idx] = slotptr->name5_10[j]; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 417 | if (l_name[*idx] == 0x00) |
| 418 | return 1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 419 | (*idx)++; |
| 420 | } |
| 421 | for (j = 0; j <= 2; j += 2) { |
| 422 | l_name[*idx] = slotptr->name11_12[j]; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 423 | if (l_name[*idx] == 0x00) |
| 424 | return 1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 425 | (*idx)++; |
| 426 | } |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 431 | /* |
| 432 | * Extract the full long filename starting at 'retdent' (which is really |
| 433 | * a slot) into 'l_name'. If successful also copy the real directory entry |
| 434 | * into 'retdent' |
| 435 | * Return 0 on success, -1 otherwise. |
| 436 | */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 437 | __attribute__ ((__aligned__ (__alignof__ (dir_entry)))) |
Bryan Wu | 95f99a4 | 2009-01-02 20:47:45 -0500 | [diff] [blame] | 438 | __u8 get_vfatname_block[MAX_CLUSTSIZE]; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 439 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 440 | static int |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 441 | get_vfatname (fsdata *mydata, int curclust, __u8 *cluster, |
| 442 | dir_entry *retdent, char *l_name) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 443 | { |
| 444 | dir_entry *realdent; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 445 | dir_slot *slotptr = (dir_slot *)retdent; |
Sergei Shtylyov | 51de6cf | 2011-08-19 09:37:46 +0000 | [diff] [blame] | 446 | __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ? |
| 447 | PREFETCH_BLOCKS : |
| 448 | mydata->clust_size); |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 449 | __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 450 | int idx = 0; |
| 451 | |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 452 | if (counter > VFAT_MAXSEQ) { |
| 453 | debug("Error: VFAT name is too long\n"); |
| 454 | return -1; |
| 455 | } |
| 456 | |
| 457 | while ((__u8 *)slotptr < buflimit) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 458 | if (counter == 0) |
| 459 | break; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 460 | if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter) |
| 461 | return -1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 462 | slotptr++; |
| 463 | counter--; |
| 464 | } |
| 465 | |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 466 | if ((__u8 *)slotptr >= buflimit) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 467 | dir_slot *slotptr2; |
| 468 | |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 469 | if (curclust == 0) |
| 470 | return -1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 471 | curclust = get_fatent(mydata, curclust); |
michael | 2c4d298 | 2008-03-02 23:33:46 +0100 | [diff] [blame] | 472 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 473 | debug("curclust: 0x%x\n", curclust); |
| 474 | printf("Invalid FAT entry\n"); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 475 | return -1; |
| 476 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 477 | |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 478 | if (get_cluster(mydata, curclust, get_vfatname_block, |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 479 | mydata->clust_size * mydata->sect_size) != 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 480 | debug("Error: reading directory block\n"); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 481 | return -1; |
| 482 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 483 | |
| 484 | slotptr2 = (dir_slot *)get_vfatname_block; |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 485 | while (counter > 0) { |
| 486 | if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK) |
| 487 | & 0xff) != counter) |
| 488 | return -1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 489 | slotptr2++; |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 490 | counter--; |
| 491 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 492 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 493 | /* Save the real directory entry */ |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 494 | realdent = (dir_entry *)slotptr2; |
| 495 | while ((__u8 *)slotptr2 > get_vfatname_block) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 496 | slotptr2--; |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 497 | slot2str(slotptr2, l_name, &idx); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 498 | } |
| 499 | } else { |
| 500 | /* Save the real directory entry */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 501 | realdent = (dir_entry *)slotptr; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | do { |
| 505 | slotptr--; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 506 | if (slot2str(slotptr, l_name, &idx)) |
| 507 | break; |
wdenk | 2ebee31 | 2004-02-23 19:30:57 +0000 | [diff] [blame] | 508 | } while (!(slotptr->id & LAST_LONG_ENTRY_MASK)); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 509 | |
| 510 | l_name[idx] = '\0'; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 511 | if (*l_name == DELETED_FLAG) |
| 512 | *l_name = '\0'; |
| 513 | else if (*l_name == aRING) |
| 514 | *l_name = DELETED_FLAG; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 515 | downcase(l_name); |
| 516 | |
| 517 | /* Return the real directory entry */ |
| 518 | memcpy(retdent, realdent, sizeof(dir_entry)); |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 523 | /* Calculate short name checksum */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 524 | static __u8 mkcksum (const char *str) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 525 | { |
| 526 | int i; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 527 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 528 | __u8 ret = 0; |
| 529 | |
| 530 | for (i = 0; i < 11; i++) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 531 | ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i]; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return ret; |
| 535 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 536 | #endif /* CONFIG_SUPPORT_VFAT */ |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | * Get the directory entry associated with 'filename' from the directory |
| 540 | * starting at 'startsect' |
| 541 | */ |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 542 | __attribute__ ((__aligned__ (__alignof__ (dir_entry)))) |
wdenk | b6c60cb3 | 2003-10-29 23:18:55 +0000 | [diff] [blame] | 543 | __u8 get_dentfromdir_block[MAX_CLUSTSIZE]; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 544 | |
| 545 | static dir_entry *get_dentfromdir (fsdata *mydata, int startsect, |
| 546 | char *filename, dir_entry *retdent, |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 547 | int dols) |
| 548 | { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 549 | __u16 prevcksum = 0xffff; |
| 550 | __u32 curclust = START(retdent); |
| 551 | int files = 0, dirs = 0; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 552 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 553 | debug("get_dentfromdir: %s\n", filename); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 554 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 555 | while (1) { |
| 556 | dir_entry *dentptr; |
| 557 | |
| 558 | int i; |
| 559 | |
| 560 | if (get_cluster(mydata, curclust, get_dentfromdir_block, |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 561 | mydata->clust_size * mydata->sect_size) != 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 562 | debug("Error: reading directory block\n"); |
| 563 | return NULL; |
| 564 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 565 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 566 | dentptr = (dir_entry *)get_dentfromdir_block; |
| 567 | |
| 568 | for (i = 0; i < DIRENTSPERCLUST; i++) { |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 569 | char s_name[14], l_name[VFAT_MAXLEN_BYTES]; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 570 | |
| 571 | l_name[0] = '\0'; |
| 572 | if (dentptr->name[0] == DELETED_FLAG) { |
| 573 | dentptr++; |
| 574 | continue; |
| 575 | } |
| 576 | if ((dentptr->attr & ATTR_VOLUME)) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 577 | #ifdef CONFIG_SUPPORT_VFAT |
J. Vijayanand | 575e21e | 2011-10-19 07:43:08 +0000 | [diff] [blame] | 578 | if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT && |
| 579 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 580 | prevcksum = ((dir_slot *)dentptr)->alias_checksum; |
| 581 | get_vfatname(mydata, curclust, |
| 582 | get_dentfromdir_block, |
| 583 | dentptr, l_name); |
| 584 | if (dols) { |
| 585 | int isdir; |
| 586 | char dirc; |
| 587 | int doit = 0; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 588 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 589 | isdir = (dentptr->attr & ATTR_DIR); |
| 590 | |
| 591 | if (isdir) { |
| 592 | dirs++; |
| 593 | dirc = '/'; |
| 594 | doit = 1; |
| 595 | } else { |
| 596 | dirc = ' '; |
| 597 | if (l_name[0] != 0) { |
| 598 | files++; |
| 599 | doit = 1; |
| 600 | } |
| 601 | } |
| 602 | if (doit) { |
| 603 | if (dirc == ' ') { |
| 604 | printf(" %8ld %s%c\n", |
| 605 | (long)FAT2CPU32(dentptr->size), |
| 606 | l_name, |
| 607 | dirc); |
| 608 | } else { |
| 609 | printf(" %s%c\n", |
| 610 | l_name, |
| 611 | dirc); |
| 612 | } |
| 613 | } |
| 614 | dentptr++; |
| 615 | continue; |
| 616 | } |
| 617 | debug("vfatname: |%s|\n", l_name); |
| 618 | } else |
| 619 | #endif |
| 620 | { |
| 621 | /* Volume label or VFAT entry */ |
| 622 | dentptr++; |
| 623 | continue; |
| 624 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 625 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 626 | if (dentptr->name[0] == 0) { |
| 627 | if (dols) { |
| 628 | printf("\n%d file(s), %d dir(s)\n\n", |
| 629 | files, dirs); |
| 630 | } |
| 631 | debug("Dentname == NULL - %d\n", i); |
| 632 | return NULL; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 633 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 634 | #ifdef CONFIG_SUPPORT_VFAT |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 635 | if (dols && mkcksum(dentptr->name) == prevcksum) { |
| 636 | dentptr++; |
| 637 | continue; |
| 638 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 639 | #endif |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 640 | get_name(dentptr, s_name); |
| 641 | if (dols) { |
| 642 | int isdir = (dentptr->attr & ATTR_DIR); |
| 643 | char dirc; |
| 644 | int doit = 0; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 645 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 646 | if (isdir) { |
| 647 | dirs++; |
| 648 | dirc = '/'; |
| 649 | doit = 1; |
| 650 | } else { |
| 651 | dirc = ' '; |
| 652 | if (s_name[0] != 0) { |
| 653 | files++; |
| 654 | doit = 1; |
| 655 | } |
| 656 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 657 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 658 | if (doit) { |
| 659 | if (dirc == ' ') { |
| 660 | printf(" %8ld %s%c\n", |
| 661 | (long)FAT2CPU32(dentptr->size), |
| 662 | s_name, dirc); |
| 663 | } else { |
| 664 | printf(" %s%c\n", |
| 665 | s_name, dirc); |
| 666 | } |
| 667 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 668 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 669 | dentptr++; |
| 670 | continue; |
| 671 | } |
| 672 | |
| 673 | if (strcmp(filename, s_name) |
| 674 | && strcmp(filename, l_name)) { |
| 675 | debug("Mismatch: |%s|%s|\n", s_name, l_name); |
| 676 | dentptr++; |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | memcpy(retdent, dentptr, sizeof(dir_entry)); |
| 681 | |
| 682 | debug("DentName: %s", s_name); |
| 683 | debug(", start: 0x%x", START(dentptr)); |
| 684 | debug(", size: 0x%x %s\n", |
| 685 | FAT2CPU32(dentptr->size), |
| 686 | (dentptr->attr & ATTR_DIR) ? "(DIR)" : ""); |
| 687 | |
| 688 | return retdent; |
| 689 | } |
| 690 | |
| 691 | curclust = get_fatent(mydata, curclust); |
| 692 | if (CHECK_CLUST(curclust, mydata->fatsize)) { |
| 693 | debug("curclust: 0x%x\n", curclust); |
| 694 | printf("Invalid FAT entry\n"); |
| 695 | return NULL; |
| 696 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 697 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 698 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 699 | return NULL; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 700 | } |
| 701 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 702 | /* |
| 703 | * Read boot sector and volume info from a FAT filesystem |
| 704 | */ |
| 705 | static int |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 706 | read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 707 | { |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 708 | __u8 *block; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 709 | volume_info *vistart; |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 710 | int ret = 0; |
| 711 | |
| 712 | if (cur_dev == NULL) { |
| 713 | debug("Error: no device selected\n"); |
| 714 | return -1; |
| 715 | } |
| 716 | |
| 717 | block = malloc(cur_dev->blksz); |
| 718 | if (block == NULL) { |
| 719 | debug("Error: allocating block\n"); |
| 720 | return -1; |
| 721 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 722 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 723 | if (disk_read (0, 1, block) < 0) { |
| 724 | debug("Error: reading block\n"); |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 725 | goto fail; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | memcpy(bs, block, sizeof(boot_sector)); |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 729 | bs->reserved = FAT2CPU16(bs->reserved); |
| 730 | bs->fat_length = FAT2CPU16(bs->fat_length); |
| 731 | bs->secs_track = FAT2CPU16(bs->secs_track); |
| 732 | bs->heads = FAT2CPU16(bs->heads); |
| 733 | bs->total_sect = FAT2CPU32(bs->total_sect); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 734 | |
| 735 | /* FAT32 entries */ |
| 736 | if (bs->fat_length == 0) { |
| 737 | /* Assume FAT32 */ |
| 738 | bs->fat32_length = FAT2CPU32(bs->fat32_length); |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 739 | bs->flags = FAT2CPU16(bs->flags); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 740 | bs->root_cluster = FAT2CPU32(bs->root_cluster); |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 741 | bs->info_sector = FAT2CPU16(bs->info_sector); |
| 742 | bs->backup_boot = FAT2CPU16(bs->backup_boot); |
| 743 | vistart = (volume_info *)(block + sizeof(boot_sector)); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 744 | *fatsize = 32; |
| 745 | } else { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 746 | vistart = (volume_info *)&(bs->fat32_length); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 747 | *fatsize = 0; |
| 748 | } |
| 749 | memcpy(volinfo, vistart, sizeof(volume_info)); |
| 750 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 751 | if (*fatsize == 32) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 752 | if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 753 | goto exit; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 754 | } else { |
Tom Rix | 155abaa | 2009-05-20 07:55:41 -0500 | [diff] [blame] | 755 | if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 756 | *fatsize = 12; |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 757 | goto exit; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 758 | } |
Tom Rix | 155abaa | 2009-05-20 07:55:41 -0500 | [diff] [blame] | 759 | if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 760 | *fatsize = 16; |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 761 | goto exit; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 765 | debug("Error: broken fs_type sign\n"); |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 766 | fail: |
| 767 | ret = -1; |
| 768 | exit: |
| 769 | free(block); |
| 770 | return ret; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 773 | __attribute__ ((__aligned__ (__alignof__ (dir_entry)))) |
Bryan Wu | 95f99a4 | 2009-01-02 20:47:45 -0500 | [diff] [blame] | 774 | __u8 do_fat_read_block[MAX_CLUSTSIZE]; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 775 | |
stroese | 8308224 | 2004-12-16 17:57:26 +0000 | [diff] [blame] | 776 | long |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 777 | do_fat_read (const char *filename, void *buffer, unsigned long maxsize, |
| 778 | int dols) |
| 779 | { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 780 | char fnamecopy[2048]; |
| 781 | boot_sector bs; |
| 782 | volume_info volinfo; |
| 783 | fsdata datablock; |
| 784 | fsdata *mydata = &datablock; |
| 785 | dir_entry *dentptr; |
| 786 | __u16 prevcksum = 0xffff; |
| 787 | char *subname = ""; |
Erik Hansen | 4ea8966 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 788 | __u32 cursect; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 789 | int idx, isdir = 0; |
| 790 | int files = 0, dirs = 0; |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 791 | long ret = -1; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 792 | int firsttime; |
Sergei Shtylyov | 42cdc9f | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 793 | __u32 root_cluster = 0; |
Erik Hansen | 4ea8966 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 794 | int rootdir_size = 0; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 795 | int j; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 796 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 797 | if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) { |
| 798 | debug("Error: reading boot sector\n"); |
| 799 | return -1; |
| 800 | } |
| 801 | |
Sergei Shtylyov | 42cdc9f | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 802 | if (mydata->fatsize == 32) { |
| 803 | root_cluster = bs.root_cluster; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 804 | mydata->fatlength = bs.fat32_length; |
Sergei Shtylyov | 42cdc9f | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 805 | } else { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 806 | mydata->fatlength = bs.fat_length; |
Sergei Shtylyov | 42cdc9f | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 807 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 808 | |
| 809 | mydata->fat_sect = bs.reserved; |
| 810 | |
| 811 | cursect = mydata->rootdir_sect |
| 812 | = mydata->fat_sect + mydata->fatlength * bs.fats; |
| 813 | |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 814 | mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0]; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 815 | mydata->clust_size = bs.cluster_size; |
Wolfgang Denk | a48d0a3 | 2010-07-19 11:36:58 +0200 | [diff] [blame] | 816 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 817 | if (mydata->fatsize == 32) { |
| 818 | mydata->data_begin = mydata->rootdir_sect - |
| 819 | (mydata->clust_size * 2); |
| 820 | } else { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 821 | rootdir_size = ((bs.dir_entries[1] * (int)256 + |
| 822 | bs.dir_entries[0]) * |
| 823 | sizeof(dir_entry)) / |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 824 | mydata->sect_size; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 825 | mydata->data_begin = mydata->rootdir_sect + |
| 826 | rootdir_size - |
| 827 | (mydata->clust_size * 2); |
| 828 | } |
| 829 | |
| 830 | mydata->fatbufnum = -1; |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 831 | mydata->fatbuf = malloc(FATBUFSIZE); |
| 832 | if (mydata->fatbuf == NULL) { |
| 833 | debug("Error: allocating memory\n"); |
| 834 | return -1; |
| 835 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 836 | |
Wolfgang Denk | a48d0a3 | 2010-07-19 11:36:58 +0200 | [diff] [blame] | 837 | #ifdef CONFIG_SUPPORT_VFAT |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 838 | debug("VFAT Support enabled\n"); |
Wolfgang Denk | a48d0a3 | 2010-07-19 11:36:58 +0200 | [diff] [blame] | 839 | #endif |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 840 | debug("FAT%d, fat_sect: %d, fatlength: %d\n", |
| 841 | mydata->fatsize, mydata->fat_sect, mydata->fatlength); |
| 842 | debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n" |
| 843 | "Data begins at: %d\n", |
| 844 | root_cluster, |
| 845 | mydata->rootdir_sect, |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 846 | mydata->rootdir_sect * mydata->sect_size, mydata->data_begin); |
| 847 | debug("Sector size: %d, cluster size: %d\n", mydata->sect_size, |
| 848 | mydata->clust_size); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 849 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 850 | /* "cwd" is always the root... */ |
| 851 | while (ISDIRDELIM(*filename)) |
| 852 | filename++; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 853 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 854 | /* Make a copy of the filename and convert it to lowercase */ |
| 855 | strcpy(fnamecopy, filename); |
| 856 | downcase(fnamecopy); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 857 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 858 | if (*fnamecopy == '\0') { |
| 859 | if (!dols) |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 860 | goto exit; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 861 | |
| 862 | dols = LS_ROOT; |
| 863 | } else if ((idx = dirdelim(fnamecopy)) >= 0) { |
| 864 | isdir = 1; |
| 865 | fnamecopy[idx] = '\0'; |
| 866 | subname = fnamecopy + idx + 1; |
| 867 | |
| 868 | /* Handle multiple delimiters */ |
| 869 | while (ISDIRDELIM(*subname)) |
| 870 | subname++; |
| 871 | } else if (dols) { |
| 872 | isdir = 1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 873 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 874 | |
| 875 | j = 0; |
| 876 | while (1) { |
| 877 | int i; |
| 878 | |
| 879 | debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%d\n", |
| 880 | cursect, mydata->clust_size, DIRENTSPERBLOCK); |
| 881 | |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 882 | if (disk_read(cursect, |
| 883 | (mydata->fatsize == 32) ? |
| 884 | (mydata->clust_size) : |
Sergei Shtylyov | 51de6cf | 2011-08-19 09:37:46 +0000 | [diff] [blame] | 885 | PREFETCH_BLOCKS, |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 886 | do_fat_read_block) < 0) { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 887 | debug("Error: reading rootdir block\n"); |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 888 | goto exit; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | dentptr = (dir_entry *) do_fat_read_block; |
| 892 | |
| 893 | for (i = 0; i < DIRENTSPERBLOCK; i++) { |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 894 | char s_name[14], l_name[VFAT_MAXLEN_BYTES]; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 895 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 896 | l_name[0] = '\0'; |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 897 | if (dentptr->name[0] == DELETED_FLAG) { |
| 898 | dentptr++; |
| 899 | continue; |
| 900 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 901 | if ((dentptr->attr & ATTR_VOLUME)) { |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 902 | #ifdef CONFIG_SUPPORT_VFAT |
J. Vijayanand | 575e21e | 2011-10-19 07:43:08 +0000 | [diff] [blame] | 903 | if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT && |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 904 | (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) { |
| 905 | prevcksum = |
| 906 | ((dir_slot *)dentptr)->alias_checksum; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 907 | |
Mikhail Zolotaryov | 45d513e | 2010-09-08 17:06:03 +0300 | [diff] [blame] | 908 | get_vfatname(mydata, |
Sergei Shtylyov | 42cdc9f | 2011-08-19 09:32:34 +0000 | [diff] [blame] | 909 | root_cluster, |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 910 | do_fat_read_block, |
| 911 | dentptr, l_name); |
| 912 | |
| 913 | if (dols == LS_ROOT) { |
| 914 | char dirc; |
| 915 | int doit = 0; |
| 916 | int isdir = |
| 917 | (dentptr->attr & ATTR_DIR); |
| 918 | |
| 919 | if (isdir) { |
| 920 | dirs++; |
| 921 | dirc = '/'; |
| 922 | doit = 1; |
| 923 | } else { |
| 924 | dirc = ' '; |
| 925 | if (l_name[0] != 0) { |
| 926 | files++; |
| 927 | doit = 1; |
| 928 | } |
| 929 | } |
| 930 | if (doit) { |
| 931 | if (dirc == ' ') { |
| 932 | printf(" %8ld %s%c\n", |
| 933 | (long)FAT2CPU32(dentptr->size), |
| 934 | l_name, |
| 935 | dirc); |
| 936 | } else { |
| 937 | printf(" %s%c\n", |
| 938 | l_name, |
| 939 | dirc); |
| 940 | } |
| 941 | } |
| 942 | dentptr++; |
| 943 | continue; |
| 944 | } |
| 945 | debug("Rootvfatname: |%s|\n", |
| 946 | l_name); |
| 947 | } else |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 948 | #endif |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 949 | { |
| 950 | /* Volume label or VFAT entry */ |
| 951 | dentptr++; |
| 952 | continue; |
| 953 | } |
| 954 | } else if (dentptr->name[0] == 0) { |
| 955 | debug("RootDentname == NULL - %d\n", i); |
| 956 | if (dols == LS_ROOT) { |
| 957 | printf("\n%d file(s), %d dir(s)\n\n", |
| 958 | files, dirs); |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 959 | ret = 0; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 960 | } |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 961 | goto exit; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 962 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 963 | #ifdef CONFIG_SUPPORT_VFAT |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 964 | else if (dols == LS_ROOT && |
| 965 | mkcksum(dentptr->name) == prevcksum) { |
| 966 | dentptr++; |
| 967 | continue; |
| 968 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 969 | #endif |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 970 | get_name(dentptr, s_name); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 971 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 972 | if (dols == LS_ROOT) { |
| 973 | int isdir = (dentptr->attr & ATTR_DIR); |
| 974 | char dirc; |
| 975 | int doit = 0; |
| 976 | |
| 977 | if (isdir) { |
| 978 | dirc = '/'; |
| 979 | if (s_name[0] != 0) { |
| 980 | dirs++; |
| 981 | doit = 1; |
| 982 | } |
| 983 | } else { |
| 984 | dirc = ' '; |
| 985 | if (s_name[0] != 0) { |
| 986 | files++; |
| 987 | doit = 1; |
| 988 | } |
| 989 | } |
| 990 | if (doit) { |
| 991 | if (dirc == ' ') { |
| 992 | printf(" %8ld %s%c\n", |
| 993 | (long)FAT2CPU32(dentptr->size), |
| 994 | s_name, dirc); |
| 995 | } else { |
| 996 | printf(" %s%c\n", |
| 997 | s_name, dirc); |
| 998 | } |
| 999 | } |
| 1000 | dentptr++; |
| 1001 | continue; |
| 1002 | } |
| 1003 | |
| 1004 | if (strcmp(fnamecopy, s_name) |
| 1005 | && strcmp(fnamecopy, l_name)) { |
| 1006 | debug("RootMismatch: |%s|%s|\n", s_name, |
| 1007 | l_name); |
| 1008 | dentptr++; |
| 1009 | continue; |
| 1010 | } |
| 1011 | |
| 1012 | if (isdir && !(dentptr->attr & ATTR_DIR)) |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1013 | goto exit; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1014 | |
| 1015 | debug("RootName: %s", s_name); |
| 1016 | debug(", start: 0x%x", START(dentptr)); |
| 1017 | debug(", size: 0x%x %s\n", |
| 1018 | FAT2CPU32(dentptr->size), |
| 1019 | isdir ? "(DIR)" : ""); |
| 1020 | |
| 1021 | goto rootdir_done; /* We got a match */ |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1022 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1023 | debug("END LOOP: j=%d clust_size=%d\n", j, |
| 1024 | mydata->clust_size); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1025 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1026 | /* |
| 1027 | * On FAT32 we must fetch the FAT entries for the next |
| 1028 | * root directory clusters when a cluster has been |
| 1029 | * completely processed. |
| 1030 | */ |
Erik Hansen | 4ea8966 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 1031 | ++j; |
| 1032 | int fat32_end = 0; |
| 1033 | if ((mydata->fatsize == 32) && (j == mydata->clust_size)) { |
| 1034 | int nxtsect = 0; |
| 1035 | int nxt_clust = 0; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1036 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1037 | nxt_clust = get_fatent(mydata, root_cluster); |
Erik Hansen | 4ea8966 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 1038 | fat32_end = CHECK_CLUST(nxt_clust, 32); |
Wolfgang Denk | a48d0a3 | 2010-07-19 11:36:58 +0200 | [diff] [blame] | 1039 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1040 | nxtsect = mydata->data_begin + |
| 1041 | (nxt_clust * mydata->clust_size); |
Wolfgang Denk | a48d0a3 | 2010-07-19 11:36:58 +0200 | [diff] [blame] | 1042 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1043 | root_cluster = nxt_clust; |
| 1044 | |
| 1045 | cursect = nxtsect; |
| 1046 | j = 0; |
| 1047 | } else { |
| 1048 | cursect++; |
| 1049 | } |
Erik Hansen | 4ea8966 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 1050 | |
| 1051 | /* If end of rootdir reached */ |
| 1052 | if ((mydata->fatsize == 32 && fat32_end) || |
| 1053 | (mydata->fatsize != 32 && j == rootdir_size)) { |
| 1054 | if (dols == LS_ROOT) { |
| 1055 | printf("\n%d file(s), %d dir(s)\n\n", |
| 1056 | files, dirs); |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1057 | ret = 0; |
Erik Hansen | 4ea8966 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 1058 | } |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1059 | goto exit; |
Erik Hansen | 4ea8966 | 2011-03-24 10:15:37 +0100 | [diff] [blame] | 1060 | } |
Wolfgang Denk | a48d0a3 | 2010-07-19 11:36:58 +0200 | [diff] [blame] | 1061 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1062 | rootdir_done: |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1063 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1064 | firsttime = 1; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1065 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1066 | while (isdir) { |
| 1067 | int startsect = mydata->data_begin |
| 1068 | + START(dentptr) * mydata->clust_size; |
| 1069 | dir_entry dent; |
| 1070 | char *nextname = NULL; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1071 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1072 | dent = *dentptr; |
| 1073 | dentptr = &dent; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1074 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1075 | idx = dirdelim(subname); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1076 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1077 | if (idx >= 0) { |
| 1078 | subname[idx] = '\0'; |
| 1079 | nextname = subname + idx + 1; |
| 1080 | /* Handle multiple delimiters */ |
| 1081 | while (ISDIRDELIM(*nextname)) |
| 1082 | nextname++; |
| 1083 | if (dols && *nextname == '\0') |
| 1084 | firsttime = 0; |
| 1085 | } else { |
| 1086 | if (dols && firsttime) { |
| 1087 | firsttime = 0; |
| 1088 | } else { |
| 1089 | isdir = 0; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | if (get_dentfromdir(mydata, startsect, subname, dentptr, |
| 1094 | isdir ? 0 : dols) == NULL) { |
| 1095 | if (dols && !isdir) |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1096 | ret = 0; |
| 1097 | goto exit; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | if (idx >= 0) { |
| 1101 | if (!(dentptr->attr & ATTR_DIR)) |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1102 | goto exit; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1103 | subname = nextname; |
| 1104 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1105 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1106 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1107 | ret = get_contents(mydata, dentptr, buffer, maxsize); |
| 1108 | debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1109 | |
Sergei Shtylyov | 5ab3ba7 | 2011-08-08 09:38:33 +0000 | [diff] [blame] | 1110 | exit: |
| 1111 | free(mydata->fatbuf); |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1112 | return ret; |
| 1113 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1114 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1115 | int file_fat_detectfs (void) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1116 | { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1117 | boot_sector bs; |
| 1118 | volume_info volinfo; |
| 1119 | int fatsize; |
| 1120 | char vol_label[12]; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1121 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1122 | if (cur_dev == NULL) { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1123 | printf("No current device\n"); |
| 1124 | return 1; |
| 1125 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1126 | |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 1127 | #if defined(CONFIG_CMD_IDE) || \ |
unsik Kim | f0b1bdd | 2009-02-25 11:31:24 +0900 | [diff] [blame] | 1128 | defined(CONFIG_CMD_MG_DISK) || \ |
Sonic Zhang | 853208e | 2008-12-09 23:20:18 -0500 | [diff] [blame] | 1129 | defined(CONFIG_CMD_SATA) || \ |
Jon Loeliger | 80eb47c | 2007-07-09 17:56:50 -0500 | [diff] [blame] | 1130 | defined(CONFIG_CMD_SCSI) || \ |
| 1131 | defined(CONFIG_CMD_USB) || \ |
Andy Fleming | 1efe578 | 2008-01-16 13:06:59 -0600 | [diff] [blame] | 1132 | defined(CONFIG_MMC) |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1133 | printf("Interface: "); |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1134 | switch (cur_dev->if_type) { |
| 1135 | case IF_TYPE_IDE: |
| 1136 | printf("IDE"); |
| 1137 | break; |
| 1138 | case IF_TYPE_SATA: |
| 1139 | printf("SATA"); |
| 1140 | break; |
| 1141 | case IF_TYPE_SCSI: |
| 1142 | printf("SCSI"); |
| 1143 | break; |
| 1144 | case IF_TYPE_ATAPI: |
| 1145 | printf("ATAPI"); |
| 1146 | break; |
| 1147 | case IF_TYPE_USB: |
| 1148 | printf("USB"); |
| 1149 | break; |
| 1150 | case IF_TYPE_DOC: |
| 1151 | printf("DOC"); |
| 1152 | break; |
| 1153 | case IF_TYPE_MMC: |
| 1154 | printf("MMC"); |
| 1155 | break; |
| 1156 | default: |
| 1157 | printf("Unknown"); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1158 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1159 | |
| 1160 | printf("\n Device %d: ", cur_dev->dev); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1161 | dev_print(cur_dev); |
| 1162 | #endif |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1163 | |
| 1164 | if (read_bootsectandvi(&bs, &volinfo, &fatsize)) { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1165 | printf("\nNo valid FAT fs found\n"); |
| 1166 | return 1; |
| 1167 | } |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1168 | |
| 1169 | memcpy(vol_label, volinfo.volume_label, 11); |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1170 | vol_label[11] = '\0'; |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1171 | volinfo.fs_type[5] = '\0'; |
| 1172 | |
| 1173 | printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part, |
| 1174 | volinfo.fs_type, vol_label); |
| 1175 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 1176 | return 0; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1179 | int file_fat_ls (const char *dir) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1180 | { |
| 1181 | return do_fat_read(dir, NULL, 0, LS_YES); |
| 1182 | } |
| 1183 | |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1184 | long file_fat_read (const char *filename, void *buffer, unsigned long maxsize) |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1185 | { |
Wolfgang Denk | 927b8ce | 2010-07-19 11:37:00 +0200 | [diff] [blame] | 1186 | printf("reading %s\n", filename); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1187 | return do_fat_read(filename, buffer, maxsize, LS_NO); |
| 1188 | } |