cmd_autoscript.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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)
  49. {
  50. ulong len;
  51. image_header_t *hdr;
  52. ulong *data;
  53. char *cmd;
  54. int rcode = 0;
  55. int verify;
  56. verify = getenv_verify ();
  57. switch (genimg_get_format ((void *)addr)) {
  58. case IMAGE_FORMAT_LEGACY:
  59. hdr = (image_header_t *)addr;
  60. if (!image_check_magic (hdr)) {
  61. puts ("Bad magic number\n");
  62. return 1;
  63. }
  64. if (!image_check_hcrc (hdr)) {
  65. puts ("Bad header crc\n");
  66. return 1;
  67. }
  68. if (verify) {
  69. if (!image_check_dcrc (hdr)) {
  70. puts ("Bad data crc\n");
  71. return 1;
  72. }
  73. }
  74. if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
  75. puts ("Bad image type\n");
  76. return 1;
  77. }
  78. /* get length of script */
  79. data = (ulong *)image_get_data (hdr);
  80. if ((len = uimage_to_cpu (*data)) == 0) {
  81. puts ("Empty Script\n");
  82. return 1;
  83. }
  84. break;
  85. #if defined(CONFIG_FIT)
  86. case IMAGE_FORMAT_FIT:
  87. fit_unsupported ("autoscript");
  88. return 1;
  89. #endif
  90. default:
  91. puts ("Wrong image format for autoscript\n");
  92. return 1;
  93. }
  94. debug ("** Script length: %ld\n", len);
  95. if ((cmd = malloc (len + 1)) == NULL) {
  96. return 1;
  97. }
  98. while (*data++);
  99. /* make sure cmd is null terminated */
  100. memmove (cmd, (char *)data, len);
  101. *(cmd + len) = 0;
  102. #ifdef CFG_HUSH_PARSER /*?? */
  103. rcode = parse_string_outer (cmd, FLAG_PARSE_SEMICOLON);
  104. #else
  105. {
  106. char *line = cmd;
  107. char *next = cmd;
  108. /*
  109. * break into individual lines,
  110. * and execute each line;
  111. * terminate on error.
  112. */
  113. while (*next) {
  114. if (*next == '\n') {
  115. *next = '\0';
  116. /* run only non-empty commands */
  117. if ((next - line) > 1) {
  118. debug ("** exec: \"%s\"\n",
  119. line);
  120. if (run_command (line, 0) < 0) {
  121. rcode = 1;
  122. break;
  123. }
  124. }
  125. line = next + 1;
  126. }
  127. ++next;
  128. }
  129. }
  130. #endif
  131. free (cmd);
  132. return rcode;
  133. }
  134. #endif
  135. /**************************************************/
  136. #if defined(CONFIG_CMD_AUTOSCRIPT)
  137. int
  138. do_autoscript (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  139. {
  140. ulong addr;
  141. int rcode;
  142. if (argc < 2) {
  143. addr = CFG_LOAD_ADDR;
  144. } else {
  145. addr = simple_strtoul (argv[1],0,16);
  146. }
  147. printf ("## Executing script at %08lx\n",addr);
  148. rcode = autoscript (addr);
  149. return rcode;
  150. }
  151. #if defined(CONFIG_CMD_AUTOSCRIPT)
  152. U_BOOT_CMD(
  153. autoscr, 2, 0, do_autoscript,
  154. "autoscr - run script from memory\n",
  155. "[addr] - run script starting at addr"
  156. " - A valid autoscr header must be present\n"
  157. );
  158. #endif
  159. #endif