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