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 | |
Jon Loeliger | baa26db | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 50 | #if defined(CONFIG_AUTOSCRIPT) || defined(CONFIG_CMD_AUTOSCRIPT) |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 51 | |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 52 | int |
| 53 | autoscript (ulong addr) |
| 54 | { |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame^] | 55 | ulong len; |
| 56 | image_header_t *hdr = (image_header_t *)addr; |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 57 | ulong *len_ptr; |
| 58 | char *cmd; |
| 59 | int rcode = 0; |
| 60 | int verify; |
| 61 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame^] | 62 | verify = getenv_verify (); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 63 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame^] | 64 | if (!image_check_magic (hdr)) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 65 | puts ("Bad magic number\n"); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 66 | return 1; |
| 67 | } |
| 68 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame^] | 69 | if (!image_check_hcrc (hdr)) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 70 | puts ("Bad header crc\n"); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 71 | return 1; |
| 72 | } |
| 73 | |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 74 | if (verify) { |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame^] | 75 | if (!image_check_dcrc (hdr)) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 76 | puts ("Bad data crc\n"); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 77 | return 1; |
| 78 | } |
| 79 | } |
| 80 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame^] | 81 | if (!image_check_type (hdr, IH_TYPE_SCRIPT)) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 82 | puts ("Bad image type\n"); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | /* get length of script */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame^] | 87 | len_ptr = (ulong *)image_get_data (hdr); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 88 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame^] | 89 | if ((len = image_to_cpu (*len_ptr)) == 0) { |
wdenk | 4b9206e | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 90 | puts ("Empty Script\n"); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 91 | return 1; |
| 92 | } |
| 93 | |
wdenk | 699b13a | 2002-11-03 18:03:52 +0000 | [diff] [blame] | 94 | debug ("** Script length: %ld\n", len); |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 95 | |
| 96 | if ((cmd = malloc (len + 1)) == NULL) { |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | while (*len_ptr++); |
| 101 | |
| 102 | /* make sure cmd is null terminated */ |
| 103 | memmove (cmd, (char *)len_ptr, len); |
| 104 | *(cmd + len) = 0; |
| 105 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 106 | #ifdef CFG_HUSH_PARSER /*?? */ |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 107 | rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON); |
| 108 | #else |
| 109 | { |
| 110 | char *line = cmd; |
| 111 | char *next = cmd; |
| 112 | |
| 113 | /* |
| 114 | * break into individual lines, |
| 115 | * and execute each line; |
| 116 | * terminate on error. |
| 117 | */ |
| 118 | while (*next) { |
| 119 | if (*next == '\n') { |
| 120 | *next = '\0'; |
| 121 | /* run only non-empty commands */ |
| 122 | if ((next - line) > 1) { |
| 123 | debug ("** exec: \"%s\"\n", |
| 124 | line); |
| 125 | if (run_command (line, 0) < 0) { |
| 126 | rcode = 1; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | line = next + 1; |
| 131 | } |
| 132 | ++next; |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | free (cmd); |
| 137 | return rcode; |
| 138 | } |
| 139 | |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 140 | #endif |
| 141 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 142 | /**************************************************/ |
Jon Loeliger | baa26db | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 143 | #if defined(CONFIG_CMD_AUTOSCRIPT) |
wdenk | d0dd107 | 2002-10-20 17:17:16 +0000 | [diff] [blame] | 144 | int |
| 145 | do_autoscript (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 146 | { |
| 147 | ulong addr; |
| 148 | int rcode; |
| 149 | |
| 150 | if (argc < 2) { |
| 151 | addr = CFG_LOAD_ADDR; |
| 152 | } else { |
| 153 | addr = simple_strtoul (argv[1],0,16); |
| 154 | } |
| 155 | |
| 156 | printf ("## Executing script at %08lx\n",addr); |
| 157 | rcode = autoscript (addr); |
| 158 | return rcode; |
| 159 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 160 | |
Jon Loeliger | baa26db | 2007-07-08 17:51:39 -0500 | [diff] [blame] | 161 | #if defined(CONFIG_CMD_AUTOSCRIPT) |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 162 | U_BOOT_CMD( |
| 163 | autoscr, 2, 0, do_autoscript, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 164 | "autoscr - run script from memory\n", |
| 165 | "[addr] - run script starting at addr" |
| 166 | " - A valid autoscr header must be present\n" |
| 167 | ); |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 168 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 169 | |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 170 | #endif |