]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_autoscript.c
* Fix mdelay() on TRAB - this was still the debugging version with
[karo-tx-uboot.git] / common / cmd_autoscript.c
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>
43 #include <cmd_boot.h>
44 #include <cmd_autoscript.h>
45 #if defined(CONFIG_8xx)
46 #include <mpc8xx.h>
47 #endif
48 #ifdef CFG_HUSH_PARSER
49 #include <hush.h>
50 #endif
51
52 #if defined(CONFIG_AUTOSCRIPT) || \
53          (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT)
54
55 extern image_header_t header;           /* from cmd_bootm.c */
56 int
57 autoscript (ulong addr)
58 {
59         ulong crc, data, len;
60         image_header_t *hdr = &header;
61         ulong *len_ptr;
62         char *cmd;
63         int rcode = 0;
64         int verify;
65
66         cmd = getenv ("verify");
67         verify = (cmd && (*cmd == 'n')) ? 0 : 1;
68
69
70         memmove (hdr, (char *)addr, sizeof(image_header_t));
71
72         if (ntohl(hdr->ih_magic) != IH_MAGIC) {
73                 printf ("Bad magic number\n");
74                 return 1;
75         }
76
77         crc = ntohl(hdr->ih_hcrc);
78         hdr->ih_hcrc = 0;
79         len = sizeof (image_header_t);
80         data = (ulong)hdr;
81         if (crc32(0, (char *)data, len) != crc) {
82                 printf ("Bad header crc\n");
83                 return 1;
84         }
85
86         data = addr + sizeof(image_header_t);
87         len = ntohl(hdr->ih_size);
88
89         if (verify) {
90                 if (crc32(0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
91                         printf ("Bad data crc\n");
92                         return 1;
93                 }
94         }
95
96         if (hdr->ih_type != IH_TYPE_SCRIPT) {
97                 printf ("Bad image type\n");
98                 return 1;
99         }
100
101         /* get length of script */
102         len_ptr = (ulong *)data;
103
104         if ((len = ntohl(*len_ptr)) == 0) {
105                 printf ("Empty Script\n");
106                 return 1;
107         }
108
109         debug ("** Script length: %ld\n", len);
110
111         if ((cmd = malloc (len + 1)) == NULL) {
112                 return 1;
113         }
114
115         while (*len_ptr++);
116
117         /* make sure cmd is null terminated */
118         memmove (cmd, (char *)len_ptr, len);
119         *(cmd + len) = 0;
120
121 #ifdef CFG_HUSH_PARSER
122         rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON);
123 #else
124         {
125                 char *line = cmd;
126                 char *next = cmd;
127
128                 /*
129                  * break into individual lines,
130                  * and execute each line;
131                  * terminate on error.
132                  */
133                 while (*next) {
134                         if (*next == '\n') {
135                                 *next = '\0';
136                                 /* run only non-empty commands */
137                                 if ((next - line) > 1) {
138                                         debug ("** exec: \"%s\"\n",
139                                                 line);
140                                         if (run_command (line, 0) < 0) {
141                                                 rcode = 1;
142                                                 break;
143                                         }
144                                 }
145                                 line = next + 1;
146                         }
147                         ++next;
148                 }
149         }
150 #endif
151         free (cmd);
152         return rcode;
153 }
154
155 #endif  /* CONFIG_AUTOSCRIPT || CFG_CMD_AUTOSCRIPT */
156
157 #if (CONFIG_COMMANDS & CFG_CMD_AUTOSCRIPT)
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 }
174 #endif /* CFG_CMD_AUTOSCRIPT */