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