cmd_ximg.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #if defined(CONFIG_CMD_XIMG)
  27. /*
  28. * Multi Image extract
  29. */
  30. #include <common.h>
  31. #include <command.h>
  32. #include <image.h>
  33. #include <asm/byteorder.h>
  34. int
  35. do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  36. {
  37. ulong addr = load_addr;
  38. ulong dest = 0;
  39. ulong data, len, count;
  40. int i, verify;
  41. int part = 0;
  42. char pbuf[10];
  43. char *s;
  44. image_header_t *hdr;
  45. #if defined(CONFIG_FIT)
  46. const char *uname;
  47. const void* fit_hdr;
  48. int noffset;
  49. const void *fit_data;
  50. size_t fit_len;
  51. #endif
  52. verify = getenv_verify ();
  53. if (argc > 1) {
  54. addr = simple_strtoul(argv[1], NULL, 16);
  55. }
  56. if (argc > 2) {
  57. part = simple_strtoul(argv[2], NULL, 16);
  58. #if defined(CONFIG_FIT)
  59. uname = argv[2];
  60. #endif
  61. }
  62. if (argc > 3) {
  63. dest = simple_strtoul(argv[3], NULL, 16);
  64. }
  65. switch (genimg_get_format ((void *)addr)) {
  66. case IMAGE_FORMAT_LEGACY:
  67. printf("## Copying part %d from legacy image "
  68. "at %08lx ...\n", part, addr);
  69. hdr = (image_header_t *)addr;
  70. if (!image_check_magic (hdr)) {
  71. printf("Bad Magic Number\n");
  72. return 1;
  73. }
  74. if (!image_check_hcrc (hdr)) {
  75. printf("Bad Header Checksum\n");
  76. return 1;
  77. }
  78. #ifdef DEBUG
  79. image_print_contents (hdr);
  80. #endif
  81. if (!image_check_type (hdr, IH_TYPE_MULTI)) {
  82. printf("Wrong Image Type for %s command\n",
  83. cmdtp->name);
  84. return 1;
  85. }
  86. if (image_get_comp (hdr) != IH_COMP_NONE) {
  87. printf("Wrong Compression Type for %s command\n",
  88. cmdtp->name);
  89. return 1;
  90. }
  91. if (verify) {
  92. printf(" Verifying Checksum ... ");
  93. if (!image_check_dcrc (hdr)) {
  94. printf("Bad Data CRC\n");
  95. return 1;
  96. }
  97. printf("OK\n");
  98. }
  99. count = image_multi_count (hdr);
  100. if (part >= count) {
  101. printf("Bad Image Part\n");
  102. return 1;
  103. }
  104. image_multi_getimg (hdr, part, &data, &len);
  105. break;
  106. #if defined(CONFIG_FIT)
  107. case IMAGE_FORMAT_FIT:
  108. if (uname == NULL) {
  109. puts ("No FIT subimage unit name\n");
  110. return 1;
  111. }
  112. printf("## Copying '%s' subimage from FIT image "
  113. "at %08lx ...\n", uname, addr);
  114. fit_hdr = (const void *)addr;
  115. if (!fit_check_format (fit_hdr)) {
  116. puts ("Bad FIT image format\n");
  117. return 1;
  118. }
  119. /* get subimage node offset */
  120. noffset = fit_image_get_node (fit_hdr, fit_uname);
  121. if (noffset < 0) {
  122. printf ("Can't find '%s' FIT subimage\n", uname);
  123. return 1;
  124. }
  125. if (fit_image_check_comp (fit_hdr, noffset, IH_COMP_NONE)) {
  126. printf("Wrong Compression Type for %s command\n",
  127. cmdtp->name);
  128. return 1;
  129. }
  130. /* verify integrity */
  131. if (verify) {
  132. if (!fit_image_check_hashes (fit_hdr, noffset)) {
  133. puts ("Bad Data Hash\n");
  134. return 1;
  135. }
  136. }
  137. /* get subimage data address and length */
  138. if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) {
  139. puts ("Could not find script subimage data\n");
  140. return 1;
  141. }
  142. data = (ulong *)fit_data;
  143. len = (ulong)fit_len;
  144. break;
  145. #endif
  146. default:
  147. puts ("Invalid image type for imxtract\n");
  148. return 1;
  149. }
  150. if (argc > 3) {
  151. memcpy((char *) dest, (char *) data, len);
  152. }
  153. sprintf(pbuf, "%8lx", data);
  154. setenv("fileaddr", pbuf);
  155. sprintf(pbuf, "%8lx", len);
  156. setenv("filesize", pbuf);
  157. return 0;
  158. }
  159. U_BOOT_CMD(imxtract, 4, 1, do_imgextract,
  160. "imxtract- extract a part of a multi-image\n",
  161. "addr part [dest]\n"
  162. " - extract <part> from legacy image at <addr> and copy to <dest>\n"
  163. #if defined(CONFIG_FIT)
  164. "addr uname [dest]\n"
  165. " - extract <uname> subimage from FIT image at <addr> and copy to <dest>\n"
  166. #endif
  167. );
  168. #endif