omapimage.c 4.1 KB

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