]>
Commit | Line | Data |
---|---|---|
d0dd1077 WD |
1 | /* |
2 | * (C) Copyright 2001 | |
3 | * Kyle Harris, [email protected] | |
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> | |
d0dd1077 WD |
43 | #if defined(CONFIG_8xx) |
44 | #include <mpc8xx.h> | |
45 | #endif | |
46 | #ifdef CFG_HUSH_PARSER | |
47 | #include <hush.h> | |
48 | #endif | |
49 | ||
baa26db4 | 50 | #if defined(CONFIG_AUTOSCRIPT) || defined(CONFIG_CMD_AUTOSCRIPT) |
d0dd1077 WD |
51 | |
52 | extern image_header_t header; /* from cmd_bootm.c */ | |
53 | int | |
54 | autoscript (ulong addr) | |
55 | { | |
56 | ulong crc, data, len; | |
57 | image_header_t *hdr = &header; | |
58 | ulong *len_ptr; | |
59 | char *cmd; | |
60 | int rcode = 0; | |
61 | int verify; | |
62 | ||
63 | cmd = getenv ("verify"); | |
64 | verify = (cmd && (*cmd == 'n')) ? 0 : 1; | |
65 | ||
66 | ||
67 | memmove (hdr, (char *)addr, sizeof(image_header_t)); | |
68 | ||
69 | if (ntohl(hdr->ih_magic) != IH_MAGIC) { | |
4b9206ed | 70 | puts ("Bad magic number\n"); |
d0dd1077 WD |
71 | return 1; |
72 | } | |
73 | ||
74 | crc = ntohl(hdr->ih_hcrc); | |
75 | hdr->ih_hcrc = 0; | |
76 | len = sizeof (image_header_t); | |
77 | data = (ulong)hdr; | |
77ddac94 | 78 | if (crc32(0, (uchar *)data, len) != crc) { |
4b9206ed | 79 | puts ("Bad header crc\n"); |
d0dd1077 WD |
80 | return 1; |
81 | } | |
82 | ||
83 | data = addr + sizeof(image_header_t); | |
84 | len = ntohl(hdr->ih_size); | |
85 | ||
86 | if (verify) { | |
77ddac94 | 87 | if (crc32(0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) { |
4b9206ed | 88 | puts ("Bad data crc\n"); |
d0dd1077 WD |
89 | return 1; |
90 | } | |
91 | } | |
92 | ||
93 | if (hdr->ih_type != IH_TYPE_SCRIPT) { | |
4b9206ed | 94 | puts ("Bad image type\n"); |
d0dd1077 WD |
95 | return 1; |
96 | } | |
97 | ||
98 | /* get length of script */ | |
99 | len_ptr = (ulong *)data; | |
100 | ||
101 | if ((len = ntohl(*len_ptr)) == 0) { | |
4b9206ed | 102 | puts ("Empty Script\n"); |
d0dd1077 WD |
103 | return 1; |
104 | } | |
105 | ||
699b13a6 | 106 | debug ("** Script length: %ld\n", len); |
d0dd1077 WD |
107 | |
108 | if ((cmd = malloc (len + 1)) == NULL) { | |
109 | return 1; | |
110 | } | |
111 | ||
112 | while (*len_ptr++); | |
113 | ||
114 | /* make sure cmd is null terminated */ | |
115 | memmove (cmd, (char *)len_ptr, len); | |
116 | *(cmd + len) = 0; | |
117 | ||
8bde7f77 | 118 | #ifdef CFG_HUSH_PARSER /*?? */ |
d0dd1077 WD |
119 | rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON); |
120 | #else | |
121 | { | |
122 | char *line = cmd; | |
123 | char *next = cmd; | |
124 | ||
125 | /* | |
126 | * break into individual lines, | |
127 | * and execute each line; | |
128 | * terminate on error. | |
129 | */ | |
130 | while (*next) { | |
131 | if (*next == '\n') { | |
132 | *next = '\0'; | |
133 | /* run only non-empty commands */ | |
134 | if ((next - line) > 1) { | |
135 | debug ("** exec: \"%s\"\n", | |
136 | line); | |
137 | if (run_command (line, 0) < 0) { | |
138 | rcode = 1; | |
139 | break; | |
140 | } | |
141 | } | |
142 | line = next + 1; | |
143 | } | |
144 | ++next; | |
145 | } | |
146 | } | |
147 | #endif | |
148 | free (cmd); | |
149 | return rcode; | |
150 | } | |
151 | ||
90253178 JL |
152 | #endif |
153 | ||
8bde7f77 | 154 | /**************************************************/ |
baa26db4 | 155 | #if defined(CONFIG_CMD_AUTOSCRIPT) |
d0dd1077 WD |
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 | } | |
8bde7f77 | 172 | |
baa26db4 | 173 | #if defined(CONFIG_CMD_AUTOSCRIPT) |
0d498393 WD |
174 | U_BOOT_CMD( |
175 | autoscr, 2, 0, do_autoscript, | |
8bde7f77 WD |
176 | "autoscr - run script from memory\n", |
177 | "[addr] - run script starting at addr" | |
178 | " - A valid autoscr header must be present\n" | |
179 | ); | |
90253178 | 180 | #endif |
8bde7f77 | 181 | |
90253178 | 182 | #endif |