wdenk | 71f9511 | 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 | 71f9511 | 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 | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 39 | |
wdenk | 7205e40 | 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 | 3f85ce2 | 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 | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
| 74 | |
wdenk | 71f9511 | 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 | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 81 | char buf [12]; |
wdenk | 7205e40 | 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 | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 86 | |
wdenk | 7205e40 | 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 | 31a6492 | 2004-08-28 21:09:14 +0000 | [diff] [blame] | 89 | return 1; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 90 | } |
wdenk | 7205e40 | 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 | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 111 | else |
| 112 | count = 0; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 113 | size = file_fat_read (argv[4], (unsigned char *) offset, count); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 114 | |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 115 | if(size==-1) { |
wdenk | 7205e40 | 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 | 31a6492 | 2004-08-28 21:09:14 +0000 | [diff] [blame] | 117 | return 1; |
wdenk | fbe4b5c | 2003-10-06 21:55:32 +0000 | [diff] [blame] | 118 | } |
| 119 | |
wdenk | 31a6492 | 2004-08-28 21:09:14 +0000 | [diff] [blame] | 120 | printf ("\n%ld bytes read\n", size); |
| 121 | |
| 122 | sprintf(buf, "%lX", size); |
| 123 | setenv("filesize", buf); |
| 124 | |
| 125 | return 0; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 126 | } |
| 127 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 128 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 129 | U_BOOT_CMD( |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 130 | fatload, 6, 0, do_fat_fsload, |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 131 | "fatload - load binary file from a dos filesystem\n", |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 132 | "<interface> <dev[:part]> <addr> <filename> [bytes]\n" |
| 133 | " - load binary file 'filename' from 'dev' on 'interface'\n" |
| 134 | " to address 'addr' from dos filesystem\n" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 135 | ); |
| 136 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 137 | int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 138 | { |
| 139 | char *filename = "/"; |
| 140 | int ret; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 141 | int dev=0; |
| 142 | int part=1; |
| 143 | char *ep; |
| 144 | block_dev_desc_t *dev_desc=NULL; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 145 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 146 | if (argc < 3) { |
| 147 | printf ("usage: fatls <interface> <dev[:part]> [directory]\n"); |
| 148 | return (0); |
| 149 | } |
| 150 | dev = (int)simple_strtoul (argv[2], &ep, 16); |
| 151 | dev_desc=get_dev(argv[1],dev); |
| 152 | if (dev_desc==NULL) { |
| 153 | puts ("\n** Invalid boot device **\n"); |
| 154 | return 1; |
| 155 | } |
| 156 | if (*ep) { |
| 157 | if (*ep != ':') { |
| 158 | puts ("\n** Invalid boot device, use `dev[:part]' **\n"); |
| 159 | return 1; |
| 160 | } |
| 161 | part = (int)simple_strtoul(++ep, NULL, 16); |
| 162 | } |
| 163 | if (fat_register_device(dev_desc,part)!=0) { |
| 164 | printf ("\n** Unable to use %s %d:%d for fatls **\n",argv[1],dev,part); |
| 165 | return 1; |
| 166 | } |
| 167 | if (argc == 4) |
| 168 | ret = file_fat_ls (argv[3]); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 169 | else |
| 170 | ret = file_fat_ls (filename); |
| 171 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 172 | if(ret!=0) |
| 173 | printf("No Fat FS detected\n"); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 174 | return (ret); |
| 175 | } |
| 176 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 177 | U_BOOT_CMD( |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 178 | fatls, 4, 1, do_fat_ls, |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 179 | "fatls - list files in a directory (default /)\n", |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 180 | "<interface> <dev[:part]> [directory]\n" |
| 181 | " - list files from 'dev' on 'interface' in a 'directory'\n" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 182 | ); |
| 183 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 184 | int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 185 | { |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 186 | int dev=0; |
| 187 | int part=1; |
| 188 | char *ep; |
| 189 | block_dev_desc_t *dev_desc=NULL; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 190 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 191 | if (argc < 2) { |
| 192 | printf ("usage: fatinfo <interface> <dev[:part]>\n"); |
| 193 | return (0); |
| 194 | } |
| 195 | dev = (int)simple_strtoul (argv[2], &ep, 16); |
| 196 | dev_desc=get_dev(argv[1],dev); |
| 197 | if (dev_desc==NULL) { |
| 198 | puts ("\n** Invalid boot device **\n"); |
| 199 | return 1; |
| 200 | } |
| 201 | if (*ep) { |
| 202 | if (*ep != ':') { |
| 203 | puts ("\n** Invalid boot device, use `dev[:part]' **\n"); |
| 204 | return 1; |
| 205 | } |
| 206 | part = (int)simple_strtoul(++ep, NULL, 16); |
| 207 | } |
| 208 | if (fat_register_device(dev_desc,part)!=0) { |
| 209 | printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,part); |
| 210 | return 1; |
| 211 | } |
| 212 | return (file_fat_detectfs ()); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 213 | } |
| 214 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 215 | U_BOOT_CMD( |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 216 | fatinfo, 3, 1, do_fat_fsinfo, |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 217 | "fatinfo - print information about filesystem\n", |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 218 | "<interface> <dev[:part]>\n" |
| 219 | " - print information about filesystem from 'dev' on 'interface'\n" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 220 | ); |
| 221 | |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 222 | #ifdef NOT_IMPLEMENTED_YET |
| 223 | /* find first device whose first partition is a DOS filesystem */ |
| 224 | int find_fat_partition (void) |
| 225 | { |
| 226 | int i, j; |
| 227 | block_dev_desc_t *dev_desc; |
| 228 | unsigned char *part_table; |
| 229 | unsigned char buffer[ATA_BLOCKSIZE]; |
| 230 | |
| 231 | for (i = 0; i < CFG_IDE_MAXDEVICE; i++) { |
| 232 | dev_desc = ide_get_dev (i); |
| 233 | if (!dev_desc) { |
| 234 | debug ("couldn't get ide device!\n"); |
| 235 | return (-1); |
| 236 | } |
| 237 | if (dev_desc->part_type == PART_TYPE_DOS) { |
| 238 | if (dev_desc-> |
| 239 | block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) { |
| 240 | debug ("can't perform block_read!\n"); |
| 241 | return (-1); |
| 242 | } |
| 243 | part_table = &buffer[0x1be]; /* start with partition #4 */ |
| 244 | for (j = 0; j < 4; j++) { |
| 245 | if ((part_table[4] == 1 || /* 12-bit FAT */ |
| 246 | part_table[4] == 4 || /* 16-bit FAT */ |
| 247 | part_table[4] == 6) && /* > 32Meg part */ |
| 248 | part_table[0] == 0x80) { /* bootable? */ |
| 249 | curr_dev = i; |
| 250 | part_offset = part_table[11]; |
| 251 | part_offset <<= 8; |
| 252 | part_offset |= part_table[10]; |
| 253 | part_offset <<= 8; |
| 254 | part_offset |= part_table[9]; |
| 255 | part_offset <<= 8; |
| 256 | part_offset |= part_table[8]; |
| 257 | debug ("found partition start at %ld\n", part_offset); |
| 258 | return (0); |
| 259 | } |
| 260 | part_table += 16; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | debug ("no valid devices found!\n"); |
| 266 | return (-1); |
| 267 | } |
| 268 | |
| 269 | int |
| 270 | do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[]) |
| 271 | { |
| 272 | __u8 block[1024]; |
| 273 | int ret; |
| 274 | int bknum; |
| 275 | |
| 276 | ret = 0; |
| 277 | |
| 278 | if (argc != 2) { |
| 279 | printf ("needs an argument!\n"); |
| 280 | return (0); |
| 281 | } |
| 282 | |
| 283 | bknum = simple_strtoul (argv[1], NULL, 10); |
| 284 | |
| 285 | if (disk_read (0, bknum, block) != 0) { |
| 286 | printf ("Error: reading block\n"); |
| 287 | return -1; |
| 288 | } |
| 289 | printf ("FAT dump: %d\n", bknum); |
| 290 | hexdump (512, block); |
| 291 | |
| 292 | return (ret); |
| 293 | } |
| 294 | |
| 295 | int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr) |
| 296 | { |
| 297 | ulong tot; |
| 298 | block_dev_desc_t *dev_desc; |
| 299 | |
| 300 | if (curr_dev < 0) { |
| 301 | if (find_fat_partition () != 0) |
| 302 | return (-1); |
| 303 | } |
| 304 | |
| 305 | dev_desc = ide_get_dev (curr_dev); |
| 306 | if (!dev_desc) { |
| 307 | debug ("couldn't get ide device\n"); |
| 308 | return (-1); |
| 309 | } |
| 310 | |
| 311 | tot = dev_desc->block_read (0, startblock + part_offset, |
| 312 | getsize, (ulong *) bufptr); |
| 313 | |
| 314 | /* should we do this here? |
| 315 | flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz); |
| 316 | */ |
| 317 | |
| 318 | if (tot == getsize) |
| 319 | return (0); |
| 320 | |
| 321 | debug ("unable to read from device!\n"); |
| 322 | |
| 323 | return (-1); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | static int isprint (unsigned char ch) |
| 328 | { |
| 329 | if (ch >= 32 && ch < 127) |
| 330 | return (1); |
| 331 | |
| 332 | return (0); |
| 333 | } |
| 334 | |
| 335 | |
| 336 | void hexdump (int cnt, unsigned char *data) |
| 337 | { |
| 338 | int i; |
| 339 | int run; |
| 340 | int offset; |
| 341 | |
| 342 | offset = 0; |
| 343 | while (cnt) { |
| 344 | printf ("%04X : ", offset); |
| 345 | if (cnt >= 16) |
| 346 | run = 16; |
| 347 | else |
| 348 | run = cnt; |
| 349 | cnt -= run; |
| 350 | for (i = 0; i < run; i++) |
| 351 | printf ("%02X ", (unsigned int) data[i]); |
| 352 | printf (": "); |
| 353 | for (i = 0; i < run; i++) |
| 354 | printf ("%c", isprint (data[i]) ? data[i] : '.'); |
| 355 | printf ("\n"); |
| 356 | data = &data[16]; |
| 357 | offset += run; |
| 358 | } |
| 359 | } |
| 360 | #endif /* NOT_IMPLEMENTED_YET */ |
| 361 | |
| 362 | #endif /* CFG_CMD_FAT */ |