image.h 32 KB

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