gbb.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2018 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Support for a Chromium OS Google Binary Block, used to record read-only
  6. # information mostly used by firmware.
  7. from collections import OrderedDict
  8. from patman import command
  9. from binman.entry import Entry, EntryArg
  10. from dtoc import fdt_util
  11. from patman import tools
  12. # Build GBB flags.
  13. # (src/platform/vboot_reference/firmware/include/gbb_header.h)
  14. gbb_flag_properties = {
  15. 'dev-screen-short-delay': 0x1,
  16. 'load-option-roms': 0x2,
  17. 'enable-alternate-os': 0x4,
  18. 'force-dev-switch-on': 0x8,
  19. 'force-dev-boot-usb': 0x10,
  20. 'disable-fw-rollback-check': 0x20,
  21. 'enter-triggers-tonorm': 0x40,
  22. 'force-dev-boot-legacy': 0x80,
  23. 'faft-key-override': 0x100,
  24. 'disable-ec-software-sync': 0x200,
  25. 'default-dev-boot-legacy': 0x400,
  26. 'disable-pd-software-sync': 0x800,
  27. 'disable-lid-shutdown': 0x1000,
  28. 'force-dev-boot-fastboot-full-cap': 0x2000,
  29. 'enable-serial': 0x4000,
  30. 'disable-dwmp': 0x8000,
  31. }
  32. class Entry_gbb(Entry):
  33. """An entry which contains a Chromium OS Google Binary Block
  34. Properties / Entry arguments:
  35. - hardware-id: Hardware ID to use for this build (a string)
  36. - keydir: Directory containing the public keys to use
  37. - bmpblk: Filename containing images used by recovery
  38. Chromium OS uses a GBB to store various pieces of information, in particular
  39. the root and recovery keys that are used to verify the boot process. Some
  40. more details are here:
  41. https://www.chromium.org/chromium-os/firmware-porting-guide/2-concepts
  42. but note that the page dates from 2013 so is quite out of date. See
  43. README.chromium for how to obtain the required keys and tools.
  44. """
  45. def __init__(self, section, etype, node):
  46. super().__init__(section, etype, node)
  47. self.hardware_id, self.keydir, self.bmpblk = self.GetEntryArgsOrProps(
  48. [EntryArg('hardware-id', str),
  49. EntryArg('keydir', str),
  50. EntryArg('bmpblk', str)])
  51. # Read in the GBB flags from the config
  52. self.gbb_flags = 0
  53. flags_node = node.FindNode('flags')
  54. if flags_node:
  55. for flag, value in gbb_flag_properties.items():
  56. if fdt_util.GetBool(flags_node, flag):
  57. self.gbb_flags |= value
  58. def ObtainContents(self):
  59. gbb = 'gbb.bin'
  60. fname = tools.GetOutputFilename(gbb)
  61. if not self.size:
  62. self.Raise('GBB must have a fixed size')
  63. gbb_size = self.size
  64. bmpfv_size = gbb_size - 0x2180
  65. if bmpfv_size < 0:
  66. self.Raise('GBB is too small (minimum 0x2180 bytes)')
  67. sizes = [0x100, 0x1000, bmpfv_size, 0x1000]
  68. sizes = ['%#x' % size for size in sizes]
  69. keydir = tools.GetInputFilename(self.keydir)
  70. gbb_set_command = [
  71. 'gbb_utility', '-s',
  72. '--hwid=%s' % self.hardware_id,
  73. '--rootkey=%s/root_key.vbpubk' % keydir,
  74. '--recoverykey=%s/recovery_key.vbpubk' % keydir,
  75. '--flags=%d' % self.gbb_flags,
  76. '--bmpfv=%s' % tools.GetInputFilename(self.bmpblk),
  77. fname]
  78. tools.Run('futility', 'gbb_utility', '-c', ','.join(sizes), fname)
  79. tools.Run('futility', *gbb_set_command)
  80. self.SetContents(tools.ReadFile(fname))
  81. return True