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