blob: d3d04c3040a9e3ec06e9480a72a41f4057382a0e [file] [log] [blame]
wdenkd0dd1072002-10-20 17:17:16 +00001/*
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>
wdenkd0dd1072002-10-20 17:17:16 +000043#if defined(CONFIG_8xx)
44#include <mpc8xx.h>
45#endif
46#ifdef CFG_HUSH_PARSER
47#include <hush.h>
48#endif
49
wdenk8bde7f72003-06-27 21:31:46 +000050#define AUTOSCRIPT_MAGIC 0x09011962
wdenkd0dd1072002-10-20 17:17:16 +000051#if defined(CONFIG_AUTOSCRIPT) || \
wdenk8bde7f72003-06-27 21:31:46 +000052 (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT )
wdenkd0dd1072002-10-20 17:17:16 +000053
54extern image_header_t header; /* from cmd_bootm.c */
55int
56autoscript (ulong addr)
57{
58 ulong crc, data, len;
59 image_header_t *hdr = &header;
60 ulong *len_ptr;
61 char *cmd;
62 int rcode = 0;
63 int verify;
64
65 cmd = getenv ("verify");
66 verify = (cmd && (*cmd == 'n')) ? 0 : 1;
67
68
69 memmove (hdr, (char *)addr, sizeof(image_header_t));
70
71 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
72 printf ("Bad magic number\n");
73 return 1;
74 }
75
76 crc = ntohl(hdr->ih_hcrc);
77 hdr->ih_hcrc = 0;
78 len = sizeof (image_header_t);
79 data = (ulong)hdr;
80 if (crc32(0, (char *)data, len) != crc) {
81 printf ("Bad header crc\n");
82 return 1;
83 }
84
85 data = addr + sizeof(image_header_t);
86 len = ntohl(hdr->ih_size);
87
88 if (verify) {
89 if (crc32(0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
90 printf ("Bad data crc\n");
91 return 1;
92 }
93 }
94
95 if (hdr->ih_type != IH_TYPE_SCRIPT) {
96 printf ("Bad image type\n");
97 return 1;
98 }
99
100 /* get length of script */
101 len_ptr = (ulong *)data;
102
103 if ((len = ntohl(*len_ptr)) == 0) {
104 printf ("Empty Script\n");
105 return 1;
106 }
107
wdenk699b13a2002-11-03 18:03:52 +0000108 debug ("** Script length: %ld\n", len);
wdenkd0dd1072002-10-20 17:17:16 +0000109
110 if ((cmd = malloc (len + 1)) == NULL) {
111 return 1;
112 }
113
114 while (*len_ptr++);
115
116 /* make sure cmd is null terminated */
117 memmove (cmd, (char *)len_ptr, len);
118 *(cmd + len) = 0;
119
wdenk8bde7f72003-06-27 21:31:46 +0000120#ifdef CFG_HUSH_PARSER /*?? */
wdenkd0dd1072002-10-20 17:17:16 +0000121 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
154#endif /* CONFIG_AUTOSCRIPT || CFG_CMD_AUTOSCRIPT */
wdenk8bde7f72003-06-27 21:31:46 +0000155/**************************************************/
wdenkd0dd1072002-10-20 17:17:16 +0000156#if (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT)
157int
158do_autoscript (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
159{
160 ulong addr;
161 int rcode;
162
163 if (argc < 2) {
164 addr = CFG_LOAD_ADDR;
165 } else {
166 addr = simple_strtoul (argv[1],0,16);
167 }
168
169 printf ("## Executing script at %08lx\n",addr);
170 rcode = autoscript (addr);
171 return rcode;
172}
wdenk8bde7f72003-06-27 21:31:46 +0000173
174#if (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT)
175cmd_tbl_t U_BOOT_CMD(AUTOSCRIPT) = MK_CMD_ENTRY(
176 "autoscr", 2, 0, do_autoscript,
177 "autoscr - run script from memory\n",
178 "[addr] - run script starting at addr"
179 " - A valid autoscr header must be present\n"
180);
wdenkd0dd1072002-10-20 17:17:16 +0000181#endif /* CFG_CMD_AUTOSCRIPT */
wdenk8bde7f72003-06-27 21:31:46 +0000182
183#endif /* CONFIG_AUTOSCRIPT || CFG_CMD_AUTOSCRIPT */