3 * Kyle Harris, kharris@nexus-tech.net
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
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.
42 #include <asm/byteorder.h>
43 #if defined(CONFIG_8xx)
46 #ifdef CFG_HUSH_PARSER
50 #if defined(CONFIG_AUTOSCRIPT) || defined(CONFIG_CMD_AUTOSCRIPT)
53 autoscript (ulong addr)
62 verify = getenv_verify ();
64 switch (gen_image_get_format ((void *)addr)) {
65 case IMAGE_FORMAT_LEGACY:
66 hdr = (image_header_t *)addr;
68 if (!image_check_magic (hdr)) {
69 puts ("Bad magic number\n");
73 if (!image_check_hcrc (hdr)) {
74 puts ("Bad header crc\n");
79 if (!image_check_dcrc (hdr)) {
80 puts ("Bad data crc\n");
85 if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
86 puts ("Bad image type\n");
90 /* get length of script */
91 data = (ulong *)image_get_data (hdr);
93 if ((len = image_to_cpu (*data)) == 0) {
94 puts ("Empty Script\n");
98 #if defined(CONFIG_FIT)
99 case IMAGE_FORMAT_FIT:
100 fit_unsupported ("autoscript");
104 puts ("Wrong image format for autoscript\n");
108 debug ("** Script length: %ld\n", len);
110 if ((cmd = malloc (len + 1)) == NULL) {
116 /* make sure cmd is null terminated */
117 memmove (cmd, (char *)data, len);
120 #ifdef CFG_HUSH_PARSER /*?? */
121 rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON);
128 * break into individual lines,
129 * and execute each line;
130 * terminate on error.
135 /* run only non-empty commands */
136 if ((next - line) > 1) {
137 debug ("** exec: \"%s\"\n",
139 if (run_command (line, 0) < 0) {
156 /**************************************************/
157 #if defined(CONFIG_CMD_AUTOSCRIPT)
159 do_autoscript (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
165 addr = CFG_LOAD_ADDR;
167 addr = simple_strtoul (argv[1],0,16);
170 printf ("## Executing script at %08lx\n",addr);
171 rcode = autoscript (addr);
175 #if defined(CONFIG_CMD_AUTOSCRIPT)
177 autoscr, 2, 0, do_autoscript,
178 "autoscr - run script from memory\n",
179 "[addr] - run script starting at addr"
180 " - A valid autoscr header must be present\n"