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