omapimage.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. /* Required to obtain the getline prototype from stdio.h */
  33. #define _GNU_SOURCE
  34. #include "mkimage.h"
  35. #include <image.h>
  36. #include "omapimage.h"
  37. /* Header size is CH header rounded up to 512 bytes plus GP header */
  38. #define OMAP_CH_HDR_SIZE 512
  39. #define OMAP_GP_HDR_SIZE (sizeof(struct gp_header))
  40. #define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE+OMAP_GP_HDR_SIZE)
  41. static int do_swap32 = 0;
  42. static uint32_t omapimage_swap32(uint32_t data)
  43. {
  44. uint32_t result = 0;
  45. result = (data & 0xFF000000) >> 24;
  46. result |= (data & 0x00FF0000) >> 8;
  47. result |= (data & 0x0000FF00) << 8;
  48. result |= (data & 0x000000FF) << 24;
  49. return result;
  50. }
  51. static uint8_t omapimage_header[OMAP_FILE_HDR_SIZE];
  52. static int omapimage_check_image_types(uint8_t type)
  53. {
  54. if (type == IH_TYPE_OMAPIMAGE)
  55. return EXIT_SUCCESS;
  56. else {
  57. return EXIT_FAILURE;
  58. }
  59. }
  60. /*
  61. * Only the simplest image type is currently supported:
  62. * TOC pointing to CHSETTINGS
  63. * TOC terminator
  64. * CHSETTINGS
  65. *
  66. * padding to OMAP_CH_HDR_SIZE bytes
  67. *
  68. * gp header
  69. * size
  70. * load_addr
  71. */
  72. static int valid_gph_size(uint32_t size)
  73. {
  74. return size;
  75. }
  76. static int valid_gph_load_addr(uint32_t load_addr)
  77. {
  78. return load_addr;
  79. }
  80. static int omapimage_verify_header(unsigned char *ptr, int image_size,
  81. struct mkimage_params *params)
  82. {
  83. struct ch_toc *toc = (struct ch_toc *)ptr;
  84. struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
  85. uint32_t offset, size, gph_size, gph_load_addr;
  86. while (toc->section_offset != 0xffffffff
  87. && toc->section_size != 0xffffffff) {
  88. if (do_swap32) {
  89. offset = omapimage_swap32(toc->section_offset);
  90. size = omapimage_swap32(toc->section_size);
  91. } else {
  92. offset = toc->section_offset;
  93. size = toc->section_size;
  94. }
  95. if (!offset || !size)
  96. return -1;
  97. if (offset >= OMAP_CH_HDR_SIZE ||
  98. offset+size >= OMAP_CH_HDR_SIZE)
  99. return -1;
  100. toc++;
  101. }
  102. if (do_swap32) {
  103. gph_size = omapimage_swap32(gph->size);
  104. gph_load_addr = omapimage_swap32(gph->load_addr);
  105. } else {
  106. gph_size = gph->size;
  107. gph_load_addr = gph->load_addr;
  108. }
  109. if (!valid_gph_size(gph_size))
  110. return -1;
  111. if (!valid_gph_load_addr(gph_load_addr))
  112. return -1;
  113. return 0;
  114. }
  115. static void omapimage_print_section(struct ch_settings *chs)
  116. {
  117. const char *section_name;
  118. if (chs->section_key)
  119. section_name = "CHSETTINGS";
  120. else
  121. section_name = "UNKNOWNKEY";
  122. printf("%s (%x) "
  123. "valid:%x "
  124. "version:%x "
  125. "reserved:%x "
  126. "flags:%x\n",
  127. section_name,
  128. chs->section_key,
  129. chs->valid,
  130. chs->version,
  131. chs->reserved,
  132. chs->flags);
  133. }
  134. static void omapimage_print_header(const void *ptr)
  135. {
  136. const struct ch_toc *toc = (struct ch_toc *)ptr;
  137. const struct gp_header *gph =
  138. (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
  139. uint32_t offset, size, gph_size, gph_load_addr;
  140. while (toc->section_offset != 0xffffffff
  141. && toc->section_size != 0xffffffff) {
  142. if (do_swap32) {
  143. offset = omapimage_swap32(toc->section_offset);
  144. size = omapimage_swap32(toc->section_size);
  145. } else {
  146. offset = toc->section_offset;
  147. size = toc->section_size;
  148. }
  149. if (offset >= OMAP_CH_HDR_SIZE ||
  150. offset+size >= OMAP_CH_HDR_SIZE)
  151. exit(EXIT_FAILURE);
  152. printf("Section %s offset %x length %x\n",
  153. toc->section_name,
  154. toc->section_offset,
  155. toc->section_size);
  156. omapimage_print_section((struct ch_settings *)(ptr+offset));
  157. toc++;
  158. }
  159. if (do_swap32) {
  160. gph_size = omapimage_swap32(gph->size);
  161. gph_load_addr = omapimage_swap32(gph->load_addr);
  162. } else {
  163. gph_size = gph->size;
  164. gph_load_addr = gph->load_addr;
  165. }
  166. if (!valid_gph_size(gph_size)) {
  167. fprintf(stderr, "Error: invalid image size %x\n", gph_size);
  168. exit(EXIT_FAILURE);
  169. }
  170. if (!valid_gph_load_addr(gph_load_addr)) {
  171. fprintf(stderr, "Error: invalid image load address %x\n",
  172. gph_load_addr);
  173. exit(EXIT_FAILURE);
  174. }
  175. printf("GP Header: Size %x LoadAddr %x\n", gph_size, gph_load_addr);
  176. }
  177. static int toc_offset(void *hdr, void *member)
  178. {
  179. return member - hdr;
  180. }
  181. static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  182. struct mkimage_params *params)
  183. {
  184. struct ch_toc *toc = (struct ch_toc *)ptr;
  185. struct ch_settings *chs = (struct ch_settings *)
  186. (ptr + 2 * sizeof(*toc));
  187. struct gp_header *gph = (struct gp_header *)(ptr + OMAP_CH_HDR_SIZE);
  188. toc->section_offset = toc_offset(ptr, chs);
  189. toc->section_size = sizeof(struct ch_settings);
  190. strcpy((char *)toc->section_name, "CHSETTINGS");
  191. chs->section_key = KEY_CHSETTINGS;
  192. chs->valid = 0;
  193. chs->version = 1;
  194. chs->reserved = 0;
  195. chs->flags = 0;
  196. toc++;
  197. memset(toc, 0xff, sizeof(*toc));
  198. gph->size = sbuf->st_size - OMAP_FILE_HDR_SIZE;
  199. gph->load_addr = params->addr;
  200. if (strncmp(params->imagename, "byteswap", 8) == 0) {
  201. do_swap32 = 1;
  202. int swapped = 0;
  203. uint32_t *data = (uint32_t *)ptr;
  204. while (swapped <= (sbuf->st_size / sizeof(uint32_t))) {
  205. *data = omapimage_swap32(*data);
  206. swapped++;
  207. data++;
  208. }
  209. }
  210. }
  211. int omapimage_check_params(struct mkimage_params *params)
  212. {
  213. return (params->dflag && (params->fflag || params->lflag)) ||
  214. (params->fflag && (params->dflag || params->lflag)) ||
  215. (params->lflag && (params->dflag || params->fflag));
  216. }
  217. /*
  218. * omapimage parameters
  219. */
  220. static struct image_type_params omapimage_params = {
  221. .name = "TI OMAP CH/GP Boot Image support",
  222. .header_size = OMAP_FILE_HDR_SIZE,
  223. .hdr = (void *)&omapimage_header,
  224. .check_image_type = omapimage_check_image_types,
  225. .verify_header = omapimage_verify_header,
  226. .print_header = omapimage_print_header,
  227. .set_header = omapimage_set_header,
  228. .check_params = omapimage_check_params,
  229. };
  230. void init_omap_image_type(void)
  231. {
  232. mkimage_register(&omapimage_params);
  233. }