vblock.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 verified boot block, used to sign a read-write
  6. # section of the image.
  7. from collections import OrderedDict
  8. import os
  9. from binman.entry import Entry, EntryArg
  10. from dtoc import fdt_util
  11. from patman import tools
  12. class Entry_vblock(Entry):
  13. """An entry which contains a Chromium OS verified boot block
  14. Properties / Entry arguments:
  15. - content: List of phandles to entries to sign
  16. - keydir: Directory containing the public keys to use
  17. - keyblock: Name of the key file to use (inside keydir)
  18. - signprivate: Name of provide key file to use (inside keydir)
  19. - version: Version number of the vblock (typically 1)
  20. - kernelkey: Name of the kernel key to use (inside keydir)
  21. - preamble-flags: Value of the vboot preamble flags (typically 0)
  22. Output files:
  23. - input.<unique_name> - input file passed to futility
  24. - vblock.<unique_name> - output file generated by futility (which is
  25. used as the entry contents)
  26. Chromium OS signs the read-write firmware and kernel, writing the signature
  27. in this block. This allows U-Boot to verify that the next firmware stage
  28. and kernel are genuine.
  29. """
  30. def __init__(self, section, etype, node):
  31. Entry.__init__(self, section, etype, node)
  32. self.content = fdt_util.GetPhandleList(self._node, 'content')
  33. if not self.content:
  34. self.Raise("Vblock must have a 'content' property")
  35. (self.keydir, self.keyblock, self.signprivate, self.version,
  36. self.kernelkey, self.preamble_flags) = self.GetEntryArgsOrProps([
  37. EntryArg('keydir', str),
  38. EntryArg('keyblock', str),
  39. EntryArg('signprivate', str),
  40. EntryArg('version', int),
  41. EntryArg('kernelkey', str),
  42. EntryArg('preamble-flags', int)])
  43. def ObtainContents(self):
  44. # Join up the data files to be signed
  45. input_data = b''
  46. for entry_phandle in self.content:
  47. data = self.section.GetContentsByPhandle(entry_phandle, self)
  48. if data is None:
  49. # Data not available yet
  50. return False
  51. input_data += data
  52. uniq = self.GetUniqueName()
  53. output_fname = tools.GetOutputFilename('vblock.%s' % uniq)
  54. input_fname = tools.GetOutputFilename('input.%s' % uniq)
  55. tools.WriteFile(input_fname, input_data)
  56. prefix = self.keydir + '/'
  57. args = [
  58. 'vbutil_firmware',
  59. '--vblock', output_fname,
  60. '--keyblock', prefix + self.keyblock,
  61. '--signprivate', prefix + self.signprivate,
  62. '--version', '%d' % self.version,
  63. '--fv', input_fname,
  64. '--kernelkey', prefix + self.kernelkey,
  65. '--flags', '%d' % self.preamble_flags,
  66. ]
  67. #out.Notice("Sign '%s' into %s" % (', '.join(self.value), self.label))
  68. stdout = tools.Run('futility', *args)
  69. self.SetContents(tools.ReadFile(output_fname))
  70. return True