cmd_autoscript.c 5.6 KB

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