cmd_autoscript.c 4.1 KB

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