bootgen.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (C) 2023 Weidmüller Interface GmbH & Co. KG
  3. # Lukas Funke <lukas.funke@weidmueller.com>
  4. #
  5. """Bintool implementation for bootgen
  6. bootgen allows creating bootable SPL for Zynq(MP)
  7. Documentation is available via:
  8. https://www.xilinx.com/support/documents/sw_manuals/xilinx2022_1/ug1283-bootgen-user-guide.pdf
  9. Source code is available at:
  10. https://github.com/Xilinx/bootgen
  11. """
  12. from binman import bintool
  13. from u_boot_pylib import tools
  14. # pylint: disable=C0103
  15. class Bintoolbootgen(bintool.Bintool):
  16. """Generate bootable fsbl image for zynq/zynqmp
  17. This bintools supports running Xilinx "bootgen" in order
  18. to generate a bootable, authenticated image form an SPL.
  19. """
  20. def __init__(self, name):
  21. super().__init__(name, 'Xilinx Bootgen',
  22. version_regex=r'^\*\*\*\*\*\* *Xilinx Bootgen *(.*)',
  23. version_args='-help')
  24. # pylint: disable=R0913
  25. def sign(self, arch, spl_elf_fname, pmufw_elf_fname,
  26. psk_fname, ssk_fname, fsbl_config, auth_params, keysrc_enc,
  27. output_fname):
  28. """Sign SPL elf file and bundle it with PMU firmware into an image
  29. The method bundels the SPL together with a 'Platform Management Unit'
  30. (PMU)[1] firmware into a single bootable image. The image in turn is
  31. signed with the provided 'secondary secret key' (ssk), which in turn is
  32. signed with the 'primary secret key' (psk). In order to verify the
  33. authenticity of the ppk, it's hash has to be fused into the device
  34. itself.
  35. In Xilinx terms the SPL is usually called 'FSBL'
  36. (First Stage Boot Loader). The jobs of the SPL and the FSBL are mostly
  37. the same: load bitstream, bootstrap u-boot.
  38. Args:
  39. arch (str): Xilinx SoC architecture. Currently only 'zynqmp' is
  40. supported.
  41. spl_elf_fname (str): Filename of SPL ELF file. The filename must end
  42. with '.elf' in order for bootgen to recognized it as an ELF
  43. file. Otherwise the start address field is missinterpreted.
  44. pmufw_elf_fname (str): Filename PMU ELF firmware.
  45. psk_fname (str): Filename of the primary secret key (psk). The psk
  46. is a .pem file which holds the RSA private key used for signing
  47. the secondary secret key.
  48. ssk_fname (str): Filename of the secondary secret key. The ssk
  49. is a .pem file which holds the RSA private key used for signing
  50. the actual boot firmware.
  51. fsbl_config (str): FSBL config options. A string list of fsbl config
  52. options. Valid values according to [2] are:
  53. "bh_auth_enable": Boot Header Authentication Enable: RSA
  54. authentication of the bootimage is done
  55. excluding the verification of PPK hash and SPK ID. This is
  56. useful for debugging before bricking a device.
  57. "auth_only": Boot image is only RSA signed. FSBL should not be
  58. decrypted. See the
  59. Zynq UltraScale+ Device Technical Reference Manual (UG1085)
  60. for more information.
  61. There are more options which relate to PUF (physical unclonable
  62. functions). Please refer to Xilinx manuals for further info.
  63. auth_params (str): Authentication parameter. A semicolon separated
  64. list of authentication parameters. Valid values according to [3]
  65. are:
  66. "ppk_select=<0|1>" - Select which ppk to use
  67. "spk_id=<32-bit spk id>" - Specifies which SPK can be
  68. used or revoked, default is 0x0
  69. "spk_select=<spk-efuse/user-efuse>" - To differentiate spk and
  70. user efuses.
  71. "auth_header" - To authenticate headers when no partition
  72. is authenticated.
  73. keysrc_enc (str): This specifies the Key source for encryption.
  74. Valid values according to [3] are:
  75. "bbram_red_key" - RED key stored in BBRAM
  76. "efuse_red_key" - RED key stored in eFUSE
  77. "efuse_gry_key" - Grey (Obfuscated) Key stored in eFUSE.
  78. "bh_gry_key" - Grey (Obfuscated) Key stored in boot header
  79. "bh_blk_key" - Black Key stored in boot header
  80. "efuse_blk_key" - Black Key stored in eFUSE
  81. "kup_key" - User Key
  82. output_fname (str): Filename where bootgen should write the result
  83. Returns:
  84. str: Bootgen output from stdout
  85. [1] https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18841724/PMU+Firmware
  86. [2] https://docs.xilinx.com/r/en-US/ug1283-bootgen-user-guide/fsbl_config
  87. [3] https://docs.xilinx.com/r/en-US/ug1283-bootgen-user-guide/auth_params
  88. [4] https://docs.xilinx.com/r/en-US/ug1283-bootgen-user-guide/keysrc_encryption
  89. """
  90. _fsbl_config = f"[fsbl_config] {fsbl_config}" if fsbl_config else ""
  91. _auth_params = f"[auth_params] {auth_params}" if auth_params else ""
  92. _keysrc_enc = f"[keysrc_encryption] {keysrc_enc}" if keysrc_enc else ""
  93. bif_template = f"""u_boot_spl_aes_rsa: {{
  94. [pskfile] {psk_fname}
  95. [sskfile] {ssk_fname}
  96. {_keysrc_enc}
  97. {_fsbl_config}
  98. {_auth_params}
  99. [ bootloader,
  100. authentication = rsa,
  101. destination_cpu=a53-0] {spl_elf_fname}
  102. [pmufw_image] {pmufw_elf_fname}
  103. }}"""
  104. args = ["-arch", arch]
  105. bif_fname = tools.get_output_filename('bootgen-in.sign.bif')
  106. tools.write_file(bif_fname, bif_template, False)
  107. args += ["-image", bif_fname, '-w', '-o', output_fname]
  108. return self.run_cmd(*args)
  109. def fetch(self, method):
  110. """Fetch bootgen from git"""
  111. if method != bintool.FETCH_BUILD:
  112. return None
  113. result = self.build_from_git(
  114. 'https://github.com/Xilinx/bootgen',
  115. 'all',
  116. 'bootgen')
  117. return result