cmd_ximg.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * (C) Copyright 2000-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2003
  6. * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * Multi Image extract
  28. */
  29. #include <common.h>
  30. #include <command.h>
  31. #include <image.h>
  32. #include <asm/byteorder.h>
  33. int
  34. do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  35. {
  36. ulong addr = load_addr;
  37. ulong dest = 0;
  38. ulong data, len, count;
  39. int verify;
  40. int part = 0;
  41. char pbuf[10];
  42. image_header_t *hdr;
  43. #if defined(CONFIG_FIT)
  44. const char *uname = NULL;
  45. const void* fit_hdr;
  46. int noffset;
  47. const void *fit_data;
  48. size_t fit_len;
  49. #endif
  50. verify = getenv_yesno ("verify");
  51. if (argc > 1) {
  52. addr = simple_strtoul(argv[1], NULL, 16);
  53. }
  54. if (argc > 2) {
  55. part = simple_strtoul(argv[2], NULL, 16);
  56. #if defined(CONFIG_FIT)
  57. uname = argv[2];
  58. #endif
  59. }
  60. if (argc > 3) {
  61. dest = simple_strtoul(argv[3], NULL, 16);
  62. }
  63. switch (genimg_get_format ((void *)addr)) {
  64. case IMAGE_FORMAT_LEGACY:
  65. printf("## Copying part %d from legacy image "
  66. "at %08lx ...\n", part, addr);
  67. hdr = (image_header_t *)addr;
  68. if (!image_check_magic (hdr)) {
  69. printf("Bad Magic Number\n");
  70. return 1;
  71. }
  72. if (!image_check_hcrc (hdr)) {
  73. printf("Bad Header Checksum\n");
  74. return 1;
  75. }
  76. #ifdef DEBUG
  77. image_print_contents (hdr);
  78. #endif
  79. if (!image_check_type (hdr, IH_TYPE_MULTI)) {
  80. printf("Wrong Image Type for %s command\n",
  81. cmdtp->name);
  82. return 1;
  83. }
  84. if (image_get_comp (hdr) != IH_COMP_NONE) {
  85. printf("Wrong Compression Type for %s command\n",
  86. cmdtp->name);
  87. return 1;
  88. }
  89. if (verify) {
  90. printf(" Verifying Checksum ... ");
  91. if (!image_check_dcrc (hdr)) {
  92. printf("Bad Data CRC\n");
  93. return 1;
  94. }
  95. printf("OK\n");
  96. }
  97. count = image_multi_count (hdr);
  98. if (part >= count) {
  99. printf("Bad Image Part\n");
  100. return 1;
  101. }
  102. image_multi_getimg (hdr, part, &data, &len);
  103. break;
  104. #if defined(CONFIG_FIT)
  105. case IMAGE_FORMAT_FIT:
  106. if (uname == NULL) {
  107. puts ("No FIT subimage unit name\n");
  108. return 1;
  109. }
  110. printf("## Copying '%s' subimage from FIT image "
  111. "at %08lx ...\n", uname, addr);
  112. fit_hdr = (const void *)addr;
  113. if (!fit_check_format (fit_hdr)) {
  114. puts ("Bad FIT image format\n");
  115. return 1;
  116. }
  117. /* get subimage node offset */
  118. noffset = fit_image_get_node (fit_hdr, uname);
  119. if (noffset < 0) {
  120. printf ("Can't find '%s' FIT subimage\n", uname);
  121. return 1;
  122. }
  123. if (fit_image_check_comp (fit_hdr, noffset, IH_COMP_NONE)) {
  124. printf("Wrong Compression Type for %s command\n",
  125. cmdtp->name);
  126. return 1;
  127. }
  128. /* verify integrity */
  129. if (verify) {
  130. if (!fit_image_check_hashes (fit_hdr, noffset)) {
  131. puts ("Bad Data Hash\n");
  132. return 1;
  133. }
  134. }
  135. /* get subimage data address and length */
  136. if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
  137. puts ("Could not find script subimage data\n");
  138. return 1;
  139. }
  140. data = (ulong)fit_data;
  141. len = (ulong)fit_len;
  142. break;
  143. #endif
  144. default:
  145. puts ("Invalid image type for imxtract\n");
  146. return 1;
  147. }
  148. if (argc > 3) {
  149. memcpy((char *) dest, (char *) data, len);
  150. }
  151. sprintf(pbuf, "%8lx", data);
  152. setenv("fileaddr", pbuf);
  153. sprintf(pbuf, "%8lx", len);
  154. setenv("filesize", pbuf);
  155. return 0;
  156. }
  157. U_BOOT_CMD(imxtract, 4, 1, do_imgextract,
  158. "extract a part of a multi-image",
  159. "addr part [dest]\n"
  160. " - extract <part> from legacy image at <addr> and copy to <dest>"
  161. #if defined(CONFIG_FIT)
  162. "\n"
  163. "addr uname [dest]\n"
  164. " - extract <uname> subimage from FIT image at <addr> and copy to <dest>"
  165. #endif
  166. );