wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Kyle Harris, kharris@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 | * autoscript allows a remote host to download a command file and, |
| 26 | * optionally, binary data for automatically updating the target. For |
| 27 | * example, you create a new kernel image and want the user to be |
| 28 | * able to simply download the image and the machine does the rest. |
| 29 | * The kernel image is postprocessed with mkimage, which creates an |
| 30 | * image with a script file prepended. If enabled, autoscript will |
| 31 | * verify the script and contents of the download and execute the |
| 32 | * script portion. This would be responsible for erasing flash, |
| 33 | * copying the new image, and rebooting the machine. |
| 34 | */ |
| 35 | |
| 36 | /* #define DEBUG */ |
| 37 | |
| 38 | #include <common.h> |
| 39 | #include <command.h> |
| 40 | #include <image.h> |
| 41 | #include <malloc.h> |
| 42 | #include <asm/byteorder.h> |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 43 | #if defined(CONFIG_8xx) |
| 44 | #include <mpc8xx.h> |
| 45 | #endif |
| 46 | #ifdef CFG_HUSH_PARSER |
| 47 | #include <hush.h> |
| 48 | #endif |
| 49 | |
| 50 | #if defined(CONFIG_AUTOSCRIPT) || \ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 51 | (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT ) |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 52 | |
| 53 | extern image_header_t header; /* from cmd_bootm.c */ |
| 54 | int |
| 55 | autoscript (ulong addr) |
| 56 | { |
| 57 | ulong crc, data, len; |
| 58 | image_header_t *hdr = &header; |
| 59 | ulong *len_ptr; |
| 60 | char *cmd; |
| 61 | int rcode = 0; |
| 62 | int verify; |
| 63 | |
| 64 | cmd = getenv ("verify"); |
| 65 | verify = (cmd && (*cmd == 'n')) ? 0 : 1; |
| 66 | |
| 67 | |
| 68 | memmove (hdr, (char *)addr, sizeof(image_header_t)); |
| 69 | |
| 70 | if (ntohl(hdr->ih_magic) != IH_MAGIC) { |
| 71 | printf ("Bad magic number\n"); |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | crc = ntohl(hdr->ih_hcrc); |
| 76 | hdr->ih_hcrc = 0; |
| 77 | len = sizeof (image_header_t); |
| 78 | data = (ulong)hdr; |
| 79 | if (crc32(0, (char *)data, len) != crc) { |
| 80 | printf ("Bad header crc\n"); |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | data = addr + sizeof(image_header_t); |
| 85 | len = ntohl(hdr->ih_size); |
| 86 | |
| 87 | if (verify) { |
| 88 | if (crc32(0, (char *)data, len) != ntohl(hdr->ih_dcrc)) { |
| 89 | printf ("Bad data crc\n"); |
| 90 | return 1; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (hdr->ih_type != IH_TYPE_SCRIPT) { |
| 95 | printf ("Bad image type\n"); |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | /* get length of script */ |
| 100 | len_ptr = (ulong *)data; |
| 101 | |
| 102 | if ((len = ntohl(*len_ptr)) == 0) { |
| 103 | printf ("Empty Script\n"); |
| 104 | return 1; |
| 105 | } |
| 106 | |
wdenk | 699b13a | 2002-11-03 18:03:52 +0000 | [diff] [blame] | 107 | debug ("** Script length: %ld\n", len); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 108 | |
| 109 | if ((cmd = malloc (len + 1)) == NULL) { |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | while (*len_ptr++); |
| 114 | |
| 115 | /* make sure cmd is null terminated */ |
| 116 | memmove (cmd, (char *)len_ptr, len); |
| 117 | *(cmd + len) = 0; |
| 118 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 119 | #ifdef CFG_HUSH_PARSER /*?? */ |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 120 | rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON); |
| 121 | #else |
| 122 | { |
| 123 | char *line = cmd; |
| 124 | char *next = cmd; |
| 125 | |
| 126 | /* |
| 127 | * break into individual lines, |
| 128 | * and execute each line; |
| 129 | * terminate on error. |
| 130 | */ |
| 131 | while (*next) { |
| 132 | if (*next == '\n') { |
| 133 | *next = '\0'; |
| 134 | /* run only non-empty commands */ |
| 135 | if ((next - line) > 1) { |
| 136 | debug ("** exec: \"%s\"\n", |
| 137 | line); |
| 138 | if (run_command (line, 0) < 0) { |
| 139 | rcode = 1; |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | line = next + 1; |
| 144 | } |
| 145 | ++next; |
| 146 | } |
| 147 | } |
| 148 | #endif |
| 149 | free (cmd); |
| 150 | return rcode; |
| 151 | } |
| 152 | |
| 153 | #endif /* CONFIG_AUTOSCRIPT || CFG_CMD_AUTOSCRIPT */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 154 | /**************************************************/ |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 155 | #if (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT) |
| 156 | int |
| 157 | do_autoscript (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 158 | { |
| 159 | ulong addr; |
| 160 | int rcode; |
| 161 | |
| 162 | if (argc < 2) { |
| 163 | addr = CFG_LOAD_ADDR; |
| 164 | } else { |
| 165 | addr = simple_strtoul (argv[1],0,16); |
| 166 | } |
| 167 | |
| 168 | printf ("## Executing script at %08lx\n",addr); |
| 169 | rcode = autoscript (addr); |
| 170 | return rcode; |
| 171 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 172 | |
| 173 | #if (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT) |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame^] | 174 | U_BOOT_CMD( |
| 175 | autoscr, 2, 0, do_autoscript, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 176 | "autoscr - run script from memory\n", |
| 177 | "[addr] - run script starting at addr" |
| 178 | " - A valid autoscr header must be present\n" |
| 179 | ); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 180 | #endif /* CFG_CMD_AUTOSCRIPT */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 181 | |
| 182 | #endif /* CONFIG_AUTOSCRIPT || CFG_CMD_AUTOSCRIPT */ |