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