default_image.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008 Semihalf
  4. *
  5. * (C) Copyright 2000-2004
  6. * DENX Software Engineering
  7. * Wolfgang Denk, wd@denx.de
  8. *
  9. * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
  10. * default_image specific code abstracted from mkimage.c
  11. * some functions added to address abstraction
  12. *
  13. * All rights reserved.
  14. */
  15. #include "imagetool.h"
  16. #include "mkimage.h"
  17. #include <image.h>
  18. #include <tee/optee.h>
  19. #include <u-boot/crc.h>
  20. static image_header_t header;
  21. static int image_check_image_types(uint8_t type)
  22. {
  23. if (((type > IH_TYPE_INVALID) && (type < IH_TYPE_FLATDT)) ||
  24. (type == IH_TYPE_KERNEL_NOLOAD) || (type == IH_TYPE_FIRMWARE_IVT))
  25. return EXIT_SUCCESS;
  26. else
  27. return EXIT_FAILURE;
  28. }
  29. static int image_check_params(struct image_tool_params *params)
  30. {
  31. return ((params->dflag && (params->fflag || params->lflag)) ||
  32. (params->fflag && (params->dflag || params->lflag)) ||
  33. (params->lflag && (params->dflag || params->fflag)));
  34. }
  35. static int image_verify_header(unsigned char *ptr, int image_size,
  36. struct image_tool_params *params)
  37. {
  38. uint32_t len;
  39. const unsigned char *data;
  40. uint32_t checksum;
  41. image_header_t header;
  42. image_header_t *hdr = &header;
  43. /*
  44. * create copy of header so that we can blank out the
  45. * checksum field for checking - this can't be done
  46. * on the PROT_READ mapped data.
  47. */
  48. memcpy(hdr, ptr, sizeof(image_header_t));
  49. if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
  50. debug("%s: Bad Magic Number: \"%s\" is no valid image\n",
  51. params->cmdname, params->imagefile);
  52. return -FDT_ERR_BADMAGIC;
  53. }
  54. data = (const unsigned char *)hdr;
  55. len = sizeof(image_header_t);
  56. checksum = be32_to_cpu(hdr->ih_hcrc);
  57. hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
  58. if (crc32(0, data, len) != checksum) {
  59. debug("%s: ERROR: \"%s\" has bad header checksum!\n",
  60. params->cmdname, params->imagefile);
  61. return -FDT_ERR_BADSTATE;
  62. }
  63. data = (const unsigned char *)ptr + sizeof(image_header_t);
  64. len = image_size - sizeof(image_header_t) ;
  65. checksum = be32_to_cpu(hdr->ih_dcrc);
  66. if (crc32(0, data, len) != checksum) {
  67. debug("%s: ERROR: \"%s\" has corrupted data!\n",
  68. params->cmdname, params->imagefile);
  69. return -FDT_ERR_BADSTRUCTURE;
  70. }
  71. return 0;
  72. }
  73. static void image_set_header(void *ptr, struct stat *sbuf, int ifd,
  74. struct image_tool_params *params)
  75. {
  76. uint32_t checksum;
  77. time_t time;
  78. uint32_t imagesize;
  79. uint32_t ep;
  80. uint32_t addr;
  81. image_header_t * hdr = (image_header_t *)ptr;
  82. checksum = crc32(0,
  83. (const unsigned char *)(ptr +
  84. sizeof(image_header_t)),
  85. sbuf->st_size - sizeof(image_header_t));
  86. time = imagetool_get_source_date(params, sbuf->st_mtime);
  87. ep = params->ep;
  88. addr = params->addr;
  89. if (params->type == IH_TYPE_FIRMWARE_IVT)
  90. /* Add size of CSF minus IVT */
  91. imagesize = sbuf->st_size - sizeof(image_header_t) + 0x1FE0;
  92. else
  93. imagesize = sbuf->st_size - sizeof(image_header_t);
  94. if (params->os == IH_OS_TEE) {
  95. addr = optee_image_get_load_addr(hdr);
  96. ep = optee_image_get_entry_point(hdr);
  97. }
  98. /* Build new header */
  99. image_set_magic(hdr, IH_MAGIC);
  100. image_set_time(hdr, time);
  101. image_set_size(hdr, imagesize);
  102. image_set_load(hdr, addr);
  103. image_set_ep(hdr, ep);
  104. image_set_dcrc(hdr, checksum);
  105. image_set_os(hdr, params->os);
  106. image_set_arch(hdr, params->arch);
  107. image_set_type(hdr, params->type);
  108. image_set_comp(hdr, params->comp);
  109. image_set_name(hdr, params->imagename);
  110. checksum = crc32(0, (const unsigned char *)hdr,
  111. sizeof(image_header_t));
  112. image_set_hcrc(hdr, checksum);
  113. }
  114. static int image_extract_subimage(void *ptr, struct image_tool_params *params)
  115. {
  116. const image_header_t *hdr = (const image_header_t *)ptr;
  117. ulong file_data;
  118. ulong file_len;
  119. if (image_check_type(hdr, IH_TYPE_MULTI)) {
  120. ulong idx = params->pflag;
  121. ulong count;
  122. /* get the number of data files present in the image */
  123. count = image_multi_count(hdr);
  124. /* retrieve the "data file" at the idx position */
  125. image_multi_getimg(hdr, idx, &file_data, &file_len);
  126. if ((file_len == 0) || (idx >= count)) {
  127. fprintf(stderr, "%s: No such data file %ld in \"%s\"\n",
  128. params->cmdname, idx, params->imagefile);
  129. return -1;
  130. }
  131. } else {
  132. file_data = image_get_data(hdr);
  133. file_len = image_get_size(hdr);
  134. }
  135. /* save the "data file" into the file system */
  136. return imagetool_save_subimage(params->outfile, file_data, file_len);
  137. }
  138. /*
  139. * Default image type parameters definition
  140. */
  141. U_BOOT_IMAGE_TYPE(
  142. defimage,
  143. "Default Image support",
  144. sizeof(image_header_t),
  145. (void *)&header,
  146. image_check_params,
  147. image_verify_header,
  148. image_print_contents,
  149. image_set_header,
  150. image_extract_subimage,
  151. image_check_image_types,
  152. NULL,
  153. NULL
  154. );