cmd_source.c 4.0 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 <asm/byteorder.h>
  20. #include <asm/io.h>
  21. #if defined(CONFIG_8xx)
  22. #include <mpc8xx.h>
  23. #endif
  24. int
  25. source (ulong addr, const char *fit_uname)
  26. {
  27. ulong len;
  28. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  29. const image_header_t *hdr;
  30. #endif
  31. ulong *data;
  32. int verify;
  33. void *buf;
  34. #if defined(CONFIG_FIT)
  35. const void* fit_hdr;
  36. int noffset;
  37. const void *fit_data;
  38. size_t fit_len;
  39. #endif
  40. verify = getenv_yesno ("verify");
  41. buf = map_sysmem(addr, 0);
  42. switch (genimg_get_format(buf)) {
  43. #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
  44. case IMAGE_FORMAT_LEGACY:
  45. hdr = buf;
  46. if (!image_check_magic (hdr)) {
  47. puts ("Bad magic number\n");
  48. return 1;
  49. }
  50. if (!image_check_hcrc (hdr)) {
  51. puts ("Bad header crc\n");
  52. return 1;
  53. }
  54. if (verify) {
  55. if (!image_check_dcrc (hdr)) {
  56. puts ("Bad data crc\n");
  57. return 1;
  58. }
  59. }
  60. if (!image_check_type (hdr, IH_TYPE_SCRIPT)) {
  61. puts ("Bad image type\n");
  62. return 1;
  63. }
  64. /* get length of script */
  65. data = (ulong *)image_get_data (hdr);
  66. if ((len = uimage_to_cpu (*data)) == 0) {
  67. puts ("Empty Script\n");
  68. return 1;
  69. }
  70. /*
  71. * scripts are just multi-image files with one component, seek
  72. * past the zero-terminated sequence of image lengths to get
  73. * to the actual image data
  74. */
  75. while (*data++);
  76. break;
  77. #endif
  78. #if defined(CONFIG_FIT)
  79. case IMAGE_FORMAT_FIT:
  80. if (fit_uname == NULL) {
  81. puts ("No FIT subimage unit name\n");
  82. return 1;
  83. }
  84. fit_hdr = buf;
  85. if (!fit_check_format (fit_hdr)) {
  86. puts ("Bad FIT image format\n");
  87. return 1;
  88. }
  89. /* get script component image node offset */
  90. noffset = fit_image_get_node (fit_hdr, fit_uname);
  91. if (noffset < 0) {
  92. printf ("Can't find '%s' FIT subimage\n", fit_uname);
  93. return 1;
  94. }
  95. if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) {
  96. puts ("Not a image image\n");
  97. return 1;
  98. }
  99. /* verify integrity */
  100. if (verify) {
  101. if (!fit_image_verify(fit_hdr, noffset)) {
  102. puts ("Bad Data Hash\n");
  103. return 1;
  104. }
  105. }
  106. /* get script subimage data address and length */
  107. if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
  108. puts ("Could not find script subimage data\n");
  109. return 1;
  110. }
  111. data = (ulong *)fit_data;
  112. len = (ulong)fit_len;
  113. break;
  114. #endif
  115. default:
  116. puts ("Wrong image format for \"source\" command\n");
  117. return 1;
  118. }
  119. debug ("** Script length: %ld\n", len);
  120. return run_command_list((char *)data, len, 0);
  121. }
  122. /**************************************************/
  123. #if defined(CONFIG_CMD_SOURCE)
  124. int
  125. 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