blob: ffc06554ab9c03ebd5514ae67b83c3f084f763af [file] [log] [blame]
Simon Glass89b86b82011-07-17 23:49:49 -07001# 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
5"""This module builds a firmware image for a tegra-based board.
6
7This modules uses a few rudimentary other libraries for its activity.
8
9Here are the names we give to the various files we deal with. It is important
10to 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 Glassceff3ff2012-04-04 11:23:45 -070019import glob
Gabe Blackcdbdfe12013-02-06 05:37:52 -080020import hashlib
Simon Glass89b86b82011-07-17 23:49:49 -070021import os
22import re
23
Simon Glass89b86b82011-07-17 23:49:49 -070024from fdt import Fdt
25from pack_firmware import PackFirmware
26import shutil
Simon Glass7c2d5572011-11-15 14:47:08 -080027import struct
Simon Glass439fe7a2012-03-09 16:19:34 -080028from tools import CmdError
Vadim Bendeburyb12e3352013-06-08 17:25:19 -070029from exynos import ExynosBl2
Simon Glass89b86b82011-07-17 23:49:49 -070030
31# This data is required by bmpblk_utility. Does it ever change?
32# It was stored with the chromeos-bootimage ebuild, but we want
33# this utility to work outside the chroot.
34yaml_data = '''
35bmpblock: 1.0
36
37images:
38 devmode: DeveloperBmp/DeveloperBmp.bmp
39 recovery: RecoveryBmp/RecoveryBmp.bmp
40 rec_yuck: RecoveryNoOSBmp/RecoveryNoOSBmp.bmp
41 rec_insert: RecoveryMissingOSBmp/RecoveryMissingOSBmp.bmp
42
43screens:
44 dev_en:
45 - [0, 0, devmode]
46 rec_en:
47 - [0, 0, recovery]
48 yuck_en:
49 - [0, 0, rec_yuck]
50 ins_en:
51 - [0, 0, rec_insert]
52
53localizations:
54 - [ dev_en, rec_en, yuck_en, ins_en ]
55'''
56
Simon Glass0c54ba52012-11-06 12:36:43 -080057# Default flash maps for various boards we support.
58# These are used when no fdt is provided (e.g. upstream U-Boot with no
59# fdt. Each is a list of nodes.
60default_flashmaps = {
Simon Glass76958702012-11-08 13:07:53 -080061 'tegra' : [
62 {
Simon Glass0c54ba52012-11-06 12:36:43 -080063 'node' : 'ro-boot',
64 'label' : 'boot-stub',
65 'size' : 512 << 10,
66 'read-only' : True,
67 'type' : 'blob signed',
68 'required' : True
69 }
70 ],
71 'daisy' : [
72 {
73 'node' : 'pre-boot',
74 'label' : "bl1 pre-boot",
75 'size' : 0x2000,
76 'read-only' : True,
77 'filename' : "e5250.nbl1.bin",
78 'type' : "blob exynos-bl1",
79 'required' : True,
80 }, {
81 'node' : 'spl',
82 'label' : "bl2 spl",
83 'size' : 0x4000,
84 'read-only' : True,
85 'filename' : "bl2.bin",
86 'type' : "blob exynos-bl2 boot,dtb",
87 'required' : True,
88 }, {
89 'node' : 'ro-boot',
90 'label' : "u-boot",
91 'size' : 0x9a000,
92 'read-only' : True,
93 'type' : "blob boot,dtb",
94 'required' : True,
95 }
Simon Glass76958702012-11-08 13:07:53 -080096 ],
97 'link' : [
98 {
99 'node' : 'si-all',
100 'label' : 'si-all',
101 'reg' : '%d %d' % (0x00000000, 0x00200000),
102 'type' : 'ifd',
103 'required' : True,
104 }, {
105 'node' : 'ro-boot',
106 'label' : 'boot-stub',
107 'reg' : '%d %d' % (0x00700000, 0x00100000),
108 'read-only' : True,
109 'type' : 'blob coreboot',
110 'required' : True,
111 }
Simon Glassf2534222013-03-20 15:42:02 -0700112 ],
113 'peach' : [
114 {
115 'node' : 'pre-boot',
116 'label' : "bl1 pre-boot",
117 'size' : 0x2000,
118 'read-only' : True,
119 'filename' : "e5420.nbl1.bin",
120 'type' : "blob exynos-bl1",
121 'required' : True,
122 }, {
123 'node' : 'spl',
124 'label' : "bl2 spl",
125 'size' : 0x4000,
126 'read-only' : True,
127 'filename' : "bl2.bin",
128 'type' : "blob exynos-bl2 boot,dtb",
129 'required' : True,
130 }, {
131 'node' : 'ro-boot',
132 'label' : "u-boot",
133 'size' : 0x9a000,
134 'read-only' : True,
135 'type' : "blob boot,dtb",
136 'required' : True,
137 }
138 ],
Simon Glass0c54ba52012-11-06 12:36:43 -0800139}
140
141
Simon Glass4a887b12012-10-23 16:29:03 -0700142# Build GBB flags.
143# (src/platform/vboot_reference/firmware/include/gbb_header.h)
144gbb_flag_properties = {
145 'dev-screen-short-delay': 0x00000001,
146 'load-option-roms': 0x00000002,
147 'enable-alternate-os': 0x00000004,
148 'force-dev-switch-on': 0x00000008,
149 'force-dev-boot-usb': 0x00000010,
150 'disable-fw-rollback-check': 0x00000020,
151 'enter-triggers-tonorm': 0x00000040,
152 'force-dev-boot-legacy': 0x00000080,
153}
154
Simon Glass49b026b2013-04-26 16:38:42 -0700155# Maps board name to Exynos product number
156type_to_model = {
157 'peach' : '5420',
158 'daisy' : '5250'
159}
160
Simon Glass5076a7f2012-10-23 16:31:54 -0700161def ListGoogleBinaryBlockFlags():
162 """Print out a list of GBB flags."""
163 print ' %-30s %s' % ('Available GBB flags:', 'Hex')
164 for name, value in gbb_flag_properties.iteritems():
165 print ' %-30s %02x' % (name, value)
166
Simon Glass89b86b82011-07-17 23:49:49 -0700167class Bundle:
Simon Glass290a1802011-07-17 13:54:32 -0700168 """This class encapsulates the entire bundle firmware logic.
Simon Glass89b86b82011-07-17 23:49:49 -0700169
Simon Glass290a1802011-07-17 13:54:32 -0700170 Sequence of events:
171 bundle = Bundle(tools.Tools(), cros_output.Output())
172 bundle.SetDirs(...)
173 bundle.SetFiles(...)
174 bundle.SetOptions(...)
175 bundle.SelectFdt(fdt.Fdt('filename.dtb')
Simon Glassa4934b72012-05-09 13:35:02 -0700176 .. can call bundle.AddConfigList(), AddEnableList() if required
Simon Glass290a1802011-07-17 13:54:32 -0700177 bundle.Start(...)
Simon Glass89b86b82011-07-17 23:49:49 -0700178
Simon Glass290a1802011-07-17 13:54:32 -0700179 Public properties:
180 fdt: The fdt object that we use for building our image. This wil be the
181 one specified by the user, except that we might add config options
182 to it. This is set up by SelectFdt() which must be called before
183 bundling starts.
184 uboot_fname: Full filename of the U-Boot binary we use.
185 bct_fname: Full filename of the BCT file we use.
Simon Glass559b6612012-05-23 13:28:45 -0700186 spl_source: Source device to load U-Boot from, in SPL:
187 straps: Select device according to CPU strap pins
188 spi: Boot from SPI
189 emmc: Boot from eMMC
Simon Glass23988ae2012-03-23 16:55:22 -0700190
191 Private attributes:
192 _small: True to create a 'small' signed U-Boot, False to produce a
193 full image. The small U-Boot is enough to boot but will not have
194 access to GBB, RW U-Boot, etc.
Simon Glass290a1802011-07-17 13:54:32 -0700195 """
Simon Glass89b86b82011-07-17 23:49:49 -0700196
Simon Glass290a1802011-07-17 13:54:32 -0700197 def __init__(self, tools, output):
198 """Set up a new Bundle object.
Simon Glass89b86b82011-07-17 23:49:49 -0700199
Simon Glass290a1802011-07-17 13:54:32 -0700200 Args:
201 tools: A tools.Tools object to use for external tools.
202 output: A cros_output.Output object to use for program output.
Simon Glass89b86b82011-07-17 23:49:49 -0700203 """
Simon Glass290a1802011-07-17 13:54:32 -0700204 self._tools = tools
205 self._out = output
206
207 # Set up the things we need to know in order to operate.
208 self._board = None # Board name, e.g. tegra2_seaboard.
209 self._fdt_fname = None # Filename of our FDT.
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700210 self._force_rw = None
211 self._gbb_flags = None
212 self._keydir = None
213 self._small = False
Simon Glass290a1802011-07-17 13:54:32 -0700214 self.bct_fname = None # Filename of our BCT file.
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700215 self.blobs = {} # Table of (type, filename) of arbitrary blobs
Hung-Te Lin5b649382011-08-03 15:01:16 +0800216 self.bmpblk_fname = None # Filename of our Bitmap Block
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700217 self.coreboot_elf = None
Stefan Reinauer8d79d362011-08-16 14:20:43 -0700218 self.coreboot_fname = None # Filename of our coreboot binary.
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700219 self.ecro_fname = None # Filename of EC read-only file
220 self.ecrw_fname = None # Filename of EC file
Simon Glass7e199222012-03-13 15:51:18 -0700221 self.exynos_bl1 = None # Filename of Exynos BL1 (pre-boot)
222 self.exynos_bl2 = None # Filename of Exynos BL2 (SPL)
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700223 self.fdt = None # Our Fdt object.
224 self.kernel_fname = None
225 self.postload_fname = None
226 self.seabios_fname = None # Filename of our SeaBIOS payload.
Simon Glass07267952012-06-08 12:45:13 -0700227 self.skeleton_fname = None # Filename of Coreboot skeleton file
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700228 self.uboot_fname = None # Filename of our U-Boot binary.
Simon Glass290a1802011-07-17 13:54:32 -0700229
230 def SetDirs(self, keydir):
231 """Set up directories required for Bundle.
232
233 Args:
234 keydir: Directory containing keys to use for signing firmware.
235 """
236 self._keydir = keydir
237
Simon Glass6dcc2f22011-07-28 15:26:49 +1200238 def SetFiles(self, board, bct, uboot=None, bmpblk=None, coreboot=None,
Simon Glassa10282a2013-01-08 17:06:41 -0800239 coreboot_elf=None,
Simon Glass07267952012-06-08 12:45:13 -0700240 postload=None, seabios=None, exynos_bl1=None, exynos_bl2=None,
Che-Liang Chiou3bc344c2013-02-21 15:18:03 -0800241 skeleton=None, ecrw=None, ecro=None, kernel=None, blobs=None):
Simon Glass290a1802011-07-17 13:54:32 -0700242 """Set up files required for Bundle.
243
244 Args:
245 board: The name of the board to target (e.g. tegra2_seaboard).
246 uboot: The filename of the u-boot.bin image to use.
247 bct: The filename of the binary BCT file to use.
Hung-Te Lin5b649382011-08-03 15:01:16 +0800248 bmpblk: The filename of bitmap block file to use.
Simon Glassa10282a2013-01-08 17:06:41 -0800249 coreboot: The filename of the coreboot image to use (on x86).
250 coreboot_elf: If not none, the ELF file to add as a Coreboot payload.
Simon Glass6dcc2f22011-07-28 15:26:49 +1200251 postload: The filename of the u-boot-post.bin image to use.
Vincent Palatinf7286772011-10-12 14:31:53 -0700252 seabios: The filename of the SeaBIOS payload to use if any.
Simon Glass07267952012-06-08 12:45:13 -0700253 exynos_bl1: The filename of the exynos BL1 file
254 exynos_bl2: The filename of the exynos BL2 file (U-Boot spl)
255 skeleton: The filename of the coreboot skeleton file.
Simon Glassbe0bc002012-08-16 12:50:48 -0700256 ecrw: The filename of the EC (Embedded Controller) read-write file.
257 ecro: The filename of the EC (Embedded Controller) read-only file.
Simon Glassde9c8072012-07-02 22:29:02 -0700258 kernel: The filename of the kernel file if any.
Che-Liang Chiou3bc344c2013-02-21 15:18:03 -0800259 blobs: List of (type, filename) of arbitrary blobs.
Simon Glass290a1802011-07-17 13:54:32 -0700260 """
261 self._board = board
262 self.uboot_fname = uboot
263 self.bct_fname = bct
Hung-Te Lin5b649382011-08-03 15:01:16 +0800264 self.bmpblk_fname = bmpblk
Stefan Reinauer8d79d362011-08-16 14:20:43 -0700265 self.coreboot_fname = coreboot
Simon Glassa10282a2013-01-08 17:06:41 -0800266 self.coreboot_elf = coreboot_elf
Simon Glass6dcc2f22011-07-28 15:26:49 +1200267 self.postload_fname = postload
Vincent Palatinf7286772011-10-12 14:31:53 -0700268 self.seabios_fname = seabios
Simon Glass7e199222012-03-13 15:51:18 -0700269 self.exynos_bl1 = exynos_bl1
270 self.exynos_bl2 = exynos_bl2
Simon Glass07267952012-06-08 12:45:13 -0700271 self.skeleton_fname = skeleton
Simon Glassbe0bc002012-08-16 12:50:48 -0700272 self.ecrw_fname = ecrw
273 self.ecro_fname = ecro
Simon Glassde9c8072012-07-02 22:29:02 -0700274 self.kernel_fname = kernel
Che-Liang Chiou3bc344c2013-02-21 15:18:03 -0800275 self.blobs = dict(blobs or ())
Simon Glass290a1802011-07-17 13:54:32 -0700276
Simon Glass6e486c22012-10-26 15:43:42 -0700277 def SetOptions(self, small, gbb_flags, force_rw=False):
Simon Glass290a1802011-07-17 13:54:32 -0700278 """Set up options supported by Bundle.
279
280 Args:
281 small: Only create a signed U-Boot - don't produce the full packed
282 firmware image. This is useful for devs who want to replace just the
283 U-Boot part while keeping the keys, gbb, etc. the same.
Simon Glass6e486c22012-10-26 15:43:42 -0700284 gbb_flags: Specification for string containing adjustments to make.
285 force_rw: Force firmware into RW mode.
Simon Glass290a1802011-07-17 13:54:32 -0700286 """
287 self._small = small
Simon Glass157c0662012-10-23 13:52:42 -0700288 self._gbb_flags = gbb_flags
Simon Glass6e486c22012-10-26 15:43:42 -0700289 self._force_rw = force_rw
Simon Glass290a1802011-07-17 13:54:32 -0700290
Simon Glass22f39fb2013-02-09 13:44:14 -0800291 def _GetBuildRoot(self):
292 """Get the path to this board's 'firmware' directory.
293
294 Returns:
295 Path to firmware directory, with ## representing the path to the
296 chroot.
297 """
Simon Glass290a1802011-07-17 13:54:32 -0700298 if not self._board:
299 raise ValueError('No board defined - please define a board to use')
Simon Glass22f39fb2013-02-09 13:44:14 -0800300 return os.path.join('##', 'build', self._board, 'firmware')
301
302 def _CheckFdtFilename(self, fname):
303 """Check provided FDT filename and return the correct name if needed.
304
305 Where the filename lacks a path, add a default path for this board.
306 Where no FDT filename is provided, select a default one for this board.
307
308 Args:
309 fname: Proposed FDT filename.
310
311 Returns:
312 Selected FDT filename, after validation.
313 """
314 build_root = self._GetBuildRoot()
Simon Glass881964d2012-04-04 11:34:09 -0700315 dir_name = os.path.join(build_root, 'dts')
Simon Glass22f39fb2013-02-09 13:44:14 -0800316 if not fname:
Simon Glassceff3ff2012-04-04 11:23:45 -0700317 # Figure out where the file should be, and the name we expect.
Simon Glassceff3ff2012-04-04 11:23:45 -0700318 base_name = re.sub('_', '-', self._board)
319
320 # In case the name exists with a prefix or suffix, find it.
321 wildcard = os.path.join(dir_name, '*%s*.dts' % base_name)
322 found_list = glob.glob(self._tools.Filename(wildcard))
323 if len(found_list) == 1:
Simon Glass22f39fb2013-02-09 13:44:14 -0800324 fname = found_list[0]
Simon Glassceff3ff2012-04-04 11:23:45 -0700325 else:
326 # We didn't find anything definite, so set up our expected name.
Simon Glass22f39fb2013-02-09 13:44:14 -0800327 fname = os.path.join(dir_name, '%s.dts' % base_name)
Simon Glassceff3ff2012-04-04 11:23:45 -0700328
Simon Glass881964d2012-04-04 11:34:09 -0700329 # Convert things like 'exynos5250-daisy' into a full path.
Simon Glass22f39fb2013-02-09 13:44:14 -0800330 root, ext = os.path.splitext(fname)
Simon Glass881964d2012-04-04 11:34:09 -0700331 if not ext and not os.path.dirname(root):
Simon Glass22f39fb2013-02-09 13:44:14 -0800332 fname = os.path.join(dir_name, '%s.dts' % root)
333 return fname
334
335 def CheckOptions(self):
336 """Check provided options and select defaults."""
337 build_root = self._GetBuildRoot()
Simon Glass881964d2012-04-04 11:34:09 -0700338
Simon Glass49b026b2013-04-26 16:38:42 -0700339 board_type = self._board.split('_')[0]
340 model = type_to_model.get(board_type)
341
Simon Glass290a1802011-07-17 13:54:32 -0700342 if not self.uboot_fname:
343 self.uboot_fname = os.path.join(build_root, 'u-boot.bin')
344 if not self.bct_fname:
345 self.bct_fname = os.path.join(build_root, 'bct', 'board.bct')
Simon Glass2a7f0b32011-08-26 11:25:17 -0700346 if not self.bmpblk_fname:
David Hendricksbdecc542012-08-21 13:53:58 -0700347 self.bmpblk_fname = os.path.join(build_root, 'bmpblk.bin')
Simon Glass49b026b2013-04-26 16:38:42 -0700348 if model:
349 if not self.exynos_bl1:
350 self.exynos_bl1 = os.path.join(build_root, 'E%s.nbl1.bin' % model)
351 if not self.exynos_bl2:
352 self.exynos_bl2 = os.path.join(build_root, 'smdk%s-spl.bin' % model)
Simon Glass07267952012-06-08 12:45:13 -0700353 if not self.coreboot_fname:
354 self.coreboot_fname = os.path.join(build_root, 'coreboot.rom')
355 if not self.skeleton_fname:
Stefan Reinauer728be822012-10-02 16:54:09 -0700356 self.skeleton_fname = os.path.join(build_root, 'coreboot.rom')
Stefan Reinauer9ad54842012-10-10 12:25:23 -0700357 if not self.seabios_fname:
358 self.seabios_fname = 'seabios.cbfs'
Simon Glassbe0bc002012-08-16 12:50:48 -0700359 if not self.ecrw_fname:
360 self.ecrw_fname = os.path.join(build_root, 'ec.RW.bin')
361 if not self.ecro_fname:
362 self.ecro_fname = os.path.join(build_root, 'ec.RO.bin')
Simon Glass89b86b82011-07-17 23:49:49 -0700363
Simon Glass75759302012-03-15 20:26:53 -0700364 def GetFiles(self):
365 """Get a list of files that we know about.
366
367 This is the opposite of SetFiles except that we may have put in some
368 default names. It returns a dictionary containing the filename for
369 each of a number of pre-defined files.
370
371 Returns:
372 Dictionary, with one entry for each file.
373 """
374 file_list = {
375 'bct' : self.bct_fname,
376 'exynos-bl1' : self.exynos_bl1,
377 'exynos-bl2' : self.exynos_bl2,
378 }
379 return file_list
380
Simon Glass4a887b12012-10-23 16:29:03 -0700381 def DecodeGBBFlagsFromFdt(self):
382 """Get Google Binary Block flags from the FDT.
383
384 These should be in the chromeos-config node, like this:
385
386 chromeos-config {
387 gbb-flag-dev-screen-short-delay;
388 gbb-flag-force-dev-switch-on;
389 gbb-flag-force-dev-boot-usb;
390 gbb-flag-disable-fw-rollback-check;
391 };
392
393 Returns:
394 GBB flags value from FDT.
395 """
396 chromeos_config = self.fdt.GetProps("/chromeos-config")
397 gbb_flags = 0
398 for name in chromeos_config:
399 if name.startswith('gbb-flag-'):
400 flag_value = gbb_flag_properties.get(name[9:])
401 if flag_value:
402 gbb_flags |= flag_value
403 self._out.Notice("FDT: Enabling %s." % name)
404 else:
405 raise ValueError("FDT contains invalid GBB flags '%s'" % name)
406 return gbb_flags
407
Simon Glass157c0662012-10-23 13:52:42 -0700408 def DecodeGBBFlagsFromOptions(self, gbb_flags, adjustments):
409 """Decode ajustments to the provided GBB flags.
410
411 We support three options:
412
413 hex value: c2
414 defined value: force-dev-boot-usb,load-option-roms
415 adjust default value: -load-option-roms,+force-dev-boot-usb
416
417 The last option starts from the passed-in GBB flags and adds or removes
418 flags.
419
420 Args:
421 gbb_flags: Base (default) FDT flags.
422 adjustments: String containing adjustments to make.
423
424 Returns:
425 Updated FDT flags.
426 """
427 use_base_value = True
428 if adjustments:
429 try:
430 return int(adjustments, base=16)
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700431 except (ValueError, TypeError):
Simon Glass157c0662012-10-23 13:52:42 -0700432 pass
433 for flag in adjustments.split(','):
434 oper = None
435 if flag[0] in ['-', '+']:
436 oper = flag[0]
437 flag = flag[1:]
438 value = gbb_flag_properties.get(flag)
439 if not value:
440 raise ValueError("Invalid GBB flag '%s'" % flag)
441 if oper == '+':
442 gbb_flags |= value
Simon Glass84816582012-11-20 10:53:10 -0800443 self._out.Notice("Cmdline: Enabling %s." % flag)
Simon Glass157c0662012-10-23 13:52:42 -0700444 elif oper == '-':
445 gbb_flags &= ~value
Simon Glass84816582012-11-20 10:53:10 -0800446 self._out.Notice("Cmdline: Disabling %s." % flag)
Simon Glass157c0662012-10-23 13:52:42 -0700447 else:
448 if use_base_value:
449 gbb_flags = 0
450 use_base_value = False
Simon Glass84816582012-11-20 10:53:10 -0800451 self._out.Notice('Cmdline: Resetting flags to 0')
Simon Glass157c0662012-10-23 13:52:42 -0700452 gbb_flags |= value
Simon Glass84816582012-11-20 10:53:10 -0800453 self._out.Notice("Cmdline: Enabling %s." % flag)
Simon Glass157c0662012-10-23 13:52:42 -0700454
455 return gbb_flags
456
Simon Glass56577572011-07-19 11:08:06 +1200457 def _CreateGoogleBinaryBlock(self, hardware_id):
Simon Glass89b86b82011-07-17 23:49:49 -0700458 """Create a GBB for the image.
459
Simon Glass56577572011-07-19 11:08:06 +1200460 Args:
461 hardware_id: Hardware ID to use for this board. If None, then the
462 default from the Fdt will be used
463
Simon Glass89b86b82011-07-17 23:49:49 -0700464 Returns:
465 Path of the created GBB file.
Simon Glass89b86b82011-07-17 23:49:49 -0700466 """
Simon Glass56577572011-07-19 11:08:06 +1200467 if not hardware_id:
Simon Glass02d124a2012-03-02 14:47:20 -0800468 hardware_id = self.fdt.GetString('/config', 'hwid')
Simon Glass89b86b82011-07-17 23:49:49 -0700469 gbb_size = self.fdt.GetFlashPartSize('ro', 'gbb')
Simon Glass290a1802011-07-17 13:54:32 -0700470 odir = self._tools.outdir
Simon Glass89b86b82011-07-17 23:49:49 -0700471
Simon Glass4a887b12012-10-23 16:29:03 -0700472 gbb_flags = self.DecodeGBBFlagsFromFdt()
Stefan Reinauer975e68f2012-02-27 13:27:08 -0800473
Simon Glass157c0662012-10-23 13:52:42 -0700474 # Allow command line to override flags
475 gbb_flags = self.DecodeGBBFlagsFromOptions(gbb_flags, self._gbb_flags)
476
Simon Glass4a887b12012-10-23 16:29:03 -0700477 self._out.Notice("GBB flags value %#x" % gbb_flags)
Simon Glass89b86b82011-07-17 23:49:49 -0700478 self._out.Progress('Creating GBB')
479 sizes = [0x100, 0x1000, gbb_size - 0x2180, 0x1000]
480 sizes = ['%#x' % size for size in sizes]
481 gbb = 'gbb.bin'
Simon Glass290a1802011-07-17 13:54:32 -0700482 keydir = self._tools.Filename(self._keydir)
483 self._tools.Run('gbb_utility', ['-c', ','.join(sizes), gbb], cwd=odir)
Simon Glass89b86b82011-07-17 23:49:49 -0700484 self._tools.Run('gbb_utility', ['-s',
Simon Glass56577572011-07-19 11:08:06 +1200485 '--hwid=%s' % hardware_id,
Simon Glass89b86b82011-07-17 23:49:49 -0700486 '--rootkey=%s/root_key.vbpubk' % keydir,
487 '--recoverykey=%s/recovery_key.vbpubk' % keydir,
Simon Glass2a7f0b32011-08-26 11:25:17 -0700488 '--bmpfv=%s' % self._tools.Filename(self.bmpblk_fname),
Stefan Reinauer975e68f2012-02-27 13:27:08 -0800489 '--flags=%d' % gbb_flags,
Simon Glass89b86b82011-07-17 23:49:49 -0700490 gbb],
Simon Glass290a1802011-07-17 13:54:32 -0700491 cwd=odir)
492 return os.path.join(odir, gbb)
Simon Glass89b86b82011-07-17 23:49:49 -0700493
Simon Glasse13ee2c2011-07-28 08:12:28 +1200494 def _SignBootstub(self, bct, bootstub, text_base):
Simon Glass89b86b82011-07-17 23:49:49 -0700495 """Sign an image so that the Tegra SOC will boot it.
496
497 Args:
498 bct: BCT file to use.
499 bootstub: Boot stub (U-Boot + fdt) file to sign.
500 text_base: Address of text base for image.
Simon Glass89b86b82011-07-17 23:49:49 -0700501
502 Returns:
503 filename of signed image.
Simon Glass89b86b82011-07-17 23:49:49 -0700504 """
505 # First create a config file - this is how we instruct cbootimage
Simon Glasse13ee2c2011-07-28 08:12:28 +1200506 signed = os.path.join(self._tools.outdir, 'signed.bin')
Simon Glass89b86b82011-07-17 23:49:49 -0700507 self._out.Progress('Signing Bootstub')
Simon Glasse13ee2c2011-07-28 08:12:28 +1200508 config = os.path.join(self._tools.outdir, 'boot.cfg')
Simon Glass89b86b82011-07-17 23:49:49 -0700509 fd = open(config, 'w')
510 fd.write('Version = 1;\n')
511 fd.write('Redundancy = 1;\n')
512 fd.write('Bctfile = %s;\n' % bct)
Doug Anderson0eeb0742011-09-15 18:11:40 -0700513
514 # TODO(dianders): Right now, we don't have enough space in our flash map
515 # for two copies of the BCT when we're using NAND, so hack it to 1. Not
516 # sure what this does for reliability, but at least things will fit...
517 is_nand = "NvBootDevType_Nand" in self._tools.Run('bct_dump', [bct])
518 if is_nand:
519 fd.write('Bctcopy = 1;\n')
520
Simon Glass89b86b82011-07-17 23:49:49 -0700521 fd.write('BootLoader = %s,%#x,%#x,Complete;\n' % (bootstub, text_base,
522 text_base))
Doug Anderson0eeb0742011-09-15 18:11:40 -0700523
Simon Glass89b86b82011-07-17 23:49:49 -0700524 fd.close()
525
526 self._tools.Run('cbootimage', [config, signed])
527 self._tools.OutputSize('BCT', bct)
528 self._tools.OutputSize('Signed image', signed)
529 return signed
530
Doug Anderson86ce5f42011-07-27 10:40:18 -0700531 def SetBootcmd(self, bootcmd, bootsecure):
Simon Glass290a1802011-07-17 13:54:32 -0700532 """Set the boot command for U-Boot.
Simon Glass89b86b82011-07-17 23:49:49 -0700533
534 Args:
Simon Glass290a1802011-07-17 13:54:32 -0700535 bootcmd: Boot command to use, as a string (if None this this is a nop).
Doug Anderson86ce5f42011-07-27 10:40:18 -0700536 bootsecure: We'll set '/config/bootsecure' to 1 if True and 0 if False.
Simon Glass89b86b82011-07-17 23:49:49 -0700537 """
Simon Glass468d8752012-09-19 16:36:19 -0700538 if bootcmd is not None:
539 if bootcmd == 'none':
540 bootcmd = ''
Simon Glass02d124a2012-03-02 14:47:20 -0800541 self.fdt.PutString('/config', 'bootcmd', bootcmd)
542 self.fdt.PutInteger('/config', 'bootsecure', int(bootsecure))
Simon Glass290a1802011-07-17 13:54:32 -0700543 self._out.Info('Boot command: %s' % bootcmd)
Simon Glass89b86b82011-07-17 23:49:49 -0700544
Simon Glassa4934b72012-05-09 13:35:02 -0700545 def SetNodeEnabled(self, node_name, enabled):
546 """Set whether an node is enabled or disabled.
547
548 This simply sets the 'status' property of a node to "ok", or "disabled".
549
550 The node should either be a full path to the node (like '/uart@10200000')
551 or an alias property.
552
553 Aliases are supported like this:
554
555 aliases {
556 console = "/uart@10200000";
557 };
558
559 pointing to a node:
560
561 uart@10200000 {
Simon Glass4c5066f2012-06-20 16:51:19 -0700562 status = "okay";
Simon Glassa4934b72012-05-09 13:35:02 -0700563 };
564
565 In this case, this function takes the name of the alias ('console' in
566 this case) and updates the status of the node that is pointed to, to
567 either ok or disabled. If the alias does not exist, a warning is
568 displayed.
569
570 Args:
571 node_name: Name of node (e.g. '/uart@10200000') or alias alias
572 (e.g. 'console') to adjust
573 enabled: True to enable, False to disable
574 """
575 # Look up the alias if this is an alias reference
576 if not node_name.startswith('/'):
577 lookup = self.fdt.GetString('/aliases', node_name, '')
578 if not lookup:
579 self._out.Warning("Cannot find alias '%s' - ignoring" % node_name)
580 return
581 node_name = lookup
582 if enabled:
Simon Glass4c5066f2012-06-20 16:51:19 -0700583 status = 'okay'
Simon Glassa4934b72012-05-09 13:35:02 -0700584 else:
585 status = 'disabled'
586 self.fdt.PutString(node_name, 'status', status)
587
588 def AddEnableList(self, enable_list):
589 """Process a list of nodes to enable/disable.
590
591 Args:
592 config_list: List of (node, value) tuples to add to the fdt. For each
593 tuple:
594 node: The fdt node to write to will be <node> or pointed to by
595 /aliases/<node>. We can tell which
596 value: 0 to disable the node, 1 to enable it
Vadim Bendebury507c0012013-06-09 12:49:25 -0700597 Raises:
598 CmdError if a command fails.
Simon Glassa4934b72012-05-09 13:35:02 -0700599 """
600 if enable_list:
601 for node_name, enabled in enable_list:
602 try:
603 enabled = int(enabled)
604 if enabled not in (0, 1):
605 raise ValueError
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700606 except ValueError:
Simon Glassa4934b72012-05-09 13:35:02 -0700607 raise CmdError("Invalid enable option value '%s' "
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700608 "(should be 0 or 1)" % str(enabled))
Simon Glassa4934b72012-05-09 13:35:02 -0700609 self.SetNodeEnabled(node_name, enabled)
610
Simon Glass290a1802011-07-17 13:54:32 -0700611 def AddConfigList(self, config_list, use_int=False):
612 """Add a list of config items to the fdt.
613
614 Normally these values are written to the fdt as strings, but integers
615 are also supported, in which case the values will be converted to integers
616 (if necessary) before being stored.
617
618 Args:
619 config_list: List of (config, value) tuples to add to the fdt. For each
620 tuple:
621 config: The fdt node to write to will be /config/<config>.
622 value: An integer or string value to write.
623 use_int: True to only write integer values.
624
625 Raises:
626 CmdError: if a value is required to be converted to integer but can't be.
627 """
628 if config_list:
629 for config in config_list:
630 value = config[1]
631 if use_int:
632 try:
633 value = int(value)
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700634 except ValueError:
Simon Glass290a1802011-07-17 13:54:32 -0700635 raise CmdError("Cannot convert config option '%s' to integer" %
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700636 str(value))
Simon Glass290a1802011-07-17 13:54:32 -0700637 if type(value) == type(1):
Simon Glass02d124a2012-03-02 14:47:20 -0800638 self.fdt.PutInteger('/config', '%s' % config[0], value)
Simon Glass290a1802011-07-17 13:54:32 -0700639 else:
Simon Glass02d124a2012-03-02 14:47:20 -0800640 self.fdt.PutString('/config', '%s' % config[0], value)
Simon Glass290a1802011-07-17 13:54:32 -0700641
Simon Glass7c2d5572011-11-15 14:47:08 -0800642 def DecodeTextBase(self, data):
643 """Look at a U-Boot image and try to decode its TEXT_BASE.
644
645 This works because U-Boot has a header with the value 0x12345678
646 immediately followed by the TEXT_BASE value. We can therefore read this
647 from the image with some certainty. We check only the first 40 words
648 since the header should be within that region.
649
Simon Glass96b50302012-07-20 06:55:28 +0100650 Since upstream Tegra has moved to having a 16KB SPL region at the start,
651 and currently this does holds the U-Boot text base (e.g. 0x10c000) instead
652 of the SPL one (e.g. 0x108000), we search in the U-Boot part as well.
653
Simon Glass7c2d5572011-11-15 14:47:08 -0800654 Args:
655 data: U-Boot binary data
656
657 Returns:
658 Text base (integer) or None if none was found
659 """
660 found = False
Simon Glass96b50302012-07-20 06:55:28 +0100661 for start in (0, 0x4000):
662 for i in range(start, start + 160, 4):
663 word = data[i:i + 4]
Simon Glass7c2d5572011-11-15 14:47:08 -0800664
Simon Glass96b50302012-07-20 06:55:28 +0100665 # TODO(sjg): This does not cope with a big-endian target
666 value = struct.unpack('<I', word)[0]
667 if found:
668 return value - start
669 if value == 0x12345678:
670 found = True
Simon Glass7c2d5572011-11-15 14:47:08 -0800671
672 return None
673
674 def CalcTextBase(self, name, fdt, fname):
675 """Calculate the TEXT_BASE to use for U-Boot.
676
677 Normally this value is in the fdt, so we just read it from there. But as
678 a second check we look at the image itself in case this is different, and
679 switch to that if it is.
680
681 This allows us to flash any U-Boot even if its TEXT_BASE is different.
682 This is particularly useful with upstream U-Boot which uses a different
683 value (which we will move to).
684 """
685 data = self._tools.ReadFile(fname)
Andrew Chewaa092542013-01-09 16:30:52 -0800686 # The value that comes back from fdt.GetInt is signed, which makes no
687 # sense for an address base. Force it to unsigned.
688 fdt_text_base = fdt.GetInt('/chromeos-config', 'textbase', 0) & 0xffffffff
Simon Glass7c2d5572011-11-15 14:47:08 -0800689 text_base = self.DecodeTextBase(data)
Simon Glass96b50302012-07-20 06:55:28 +0100690 text_base_str = '%#x' % text_base if text_base else 'None'
691 self._out.Info('TEXT_BASE: fdt says %#x, %s says %s' % (fdt_text_base,
692 fname, text_base_str))
Simon Glass7c2d5572011-11-15 14:47:08 -0800693
694 # If they are different, issue a warning and switch over.
695 if text_base and text_base != fdt_text_base:
696 self._out.Warning("TEXT_BASE %x in %sU-Boot doesn't match "
697 "fdt value of %x. Using %x" % (text_base, name,
698 fdt_text_base, text_base))
699 fdt_text_base = text_base
700 return fdt_text_base
701
Simon Glass6dcc2f22011-07-28 15:26:49 +1200702 def _CreateBootStub(self, uboot, base_fdt, postload):
Simon Glass89b86b82011-07-17 23:49:49 -0700703 """Create a boot stub and a signed boot stub.
704
Simon Glass6dcc2f22011-07-28 15:26:49 +1200705 For postload:
706 We add a /config/postload-text-offset entry to the signed bootstub's
707 fdt so that U-Boot can find the postload code.
708
709 The raw (unsigned) bootstub will have a value of -1 for this since we will
710 simply append the postload code to the bootstub and it can find it there.
711 This will be used for RW A/B firmware.
712
713 For the signed case this value will specify where in the flash to find
714 the postload code. This will be used for RO firmware.
715
Simon Glass89b86b82011-07-17 23:49:49 -0700716 Args:
717 uboot: Path to u-boot.bin (may be chroot-relative)
Simon Glass29b96ad2012-03-09 15:34:33 -0800718 base_fdt: Fdt object containing the flat device tree.
Simon Glass6dcc2f22011-07-28 15:26:49 +1200719 postload: Path to u-boot-post.bin, or None if none.
Simon Glass89b86b82011-07-17 23:49:49 -0700720
721 Returns:
722 Tuple containing:
Simon Glass6dcc2f22011-07-28 15:26:49 +1200723 Full path to bootstub (uboot + fdt(-1) + postload).
724 Full path to signed (uboot + fdt(flash pos) + bct) + postload.
Simon Glass89b86b82011-07-17 23:49:49 -0700725
726 Raises:
727 CmdError if a command fails.
728 """
Simon Glasse13ee2c2011-07-28 08:12:28 +1200729 bootstub = os.path.join(self._tools.outdir, 'u-boot-fdt.bin')
Simon Glass7c2d5572011-11-15 14:47:08 -0800730 text_base = self.CalcTextBase('', self.fdt, uboot)
Simon Glass89b86b82011-07-17 23:49:49 -0700731 uboot_data = self._tools.ReadFile(uboot)
Simon Glass6dcc2f22011-07-28 15:26:49 +1200732
733 # Make a copy of the fdt for the bootstub
734 fdt = base_fdt.Copy(os.path.join(self._tools.outdir, 'bootstub.dtb'))
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700735 fdt.PutInteger('/config', 'postload-text-offset', 0xffffffff)
Simon Glass290a1802011-07-17 13:54:32 -0700736 fdt_data = self._tools.ReadFile(fdt.fname)
Simon Glasse13ee2c2011-07-28 08:12:28 +1200737
Simon Glass89b86b82011-07-17 23:49:49 -0700738 self._tools.WriteFile(bootstub, uboot_data + fdt_data)
Simon Glass290a1802011-07-17 13:54:32 -0700739 self._tools.OutputSize('U-Boot binary', self.uboot_fname)
740 self._tools.OutputSize('U-Boot fdt', self._fdt_fname)
Simon Glass89b86b82011-07-17 23:49:49 -0700741 self._tools.OutputSize('Combined binary', bootstub)
742
Simon Glasse13ee2c2011-07-28 08:12:28 +1200743 # Sign the bootstub; this is a combination of the board specific
Simon Glass89b86b82011-07-17 23:49:49 -0700744 # bct and the stub u-boot image.
Simon Glass290a1802011-07-17 13:54:32 -0700745 signed = self._SignBootstub(self._tools.Filename(self.bct_fname),
Simon Glasse13ee2c2011-07-28 08:12:28 +1200746 bootstub, text_base)
Simon Glass6dcc2f22011-07-28 15:26:49 +1200747
748 signed_postload = os.path.join(self._tools.outdir, 'signed-postload.bin')
749 data = self._tools.ReadFile(signed)
750
751 if postload:
752 # We must add postload to the bootstub since A and B will need to
753 # be able to find it without the /config/postload-text-offset mechanism.
754 bs_data = self._tools.ReadFile(bootstub)
755 bs_data += self._tools.ReadFile(postload)
756 bootstub = os.path.join(self._tools.outdir, 'u-boot-fdt-postload.bin')
757 self._tools.WriteFile(bootstub, bs_data)
758 self._tools.OutputSize('Combined binary with postload', bootstub)
759
760 # Now that we know the file size, adjust the fdt and re-sign
761 postload_bootstub = os.path.join(self._tools.outdir, 'postload.bin')
Simon Glass02d124a2012-03-02 14:47:20 -0800762 fdt.PutInteger('/config', 'postload-text-offset', len(data))
Simon Glass6dcc2f22011-07-28 15:26:49 +1200763 fdt_data = self._tools.ReadFile(fdt.fname)
764 self._tools.WriteFile(postload_bootstub, uboot_data + fdt_data)
765 signed = self._SignBootstub(self._tools.Filename(self.bct_fname),
766 postload_bootstub, text_base)
767 if len(data) != os.path.getsize(signed):
768 raise CmdError('Signed file size changed from %d to %d after updating '
769 'fdt' % (len(data), os.path.getsize(signed)))
770
771 # Re-read the signed image, and add the post-load binary.
772 data = self._tools.ReadFile(signed)
773 data += self._tools.ReadFile(postload)
774 self._tools.OutputSize('Post-load binary', postload)
775
776 self._tools.WriteFile(signed_postload, data)
777 self._tools.OutputSize('Final bootstub with postload', signed_postload)
778
779 return bootstub, signed_postload
Simon Glass89b86b82011-07-17 23:49:49 -0700780
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700781 def _CreateCorebootStub(self, coreboot):
Stefan Reinauerc2e1e4d2011-08-23 14:50:59 -0700782 """Create a coreboot boot stub.
783
784 Args:
Stefan Reinauerc2e1e4d2011-08-23 14:50:59 -0700785 coreboot: Path to coreboot.rom
Stefan Reinauerc2e1e4d2011-08-23 14:50:59 -0700786
787 Returns:
Simon Glasscbc83552012-07-23 15:26:22 +0100788 Full path to bootstub (coreboot + uboot).
Stefan Reinauerc2e1e4d2011-08-23 14:50:59 -0700789 """
790 bootstub = os.path.join(self._tools.outdir, 'coreboot-full.rom')
Simon Glassf2b3a5c2012-06-07 14:02:36 -0700791 shutil.copyfile(self._tools.Filename(coreboot), bootstub)
Simon Glasscbc83552012-07-23 15:26:22 +0100792
793 # Don't add the fdt yet since it is not in final form
Stefan Reinauerc2e1e4d2011-08-23 14:50:59 -0700794 return bootstub
795
Simon Glass7e199222012-03-13 15:51:18 -0700796
Simon Glass89b86b82011-07-17 23:49:49 -0700797 def _PackOutput(self, msg):
798 """Helper function to write output from PackFirmware (verbose level 2).
799
800 This is passed to PackFirmware for it to use to write output.
801
802 Args:
803 msg: Message to display.
804 """
805 self._out.Notice(msg)
806
Simon Glass439fe7a2012-03-09 16:19:34 -0800807 def _BuildBlob(self, pack, fdt, blob_type):
808 """Build the blob data for a particular blob type.
809
810 Args:
811 blob_type: The type of blob to create data for. Supported types are:
812 coreboot A coreboot image (ROM plus U-boot and .dtb payloads).
813 signed Nvidia T20/T30 signed image (BCT, U-Boot, .dtb).
Vadim Bendebury507c0012013-06-09 12:49:25 -0700814
815 Raises:
816 CmdError if a command fails.
Simon Glass439fe7a2012-03-09 16:19:34 -0800817 """
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700818 # stupid pylint insists that sha256 is not in hashlib.
819 # pylint: disable=E1101
Simon Glass439fe7a2012-03-09 16:19:34 -0800820 if blob_type == 'coreboot':
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700821 coreboot = self._CreateCorebootStub(self.coreboot_fname)
Simon Glass439fe7a2012-03-09 16:19:34 -0800822 pack.AddProperty('coreboot', coreboot)
823 pack.AddProperty('image', coreboot)
Stefan Reinauer9ad54842012-10-10 12:25:23 -0700824 elif blob_type == 'legacy':
825 pack.AddProperty('legacy', self.seabios_fname)
Simon Glass439fe7a2012-03-09 16:19:34 -0800826 elif blob_type == 'signed':
827 bootstub, signed = self._CreateBootStub(self.uboot_fname, fdt,
828 self.postload_fname)
829 pack.AddProperty('bootstub', bootstub)
830 pack.AddProperty('signed', signed)
831 pack.AddProperty('image', signed)
Simon Glass7e199222012-03-13 15:51:18 -0700832 elif blob_type == 'exynos-bl1':
833 pack.AddProperty(blob_type, self.exynos_bl1)
Simon Glassbe0bc002012-08-16 12:50:48 -0700834
835 # TODO(sjg@chromium.org): Deprecate ecbin
836 elif blob_type in ['ecrw', 'ecbin']:
837 pack.AddProperty('ecrw', self.ecrw_fname)
838 pack.AddProperty('ecbin', self.ecrw_fname)
Gabe Blackcdbdfe12013-02-06 05:37:52 -0800839 elif blob_type == 'ecrwhash':
840 ec_hash_file = os.path.join(self._tools.outdir, 'ec_hash.bin')
841 ecrw = self._tools.ReadFile(self.ecrw_fname)
842 hasher = hashlib.sha256()
843 hasher.update(ecrw)
844 self._tools.WriteFile(ec_hash_file, hasher.digest())
845 pack.AddProperty(blob_type, ec_hash_file)
Simon Glassbe0bc002012-08-16 12:50:48 -0700846 elif blob_type == 'ecro':
Simon Glass693b40f2012-08-28 10:51:05 -0700847 # crosbug.com/p/13143
848 # We cannot have an fmap in the EC image since there can be only one,
849 # which is the main fmap describing the whole image.
850 # Ultimately the EC will not have an fmap, since with software sync
851 # there is no flashrom involvement in updating the EC flash, and thus
852 # no need for the fmap.
853 # For now, mangle the fmap name to avoid problems.
854 updated_ecro = os.path.join(self._tools.outdir, 'updated-ecro.bin')
855 data = self._tools.ReadFile(self.ecro_fname)
856 data = re.sub('__FMAP__', '__fMAP__', data)
857 self._tools.WriteFile(updated_ecro, data)
858 pack.AddProperty(blob_type, updated_ecro)
Simon Glass7e199222012-03-13 15:51:18 -0700859 elif blob_type == 'exynos-bl2':
Simon Glass7d2542f2012-06-21 07:10:59 -0700860 spl_payload = pack.GetBlobParams(blob_type)
861
862 # TODO(sjg@chromium): Remove this later, when we remove boot+dtb
863 # from all flash map files.
864 if not spl_payload:
865 spl_load_size = os.stat(pack.GetProperty('boot+dtb')).st_size
866 prop_list = 'boot+dtb'
867
868 # Do this later, when we remove boot+dtb.
869 # raise CmdError("No parameters provided for blob type '%s'" %
870 # blob_type)
871 else:
872 prop_list = spl_payload[0].split(',')
Tom Wai-Hong Tam26e3a4c2013-02-06 09:36:47 +0800873 compress = fdt.GetString('/flash/ro-boot', 'compress', 'none')
874 if compress == 'none':
875 compress = None
876 spl_load_size = len(pack.ConcatPropContents(prop_list, compress,
877 False)[0])
Simon Glass7d2542f2012-06-21 07:10:59 -0700878 self._out.Info("BL2/SPL contains '%s', size is %d / %#x" %
879 (', '.join(prop_list), spl_load_size, spl_load_size))
Vadim Bendeburyb12e3352013-06-08 17:25:19 -0700880 bl2 = ExynosBl2(self._tools, self._out)
881 pack.AddProperty(blob_type, bl2.Configure(fdt, spl_load_size,
882 self.exynos_bl2))
Simon Glass439fe7a2012-03-09 16:19:34 -0800883 elif pack.GetProperty(blob_type):
884 pass
Che-Liang Chiou3bc344c2013-02-21 15:18:03 -0800885 elif blob_type in self.blobs:
886 pack.AddProperty(blob_type, self.blobs[blob_type])
Simon Glass439fe7a2012-03-09 16:19:34 -0800887 else:
888 raise CmdError("Unknown blob type '%s' required in flash map" %
889 blob_type)
890
Simon Glass290a1802011-07-17 13:54:32 -0700891 def _CreateImage(self, gbb, fdt):
Simon Glass89b86b82011-07-17 23:49:49 -0700892 """Create a full firmware image, along with various by-products.
893
894 This uses the provided u-boot.bin, fdt and bct to create a firmware
895 image containing all the required parts. If the GBB is not supplied
896 then this will just return a signed U-Boot as the image.
897
898 Args:
Simon Glasse13ee2c2011-07-28 08:12:28 +1200899 gbb: Full path to the GBB file, or empty if a GBB is not required.
900 fdt: Fdt object containing required information.
901
902 Returns:
903 Path to image file
Simon Glass89b86b82011-07-17 23:49:49 -0700904 """
Simon Glass02d124a2012-03-02 14:47:20 -0800905 self._out.Notice("Model: %s" % fdt.GetString('/', 'model'))
Simon Glass89b86b82011-07-17 23:49:49 -0700906
Simon Glass439fe7a2012-03-09 16:19:34 -0800907 pack = PackFirmware(self._tools, self._out)
Vadim Bendebury238f6442013-03-27 11:23:25 -0700908 # Get the flashmap so we know what to build. For board variants use the
909 # main board name as the key (drop the _<variant> suffix).
910 default_flashmap = default_flashmaps.get(self._board.split('_')[0])
Simon Glassb8c6d952012-12-01 06:14:35 -0800911 if self._force_rw:
Vadim Bendebury7bfdb372013-03-27 11:52:58 -0700912 fdt.PutInteger('/flash/rw-a-vblock', 'preamble-flags', 0)
913 fdt.PutInteger('/flash/rw-b-vblock', 'preamble-flags', 0)
Simon Glassb8c6d952012-12-01 06:14:35 -0800914
Simon Glass0c54ba52012-11-06 12:36:43 -0800915 pack.SelectFdt(fdt, self._board, default_flashmap)
Simon Glass439fe7a2012-03-09 16:19:34 -0800916
917 # Get all our blobs ready
918 pack.AddProperty('boot', self.uboot_fname)
Simon Glass284cb892013-02-09 13:38:03 -0800919 if self.skeleton_fname:
920 pack.AddProperty('skeleton', self.skeleton_fname)
Simon Glass3b85f712012-06-21 07:06:46 -0700921 pack.AddProperty('dtb', fdt.fname)
Simon Glass50f74602012-03-15 21:04:25 -0700922
Simon Glass47817052012-10-20 13:30:07 -0700923 # Let's create some copies of the fdt for vboot. These can be used to
924 # pass a different fdt to each firmware type. For now it is just used to
925 # check that the right fdt comes through.
926 fdt_rwa = fdt.Copy(os.path.join(self._tools.outdir, 'updated-rwa.dtb'))
927 fdt_rwa.PutString('/chromeos-config', 'firmware-type', 'rw-a')
928 pack.AddProperty('dtb-rwa', fdt_rwa.fname)
929 fdt_rwb = fdt.Copy(os.path.join(self._tools.outdir, 'updated-rwb.dtb'))
930 fdt_rwb.PutString('/chromeos-config', 'firmware-type', 'rw-b')
931 pack.AddProperty('dtb-rwb', fdt_rwb.fname)
932 fdt.PutString('/chromeos-config', 'firmware-type', 'ro')
933
Simon Glassde9c8072012-07-02 22:29:02 -0700934 # If we are writing a kernel, add its offset from TEXT_BASE to the fdt.
935 if self.kernel_fname:
936 fdt.PutInteger('/config', 'kernel-offset', pack.image_size)
937
Simon Glass439fe7a2012-03-09 16:19:34 -0800938 pack.AddProperty('gbb', self.uboot_fname)
Simon Glass9d088d92012-07-16 16:27:11 +0100939 blob_list = pack.GetBlobList()
940 self._out.Info('Building blobs %s\n' % blob_list)
Simon Glass07267952012-06-08 12:45:13 -0700941 for blob_type in pack.GetBlobList():
Simon Glass439fe7a2012-03-09 16:19:34 -0800942 self._BuildBlob(pack, fdt, blob_type)
Simon Glass89b86b82011-07-17 23:49:49 -0700943
Simon Glass7306b902012-12-17 15:06:21 -0800944 self._out.Progress('Packing image')
Simon Glass89b86b82011-07-17 23:49:49 -0700945 if gbb:
Simon Glasse76bf7b2012-03-13 15:34:41 -0700946 pack.RequireAllEntries()
Hung-Te Lina7462e72011-07-27 19:17:10 +0800947 fwid = '.'.join([
Simon Glass02d124a2012-03-02 14:47:20 -0800948 re.sub('[ ,]+', '_', fdt.GetString('/', 'model')),
Hung-Te Lina7462e72011-07-27 19:17:10 +0800949 self._tools.GetChromeosVersion()])
Simon Glass89b86b82011-07-17 23:49:49 -0700950 self._out.Notice('Firmware ID: %s' % fwid)
Simon Glass439fe7a2012-03-09 16:19:34 -0800951 pack.AddProperty('fwid', fwid)
952 pack.AddProperty('gbb', gbb)
953 pack.AddProperty('keydir', self._keydir)
Simon Glassc90cf582012-03-13 15:40:47 -0700954
955 pack.CheckProperties()
Simon Glass8884b982012-06-21 12:41:41 -0700956
957 # Record position and size of all blob members in the FDT
Gabe Blackcc22d772013-02-04 23:12:02 -0800958 pack.UpdateBlobPositionsAndHashes(fdt)
959 pack.UpdateBlobPositionsAndHashes(fdt_rwa)
960 pack.UpdateBlobPositionsAndHashes(fdt_rwb)
Simon Glass8884b982012-06-21 12:41:41 -0700961
Simon Glass6207efe2012-12-17 15:04:36 -0800962 # Make a copy of the fdt for the bootstub
963 fdt_data = self._tools.ReadFile(fdt.fname)
964 uboot_data = self._tools.ReadFile(self.uboot_fname)
965 uboot_copy = os.path.join(self._tools.outdir, 'u-boot.bin')
966 self._tools.WriteFile(uboot_copy, uboot_data)
967
968 uboot_dtb = os.path.join(self._tools.outdir, 'u-boot-dtb.bin')
969 self._tools.WriteFile(uboot_dtb, uboot_data + fdt_data)
970
Simon Glassa10282a2013-01-08 17:06:41 -0800971 # Fix up the coreboot image here, since we can't do this until we have
972 # a final device tree binary.
Simon Glasscbc83552012-07-23 15:26:22 +0100973 if 'coreboot' in blob_list:
974 bootstub = pack.GetProperty('coreboot')
975 fdt = fdt.Copy(os.path.join(self._tools.outdir, 'bootstub.dtb'))
Simon Glassa10282a2013-01-08 17:06:41 -0800976 if self.coreboot_elf:
977 self._tools.Run('cbfstool', [bootstub, 'add-payload', '-f',
978 self.coreboot_elf, '-n', 'fallback/payload', '-c', 'lzma'])
979 else:
Simon Glass0a7cf112013-05-21 23:08:21 -0700980 text_base = 0x1110000
981
982 # This is the the 'movw $GD_FLG_COLD_BOOT, %bx' instruction
983 # 1110015: 66 bb 00 01 mov $0x100,%bx
984 marker = struct.pack('<L', 0x0100bb66)
985 pos = uboot_data.find(marker)
986 if pos == -1 or pos > 0x100:
987 raise ValueError('Cannot find U-Boot cold boot entry point')
988 entry = text_base + pos
989 self._out.Notice('U-Boot entry point %#08x' % entry)
Simon Glassa10282a2013-01-08 17:06:41 -0800990 self._tools.Run('cbfstool', [bootstub, 'add-flat-binary', '-f',
991 uboot_dtb, '-n', 'fallback/payload', '-c', 'lzma',
Simon Glass0a7cf112013-05-21 23:08:21 -0700992 '-l', '%#x' % text_base, '-e', '%#x' % entry])
Stefan Reinauer1502ea62012-11-01 10:15:38 -0700993 self._tools.Run('cbfstool', [bootstub, 'add', '-f', fdt.fname,
994 '-n', 'u-boot.dtb', '-t', '0xac'])
Simon Glassb8ea1802012-12-17 15:08:00 -0800995 data = self._tools.ReadFile(bootstub)
996 bootstub_copy = os.path.join(self._tools.outdir, 'coreboot-8mb.rom')
997 self._tools.WriteFile(bootstub_copy, data)
Gabe Black3df75252013-02-14 21:32:10 -0800998 self._tools.WriteFile(bootstub, data[-0x100000:])
Simon Glasscbc83552012-07-23 15:26:22 +0100999
Simon Glass208ad952013-02-10 11:16:46 -08001000 pack.AddProperty('fdtmap', fdt.fname)
Simon Glassc90cf582012-03-13 15:40:47 -07001001 image = os.path.join(self._tools.outdir, 'image.bin')
1002 pack.PackImage(self._tools.outdir, image)
1003 pack.AddProperty('image', image)
Simon Glass89b86b82011-07-17 23:49:49 -07001004
Simon Glass439fe7a2012-03-09 16:19:34 -08001005 image = pack.GetProperty('image')
Simon Glass89b86b82011-07-17 23:49:49 -07001006 self._tools.OutputSize('Final image', image)
Simon Glassc90cf582012-03-13 15:40:47 -07001007 return image, pack
Simon Glass89b86b82011-07-17 23:49:49 -07001008
Simon Glassdedda6f2013-02-09 13:44:14 -08001009 def SelectFdt(self, fdt_fname, use_defaults):
Simon Glass290a1802011-07-17 13:54:32 -07001010 """Select an FDT to control the firmware bundling
1011
Simon Glassdedda6f2013-02-09 13:44:14 -08001012 We make a copy of this which will include any on-the-fly changes we want
1013 to make.
1014
Simon Glass290a1802011-07-17 13:54:32 -07001015 Args:
1016 fdt_fname: The filename of the fdt to use.
Simon Glassdedda6f2013-02-09 13:44:14 -08001017 use_defaults: True to use a default FDT name if available, and to add
1018 a full path to the provided filename if necessary.
Simon Glass290a1802011-07-17 13:54:32 -07001019
Simon Glassc0f3dc62011-08-09 14:19:05 -07001020 Returns:
1021 The Fdt object of the original fdt file, which we will not modify.
1022
Simon Glassdedda6f2013-02-09 13:44:14 -08001023 Raises:
1024 ValueError if no FDT is provided (fdt_fname is None and use_defaults is
1025 False).
Simon Glass290a1802011-07-17 13:54:32 -07001026 """
Simon Glassdedda6f2013-02-09 13:44:14 -08001027 if use_defaults:
1028 fdt_fname = self._CheckFdtFilename(fdt_fname)
Simon Glass22f39fb2013-02-09 13:44:14 -08001029 if not fdt_fname:
1030 raise ValueError('Please provide an FDT filename')
1031 fdt = Fdt(self._tools, fdt_fname)
Simon Glass290a1802011-07-17 13:54:32 -07001032 self._fdt_fname = fdt_fname
Simon Glassc3e42c32012-12-17 15:00:04 -08001033
1034 # For upstream, select the correct architecture .dtsi manually.
1035 if self._board == 'link' or 'x86' in self._board:
1036 arch_dts = 'coreboot.dtsi'
1037 elif self._board == 'daisy':
1038 arch_dts = 'exynos5250.dtsi'
1039 else:
1040 arch_dts = 'tegra20.dtsi'
1041
1042 fdt.Compile(arch_dts)
Simon Glass290a1802011-07-17 13:54:32 -07001043 self.fdt = fdt.Copy(os.path.join(self._tools.outdir, 'updated.dtb'))
Simon Glassc0f3dc62011-08-09 14:19:05 -07001044 return fdt
Simon Glass290a1802011-07-17 13:54:32 -07001045
Simon Glassc90cf582012-03-13 15:40:47 -07001046 def Start(self, hardware_id, output_fname, show_map):
Simon Glass290a1802011-07-17 13:54:32 -07001047 """This creates a firmware bundle according to settings provided.
Simon Glass89b86b82011-07-17 23:49:49 -07001048
1049 - Checks options, tools, output directory, fdt.
1050 - Creates GBB and image.
Simon Glass290a1802011-07-17 13:54:32 -07001051
1052 Args:
Simon Glass56577572011-07-19 11:08:06 +12001053 hardware_id: Hardware ID to use for this board. If None, then the
1054 default from the Fdt will be used
Simon Glass290a1802011-07-17 13:54:32 -07001055 output_fname: Output filename for the image. If this is not None, then
1056 the final image will be copied here.
Simon Glassc90cf582012-03-13 15:40:47 -07001057 show_map: Show a flash map, with each area's name and position
Simon Glass290a1802011-07-17 13:54:32 -07001058
1059 Returns:
1060 Filename of the resulting image (not the output_fname copy).
Simon Glass89b86b82011-07-17 23:49:49 -07001061 """
Vadim Bendebury5baeec12013-04-02 13:01:22 -07001062 if self._small or self.fdt.GetProp('/config', 'nogbb', 'any') != 'any':
1063 gbb = '' # Building a small image or `nogbb' is requested in device tree.
1064 else:
Simon Glass56577572011-07-19 11:08:06 +12001065 gbb = self._CreateGoogleBinaryBlock(hardware_id)
Simon Glass89b86b82011-07-17 23:49:49 -07001066
1067 # This creates the actual image.
Simon Glassc90cf582012-03-13 15:40:47 -07001068 image, pack = self._CreateImage(gbb, self.fdt)
1069 if show_map:
1070 pack.ShowMap()
Simon Glass290a1802011-07-17 13:54:32 -07001071 if output_fname:
1072 shutil.copyfile(image, output_fname)
1073 self._out.Notice("Output image '%s'" % output_fname)
Simon Glass794217e2012-06-07 11:40:37 -07001074 return image, pack.props