image.h 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2008 Semihalf
  4. *
  5. * (C) Copyright 2000-2005
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. ********************************************************************
  8. * NOTE: This header file defines an interface to U-Boot. Including
  9. * this (unmodified) header file in another file is considered normal
  10. * use of U-Boot, and does *not* fall under the heading of "derived
  11. * work".
  12. ********************************************************************
  13. */
  14. #ifndef __IMAGE_H__
  15. #define __IMAGE_H__
  16. #include "compiler.h"
  17. #include <asm/byteorder.h>
  18. #include <stdbool.h>
  19. /* Define this to avoid #ifdefs later on */
  20. struct lmb;
  21. struct fdt_region;
  22. #ifdef USE_HOSTCC
  23. #include <sys/types.h>
  24. #include <linux/kconfig.h>
  25. #define IMAGE_INDENT_STRING ""
  26. #else
  27. #include <lmb.h>
  28. #include <asm/u-boot.h>
  29. #include <command.h>
  30. #include <linker_lists.h>
  31. #define IMAGE_INDENT_STRING " "
  32. #endif /* USE_HOSTCC */
  33. #include <hash.h>
  34. #include <linux/libfdt.h>
  35. #include <fdt_support.h>
  36. #include <u-boot/hash-checksum.h>
  37. extern ulong image_load_addr; /* Default Load Address */
  38. extern ulong image_save_addr; /* Default Save Address */
  39. extern ulong image_save_size; /* Default Save Size */
  40. extern ulong image_load_offset; /* Default Load Address Offset */
  41. /* An invalid size, meaning that the image size is not known */
  42. #define IMAGE_SIZE_INVAL (-1UL)
  43. enum ih_category {
  44. IH_ARCH,
  45. IH_COMP,
  46. IH_OS,
  47. IH_TYPE,
  48. IH_PHASE,
  49. IH_COUNT,
  50. };
  51. /*
  52. * Operating System Codes
  53. *
  54. * The following are exposed to uImage header.
  55. * New IDs *MUST* be appended at the end of the list and *NEVER*
  56. * inserted for backward compatibility.
  57. */
  58. enum {
  59. IH_OS_INVALID = 0, /* Invalid OS */
  60. IH_OS_OPENBSD, /* OpenBSD */
  61. IH_OS_NETBSD, /* NetBSD */
  62. IH_OS_FREEBSD, /* FreeBSD */
  63. IH_OS_4_4BSD, /* 4.4BSD */
  64. IH_OS_LINUX, /* Linux */
  65. IH_OS_SVR4, /* SVR4 */
  66. IH_OS_ESIX, /* Esix */
  67. IH_OS_SOLARIS, /* Solaris */
  68. IH_OS_IRIX, /* Irix */
  69. IH_OS_SCO, /* SCO */
  70. IH_OS_DELL, /* Dell */
  71. IH_OS_NCR, /* NCR */
  72. IH_OS_LYNXOS, /* LynxOS */
  73. IH_OS_VXWORKS, /* VxWorks */
  74. IH_OS_PSOS, /* pSOS */
  75. IH_OS_QNX, /* QNX */
  76. IH_OS_U_BOOT, /* Firmware */
  77. IH_OS_RTEMS, /* RTEMS */
  78. IH_OS_ARTOS, /* ARTOS */
  79. IH_OS_UNITY, /* Unity OS */
  80. IH_OS_INTEGRITY, /* INTEGRITY */
  81. IH_OS_OSE, /* OSE */
  82. IH_OS_PLAN9, /* Plan 9 */
  83. IH_OS_OPENRTOS, /* OpenRTOS */
  84. IH_OS_ARM_TRUSTED_FIRMWARE, /* ARM Trusted Firmware */
  85. IH_OS_TEE, /* Trusted Execution Environment */
  86. IH_OS_OPENSBI, /* RISC-V OpenSBI */
  87. IH_OS_EFI, /* EFI Firmware (e.g. GRUB2) */
  88. IH_OS_COUNT,
  89. };
  90. /*
  91. * CPU Architecture Codes (supported by Linux)
  92. *
  93. * The following are exposed to uImage header.
  94. * New IDs *MUST* be appended at the end of the list and *NEVER*
  95. * inserted for backward compatibility.
  96. */
  97. enum {
  98. IH_ARCH_INVALID = 0, /* Invalid CPU */
  99. IH_ARCH_ALPHA, /* Alpha */
  100. IH_ARCH_ARM, /* ARM */
  101. IH_ARCH_I386, /* Intel x86 */
  102. IH_ARCH_IA64, /* IA64 */
  103. IH_ARCH_MIPS, /* MIPS */
  104. IH_ARCH_MIPS64, /* MIPS 64 Bit */
  105. IH_ARCH_PPC, /* PowerPC */
  106. IH_ARCH_S390, /* IBM S390 */
  107. IH_ARCH_SH, /* SuperH */
  108. IH_ARCH_SPARC, /* Sparc */
  109. IH_ARCH_SPARC64, /* Sparc 64 Bit */
  110. IH_ARCH_M68K, /* M68K */
  111. IH_ARCH_NIOS, /* Nios-32 */
  112. IH_ARCH_MICROBLAZE, /* MicroBlaze */
  113. IH_ARCH_NIOS2, /* Nios-II */
  114. IH_ARCH_BLACKFIN, /* Blackfin */
  115. IH_ARCH_AVR32, /* AVR32 */
  116. IH_ARCH_ST200, /* STMicroelectronics ST200 */
  117. IH_ARCH_SANDBOX, /* Sandbox architecture (test only) */
  118. IH_ARCH_NDS32, /* ANDES Technology - NDS32 */
  119. IH_ARCH_OPENRISC, /* OpenRISC 1000 */
  120. IH_ARCH_ARM64, /* ARM64 */
  121. IH_ARCH_ARC, /* Synopsys DesignWare ARC */
  122. IH_ARCH_X86_64, /* AMD x86_64, Intel and Via */
  123. IH_ARCH_XTENSA, /* Xtensa */
  124. IH_ARCH_RISCV, /* RISC-V */
  125. IH_ARCH_COUNT,
  126. };
  127. /*
  128. * Image Types
  129. *
  130. * "Standalone Programs" are directly runnable in the environment
  131. * provided by U-Boot; it is expected that (if they behave
  132. * well) you can continue to work in U-Boot after return from
  133. * the Standalone Program.
  134. * "OS Kernel Images" are usually images of some Embedded OS which
  135. * will take over control completely. Usually these programs
  136. * will install their own set of exception handlers, device
  137. * drivers, set up the MMU, etc. - this means, that you cannot
  138. * expect to re-enter U-Boot except by resetting the CPU.
  139. * "RAMDisk Images" are more or less just data blocks, and their
  140. * parameters (address, size) are passed to an OS kernel that is
  141. * being started.
  142. * "Multi-File Images" contain several images, typically an OS
  143. * (Linux) kernel image and one or more data images like
  144. * RAMDisks. This construct is useful for instance when you want
  145. * to boot over the network using BOOTP etc., where the boot
  146. * server provides just a single image file, but you want to get
  147. * for instance an OS kernel and a RAMDisk image.
  148. *
  149. * "Multi-File Images" start with a list of image sizes, each
  150. * image size (in bytes) specified by an "uint32_t" in network
  151. * byte order. This list is terminated by an "(uint32_t)0".
  152. * Immediately after the terminating 0 follow the images, one by
  153. * one, all aligned on "uint32_t" boundaries (size rounded up to
  154. * a multiple of 4 bytes - except for the last file).
  155. *
  156. * "Firmware Images" are binary images containing firmware (like
  157. * U-Boot or FPGA images) which usually will be programmed to
  158. * flash memory.
  159. *
  160. * "Script files" are command sequences that will be executed by
  161. * U-Boot's command interpreter; this feature is especially
  162. * useful when you configure U-Boot to use a real shell (hush)
  163. * as command interpreter (=> Shell Scripts).
  164. *
  165. * The following are exposed to uImage header.
  166. * New IDs *MUST* be appended at the end of the list and *NEVER*
  167. * inserted for backward compatibility.
  168. */
  169. enum image_type_t {
  170. IH_TYPE_INVALID = 0, /* Invalid Image */
  171. IH_TYPE_STANDALONE, /* Standalone Program */
  172. IH_TYPE_KERNEL, /* OS Kernel Image */
  173. IH_TYPE_RAMDISK, /* RAMDisk Image */
  174. IH_TYPE_MULTI, /* Multi-File Image */
  175. IH_TYPE_FIRMWARE, /* Firmware Image */
  176. IH_TYPE_SCRIPT, /* Script file */
  177. IH_TYPE_FILESYSTEM, /* Filesystem Image (any type) */
  178. IH_TYPE_FLATDT, /* Binary Flat Device Tree Blob */
  179. IH_TYPE_KWBIMAGE, /* Kirkwood Boot Image */
  180. IH_TYPE_IMXIMAGE, /* Freescale IMXBoot Image */
  181. IH_TYPE_UBLIMAGE, /* Davinci UBL Image */
  182. IH_TYPE_OMAPIMAGE, /* TI OMAP Config Header Image */
  183. IH_TYPE_AISIMAGE, /* TI Davinci AIS Image */
  184. /* OS Kernel Image, can run from any load address */
  185. IH_TYPE_KERNEL_NOLOAD,
  186. IH_TYPE_PBLIMAGE, /* Freescale PBL Boot Image */
  187. IH_TYPE_MXSIMAGE, /* Freescale MXSBoot Image */
  188. IH_TYPE_GPIMAGE, /* TI Keystone GPHeader Image */
  189. IH_TYPE_ATMELIMAGE, /* ATMEL ROM bootable Image */
  190. IH_TYPE_SOCFPGAIMAGE, /* Altera SOCFPGA CV/AV Preloader */
  191. IH_TYPE_X86_SETUP, /* x86 setup.bin Image */
  192. IH_TYPE_LPC32XXIMAGE, /* x86 setup.bin Image */
  193. IH_TYPE_LOADABLE, /* A list of typeless images */
  194. IH_TYPE_RKIMAGE, /* Rockchip Boot Image */
  195. IH_TYPE_RKSD, /* Rockchip SD card */
  196. IH_TYPE_RKSPI, /* Rockchip SPI image */
  197. IH_TYPE_ZYNQIMAGE, /* Xilinx Zynq Boot Image */
  198. IH_TYPE_ZYNQMPIMAGE, /* Xilinx ZynqMP Boot Image */
  199. IH_TYPE_ZYNQMPBIF, /* Xilinx ZynqMP Boot Image (bif) */
  200. IH_TYPE_FPGA, /* FPGA Image */
  201. IH_TYPE_VYBRIDIMAGE, /* VYBRID .vyb Image */
  202. IH_TYPE_TEE, /* Trusted Execution Environment OS Image */
  203. IH_TYPE_FIRMWARE_IVT, /* Firmware Image with HABv4 IVT */
  204. IH_TYPE_PMMC, /* TI Power Management Micro-Controller Firmware */
  205. IH_TYPE_STM32IMAGE, /* STMicroelectronics STM32 Image */
  206. IH_TYPE_SOCFPGAIMAGE_V1, /* Altera SOCFPGA A10 Preloader */
  207. IH_TYPE_MTKIMAGE, /* MediaTek BootROM loadable Image */
  208. IH_TYPE_IMX8MIMAGE, /* Freescale IMX8MBoot Image */
  209. IH_TYPE_IMX8IMAGE, /* Freescale IMX8Boot Image */
  210. IH_TYPE_COPRO, /* Coprocessor Image for remoteproc*/
  211. IH_TYPE_SUNXI_EGON, /* Allwinner eGON Boot Image */
  212. IH_TYPE_SUNXI_TOC0, /* Allwinner TOC0 Boot Image */
  213. IH_TYPE_FDT_LEGACY, /* Binary Flat Device Tree Blob in a Legacy Image */
  214. IH_TYPE_RENESAS_SPKG, /* Renesas SPKG image */
  215. IH_TYPE_COUNT, /* Number of image types */
  216. };
  217. /*
  218. * Compression Types
  219. *
  220. * The following are exposed to uImage header.
  221. * New IDs *MUST* be appended at the end of the list and *NEVER*
  222. * inserted for backward compatibility.
  223. */
  224. enum {
  225. IH_COMP_NONE = 0, /* No Compression Used */
  226. IH_COMP_GZIP, /* gzip Compression Used */
  227. IH_COMP_BZIP2, /* bzip2 Compression Used */
  228. IH_COMP_LZMA, /* lzma Compression Used */
  229. IH_COMP_LZO, /* lzo Compression Used */
  230. IH_COMP_LZ4, /* lz4 Compression Used */
  231. IH_COMP_ZSTD, /* zstd Compression Used */
  232. IH_COMP_COUNT,
  233. };
  234. /**
  235. * Phases - images intended for particular U-Boot phases (SPL, etc.)
  236. *
  237. * @IH_PHASE_NONE: No phase information, can be loaded by any phase
  238. * @IH_PHASE_U_BOOT: Only for U-Boot proper
  239. * @IH_PHASE_SPL: Only for SPL
  240. */
  241. enum image_phase_t {
  242. IH_PHASE_NONE = 0,
  243. IH_PHASE_U_BOOT,
  244. IH_PHASE_SPL,
  245. IH_PHASE_COUNT,
  246. };
  247. #define IMAGE_PHASE_SHIFT 8
  248. #define IMAGE_PHASE_MASK (0xff << IMAGE_PHASE_SHIFT)
  249. #define IMAGE_TYPE_MASK 0xff
  250. /**
  251. * image_ph() - build a composite value combining and type
  252. *
  253. * @phase: Image phase value
  254. * @type: Image type value
  255. * Returns: Composite value containing both
  256. */
  257. static inline int image_ph(enum image_phase_t phase, enum image_type_t type)
  258. {
  259. return type | (phase << IMAGE_PHASE_SHIFT);
  260. }
  261. /**
  262. * image_ph_phase() - obtain the phase from a composite phase/type value
  263. *
  264. * @image_ph_type: Composite value to convert
  265. * Returns: Phase value taken from the composite value
  266. */
  267. static inline int image_ph_phase(int image_ph_type)
  268. {
  269. return (image_ph_type & IMAGE_PHASE_MASK) >> IMAGE_PHASE_SHIFT;
  270. }
  271. /**
  272. * image_ph_type() - obtain the type from a composite phase/type value
  273. *
  274. * @image_ph_type: Composite value to convert
  275. * Returns: Type value taken from the composite value
  276. */
  277. static inline int image_ph_type(int image_ph_type)
  278. {
  279. return image_ph_type & IMAGE_TYPE_MASK;
  280. }
  281. #define LZ4F_MAGIC 0x184D2204 /* LZ4 Magic Number */
  282. #define IH_MAGIC 0x27051956 /* Image Magic Number */
  283. #define IH_NMLEN 32 /* Image Name Length */
  284. /* Reused from common.h */
  285. #define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
  286. /*
  287. * Legacy format image header,
  288. * all data in network byte order (aka natural aka bigendian).
  289. */
  290. struct legacy_img_hdr {
  291. uint32_t ih_magic; /* Image Header Magic Number */
  292. uint32_t ih_hcrc; /* Image Header CRC Checksum */
  293. uint32_t ih_time; /* Image Creation Timestamp */
  294. uint32_t ih_size; /* Image Data Size */
  295. uint32_t ih_load; /* Data Load Address */
  296. uint32_t ih_ep; /* Entry Point Address */
  297. uint32_t ih_dcrc; /* Image Data CRC Checksum */
  298. uint8_t ih_os; /* Operating System */
  299. uint8_t ih_arch; /* CPU architecture */
  300. uint8_t ih_type; /* Image Type */
  301. uint8_t ih_comp; /* Compression Type */
  302. uint8_t ih_name[IH_NMLEN]; /* Image Name */
  303. };
  304. struct image_info {
  305. ulong start, end; /* start/end of blob */
  306. ulong image_start, image_len; /* start of image within blob, len of image */
  307. ulong load; /* load addr for the image */
  308. uint8_t comp, type, os; /* compression, type of image, os type */
  309. uint8_t arch; /* CPU architecture */
  310. };
  311. /*
  312. * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
  313. * routines.
  314. */
  315. struct bootm_headers {
  316. /*
  317. * Legacy os image header, if it is a multi component image
  318. * then boot_get_ramdisk() and get_fdt() will attempt to get
  319. * data from second and third component accordingly.
  320. */
  321. struct legacy_img_hdr *legacy_hdr_os; /* image header pointer */
  322. struct legacy_img_hdr legacy_hdr_os_copy; /* header copy */
  323. ulong legacy_hdr_valid;
  324. /*
  325. * The fit_ members are only used with FIT, but it involves a lot of
  326. * #ifdefs to avoid compiling that code. Since FIT is the standard
  327. * format, even for SPL, this extra data size seems worth it.
  328. */
  329. const char *fit_uname_cfg; /* configuration node unit name */
  330. void *fit_hdr_os; /* os FIT image header */
  331. const char *fit_uname_os; /* os subimage node unit name */
  332. int fit_noffset_os; /* os subimage node offset */
  333. void *fit_hdr_rd; /* init ramdisk FIT image header */
  334. const char *fit_uname_rd; /* init ramdisk subimage node unit name */
  335. int fit_noffset_rd; /* init ramdisk subimage node offset */
  336. void *fit_hdr_fdt; /* FDT blob FIT image header */
  337. const char *fit_uname_fdt; /* FDT blob subimage node unit name */
  338. int fit_noffset_fdt;/* FDT blob subimage node offset */
  339. void *fit_hdr_setup; /* x86 setup FIT image header */
  340. const char *fit_uname_setup; /* x86 setup subimage node name */
  341. int fit_noffset_setup;/* x86 setup subimage node offset */
  342. #ifndef USE_HOSTCC
  343. struct image_info os; /* os image info */
  344. ulong ep; /* entry point of OS */
  345. ulong rd_start, rd_end;/* ramdisk start/end */
  346. char *ft_addr; /* flat dev tree address */
  347. ulong ft_len; /* length of flat device tree */
  348. ulong initrd_start;
  349. ulong initrd_end;
  350. ulong cmdline_start;
  351. ulong cmdline_end;
  352. struct bd_info *kbd;
  353. #endif
  354. int verify; /* env_get("verify")[0] != 'n' */
  355. #define BOOTM_STATE_START 0x00000001
  356. #define BOOTM_STATE_FINDOS 0x00000002
  357. #define BOOTM_STATE_FINDOTHER 0x00000004
  358. #define BOOTM_STATE_LOADOS 0x00000008
  359. #define BOOTM_STATE_RAMDISK 0x00000010
  360. #define BOOTM_STATE_FDT 0x00000020
  361. #define BOOTM_STATE_OS_CMDLINE 0x00000040
  362. #define BOOTM_STATE_OS_BD_T 0x00000080
  363. #define BOOTM_STATE_OS_PREP 0x00000100
  364. #define BOOTM_STATE_OS_FAKE_GO 0x00000200 /* 'Almost' run the OS */
  365. #define BOOTM_STATE_OS_GO 0x00000400
  366. #define BOOTM_STATE_PRE_LOAD 0x00000800
  367. int state;
  368. #if defined(CONFIG_LMB) && !defined(USE_HOSTCC)
  369. struct lmb lmb; /* for memory mgmt */
  370. #endif
  371. };
  372. #ifdef CONFIG_LMB
  373. #define images_lmb(_images) (&(_images)->lmb)
  374. #else
  375. #define images_lmb(_images) NULL
  376. #endif
  377. extern struct bootm_headers images;
  378. /*
  379. * Some systems (for example LWMON) have very short watchdog periods;
  380. * we must make sure to split long operations like memmove() or
  381. * checksum calculations into reasonable chunks.
  382. */
  383. #ifndef CHUNKSZ
  384. #define CHUNKSZ (64 * 1024)
  385. #endif
  386. #ifndef CHUNKSZ_CRC32
  387. #define CHUNKSZ_CRC32 (64 * 1024)
  388. #endif
  389. #ifndef CHUNKSZ_MD5
  390. #define CHUNKSZ_MD5 (64 * 1024)
  391. #endif
  392. #ifndef CHUNKSZ_SHA1
  393. #define CHUNKSZ_SHA1 (64 * 1024)
  394. #endif
  395. #define uimage_to_cpu(x) be32_to_cpu(x)
  396. #define cpu_to_uimage(x) cpu_to_be32(x)
  397. /*
  398. * Translation table for entries of a specific type; used by
  399. * get_table_entry_id() and get_table_entry_name().
  400. */
  401. typedef struct table_entry {
  402. int id;
  403. char *sname; /* short (input) name to find table entry */
  404. char *lname; /* long (output) name to print for messages */
  405. } table_entry_t;
  406. /*
  407. * Compression type and magic number mapping table.
  408. */
  409. struct comp_magic_map {
  410. int comp_id;
  411. const char *name;
  412. unsigned char magic[2];
  413. };
  414. /*
  415. * get_table_entry_id() scans the translation table trying to find an
  416. * entry that matches the given short name. If a matching entry is
  417. * found, it's id is returned to the caller.
  418. */
  419. int get_table_entry_id(const table_entry_t *table,
  420. const char *table_name, const char *name);
  421. /*
  422. * get_table_entry_name() scans the translation table trying to find
  423. * an entry that matches the given id. If a matching entry is found,
  424. * its long name is returned to the caller.
  425. */
  426. char *get_table_entry_name(const table_entry_t *table, char *msg, int id);
  427. const char *genimg_get_os_name(uint8_t os);
  428. /**
  429. * genimg_get_os_short_name() - get the short name for an OS
  430. *
  431. * @param os OS (IH_OS_...)
  432. * Return: OS short name, or "unknown" if unknown
  433. */
  434. const char *genimg_get_os_short_name(uint8_t comp);
  435. const char *genimg_get_arch_name(uint8_t arch);
  436. /**
  437. * genimg_get_phase_name() - Get the friendly name for a phase
  438. *
  439. * @phase: Phase value to look up
  440. * Returns: Friendly name for the phase (e.g. "U-Boot phase")
  441. */
  442. const char *genimg_get_phase_name(enum image_phase_t phase);
  443. /**
  444. * genimg_get_phase_id() - Convert a phase name to an ID
  445. *
  446. * @name: Name to convert (e.g. "u-boot")
  447. * Returns: ID for that phase (e.g. IH_PHASE_U_BOOT)
  448. */
  449. int genimg_get_phase_id(const char *name);
  450. /**
  451. * genimg_get_arch_short_name() - get the short name for an architecture
  452. *
  453. * @param arch Architecture type (IH_ARCH_...)
  454. * Return: architecture short name, or "unknown" if unknown
  455. */
  456. const char *genimg_get_arch_short_name(uint8_t arch);
  457. const char *genimg_get_type_name(uint8_t type);
  458. /**
  459. * genimg_get_type_short_name() - get the short name for an image type
  460. *
  461. * @param type Image type (IH_TYPE_...)
  462. * Return: image short name, or "unknown" if unknown
  463. */
  464. const char *genimg_get_type_short_name(uint8_t type);
  465. const char *genimg_get_comp_name(uint8_t comp);
  466. /**
  467. * genimg_get_comp_short_name() - get the short name for a compression method
  468. *
  469. * @param comp compression method (IH_COMP_...)
  470. * Return: compression method short name, or "unknown" if unknown
  471. */
  472. const char *genimg_get_comp_short_name(uint8_t comp);
  473. /**
  474. * genimg_get_cat_name() - Get the name of an item in a category
  475. *
  476. * @category: Category of item
  477. * @id: Item ID
  478. * Return: name of item, or "Unknown ..." if unknown
  479. */
  480. const char *genimg_get_cat_name(enum ih_category category, uint id);
  481. /**
  482. * genimg_get_cat_short_name() - Get the short name of an item in a category
  483. *
  484. * @category: Category of item
  485. * @id: Item ID
  486. * Return: short name of item, or "Unknown ..." if unknown
  487. */
  488. const char *genimg_get_cat_short_name(enum ih_category category, uint id);
  489. /**
  490. * genimg_get_cat_count() - Get the number of items in a category
  491. *
  492. * @category: Category to check
  493. * Return: the number of items in the category (IH_xxx_COUNT)
  494. */
  495. int genimg_get_cat_count(enum ih_category category);
  496. /**
  497. * genimg_get_cat_desc() - Get the description of a category
  498. *
  499. * @category: Category to check
  500. * Return: the description of a category, e.g. "architecture". This
  501. * effectively converts the enum to a string.
  502. */
  503. const char *genimg_get_cat_desc(enum ih_category category);
  504. /**
  505. * genimg_cat_has_id() - Check whether a category has an item
  506. *
  507. * @category: Category to check
  508. * @id: Item ID
  509. * Return: true or false as to whether a category has an item
  510. */
  511. bool genimg_cat_has_id(enum ih_category category, uint id);
  512. int genimg_get_os_id(const char *name);
  513. int genimg_get_arch_id(const char *name);
  514. int genimg_get_type_id(const char *name);
  515. int genimg_get_comp_id(const char *name);
  516. void genimg_print_size(uint32_t size);
  517. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  518. #define IMAGE_ENABLE_TIMESTAMP 1
  519. #else
  520. #define IMAGE_ENABLE_TIMESTAMP 0
  521. #endif
  522. void genimg_print_time(time_t timestamp);
  523. /* What to do with a image load address ('load = <> 'in the FIT) */
  524. enum fit_load_op {
  525. FIT_LOAD_IGNORED, /* Ignore load address */
  526. FIT_LOAD_OPTIONAL, /* Can be provided, but optional */
  527. FIT_LOAD_OPTIONAL_NON_ZERO, /* Optional, a value of 0 is ignored */
  528. FIT_LOAD_REQUIRED, /* Must be provided */
  529. };
  530. int boot_get_setup(struct bootm_headers *images, uint8_t arch, ulong *setup_start,
  531. ulong *setup_len);
  532. /* Image format types, returned by _get_format() routine */
  533. #define IMAGE_FORMAT_INVALID 0x00
  534. #define IMAGE_FORMAT_LEGACY 0x01 /* legacy image_header based format */
  535. #define IMAGE_FORMAT_FIT 0x02 /* new, libfdt based format */
  536. #define IMAGE_FORMAT_ANDROID 0x03 /* Android boot image */
  537. ulong genimg_get_kernel_addr_fit(char * const img_addr,
  538. const char **fit_uname_config,
  539. const char **fit_uname_kernel);
  540. ulong genimg_get_kernel_addr(char * const img_addr);
  541. int genimg_get_format(const void *img_addr);
  542. int genimg_has_config(struct bootm_headers *images);
  543. int boot_get_fpga(int argc, char *const argv[], struct bootm_headers *images,
  544. uint8_t arch, const ulong *ld_start, ulong * const ld_len);
  545. int boot_get_ramdisk(int argc, char *const argv[], struct bootm_headers *images,
  546. uint8_t arch, ulong *rd_start, ulong *rd_end);
  547. /**
  548. * boot_get_loadable - routine to load a list of binaries to memory
  549. * @argc: Ignored Argument
  550. * @argv: Ignored Argument
  551. * @images: pointer to the bootm images structure
  552. * @arch: expected architecture for the image
  553. * @ld_start: Ignored Argument
  554. * @ld_len: Ignored Argument
  555. *
  556. * boot_get_loadable() will take the given FIT configuration, and look
  557. * for a field named "loadables". Loadables, is a list of elements in
  558. * the FIT given as strings. exe:
  559. * loadables = "linux_kernel", "fdt-2";
  560. * this function will attempt to parse each string, and load the
  561. * corresponding element from the FIT into memory. Once placed,
  562. * no aditional actions are taken.
  563. *
  564. * @return:
  565. * 0, if only valid images or no images are found
  566. * error code, if an error occurs during fit_image_load
  567. */
  568. int boot_get_loadable(int argc, char *const argv[], struct bootm_headers *images,
  569. uint8_t arch, const ulong *ld_start, ulong *const ld_len);
  570. int boot_get_setup_fit(struct bootm_headers *images, uint8_t arch,
  571. ulong *setup_start, ulong *setup_len);
  572. /**
  573. * boot_get_fdt_fit() - load a DTB from a FIT file (applying overlays)
  574. *
  575. * This deals with all aspects of loading an DTB from a FIT.
  576. * The correct base image based on configuration will be selected, and
  577. * then any overlays specified will be applied (as present in fit_uname_configp).
  578. *
  579. * @param images Boot images structure
  580. * @param addr Address of FIT in memory
  581. * @param fit_unamep On entry this is the requested image name
  582. * (e.g. "kernel") or NULL to use the default. On exit
  583. * points to the selected image name
  584. * @param fit_uname_configp On entry this is the requested configuration
  585. * name (e.g. "conf-1") or NULL to use the default. On
  586. * exit points to the selected configuration name.
  587. * @param arch Expected architecture (IH_ARCH_...)
  588. * @param datap Returns address of loaded image
  589. * @param lenp Returns length of loaded image
  590. *
  591. * Return: node offset of base image, or -ve error code on error
  592. */
  593. int boot_get_fdt_fit(struct bootm_headers *images, ulong addr,
  594. const char **fit_unamep, const char **fit_uname_configp,
  595. int arch, ulong *datap, ulong *lenp);
  596. /**
  597. * fit_image_load() - load an image from a FIT
  598. *
  599. * This deals with all aspects of loading an image from a FIT, including
  600. * selecting the right image based on configuration, verifying it, printing
  601. * out progress messages, checking the type/arch/os and optionally copying it
  602. * to the right load address.
  603. *
  604. * The property to look up is defined by image_type.
  605. *
  606. * @param images Boot images structure
  607. * @param addr Address of FIT in memory
  608. * @param fit_unamep On entry this is the requested image name
  609. * (e.g. "kernel") or NULL to use the default. On exit
  610. * points to the selected image name
  611. * @param fit_uname_configp On entry this is the requested configuration
  612. * name (e.g. "conf-1") or NULL to use the default. On
  613. * exit points to the selected configuration name.
  614. * @param arch Expected architecture (IH_ARCH_...)
  615. * @param image_ph_type Required image type (IH_TYPE_...). If this is
  616. * IH_TYPE_KERNEL then we allow IH_TYPE_KERNEL_NOLOAD
  617. * also. If a phase is required, this is included also,
  618. * see image_phase_and_type()
  619. * @param bootstage_id ID of starting bootstage to use for progress updates.
  620. * This will be added to the BOOTSTAGE_SUB values when
  621. * calling bootstage_mark()
  622. * @param load_op Decribes what to do with the load address
  623. * @param datap Returns address of loaded image
  624. * @param lenp Returns length of loaded image
  625. * Return: node offset of image, or -ve error code on error
  626. */
  627. int fit_image_load(struct bootm_headers *images, ulong addr,
  628. const char **fit_unamep, const char **fit_uname_configp,
  629. int arch, int image_ph_type, int bootstage_id,
  630. enum fit_load_op load_op, ulong *datap, ulong *lenp);
  631. /**
  632. * image_locate_script() - Locate the raw script in an image
  633. *
  634. * @buf: Address of image
  635. * @size: Size of image in bytes
  636. * @fit_uname: Node name of FIT image to read
  637. * @confname: Node name of FIT config to read
  638. * @datap: Returns pointer to raw script on success
  639. * @lenp: Returns size of raw script on success
  640. * @return 0 if OK, non-zero on error
  641. */
  642. int image_locate_script(void *buf, int size, const char *fit_uname,
  643. const char *confname, char **datap, uint *lenp);
  644. /**
  645. * fit_get_node_from_config() - Look up an image a FIT by type
  646. *
  647. * This looks in the selected conf- node (images->fit_uname_cfg) for a
  648. * particular image type (e.g. "kernel") and then finds the image that is
  649. * referred to.
  650. *
  651. * For example, for something like:
  652. *
  653. * images {
  654. * kernel {
  655. * ...
  656. * };
  657. * };
  658. * configurations {
  659. * conf-1 {
  660. * kernel = "kernel";
  661. * };
  662. * };
  663. *
  664. * the function will return the node offset of the kernel@1 node, assuming
  665. * that conf-1 is the chosen configuration.
  666. *
  667. * @param images Boot images structure
  668. * @param prop_name Property name to look up (FIT_..._PROP)
  669. * @param addr Address of FIT in memory
  670. */
  671. int fit_get_node_from_config(struct bootm_headers *images,
  672. const char *prop_name, ulong addr);
  673. int boot_get_fdt(int flag, int argc, char *const argv[], uint8_t arch,
  674. struct bootm_headers *images,
  675. char **of_flat_tree, ulong *of_size);
  676. void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
  677. int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
  678. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  679. ulong *initrd_start, ulong *initrd_end);
  680. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
  681. int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd);
  682. /*******************************************************************/
  683. /* Legacy format specific code (prefixed with image_) */
  684. /*******************************************************************/
  685. static inline uint32_t image_get_header_size(void)
  686. {
  687. return sizeof(struct legacy_img_hdr);
  688. }
  689. #define image_get_hdr_l(f) \
  690. static inline uint32_t image_get_##f(const struct legacy_img_hdr *hdr) \
  691. { \
  692. return uimage_to_cpu(hdr->ih_##f); \
  693. }
  694. image_get_hdr_l(magic) /* image_get_magic */
  695. image_get_hdr_l(hcrc) /* image_get_hcrc */
  696. image_get_hdr_l(time) /* image_get_time */
  697. image_get_hdr_l(size) /* image_get_size */
  698. image_get_hdr_l(load) /* image_get_load */
  699. image_get_hdr_l(ep) /* image_get_ep */
  700. image_get_hdr_l(dcrc) /* image_get_dcrc */
  701. #define image_get_hdr_b(f) \
  702. static inline uint8_t image_get_##f(const struct legacy_img_hdr *hdr) \
  703. { \
  704. return hdr->ih_##f; \
  705. }
  706. image_get_hdr_b(os) /* image_get_os */
  707. image_get_hdr_b(arch) /* image_get_arch */
  708. image_get_hdr_b(type) /* image_get_type */
  709. image_get_hdr_b(comp) /* image_get_comp */
  710. static inline char *image_get_name(const struct legacy_img_hdr *hdr)
  711. {
  712. return (char *)hdr->ih_name;
  713. }
  714. static inline uint32_t image_get_data_size(const struct legacy_img_hdr *hdr)
  715. {
  716. return image_get_size(hdr);
  717. }
  718. /**
  719. * image_get_data - get image payload start address
  720. * @hdr: image header
  721. *
  722. * image_get_data() returns address of the image payload. For single
  723. * component images it is image data start. For multi component
  724. * images it points to the null terminated table of sub-images sizes.
  725. *
  726. * returns:
  727. * image payload data start address
  728. */
  729. static inline ulong image_get_data(const struct legacy_img_hdr *hdr)
  730. {
  731. return ((ulong)hdr + image_get_header_size());
  732. }
  733. static inline uint32_t image_get_image_size(const struct legacy_img_hdr *hdr)
  734. {
  735. return (image_get_size(hdr) + image_get_header_size());
  736. }
  737. static inline ulong image_get_image_end(const struct legacy_img_hdr *hdr)
  738. {
  739. return ((ulong)hdr + image_get_image_size(hdr));
  740. }
  741. #define image_set_hdr_l(f) \
  742. static inline void image_set_##f(struct legacy_img_hdr *hdr, uint32_t val) \
  743. { \
  744. hdr->ih_##f = cpu_to_uimage(val); \
  745. }
  746. image_set_hdr_l(magic) /* image_set_magic */
  747. image_set_hdr_l(hcrc) /* image_set_hcrc */
  748. image_set_hdr_l(time) /* image_set_time */
  749. image_set_hdr_l(size) /* image_set_size */
  750. image_set_hdr_l(load) /* image_set_load */
  751. image_set_hdr_l(ep) /* image_set_ep */
  752. image_set_hdr_l(dcrc) /* image_set_dcrc */
  753. #define image_set_hdr_b(f) \
  754. static inline void image_set_##f(struct legacy_img_hdr *hdr, uint8_t val) \
  755. { \
  756. hdr->ih_##f = val; \
  757. }
  758. image_set_hdr_b(os) /* image_set_os */
  759. image_set_hdr_b(arch) /* image_set_arch */
  760. image_set_hdr_b(type) /* image_set_type */
  761. image_set_hdr_b(comp) /* image_set_comp */
  762. static inline void image_set_name(struct legacy_img_hdr *hdr, const char *name)
  763. {
  764. /*
  765. * This is equivalent to: strncpy(image_get_name(hdr), name, IH_NMLEN);
  766. *
  767. * Use the tortured code below to avoid a warning with gcc 12. We do not
  768. * want to include a nul terminator if the name is of length IH_NMLEN
  769. */
  770. memcpy(image_get_name(hdr), name, strnlen(name, IH_NMLEN));
  771. }
  772. int image_check_hcrc(const struct legacy_img_hdr *hdr);
  773. int image_check_dcrc(const struct legacy_img_hdr *hdr);
  774. #ifndef USE_HOSTCC
  775. ulong env_get_bootm_low(void);
  776. phys_size_t env_get_bootm_size(void);
  777. phys_size_t env_get_bootm_mapsize(void);
  778. #endif
  779. void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
  780. static inline int image_check_magic(const struct legacy_img_hdr *hdr)
  781. {
  782. return (image_get_magic(hdr) == IH_MAGIC);
  783. }
  784. static inline int image_check_type(const struct legacy_img_hdr *hdr, uint8_t type)
  785. {
  786. return (image_get_type(hdr) == type);
  787. }
  788. static inline int image_check_arch(const struct legacy_img_hdr *hdr, uint8_t arch)
  789. {
  790. /* Let's assume that sandbox can load any architecture */
  791. if (!tools_build() && IS_ENABLED(CONFIG_SANDBOX))
  792. return true;
  793. return (image_get_arch(hdr) == arch) ||
  794. (image_get_arch(hdr) == IH_ARCH_ARM && arch == IH_ARCH_ARM64);
  795. }
  796. static inline int image_check_os(const struct legacy_img_hdr *hdr, uint8_t os)
  797. {
  798. return (image_get_os(hdr) == os);
  799. }
  800. ulong image_multi_count(const struct legacy_img_hdr *hdr);
  801. void image_multi_getimg(const struct legacy_img_hdr *hdr, ulong idx,
  802. ulong *data, ulong *len);
  803. void image_print_contents(const void *hdr);
  804. #ifndef USE_HOSTCC
  805. static inline int image_check_target_arch(const struct legacy_img_hdr *hdr)
  806. {
  807. #ifndef IH_ARCH_DEFAULT
  808. # error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
  809. #endif
  810. return image_check_arch(hdr, IH_ARCH_DEFAULT);
  811. }
  812. #endif /* USE_HOSTCC */
  813. /**
  814. * image_decomp_type() - Find out compression type of an image
  815. *
  816. * @buf: Address in U-Boot memory where image is loaded.
  817. * @len: Length of the compressed image.
  818. * Return: compression type or IH_COMP_NONE if not compressed.
  819. *
  820. * Note: Only following compression types are supported now.
  821. * lzo, lzma, gzip, bzip2
  822. */
  823. int image_decomp_type(const unsigned char *buf, ulong len);
  824. /**
  825. * image_decomp() - decompress an image
  826. *
  827. * @comp: Compression algorithm that is used (IH_COMP_...)
  828. * @load: Destination load address in U-Boot memory
  829. * @image_start Image start address (where we are decompressing from)
  830. * @type: OS type (IH_OS_...)
  831. * @load_bug: Place to decompress to
  832. * @image_buf: Address to decompress from
  833. * @image_len: Number of bytes in @image_buf to decompress
  834. * @unc_len: Available space for decompression
  835. * Return: 0 if OK, -ve on error (BOOTM_ERR_...)
  836. */
  837. int image_decomp(int comp, ulong load, ulong image_start, int type,
  838. void *load_buf, void *image_buf, ulong image_len,
  839. uint unc_len, ulong *load_end);
  840. /**
  841. * Set up properties in the FDT
  842. *
  843. * This sets up properties in the FDT that is to be passed to linux.
  844. *
  845. * @images: Images information
  846. * @blob: FDT to update
  847. * @of_size: Size of the FDT
  848. * @lmb: Points to logical memory block structure
  849. * Return: 0 if ok, <0 on failure
  850. */
  851. int image_setup_libfdt(struct bootm_headers *images, void *blob,
  852. int of_size, struct lmb *lmb);
  853. /**
  854. * Set up the FDT to use for booting a kernel
  855. *
  856. * This performs ramdisk setup, sets up the FDT if required, and adds
  857. * paramters to the FDT if libfdt is available.
  858. *
  859. * @param images Images information
  860. * Return: 0 if ok, <0 on failure
  861. */
  862. int image_setup_linux(struct bootm_headers *images);
  863. /**
  864. * bootz_setup() - Extract stat and size of a Linux xImage
  865. *
  866. * @image: Address of image
  867. * @start: Returns start address of image
  868. * @end : Returns end address of image
  869. * Return: 0 if OK, 1 if the image was not recognised
  870. */
  871. int bootz_setup(ulong image, ulong *start, ulong *end);
  872. /**
  873. * Return the correct start address and size of a Linux aarch64 Image.
  874. *
  875. * @image: Address of image
  876. * @start: Returns start address of image
  877. * @size : Returns size image
  878. * @force_reloc: Ignore image->ep field, always place image to RAM start
  879. * Return: 0 if OK, 1 if the image was not recognised
  880. */
  881. int booti_setup(ulong image, ulong *relocated_addr, ulong *size,
  882. bool force_reloc);
  883. /*******************************************************************/
  884. /* New uImage format specific code (prefixed with fit_) */
  885. /*******************************************************************/
  886. #define FIT_IMAGES_PATH "/images"
  887. #define FIT_CONFS_PATH "/configurations"
  888. /* hash/signature/key node */
  889. #define FIT_HASH_NODENAME "hash"
  890. #define FIT_ALGO_PROP "algo"
  891. #define FIT_VALUE_PROP "value"
  892. #define FIT_IGNORE_PROP "uboot-ignore"
  893. #define FIT_SIG_NODENAME "signature"
  894. #define FIT_KEY_REQUIRED "required"
  895. #define FIT_KEY_HINT "key-name-hint"
  896. /* cipher node */
  897. #define FIT_CIPHER_NODENAME "cipher"
  898. #define FIT_ALGO_PROP "algo"
  899. /* image node */
  900. #define FIT_DATA_PROP "data"
  901. #define FIT_DATA_POSITION_PROP "data-position"
  902. #define FIT_DATA_OFFSET_PROP "data-offset"
  903. #define FIT_DATA_SIZE_PROP "data-size"
  904. #define FIT_TIMESTAMP_PROP "timestamp"
  905. #define FIT_DESC_PROP "description"
  906. #define FIT_ARCH_PROP "arch"
  907. #define FIT_TYPE_PROP "type"
  908. #define FIT_OS_PROP "os"
  909. #define FIT_COMP_PROP "compression"
  910. #define FIT_ENTRY_PROP "entry"
  911. #define FIT_LOAD_PROP "load"
  912. /* configuration node */
  913. #define FIT_KERNEL_PROP "kernel"
  914. #define FIT_RAMDISK_PROP "ramdisk"
  915. #define FIT_FDT_PROP "fdt"
  916. #define FIT_LOADABLE_PROP "loadables"
  917. #define FIT_DEFAULT_PROP "default"
  918. #define FIT_SETUP_PROP "setup"
  919. #define FIT_FPGA_PROP "fpga"
  920. #define FIT_FIRMWARE_PROP "firmware"
  921. #define FIT_STANDALONE_PROP "standalone"
  922. #define FIT_SCRIPT_PROP "script"
  923. #define FIT_PHASE_PROP "phase"
  924. #define FIT_MAX_HASH_LEN HASH_MAX_DIGEST_SIZE
  925. /* cmdline argument format parsing */
  926. int fit_parse_conf(const char *spec, ulong addr_curr,
  927. ulong *addr, const char **conf_name);
  928. int fit_parse_subimage(const char *spec, ulong addr_curr,
  929. ulong *addr, const char **image_name);
  930. int fit_get_subimage_count(const void *fit, int images_noffset);
  931. void fit_print_contents(const void *fit);
  932. void fit_image_print(const void *fit, int noffset, const char *p);
  933. /**
  934. * fit_get_end - get FIT image size
  935. * @fit: pointer to the FIT format image header
  936. *
  937. * returns:
  938. * size of the FIT image (blob) in memory
  939. */
  940. static inline ulong fit_get_size(const void *fit)
  941. {
  942. return fdt_totalsize(fit);
  943. }
  944. /**
  945. * fit_get_end - get FIT image end
  946. * @fit: pointer to the FIT format image header
  947. *
  948. * returns:
  949. * end address of the FIT image (blob) in memory
  950. */
  951. ulong fit_get_end(const void *fit);
  952. /**
  953. * fit_get_name - get FIT node name
  954. * @fit: pointer to the FIT format image header
  955. *
  956. * returns:
  957. * NULL, on error
  958. * pointer to node name, on success
  959. */
  960. static inline const char *fit_get_name(const void *fit_hdr,
  961. int noffset, int *len)
  962. {
  963. return fdt_get_name(fit_hdr, noffset, len);
  964. }
  965. int fit_get_desc(const void *fit, int noffset, char **desc);
  966. int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
  967. int fit_image_get_node(const void *fit, const char *image_uname);
  968. int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
  969. int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
  970. int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
  971. int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
  972. int fit_image_get_load(const void *fit, int noffset, ulong *load);
  973. int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
  974. int fit_image_get_data(const void *fit, int noffset,
  975. const void **data, size_t *size);
  976. int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset);
  977. int fit_image_get_data_position(const void *fit, int noffset,
  978. int *data_position);
  979. int fit_image_get_data_size(const void *fit, int noffset, int *data_size);
  980. int fit_image_get_data_size_unciphered(const void *fit, int noffset,
  981. size_t *data_size);
  982. int fit_image_get_data_and_size(const void *fit, int noffset,
  983. const void **data, size_t *size);
  984. /**
  985. * fit_get_data_node() - Get verified image data for an image
  986. * @fit: Pointer to the FIT format image header
  987. * @image_uname: The name of the image node
  988. * @data: A pointer which will be filled with the location of the image data
  989. * @size: A pointer which will be filled with the size of the image data
  990. *
  991. * This function looks up the location and size of an image specified by its
  992. * name. For example, if you had a FIT like::
  993. *
  994. * images {
  995. * my-firmware {
  996. * ...
  997. * };
  998. * };
  999. *
  1000. * Then you could look up the data location and size of the my-firmware image
  1001. * by calling this function with @image_uname set to "my-firmware". This
  1002. * function also verifies the image data (if enabled) before returning. The
  1003. * image description is printed out on success. @data and @size will not be
  1004. * modified on faulure.
  1005. *
  1006. * Return:
  1007. * * 0 on success
  1008. * * -EINVAL if the image could not be verified
  1009. * * -ENOENT if there was a problem getting the data/size
  1010. * * Another negative error if there was a problem looking up the image node.
  1011. */
  1012. int fit_get_data_node(const void *fit, const char *image_uname,
  1013. const void **data, size_t *size);
  1014. /**
  1015. * fit_get_data_conf_prop() - Get verified image data for a property in /conf
  1016. * @fit: Pointer to the FIT format image header
  1017. * @prop_name: The name of the property in /conf referencing the image
  1018. * @data: A pointer which will be filled with the location of the image data
  1019. * @size: A pointer which will be filled with the size of the image data
  1020. *
  1021. * This function looks up the location and size of an image specified by a
  1022. * property in /conf. For example, if you had a FIT like::
  1023. *
  1024. * images {
  1025. * my-firmware {
  1026. * ...
  1027. * };
  1028. * };
  1029. *
  1030. * configurations {
  1031. * default = "conf-1";
  1032. * conf-1 {
  1033. * some-firmware = "my-firmware";
  1034. * };
  1035. * };
  1036. *
  1037. * Then you could look up the data location and size of the my-firmware image
  1038. * by calling this function with @prop_name set to "some-firmware". This
  1039. * function also verifies the image data (if enabled) before returning. The
  1040. * image description is printed out on success. @data and @size will not be
  1041. * modified on faulure.
  1042. *
  1043. * Return:
  1044. * * 0 on success
  1045. * * -EINVAL if the image could not be verified
  1046. * * -ENOENT if there was a problem getting the data/size
  1047. * * Another negative error if there was a problem looking up the configuration
  1048. * or image node.
  1049. */
  1050. int fit_get_data_conf_prop(const void *fit, const char *prop_name,
  1051. const void **data, size_t *size);
  1052. int fit_image_hash_get_algo(const void *fit, int noffset, const char **algo);
  1053. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  1054. int *value_len);
  1055. int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
  1056. /**
  1057. * fit_pre_load_data() - add public key to fdt blob
  1058. *
  1059. * Adds public key to the node pre load.
  1060. *
  1061. * @keydir: Directory containing keys
  1062. * @keydest: FDT blob to write public key
  1063. * @fit: Pointer to the FIT format image header
  1064. *
  1065. * returns:
  1066. * 0, on success
  1067. * < 0, on failure
  1068. */
  1069. int fit_pre_load_data(const char *keydir, void *keydest, void *fit);
  1070. int fit_cipher_data(const char *keydir, void *keydest, void *fit,
  1071. const char *comment, int require_keys,
  1072. const char *engine_id, const char *cmdname);
  1073. #define NODE_MAX_NAME_LEN 80
  1074. /**
  1075. * struct image_summary - Provides information about signing info added
  1076. *
  1077. * @sig_offset: Offset of the node in the blob devicetree where the signature
  1078. * was wriiten
  1079. * @sig_path: Path to @sig_offset
  1080. * @keydest_offset: Offset of the node in the keydest devicetree where the
  1081. * public key was written (-1 if none)
  1082. * @keydest_path: Path to @keydest_offset
  1083. */
  1084. struct image_summary {
  1085. int sig_offset;
  1086. char sig_path[NODE_MAX_NAME_LEN];
  1087. int keydest_offset;
  1088. char keydest_path[NODE_MAX_NAME_LEN];
  1089. };
  1090. /**
  1091. * fit_add_verification_data() - add verification data to FIT image nodes
  1092. *
  1093. * @keydir: Directory containing keys
  1094. * @kwydest: FDT blob to write public key information to (NULL if none)
  1095. * @fit: Pointer to the FIT format image header
  1096. * @comment: Comment to add to signature nodes
  1097. * @require_keys: Mark all keys as 'required'
  1098. * @engine_id: Engine to use for signing
  1099. * @cmdname: Command name used when reporting errors
  1100. * @algo_name: Algorithm name, or NULL if to be read from FIT
  1101. * @summary: Returns information about what data was written
  1102. *
  1103. * Adds hash values for all component images in the FIT blob.
  1104. * Hashes are calculated for all component images which have hash subnodes
  1105. * with algorithm property set to one of the supported hash algorithms.
  1106. *
  1107. * Also add signatures if signature nodes are present.
  1108. *
  1109. * returns
  1110. * 0, on success
  1111. * libfdt error code, on failure
  1112. */
  1113. int fit_add_verification_data(const char *keydir, const char *keyfile,
  1114. void *keydest, void *fit, const char *comment,
  1115. int require_keys, const char *engine_id,
  1116. const char *cmdname, const char *algo_name,
  1117. struct image_summary *summary);
  1118. /**
  1119. * fit_image_verify_with_data() - Verify an image with given data
  1120. *
  1121. * @fit: Pointer to the FIT format image header
  1122. * @image_offset: Offset in @fit of image to verify
  1123. * @key_blob: FDT containing public keys
  1124. * @data: Image data to verify
  1125. * @size: Size of image data
  1126. */
  1127. int fit_image_verify_with_data(const void *fit, int image_noffset,
  1128. const void *key_blob, const void *data,
  1129. size_t size);
  1130. int fit_image_verify(const void *fit, int noffset);
  1131. #if CONFIG_IS_ENABLED(FIT_SIGNATURE)
  1132. int fit_config_verify(const void *fit, int conf_noffset);
  1133. #else
  1134. static inline int fit_config_verify(const void *fit, int conf_noffset)
  1135. {
  1136. return 0;
  1137. }
  1138. #endif
  1139. int fit_all_image_verify(const void *fit);
  1140. int fit_config_decrypt(const void *fit, int conf_noffset);
  1141. int fit_image_check_os(const void *fit, int noffset, uint8_t os);
  1142. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
  1143. int fit_image_check_type(const void *fit, int noffset, uint8_t type);
  1144. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
  1145. /**
  1146. * fit_check_format() - Check that the FIT is valid
  1147. *
  1148. * This performs various checks on the FIT to make sure it is suitable for
  1149. * use, looking for mandatory properties, nodes, etc.
  1150. *
  1151. * If FIT_FULL_CHECK is enabled, it also runs it through libfdt to make
  1152. * sure that there are no strange tags or broken nodes in the FIT.
  1153. *
  1154. * @fit: pointer to the FIT format image header
  1155. * Return: 0 if OK, -ENOEXEC if not an FDT file, -EINVAL if the full FDT check
  1156. * failed (e.g. due to bad structure), -ENOMSG if the description is
  1157. * missing, -EBADMSG if the timestamp is missing, -ENOENT if the /images
  1158. * path is missing
  1159. */
  1160. int fit_check_format(const void *fit, ulong size);
  1161. /**
  1162. * fit_conf_find_compat() - find most compatible configuration
  1163. * @fit: pointer to the FIT format image header
  1164. * @fdt: pointer to the device tree to compare against
  1165. *
  1166. * Attempts to find the configuration whose fdt is the most compatible with the
  1167. * passed in device tree
  1168. *
  1169. * Example::
  1170. *
  1171. * / o image-tree
  1172. * |-o images
  1173. * | |-o fdt-1
  1174. * | |-o fdt-2
  1175. * |
  1176. * |-o configurations
  1177. * |-o config-1
  1178. * | |-fdt = fdt-1
  1179. * |
  1180. * |-o config-2
  1181. * |-fdt = fdt-2
  1182. *
  1183. * / o U-Boot fdt
  1184. * |-compatible = "foo,bar", "bim,bam"
  1185. *
  1186. * / o kernel fdt1
  1187. * |-compatible = "foo,bar",
  1188. *
  1189. * / o kernel fdt2
  1190. * |-compatible = "bim,bam", "baz,biz"
  1191. *
  1192. * Configuration 1 would be picked because the first string in U-Boot's
  1193. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1194. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1195. *
  1196. * As an optimization, the compatible property from the FDT's root node can be
  1197. * copied into the configuration node in the FIT image. This is required to
  1198. * match configurations with compressed FDTs.
  1199. *
  1200. * Returns: offset to the configuration to use if one was found, -1 otherwise
  1201. */
  1202. int fit_conf_find_compat(const void *fit, const void *fdt);
  1203. /**
  1204. * fit_conf_get_node - get node offset for configuration of a given unit name
  1205. * @fit: pointer to the FIT format image header
  1206. * @conf_uname: configuration node unit name (NULL to use default)
  1207. *
  1208. * fit_conf_get_node() finds a configuration (within the '/configurations'
  1209. * parent node) of a provided unit name. If configuration is found its node
  1210. * offset is returned to the caller.
  1211. *
  1212. * When NULL is provided in second argument fit_conf_get_node() will search
  1213. * for a default configuration node instead. Default configuration node unit
  1214. * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
  1215. * node.
  1216. *
  1217. * returns:
  1218. * configuration node offset when found (>=0)
  1219. * negative number on failure (FDT_ERR_* code)
  1220. */
  1221. int fit_conf_get_node(const void *fit, const char *conf_uname);
  1222. int fit_conf_get_prop_node_count(const void *fit, int noffset,
  1223. const char *prop_name);
  1224. int fit_conf_get_prop_node_index(const void *fit, int noffset,
  1225. const char *prop_name, int index);
  1226. /**
  1227. * fit_conf_get_prop_node() - Get node refered to by a configuration
  1228. * @fit: FIT to check
  1229. * @noffset: Offset of conf@xxx node to check
  1230. * @prop_name: Property to read from the conf node
  1231. * @phase: Image phase to use, IH_PHASE_NONE for any
  1232. *
  1233. * The conf- nodes contain references to other nodes, using properties
  1234. * like 'kernel = "kernel"'. Given such a property name (e.g. "kernel"),
  1235. * return the offset of the node referred to (e.g. offset of node
  1236. * "/images/kernel".
  1237. */
  1238. int fit_conf_get_prop_node(const void *fit, int noffset, const char *prop_name,
  1239. enum image_phase_t phase);
  1240. int fit_check_ramdisk(const void *fit, int os_noffset,
  1241. uint8_t arch, int verify);
  1242. int calculate_hash(const void *data, int data_len, const char *algo,
  1243. uint8_t *value, int *value_len);
  1244. /*
  1245. * At present we only support signing on the host, and verification on the
  1246. * device
  1247. */
  1248. #if defined(USE_HOSTCC)
  1249. # if defined(CONFIG_FIT_SIGNATURE)
  1250. # define IMAGE_ENABLE_SIGN 1
  1251. # define FIT_IMAGE_ENABLE_VERIFY 1
  1252. # include <openssl/evp.h>
  1253. # else
  1254. # define IMAGE_ENABLE_SIGN 0
  1255. # define FIT_IMAGE_ENABLE_VERIFY 0
  1256. # endif
  1257. #else
  1258. # define IMAGE_ENABLE_SIGN 0
  1259. # define FIT_IMAGE_ENABLE_VERIFY CONFIG_IS_ENABLED(FIT_SIGNATURE)
  1260. #endif
  1261. #ifdef USE_HOSTCC
  1262. void *image_get_host_blob(void);
  1263. void image_set_host_blob(void *host_blob);
  1264. # define gd_fdt_blob() image_get_host_blob()
  1265. #else
  1266. # define gd_fdt_blob() (gd->fdt_blob)
  1267. #endif
  1268. /*
  1269. * Information passed to the signing routines
  1270. *
  1271. * Either 'keydir', 'keyname', or 'keyfile' can be NULL. However, either
  1272. * 'keyfile', or both 'keydir' and 'keyname' should have valid values. If
  1273. * neither are valid, some operations might fail with EINVAL.
  1274. */
  1275. struct image_sign_info {
  1276. const char *keydir; /* Directory conaining keys */
  1277. const char *keyname; /* Name of key to use */
  1278. const char *keyfile; /* Filename of private or public key */
  1279. const void *fit; /* Pointer to FIT blob */
  1280. int node_offset; /* Offset of signature node */
  1281. const char *name; /* Algorithm name */
  1282. struct checksum_algo *checksum; /* Checksum algorithm information */
  1283. struct padding_algo *padding; /* Padding algorithm information */
  1284. struct crypto_algo *crypto; /* Crypto algorithm information */
  1285. const void *fdt_blob; /* FDT containing public keys */
  1286. int required_keynode; /* Node offset of key to use: -1=any */
  1287. const char *require_keys; /* Value for 'required' property */
  1288. const char *engine_id; /* Engine to use for signing */
  1289. /*
  1290. * Note: the following two fields are always valid even w/o
  1291. * RSA_VERIFY_WITH_PKEY in order to make sure this structure is
  1292. * the same on target and host. Otherwise, vboot test may fail.
  1293. */
  1294. const void *key; /* Pointer to public key in DER */
  1295. int keylen; /* Length of public key */
  1296. };
  1297. /* A part of an image, used for hashing */
  1298. struct image_region {
  1299. const void *data;
  1300. int size;
  1301. };
  1302. struct checksum_algo {
  1303. const char *name;
  1304. const int checksum_len;
  1305. const int der_len;
  1306. const uint8_t *der_prefix;
  1307. #if IMAGE_ENABLE_SIGN
  1308. const EVP_MD *(*calculate_sign)(void);
  1309. #endif
  1310. int (*calculate)(const char *name,
  1311. const struct image_region *region,
  1312. int region_count, uint8_t *checksum);
  1313. };
  1314. struct crypto_algo {
  1315. const char *name; /* Name of algorithm */
  1316. const int key_len;
  1317. /**
  1318. * sign() - calculate and return signature for given input data
  1319. *
  1320. * @info: Specifies key and FIT information
  1321. * @data: Pointer to the input data
  1322. * @data_len: Data length
  1323. * @sigp: Set to an allocated buffer holding the signature
  1324. * @sig_len: Set to length of the calculated hash
  1325. *
  1326. * This computes input data signature according to selected algorithm.
  1327. * Resulting signature value is placed in an allocated buffer, the
  1328. * pointer is returned as *sigp. The length of the calculated
  1329. * signature is returned via the sig_len pointer argument. The caller
  1330. * should free *sigp.
  1331. *
  1332. * @return: 0, on success, -ve on error
  1333. */
  1334. int (*sign)(struct image_sign_info *info,
  1335. const struct image_region region[],
  1336. int region_count, uint8_t **sigp, uint *sig_len);
  1337. /**
  1338. * add_verify_data() - Add verification information to FDT
  1339. *
  1340. * Add public key information to the FDT node, suitable for
  1341. * verification at run-time. The information added depends on the
  1342. * algorithm being used.
  1343. *
  1344. * @info: Specifies key and FIT information
  1345. * @keydest: Destination FDT blob for public key data
  1346. * @return: node offset within the FDT blob where the data was written,
  1347. * or -ve on error
  1348. */
  1349. int (*add_verify_data)(struct image_sign_info *info, void *keydest);
  1350. /**
  1351. * verify() - Verify a signature against some data
  1352. *
  1353. * @info: Specifies key and FIT information
  1354. * @data: Pointer to the input data
  1355. * @data_len: Data length
  1356. * @sig: Signature
  1357. * @sig_len: Number of bytes in signature
  1358. * @return 0 if verified, -ve on error
  1359. */
  1360. int (*verify)(struct image_sign_info *info,
  1361. const struct image_region region[], int region_count,
  1362. uint8_t *sig, uint sig_len);
  1363. };
  1364. /* Declare a new U-Boot crypto algorithm handler */
  1365. #define U_BOOT_CRYPTO_ALGO(__name) \
  1366. ll_entry_declare(struct crypto_algo, __name, cryptos)
  1367. struct padding_algo {
  1368. const char *name;
  1369. int (*verify)(struct image_sign_info *info,
  1370. const uint8_t *pad, int pad_len,
  1371. const uint8_t *hash, int hash_len);
  1372. };
  1373. /* Declare a new U-Boot padding algorithm handler */
  1374. #define U_BOOT_PADDING_ALGO(__name) \
  1375. ll_entry_declare(struct padding_algo, __name, paddings)
  1376. /**
  1377. * image_get_checksum_algo() - Look up a checksum algorithm
  1378. *
  1379. * @param full_name Name of algorithm in the form "checksum,crypto"
  1380. * Return: pointer to algorithm information, or NULL if not found
  1381. */
  1382. struct checksum_algo *image_get_checksum_algo(const char *full_name);
  1383. /**
  1384. * image_get_crypto_algo() - Look up a cryptosystem algorithm
  1385. *
  1386. * @param full_name Name of algorithm in the form "checksum,crypto"
  1387. * Return: pointer to algorithm information, or NULL if not found
  1388. */
  1389. struct crypto_algo *image_get_crypto_algo(const char *full_name);
  1390. /**
  1391. * image_get_padding_algo() - Look up a padding algorithm
  1392. *
  1393. * @param name Name of padding algorithm
  1394. * Return: pointer to algorithm information, or NULL if not found
  1395. */
  1396. struct padding_algo *image_get_padding_algo(const char *name);
  1397. #define IMAGE_PRE_LOAD_SIG_MAGIC 0x55425348
  1398. #define IMAGE_PRE_LOAD_SIG_OFFSET_MAGIC 0
  1399. #define IMAGE_PRE_LOAD_SIG_OFFSET_IMG_LEN 4
  1400. #define IMAGE_PRE_LOAD_SIG_OFFSET_SIG 8
  1401. #define IMAGE_PRE_LOAD_PATH "/image/pre-load/sig"
  1402. #define IMAGE_PRE_LOAD_PROP_ALGO_NAME "algo-name"
  1403. #define IMAGE_PRE_LOAD_PROP_PADDING_NAME "padding-name"
  1404. #define IMAGE_PRE_LOAD_PROP_SIG_SIZE "signature-size"
  1405. #define IMAGE_PRE_LOAD_PROP_PUBLIC_KEY "public-key"
  1406. #define IMAGE_PRE_LOAD_PROP_MANDATORY "mandatory"
  1407. /*
  1408. * Information in the device-tree about the signature in the header
  1409. */
  1410. struct image_sig_info {
  1411. char *algo_name; /* Name of the algo (eg: sha256,rsa2048) */
  1412. char *padding_name; /* Name of the padding */
  1413. uint8_t *key; /* Public signature key */
  1414. int key_len; /* Length of the public key */
  1415. uint32_t sig_size; /* size of the signature (in the header) */
  1416. int mandatory; /* Set if the signature is mandatory */
  1417. struct image_sign_info sig_info; /* Signature info */
  1418. };
  1419. /*
  1420. * Header of the signature header
  1421. */
  1422. struct sig_header_s {
  1423. uint32_t magic;
  1424. uint32_t version;
  1425. uint32_t header_size;
  1426. uint32_t image_size;
  1427. uint32_t offset_img_sig;
  1428. uint32_t flags;
  1429. uint32_t reserved0;
  1430. uint32_t reserved1;
  1431. uint8_t sha256_img_sig[SHA256_SUM_LEN];
  1432. };
  1433. #define SIG_HEADER_LEN (sizeof(struct sig_header_s))
  1434. /**
  1435. * image_pre_load() - Manage pre load header
  1436. *
  1437. * Manage the pre-load header before launching the image.
  1438. * It checks the signature of the image. It also set the
  1439. * variable image_load_offset to skip this header before
  1440. * launching the image.
  1441. *
  1442. * @param addr Address of the image
  1443. * @return: 0 on success, -ve on error
  1444. */
  1445. int image_pre_load(ulong addr);
  1446. /**
  1447. * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
  1448. *
  1449. * @fit: FIT to check
  1450. * @image_noffset: Offset of image node to check
  1451. * @data: Image data to check
  1452. * @size: Size of image data
  1453. * @key_blob: FDT containing public keys
  1454. * @no_sigsp: Returns 1 if no signatures were required, and
  1455. * therefore nothing was checked. The caller may wish
  1456. * to fall back to other mechanisms, or refuse to
  1457. * boot.
  1458. * Return: 0 if all verified ok, <0 on error
  1459. */
  1460. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  1461. const char *data, size_t size, const void *key_blob,
  1462. int *no_sigsp);
  1463. /**
  1464. * fit_image_check_sig() - Check a single image signature node
  1465. *
  1466. * @fit: FIT to check
  1467. * @noffset: Offset of signature node to check
  1468. * @data: Image data to check
  1469. * @size: Size of image data
  1470. * @keyblob: Key blob to check (typically the control FDT)
  1471. * @required_keynode: Offset in the keyblob of the required key node,
  1472. * if any. If this is given, then the image wil not
  1473. * pass verification unless that key is used. If this is
  1474. * -1 then any signature will do.
  1475. * @err_msgp: In the event of an error, this will be pointed to a
  1476. * help error string to display to the user.
  1477. * Return: 0 if all verified ok, <0 on error
  1478. */
  1479. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  1480. size_t size, const void *key_blob, int required_keynode,
  1481. char **err_msgp);
  1482. int fit_image_decrypt_data(const void *fit,
  1483. int image_noffset, int cipher_noffset,
  1484. const void *data, size_t size,
  1485. void **data_unciphered, size_t *size_unciphered);
  1486. /**
  1487. * fit_region_make_list() - Make a list of regions to hash
  1488. *
  1489. * Given a list of FIT regions (offset, size) provided by libfdt, create
  1490. * a list of regions (void *, size) for use by the signature creationg
  1491. * and verification code.
  1492. *
  1493. * @fit: FIT image to process
  1494. * @fdt_regions: Regions as returned by libfdt
  1495. * @count: Number of regions returned by libfdt
  1496. * @region: Place to put list of regions (NULL to allocate it)
  1497. * Return: pointer to list of regions, or NULL if out of memory
  1498. */
  1499. struct image_region *fit_region_make_list(const void *fit,
  1500. struct fdt_region *fdt_regions, int count,
  1501. struct image_region *region);
  1502. static inline int fit_image_check_target_arch(const void *fdt, int node)
  1503. {
  1504. #ifndef USE_HOSTCC
  1505. return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
  1506. #else
  1507. return 0;
  1508. #endif
  1509. }
  1510. /*
  1511. * At present we only support ciphering on the host, and unciphering on the
  1512. * device
  1513. */
  1514. #if defined(USE_HOSTCC)
  1515. # if defined(CONFIG_FIT_CIPHER)
  1516. # define IMAGE_ENABLE_ENCRYPT 1
  1517. # define IMAGE_ENABLE_DECRYPT 1
  1518. # include <openssl/evp.h>
  1519. # else
  1520. # define IMAGE_ENABLE_ENCRYPT 0
  1521. # define IMAGE_ENABLE_DECRYPT 0
  1522. # endif
  1523. #else
  1524. # define IMAGE_ENABLE_ENCRYPT 0
  1525. # define IMAGE_ENABLE_DECRYPT CONFIG_IS_ENABLED(FIT_CIPHER)
  1526. #endif
  1527. /* Information passed to the ciphering routines */
  1528. struct image_cipher_info {
  1529. const char *keydir; /* Directory containing keys */
  1530. const char *keyname; /* Name of key to use */
  1531. const char *ivname; /* Name of IV to use */
  1532. const void *fit; /* Pointer to FIT blob */
  1533. int node_noffset; /* Offset of the cipher node */
  1534. const char *name; /* Algorithm name */
  1535. struct cipher_algo *cipher; /* Cipher algorithm information */
  1536. const void *fdt_blob; /* FDT containing key and IV */
  1537. const void *key; /* Value of the key */
  1538. const void *iv; /* Value of the IV */
  1539. size_t size_unciphered; /* Size of the unciphered data */
  1540. };
  1541. struct cipher_algo {
  1542. const char *name; /* Name of algorithm */
  1543. int key_len; /* Length of the key */
  1544. int iv_len; /* Length of the IV */
  1545. #if IMAGE_ENABLE_ENCRYPT
  1546. const EVP_CIPHER * (*calculate_type)(void);
  1547. #endif
  1548. int (*encrypt)(struct image_cipher_info *info,
  1549. const unsigned char *data, int data_len,
  1550. unsigned char **cipher, int *cipher_len);
  1551. int (*add_cipher_data)(struct image_cipher_info *info,
  1552. void *keydest, void *fit, int node_noffset);
  1553. int (*decrypt)(struct image_cipher_info *info,
  1554. const void *cipher, size_t cipher_len,
  1555. void **data, size_t *data_len);
  1556. };
  1557. int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo);
  1558. struct cipher_algo *image_get_cipher_algo(const char *full_name);
  1559. struct andr_image_data;
  1560. /**
  1561. * android_image_get_data() - Parse Android boot images
  1562. *
  1563. * This is used to parse boot and vendor-boot header into
  1564. * andr_image_data generic structure.
  1565. *
  1566. * @boot_hdr: Pointer to boot image header
  1567. * @vendor_boot_hdr: Pointer to vendor boot image header
  1568. * @data: Pointer to generic boot format structure
  1569. * Return: true if succeeded, false otherwise
  1570. */
  1571. bool android_image_get_data(const void *boot_hdr, const void *vendor_boot_hdr,
  1572. struct andr_image_data *data);
  1573. struct andr_boot_img_hdr_v0;
  1574. /**
  1575. * android_image_get_kernel() - Processes kernel part of Android boot images
  1576. *
  1577. * This function returns the os image's start address and length. Also,
  1578. * it appends the kernel command line to the bootargs env variable.
  1579. *
  1580. * @hdr: Pointer to image header, which is at the start
  1581. * of the image.
  1582. * @vendor_boot_img : Pointer to vendor boot image header
  1583. * @verify: Checksum verification flag. Currently unimplemented.
  1584. * @os_data: Pointer to a ulong variable, will hold os data start
  1585. * address.
  1586. * @os_len: Pointer to a ulong variable, will hold os data length.
  1587. * Return: Zero, os start address and length on success,
  1588. * otherwise on failure.
  1589. */
  1590. int android_image_get_kernel(const void *hdr,
  1591. const void *vendor_boot_img, int verify,
  1592. ulong *os_data, ulong *os_len);
  1593. /**
  1594. * android_image_get_ramdisk() - Extracts the ramdisk load address and its size
  1595. *
  1596. * This extracts the load address of the ramdisk and its size
  1597. *
  1598. * @hdr: Pointer to image header
  1599. * @vendor_boot_img : Pointer to vendor boot image header
  1600. * @rd_data: Pointer to a ulong variable, will hold ramdisk address
  1601. * @rd_len: Pointer to a ulong variable, will hold ramdisk length
  1602. * Return: 0 if succeeded, -1 if ramdisk size is 0
  1603. */
  1604. int android_image_get_ramdisk(const void *hdr, const void *vendor_boot_img,
  1605. ulong *rd_data, ulong *rd_len);
  1606. /**
  1607. * android_image_get_second() - Extracts the secondary bootloader address
  1608. * and its size
  1609. *
  1610. * This extracts the address of the secondary bootloader and its size
  1611. *
  1612. * @hdr: Pointer to image header
  1613. * @second_data: Pointer to a ulong variable, will hold secondary bootloader address
  1614. * @second_len : Pointer to a ulong variable, will hold secondary bootloader length
  1615. * Return: 0 if succeeded, -1 if secondary bootloader size is 0
  1616. */
  1617. int android_image_get_second(const void *hdr, ulong *second_data, ulong *second_len);
  1618. bool android_image_get_dtbo(ulong hdr_addr, ulong *addr, u32 *size);
  1619. /**
  1620. * android_image_get_dtb_by_index() - Get address and size of blob in DTB area.
  1621. * @hdr_addr: Boot image header address
  1622. * @vendor_boot_img: Pointer to vendor boot image header, which is at the start of the image.
  1623. * @index: Index of desired DTB in DTB area (starting from 0)
  1624. * @addr: If not NULL, will contain address to specified DTB
  1625. * @size: If not NULL, will contain size of specified DTB
  1626. *
  1627. * Get the address and size of DTB blob by its index in DTB area of Android
  1628. * Boot Image in RAM.
  1629. *
  1630. * Return: true on success or false on error.
  1631. */
  1632. bool android_image_get_dtb_by_index(ulong hdr_addr, ulong vendor_boot_img,
  1633. u32 index, ulong *addr, u32 *size);
  1634. /**
  1635. * android_image_get_end() - Get the end of Android boot image
  1636. *
  1637. * This returns the end address of Android boot image address
  1638. *
  1639. * @hdr: Pointer to image header
  1640. * @vendor_boot_img : Pointer to vendor boot image header
  1641. * Return: The end address of Android boot image
  1642. */
  1643. ulong android_image_get_end(const struct andr_boot_img_hdr_v0 *hdr,
  1644. const void *vendor_boot_img);
  1645. /**
  1646. * android_image_get_kload() - Get the kernel load address
  1647. *
  1648. * This returns the kernel load address. The load address is extracted
  1649. * from the boot image header or the "kernel_addr_r" environment variable
  1650. *
  1651. * @hdr: Pointer to image header
  1652. * @vendor_boot_img : Pointer to vendor boot image header
  1653. * Return: The kernel load address
  1654. */
  1655. ulong android_image_get_kload(const void *hdr,
  1656. const void *vendor_boot_img);
  1657. /**
  1658. * android_image_get_kcomp() - Get kernel compression type
  1659. *
  1660. * This gets the kernel compression type from the boot image header
  1661. *
  1662. * @hdr: Pointer to image header
  1663. * @vendor_boot_img : Pointer to vendor boot image header
  1664. * Return: Kernel compression type
  1665. */
  1666. ulong android_image_get_kcomp(const void *hdr,
  1667. const void *vendor_boot_img);
  1668. /**
  1669. * android_print_contents() - Prints out the contents of the Android format image
  1670. *
  1671. * This formats a multi line Android image contents description.
  1672. * The routine prints out Android image properties
  1673. *
  1674. * @hdr: Pointer to the Android format image header
  1675. * Return: no returned results
  1676. */
  1677. void android_print_contents(const struct andr_boot_img_hdr_v0 *hdr);
  1678. bool android_image_print_dtb_contents(ulong hdr_addr);
  1679. /**
  1680. * is_android_boot_image_header() - Check the magic of boot image
  1681. *
  1682. * This checks the header of Android boot image and verifies the
  1683. * magic is "ANDROID!"
  1684. *
  1685. * @hdr: Pointer to boot image
  1686. * Return: non-zero if the magic is correct, zero otherwise
  1687. */
  1688. bool is_android_boot_image_header(const void *hdr);
  1689. /**
  1690. * is_android_vendor_boot_image_header() - Check the magic of vendor boot image
  1691. *
  1692. * This checks the header of Android vendor boot image and verifies the magic
  1693. * is "VNDRBOOT"
  1694. *
  1695. * @vendor_boot_img: Pointer to boot image
  1696. * Return: non-zero if the magic is correct, zero otherwise
  1697. */
  1698. bool is_android_vendor_boot_image_header(const void *vendor_boot_img);
  1699. /**
  1700. * get_abootimg_addr() - Get Android boot image address
  1701. *
  1702. * Return: Android boot image address
  1703. */
  1704. ulong get_abootimg_addr(void);
  1705. /**
  1706. * get_avendor_bootimg_addr() - Get Android vendor boot image address
  1707. *
  1708. * Return: Android vendor boot image address
  1709. */
  1710. ulong get_avendor_bootimg_addr(void);
  1711. /**
  1712. * board_fit_config_name_match() - Check for a matching board name
  1713. *
  1714. * This is used when SPL loads a FIT containing multiple device tree files
  1715. * and wants to work out which one to use. The description of each one is
  1716. * passed to this function. The description comes from the 'description' field
  1717. * in each (FDT) image node.
  1718. *
  1719. * @name: Device tree description
  1720. * Return: 0 if this device tree should be used, non-zero to try the next
  1721. */
  1722. int board_fit_config_name_match(const char *name);
  1723. /**
  1724. * board_fit_image_post_process() - Do any post-process on FIT binary data
  1725. *
  1726. * This is used to do any sort of image manipulation, verification, decryption
  1727. * etc. in a platform or board specific way. Obviously, anything done here would
  1728. * need to be comprehended in how the images were prepared before being injected
  1729. * into the FIT creation (i.e. the binary blobs would have been pre-processed
  1730. * before being added to the FIT image).
  1731. *
  1732. * @fit: pointer to fit image
  1733. * @node: offset of image node
  1734. * @image: pointer to the image start pointer
  1735. * @size: pointer to the image size
  1736. * Return: no return value (failure should be handled internally)
  1737. */
  1738. void board_fit_image_post_process(const void *fit, int node, void **p_image,
  1739. size_t *p_size);
  1740. #define FDT_ERROR ((ulong)(-1))
  1741. ulong fdt_getprop_u32(const void *fdt, int node, const char *prop);
  1742. /**
  1743. * fit_find_config_node() - Find the node for the best DTB in a FIT image
  1744. *
  1745. * A FIT image contains one or more DTBs. This function parses the
  1746. * configurations described in the FIT images and returns the node of
  1747. * the first matching DTB. To check if a DTB matches a board, this function
  1748. * calls board_fit_config_name_match(). If no matching DTB is found, it returns
  1749. * the node described by the default configuration if it exists.
  1750. *
  1751. * @fdt: pointer to flat device tree
  1752. * Return: the node if found, -ve otherwise
  1753. */
  1754. int fit_find_config_node(const void *fdt);
  1755. /**
  1756. * Mapping of image types to function handlers to be invoked on the associated
  1757. * loaded images
  1758. *
  1759. * @type: Type of image, I.E. IH_TYPE_*
  1760. * @handler: Function to call on loaded image
  1761. */
  1762. struct fit_loadable_tbl {
  1763. int type;
  1764. /**
  1765. * handler() - Process a loaded image
  1766. *
  1767. * @data: Pointer to start of loaded image data
  1768. * @size: Size of loaded image data
  1769. */
  1770. void (*handler)(ulong data, size_t size);
  1771. };
  1772. /*
  1773. * Define a FIT loadable image type handler
  1774. *
  1775. * _type is a valid uimage_type ID as defined in the "Image Type" enum above
  1776. * _handler is the handler function to call after this image type is loaded
  1777. */
  1778. #define U_BOOT_FIT_LOADABLE_HANDLER(_type, _handler) \
  1779. ll_entry_declare(struct fit_loadable_tbl, _function, fit_loadable) = { \
  1780. .type = _type, \
  1781. .handler = _handler, \
  1782. }
  1783. /**
  1784. * fit_update - update storage with FIT image
  1785. * @fit: Pointer to FIT image
  1786. *
  1787. * Update firmware on storage using FIT image as input.
  1788. * The storage area to be update will be identified by the name
  1789. * in FIT and matching it to "dfu_alt_info" variable.
  1790. *
  1791. * Return: 0 on success, non-zero otherwise
  1792. */
  1793. int fit_update(const void *fit);
  1794. #endif /* __IMAGE_H__ */