imagetool.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2013
  4. *
  5. * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
  6. */
  7. #ifndef _IMAGETOOL_H_
  8. #define _IMAGETOOL_H_
  9. #include "os_support.h"
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <stdbool.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 <image.h>
  22. #include "fdt_host.h"
  23. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  24. #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
  25. #define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1)
  26. #define IH_ARCH_DEFAULT IH_ARCH_INVALID
  27. /* Information about a file that needs to be placed into the FIT */
  28. struct content_info {
  29. struct content_info *next;
  30. int type; /* File type (IH_TYPE_...) */
  31. const char *fname;
  32. };
  33. /* FIT auto generation modes */
  34. enum af_mode {
  35. AF_OFF = 0, /* Needs .its or existing FIT to be provided */
  36. AF_HASHED_IMG, /* Auto FIT with crc32 hashed images subnodes */
  37. AF_SIGNED_IMG, /* Auto FIT with signed images subnodes */
  38. AF_SIGNED_CONF, /* Auto FIT with sha1 images and signed configs */
  39. };
  40. /*
  41. * This structure defines all such variables those are initialized by
  42. * mkimage and dumpimage main core and need to be referred by image
  43. * type specific functions
  44. */
  45. struct image_tool_params {
  46. int dflag;
  47. int eflag;
  48. int fflag;
  49. int iflag;
  50. int lflag;
  51. int pflag;
  52. int vflag;
  53. int xflag;
  54. int Aflag;
  55. int skipcpy;
  56. int os;
  57. int arch;
  58. int type;
  59. int comp;
  60. char *dtc;
  61. unsigned int addr;
  62. unsigned int ep;
  63. char *imagename;
  64. char *imagename2;
  65. char *datafile;
  66. char *imagefile;
  67. char *cmdname;
  68. const char *outfile; /* Output filename */
  69. const char *keydir; /* Directory holding private keys */
  70. const char *keydest; /* Destination .dtb for public key */
  71. const char *keyfile; /* Filename of private or public key */
  72. const char *keyname; /* Key name "hint" */
  73. const char *comment; /* Comment to add to signature node */
  74. /* Algorithm name to use for hashing/signing or NULL to use the one
  75. * specified in the its */
  76. const char *algo_name;
  77. int require_keys; /* 1 to mark signing keys as 'required' */
  78. int file_size; /* Total size of output file */
  79. int orig_file_size; /* Original size for file before padding */
  80. enum af_mode auto_fit; /* Automatically create the FIT */
  81. int fit_image_type; /* Image type to put into the FIT */
  82. char *fit_ramdisk; /* Ramdisk file to include */
  83. struct content_info *content_head; /* List of files to include */
  84. struct content_info *content_tail;
  85. bool external_data; /* Store data outside the FIT */
  86. bool quiet; /* Don't output text in normal operation */
  87. unsigned int external_offset; /* Add padding to external data */
  88. int bl_len; /* Block length in byte for external data */
  89. const char *engine_id; /* Engine to use for signing */
  90. bool reset_timestamp; /* Reset the timestamp on an existing image */
  91. struct image_summary summary; /* results of signing process */
  92. };
  93. /*
  94. * image type specific variables and callback functions
  95. */
  96. struct image_type_params {
  97. /* name is an identification tag string for added support */
  98. char *name;
  99. /*
  100. * header size is local to the specific image type to be supported,
  101. * mkimage core treats this as number of bytes
  102. */
  103. uint32_t header_size;
  104. /* Image type header pointer */
  105. void *hdr;
  106. /*
  107. * There are several arguments that are passed on the command line
  108. * and are registered as flags in image_tool_params structure.
  109. * This callback function can be used to check the passed arguments
  110. * are in-lined with the image type to be supported
  111. *
  112. * Returns 1 if parameter check is successful
  113. */
  114. int (*check_params) (struct image_tool_params *);
  115. /*
  116. * This function is used by list command (i.e. mkimage -l <filename>)
  117. * image type verification code must be put here
  118. *
  119. * Returns 0 if image header verification is successful
  120. * otherwise, returns respective negative error codes
  121. */
  122. int (*verify_header) (unsigned char *, int, struct image_tool_params *);
  123. /* Prints image information abstracting from image header */
  124. void (*print_header) (const void *, struct image_tool_params *);
  125. /*
  126. * The header or image contents need to be set as per image type to
  127. * be generated using this callback function.
  128. * further output file post processing (for ex. checksum calculation,
  129. * padding bytes etc..) can also be done in this callback function.
  130. */
  131. void (*set_header) (void *, struct stat *, int,
  132. struct image_tool_params *);
  133. /*
  134. * This function is used by the command to retrieve a component
  135. * (sub-image) from the image (i.e. dumpimage -p <position>
  136. * -o <component-outfile> <image>). Thus the code to extract a file
  137. * from an image must be put here.
  138. *
  139. * Returns 0 if the file was successfully retrieved from the image,
  140. * or a negative value on error.
  141. */
  142. int (*extract_subimage)(void *, struct image_tool_params *);
  143. /*
  144. * Some image generation support for ex (default image type) supports
  145. * more than one type_ids, this callback function is used to check
  146. * whether input (-T <image_type>) is supported by registered image
  147. * generation/list low level code
  148. */
  149. int (*check_image_type) (uint8_t);
  150. /* This callback function will be executed if fflag is defined */
  151. int (*fflag_handle) (struct image_tool_params *);
  152. /*
  153. * This callback function will be executed for variable size record
  154. * It is expected to build this header in memory and return its length
  155. * and a pointer to it by using image_type_params.header_size and
  156. * image_type_params.hdr. The return value shall indicate if an
  157. * additional padding should be used when copying the data image
  158. * by returning the padding length.
  159. */
  160. int (*vrec_header) (struct image_tool_params *,
  161. struct image_type_params *);
  162. };
  163. /**
  164. * imagetool_get_type() - find the image type params for a given image type
  165. *
  166. * It scans all registers image type supports
  167. * checks the input type for each supported image type
  168. *
  169. * if successful,
  170. * returns respective image_type_params pointer if success
  171. * if input type_id is not supported by any of image_type_support
  172. * returns NULL
  173. */
  174. struct image_type_params *imagetool_get_type(int type);
  175. /*
  176. * imagetool_verify_print_header() - verifies the image header
  177. *
  178. * Verify the image_header for the image type given by tparams.
  179. * If tparams is NULL then scan registered image types and verify the
  180. * image_header for each supported image type.
  181. * If verification is successful, this prints the respective header.
  182. * @ptr: pointer the the image header
  183. * @sbuf: stat information about the file pointed to by ptr
  184. * @tparams: image type parameters or NULL
  185. * @params: mkimage parameters
  186. *
  187. * Return: 0 on success, negative if input image format does not match with
  188. * the given image type
  189. */
  190. int imagetool_verify_print_header(
  191. void *ptr,
  192. struct stat *sbuf,
  193. struct image_type_params *tparams,
  194. struct image_tool_params *params);
  195. /**
  196. * imagetool_save_subimage - store data into a file
  197. * @file_name: name of the destination file
  198. * @file_data: data to be written
  199. * @file_len: the amount of data to store
  200. *
  201. * imagetool_save_subimage() store file_len bytes of data pointed by file_data
  202. * into the file name by file_name.
  203. *
  204. * returns:
  205. * zero in case of success or a negative value if fail.
  206. */
  207. int imagetool_save_subimage(
  208. const char *file_name,
  209. ulong file_data,
  210. ulong file_len);
  211. /**
  212. * imagetool_get_filesize() - Utility function to obtain the size of a file
  213. *
  214. * This function prints a message if an error occurs, showing the error that
  215. * was obtained.
  216. *
  217. * @params: mkimage parameters
  218. * @fname: filename to check
  219. * Return: size of file, or -ve value on error
  220. */
  221. int imagetool_get_filesize(struct image_tool_params *params, const char *fname);
  222. /**
  223. * imagetool_get_source_date() - Get timestamp for build output.
  224. *
  225. * Gets a timestamp for embedding it in a build output. If set
  226. * SOURCE_DATE_EPOCH is used. Else the given fallback value is returned. Prints
  227. * an error message if SOURCE_DATE_EPOCH contains an invalid value and returns
  228. * 0.
  229. *
  230. * @cmdname: command name
  231. * @fallback: timestamp to use if SOURCE_DATE_EPOCH isn't set
  232. * Return: timestamp based on SOURCE_DATE_EPOCH
  233. */
  234. time_t imagetool_get_source_date(
  235. const char *cmdname,
  236. time_t fallback);
  237. /*
  238. * There is a c file associated with supported image type low level code
  239. * for ex. default_image.c, fit_image.c
  240. */
  241. void pbl_load_uboot(int fd, struct image_tool_params *mparams);
  242. int zynqmpbif_copy_image(int fd, struct image_tool_params *mparams);
  243. int imx8image_copy_image(int fd, struct image_tool_params *mparams);
  244. int imx8mimage_copy_image(int fd, struct image_tool_params *mparams);
  245. int rockchip_copy_image(int fd, struct image_tool_params *mparams);
  246. #define ___cat(a, b) a ## b
  247. #define __cat(a, b) ___cat(a, b)
  248. /* we need some special handling for this host tool running eventually on
  249. * Darwin. The Mach-O section handling is a bit different than ELF section
  250. * handling. The differnces in detail are:
  251. * a) we have segments which have sections
  252. * b) we need a API call to get the respective section symbols */
  253. #if defined(__MACH__)
  254. #include <mach-o/getsect.h>
  255. #include <mach-o/dyld.h>
  256. #define INIT_SECTION(name) do { \
  257. unsigned long name ## _len; \
  258. char *__cat(pstart_, name) = getsectdata("__DATA", \
  259. #name, &__cat(name, _len)); \
  260. __cat(pstart_, name) += _dyld_get_image_vmaddr_slide(0);\
  261. char *__cat(pstop_, name) = __cat(pstart_, name) + \
  262. __cat(name, _len); \
  263. __cat(__start_, name) = (void *)__cat(pstart_, name); \
  264. __cat(__stop_, name) = (void *)__cat(pstop_, name); \
  265. } while (0)
  266. #define SECTION(name) __attribute__((section("__DATA, " #name)))
  267. struct image_type_params **__start_image_type, **__stop_image_type;
  268. #else
  269. #define INIT_SECTION(name) /* no-op for ELF */
  270. #define SECTION(name) __attribute__((section(#name)))
  271. /* We construct a table of pointers in an ELF section (pointers generally
  272. * go unpadded by gcc). ld creates boundary syms for us. */
  273. extern struct image_type_params *__start_image_type[], *__stop_image_type[];
  274. #endif /* __MACH__ */
  275. #if !defined(__used)
  276. # if __GNUC__ == 3 && __GNUC_MINOR__ < 3
  277. # define __used __attribute__((__unused__))
  278. # else
  279. # define __used __attribute__((__used__))
  280. # endif
  281. #endif
  282. #define U_BOOT_IMAGE_TYPE( \
  283. _id, \
  284. _name, \
  285. _header_size, \
  286. _header, \
  287. _check_params, \
  288. _verify_header, \
  289. _print_header, \
  290. _set_header, \
  291. _extract_subimage, \
  292. _check_image_type, \
  293. _fflag_handle, \
  294. _vrec_header \
  295. ) \
  296. static struct image_type_params __cat(image_type_, _id) = \
  297. { \
  298. .name = _name, \
  299. .header_size = _header_size, \
  300. .hdr = _header, \
  301. .check_params = _check_params, \
  302. .verify_header = _verify_header, \
  303. .print_header = _print_header, \
  304. .set_header = _set_header, \
  305. .extract_subimage = _extract_subimage, \
  306. .check_image_type = _check_image_type, \
  307. .fflag_handle = _fflag_handle, \
  308. .vrec_header = _vrec_header \
  309. }; \
  310. static struct image_type_params *SECTION(image_type) __used \
  311. __cat(image_type_ptr_, _id) = &__cat(image_type_, _id)
  312. #endif /* _IMAGETOOL_H_ */