omapimage.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2010
  4. * Linaro LTD, www.linaro.org
  5. * Author: John Rigby <john.rigby@linaro.org>
  6. * Based on TI's signGP.c
  7. *
  8. * (C) Copyright 2009
  9. * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
  10. *
  11. * (C) Copyright 2008
  12. * Marvell Semiconductor <www.marvell.com>
  13. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  14. */
  15. #include "imagetool.h"
  16. #include <compiler.h>
  17. #include <image.h>
  18. #include "gpheader.h"
  19. #include "omapimage.h"
  20. #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
  21. /* Header size is CH header rounded up to 512 bytes plus GP header */
  22. #define OMAP_CH_HDR_SIZE 512
  23. #define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE + GPIMAGE_HDR_SIZE)
  24. static int do_swap32 = 0;
  25. static uint8_t omapimage_header[OMAP_FILE_HDR_SIZE];
  26. static int omapimage_check_image_types(uint8_t type)
  27. {
  28. if (type == IH_TYPE_OMAPIMAGE)
  29. return EXIT_SUCCESS;
  30. return EXIT_FAILURE;
  31. }
  32. static int omapimage_verify_header(unsigned char *ptr, int image_size,
  33. struct image_tool_params *params)
  34. {
  35. struct ch_toc *toc = (struct ch_toc *)ptr;
  36. struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
  37. uint32_t offset, size;
  38. while (toc->section_offset != 0xffffffff
  39. && toc->section_size != 0xffffffff) {
  40. if (do_swap32) {
  41. offset = cpu_to_be32(toc->section_offset);
  42. size = cpu_to_be32(toc->section_size);
  43. } else {
  44. offset = toc->section_offset;
  45. size = toc->section_size;
  46. }
  47. if (!offset || !size)
  48. return -1;
  49. if (offset >= OMAP_CH_HDR_SIZE ||
  50. offset+size >= OMAP_CH_HDR_SIZE)
  51. return -1;
  52. toc++;
  53. }
  54. return gph_verify_header(gph, do_swap32);
  55. }
  56. static void omapimage_print_section(struct ch_settings *chs)
  57. {
  58. const char *section_name;
  59. if (chs->section_key)
  60. section_name = "CHSETTINGS";
  61. else
  62. section_name = "UNKNOWNKEY";
  63. printf("%s (%x) "
  64. "valid:%x "
  65. "version:%x "
  66. "reserved:%x "
  67. "flags:%x\n",
  68. section_name,
  69. chs->section_key,
  70. chs->valid,
  71. chs->version,
  72. chs->reserved,
  73. chs->flags);
  74. }
  75. static void omapimage_print_header(const void *ptr)
  76. {
  77. const struct ch_toc *toc = (struct ch_toc *)ptr;
  78. const struct gp_header *gph =
  79. (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
  80. uint32_t offset, size;
  81. while (toc->section_offset != 0xffffffff
  82. && toc->section_size != 0xffffffff) {
  83. if (do_swap32) {
  84. offset = cpu_to_be32(toc->section_offset);
  85. size = cpu_to_be32(toc->section_size);
  86. } else {
  87. offset = toc->section_offset;
  88. size = toc->section_size;
  89. }
  90. if (offset >= OMAP_CH_HDR_SIZE ||
  91. offset+size >= OMAP_CH_HDR_SIZE)
  92. exit(EXIT_FAILURE);
  93. printf("Section %s offset %x length %x\n",
  94. toc->section_name,
  95. toc->section_offset,
  96. toc->section_size);
  97. omapimage_print_section((struct ch_settings *)(ptr+offset));
  98. toc++;
  99. }
  100. gph_print_header(gph, do_swap32);
  101. }
  102. static int toc_offset(void *hdr, void *member)
  103. {
  104. return member - hdr;
  105. }
  106. static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  107. struct image_tool_params *params)
  108. {
  109. struct ch_toc *toc = (struct ch_toc *)ptr;
  110. struct ch_settings *chs = (struct ch_settings *)
  111. (ptr + 2 * sizeof(*toc));
  112. struct gp_header *gph = (struct gp_header *)(ptr + OMAP_CH_HDR_SIZE);
  113. toc->section_offset = toc_offset(ptr, chs);
  114. toc->section_size = sizeof(struct ch_settings);
  115. strcpy((char *)toc->section_name, "CHSETTINGS");
  116. chs->section_key = KEY_CHSETTINGS;
  117. chs->valid = 0;
  118. chs->version = 1;
  119. chs->reserved = 0;
  120. chs->flags = 0;
  121. toc++;
  122. memset(toc, 0xff, sizeof(*toc));
  123. gph_set_header(gph, sbuf->st_size - OMAP_CH_HDR_SIZE,
  124. params->addr, 0);
  125. if (strncmp(params->imagename, "byteswap", 8) == 0) {
  126. do_swap32 = 1;
  127. int swapped = 0;
  128. uint32_t *data = (uint32_t *)ptr;
  129. const off_t size_in_words =
  130. DIV_ROUND_UP(sbuf->st_size, sizeof(uint32_t));
  131. while (swapped < size_in_words) {
  132. *data = cpu_to_be32(*data);
  133. swapped++;
  134. data++;
  135. }
  136. }
  137. }
  138. /*
  139. * omapimage parameters
  140. */
  141. U_BOOT_IMAGE_TYPE(
  142. omapimage,
  143. "TI OMAP CH/GP Boot Image support",
  144. OMAP_FILE_HDR_SIZE,
  145. (void *)&omapimage_header,
  146. gpimage_check_params,
  147. omapimage_verify_header,
  148. omapimage_print_header,
  149. omapimage_set_header,
  150. NULL,
  151. omapimage_check_image_types,
  152. NULL,
  153. NULL
  154. );