cmd_ximg.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, dest = 0;
  38. ulong data, len;
  39. ulong *len_ptr;
  40. int i, verify, part = 0;
  41. char pbuf[10], *s;
  42. image_header_t *hdr;
  43. verify = getenv_verify ();
  44. if (argc > 1) {
  45. addr = simple_strtoul(argv[1], NULL, 16);
  46. }
  47. if (argc > 2) {
  48. part = simple_strtoul(argv[2], NULL, 16);
  49. }
  50. if (argc > 3) {
  51. dest = simple_strtoul(argv[3], NULL, 16);
  52. }
  53. switch (gen_image_get_format ((void *)addr)) {
  54. case IMAGE_FORMAT_LEGACY:
  55. printf("## Copying from legacy image at %08lx ...\n", addr);
  56. hdr = (image_header_t *)addr;
  57. if (!image_check_magic (hdr)) {
  58. printf("Bad Magic Number\n");
  59. return 1;
  60. }
  61. if (!image_check_hcrc (hdr)) {
  62. printf("Bad Header Checksum\n");
  63. return 1;
  64. }
  65. #ifdef DEBUG
  66. image_print_contents (hdr);
  67. #endif
  68. if (!image_check_type (hdr, IH_TYPE_MULTI)) {
  69. printf("Wrong Image Type for %s command\n",
  70. cmdtp->name);
  71. return 1;
  72. }
  73. if (image_get_comp (hdr) != IH_COMP_NONE) {
  74. printf("Wrong Compression Type for %s command\n",
  75. cmdtp->name);
  76. return 1;
  77. }
  78. if (verify) {
  79. printf(" Verifying Checksum ... ");
  80. if (!image_check_dcrc (hdr)) {
  81. printf("Bad Data CRC\n");
  82. return 1;
  83. }
  84. printf("OK\n");
  85. }
  86. data = image_get_data (hdr);
  87. len_ptr = (ulong *) data;
  88. data += 4; /* terminator */
  89. for (i = 0; len_ptr[i]; ++i) {
  90. data += 4;
  91. if (argc > 2 && part > i) {
  92. u_long tail;
  93. len = image_to_cpu (len_ptr[i]);
  94. tail = len % 4;
  95. data += len;
  96. if (tail) {
  97. data += 4 - tail;
  98. }
  99. }
  100. }
  101. if (argc > 2 && part >= i) {
  102. printf("Bad Image Part\n");
  103. return 1;
  104. }
  105. len = image_to_cpu (len_ptr[part]);
  106. #if defined(CONFIG_FIT)
  107. case IMAGE_FORMAT_FIT:
  108. fit_unsupported ("imxtract");
  109. return 1;
  110. #endif
  111. default:
  112. puts ("Invalid image type for imxtract\n");
  113. return 1;
  114. }
  115. if (argc > 3) {
  116. memcpy((char *) dest, (char *) data, len);
  117. }
  118. sprintf(pbuf, "%8lx", data);
  119. setenv("fileaddr", pbuf);
  120. sprintf(pbuf, "%8lx", len);
  121. setenv("filesize", pbuf);
  122. return 0;
  123. }
  124. U_BOOT_CMD(imxtract, 4, 1, do_imgextract,
  125. "imxtract- extract a part of a multi-image\n",
  126. "addr part [dest]\n"
  127. " - extract <part> from image at <addr> and copy to <dest>\n");
  128. #endif