omapimage.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 uint8_t omapimage_header[OMAP_FILE_HDR_SIZE];
  42. static int omapimage_check_image_types(uint8_t type)
  43. {
  44. if (type == IH_TYPE_OMAPIMAGE)
  45. return EXIT_SUCCESS;
  46. else {
  47. return EXIT_FAILURE;
  48. }
  49. }
  50. /*
  51. * Only the simplest image type is currently supported:
  52. * TOC pointing to CHSETTINGS
  53. * TOC terminator
  54. * CHSETTINGS
  55. *
  56. * padding to OMAP_CH_HDR_SIZE bytes
  57. *
  58. * gp header
  59. * size
  60. * load_addr
  61. */
  62. static int valid_gph_size(uint32_t size)
  63. {
  64. return size;
  65. }
  66. static int valid_gph_load_addr(uint32_t load_addr)
  67. {
  68. return load_addr;
  69. }
  70. static int omapimage_verify_header(unsigned char *ptr, int image_size,
  71. struct mkimage_params *params)
  72. {
  73. struct ch_toc *toc = (struct ch_toc *)ptr;
  74. struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
  75. uint32_t offset, size;
  76. while (toc->section_offset != 0xffffffff
  77. && toc->section_size != 0xffffffff) {
  78. offset = toc->section_offset;
  79. size = toc->section_size;
  80. if (!offset || !size)
  81. return -1;
  82. if (offset >= OMAP_CH_HDR_SIZE ||
  83. offset+size >= OMAP_CH_HDR_SIZE)
  84. return -1;
  85. toc++;
  86. }
  87. if (!valid_gph_size(gph->size))
  88. return -1;
  89. if (!valid_gph_load_addr(gph->load_addr))
  90. return -1;
  91. return 0;
  92. }
  93. static void omapimage_print_section(struct ch_settings *chs)
  94. {
  95. const char *section_name;
  96. if (chs->section_key)
  97. section_name = "CHSETTINGS";
  98. else
  99. section_name = "UNKNOWNKEY";
  100. printf("%s (%x) "
  101. "valid:%x "
  102. "version:%x "
  103. "reserved:%x "
  104. "flags:%x\n",
  105. section_name,
  106. chs->section_key,
  107. chs->valid,
  108. chs->version,
  109. chs->reserved,
  110. chs->flags);
  111. }
  112. static void omapimage_print_header(const void *ptr)
  113. {
  114. const struct ch_toc *toc = (struct ch_toc *)ptr;
  115. const struct gp_header *gph =
  116. (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
  117. uint32_t offset, size;
  118. while (toc->section_offset != 0xffffffff
  119. && toc->section_size != 0xffffffff) {
  120. offset = toc->section_offset;
  121. size = toc->section_size;
  122. if (offset >= OMAP_CH_HDR_SIZE ||
  123. offset+size >= OMAP_CH_HDR_SIZE)
  124. exit(EXIT_FAILURE);
  125. printf("Section %s offset %x length %x\n",
  126. toc->section_name,
  127. toc->section_offset,
  128. toc->section_size);
  129. omapimage_print_section((struct ch_settings *)(ptr+offset));
  130. toc++;
  131. }
  132. if (!valid_gph_size(gph->size)) {
  133. fprintf(stderr,
  134. "Error: invalid image size %x\n",
  135. gph->size);
  136. exit(EXIT_FAILURE);
  137. }
  138. if (!valid_gph_load_addr(gph->load_addr)) {
  139. fprintf(stderr,
  140. "Error: invalid image load address %x\n",
  141. gph->size);
  142. exit(EXIT_FAILURE);
  143. }
  144. printf("GP Header: Size %x LoadAddr %x\n",
  145. gph->size, gph->load_addr);
  146. }
  147. static int toc_offset(void *hdr, void *member)
  148. {
  149. return member - hdr;
  150. }
  151. static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  152. struct mkimage_params *params)
  153. {
  154. struct ch_toc *toc = (struct ch_toc *)ptr;
  155. struct ch_settings *chs = (struct ch_settings *)
  156. (ptr + 2 * sizeof(*toc));
  157. struct gp_header *gph = (struct gp_header *)(ptr + OMAP_CH_HDR_SIZE);
  158. toc->section_offset = toc_offset(ptr, chs);
  159. toc->section_size = sizeof(struct ch_settings);
  160. strcpy((char *)toc->section_name, "CHSETTINGS");
  161. chs->section_key = KEY_CHSETTINGS;
  162. chs->valid = 0;
  163. chs->version = 1;
  164. chs->reserved = 0;
  165. chs->flags = 0;
  166. toc++;
  167. memset(toc, 0xff, sizeof(*toc));
  168. gph->size = sbuf->st_size - OMAP_FILE_HDR_SIZE;
  169. gph->load_addr = params->addr;
  170. }
  171. int omapimage_check_params(struct mkimage_params *params)
  172. {
  173. return (params->dflag && (params->fflag || params->lflag)) ||
  174. (params->fflag && (params->dflag || params->lflag)) ||
  175. (params->lflag && (params->dflag || params->fflag));
  176. }
  177. /*
  178. * omapimage parameters
  179. */
  180. static struct image_type_params omapimage_params = {
  181. .name = "TI OMAP CH/GP Boot Image support",
  182. .header_size = OMAP_FILE_HDR_SIZE,
  183. .hdr = (void *)&omapimage_header,
  184. .check_image_type = omapimage_check_image_types,
  185. .verify_header = omapimage_verify_header,
  186. .print_header = omapimage_print_header,
  187. .set_header = omapimage_set_header,
  188. .check_params = omapimage_check_params,
  189. };
  190. void init_omap_image_type(void)
  191. {
  192. mkimage_register(&omapimage_params);
  193. }