cmd_autoscript.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * autoscript allows a remote host to download a command file and,
  25. * optionally, binary data for automatically updating the target. For
  26. * example, you create a new kernel image and want the user to be
  27. * able to simply download the image and the machine does the rest.
  28. * The kernel image is postprocessed with mkimage, which creates an
  29. * image with a script file prepended. If enabled, autoscript will
  30. * verify the script and contents of the download and execute the
  31. * script portion. This would be responsible for erasing flash,
  32. * copying the new image, and rebooting the machine.
  33. */
  34. /* #define DEBUG */
  35. #include <common.h>
  36. #include <command.h>
  37. #include <image.h>
  38. #include <malloc.h>
  39. #include <asm/byteorder.h>
  40. #if defined(CONFIG_8xx)
  41. #include <mpc8xx.h>
  42. #endif
  43. #ifdef CFG_HUSH_PARSER
  44. #include <hush.h>
  45. #endif
  46. #if defined(CONFIG_AUTOSCRIPT) || defined(CONFIG_CMD_AUTOSCRIPT)
  47. int
  48. autoscript (ulong addr, const char *fit_uname)
  49. {
  50. ulong len;
  51. image_header_t *hdr;
  52. ulong *data;
  53. char *cmd;
  54. int rcode = 0;
  55. int verify;
  56. #if defined(CONFIG_FIT)
  57. const void* fit_hdr;
  58. int noffset;
  59. const void *fit_data;
  60. size_t fit_len;
  61. #endif
  62. verify = getenv_yesno ("verify");
  63. switch (genimg_get_format ((void *)addr)) {
  64. case IMAGE_FORMAT_LEGACY:
  65. hdr = (image_header_t *)addr;
  66. if (!image_check_magic (hdr)) {
  67. puts ("Bad magic number\n");
  68. return 1;
  69. }
  70. if (!image_check_hcrc (hdr)) {
  71. puts ("Bad header crc\n");
  72. return 1;
  73. }
  74. if (verify) {
  75. if (!image_check_dcrc (hdr)) {
  76. puts ("Bad data crc\n");
  77. return 1;
  78. }
  79. }
  80. if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
  81. puts ("Bad image type\n");
  82. return 1;
  83. }
  84. /* get length of script */
  85. data = (ulong *)image_get_data (hdr);
  86. if ((len = uimage_to_cpu (*data)) == 0) {
  87. puts ("Empty Script\n");
  88. return 1;
  89. }
  90. /*
  91. * scripts are just multi-image files with one component, seek
  92. * past the zero-terminated sequence of image lengths to get
  93. * to the actual image data
  94. */
  95. while (*data++);
  96. break;
  97. #if defined(CONFIG_FIT)
  98. case IMAGE_FORMAT_FIT:
  99. if (fit_uname == NULL) {
  100. puts ("No FIT subimage unit name\n");
  101. return 1;
  102. }
  103. fit_hdr = (const void *)addr;
  104. if (!fit_check_format (fit_hdr)) {
  105. puts ("Bad FIT image format\n");
  106. return 1;
  107. }
  108. /* get script component image node offset */
  109. noffset = fit_image_get_node (fit_hdr, fit_uname);
  110. if (noffset < 0) {
  111. printf ("Can't find '%s' FIT subimage\n", fit_uname);
  112. return 1;
  113. }
  114. if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
  115. puts ("Not a image image\n");
  116. return 1;
  117. }
  118. /* verify integrity */
  119. if (verify) {
  120. if (!fit_image_check_hashes (fit_hdr, noffset)) {
  121. puts ("Bad Data Hash\n");
  122. return 1;
  123. }
  124. }
  125. /* get script subimage data address and length */
  126. if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
  127. puts ("Could not find script subimage data\n");
  128. return 1;
  129. }
  130. data = (ulong *)fit_data;
  131. len = (ulong)fit_len;
  132. break;
  133. #endif
  134. default:
  135. puts ("Wrong image format for autoscript\n");
  136. return 1;
  137. }
  138. debug ("** Script length: %ld\n", len);
  139. if ((cmd = malloc (len + 1)) == NULL) {
  140. return 1;
  141. }
  142. /* make sure cmd is null terminated */
  143. memmove (cmd, (char *)data, len);
  144. *(cmd + len) = 0;
  145. #ifdef CFG_HUSH_PARSER /*?? */
  146. rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON);
  147. #else
  148. {
  149. char *line = cmd;
  150. char *next = cmd;
  151. /*
  152. * break into individual lines,
  153. * and execute each line;
  154. * terminate on error.
  155. */
  156. while (*next) {
  157. if (*next == '\n') {
  158. *next = '\0';
  159. /* run only non-empty commands */
  160. if ((next - line) > 1) {
  161. debug ("** exec: \"%s\"\n",
  162. line);
  163. if (run_command (line, 0) < 0) {
  164. rcode = 1;
  165. break;
  166. }
  167. }
  168. line = next + 1;
  169. }
  170. ++next;
  171. }
  172. }
  173. #endif
  174. free (cmd);
  175. return rcode;
  176. }
  177. #endif
  178. /**************************************************/
  179. #if defined(CONFIG_CMD_AUTOSCRIPT)
  180. int
  181. do_autoscript (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  182. {
  183. ulong addr;
  184. int rcode;
  185. const char *fit_uname = NULL;
  186. /* Find script image */
  187. if (argc < 2) {
  188. addr = CFG_LOAD_ADDR;
  189. debug ("* autoscr: default load address = 0x%08lx\n", addr);
  190. #if defined(CONFIG_FIT)
  191. } else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
  192. debug ("* autoscr: subimage '%s' from FIT image at 0x%08lx\n",
  193. fit_uname, addr);
  194. #endif
  195. } else {
  196. addr = simple_strtoul(argv[1], NULL, 16);
  197. debug ("* autoscr: cmdline image address = 0x%08lx\n", addr);
  198. }
  199. printf ("## Executing script at %08lx\n", addr);
  200. rcode = autoscript (addr, fit_uname);
  201. return rcode;
  202. }
  203. U_BOOT_CMD(
  204. autoscr, 2, 0, do_autoscript,
  205. "autoscr - run script from memory\n",
  206. "[addr] - run script starting at addr"
  207. " - A valid autoscr header must be present\n"
  208. #if defined(CONFIG_FIT)
  209. "For FIT format uImage addr must include subimage\n"
  210. "unit name in the form of addr:<subimg_uname>\n"
  211. #endif
  212. );
  213. #endif