default_image.c 4.9 KB

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