cmd_autoscript.c 4.3 KB

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