imagetool.h 7.7 KB

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