cmd_source.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. int
  40. source (ulong addr, const char *fit_uname)
  41. {
  42. ulong len;
  43. image_header_t *hdr;
  44. ulong *data;
  45. int verify;
  46. #if defined(CONFIG_FIT)
  47. const void* fit_hdr;
  48. int noffset;
  49. const void *fit_data;
  50. size_t fit_len;
  51. #endif
  52. verify = getenv_yesno ("verify");
  53. switch (genimg_get_format ((void *)addr)) {
  54. case IMAGE_FORMAT_LEGACY:
  55. hdr = (image_header_t *)addr;
  56. if (!image_check_magic (hdr)) {
  57. puts ("Bad magic number\n");
  58. return 1;
  59. }
  60. if (!image_check_hcrc (hdr)) {
  61. puts ("Bad header crc\n");
  62. return 1;
  63. }
  64. if (verify) {
  65. if (!image_check_dcrc (hdr)) {
  66. puts ("Bad data crc\n");
  67. return 1;
  68. }
  69. }
  70. if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
  71. puts ("Bad image type\n");
  72. return 1;
  73. }
  74. /* get length of script */
  75. data = (ulong *)image_get_data (hdr);
  76. if ((len = uimage_to_cpu (*data)) == 0) {
  77. puts ("Empty Script\n");
  78. return 1;
  79. }
  80. /*
  81. * scripts are just multi-image files with one component, seek
  82. * past the zero-terminated sequence of image lengths to get
  83. * to the actual image data
  84. */
  85. while (*data++);
  86. break;
  87. #if defined(CONFIG_FIT)
  88. case IMAGE_FORMAT_FIT:
  89. if (fit_uname == NULL) {
  90. puts ("No FIT subimage unit name\n");
  91. return 1;
  92. }
  93. fit_hdr = (const void *)addr;
  94. if (!fit_check_format (fit_hdr)) {
  95. puts ("Bad FIT image format\n");
  96. return 1;
  97. }
  98. /* get script component image node offset */
  99. noffset = fit_image_get_node (fit_hdr, fit_uname);
  100. if (noffset < 0) {
  101. printf ("Can't find '%s' FIT subimage\n", fit_uname);
  102. return 1;
  103. }
  104. if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
  105. puts ("Not a image image\n");
  106. return 1;
  107. }
  108. /* verify integrity */
  109. if (verify) {
  110. if (!fit_image_check_hashes (fit_hdr, noffset)) {
  111. puts ("Bad Data Hash\n");
  112. return 1;
  113. }
  114. }
  115. /* get script subimage data address and length */
  116. if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
  117. puts ("Could not find script subimage data\n");
  118. return 1;
  119. }
  120. data = (ulong *)fit_data;
  121. len = (ulong)fit_len;
  122. break;
  123. #endif
  124. default:
  125. puts ("Wrong image format for \"source\" command\n");
  126. return 1;
  127. }
  128. debug ("** Script length: %ld\n", len);
  129. return run_command_list((char *)data, len, 0);
  130. }
  131. /**************************************************/
  132. #if defined(CONFIG_CMD_SOURCE)
  133. int
  134. do_source (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  135. {
  136. ulong addr;
  137. int rcode;
  138. const char *fit_uname = NULL;
  139. /* Find script image */
  140. if (argc < 2) {
  141. addr = CONFIG_SYS_LOAD_ADDR;
  142. debug ("* source: default load address = 0x%08lx\n", addr);
  143. #if defined(CONFIG_FIT)
  144. } else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
  145. debug ("* source: subimage '%s' from FIT image at 0x%08lx\n",
  146. fit_uname, addr);
  147. #endif
  148. } else {
  149. addr = simple_strtoul(argv[1], NULL, 16);
  150. debug ("* source: cmdline image address = 0x%08lx\n", addr);
  151. }
  152. printf ("## Executing script at %08lx\n", addr);
  153. rcode = source (addr, fit_uname);
  154. return rcode;
  155. }
  156. U_BOOT_CMD(
  157. source, 2, 0, do_source,
  158. "run script from memory",
  159. "[addr]\n"
  160. "\t- run script starting at addr\n"
  161. "\t- A valid image header must be present"
  162. #if defined(CONFIG_FIT)
  163. "\n"
  164. "For FIT format uImage addr must include subimage\n"
  165. "unit name in the form of addr:<subimg_uname>"
  166. #endif
  167. );
  168. #endif