image.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2005
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. ********************************************************************
  9. * NOTE: This header file defines an interface to U-Boot. Including
  10. * this (unmodified) header file in another file is considered normal
  11. * use of U-Boot, and does *not* fall under the heading of "derived
  12. * work".
  13. ********************************************************************
  14. */
  15. #ifndef __IMAGE_H__
  16. #define __IMAGE_H__
  17. #include "compiler.h"
  18. #include <asm/byteorder.h>
  19. /* Define this to avoid #ifdefs later on */
  20. struct lmb;
  21. #ifdef USE_HOSTCC
  22. /* new uImage format support enabled on host */
  23. #define CONFIG_FIT 1
  24. #define CONFIG_OF_LIBFDT 1
  25. #define CONFIG_FIT_VERBOSE 1 /* enable fit_format_{error,warning}() */
  26. #define IMAGE_ENABLE_IGNORE 0
  27. #define IMAGE_INDENT_STRING ""
  28. #else
  29. #include <lmb.h>
  30. #include <asm/u-boot.h>
  31. #include <command.h>
  32. /* Take notice of the 'ignore' property for hashes */
  33. #define IMAGE_ENABLE_IGNORE 1
  34. #define IMAGE_INDENT_STRING " "
  35. #endif /* USE_HOSTCC */
  36. #if defined(CONFIG_FIT)
  37. #include <libfdt.h>
  38. #include <fdt_support.h>
  39. # ifdef CONFIG_SPL_BUILD
  40. # ifdef CONFIG_SPL_CRC32_SUPPORT
  41. # define IMAGE_ENABLE_CRC32 1
  42. # endif
  43. # ifdef CONFIG_SPL_MD5_SUPPORT
  44. # define IMAGE_ENABLE_MD5 1
  45. # endif
  46. # ifdef CONFIG_SPL_SHA1_SUPPORT
  47. # define IMAGE_ENABLE_SHA1 1
  48. # endif
  49. # else
  50. # define CONFIG_CRC32 /* FIT images need CRC32 support */
  51. # define CONFIG_MD5 /* and MD5 */
  52. # define CONFIG_SHA1 /* and SHA1 */
  53. # define IMAGE_ENABLE_CRC32 1
  54. # define IMAGE_ENABLE_MD5 1
  55. # define IMAGE_ENABLE_SHA1 1
  56. # endif
  57. #ifndef IMAGE_ENABLE_CRC32
  58. #define IMAGE_ENABLE_CRC32 0
  59. #endif
  60. #ifndef IMAGE_ENABLE_MD5
  61. #define IMAGE_ENABLE_MD5 0
  62. #endif
  63. #ifndef IMAGE_ENABLE_SHA1
  64. #define IMAGE_ENABLE_SHA1 0
  65. #endif
  66. #endif /* CONFIG_FIT */
  67. #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
  68. # define IMAGE_ENABLE_RAMDISK_HIGH 1
  69. #else
  70. # define IMAGE_ENABLE_RAMDISK_HIGH 0
  71. #endif
  72. #ifdef CONFIG_OF_LIBFDT
  73. # define IMAGE_ENABLE_OF_LIBFDT 1
  74. #else
  75. # define IMAGE_ENABLE_OF_LIBFDT 0
  76. #endif
  77. #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
  78. # define IMAGE_BOOT_GET_CMDLINE 1
  79. #else
  80. # define IMAGE_BOOT_GET_CMDLINE 0
  81. #endif
  82. #ifdef CONFIG_OF_BOARD_SETUP
  83. # define IMAGE_OF_BOARD_SETUP 1
  84. #else
  85. # define IMAGE_OF_BOARD_SETUP 0
  86. #endif
  87. /*
  88. * Operating System Codes
  89. */
  90. #define IH_OS_INVALID 0 /* Invalid OS */
  91. #define IH_OS_OPENBSD 1 /* OpenBSD */
  92. #define IH_OS_NETBSD 2 /* NetBSD */
  93. #define IH_OS_FREEBSD 3 /* FreeBSD */
  94. #define IH_OS_4_4BSD 4 /* 4.4BSD */
  95. #define IH_OS_LINUX 5 /* Linux */
  96. #define IH_OS_SVR4 6 /* SVR4 */
  97. #define IH_OS_ESIX 7 /* Esix */
  98. #define IH_OS_SOLARIS 8 /* Solaris */
  99. #define IH_OS_IRIX 9 /* Irix */
  100. #define IH_OS_SCO 10 /* SCO */
  101. #define IH_OS_DELL 11 /* Dell */
  102. #define IH_OS_NCR 12 /* NCR */
  103. #define IH_OS_LYNXOS 13 /* LynxOS */
  104. #define IH_OS_VXWORKS 14 /* VxWorks */
  105. #define IH_OS_PSOS 15 /* pSOS */
  106. #define IH_OS_QNX 16 /* QNX */
  107. #define IH_OS_U_BOOT 17 /* Firmware */
  108. #define IH_OS_RTEMS 18 /* RTEMS */
  109. #define IH_OS_ARTOS 19 /* ARTOS */
  110. #define IH_OS_UNITY 20 /* Unity OS */
  111. #define IH_OS_INTEGRITY 21 /* INTEGRITY */
  112. #define IH_OS_OSE 22 /* OSE */
  113. #define IH_OS_PLAN9 23 /* Plan 9 */
  114. /*
  115. * CPU Architecture Codes (supported by Linux)
  116. */
  117. #define IH_ARCH_INVALID 0 /* Invalid CPU */
  118. #define IH_ARCH_ALPHA 1 /* Alpha */
  119. #define IH_ARCH_ARM 2 /* ARM */
  120. #define IH_ARCH_I386 3 /* Intel x86 */
  121. #define IH_ARCH_IA64 4 /* IA64 */
  122. #define IH_ARCH_MIPS 5 /* MIPS */
  123. #define IH_ARCH_MIPS64 6 /* MIPS 64 Bit */
  124. #define IH_ARCH_PPC 7 /* PowerPC */
  125. #define IH_ARCH_S390 8 /* IBM S390 */
  126. #define IH_ARCH_SH 9 /* SuperH */
  127. #define IH_ARCH_SPARC 10 /* Sparc */
  128. #define IH_ARCH_SPARC64 11 /* Sparc 64 Bit */
  129. #define IH_ARCH_M68K 12 /* M68K */
  130. #define IH_ARCH_MICROBLAZE 14 /* MicroBlaze */
  131. #define IH_ARCH_NIOS2 15 /* Nios-II */
  132. #define IH_ARCH_BLACKFIN 16 /* Blackfin */
  133. #define IH_ARCH_AVR32 17 /* AVR32 */
  134. #define IH_ARCH_ST200 18 /* STMicroelectronics ST200 */
  135. #define IH_ARCH_SANDBOX 19 /* Sandbox architecture (test only) */
  136. #define IH_ARCH_NDS32 20 /* ANDES Technology - NDS32 */
  137. #define IH_ARCH_OPENRISC 21 /* OpenRISC 1000 */
  138. #define IH_ARCH_ARM64 22 /* ARM64 */
  139. #define IH_ARCH_ARC 23 /* Synopsys DesignWare ARC */
  140. /*
  141. * Image Types
  142. *
  143. * "Standalone Programs" are directly runnable in the environment
  144. * provided by U-Boot; it is expected that (if they behave
  145. * well) you can continue to work in U-Boot after return from
  146. * the Standalone Program.
  147. * "OS Kernel Images" are usually images of some Embedded OS which
  148. * will take over control completely. Usually these programs
  149. * will install their own set of exception handlers, device
  150. * drivers, set up the MMU, etc. - this means, that you cannot
  151. * expect to re-enter U-Boot except by resetting the CPU.
  152. * "RAMDisk Images" are more or less just data blocks, and their
  153. * parameters (address, size) are passed to an OS kernel that is
  154. * being started.
  155. * "Multi-File Images" contain several images, typically an OS
  156. * (Linux) kernel image and one or more data images like
  157. * RAMDisks. This construct is useful for instance when you want
  158. * to boot over the network using BOOTP etc., where the boot
  159. * server provides just a single image file, but you want to get
  160. * for instance an OS kernel and a RAMDisk image.
  161. *
  162. * "Multi-File Images" start with a list of image sizes, each
  163. * image size (in bytes) specified by an "uint32_t" in network
  164. * byte order. This list is terminated by an "(uint32_t)0".
  165. * Immediately after the terminating 0 follow the images, one by
  166. * one, all aligned on "uint32_t" boundaries (size rounded up to
  167. * a multiple of 4 bytes - except for the last file).
  168. *
  169. * "Firmware Images" are binary images containing firmware (like
  170. * U-Boot or FPGA images) which usually will be programmed to
  171. * flash memory.
  172. *
  173. * "Script files" are command sequences that will be executed by
  174. * U-Boot's command interpreter; this feature is especially
  175. * useful when you configure U-Boot to use a real shell (hush)
  176. * as command interpreter (=> Shell Scripts).
  177. */
  178. #define IH_TYPE_INVALID 0 /* Invalid Image */
  179. #define IH_TYPE_STANDALONE 1 /* Standalone Program */
  180. #define IH_TYPE_KERNEL 2 /* OS Kernel Image */
  181. #define IH_TYPE_RAMDISK 3 /* RAMDisk Image */
  182. #define IH_TYPE_MULTI 4 /* Multi-File Image */
  183. #define IH_TYPE_FIRMWARE 5 /* Firmware Image */
  184. #define IH_TYPE_SCRIPT 6 /* Script file */
  185. #define IH_TYPE_FILESYSTEM 7 /* Filesystem Image (any type) */
  186. #define IH_TYPE_FLATDT 8 /* Binary Flat Device Tree Blob */
  187. #define IH_TYPE_KWBIMAGE 9 /* Kirkwood Boot Image */
  188. #define IH_TYPE_IMXIMAGE 10 /* Freescale IMXBoot Image */
  189. #define IH_TYPE_UBLIMAGE 11 /* Davinci UBL Image */
  190. #define IH_TYPE_OMAPIMAGE 12 /* TI OMAP Config Header Image */
  191. #define IH_TYPE_AISIMAGE 13 /* TI Davinci AIS Image */
  192. #define IH_TYPE_KERNEL_NOLOAD 14 /* OS Kernel Image, can run from any load address */
  193. #define IH_TYPE_PBLIMAGE 15 /* Freescale PBL Boot Image */
  194. #define IH_TYPE_MXSIMAGE 16 /* Freescale MXSBoot Image */
  195. #define IH_TYPE_GPIMAGE 17 /* TI Keystone GPHeader Image */
  196. /*
  197. * Compression Types
  198. */
  199. #define IH_COMP_NONE 0 /* No Compression Used */
  200. #define IH_COMP_GZIP 1 /* gzip Compression Used */
  201. #define IH_COMP_BZIP2 2 /* bzip2 Compression Used */
  202. #define IH_COMP_LZMA 3 /* lzma Compression Used */
  203. #define IH_COMP_LZO 4 /* lzo Compression Used */
  204. #define IH_MAGIC 0x27051956 /* Image Magic Number */
  205. #define IH_NMLEN 32 /* Image Name Length */
  206. /* Reused from common.h */
  207. #define ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
  208. /*
  209. * Legacy format image header,
  210. * all data in network byte order (aka natural aka bigendian).
  211. */
  212. typedef struct image_header {
  213. __be32 ih_magic; /* Image Header Magic Number */
  214. __be32 ih_hcrc; /* Image Header CRC Checksum */
  215. __be32 ih_time; /* Image Creation Timestamp */
  216. __be32 ih_size; /* Image Data Size */
  217. __be32 ih_load; /* Data Load Address */
  218. __be32 ih_ep; /* Entry Point Address */
  219. __be32 ih_dcrc; /* Image Data CRC Checksum */
  220. uint8_t ih_os; /* Operating System */
  221. uint8_t ih_arch; /* CPU architecture */
  222. uint8_t ih_type; /* Image Type */
  223. uint8_t ih_comp; /* Compression Type */
  224. uint8_t ih_name[IH_NMLEN]; /* Image Name */
  225. } image_header_t;
  226. typedef struct image_info {
  227. ulong start, end; /* start/end of blob */
  228. ulong image_start, image_len; /* start of image within blob, len of image */
  229. ulong load; /* load addr for the image */
  230. uint8_t comp, type, os; /* compression, type of image, os type */
  231. } image_info_t;
  232. /*
  233. * Legacy and FIT format headers used by do_bootm() and do_bootm_<os>()
  234. * routines.
  235. */
  236. typedef struct bootm_headers {
  237. /*
  238. * Legacy os image header, if it is a multi component image
  239. * then boot_get_ramdisk() and get_fdt() will attempt to get
  240. * data from second and third component accordingly.
  241. */
  242. image_header_t *legacy_hdr_os; /* image header pointer */
  243. image_header_t legacy_hdr_os_copy; /* header copy */
  244. ulong legacy_hdr_valid;
  245. #if defined(CONFIG_FIT)
  246. const char *fit_uname_cfg; /* configuration node unit name */
  247. void *fit_hdr_os; /* os FIT image header */
  248. const char *fit_uname_os; /* os subimage node unit name */
  249. int fit_noffset_os; /* os subimage node offset */
  250. void *fit_hdr_rd; /* init ramdisk FIT image header */
  251. const char *fit_uname_rd; /* init ramdisk subimage node unit name */
  252. int fit_noffset_rd; /* init ramdisk subimage node offset */
  253. void *fit_hdr_fdt; /* FDT blob FIT image header */
  254. const char *fit_uname_fdt; /* FDT blob subimage node unit name */
  255. int fit_noffset_fdt;/* FDT blob subimage node offset */
  256. #endif
  257. #ifndef USE_HOSTCC
  258. image_info_t os; /* os image info */
  259. ulong ep; /* entry point of OS */
  260. ulong rd_start, rd_end;/* ramdisk start/end */
  261. char *ft_addr; /* flat dev tree address */
  262. ulong ft_len; /* length of flat device tree */
  263. ulong initrd_start;
  264. ulong initrd_end;
  265. ulong cmdline_start;
  266. ulong cmdline_end;
  267. bd_t *kbd;
  268. #endif
  269. int verify; /* getenv("verify")[0] != 'n' */
  270. #define BOOTM_STATE_START (0x00000001)
  271. #define BOOTM_STATE_FINDOS (0x00000002)
  272. #define BOOTM_STATE_FINDOTHER (0x00000004)
  273. #define BOOTM_STATE_LOADOS (0x00000008)
  274. #define BOOTM_STATE_RAMDISK (0x00000010)
  275. #define BOOTM_STATE_FDT (0x00000020)
  276. #define BOOTM_STATE_OS_CMDLINE (0x00000040)
  277. #define BOOTM_STATE_OS_BD_T (0x00000080)
  278. #define BOOTM_STATE_OS_PREP (0x00000100)
  279. #define BOOTM_STATE_OS_FAKE_GO (0x00000200) /* 'Almost' run the OS */
  280. #define BOOTM_STATE_OS_GO (0x00000400)
  281. int state;
  282. #ifdef CONFIG_LMB
  283. struct lmb lmb; /* for memory mgmt */
  284. #endif
  285. } bootm_headers_t;
  286. extern bootm_headers_t images;
  287. /*
  288. * Some systems (for example LWMON) have very short watchdog periods;
  289. * we must make sure to split long operations like memmove() or
  290. * checksum calculations into reasonable chunks.
  291. */
  292. #ifndef CHUNKSZ
  293. #define CHUNKSZ (64 * 1024)
  294. #endif
  295. #ifndef CHUNKSZ_CRC32
  296. #define CHUNKSZ_CRC32 (64 * 1024)
  297. #endif
  298. #ifndef CHUNKSZ_MD5
  299. #define CHUNKSZ_MD5 (64 * 1024)
  300. #endif
  301. #ifndef CHUNKSZ_SHA1
  302. #define CHUNKSZ_SHA1 (64 * 1024)
  303. #endif
  304. #define uimage_to_cpu(x) be32_to_cpu(x)
  305. #define cpu_to_uimage(x) cpu_to_be32(x)
  306. /*
  307. * Translation table for entries of a specific type; used by
  308. * get_table_entry_id() and get_table_entry_name().
  309. */
  310. typedef struct table_entry {
  311. int id;
  312. char *sname; /* short (input) name to find table entry */
  313. char *lname; /* long (output) name to print for messages */
  314. } table_entry_t;
  315. /*
  316. * get_table_entry_id() scans the translation table trying to find an
  317. * entry that matches the given short name. If a matching entry is
  318. * found, it's id is returned to the caller.
  319. */
  320. int get_table_entry_id(const table_entry_t *table,
  321. const char *table_name, const char *name);
  322. /*
  323. * get_table_entry_name() scans the translation table trying to find
  324. * an entry that matches the given id. If a matching entry is found,
  325. * its long name is returned to the caller.
  326. */
  327. char *get_table_entry_name(const table_entry_t *table, char *msg, int id);
  328. const char *genimg_get_os_name(uint8_t os);
  329. const char *genimg_get_arch_name(uint8_t arch);
  330. const char *genimg_get_type_name(uint8_t type);
  331. const char *genimg_get_comp_name(uint8_t comp);
  332. int genimg_get_os_id(const char *name);
  333. int genimg_get_arch_id(const char *name);
  334. int genimg_get_type_id(const char *name);
  335. int genimg_get_comp_id(const char *name);
  336. void genimg_print_size(uint32_t size);
  337. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || \
  338. defined(USE_HOSTCC)
  339. #define IMAGE_ENABLE_TIMESTAMP 1
  340. #else
  341. #define IMAGE_ENABLE_TIMESTAMP 0
  342. #endif
  343. void genimg_print_time(time_t timestamp);
  344. /* What to do with a image load address ('load = <> 'in the FIT) */
  345. enum fit_load_op {
  346. FIT_LOAD_IGNORED, /* Ignore load address */
  347. FIT_LOAD_OPTIONAL, /* Can be provided, but optional */
  348. FIT_LOAD_REQUIRED, /* Must be provided */
  349. };
  350. #ifndef USE_HOSTCC
  351. /* Image format types, returned by _get_format() routine */
  352. #define IMAGE_FORMAT_INVALID 0x00
  353. #define IMAGE_FORMAT_LEGACY 0x01 /* legacy image_header based format */
  354. #define IMAGE_FORMAT_FIT 0x02 /* new, libfdt based format */
  355. int genimg_get_format(const void *img_addr);
  356. int genimg_has_config(bootm_headers_t *images);
  357. ulong genimg_get_image(ulong img_addr);
  358. int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
  359. uint8_t arch, ulong *rd_start, ulong *rd_end);
  360. /**
  361. * fit_image_load() - load an image from a FIT
  362. *
  363. * This deals with all aspects of loading an image from a FIT, including
  364. * selecting the right image based on configuration, verifying it, printing
  365. * out progress messages, checking the type/arch/os and optionally copying it
  366. * to the right load address.
  367. *
  368. * @param images Boot images structure
  369. * @param prop_name Property name to look up (FIT_..._PROP)
  370. * @param addr Address of FIT in memory
  371. * @param fit_unamep On entry this is the requested image name
  372. * (e.g. "kernel@1") or NULL to use the default. On exit
  373. * points to the selected image name
  374. * @param fit_uname_configp On entry this is the requested configuration
  375. * name (e.g. "conf@1") or NULL to use the default. On
  376. * exit points to the selected configuration name.
  377. * @param arch Expected architecture (IH_ARCH_...)
  378. * @param image_type Required image type (IH_TYPE_...). If this is
  379. * IH_TYPE_KERNEL then we allow IH_TYPE_KERNEL_NOLOAD
  380. * also.
  381. * @param bootstage_id ID of starting bootstage to use for progress updates.
  382. * This will be added to the BOOTSTAGE_SUB values when
  383. * calling bootstage_mark()
  384. * @param load_op Decribes what to do with the load address
  385. * @param datap Returns address of loaded image
  386. * @param lenp Returns length of loaded image
  387. */
  388. int fit_image_load(bootm_headers_t *images, const char *prop_name, ulong addr,
  389. const char **fit_unamep, const char **fit_uname_configp,
  390. int arch, int image_type, int bootstage_id,
  391. enum fit_load_op load_op, ulong *datap, ulong *lenp);
  392. /**
  393. * fit_get_node_from_config() - Look up an image a FIT by type
  394. *
  395. * This looks in the selected conf@ node (images->fit_uname_cfg) for a
  396. * particular image type (e.g. "kernel") and then finds the image that is
  397. * referred to.
  398. *
  399. * For example, for something like:
  400. *
  401. * images {
  402. * kernel@1 {
  403. * ...
  404. * };
  405. * };
  406. * configurations {
  407. * conf@1 {
  408. * kernel = "kernel@1";
  409. * };
  410. * };
  411. *
  412. * the function will return the node offset of the kernel@1 node, assuming
  413. * that conf@1 is the chosen configuration.
  414. *
  415. * @param images Boot images structure
  416. * @param prop_name Property name to look up (FIT_..._PROP)
  417. * @param addr Address of FIT in memory
  418. */
  419. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  420. ulong addr);
  421. int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
  422. bootm_headers_t *images,
  423. char **of_flat_tree, ulong *of_size);
  424. void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
  425. int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);
  426. int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
  427. ulong *initrd_start, ulong *initrd_end);
  428. int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end);
  429. #ifdef CONFIG_SYS_BOOT_GET_KBD
  430. int boot_get_kbd(struct lmb *lmb, bd_t **kbd);
  431. #endif /* CONFIG_SYS_BOOT_GET_KBD */
  432. #endif /* !USE_HOSTCC */
  433. /*******************************************************************/
  434. /* Legacy format specific code (prefixed with image_) */
  435. /*******************************************************************/
  436. static inline uint32_t image_get_header_size(void)
  437. {
  438. return (sizeof(image_header_t));
  439. }
  440. #define image_get_hdr_l(f) \
  441. static inline uint32_t image_get_##f(const image_header_t *hdr) \
  442. { \
  443. return uimage_to_cpu(hdr->ih_##f); \
  444. }
  445. image_get_hdr_l(magic) /* image_get_magic */
  446. image_get_hdr_l(hcrc) /* image_get_hcrc */
  447. image_get_hdr_l(time) /* image_get_time */
  448. image_get_hdr_l(size) /* image_get_size */
  449. image_get_hdr_l(load) /* image_get_load */
  450. image_get_hdr_l(ep) /* image_get_ep */
  451. image_get_hdr_l(dcrc) /* image_get_dcrc */
  452. #define image_get_hdr_b(f) \
  453. static inline uint8_t image_get_##f(const image_header_t *hdr) \
  454. { \
  455. return hdr->ih_##f; \
  456. }
  457. image_get_hdr_b(os) /* image_get_os */
  458. image_get_hdr_b(arch) /* image_get_arch */
  459. image_get_hdr_b(type) /* image_get_type */
  460. image_get_hdr_b(comp) /* image_get_comp */
  461. static inline char *image_get_name(const image_header_t *hdr)
  462. {
  463. return (char *)hdr->ih_name;
  464. }
  465. static inline uint32_t image_get_data_size(const image_header_t *hdr)
  466. {
  467. return image_get_size(hdr);
  468. }
  469. /**
  470. * image_get_data - get image payload start address
  471. * @hdr: image header
  472. *
  473. * image_get_data() returns address of the image payload. For single
  474. * component images it is image data start. For multi component
  475. * images it points to the null terminated table of sub-images sizes.
  476. *
  477. * returns:
  478. * image payload data start address
  479. */
  480. static inline ulong image_get_data(const image_header_t *hdr)
  481. {
  482. return ((ulong)hdr + image_get_header_size());
  483. }
  484. static inline uint32_t image_get_image_size(const image_header_t *hdr)
  485. {
  486. return (image_get_size(hdr) + image_get_header_size());
  487. }
  488. static inline ulong image_get_image_end(const image_header_t *hdr)
  489. {
  490. return ((ulong)hdr + image_get_image_size(hdr));
  491. }
  492. #define image_set_hdr_l(f) \
  493. static inline void image_set_##f(image_header_t *hdr, uint32_t val) \
  494. { \
  495. hdr->ih_##f = cpu_to_uimage(val); \
  496. }
  497. image_set_hdr_l(magic) /* image_set_magic */
  498. image_set_hdr_l(hcrc) /* image_set_hcrc */
  499. image_set_hdr_l(time) /* image_set_time */
  500. image_set_hdr_l(size) /* image_set_size */
  501. image_set_hdr_l(load) /* image_set_load */
  502. image_set_hdr_l(ep) /* image_set_ep */
  503. image_set_hdr_l(dcrc) /* image_set_dcrc */
  504. #define image_set_hdr_b(f) \
  505. static inline void image_set_##f(image_header_t *hdr, uint8_t val) \
  506. { \
  507. hdr->ih_##f = val; \
  508. }
  509. image_set_hdr_b(os) /* image_set_os */
  510. image_set_hdr_b(arch) /* image_set_arch */
  511. image_set_hdr_b(type) /* image_set_type */
  512. image_set_hdr_b(comp) /* image_set_comp */
  513. static inline void image_set_name(image_header_t *hdr, const char *name)
  514. {
  515. strncpy(image_get_name(hdr), name, IH_NMLEN);
  516. }
  517. int image_check_hcrc(const image_header_t *hdr);
  518. int image_check_dcrc(const image_header_t *hdr);
  519. #ifndef USE_HOSTCC
  520. ulong getenv_bootm_low(void);
  521. phys_size_t getenv_bootm_size(void);
  522. phys_size_t getenv_bootm_mapsize(void);
  523. void memmove_wd(void *to, void *from, size_t len, ulong chunksz);
  524. #endif
  525. static inline int image_check_magic(const image_header_t *hdr)
  526. {
  527. return (image_get_magic(hdr) == IH_MAGIC);
  528. }
  529. static inline int image_check_type(const image_header_t *hdr, uint8_t type)
  530. {
  531. return (image_get_type(hdr) == type);
  532. }
  533. static inline int image_check_arch(const image_header_t *hdr, uint8_t arch)
  534. {
  535. return (image_get_arch(hdr) == arch);
  536. }
  537. static inline int image_check_os(const image_header_t *hdr, uint8_t os)
  538. {
  539. return (image_get_os(hdr) == os);
  540. }
  541. ulong image_multi_count(const image_header_t *hdr);
  542. void image_multi_getimg(const image_header_t *hdr, ulong idx,
  543. ulong *data, ulong *len);
  544. void image_print_contents(const void *hdr);
  545. #ifndef USE_HOSTCC
  546. static inline int image_check_target_arch(const image_header_t *hdr)
  547. {
  548. #ifndef IH_ARCH_DEFAULT
  549. # error "please define IH_ARCH_DEFAULT in your arch asm/u-boot.h"
  550. #endif
  551. return image_check_arch(hdr, IH_ARCH_DEFAULT);
  552. }
  553. #endif /* USE_HOSTCC */
  554. /**
  555. * Set up properties in the FDT
  556. *
  557. * This sets up properties in the FDT that is to be passed to linux.
  558. *
  559. * @images: Images information
  560. * @blob: FDT to update
  561. * @of_size: Size of the FDT
  562. * @lmb: Points to logical memory block structure
  563. * @return 0 if ok, <0 on failure
  564. */
  565. int image_setup_libfdt(bootm_headers_t *images, void *blob,
  566. int of_size, struct lmb *lmb);
  567. /**
  568. * Set up the FDT to use for booting a kernel
  569. *
  570. * This performs ramdisk setup, sets up the FDT if required, and adds
  571. * paramters to the FDT if libfdt is available.
  572. *
  573. * @param images Images information
  574. * @return 0 if ok, <0 on failure
  575. */
  576. int image_setup_linux(bootm_headers_t *images);
  577. /**
  578. * bootz_setup() - Extract stat and size of a Linux xImage
  579. *
  580. * @image: Address of image
  581. * @start: Returns start address of image
  582. * @end : Returns end address of image
  583. * @return 0 if OK, 1 if the image was not recognised
  584. */
  585. int bootz_setup(ulong image, ulong *start, ulong *end);
  586. /*******************************************************************/
  587. /* New uImage format specific code (prefixed with fit_) */
  588. /*******************************************************************/
  589. #if defined(CONFIG_FIT)
  590. #define FIT_IMAGES_PATH "/images"
  591. #define FIT_CONFS_PATH "/configurations"
  592. /* hash/signature node */
  593. #define FIT_HASH_NODENAME "hash"
  594. #define FIT_ALGO_PROP "algo"
  595. #define FIT_VALUE_PROP "value"
  596. #define FIT_IGNORE_PROP "uboot-ignore"
  597. #define FIT_SIG_NODENAME "signature"
  598. /* image node */
  599. #define FIT_DATA_PROP "data"
  600. #define FIT_TIMESTAMP_PROP "timestamp"
  601. #define FIT_DESC_PROP "description"
  602. #define FIT_ARCH_PROP "arch"
  603. #define FIT_TYPE_PROP "type"
  604. #define FIT_OS_PROP "os"
  605. #define FIT_COMP_PROP "compression"
  606. #define FIT_ENTRY_PROP "entry"
  607. #define FIT_LOAD_PROP "load"
  608. /* configuration node */
  609. #define FIT_KERNEL_PROP "kernel"
  610. #define FIT_RAMDISK_PROP "ramdisk"
  611. #define FIT_FDT_PROP "fdt"
  612. #define FIT_DEFAULT_PROP "default"
  613. #define FIT_MAX_HASH_LEN 20 /* max(crc32_len(4), sha1_len(20)) */
  614. /* cmdline argument format parsing */
  615. int fit_parse_conf(const char *spec, ulong addr_curr,
  616. ulong *addr, const char **conf_name);
  617. int fit_parse_subimage(const char *spec, ulong addr_curr,
  618. ulong *addr, const char **image_name);
  619. void fit_print_contents(const void *fit);
  620. void fit_image_print(const void *fit, int noffset, const char *p);
  621. /**
  622. * fit_get_end - get FIT image size
  623. * @fit: pointer to the FIT format image header
  624. *
  625. * returns:
  626. * size of the FIT image (blob) in memory
  627. */
  628. static inline ulong fit_get_size(const void *fit)
  629. {
  630. return fdt_totalsize(fit);
  631. }
  632. /**
  633. * fit_get_end - get FIT image end
  634. * @fit: pointer to the FIT format image header
  635. *
  636. * returns:
  637. * end address of the FIT image (blob) in memory
  638. */
  639. static inline ulong fit_get_end(const void *fit)
  640. {
  641. return (ulong)fit + fdt_totalsize(fit);
  642. }
  643. /**
  644. * fit_get_name - get FIT node name
  645. * @fit: pointer to the FIT format image header
  646. *
  647. * returns:
  648. * NULL, on error
  649. * pointer to node name, on success
  650. */
  651. static inline const char *fit_get_name(const void *fit_hdr,
  652. int noffset, int *len)
  653. {
  654. return fdt_get_name(fit_hdr, noffset, len);
  655. }
  656. int fit_get_desc(const void *fit, int noffset, char **desc);
  657. int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp);
  658. int fit_image_get_node(const void *fit, const char *image_uname);
  659. int fit_image_get_os(const void *fit, int noffset, uint8_t *os);
  660. int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch);
  661. int fit_image_get_type(const void *fit, int noffset, uint8_t *type);
  662. int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp);
  663. int fit_image_get_load(const void *fit, int noffset, ulong *load);
  664. int fit_image_get_entry(const void *fit, int noffset, ulong *entry);
  665. int fit_image_get_data(const void *fit, int noffset,
  666. const void **data, size_t *size);
  667. int fit_image_hash_get_algo(const void *fit, int noffset, char **algo);
  668. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  669. int *value_len);
  670. int fit_set_timestamp(void *fit, int noffset, time_t timestamp);
  671. /**
  672. * fit_add_verification_data() - add verification data to FIT image nodes
  673. *
  674. * @keydir: Directory containing keys
  675. * @kwydest: FDT blob to write public key information to
  676. * @fit: Pointer to the FIT format image header
  677. * @comment: Comment to add to signature nodes
  678. * @require_keys: Mark all keys as 'required'
  679. *
  680. * Adds hash values for all component images in the FIT blob.
  681. * Hashes are calculated for all component images which have hash subnodes
  682. * with algorithm property set to one of the supported hash algorithms.
  683. *
  684. * Also add signatures if signature nodes are present.
  685. *
  686. * returns
  687. * 0, on success
  688. * libfdt error code, on failure
  689. */
  690. int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
  691. const char *comment, int require_keys);
  692. int fit_image_verify(const void *fit, int noffset);
  693. int fit_config_verify(const void *fit, int conf_noffset);
  694. int fit_all_image_verify(const void *fit);
  695. int fit_image_check_os(const void *fit, int noffset, uint8_t os);
  696. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch);
  697. int fit_image_check_type(const void *fit, int noffset, uint8_t type);
  698. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp);
  699. int fit_check_format(const void *fit);
  700. int fit_conf_find_compat(const void *fit, const void *fdt);
  701. int fit_conf_get_node(const void *fit, const char *conf_uname);
  702. /**
  703. * fit_conf_get_prop_node() - Get node refered to by a configuration
  704. * @fit: FIT to check
  705. * @noffset: Offset of conf@xxx node to check
  706. * @prop_name: Property to read from the conf node
  707. *
  708. * The conf@ nodes contain references to other nodes, using properties
  709. * like 'kernel = "kernel@1"'. Given such a property name (e.g. "kernel"),
  710. * return the offset of the node referred to (e.g. offset of node
  711. * "/images/kernel@1".
  712. */
  713. int fit_conf_get_prop_node(const void *fit, int noffset,
  714. const char *prop_name);
  715. void fit_conf_print(const void *fit, int noffset, const char *p);
  716. int fit_check_ramdisk(const void *fit, int os_noffset,
  717. uint8_t arch, int verify);
  718. int calculate_hash(const void *data, int data_len, const char *algo,
  719. uint8_t *value, int *value_len);
  720. /*
  721. * At present we only support signing on the host, and verification on the
  722. * device
  723. */
  724. #if defined(CONFIG_FIT_SIGNATURE)
  725. # ifdef USE_HOSTCC
  726. # define IMAGE_ENABLE_SIGN 1
  727. # define IMAGE_ENABLE_VERIFY 0
  728. #else
  729. # define IMAGE_ENABLE_SIGN 0
  730. # define IMAGE_ENABLE_VERIFY 1
  731. # endif
  732. #else
  733. # define IMAGE_ENABLE_SIGN 0
  734. # define IMAGE_ENABLE_VERIFY 0
  735. #endif
  736. #ifdef USE_HOSTCC
  737. # define gd_fdt_blob() NULL
  738. #else
  739. # define gd_fdt_blob() (gd->fdt_blob)
  740. #endif
  741. #ifdef CONFIG_FIT_BEST_MATCH
  742. #define IMAGE_ENABLE_BEST_MATCH 1
  743. #else
  744. #define IMAGE_ENABLE_BEST_MATCH 0
  745. #endif
  746. /* Information passed to the signing routines */
  747. struct image_sign_info {
  748. const char *keydir; /* Directory conaining keys */
  749. const char *keyname; /* Name of key to use */
  750. void *fit; /* Pointer to FIT blob */
  751. int node_offset; /* Offset of signature node */
  752. struct image_sig_algo *algo; /* Algorithm information */
  753. const void *fdt_blob; /* FDT containing public keys */
  754. int required_keynode; /* Node offset of key to use: -1=any */
  755. const char *require_keys; /* Value for 'required' property */
  756. };
  757. /* A part of an image, used for hashing */
  758. struct image_region {
  759. const void *data;
  760. int size;
  761. };
  762. struct image_sig_algo {
  763. const char *name; /* Name of algorithm */
  764. /**
  765. * sign() - calculate and return signature for given input data
  766. *
  767. * @info: Specifies key and FIT information
  768. * @data: Pointer to the input data
  769. * @data_len: Data length
  770. * @sigp: Set to an allocated buffer holding the signature
  771. * @sig_len: Set to length of the calculated hash
  772. *
  773. * This computes input data signature according to selected algorithm.
  774. * Resulting signature value is placed in an allocated buffer, the
  775. * pointer is returned as *sigp. The length of the calculated
  776. * signature is returned via the sig_len pointer argument. The caller
  777. * should free *sigp.
  778. *
  779. * @return: 0, on success, -ve on error
  780. */
  781. int (*sign)(struct image_sign_info *info,
  782. const struct image_region region[],
  783. int region_count, uint8_t **sigp, uint *sig_len);
  784. /**
  785. * add_verify_data() - Add verification information to FDT
  786. *
  787. * Add public key information to the FDT node, suitable for
  788. * verification at run-time. The information added depends on the
  789. * algorithm being used.
  790. *
  791. * @info: Specifies key and FIT information
  792. * @keydest: Destination FDT blob for public key data
  793. * @return: 0, on success, -ve on error
  794. */
  795. int (*add_verify_data)(struct image_sign_info *info, void *keydest);
  796. /**
  797. * verify() - Verify a signature against some data
  798. *
  799. * @info: Specifies key and FIT information
  800. * @data: Pointer to the input data
  801. * @data_len: Data length
  802. * @sig: Signature
  803. * @sig_len: Number of bytes in signature
  804. * @return 0 if verified, -ve on error
  805. */
  806. int (*verify)(struct image_sign_info *info,
  807. const struct image_region region[], int region_count,
  808. uint8_t *sig, uint sig_len);
  809. };
  810. /**
  811. * image_get_sig_algo() - Look up a signature algortihm
  812. *
  813. * @param name Name of algorithm
  814. * @return pointer to algorithm information, or NULL if not found
  815. */
  816. struct image_sig_algo *image_get_sig_algo(const char *name);
  817. /**
  818. * fit_image_verify_required_sigs() - Verify signatures marked as 'required'
  819. *
  820. * @fit: FIT to check
  821. * @image_noffset: Offset of image node to check
  822. * @data: Image data to check
  823. * @size: Size of image data
  824. * @sig_blob: FDT containing public keys
  825. * @no_sigsp: Returns 1 if no signatures were required, and
  826. * therefore nothing was checked. The caller may wish
  827. * to fall back to other mechanisms, or refuse to
  828. * boot.
  829. * @return 0 if all verified ok, <0 on error
  830. */
  831. int fit_image_verify_required_sigs(const void *fit, int image_noffset,
  832. const char *data, size_t size, const void *sig_blob,
  833. int *no_sigsp);
  834. /**
  835. * fit_image_check_sig() - Check a single image signature node
  836. *
  837. * @fit: FIT to check
  838. * @noffset: Offset of signature node to check
  839. * @data: Image data to check
  840. * @size: Size of image data
  841. * @required_keynode: Offset in the control FDT of the required key node,
  842. * if any. If this is given, then the image wil not
  843. * pass verification unless that key is used. If this is
  844. * -1 then any signature will do.
  845. * @err_msgp: In the event of an error, this will be pointed to a
  846. * help error string to display to the user.
  847. * @return 0 if all verified ok, <0 on error
  848. */
  849. int fit_image_check_sig(const void *fit, int noffset, const void *data,
  850. size_t size, int required_keynode, char **err_msgp);
  851. /**
  852. * fit_region_make_list() - Make a list of regions to hash
  853. *
  854. * Given a list of FIT regions (offset, size) provided by libfdt, create
  855. * a list of regions (void *, size) for use by the signature creationg
  856. * and verification code.
  857. *
  858. * @fit: FIT image to process
  859. * @fdt_regions: Regions as returned by libfdt
  860. * @count: Number of regions returned by libfdt
  861. * @region: Place to put list of regions (NULL to allocate it)
  862. * @return pointer to list of regions, or NULL if out of memory
  863. */
  864. struct image_region *fit_region_make_list(const void *fit,
  865. struct fdt_region *fdt_regions, int count,
  866. struct image_region *region);
  867. static inline int fit_image_check_target_arch(const void *fdt, int node)
  868. {
  869. return fit_image_check_arch(fdt, node, IH_ARCH_DEFAULT);
  870. }
  871. #ifdef CONFIG_FIT_VERBOSE
  872. #define fit_unsupported(msg) printf("! %s:%d " \
  873. "FIT images not supported for '%s'\n", \
  874. __FILE__, __LINE__, (msg))
  875. #define fit_unsupported_reset(msg) printf("! %s:%d " \
  876. "FIT images not supported for '%s' " \
  877. "- must reset board to recover!\n", \
  878. __FILE__, __LINE__, (msg))
  879. #else
  880. #define fit_unsupported(msg)
  881. #define fit_unsupported_reset(msg)
  882. #endif /* CONFIG_FIT_VERBOSE */
  883. #endif /* CONFIG_FIT */
  884. #endif /* __IMAGE_H__ */