source.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * (C) Copyright 2001
  3. * Kyle Harris, kharris@nexus-tech.net
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * The "source" command allows to define "script images", i. e. files
  9. * that contain command sequences that can be executed by the command
  10. * interpreter. It returns the exit status of the last command
  11. * executed from the script. This is very similar to running a shell
  12. * script in a UNIX shell, hence the name for the command.
  13. */
  14. /* #define DEBUG */
  15. #include <common.h>
  16. #include <command.h>
  17. #include <image.h>
  18. #include <malloc.h>
  19. #include <mapmem.h>
  20. #include <asm/byteorder.h>
  21. #include <asm/io.h>
  22. #if defined(CONFIG_8xx)
  23. #include <mpc8xx.h>
  24. #endif
  25. int
  26. source (ulong addr, const char *fit_uname)
  27. {
  28. ulong len;
  29. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  30. const image_header_t *hdr;
  31. #endif
  32. u32 *data;
  33. int verify;
  34. void *buf;
  35. #if defined(CONFIG_FIT)
  36. const void* fit_hdr;
  37. int noffset;
  38. const void *fit_data;
  39. size_t fit_len;
  40. #endif
  41. verify = getenv_yesno ("verify");
  42. buf = map_sysmem(addr, 0);
  43. switch (genimg_get_format(buf)) {
  44. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  45. case IMAGE_FORMAT_LEGACY:
  46. hdr = buf;
  47. if (!image_check_magic (hdr)) {
  48. puts ("Bad magic number\n");
  49. return 1;
  50. }
  51. if (!image_check_hcrc (hdr)) {
  52. puts ("Bad header crc\n");
  53. return 1;
  54. }
  55. if (verify) {
  56. if (!image_check_dcrc (hdr)) {
  57. puts ("Bad data crc\n");
  58. return 1;
  59. }
  60. }
  61. if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
  62. puts ("Bad image type\n");
  63. return 1;
  64. }
  65. /* get length of script */
  66. data = (u32 *)image_get_data (hdr);
  67. if ((len = uimage_to_cpu (*data)) == 0) {
  68. puts ("Empty Script\n");
  69. return 1;
  70. }
  71. /*
  72. * scripts are just multi-image files with one component, seek
  73. * past the zero-terminated sequence of image lengths to get
  74. * to the actual image data
  75. */
  76. while (*data++);
  77. break;
  78. #endif
  79. #if defined(CONFIG_FIT)
  80. case IMAGE_FORMAT_FIT:
  81. if (fit_uname == NULL) {
  82. puts ("No FIT subimage unit name\n");
  83. return 1;
  84. }
  85. fit_hdr = buf;
  86. if (!fit_check_format (fit_hdr)) {
  87. puts ("Bad FIT image format\n");
  88. return 1;
  89. }
  90. /* get script component image node offset */
  91. noffset = fit_image_get_node (fit_hdr, fit_uname);
  92. if (noffset < 0) {
  93. printf ("Can't find '%s' FIT subimage\n", fit_uname);
  94. return 1;
  95. }
  96. if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
  97. puts ("Not a image image\n");
  98. return 1;
  99. }
  100. /* verify integrity */
  101. if (verify) {
  102. if (!fit_image_verify(fit_hdr, noffset)) {
  103. puts ("Bad Data Hash\n");
  104. return 1;
  105. }
  106. }
  107. /* get script subimage data address and length */
  108. if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
  109. puts ("Could not find script subimage data\n");
  110. return 1;
  111. }
  112. data = (u32 *)fit_data;
  113. len = (ulong)fit_len;
  114. break;
  115. #endif
  116. default:
  117. puts ("Wrong image format for \"source\" command\n");
  118. return 1;
  119. }
  120. debug ("** Script length: %ld\n", len);
  121. return run_command_list((char *)data, len, 0);
  122. }
  123. /**************************************************/
  124. #if defined(CONFIG_CMD_SOURCE)
  125. static int do_source(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  126. {
  127. ulong addr;
  128. int rcode;
  129. const char *fit_uname = NULL;
  130. /* Find script image */
  131. if (argc < 2) {
  132. addr = CONFIG_SYS_LOAD_ADDR;
  133. debug ("* source: default load address = 0x%08lx\n", addr);
  134. #if defined(CONFIG_FIT)
  135. } else if (fit_parse_subimage (argv[1], load_addr, &addr, &fit_uname)) {
  136. debug ("* source: subimage '%s' from FIT image at 0x%08lx\n",
  137. fit_uname, addr);
  138. #endif
  139. } else {
  140. addr = simple_strtoul(argv[1], NULL, 16);
  141. debug ("* source: cmdline image address = 0x%08lx\n", addr);
  142. }
  143. printf ("## Executing script at %08lx\n", addr);
  144. rcode = source (addr, fit_uname);
  145. return rcode;
  146. }
  147. #ifdef CONFIG_SYS_LONGHELP
  148. static char source_help_text[] =
  149. "[addr]\n"
  150. "\t- run script starting at addr\n"
  151. "\t- A valid image header must be present"
  152. #if defined(CONFIG_FIT)
  153. "\n"
  154. "For FIT format uImage addr must include subimage\n"
  155. "unit name in the form of addr:<subimg_uname>"
  156. #endif
  157. "";
  158. #endif
  159. U_BOOT_CMD(
  160. source, 2, 0, do_source,
  161. "run script from memory", source_help_text
  162. );
  163. #endif