cmd_ximg.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. printf("## Copying from image at %08lx ...\n", addr);
  54. hdr = (image_header_t *)addr;
  55. if (!image_check_magic (hdr)) {
  56. printf("Bad Magic Number\n");
  57. return 1;
  58. }
  59. if (!image_check_hcrc (hdr)) {
  60. printf("Bad Header Checksum\n");
  61. return 1;
  62. }
  63. #ifdef DEBUG
  64. print_image_hdr (hdr);
  65. #endif
  66. if (!image_check_type (hdr, IH_TYPE_MULTI)) {
  67. printf("Wrong Image Type for %s command\n", cmdtp->name);
  68. return 1;
  69. }
  70. if (image_get_comp (hdr) != IH_COMP_NONE) {
  71. printf("Wrong Compression Type for %s command\n", cmdtp->name);
  72. return 1;
  73. }
  74. if (verify) {
  75. printf(" Verifying Checksum ... ");
  76. if (!image_check_dcrc (hdr)) {
  77. printf("Bad Data CRC\n");
  78. return 1;
  79. }
  80. printf("OK\n");
  81. }
  82. data = image_get_data (hdr);
  83. len_ptr = (ulong *) data;
  84. data += 4; /* terminator */
  85. for (i = 0; len_ptr[i]; ++i) {
  86. data += 4;
  87. if (argc > 2 && part > i) {
  88. u_long tail;
  89. len = image_to_cpu (len_ptr[i]);
  90. tail = len % 4;
  91. data += len;
  92. if (tail) {
  93. data += 4 - tail;
  94. }
  95. }
  96. }
  97. if (argc > 2 && part >= i) {
  98. printf("Bad Image Part\n");
  99. return 1;
  100. }
  101. len = image_to_cpu (len_ptr[part]);
  102. if (argc > 3) {
  103. memcpy((char *) dest, (char *) data, len);
  104. }
  105. sprintf(pbuf, "%8lx", data);
  106. setenv("fileaddr", pbuf);
  107. sprintf(pbuf, "%8lx", len);
  108. setenv("filesize", pbuf);
  109. return 0;
  110. }
  111. U_BOOT_CMD(imxtract, 4, 1, do_imgextract,
  112. "imxtract- extract a part of a multi-image\n",
  113. "addr part [dest]\n"
  114. " - extract <part> from image at <addr> and copy to <dest>\n");
  115. #endif