cmd_ximg.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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, checksum;
  39. ulong *len_ptr;
  40. int i, verify, part = 0;
  41. char pbuf[10], *s;
  42. image_header_t header;
  43. s = getenv("verify");
  44. verify = (s && (*s == 'n')) ? 0 : 1;
  45. if (argc > 1) {
  46. addr = simple_strtoul(argv[1], NULL, 16);
  47. }
  48. if (argc > 2) {
  49. part = simple_strtoul(argv[2], NULL, 16);
  50. }
  51. if (argc > 3) {
  52. dest = simple_strtoul(argv[3], NULL, 16);
  53. }
  54. printf("## Copying from image at %08lx ...\n", addr);
  55. /* Copy header so we can blank CRC field for re-calculation */
  56. memmove(&header, (char *) addr, sizeof (image_header_t));
  57. if (ntohl(header.ih_magic) != IH_MAGIC) {
  58. printf("Bad Magic Number\n");
  59. return 1;
  60. }
  61. data = (ulong) & header;
  62. len = sizeof (image_header_t);
  63. checksum = ntohl(header.ih_hcrc);
  64. header.ih_hcrc = 0;
  65. if (crc32(0, (char *) data, len) != checksum) {
  66. printf("Bad Header Checksum\n");
  67. return 1;
  68. }
  69. #ifdef DEBUG
  70. print_image_hdr((image_header_t *) addr);
  71. #endif
  72. data = addr + sizeof (image_header_t);
  73. len = ntohl(header.ih_size);
  74. if (header.ih_type != IH_TYPE_MULTI) {
  75. printf("Wrong Image Type for %s command\n", cmdtp->name);
  76. return 1;
  77. }
  78. if (header.ih_comp != IH_COMP_NONE) {
  79. printf("Wrong Compression Type for %s command\n", cmdtp->name);
  80. return 1;
  81. }
  82. if (verify) {
  83. printf(" Verifying Checksum ... ");
  84. if (crc32(0, (char *) data, len) != ntohl(header.ih_dcrc)) {
  85. printf("Bad Data CRC\n");
  86. return 1;
  87. }
  88. printf("OK\n");
  89. }
  90. len_ptr = (ulong *) data;
  91. data += 4; /* terminator */
  92. for (i = 0; len_ptr[i]; ++i) {
  93. data += 4;
  94. if (argc > 2 && part > i) {
  95. u_long tail;
  96. len = ntohl(len_ptr[i]);
  97. tail = len % 4;
  98. data += len;
  99. if (tail) {
  100. data += 4 - tail;
  101. }
  102. }
  103. }
  104. if (argc > 2 && part >= i) {
  105. printf("Bad Image Part\n");
  106. return 1;
  107. }
  108. len = ntohl(len_ptr[part]);
  109. if (argc > 3) {
  110. memcpy((char *) dest, (char *) data, len);
  111. }
  112. sprintf(pbuf, "%8lx", data);
  113. setenv("fileaddr", pbuf);
  114. sprintf(pbuf, "%8lx", len);
  115. setenv("filesize", pbuf);
  116. return 0;
  117. }
  118. U_BOOT_CMD(imxtract, 4, 1, do_imgextract,
  119. "imxtract- extract a part of a multi-image\n",
  120. "addr part [dest]\n"
  121. " - extract <part> from image at <addr> and copy to <dest>\n");
  122. #endif