cmd_source.c 4.7 KB

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