wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Richard Jones, rjones@nexus-tech.net |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Boot support |
| 26 | */ |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 29 | #include <s_record.h> |
| 30 | #include <net.h> |
| 31 | #include <ata.h> |
| 32 | |
| 33 | #if (CONFIG_COMMANDS & CFG_CMD_FAT) |
| 34 | |
| 35 | #undef DEBUG |
| 36 | |
| 37 | #include <fat.h> |
| 38 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 39 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 40 | block_dev_desc_t *get_dev (char* ifname, int dev) |
| 41 | { |
| 42 | #if (CONFIG_COMMANDS & CFG_CMD_IDE) |
| 43 | if (strncmp(ifname,"ide",3)==0) { |
| 44 | extern block_dev_desc_t * ide_get_dev(int dev); |
| 45 | return(ide_get_dev(dev)); |
| 46 | } |
| 47 | #endif |
| 48 | #if (CONFIG_COMMANDS & CFG_CMD_SCSI) |
| 49 | if (strncmp(ifname,"scsi",4)==0) { |
| 50 | extern block_dev_desc_t * scsi_get_dev(int dev); |
| 51 | return(scsi_get_dev(dev)); |
| 52 | } |
| 53 | #endif |
| 54 | #if ((CONFIG_COMMANDS & CFG_CMD_USB) && defined(CONFIG_USB_STORAGE)) |
| 55 | if (strncmp(ifname,"usb",3)==0) { |
| 56 | extern block_dev_desc_t * usb_stor_get_dev(int dev); |
| 57 | return(usb_stor_get_dev(dev)); |
| 58 | } |
| 59 | #endif |
| 60 | #if defined(CONFIG_MMC) |
| 61 | if (strncmp(ifname,"mmc",3)==0) { |
| 62 | extern block_dev_desc_t * mmc_get_dev(int dev); |
| 63 | return(mmc_get_dev(dev)); |
| 64 | } |
| 65 | #endif |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 66 | #if defined(CONFIG_SYSTEMACE) |
| 67 | if (strcmp(ifname,"ace")==0) { |
| 68 | extern block_dev_desc_t * systemace_get_dev(int dev); |
| 69 | return(systemace_get_dev(dev)); |
| 70 | } |
| 71 | #endif |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
| 74 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 75 | |
| 76 | int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 77 | { |
| 78 | long size; |
| 79 | unsigned long offset; |
| 80 | unsigned long count; |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 81 | char buf [12]; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 82 | block_dev_desc_t *dev_desc=NULL; |
| 83 | int dev=0; |
| 84 | int part=1; |
| 85 | char *ep; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 86 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 87 | if (argc < 5) { |
| 88 | printf ("usage: fatload <interface> <dev[:part]> <addr> <filename> [bytes]\n"); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 89 | return (0); |
| 90 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 91 | dev = (int)simple_strtoul (argv[2], &ep, 16); |
| 92 | dev_desc=get_dev(argv[1],dev); |
| 93 | if (dev_desc==NULL) { |
| 94 | puts ("\n** Invalid boot device **\n"); |
| 95 | return 1; |
| 96 | } |
| 97 | if (*ep) { |
| 98 | if (*ep != ':') { |
| 99 | puts ("\n** Invalid boot device, use `dev[:part]' **\n"); |
| 100 | return 1; |
| 101 | } |
| 102 | part = (int)simple_strtoul(++ep, NULL, 16); |
| 103 | } |
| 104 | if (fat_register_device(dev_desc,part)!=0) { |
| 105 | printf ("\n** Unable to use %s %d:%d for fatload **\n",argv[1],dev,part); |
| 106 | return 1; |
| 107 | } |
| 108 | offset = simple_strtoul (argv[3], NULL, 16); |
| 109 | if (argc == 6) |
| 110 | count = simple_strtoul (argv[5], NULL, 16); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 111 | else |
| 112 | count = 0; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 113 | size = file_fat_read (argv[4], (unsigned char *) offset, count); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 114 | |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 115 | if(size==-1) { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 116 | printf("\n** Unable to read \"%s\" from %s %d:%d **\n",argv[4],argv[1],dev,part); |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 117 | } else { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 118 | printf ("\n%ld bytes read\n", size); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 119 | |
wdenk | 7182b0f | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 120 | sprintf(buf, "%lX", size); |
| 121 | setenv("filesize", buf); |
| 122 | } |
| 123 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 124 | return size; |
| 125 | } |
| 126 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 127 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 128 | U_BOOT_CMD( |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 129 | fatload, 6, 0, do_fat_fsload, |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 130 | "fatload - load binary file from a dos filesystem\n", |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 131 | "<interface> <dev[:part]> <addr> <filename> [bytes]\n" |
| 132 | " - load binary file 'filename' from 'dev' on 'interface'\n" |
| 133 | " to address 'addr' from dos filesystem\n" |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 134 | ); |
| 135 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 136 | int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 137 | { |
| 138 | char *filename = "/"; |
| 139 | int ret; |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 140 | int dev=0; |
| 141 | int part=1; |
| 142 | char *ep; |
| 143 | block_dev_desc_t *dev_desc=NULL; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 144 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 145 | if (argc < 3) { |
| 146 | printf ("usage: fatls <interface> <dev[:part]> [directory]\n"); |
| 147 | return (0); |
| 148 | } |
| 149 | dev = (int)simple_strtoul (argv[2], &ep, 16); |
| 150 | dev_desc=get_dev(argv[1],dev); |
| 151 | if (dev_desc==NULL) { |
| 152 | puts ("\n** Invalid boot device **\n"); |
| 153 | return 1; |
| 154 | } |
| 155 | if (*ep) { |
| 156 | if (*ep != ':') { |
| 157 | puts ("\n** Invalid boot device, use `dev[:part]' **\n"); |
| 158 | return 1; |
| 159 | } |
| 160 | part = (int)simple_strtoul(++ep, NULL, 16); |
| 161 | } |
| 162 | if (fat_register_device(dev_desc,part)!=0) { |
| 163 | printf ("\n** Unable to use %s %d:%d for fatls **\n",argv[1],dev,part); |
| 164 | return 1; |
| 165 | } |
| 166 | if (argc == 4) |
| 167 | ret = file_fat_ls (argv[3]); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 168 | else |
| 169 | ret = file_fat_ls (filename); |
| 170 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 171 | if(ret!=0) |
| 172 | printf("No Fat FS detected\n"); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 173 | return (ret); |
| 174 | } |
| 175 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 176 | U_BOOT_CMD( |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 177 | fatls, 4, 1, do_fat_ls, |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 178 | "fatls - list files in a directory (default /)\n", |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 179 | "<interface> <dev[:part]> [directory]\n" |
| 180 | " - list files from 'dev' on 'interface' in a 'directory'\n" |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 181 | ); |
| 182 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 183 | int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 184 | { |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 185 | int dev=0; |
| 186 | int part=1; |
| 187 | char *ep; |
| 188 | block_dev_desc_t *dev_desc=NULL; |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 189 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 190 | if (argc < 2) { |
| 191 | printf ("usage: fatinfo <interface> <dev[:part]>\n"); |
| 192 | return (0); |
| 193 | } |
| 194 | dev = (int)simple_strtoul (argv[2], &ep, 16); |
| 195 | dev_desc=get_dev(argv[1],dev); |
| 196 | if (dev_desc==NULL) { |
| 197 | puts ("\n** Invalid boot device **\n"); |
| 198 | return 1; |
| 199 | } |
| 200 | if (*ep) { |
| 201 | if (*ep != ':') { |
| 202 | puts ("\n** Invalid boot device, use `dev[:part]' **\n"); |
| 203 | return 1; |
| 204 | } |
| 205 | part = (int)simple_strtoul(++ep, NULL, 16); |
| 206 | } |
| 207 | if (fat_register_device(dev_desc,part)!=0) { |
| 208 | printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,part); |
| 209 | return 1; |
| 210 | } |
| 211 | return (file_fat_detectfs ()); |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 212 | } |
| 213 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 214 | U_BOOT_CMD( |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 215 | fatinfo, 3, 1, do_fat_fsinfo, |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 216 | "fatinfo - print information about filesystem\n", |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 217 | "<interface> <dev[:part]>\n" |
| 218 | " - print information about filesystem from 'dev' on 'interface'\n" |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 219 | ); |
| 220 | |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 221 | #ifdef NOT_IMPLEMENTED_YET |
| 222 | /* find first device whose first partition is a DOS filesystem */ |
| 223 | int find_fat_partition (void) |
| 224 | { |
| 225 | int i, j; |
| 226 | block_dev_desc_t *dev_desc; |
| 227 | unsigned char *part_table; |
| 228 | unsigned char buffer[ATA_BLOCKSIZE]; |
| 229 | |
| 230 | for (i = 0; i < CFG_IDE_MAXDEVICE; i++) { |
| 231 | dev_desc = ide_get_dev (i); |
| 232 | if (!dev_desc) { |
| 233 | debug ("couldn't get ide device!\n"); |
| 234 | return (-1); |
| 235 | } |
| 236 | if (dev_desc->part_type == PART_TYPE_DOS) { |
| 237 | if (dev_desc-> |
| 238 | block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) { |
| 239 | debug ("can't perform block_read!\n"); |
| 240 | return (-1); |
| 241 | } |
| 242 | part_table = &buffer[0x1be]; /* start with partition #4 */ |
| 243 | for (j = 0; j < 4; j++) { |
| 244 | if ((part_table[4] == 1 || /* 12-bit FAT */ |
| 245 | part_table[4] == 4 || /* 16-bit FAT */ |
| 246 | part_table[4] == 6) && /* > 32Meg part */ |
| 247 | part_table[0] == 0x80) { /* bootable? */ |
| 248 | curr_dev = i; |
| 249 | part_offset = part_table[11]; |
| 250 | part_offset <<= 8; |
| 251 | part_offset |= part_table[10]; |
| 252 | part_offset <<= 8; |
| 253 | part_offset |= part_table[9]; |
| 254 | part_offset <<= 8; |
| 255 | part_offset |= part_table[8]; |
| 256 | debug ("found partition start at %ld\n", part_offset); |
| 257 | return (0); |
| 258 | } |
| 259 | part_table += 16; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | debug ("no valid devices found!\n"); |
| 265 | return (-1); |
| 266 | } |
| 267 | |
| 268 | int |
| 269 | do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[]) |
| 270 | { |
| 271 | __u8 block[1024]; |
| 272 | int ret; |
| 273 | int bknum; |
| 274 | |
| 275 | ret = 0; |
| 276 | |
| 277 | if (argc != 2) { |
| 278 | printf ("needs an argument!\n"); |
| 279 | return (0); |
| 280 | } |
| 281 | |
| 282 | bknum = simple_strtoul (argv[1], NULL, 10); |
| 283 | |
| 284 | if (disk_read (0, bknum, block) != 0) { |
| 285 | printf ("Error: reading block\n"); |
| 286 | return -1; |
| 287 | } |
| 288 | printf ("FAT dump: %d\n", bknum); |
| 289 | hexdump (512, block); |
| 290 | |
| 291 | return (ret); |
| 292 | } |
| 293 | |
| 294 | int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr) |
| 295 | { |
| 296 | ulong tot; |
| 297 | block_dev_desc_t *dev_desc; |
| 298 | |
| 299 | if (curr_dev < 0) { |
| 300 | if (find_fat_partition () != 0) |
| 301 | return (-1); |
| 302 | } |
| 303 | |
| 304 | dev_desc = ide_get_dev (curr_dev); |
| 305 | if (!dev_desc) { |
| 306 | debug ("couldn't get ide device\n"); |
| 307 | return (-1); |
| 308 | } |
| 309 | |
| 310 | tot = dev_desc->block_read (0, startblock + part_offset, |
| 311 | getsize, (ulong *) bufptr); |
| 312 | |
| 313 | /* should we do this here? |
| 314 | flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz); |
| 315 | */ |
| 316 | |
| 317 | if (tot == getsize) |
| 318 | return (0); |
| 319 | |
| 320 | debug ("unable to read from device!\n"); |
| 321 | |
| 322 | return (-1); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | static int isprint (unsigned char ch) |
| 327 | { |
| 328 | if (ch >= 32 && ch < 127) |
| 329 | return (1); |
| 330 | |
| 331 | return (0); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | void hexdump (int cnt, unsigned char *data) |
| 336 | { |
| 337 | int i; |
| 338 | int run; |
| 339 | int offset; |
| 340 | |
| 341 | offset = 0; |
| 342 | while (cnt) { |
| 343 | printf ("%04X : ", offset); |
| 344 | if (cnt >= 16) |
| 345 | run = 16; |
| 346 | else |
| 347 | run = cnt; |
| 348 | cnt -= run; |
| 349 | for (i = 0; i < run; i++) |
| 350 | printf ("%02X ", (unsigned int) data[i]); |
| 351 | printf (": "); |
| 352 | for (i = 0; i < run; i++) |
| 353 | printf ("%c", isprint (data[i]) ? data[i] : '.'); |
| 354 | printf ("\n"); |
| 355 | data = &data[16]; |
| 356 | offset += run; |
| 357 | } |
| 358 | } |
| 359 | #endif /* NOT_IMPLEMENTED_YET */ |
| 360 | |
| 361 | #endif /* CFG_CMD_FAT */ |