imagetool.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * (C) Copyright 2013
  3. *
  4. * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #ifndef _IMAGETOOL_H_
  9. #define _IMAGETOOL_H_
  10. #include "os_support.h"
  11. #include <errno.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/stat.h>
  17. #include <sys/types.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include <u-boot/sha1.h>
  21. #include "fdt_host.h"
  22. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  23. #define IH_ARCH_DEFAULT IH_ARCH_INVALID
  24. /*
  25. * This structure defines all such variables those are initialized by
  26. * mkimage and dumpimage main core and need to be referred by image
  27. * type specific functions
  28. */
  29. struct image_tool_params {
  30. int dflag;
  31. int eflag;
  32. int fflag;
  33. int iflag;
  34. int lflag;
  35. int pflag;
  36. int vflag;
  37. int xflag;
  38. int skipcpy;
  39. int os;
  40. int arch;
  41. int type;
  42. int comp;
  43. char *dtc;
  44. unsigned int addr;
  45. unsigned int ep;
  46. char *imagename;
  47. char *imagename2;
  48. char *datafile;
  49. char *imagefile;
  50. char *cmdname;
  51. const char *outfile; /* Output filename */
  52. const char *keydir; /* Directory holding private keys */
  53. const char *keydest; /* Destination .dtb for public key */
  54. const char *comment; /* Comment to add to signature node */
  55. int require_keys; /* 1 to mark signing keys as 'required' */
  56. };
  57. /*
  58. * image type specific variables and callback functions
  59. */
  60. struct image_type_params {
  61. /* name is an identification tag string for added support */
  62. char *name;
  63. /*
  64. * header size is local to the specific image type to be supported,
  65. * mkimage core treats this as number of bytes
  66. */
  67. uint32_t header_size;
  68. /* Image type header pointer */
  69. void *hdr;
  70. /*
  71. * There are several arguments that are passed on the command line
  72. * and are registered as flags in image_tool_params structure.
  73. * This callback function can be used to check the passed arguments
  74. * are in-lined with the image type to be supported
  75. *
  76. * Returns 1 if parameter check is successful
  77. */
  78. int (*check_params) (struct image_tool_params *);
  79. /*
  80. * This function is used by list command (i.e. mkimage -l <filename>)
  81. * image type verification code must be put here
  82. *
  83. * Returns 0 if image header verification is successful
  84. * otherwise, returns respective negative error codes
  85. */
  86. int (*verify_header) (unsigned char *, int, struct image_tool_params *);
  87. /* Prints image information abstracting from image header */
  88. void (*print_header) (const void *);
  89. /*
  90. * The header or image contents need to be set as per image type to
  91. * be generated using this callback function.
  92. * further output file post processing (for ex. checksum calculation,
  93. * padding bytes etc..) can also be done in this callback function.
  94. */
  95. void (*set_header) (void *, struct stat *, int,
  96. struct image_tool_params *);
  97. /*
  98. * This function is used by the command to retrieve a component
  99. * (sub-image) from the image (i.e. dumpimage -i <image> -p <position>
  100. * <sub-image-name>).
  101. * Thus the code to extract a file from an image must be put here.
  102. *
  103. * Returns 0 if the file was successfully retrieved from the image,
  104. * or a negative value on error.
  105. */
  106. int (*extract_subimage)(void *, struct image_tool_params *);
  107. /*
  108. * Some image generation support for ex (default image type) supports
  109. * more than one type_ids, this callback function is used to check
  110. * whether input (-T <image_type>) is supported by registered image
  111. * generation/list low level code
  112. */
  113. int (*check_image_type) (uint8_t);
  114. /* This callback function will be executed if fflag is defined */
  115. int (*fflag_handle) (struct image_tool_params *);
  116. /*
  117. * This callback function will be executed for variable size record
  118. * It is expected to build this header in memory and return its length
  119. * and a pointer to it by using image_type_params.header_size and
  120. * image_type_params.hdr. The return value shall indicate if an
  121. * additional padding should be used when copying the data image
  122. * by returning the padding length.
  123. */
  124. int (*vrec_header) (struct image_tool_params *,
  125. struct image_type_params *);
  126. };
  127. /**
  128. * imagetool_get_type() - find the image type params for a given image type
  129. *
  130. * It scans all registers image type supports
  131. * checks the input type for each supported image type
  132. *
  133. * if successful,
  134. * returns respective image_type_params pointer if success
  135. * if input type_id is not supported by any of image_type_support
  136. * returns NULL
  137. */
  138. struct image_type_params *imagetool_get_type(int type);
  139. /*
  140. * imagetool_verify_print_header() - verifies the image header
  141. *
  142. * Scan registered image types and verify the image_header for each
  143. * supported image type. If verification is successful, this prints
  144. * the respective header.
  145. *
  146. * @return 0 on success, negative if input image format does not match with
  147. * any of supported image types
  148. */
  149. int imagetool_verify_print_header(
  150. void *ptr,
  151. struct stat *sbuf,
  152. struct image_type_params *tparams,
  153. struct image_tool_params *params);
  154. /**
  155. * imagetool_save_subimage - store data into a file
  156. * @file_name: name of the destination file
  157. * @file_data: data to be written
  158. * @file_len: the amount of data to store
  159. *
  160. * imagetool_save_subimage() store file_len bytes of data pointed by file_data
  161. * into the file name by file_name.
  162. *
  163. * returns:
  164. * zero in case of success or a negative value if fail.
  165. */
  166. int imagetool_save_subimage(
  167. const char *file_name,
  168. ulong file_data,
  169. ulong file_len);
  170. /*
  171. * There is a c file associated with supported image type low level code
  172. * for ex. default_image.c, fit_image.c
  173. */
  174. void pbl_load_uboot(int fd, struct image_tool_params *mparams);
  175. #define ___cat(a, b) a ## b
  176. #define __cat(a, b) ___cat(a, b)
  177. /* we need some special handling for this host tool running eventually on
  178. * Darwin. The Mach-O section handling is a bit different than ELF section
  179. * handling. The differnces in detail are:
  180. * a) we have segments which have sections
  181. * b) we need a API call to get the respective section symbols */
  182. #if defined(__MACH__)
  183. #include <mach-o/getsect.h>
  184. #define INIT_SECTION(name) do { \
  185. unsigned long name ## _len; \
  186. char *__cat(pstart_, name) = getsectdata("__TEXT", \
  187. #name, &__cat(name, _len)); \
  188. char *__cat(pstop_, name) = __cat(pstart_, name) + \
  189. __cat(name, _len); \
  190. __cat(__start_, name) = (void *)__cat(pstart_, name); \
  191. __cat(__stop_, name) = (void *)__cat(pstop_, name); \
  192. } while (0)
  193. #define SECTION(name) __attribute__((section("__TEXT, " #name)))
  194. struct image_type_params **__start_image_type, **__stop_image_type;
  195. #else
  196. #define INIT_SECTION(name) /* no-op for ELF */
  197. #define SECTION(name) __attribute__((section(#name)))
  198. /* We construct a table of pointers in an ELF section (pointers generally
  199. * go unpadded by gcc). ld creates boundary syms for us. */
  200. extern struct image_type_params *__start_image_type[], *__stop_image_type[];
  201. #endif /* __MACH__ */
  202. #if !defined(__used)
  203. # if __GNUC__ == 3 && __GNUC_MINOR__ < 3
  204. # define __used __attribute__((__unused__))
  205. # else
  206. # define __used __attribute__((__used__))
  207. # endif
  208. #endif
  209. #define U_BOOT_IMAGE_TYPE( \
  210. _id, \
  211. _name, \
  212. _header_size, \
  213. _header, \
  214. _check_params, \
  215. _verify_header, \
  216. _print_header, \
  217. _set_header, \
  218. _extract_subimage, \
  219. _check_image_type, \
  220. _fflag_handle, \
  221. _vrec_header \
  222. ) \
  223. static struct image_type_params __cat(image_type_, _id) = \
  224. { \
  225. .name = _name, \
  226. .header_size = _header_size, \
  227. .hdr = _header, \
  228. .check_params = _check_params, \
  229. .verify_header = _verify_header, \
  230. .print_header = _print_header, \
  231. .set_header = _set_header, \
  232. .extract_subimage = _extract_subimage, \
  233. .check_image_type = _check_image_type, \
  234. .fflag_handle = _fflag_handle, \
  235. .vrec_header = _vrec_header \
  236. }; \
  237. static struct image_type_params *SECTION(image_type) __used \
  238. __cat(image_type_ptr_, _id) = &__cat(image_type_, _id)
  239. #endif /* _IMAGETOOL_H_ */