Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Rhyland Klein | c2df3ca | 2014-01-06 15:15:34 -0500 | [diff] [blame] | 5 | """This module builds a firmware image. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 6 | |
| 7 | This modules uses a few rudimentary other libraries for its activity. |
| 8 | |
| 9 | Here are the names we give to the various files we deal with. It is important |
| 10 | to keep these consistent! |
| 11 | |
| 12 | uboot u-boot.bin (with no device tree) |
| 13 | fdt the fdt blob |
| 14 | bct the BCT file |
| 15 | bootstub uboot + fdt |
| 16 | signed (uboot + fdt + bct) signed blob |
| 17 | """ |
| 18 | |
Simon Glass | ceff3ff | 2012-04-04 11:23:45 -0700 | [diff] [blame] | 19 | import glob |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 20 | import os |
| 21 | import re |
| 22 | |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 23 | from fdt import Fdt |
| 24 | from pack_firmware import PackFirmware |
| 25 | import shutil |
Simon Glass | 7c2d557 | 2011-11-15 14:47:08 -0800 | [diff] [blame] | 26 | import struct |
Patrick Georgi | cebb5a2 | 2016-08-15 19:07:02 +0200 | [diff] [blame] | 27 | import fmap |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 28 | from tools import CmdError |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 29 | |
Simon Glass | 4a887b1 | 2012-10-23 16:29:03 -0700 | [diff] [blame] | 30 | # Build GBB flags. |
| 31 | # (src/platform/vboot_reference/firmware/include/gbb_header.h) |
| 32 | gbb_flag_properties = { |
| 33 | 'dev-screen-short-delay': 0x00000001, |
| 34 | 'load-option-roms': 0x00000002, |
| 35 | 'enable-alternate-os': 0x00000004, |
| 36 | 'force-dev-switch-on': 0x00000008, |
| 37 | 'force-dev-boot-usb': 0x00000010, |
| 38 | 'disable-fw-rollback-check': 0x00000020, |
| 39 | 'enter-triggers-tonorm': 0x00000040, |
| 40 | 'force-dev-boot-legacy': 0x00000080, |
Shawn Nematbakhsh | 07c1988 | 2014-08-19 10:21:59 -0700 | [diff] [blame] | 41 | 'faft-key-overide': 0x00000100, |
| 42 | 'disable-ec-software-sync': 0x00000200, |
| 43 | 'default-dev-boot-legacy': 0x00000400, |
| 44 | 'disable-pd-software-sync': 0x00000800, |
Furquan Shaikh | d4eac3b | 2015-05-15 18:05:09 -0700 | [diff] [blame] | 45 | 'force-dev-boot-fastboot-full-cap': 0x00002000, |
Mary Ruthven | a759c32 | 2015-11-16 08:23:26 -0800 | [diff] [blame] | 46 | 'enable-serial': 0x00004000, |
Simon Glass | 4a887b1 | 2012-10-23 16:29:03 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Simon Glass | 49b026b | 2013-04-26 16:38:42 -0700 | [diff] [blame] | 49 | # Maps board name to Exynos product number |
| 50 | type_to_model = { |
| 51 | 'peach' : '5420', |
| 52 | 'daisy' : '5250' |
| 53 | } |
| 54 | |
Simon Glass | 5076a7f | 2012-10-23 16:31:54 -0700 | [diff] [blame] | 55 | def ListGoogleBinaryBlockFlags(): |
| 56 | """Print out a list of GBB flags.""" |
| 57 | print ' %-30s %s' % ('Available GBB flags:', 'Hex') |
| 58 | for name, value in gbb_flag_properties.iteritems(): |
| 59 | print ' %-30s %02x' % (name, value) |
| 60 | |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 61 | class Bundle: |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 62 | """This class encapsulates the entire bundle firmware logic. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 63 | |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 64 | Sequence of events: |
| 65 | bundle = Bundle(tools.Tools(), cros_output.Output()) |
| 66 | bundle.SetDirs(...) |
| 67 | bundle.SetFiles(...) |
| 68 | bundle.SetOptions(...) |
| 69 | bundle.SelectFdt(fdt.Fdt('filename.dtb') |
Simon Glass | a4934b7 | 2012-05-09 13:35:02 -0700 | [diff] [blame] | 70 | .. can call bundle.AddConfigList(), AddEnableList() if required |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 71 | bundle.Start(...) |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 72 | |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 73 | Public properties: |
| 74 | fdt: The fdt object that we use for building our image. This wil be the |
| 75 | one specified by the user, except that we might add config options |
| 76 | to it. This is set up by SelectFdt() which must be called before |
| 77 | bundling starts. |
| 78 | uboot_fname: Full filename of the U-Boot binary we use. |
| 79 | bct_fname: Full filename of the BCT file we use. |
Simon Glass | 559b661 | 2012-05-23 13:28:45 -0700 | [diff] [blame] | 80 | spl_source: Source device to load U-Boot from, in SPL: |
| 81 | straps: Select device according to CPU strap pins |
| 82 | spi: Boot from SPI |
| 83 | emmc: Boot from eMMC |
Simon Glass | 23988ae | 2012-03-23 16:55:22 -0700 | [diff] [blame] | 84 | |
| 85 | Private attributes: |
| 86 | _small: True to create a 'small' signed U-Boot, False to produce a |
| 87 | full image. The small U-Boot is enough to boot but will not have |
| 88 | access to GBB, RW U-Boot, etc. |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 89 | """ |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 90 | |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 91 | def __init__(self, tools, output): |
| 92 | """Set up a new Bundle object. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 93 | |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 94 | Args: |
| 95 | tools: A tools.Tools object to use for external tools. |
| 96 | output: A cros_output.Output object to use for program output. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 97 | """ |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 98 | self._tools = tools |
| 99 | self._out = output |
| 100 | |
| 101 | # Set up the things we need to know in order to operate. |
Rhyland Klein | c2df3ca | 2014-01-06 15:15:34 -0500 | [diff] [blame] | 102 | self._board = None # Board name, e.g. nyan. |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 103 | self._fdt_fname = None # Filename of our FDT. |
Simon Glass | 00d027e | 2013-07-20 14:51:12 -0600 | [diff] [blame] | 104 | self._force_efs = None |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 105 | self._gbb_flags = None |
| 106 | self._keydir = None |
| 107 | self._small = False |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 108 | self.bct_fname = None # Filename of our BCT file. |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 109 | self.blobs = {} # Table of (type, filename) of arbitrary blobs |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 110 | self.coreboot_elf = None |
Stefan Reinauer | 8d79d36 | 2011-08-16 14:20:43 -0700 | [diff] [blame] | 111 | self.coreboot_fname = None # Filename of our coreboot binary. |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 112 | self.ecro_fname = None # Filename of EC read-only file |
| 113 | self.ecrw_fname = None # Filename of EC file |
Randall Spangler | 7307da9 | 2014-07-18 12:47:34 -0700 | [diff] [blame] | 114 | self.pdrw_fname = None # Filename of PD file |
Simon Glass | 7e19922 | 2012-03-13 15:51:18 -0700 | [diff] [blame] | 115 | self.exynos_bl1 = None # Filename of Exynos BL1 (pre-boot) |
| 116 | self.exynos_bl2 = None # Filename of Exynos BL2 (SPL) |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 117 | self.fdt = None # Our Fdt object. |
| 118 | self.kernel_fname = None |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 119 | self.seabios_fname = None # Filename of our SeaBIOS payload. |
Simon Glass | 0726795 | 2012-06-08 12:45:13 -0700 | [diff] [blame] | 120 | self.skeleton_fname = None # Filename of Coreboot skeleton file |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 121 | self.uboot_fname = None # Filename of our U-Boot binary. |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 122 | |
| 123 | def SetDirs(self, keydir): |
| 124 | """Set up directories required for Bundle. |
| 125 | |
| 126 | Args: |
| 127 | keydir: Directory containing keys to use for signing firmware. |
| 128 | """ |
| 129 | self._keydir = keydir |
| 130 | |
Patrick Georgi | 4803057 | 2016-09-09 22:55:46 +0200 | [diff] [blame] | 131 | def SetFiles(self, board, bct, uboot=None, coreboot=None, |
Simon Glass | a10282a | 2013-01-08 17:06:41 -0800 | [diff] [blame] | 132 | coreboot_elf=None, |
Simon Glass | 942bedb | 2016-05-14 16:19:59 -0600 | [diff] [blame] | 133 | seabios=None, exynos_bl1=None, exynos_bl2=None, |
Randall Spangler | 7307da9 | 2014-07-18 12:47:34 -0700 | [diff] [blame] | 134 | skeleton=None, ecrw=None, ecro=None, pdrw=None, |
Patrick Georgi | 4803057 | 2016-09-09 22:55:46 +0200 | [diff] [blame] | 135 | kernel=None, blobs=None, cbfs_files=None, |
Aaron Durbin | 95ef60c | 2016-01-06 09:17:21 -0600 | [diff] [blame] | 136 | rocbfs_files=None): |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 137 | """Set up files required for Bundle. |
| 138 | |
| 139 | Args: |
Rhyland Klein | c2df3ca | 2014-01-06 15:15:34 -0500 | [diff] [blame] | 140 | board: The name of the board to target (e.g. nyan). |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 141 | uboot: The filename of the u-boot.bin image to use. |
| 142 | bct: The filename of the binary BCT file to use. |
Simon Glass | a10282a | 2013-01-08 17:06:41 -0800 | [diff] [blame] | 143 | coreboot: The filename of the coreboot image to use (on x86). |
| 144 | coreboot_elf: If not none, the ELF file to add as a Coreboot payload. |
Vincent Palatin | f728677 | 2011-10-12 14:31:53 -0700 | [diff] [blame] | 145 | seabios: The filename of the SeaBIOS payload to use if any. |
Simon Glass | 0726795 | 2012-06-08 12:45:13 -0700 | [diff] [blame] | 146 | exynos_bl1: The filename of the exynos BL1 file |
| 147 | exynos_bl2: The filename of the exynos BL2 file (U-Boot spl) |
| 148 | skeleton: The filename of the coreboot skeleton file. |
Simon Glass | be0bc00 | 2012-08-16 12:50:48 -0700 | [diff] [blame] | 149 | ecrw: The filename of the EC (Embedded Controller) read-write file. |
| 150 | ecro: The filename of the EC (Embedded Controller) read-only file. |
Randall Spangler | 7307da9 | 2014-07-18 12:47:34 -0700 | [diff] [blame] | 151 | pdrw: The filename of the PD (PD embedded controller) read-write file. |
Simon Glass | de9c807 | 2012-07-02 22:29:02 -0700 | [diff] [blame] | 152 | kernel: The filename of the kernel file if any. |
Che-Liang Chiou | 3bc344c | 2013-02-21 15:18:03 -0800 | [diff] [blame] | 153 | blobs: List of (type, filename) of arbitrary blobs. |
Aaron Durbin | 95ef60c | 2016-01-06 09:17:21 -0600 | [diff] [blame] | 154 | cbfs_files: Root directory of files to be stored in RO and RW CBFS |
| 155 | rocbfs_files: Root directory of files to be stored in RO CBFS |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 156 | """ |
| 157 | self._board = board |
| 158 | self.uboot_fname = uboot |
| 159 | self.bct_fname = bct |
Stefan Reinauer | 8d79d36 | 2011-08-16 14:20:43 -0700 | [diff] [blame] | 160 | self.coreboot_fname = coreboot |
Simon Glass | a10282a | 2013-01-08 17:06:41 -0800 | [diff] [blame] | 161 | self.coreboot_elf = coreboot_elf |
Vincent Palatin | f728677 | 2011-10-12 14:31:53 -0700 | [diff] [blame] | 162 | self.seabios_fname = seabios |
Simon Glass | 7e19922 | 2012-03-13 15:51:18 -0700 | [diff] [blame] | 163 | self.exynos_bl1 = exynos_bl1 |
| 164 | self.exynos_bl2 = exynos_bl2 |
Simon Glass | 0726795 | 2012-06-08 12:45:13 -0700 | [diff] [blame] | 165 | self.skeleton_fname = skeleton |
Simon Glass | be0bc00 | 2012-08-16 12:50:48 -0700 | [diff] [blame] | 166 | self.ecrw_fname = ecrw |
| 167 | self.ecro_fname = ecro |
Randall Spangler | 7307da9 | 2014-07-18 12:47:34 -0700 | [diff] [blame] | 168 | self.pdrw_fname = pdrw |
Simon Glass | de9c807 | 2012-07-02 22:29:02 -0700 | [diff] [blame] | 169 | self.kernel_fname = kernel |
Che-Liang Chiou | 3bc344c | 2013-02-21 15:18:03 -0800 | [diff] [blame] | 170 | self.blobs = dict(blobs or ()) |
Daisuke Nojiri | 6966289 | 2015-09-25 15:24:04 -0700 | [diff] [blame] | 171 | self.cbfs_files = cbfs_files |
Aaron Durbin | 95ef60c | 2016-01-06 09:17:21 -0600 | [diff] [blame] | 172 | self.rocbfs_files = rocbfs_files |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 173 | |
Patrick Georgi | 450204a | 2016-08-15 19:13:29 +0200 | [diff] [blame] | 174 | def SetOptions(self, small, gbb_flags, force_efs=False): |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 175 | """Set up options supported by Bundle. |
| 176 | |
| 177 | Args: |
| 178 | small: Only create a signed U-Boot - don't produce the full packed |
| 179 | firmware image. This is useful for devs who want to replace just the |
| 180 | U-Boot part while keeping the keys, gbb, etc. the same. |
Simon Glass | 6e486c2 | 2012-10-26 15:43:42 -0700 | [diff] [blame] | 181 | gbb_flags: Specification for string containing adjustments to make. |
Simon Glass | 00d027e | 2013-07-20 14:51:12 -0600 | [diff] [blame] | 182 | force_efs: Force firmware to use 'early firmware selection' feature, |
| 183 | where RW firmware is selected before SDRAM is initialized. |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 184 | """ |
| 185 | self._small = small |
Simon Glass | 157c066 | 2012-10-23 13:52:42 -0700 | [diff] [blame] | 186 | self._gbb_flags = gbb_flags |
Simon Glass | 00d027e | 2013-07-20 14:51:12 -0600 | [diff] [blame] | 187 | self._force_efs = force_efs |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 188 | |
Simon Glass | 22f39fb | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 189 | def _GetBuildRoot(self): |
| 190 | """Get the path to this board's 'firmware' directory. |
| 191 | |
| 192 | Returns: |
| 193 | Path to firmware directory, with ## representing the path to the |
| 194 | chroot. |
| 195 | """ |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 196 | if not self._board: |
| 197 | raise ValueError('No board defined - please define a board to use') |
Simon Glass | 22f39fb | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 198 | return os.path.join('##', 'build', self._board, 'firmware') |
| 199 | |
| 200 | def _CheckFdtFilename(self, fname): |
| 201 | """Check provided FDT filename and return the correct name if needed. |
| 202 | |
| 203 | Where the filename lacks a path, add a default path for this board. |
| 204 | Where no FDT filename is provided, select a default one for this board. |
| 205 | |
| 206 | Args: |
| 207 | fname: Proposed FDT filename. |
| 208 | |
| 209 | Returns: |
| 210 | Selected FDT filename, after validation. |
| 211 | """ |
| 212 | build_root = self._GetBuildRoot() |
Julius Werner | b4b1439 | 2013-08-09 14:41:40 -0700 | [diff] [blame] | 213 | dir_name = os.path.join(build_root, 'dtb') |
Simon Glass | 22f39fb | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 214 | if not fname: |
Simon Glass | ceff3ff | 2012-04-04 11:23:45 -0700 | [diff] [blame] | 215 | # Figure out where the file should be, and the name we expect. |
Simon Glass | ceff3ff | 2012-04-04 11:23:45 -0700 | [diff] [blame] | 216 | base_name = re.sub('_', '-', self._board) |
| 217 | |
| 218 | # In case the name exists with a prefix or suffix, find it. |
Julius Werner | b4b1439 | 2013-08-09 14:41:40 -0700 | [diff] [blame] | 219 | wildcard = os.path.join(dir_name, '*%s.dtb' % base_name) |
Simon Glass | ceff3ff | 2012-04-04 11:23:45 -0700 | [diff] [blame] | 220 | found_list = glob.glob(self._tools.Filename(wildcard)) |
| 221 | if len(found_list) == 1: |
Simon Glass | 22f39fb | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 222 | fname = found_list[0] |
Simon Glass | ceff3ff | 2012-04-04 11:23:45 -0700 | [diff] [blame] | 223 | else: |
| 224 | # We didn't find anything definite, so set up our expected name. |
Julius Werner | b4b1439 | 2013-08-09 14:41:40 -0700 | [diff] [blame] | 225 | fname = os.path.join(dir_name, '%s.dtb' % base_name) |
Simon Glass | ceff3ff | 2012-04-04 11:23:45 -0700 | [diff] [blame] | 226 | |
Simon Glass | 881964d | 2012-04-04 11:34:09 -0700 | [diff] [blame] | 227 | # Convert things like 'exynos5250-daisy' into a full path. |
Simon Glass | 22f39fb | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 228 | root, ext = os.path.splitext(fname) |
Simon Glass | 881964d | 2012-04-04 11:34:09 -0700 | [diff] [blame] | 229 | if not ext and not os.path.dirname(root): |
Julius Werner | b4b1439 | 2013-08-09 14:41:40 -0700 | [diff] [blame] | 230 | fname = os.path.join(dir_name, '%s.dtb' % root) |
Simon Glass | 22f39fb | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 231 | return fname |
| 232 | |
| 233 | def CheckOptions(self): |
| 234 | """Check provided options and select defaults.""" |
| 235 | build_root = self._GetBuildRoot() |
Simon Glass | 881964d | 2012-04-04 11:34:09 -0700 | [diff] [blame] | 236 | |
Simon Glass | 49b026b | 2013-04-26 16:38:42 -0700 | [diff] [blame] | 237 | board_type = self._board.split('_')[0] |
| 238 | model = type_to_model.get(board_type) |
| 239 | |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 240 | if not self.uboot_fname: |
| 241 | self.uboot_fname = os.path.join(build_root, 'u-boot.bin') |
| 242 | if not self.bct_fname: |
| 243 | self.bct_fname = os.path.join(build_root, 'bct', 'board.bct') |
Simon Glass | 49b026b | 2013-04-26 16:38:42 -0700 | [diff] [blame] | 244 | if model: |
| 245 | if not self.exynos_bl1: |
Simon Glass | d05696e | 2013-06-13 20:14:00 -0700 | [diff] [blame] | 246 | self.exynos_bl1 = os.path.join(build_root, 'u-boot.bl1.bin') |
Simon Glass | 49b026b | 2013-04-26 16:38:42 -0700 | [diff] [blame] | 247 | if not self.exynos_bl2: |
Julius Werner | b12c005 | 2013-08-14 13:57:04 -0700 | [diff] [blame] | 248 | self.exynos_bl2 = os.path.join(build_root, 'u-boot-spl.wrapped.bin') |
Simon Glass | 0726795 | 2012-06-08 12:45:13 -0700 | [diff] [blame] | 249 | if not self.coreboot_fname: |
| 250 | self.coreboot_fname = os.path.join(build_root, 'coreboot.rom') |
| 251 | if not self.skeleton_fname: |
Stefan Reinauer | 728be82 | 2012-10-02 16:54:09 -0700 | [diff] [blame] | 252 | self.skeleton_fname = os.path.join(build_root, 'coreboot.rom') |
Simon Glass | be0bc00 | 2012-08-16 12:50:48 -0700 | [diff] [blame] | 253 | if not self.ecrw_fname: |
| 254 | self.ecrw_fname = os.path.join(build_root, 'ec.RW.bin') |
Randall Spangler | 7307da9 | 2014-07-18 12:47:34 -0700 | [diff] [blame] | 255 | if not self.pdrw_fname: |
| 256 | self.pdrw_fname = os.path.join(build_root, 'pd.RW.bin') |
Simon Glass | be0bc00 | 2012-08-16 12:50:48 -0700 | [diff] [blame] | 257 | if not self.ecro_fname: |
| 258 | self.ecro_fname = os.path.join(build_root, 'ec.RO.bin') |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 259 | |
Simon Glass | 7575930 | 2012-03-15 20:26:53 -0700 | [diff] [blame] | 260 | def GetFiles(self): |
| 261 | """Get a list of files that we know about. |
| 262 | |
| 263 | This is the opposite of SetFiles except that we may have put in some |
| 264 | default names. It returns a dictionary containing the filename for |
| 265 | each of a number of pre-defined files. |
| 266 | |
| 267 | Returns: |
| 268 | Dictionary, with one entry for each file. |
| 269 | """ |
| 270 | file_list = { |
| 271 | 'bct' : self.bct_fname, |
| 272 | 'exynos-bl1' : self.exynos_bl1, |
| 273 | 'exynos-bl2' : self.exynos_bl2, |
| 274 | } |
| 275 | return file_list |
| 276 | |
Simon Glass | 4a887b1 | 2012-10-23 16:29:03 -0700 | [diff] [blame] | 277 | def DecodeGBBFlagsFromFdt(self): |
| 278 | """Get Google Binary Block flags from the FDT. |
| 279 | |
| 280 | These should be in the chromeos-config node, like this: |
| 281 | |
| 282 | chromeos-config { |
| 283 | gbb-flag-dev-screen-short-delay; |
| 284 | gbb-flag-force-dev-switch-on; |
| 285 | gbb-flag-force-dev-boot-usb; |
| 286 | gbb-flag-disable-fw-rollback-check; |
| 287 | }; |
| 288 | |
| 289 | Returns: |
| 290 | GBB flags value from FDT. |
| 291 | """ |
| 292 | chromeos_config = self.fdt.GetProps("/chromeos-config") |
| 293 | gbb_flags = 0 |
| 294 | for name in chromeos_config: |
| 295 | if name.startswith('gbb-flag-'): |
| 296 | flag_value = gbb_flag_properties.get(name[9:]) |
| 297 | if flag_value: |
| 298 | gbb_flags |= flag_value |
| 299 | self._out.Notice("FDT: Enabling %s." % name) |
| 300 | else: |
| 301 | raise ValueError("FDT contains invalid GBB flags '%s'" % name) |
| 302 | return gbb_flags |
| 303 | |
Simon Glass | 157c066 | 2012-10-23 13:52:42 -0700 | [diff] [blame] | 304 | def DecodeGBBFlagsFromOptions(self, gbb_flags, adjustments): |
| 305 | """Decode ajustments to the provided GBB flags. |
| 306 | |
| 307 | We support three options: |
| 308 | |
| 309 | hex value: c2 |
| 310 | defined value: force-dev-boot-usb,load-option-roms |
| 311 | adjust default value: -load-option-roms,+force-dev-boot-usb |
| 312 | |
| 313 | The last option starts from the passed-in GBB flags and adds or removes |
| 314 | flags. |
| 315 | |
| 316 | Args: |
| 317 | gbb_flags: Base (default) FDT flags. |
| 318 | adjustments: String containing adjustments to make. |
| 319 | |
| 320 | Returns: |
| 321 | Updated FDT flags. |
| 322 | """ |
| 323 | use_base_value = True |
| 324 | if adjustments: |
| 325 | try: |
| 326 | return int(adjustments, base=16) |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 327 | except (ValueError, TypeError): |
Simon Glass | 157c066 | 2012-10-23 13:52:42 -0700 | [diff] [blame] | 328 | pass |
| 329 | for flag in adjustments.split(','): |
| 330 | oper = None |
| 331 | if flag[0] in ['-', '+']: |
| 332 | oper = flag[0] |
| 333 | flag = flag[1:] |
| 334 | value = gbb_flag_properties.get(flag) |
| 335 | if not value: |
| 336 | raise ValueError("Invalid GBB flag '%s'" % flag) |
| 337 | if oper == '+': |
| 338 | gbb_flags |= value |
Simon Glass | 8481658 | 2012-11-20 10:53:10 -0800 | [diff] [blame] | 339 | self._out.Notice("Cmdline: Enabling %s." % flag) |
Simon Glass | 157c066 | 2012-10-23 13:52:42 -0700 | [diff] [blame] | 340 | elif oper == '-': |
| 341 | gbb_flags &= ~value |
Simon Glass | 8481658 | 2012-11-20 10:53:10 -0800 | [diff] [blame] | 342 | self._out.Notice("Cmdline: Disabling %s." % flag) |
Simon Glass | 157c066 | 2012-10-23 13:52:42 -0700 | [diff] [blame] | 343 | else: |
| 344 | if use_base_value: |
| 345 | gbb_flags = 0 |
| 346 | use_base_value = False |
Simon Glass | 8481658 | 2012-11-20 10:53:10 -0800 | [diff] [blame] | 347 | self._out.Notice('Cmdline: Resetting flags to 0') |
Simon Glass | 157c066 | 2012-10-23 13:52:42 -0700 | [diff] [blame] | 348 | gbb_flags |= value |
Simon Glass | 8481658 | 2012-11-20 10:53:10 -0800 | [diff] [blame] | 349 | self._out.Notice("Cmdline: Enabling %s." % flag) |
Simon Glass | 157c066 | 2012-10-23 13:52:42 -0700 | [diff] [blame] | 350 | |
| 351 | return gbb_flags |
| 352 | |
Simon Glass | 5657757 | 2011-07-19 11:08:06 +1200 | [diff] [blame] | 353 | def _CreateGoogleBinaryBlock(self, hardware_id): |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 354 | """Create a GBB for the image. |
| 355 | |
Simon Glass | 5657757 | 2011-07-19 11:08:06 +1200 | [diff] [blame] | 356 | Args: |
| 357 | hardware_id: Hardware ID to use for this board. If None, then the |
| 358 | default from the Fdt will be used |
| 359 | |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 360 | Returns: |
| 361 | Path of the created GBB file. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 362 | """ |
Simon Glass | 5657757 | 2011-07-19 11:08:06 +1200 | [diff] [blame] | 363 | if not hardware_id: |
Simon Glass | 02d124a | 2012-03-02 14:47:20 -0800 | [diff] [blame] | 364 | hardware_id = self.fdt.GetString('/config', 'hwid') |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 365 | gbb_size = self.fdt.GetFlashPartSize('ro', 'gbb') |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 366 | odir = self._tools.outdir |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 367 | |
Simon Glass | 4a887b1 | 2012-10-23 16:29:03 -0700 | [diff] [blame] | 368 | gbb_flags = self.DecodeGBBFlagsFromFdt() |
Stefan Reinauer | 975e68f | 2012-02-27 13:27:08 -0800 | [diff] [blame] | 369 | |
Simon Glass | 157c066 | 2012-10-23 13:52:42 -0700 | [diff] [blame] | 370 | # Allow command line to override flags |
| 371 | gbb_flags = self.DecodeGBBFlagsFromOptions(gbb_flags, self._gbb_flags) |
| 372 | |
Simon Glass | 4a887b1 | 2012-10-23 16:29:03 -0700 | [diff] [blame] | 373 | self._out.Notice("GBB flags value %#x" % gbb_flags) |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 374 | self._out.Progress('Creating GBB') |
Patrick Georgi | 4803057 | 2016-09-09 22:55:46 +0200 | [diff] [blame] | 375 | sizes = [0x100, 0x1000, 0, 0x1000] |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 376 | sizes = ['%#x' % size for size in sizes] |
| 377 | gbb = 'gbb.bin' |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 378 | keydir = self._tools.Filename(self._keydir) |
Vadim Bendebury | bfd227f | 2014-11-28 22:14:24 -0800 | [diff] [blame] | 379 | |
| 380 | gbb_set_command = ['-s', |
| 381 | '--hwid=%s' % hardware_id, |
| 382 | '--rootkey=%s/root_key.vbpubk' % keydir, |
| 383 | '--recoverykey=%s/recovery_key.vbpubk' % keydir, |
| 384 | '--flags=%d' % gbb_flags, |
| 385 | gbb] |
Vadim Bendebury | bfd227f | 2014-11-28 22:14:24 -0800 | [diff] [blame] | 386 | |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 387 | self._tools.Run('gbb_utility', ['-c', ','.join(sizes), gbb], cwd=odir) |
Vadim Bendebury | bfd227f | 2014-11-28 22:14:24 -0800 | [diff] [blame] | 388 | self._tools.Run('gbb_utility', gbb_set_command, cwd=odir) |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 389 | return os.path.join(odir, gbb) |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 390 | |
Simon Glass | e13ee2c | 2011-07-28 08:12:28 +1200 | [diff] [blame] | 391 | def _SignBootstub(self, bct, bootstub, text_base): |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 392 | """Sign an image so that the Tegra SOC will boot it. |
| 393 | |
| 394 | Args: |
| 395 | bct: BCT file to use. |
| 396 | bootstub: Boot stub (U-Boot + fdt) file to sign. |
| 397 | text_base: Address of text base for image. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 398 | |
| 399 | Returns: |
| 400 | filename of signed image. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 401 | """ |
| 402 | # First create a config file - this is how we instruct cbootimage |
Simon Glass | e13ee2c | 2011-07-28 08:12:28 +1200 | [diff] [blame] | 403 | signed = os.path.join(self._tools.outdir, 'signed.bin') |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 404 | self._out.Progress('Signing Bootstub') |
Simon Glass | e13ee2c | 2011-07-28 08:12:28 +1200 | [diff] [blame] | 405 | config = os.path.join(self._tools.outdir, 'boot.cfg') |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 406 | fd = open(config, 'w') |
| 407 | fd.write('Version = 1;\n') |
| 408 | fd.write('Redundancy = 1;\n') |
| 409 | fd.write('Bctfile = %s;\n' % bct) |
Doug Anderson | 0eeb074 | 2011-09-15 18:11:40 -0700 | [diff] [blame] | 410 | |
| 411 | # TODO(dianders): Right now, we don't have enough space in our flash map |
| 412 | # for two copies of the BCT when we're using NAND, so hack it to 1. Not |
| 413 | # sure what this does for reliability, but at least things will fit... |
| 414 | is_nand = "NvBootDevType_Nand" in self._tools.Run('bct_dump', [bct]) |
| 415 | if is_nand: |
| 416 | fd.write('Bctcopy = 1;\n') |
| 417 | |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 418 | fd.write('BootLoader = %s,%#x,%#x,Complete;\n' % (bootstub, text_base, |
| 419 | text_base)) |
Doug Anderson | 0eeb074 | 2011-09-15 18:11:40 -0700 | [diff] [blame] | 420 | |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 421 | fd.close() |
| 422 | |
| 423 | self._tools.Run('cbootimage', [config, signed]) |
| 424 | self._tools.OutputSize('BCT', bct) |
| 425 | self._tools.OutputSize('Signed image', signed) |
| 426 | return signed |
| 427 | |
Doug Anderson | 86ce5f4 | 2011-07-27 10:40:18 -0700 | [diff] [blame] | 428 | def SetBootcmd(self, bootcmd, bootsecure): |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 429 | """Set the boot command for U-Boot. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 430 | |
| 431 | Args: |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 432 | bootcmd: Boot command to use, as a string (if None this this is a nop). |
Doug Anderson | 86ce5f4 | 2011-07-27 10:40:18 -0700 | [diff] [blame] | 433 | bootsecure: We'll set '/config/bootsecure' to 1 if True and 0 if False. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 434 | """ |
Simon Glass | 468d875 | 2012-09-19 16:36:19 -0700 | [diff] [blame] | 435 | if bootcmd is not None: |
| 436 | if bootcmd == 'none': |
| 437 | bootcmd = '' |
Simon Glass | 02d124a | 2012-03-02 14:47:20 -0800 | [diff] [blame] | 438 | self.fdt.PutString('/config', 'bootcmd', bootcmd) |
| 439 | self.fdt.PutInteger('/config', 'bootsecure', int(bootsecure)) |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 440 | self._out.Info('Boot command: %s' % bootcmd) |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 441 | |
Simon Glass | a4934b7 | 2012-05-09 13:35:02 -0700 | [diff] [blame] | 442 | def SetNodeEnabled(self, node_name, enabled): |
| 443 | """Set whether an node is enabled or disabled. |
| 444 | |
| 445 | This simply sets the 'status' property of a node to "ok", or "disabled". |
| 446 | |
| 447 | The node should either be a full path to the node (like '/uart@10200000') |
| 448 | or an alias property. |
| 449 | |
| 450 | Aliases are supported like this: |
| 451 | |
| 452 | aliases { |
| 453 | console = "/uart@10200000"; |
| 454 | }; |
| 455 | |
| 456 | pointing to a node: |
| 457 | |
| 458 | uart@10200000 { |
Simon Glass | 4c5066f | 2012-06-20 16:51:19 -0700 | [diff] [blame] | 459 | status = "okay"; |
Simon Glass | a4934b7 | 2012-05-09 13:35:02 -0700 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | In this case, this function takes the name of the alias ('console' in |
| 463 | this case) and updates the status of the node that is pointed to, to |
| 464 | either ok or disabled. If the alias does not exist, a warning is |
| 465 | displayed. |
| 466 | |
| 467 | Args: |
| 468 | node_name: Name of node (e.g. '/uart@10200000') or alias alias |
| 469 | (e.g. 'console') to adjust |
| 470 | enabled: True to enable, False to disable |
| 471 | """ |
| 472 | # Look up the alias if this is an alias reference |
| 473 | if not node_name.startswith('/'): |
| 474 | lookup = self.fdt.GetString('/aliases', node_name, '') |
| 475 | if not lookup: |
| 476 | self._out.Warning("Cannot find alias '%s' - ignoring" % node_name) |
| 477 | return |
| 478 | node_name = lookup |
| 479 | if enabled: |
Simon Glass | 4c5066f | 2012-06-20 16:51:19 -0700 | [diff] [blame] | 480 | status = 'okay' |
Simon Glass | a4934b7 | 2012-05-09 13:35:02 -0700 | [diff] [blame] | 481 | else: |
| 482 | status = 'disabled' |
| 483 | self.fdt.PutString(node_name, 'status', status) |
| 484 | |
| 485 | def AddEnableList(self, enable_list): |
| 486 | """Process a list of nodes to enable/disable. |
| 487 | |
| 488 | Args: |
Vadim Bendebury | 7dac18c | 2014-05-06 14:13:35 -0700 | [diff] [blame] | 489 | enable_list: List of (node, value) tuples to add to the fdt. For each |
Simon Glass | a4934b7 | 2012-05-09 13:35:02 -0700 | [diff] [blame] | 490 | tuple: |
| 491 | node: The fdt node to write to will be <node> or pointed to by |
| 492 | /aliases/<node>. We can tell which |
| 493 | value: 0 to disable the node, 1 to enable it |
Vadim Bendebury | 7dac18c | 2014-05-06 14:13:35 -0700 | [diff] [blame] | 494 | |
Vadim Bendebury | 507c001 | 2013-06-09 12:49:25 -0700 | [diff] [blame] | 495 | Raises: |
| 496 | CmdError if a command fails. |
Simon Glass | a4934b7 | 2012-05-09 13:35:02 -0700 | [diff] [blame] | 497 | """ |
| 498 | if enable_list: |
| 499 | for node_name, enabled in enable_list: |
| 500 | try: |
| 501 | enabled = int(enabled) |
| 502 | if enabled not in (0, 1): |
| 503 | raise ValueError |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 504 | except ValueError: |
Simon Glass | a4934b7 | 2012-05-09 13:35:02 -0700 | [diff] [blame] | 505 | raise CmdError("Invalid enable option value '%s' " |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 506 | "(should be 0 or 1)" % str(enabled)) |
Simon Glass | a4934b7 | 2012-05-09 13:35:02 -0700 | [diff] [blame] | 507 | self.SetNodeEnabled(node_name, enabled) |
| 508 | |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 509 | def AddConfigList(self, config_list, use_int=False): |
| 510 | """Add a list of config items to the fdt. |
| 511 | |
| 512 | Normally these values are written to the fdt as strings, but integers |
| 513 | are also supported, in which case the values will be converted to integers |
| 514 | (if necessary) before being stored. |
| 515 | |
| 516 | Args: |
| 517 | config_list: List of (config, value) tuples to add to the fdt. For each |
| 518 | tuple: |
| 519 | config: The fdt node to write to will be /config/<config>. |
| 520 | value: An integer or string value to write. |
| 521 | use_int: True to only write integer values. |
| 522 | |
| 523 | Raises: |
| 524 | CmdError: if a value is required to be converted to integer but can't be. |
| 525 | """ |
| 526 | if config_list: |
| 527 | for config in config_list: |
| 528 | value = config[1] |
| 529 | if use_int: |
| 530 | try: |
| 531 | value = int(value) |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 532 | except ValueError: |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 533 | raise CmdError("Cannot convert config option '%s' to integer" % |
Vadim Bendebury | 7bfdb37 | 2013-03-27 11:52:58 -0700 | [diff] [blame] | 534 | str(value)) |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 535 | if type(value) == type(1): |
Simon Glass | 02d124a | 2012-03-02 14:47:20 -0800 | [diff] [blame] | 536 | self.fdt.PutInteger('/config', '%s' % config[0], value) |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 537 | else: |
Simon Glass | 02d124a | 2012-03-02 14:47:20 -0800 | [diff] [blame] | 538 | self.fdt.PutString('/config', '%s' % config[0], value) |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 539 | |
Simon Glass | 7c2d557 | 2011-11-15 14:47:08 -0800 | [diff] [blame] | 540 | def DecodeTextBase(self, data): |
| 541 | """Look at a U-Boot image and try to decode its TEXT_BASE. |
| 542 | |
| 543 | This works because U-Boot has a header with the value 0x12345678 |
| 544 | immediately followed by the TEXT_BASE value. We can therefore read this |
| 545 | from the image with some certainty. We check only the first 40 words |
| 546 | since the header should be within that region. |
| 547 | |
Simon Glass | 96b5030 | 2012-07-20 06:55:28 +0100 | [diff] [blame] | 548 | Since upstream Tegra has moved to having a 16KB SPL region at the start, |
| 549 | and currently this does holds the U-Boot text base (e.g. 0x10c000) instead |
| 550 | of the SPL one (e.g. 0x108000), we search in the U-Boot part as well. |
| 551 | |
Simon Glass | 7c2d557 | 2011-11-15 14:47:08 -0800 | [diff] [blame] | 552 | Args: |
| 553 | data: U-Boot binary data |
| 554 | |
| 555 | Returns: |
| 556 | Text base (integer) or None if none was found |
| 557 | """ |
| 558 | found = False |
Simon Glass | 96b5030 | 2012-07-20 06:55:28 +0100 | [diff] [blame] | 559 | for start in (0, 0x4000): |
| 560 | for i in range(start, start + 160, 4): |
| 561 | word = data[i:i + 4] |
Simon Glass | 7c2d557 | 2011-11-15 14:47:08 -0800 | [diff] [blame] | 562 | |
Simon Glass | 96b5030 | 2012-07-20 06:55:28 +0100 | [diff] [blame] | 563 | # TODO(sjg): This does not cope with a big-endian target |
| 564 | value = struct.unpack('<I', word)[0] |
| 565 | if found: |
| 566 | return value - start |
| 567 | if value == 0x12345678: |
| 568 | found = True |
Simon Glass | 7c2d557 | 2011-11-15 14:47:08 -0800 | [diff] [blame] | 569 | |
| 570 | return None |
| 571 | |
| 572 | def CalcTextBase(self, name, fdt, fname): |
| 573 | """Calculate the TEXT_BASE to use for U-Boot. |
| 574 | |
| 575 | Normally this value is in the fdt, so we just read it from there. But as |
| 576 | a second check we look at the image itself in case this is different, and |
| 577 | switch to that if it is. |
| 578 | |
| 579 | This allows us to flash any U-Boot even if its TEXT_BASE is different. |
| 580 | This is particularly useful with upstream U-Boot which uses a different |
| 581 | value (which we will move to). |
| 582 | """ |
| 583 | data = self._tools.ReadFile(fname) |
Andrew Chew | aa09254 | 2013-01-09 16:30:52 -0800 | [diff] [blame] | 584 | # The value that comes back from fdt.GetInt is signed, which makes no |
| 585 | # sense for an address base. Force it to unsigned. |
| 586 | fdt_text_base = fdt.GetInt('/chromeos-config', 'textbase', 0) & 0xffffffff |
Simon Glass | 7c2d557 | 2011-11-15 14:47:08 -0800 | [diff] [blame] | 587 | text_base = self.DecodeTextBase(data) |
Simon Glass | 96b5030 | 2012-07-20 06:55:28 +0100 | [diff] [blame] | 588 | text_base_str = '%#x' % text_base if text_base else 'None' |
| 589 | self._out.Info('TEXT_BASE: fdt says %#x, %s says %s' % (fdt_text_base, |
| 590 | fname, text_base_str)) |
Simon Glass | 7c2d557 | 2011-11-15 14:47:08 -0800 | [diff] [blame] | 591 | |
| 592 | # If they are different, issue a warning and switch over. |
| 593 | if text_base and text_base != fdt_text_base: |
| 594 | self._out.Warning("TEXT_BASE %x in %sU-Boot doesn't match " |
| 595 | "fdt value of %x. Using %x" % (text_base, name, |
| 596 | fdt_text_base, text_base)) |
| 597 | fdt_text_base = text_base |
| 598 | return fdt_text_base |
| 599 | |
Aaron Durbin | 95ef60c | 2016-01-06 09:17:21 -0600 | [diff] [blame] | 600 | def _AddCbfsFiles(self, bootstub, cbfs_files): |
| 601 | for dir, subs, files in os.walk(cbfs_files): |
Aaron Durbin | 5751c3e | 2016-01-04 11:45:22 -0600 | [diff] [blame] | 602 | for file in files: |
| 603 | file = os.path.join(dir, file) |
Aaron Durbin | 95ef60c | 2016-01-06 09:17:21 -0600 | [diff] [blame] | 604 | cbfs_name = file.replace(cbfs_files, '', 1).strip('/') |
Aaron Durbin | 5751c3e | 2016-01-04 11:45:22 -0600 | [diff] [blame] | 605 | self._tools.Run('cbfstool', [bootstub, 'add', '-f', file, |
| 606 | '-n', cbfs_name, '-t', 'raw', '-c', 'lzma']) |
| 607 | |
| 608 | def _CreateCorebootStub(self, pack, coreboot): |
| 609 | """Create a coreboot boot stub and add pack properties. |
Stefan Reinauer | c2e1e4d | 2011-08-23 14:50:59 -0700 | [diff] [blame] | 610 | |
| 611 | Args: |
Aaron Durbin | a113f52 | 2016-01-05 09:09:55 -0600 | [diff] [blame] | 612 | pack: a PackFirmware object describing the firmware image to build. |
Stefan Reinauer | c2e1e4d | 2011-08-23 14:50:59 -0700 | [diff] [blame] | 613 | coreboot: Path to coreboot.rom |
Stefan Reinauer | c2e1e4d | 2011-08-23 14:50:59 -0700 | [diff] [blame] | 614 | """ |
| 615 | bootstub = os.path.join(self._tools.outdir, 'coreboot-full.rom') |
Simon Glass | f2b3a5c | 2012-06-07 14:02:36 -0700 | [diff] [blame] | 616 | shutil.copyfile(self._tools.Filename(coreboot), bootstub) |
Simon Glass | cbc8355 | 2012-07-23 15:26:22 +0100 | [diff] [blame] | 617 | |
Aaron Durbin | 5751c3e | 2016-01-04 11:45:22 -0600 | [diff] [blame] | 618 | pack.AddProperty('coreboot', bootstub) |
| 619 | pack.AddProperty('image', bootstub) |
Stefan Reinauer | c2e1e4d | 2011-08-23 14:50:59 -0700 | [diff] [blame] | 620 | |
Aaron Durbin | 95ef60c | 2016-01-06 09:17:21 -0600 | [diff] [blame] | 621 | # Add files to to RO and RW CBFS if provided. |
Aaron Durbin | 5751c3e | 2016-01-04 11:45:22 -0600 | [diff] [blame] | 622 | if self.cbfs_files: |
Aaron Durbin | 95ef60c | 2016-01-06 09:17:21 -0600 | [diff] [blame] | 623 | self._AddCbfsFiles(bootstub, self.cbfs_files) |
Simon Glass | 7e19922 | 2012-03-13 15:51:18 -0700 | [diff] [blame] | 624 | |
Aaron Durbin | a113f52 | 2016-01-05 09:09:55 -0600 | [diff] [blame] | 625 | # Create a coreboot copy to use as a scratch pad. Order matters. The |
| 626 | # cbfs_files were added prior to this action. That's so the RW CBFS |
Patrick Georgi | 45b64b8 | 2016-10-05 16:35:27 +0200 | [diff] [blame] | 627 | # regions inherit the files from the RO CBFS region. |
Patrick Georgi | 3a33267 | 2015-11-20 10:02:51 +0100 | [diff] [blame] | 628 | cb_copy = os.path.abspath(os.path.join(self._tools.outdir, 'cb_with_fmap')) |
Aaron Durbin | a113f52 | 2016-01-05 09:09:55 -0600 | [diff] [blame] | 629 | self._tools.WriteFile(cb_copy, self._tools.ReadFile(bootstub)) |
Patrick Georgi | 3a33267 | 2015-11-20 10:02:51 +0100 | [diff] [blame] | 630 | binary = self._tools.ReadFile(bootstub) |
Patrick Georgi | 3a33267 | 2015-11-20 10:02:51 +0100 | [diff] [blame] | 631 | self._tools.WriteFile(cb_copy, binary) |
| 632 | # Publish where coreboot is with the FMAP data. |
| 633 | pack.AddProperty('cb_with_fmap', cb_copy) |
Aaron Durbin | a113f52 | 2016-01-05 09:09:55 -0600 | [diff] [blame] | 634 | |
Aaron Durbin | 95ef60c | 2016-01-06 09:17:21 -0600 | [diff] [blame] | 635 | # Add files to to RO CBFS if provided. This done here such that the |
| 636 | # copy above does not contain the RO CBFS files. |
| 637 | if self.rocbfs_files: |
| 638 | self._AddCbfsFiles(bootstub, self.rocbfs_files) |
| 639 | |
Aaron Durbin | a113f52 | 2016-01-05 09:09:55 -0600 | [diff] [blame] | 640 | |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 641 | def _PackOutput(self, msg): |
| 642 | """Helper function to write output from PackFirmware (verbose level 2). |
| 643 | |
| 644 | This is passed to PackFirmware for it to use to write output. |
| 645 | |
| 646 | Args: |
| 647 | msg: Message to display. |
| 648 | """ |
| 649 | self._out.Notice(msg) |
| 650 | |
Patrick Georgi | c1064f4 | 2016-10-12 11:34:24 +0200 | [diff] [blame] | 651 | def _FdtNameToFmap(self, fdtstr): |
| 652 | return re.sub('-', '_', fdtstr).upper() |
| 653 | |
Aaron Durbin | 8056445 | 2015-12-21 15:25:06 -0600 | [diff] [blame] | 654 | def _FmapNameByPath(self, path): |
| 655 | """ Take list of names to form node path. Return FMAP name. |
| 656 | |
| 657 | Obtain the FMAP name described by the node path. |
| 658 | |
| 659 | Args: |
| 660 | path: list forming a node path. |
| 661 | |
| 662 | Returns: |
| 663 | FMAP name of fdt node. |
| 664 | |
| 665 | Raises: |
| 666 | CmdError if path not found. |
| 667 | """ |
| 668 | lbl = self.fdt.GetLabel(self.fdt.GetFlashNode(*path)) |
Patrick Georgi | c1064f4 | 2016-10-12 11:34:24 +0200 | [diff] [blame] | 669 | return self._FdtNameToFmap(lbl) |
Aaron Durbin | 8056445 | 2015-12-21 15:25:06 -0600 | [diff] [blame] | 670 | |
Vadim Bendebury | c9600c6 | 2014-12-22 16:31:13 -0800 | [diff] [blame] | 671 | def _PrepareCbfs(self, pack, blob_name): |
| 672 | """Create CBFS blob in rw-boot-{a,b} FMAP sections. |
| 673 | |
| 674 | When the blob name is defined as cbfs#<section>#<subsection>, fill the |
| 675 | <section>_<subsection> area in the flash map with a CBFS copy, putting the |
| 676 | CBFS header of the copy at the base of the section. |
| 677 | |
| 678 | If --coreboot-elf parameter was specified during cros_bumdle_firmware |
| 679 | invocation, add the parameter of this option as the payload to the new |
| 680 | CBFS instance. |
| 681 | |
| 682 | Args: |
| 683 | pack: a PackFirmware object describing the firmware image to build. |
| 684 | blob_name: a string, blob name describing what FMAP section this CBFS |
| 685 | copy is destined to |
| 686 | Raises: |
Patrick Georgi | 3a33267 | 2015-11-20 10:02:51 +0100 | [diff] [blame] | 687 | CmdError if cbfs-files node has incorrect parameters. |
Vadim Bendebury | c9600c6 | 2014-12-22 16:31:13 -0800 | [diff] [blame] | 688 | """ |
Patrick Georgi | 3a33267 | 2015-11-20 10:02:51 +0100 | [diff] [blame] | 689 | cb_copy = pack.GetProperty('cb_with_fmap') |
Vadim Bendebury | c9600c6 | 2014-12-22 16:31:13 -0800 | [diff] [blame] | 690 | |
| 691 | part_sections = blob_name.split('/')[1:] |
Aaron Durbin | 8056445 | 2015-12-21 15:25:06 -0600 | [diff] [blame] | 692 | fmap_dst = self._FmapNameByPath(part_sections) |
Vadim Bendebury | c9600c6 | 2014-12-22 16:31:13 -0800 | [diff] [blame] | 693 | |
| 694 | # Base address and size of the desitnation partition |
| 695 | base, size = self.fdt.GetFlashPart(*part_sections) |
| 696 | |
Vadim Bendebury | c9600c6 | 2014-12-22 16:31:13 -0800 | [diff] [blame] | 697 | # Add coreboot payload if so requested. Note that the some images use |
| 698 | # different payload for the rw sections, which is passed in as the value |
| 699 | # of the --uboot option in the command line. |
| 700 | if self.uboot_fname: |
| 701 | payload_fname = self.uboot_fname |
| 702 | elif self.coreboot_elf: |
| 703 | payload_fname = self.coreboot_elf |
| 704 | else: |
| 705 | payload_fname = None |
| 706 | |
| 707 | if payload_fname: |
| 708 | self._tools.Run('cbfstool', [ |
| 709 | cb_copy, 'add-payload', '-f', payload_fname, |
Patrick Georgi | 10690b4 | 2015-11-20 22:06:38 +0100 | [diff] [blame] | 710 | '-n', 'fallback/payload', '-c', 'lzma' , '-r', fmap_dst]) |
Vadim Bendebury | c9600c6 | 2014-12-22 16:31:13 -0800 | [diff] [blame] | 711 | |
Patrick Georgi | 964fb54 | 2015-10-16 16:52:03 +0200 | [diff] [blame] | 712 | if self.ecrw_fname: |
| 713 | self._tools.Run('cbfstool', [ |
| 714 | cb_copy, 'add', '-f', self.ecrw_fname, '-t', 'raw', |
Patrick Georgi | 10690b4 | 2015-11-20 22:06:38 +0100 | [diff] [blame] | 715 | '-n', 'ecrw', '-A', 'sha256', '-r', fmap_dst ]) |
Patrick Georgi | 964fb54 | 2015-10-16 16:52:03 +0200 | [diff] [blame] | 716 | |
| 717 | if self.pdrw_fname: |
| 718 | self._tools.Run('cbfstool', [ |
| 719 | cb_copy, 'add', '-f', self.pdrw_fname, '-t', 'raw', |
Patrick Georgi | 10690b4 | 2015-11-20 22:06:38 +0100 | [diff] [blame] | 720 | '-n', 'pdrw', '-A', 'sha256', '-r', fmap_dst ]) |
Patrick Georgi | 964fb54 | 2015-10-16 16:52:03 +0200 | [diff] [blame] | 721 | |
Aaron Durbin | 880cf95 | 2016-01-27 14:11:38 -0600 | [diff] [blame] | 722 | # Parse the file list to obtain the last entry. If its empty use its |
| 723 | # offset as the size of the CBFS to hash. |
| 724 | stdout = self._tools.Run('cbfstool', |
| 725 | [ cb_copy, 'print', '-k', '-r', fmap_dst ]) |
| 726 | # Fields are tab separated in the following order. |
| 727 | # Name Offset Type Metadata Size Data Size Total Size |
| 728 | last_entry = stdout.strip().splitlines()[-1].split('\t') |
| 729 | if last_entry[0] == '(empty)' and last_entry[2] == 'null': |
| 730 | size = int(last_entry[1], 16) |
| 731 | |
Vadim Bendebury | c9600c6 | 2014-12-22 16:31:13 -0800 | [diff] [blame] | 732 | # And extract the blob for the FW section |
| 733 | rw_section = os.path.join(self._tools.outdir, '_'.join(part_sections)) |
| 734 | self._tools.WriteFile(rw_section, |
| 735 | self._tools.ReadFile(cb_copy)[base:base+size]) |
| 736 | |
| 737 | pack.AddProperty(blob_name, rw_section) |
| 738 | |
Patrick Georgi | df77271 | 2016-10-05 16:24:20 +0200 | [diff] [blame] | 739 | def _PrepareBootblock(self, pack): |
| 740 | """Copy bootblock into blob file for packaging |
| 741 | |
| 742 | Args: |
| 743 | pack: a PackFirmware object describing the firmware image to build. |
| 744 | Raises: |
| 745 | CmdError if cbfs-files node has incorrect parameters. |
| 746 | """ |
| 747 | cb_copy = pack.GetProperty('cb_with_fmap') |
| 748 | |
| 749 | bootblock_section = os.path.join(self._tools.outdir, 'bootblock.section') |
| 750 | |
| 751 | self._tools.Run('cbfstool', [ |
| 752 | cb_copy, 'read', '-r', 'BOOTBLOCK', '-f', bootblock_section]) |
| 753 | |
| 754 | pack.AddProperty('bootblock', bootblock_section) |
Vadim Bendebury | c9600c6 | 2014-12-22 16:31:13 -0800 | [diff] [blame] | 755 | |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 756 | def _BuildBlob(self, pack, fdt, blob_type): |
| 757 | """Build the blob data for a particular blob type. |
| 758 | |
| 759 | Args: |
Vadim Bendebury | 7dac18c | 2014-05-06 14:13:35 -0700 | [diff] [blame] | 760 | pack: a PackFirmware object describing the firmware image to build. |
| 761 | fdt: an fdt object including image layout information |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 762 | blob_type: The type of blob to create data for. Supported types are: |
| 763 | coreboot A coreboot image (ROM plus U-boot and .dtb payloads). |
| 764 | signed Nvidia T20/T30 signed image (BCT, U-Boot, .dtb). |
Vadim Bendebury | 507c001 | 2013-06-09 12:49:25 -0700 | [diff] [blame] | 765 | |
| 766 | Raises: |
| 767 | CmdError if a command fails. |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 768 | """ |
Patrick Georgi | 4409222 | 2016-10-12 12:15:48 +0200 | [diff] [blame^] | 769 | cb_copy = pack.GetProperty('cb_with_fmap') |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 770 | if blob_type == 'coreboot': |
Patrick Georgi | fa20ddc | 2016-09-27 14:10:51 +0200 | [diff] [blame] | 771 | pass |
Stefan Reinauer | 9ad5484 | 2012-10-10 12:25:23 -0700 | [diff] [blame] | 772 | elif blob_type == 'legacy': |
Patrick Georgi | 4409222 | 2016-10-12 12:15:48 +0200 | [diff] [blame^] | 773 | self._tools.Run('cbfstool', [cb_copy, 'write', |
| 774 | '-f', self.seabios_fname, |
| 775 | '--force', |
| 776 | '-r', 'RW_LEGACY']) |
Stefan Reinauer | 9ad5484 | 2012-10-10 12:25:23 -0700 | [diff] [blame] | 777 | pack.AddProperty('legacy', self.seabios_fname) |
Vadim Bendebury | c9600c6 | 2014-12-22 16:31:13 -0800 | [diff] [blame] | 778 | elif blob_type.startswith('cbfs'): |
| 779 | self._PrepareCbfs(pack, blob_type) |
Patrick Georgi | df77271 | 2016-10-05 16:24:20 +0200 | [diff] [blame] | 780 | elif blob_type == 'bootblock': |
| 781 | self._PrepareBootblock(pack) |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 782 | elif pack.GetProperty(blob_type): |
| 783 | pass |
Furquan Shaikh | 95ee50c | 2016-05-29 09:46:41 -0700 | [diff] [blame] | 784 | elif blob_type == 'ifwi' or blob_type == 'sig2': |
| 785 | # Copy IFWI/CSE_SIGN(sig2) regions from coreboot copy and build a blob |
| 786 | # for the blob_type |
Furquan Shaikh | 95ee50c | 2016-05-29 09:46:41 -0700 | [diff] [blame] | 787 | blob_start, blob_size = fdt.GetFlashPart('ro', blob_type) |
| 788 | blob_file = blob_type + '.bin' |
| 789 | blob_path = os.path.join(self._tools.outdir, blob_file) |
| 790 | data = self._tools.ReadFile(cb_copy) |
| 791 | self._tools.WriteFile(blob_path, data[blob_start:blob_start+blob_size]) |
| 792 | pack.AddProperty(blob_type, blob_path) |
Che-Liang Chiou | 3bc344c | 2013-02-21 15:18:03 -0800 | [diff] [blame] | 793 | elif blob_type in self.blobs: |
Patrick Georgi | 4409222 | 2016-10-12 12:15:48 +0200 | [diff] [blame^] | 794 | self._tools.Run('cbfstool', [cb_copy, 'write', |
| 795 | '--fill-upward', |
| 796 | '-f', self.blobs[blob_type], |
| 797 | '-r', _FdtNameToFmap(blob_type)]) |
Che-Liang Chiou | 3bc344c | 2013-02-21 15:18:03 -0800 | [diff] [blame] | 798 | pack.AddProperty(blob_type, self.blobs[blob_type]) |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 799 | else: |
| 800 | raise CmdError("Unknown blob type '%s' required in flash map" % |
| 801 | blob_type) |
| 802 | |
Aaron Durbin | 41c85b6 | 2015-12-17 17:40:29 -0600 | [diff] [blame] | 803 | def _BuildBlobs(self, pack, fdt): |
| 804 | """Build the blob data for the list of blobs in the pack. |
| 805 | |
| 806 | Args: |
| 807 | pack: a PackFirmware object describing the firmware image to build. |
| 808 | fdt: an fdt object including image layout information |
| 809 | |
| 810 | Raises: |
| 811 | CmdError if a command fails. |
Aaron Durbin | 41c85b6 | 2015-12-17 17:40:29 -0600 | [diff] [blame] | 812 | """ |
| 813 | blob_list = pack.GetBlobList() |
| 814 | self._out.Info('Building blobs %s\n' % blob_list) |
| 815 | |
| 816 | complete = False |
| 817 | deferred_list = [] |
| 818 | |
Patrick Georgi | fa20ddc | 2016-09-27 14:10:51 +0200 | [diff] [blame] | 819 | # We always have a coreboot blob, and some other components rely on it, so |
| 820 | # make sure it's ready when the others come in. |
| 821 | blob_list.remove('coreboot') |
| 822 | self._CreateCorebootStub(pack, self.coreboot_fname) |
| 823 | |
Patrick Georgi | 49cf7b2 | 2016-09-27 14:25:03 +0200 | [diff] [blame] | 824 | for blob_type in blob_list: |
| 825 | self._BuildBlob(pack, fdt, blob_type) |
Aaron Durbin | 41c85b6 | 2015-12-17 17:40:29 -0600 | [diff] [blame] | 826 | |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 827 | def _CreateImage(self, gbb, fdt): |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 828 | """Create a full firmware image, along with various by-products. |
| 829 | |
| 830 | This uses the provided u-boot.bin, fdt and bct to create a firmware |
| 831 | image containing all the required parts. If the GBB is not supplied |
| 832 | then this will just return a signed U-Boot as the image. |
| 833 | |
| 834 | Args: |
Vadim Bendebury | 7dac18c | 2014-05-06 14:13:35 -0700 | [diff] [blame] | 835 | gbb: a string, full path to the GBB file, or empty if a GBB is not |
| 836 | required. |
| 837 | fdt: an fdt object containing required information. |
Simon Glass | e13ee2c | 2011-07-28 08:12:28 +1200 | [diff] [blame] | 838 | |
| 839 | Returns: |
| 840 | Path to image file |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 841 | """ |
Simon Glass | 02d124a | 2012-03-02 14:47:20 -0800 | [diff] [blame] | 842 | self._out.Notice("Model: %s" % fdt.GetString('/', 'model')) |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 843 | |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 844 | pack = PackFirmware(self._tools, self._out) |
Simon Glass | 00d027e | 2013-07-20 14:51:12 -0600 | [diff] [blame] | 845 | if self._force_efs: |
| 846 | fdt.PutInteger('/chromeos-config', 'early-firmware-selection', 1) |
Simon Glass | a7e66e2 | 2013-07-23 07:21:16 -0600 | [diff] [blame] | 847 | pack.use_efs = fdt.GetInt('/chromeos-config', 'early-firmware-selection', |
| 848 | 0) |
Simon Glass | b8c6d95 | 2012-12-01 06:14:35 -0800 | [diff] [blame] | 849 | |
Simon Glass | 4f31891 | 2013-07-20 16:13:06 -0600 | [diff] [blame] | 850 | pack.SelectFdt(fdt, self._board) |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 851 | |
| 852 | # Get all our blobs ready |
Vadim Bendebury | 466a7d8 | 2014-12-22 10:08:58 -0800 | [diff] [blame] | 853 | if self.uboot_fname: |
| 854 | pack.AddProperty('boot', self.uboot_fname) |
Simon Glass | 284cb89 | 2013-02-09 13:38:03 -0800 | [diff] [blame] | 855 | if self.skeleton_fname: |
| 856 | pack.AddProperty('skeleton', self.skeleton_fname) |
Simon Glass | 3b85f71 | 2012-06-21 07:06:46 -0700 | [diff] [blame] | 857 | pack.AddProperty('dtb', fdt.fname) |
Simon Glass | 50f7460 | 2012-03-15 21:04:25 -0700 | [diff] [blame] | 858 | |
Simon Glass | de9c807 | 2012-07-02 22:29:02 -0700 | [diff] [blame] | 859 | # If we are writing a kernel, add its offset from TEXT_BASE to the fdt. |
| 860 | if self.kernel_fname: |
| 861 | fdt.PutInteger('/config', 'kernel-offset', pack.image_size) |
| 862 | |
Vadim Bendebury | 466a7d8 | 2014-12-22 10:08:58 -0800 | [diff] [blame] | 863 | if gbb: |
| 864 | pack.AddProperty('gbb', gbb) |
Aaron Durbin | 41c85b6 | 2015-12-17 17:40:29 -0600 | [diff] [blame] | 865 | |
| 866 | # Build the blobs out. |
| 867 | self._BuildBlobs(pack, fdt) |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 868 | |
Simon Glass | 7306b90 | 2012-12-17 15:06:21 -0800 | [diff] [blame] | 869 | self._out.Progress('Packing image') |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 870 | if gbb: |
Simon Glass | e76bf7b | 2012-03-13 15:34:41 -0700 | [diff] [blame] | 871 | pack.RequireAllEntries() |
Hung-Te Lin | a7462e7 | 2011-07-27 19:17:10 +0800 | [diff] [blame] | 872 | fwid = '.'.join([ |
Simon Glass | 02d124a | 2012-03-02 14:47:20 -0800 | [diff] [blame] | 873 | re.sub('[ ,]+', '_', fdt.GetString('/', 'model')), |
Hung-Te Lin | a7462e7 | 2011-07-27 19:17:10 +0800 | [diff] [blame] | 874 | self._tools.GetChromeosVersion()]) |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 875 | self._out.Notice('Firmware ID: %s' % fwid) |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 876 | pack.AddProperty('fwid', fwid) |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 877 | pack.AddProperty('keydir', self._keydir) |
Simon Glass | c90cf58 | 2012-03-13 15:40:47 -0700 | [diff] [blame] | 878 | |
| 879 | pack.CheckProperties() |
Simon Glass | 8884b98 | 2012-06-21 12:41:41 -0700 | [diff] [blame] | 880 | |
| 881 | # Record position and size of all blob members in the FDT |
Gabe Black | cc22d77 | 2013-02-04 23:12:02 -0800 | [diff] [blame] | 882 | pack.UpdateBlobPositionsAndHashes(fdt) |
Simon Glass | 8884b98 | 2012-06-21 12:41:41 -0700 | [diff] [blame] | 883 | |
Simon Glass | 6207efe | 2012-12-17 15:04:36 -0800 | [diff] [blame] | 884 | # Make a copy of the fdt for the bootstub |
| 885 | fdt_data = self._tools.ReadFile(fdt.fname) |
Vadim Bendebury | 466a7d8 | 2014-12-22 10:08:58 -0800 | [diff] [blame] | 886 | if self.uboot_fname: |
| 887 | uboot_data = self._tools.ReadFile(self.uboot_fname) |
| 888 | uboot_copy = os.path.join(self._tools.outdir, 'u-boot.bin') |
| 889 | self._tools.WriteFile(uboot_copy, uboot_data) |
Simon Glass | 6207efe | 2012-12-17 15:04:36 -0800 | [diff] [blame] | 890 | |
Vadim Bendebury | 466a7d8 | 2014-12-22 10:08:58 -0800 | [diff] [blame] | 891 | uboot_dtb = os.path.join(self._tools.outdir, 'u-boot-dtb.bin') |
| 892 | self._tools.WriteFile(uboot_dtb, uboot_data + fdt_data) |
Simon Glass | 6207efe | 2012-12-17 15:04:36 -0800 | [diff] [blame] | 893 | |
Simon Glass | a10282a | 2013-01-08 17:06:41 -0800 | [diff] [blame] | 894 | # Fix up the coreboot image here, since we can't do this until we have |
| 895 | # a final device tree binary. |
Patrick Georgi | 50561e8 | 2016-10-06 18:45:05 +0200 | [diff] [blame] | 896 | bootstub = pack.GetProperty('coreboot') |
| 897 | fdt = fdt.Copy(os.path.join(self._tools.outdir, 'bootstub.dtb')) |
| 898 | if self.coreboot_elf: |
| 899 | self._tools.Run('cbfstool', [bootstub, 'add-payload', '-f', |
| 900 | self.coreboot_elf, '-n', 'fallback/payload', '-c', 'lzma']) |
| 901 | elif self.uboot_fname: |
| 902 | text_base = 0x1110000 |
Simon Glass | 0a7cf11 | 2013-05-21 23:08:21 -0700 | [diff] [blame] | 903 | |
Patrick Georgi | 50561e8 | 2016-10-06 18:45:05 +0200 | [diff] [blame] | 904 | # This is the the 'movw $GD_FLG_COLD_BOOT, %bx' instruction |
| 905 | # 1110015: 66 bb 00 01 mov $0x100,%bx |
| 906 | marker = struct.pack('<L', 0x0100bb66) |
| 907 | pos = uboot_data.find(marker) |
| 908 | if pos == -1 or pos > 0x100: |
| 909 | raise ValueError('Cannot find U-Boot cold boot entry point') |
| 910 | entry = text_base + pos |
| 911 | self._out.Notice('U-Boot entry point %#08x' % entry) |
| 912 | self._tools.Run('cbfstool', [bootstub, 'add-flat-binary', '-f', |
| 913 | uboot_dtb, '-n', 'fallback/payload', '-c', 'lzma', |
| 914 | '-l', '%#x' % text_base, '-e', '%#x' % entry]) |
| 915 | self._tools.Run('cbfstool', [bootstub, 'add', '-f', fdt.fname, |
| 916 | '-n', 'u-boot.dtb', '-t', '0xac']) |
| 917 | data = self._tools.ReadFile(bootstub) |
| 918 | bootstub_copy = os.path.join(self._tools.outdir, 'coreboot-8mb.rom') |
| 919 | self._tools.WriteFile(bootstub_copy, data) |
Vadim Bendebury | 9f36e71 | 2014-06-12 13:37:59 -0700 | [diff] [blame] | 920 | |
Patrick Georgi | 50561e8 | 2016-10-06 18:45:05 +0200 | [diff] [blame] | 921 | # Use offset and size from fmap.dts to extract CBFS area from coreboot.rom |
| 922 | cbfs_offset, cbfs_size = fdt.GetFlashPart('ro', 'boot') |
| 923 | self._tools.WriteFile(bootstub, data[cbfs_offset:cbfs_offset+cbfs_size]) |
Simon Glass | cbc8355 | 2012-07-23 15:26:22 +0100 | [diff] [blame] | 924 | |
Simon Glass | 208ad95 | 2013-02-10 11:16:46 -0800 | [diff] [blame] | 925 | pack.AddProperty('fdtmap', fdt.fname) |
Simon Glass | c90cf58 | 2012-03-13 15:40:47 -0700 | [diff] [blame] | 926 | image = os.path.join(self._tools.outdir, 'image.bin') |
| 927 | pack.PackImage(self._tools.outdir, image) |
| 928 | pack.AddProperty('image', image) |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 929 | |
Simon Glass | 439fe7a | 2012-03-09 16:19:34 -0800 | [diff] [blame] | 930 | image = pack.GetProperty('image') |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 931 | self._tools.OutputSize('Final image', image) |
Simon Glass | c90cf58 | 2012-03-13 15:40:47 -0700 | [diff] [blame] | 932 | return image, pack |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 933 | |
Simon Glass | dedda6f | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 934 | def SelectFdt(self, fdt_fname, use_defaults): |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 935 | """Select an FDT to control the firmware bundling |
| 936 | |
Simon Glass | dedda6f | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 937 | We make a copy of this which will include any on-the-fly changes we want |
| 938 | to make. |
| 939 | |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 940 | Args: |
| 941 | fdt_fname: The filename of the fdt to use. |
Simon Glass | dedda6f | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 942 | use_defaults: True to use a default FDT name if available, and to add |
| 943 | a full path to the provided filename if necessary. |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 944 | |
Simon Glass | c0f3dc6 | 2011-08-09 14:19:05 -0700 | [diff] [blame] | 945 | Returns: |
| 946 | The Fdt object of the original fdt file, which we will not modify. |
| 947 | |
Simon Glass | dedda6f | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 948 | Raises: |
| 949 | ValueError if no FDT is provided (fdt_fname is None and use_defaults is |
| 950 | False). |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 951 | """ |
Simon Glass | dedda6f | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 952 | if use_defaults: |
| 953 | fdt_fname = self._CheckFdtFilename(fdt_fname) |
Simon Glass | 22f39fb | 2013-02-09 13:44:14 -0800 | [diff] [blame] | 954 | if not fdt_fname: |
| 955 | raise ValueError('Please provide an FDT filename') |
| 956 | fdt = Fdt(self._tools, fdt_fname) |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 957 | self._fdt_fname = fdt_fname |
Simon Glass | c3e42c3 | 2012-12-17 15:00:04 -0800 | [diff] [blame] | 958 | |
Patrick Georgi | 8126307 | 2016-09-09 22:33:06 +0200 | [diff] [blame] | 959 | fdt.Compile(None) |
Simon Glass | e53abbc | 2013-08-21 22:29:55 -0600 | [diff] [blame] | 960 | fdt = fdt.Copy(os.path.join(self._tools.outdir, 'updated.dtb')) |
| 961 | |
Patrick Georgi | cebb5a2 | 2016-08-15 19:07:02 +0200 | [diff] [blame] | 962 | if fdt.GetProp('/flash', 'reg', ''): |
| 963 | raise ValueError('fmap.dts /flash is deprecated. Use chromeos.fmd') |
| 964 | |
| 965 | # fill in /flash from binary fmap |
| 966 | # ignore "read-only" attribute, that isn't used anywhere |
| 967 | fmap_blob = open(self.coreboot_fname).read() |
| 968 | f = fmap.fmap_decode(fmap_blob) |
| 969 | fdt.PutString('/flash', 'compatible', 'chromeos,flashmap') |
| 970 | fdt.PutIntList('/flash', 'reg', [f['base'], f['size']]) |
| 971 | for area in f['areas']: |
| 972 | label = re.sub('_', '-', area['name']).lower() |
| 973 | fdt_path = '/flash/' + label |
| 974 | slot=label[-1] |
| 975 | if label == 'gbb': |
| 976 | fdt_path = '/flash/ro-gbb' |
| 977 | fdt.PutString(fdt_path, 'type', 'blob gbb') |
| 978 | elif label == 'fmap': |
| 979 | fdt_path = '/flash/ro-fmap' |
| 980 | fdt.PutString(fdt_path, 'type', 'fmap') |
| 981 | fdt.PutIntList(fdt_path, 'ver-major', [1]) |
| 982 | fdt.PutIntList(fdt_path, 'ver-minor', [0]) |
Julius Werner | bcf0c94 | 2016-09-07 16:57:40 -0700 | [diff] [blame] | 983 | elif label == 'bootblock': |
Patrick Georgi | df77271 | 2016-10-05 16:24:20 +0200 | [diff] [blame] | 984 | fdt.PutString(fdt_path, 'type', 'blob bootblock') |
Patrick Georgi | cebb5a2 | 2016-08-15 19:07:02 +0200 | [diff] [blame] | 985 | elif label == 'coreboot': |
Patrick Georgi | df77271 | 2016-10-05 16:24:20 +0200 | [diff] [blame] | 986 | fdt_path = '/flash/ro-boot' |
| 987 | fdt.PutString(fdt_path, 'type', 'blob coreboot') |
Patrick Georgi | cebb5a2 | 2016-08-15 19:07:02 +0200 | [diff] [blame] | 988 | elif label == 'si-desc': |
| 989 | fdt.PutString(fdt_path, 'type', 'ifd') |
| 990 | elif label == 'rw-shared': |
| 991 | fdt_path = '/flash/shared-section' |
| 992 | elif label == 'rw-section-'+slot: |
| 993 | fdt_path = '/flash/rw-'+slot |
| 994 | elif label == 'rw-legacy' and self.seabios_fname: |
| 995 | fdt.PutString(fdt_path, 'type', 'blob legacy') |
| 996 | elif label in ['rw-mrc-cache', 'rw-elog', 'rw-legacy', |
| 997 | 'rw-vpd', 'rw-unused', 'ro-vpd', 'ro-unused', |
| 998 | 'ro-frid-pad', 'bios-unusable', 'device-extension', |
| 999 | 'unused-hole', 'rw-gpt-primary', 'rw-gpt-secondary', |
| 1000 | 'rw-nvram', 'ro-unused-1', 'ro-unused-2']: |
| 1001 | fdt.PutString(fdt_path, 'type', 'wiped') |
| 1002 | fdt.PutIntList(fdt_path, 'wipe-value', [0xff]) |
| 1003 | elif label == 'shared-data': |
| 1004 | fdt.PutString(fdt_path, 'type', 'wiped') |
| 1005 | fdt.PutIntList(fdt_path, 'wipe-value', [0]) |
| 1006 | elif label == 'vblock-dev': |
| 1007 | fdt_path = '/flash/rw-vblock-dev' |
| 1008 | fdt.PutString(fdt_path, 'type', 'wiped') |
| 1009 | fdt.PutIntList(fdt_path, 'wipe-value', [0xff]) |
| 1010 | elif label[:-1] == 'vblock-': |
| 1011 | fdt_path = '/flash/rw-'+slot+'-vblock' |
| 1012 | fdt.PutString(fdt_path, 'type', 'keyblock cbfs/rw/'+slot+'-boot') |
| 1013 | fdt.PutString(fdt_path, 'keyblock', 'firmware.keyblock') |
| 1014 | fdt.PutString(fdt_path, 'signprivate', 'firmware_data_key.vbprivk') |
| 1015 | fdt.PutString(fdt_path, 'kernelkey', 'kernel_subkey.vbpubk') |
| 1016 | fdt.PutIntList(fdt_path, 'version', [1]) |
| 1017 | fdt.PutIntList(fdt_path, 'preamble-flags', [0]) |
| 1018 | elif label[:-1] == 'fw-main-': |
| 1019 | fdt_path = '/flash/rw-'+slot+'-boot' |
| 1020 | fdt.PutString(fdt_path, 'type', 'blob cbfs/rw/'+slot+'-boot') |
| 1021 | elif label[:-1] == 'rw-fwid-': |
| 1022 | fdt_path = '/flash/rw-'+slot+'-firmware-id' |
| 1023 | fdt.PutString(fdt_path, 'type', 'blobstring fwid') |
| 1024 | elif label == 'ro-frid': |
| 1025 | fdt_path = '/flash/ro-firmware-id' |
| 1026 | fdt.PutString(fdt_path, 'type', 'blobstring fwid') |
| 1027 | elif label == 'ifwi': |
| 1028 | fdt_path = '/flash/ro-ifwi' |
| 1029 | fdt.PutString(fdt_path, 'type', 'blob ifwi') |
| 1030 | elif label == 'sign-cse': |
| 1031 | fdt_path = '/flash/ro-sig' |
| 1032 | fdt.PutString(fdt_path, 'type', 'blob sig2') |
| 1033 | # white list for empty regions |
Patrick Georgi | 56dc920 | 2016-09-07 21:12:04 +0200 | [diff] [blame] | 1034 | elif label in ['bootblock', 'misc-rw', 'ro-section', 'rw-environment', |
| 1035 | 'rw-gpt', 'si-all', 'si-bios', 'si-me', 'wp-ro']: |
Patrick Georgi | cebb5a2 | 2016-08-15 19:07:02 +0200 | [diff] [blame] | 1036 | pass |
| 1037 | else: |
| 1038 | raise ValueError('encountered label "'+label+'" in binary fmap. '+ |
| 1039 | 'Check chromeos.fmd') |
| 1040 | fdt.PutString(fdt_path, 'label', label) |
| 1041 | fdt.PutIntList(fdt_path, 'reg', [area['offset'], area['size']]) |
| 1042 | |
Simon Glass | 7df773b | 2013-08-25 18:02:29 -0600 | [diff] [blame] | 1043 | # Remember our board type. |
| 1044 | fdt.PutString('/chromeos-config', 'board', self._board) |
| 1045 | |
Simon Glass | e53abbc | 2013-08-21 22:29:55 -0600 | [diff] [blame] | 1046 | self.fdt = fdt |
| 1047 | return fdt |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 1048 | |
Simon Glass | c90cf58 | 2012-03-13 15:40:47 -0700 | [diff] [blame] | 1049 | def Start(self, hardware_id, output_fname, show_map): |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 1050 | """This creates a firmware bundle according to settings provided. |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 1051 | |
| 1052 | - Checks options, tools, output directory, fdt. |
| 1053 | - Creates GBB and image. |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 1054 | |
| 1055 | Args: |
Simon Glass | 5657757 | 2011-07-19 11:08:06 +1200 | [diff] [blame] | 1056 | hardware_id: Hardware ID to use for this board. If None, then the |
| 1057 | default from the Fdt will be used |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 1058 | output_fname: Output filename for the image. If this is not None, then |
| 1059 | the final image will be copied here. |
Simon Glass | c90cf58 | 2012-03-13 15:40:47 -0700 | [diff] [blame] | 1060 | show_map: Show a flash map, with each area's name and position |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 1061 | |
| 1062 | Returns: |
| 1063 | Filename of the resulting image (not the output_fname copy). |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 1064 | """ |
Vadim Bendebury | 5baeec1 | 2013-04-02 13:01:22 -0700 | [diff] [blame] | 1065 | if self._small or self.fdt.GetProp('/config', 'nogbb', 'any') != 'any': |
| 1066 | gbb = '' # Building a small image or `nogbb' is requested in device tree. |
| 1067 | else: |
Simon Glass | 5657757 | 2011-07-19 11:08:06 +1200 | [diff] [blame] | 1068 | gbb = self._CreateGoogleBinaryBlock(hardware_id) |
Simon Glass | 89b86b8 | 2011-07-17 23:49:49 -0700 | [diff] [blame] | 1069 | |
| 1070 | # This creates the actual image. |
Simon Glass | c90cf58 | 2012-03-13 15:40:47 -0700 | [diff] [blame] | 1071 | image, pack = self._CreateImage(gbb, self.fdt) |
| 1072 | if show_map: |
| 1073 | pack.ShowMap() |
Simon Glass | 290a180 | 2011-07-17 13:54:32 -0700 | [diff] [blame] | 1074 | if output_fname: |
| 1075 | shutil.copyfile(image, output_fname) |
| 1076 | self._out.Notice("Output image '%s'" % output_fname) |
Simon Glass | 794217e | 2012-06-07 11:40:37 -0700 | [diff] [blame] | 1077 | return image, pack.props |