[U-Boot] [PATCH 00/30] binman: A tool for creating firmware images

This series introduces binman, a tool designed to create firmware images. It provides a way to bring together various binaries and place them in an image, at particular positions and with configurable alignment.
Packaging of firmware is quite a different task from building the various parts. In many cases the various binaries which go into the image come from separate build systems. For example, ARM Trusted Firmware is used on ARMv8 devices but is not built in the U-Boot tree. If a Linux kernel is included in the firmware image, it is built elsewhere.
It is of course possible to add more and more build rules to the U-Boot build system to cover these cases. It can shell out to other Makefiles and build scripts. But it seems better to create a clear divide between building software and packaging it.
U-Boot supports a very large number of boards. Many of these have their own specific rules for how an image should be put together so that it boots correctly. At present these rules are described by manual instructions, different for each board. By turning these instructions into a standard format, we can support making valid images for any board without manual effort, lots of READMEs, etc.
Images consist of a number of entries which are combined to make up the final image. The image is described in the device tree for the board, meaning that it can be accessed at run-time if desired.
Binman is an extensible tool. A set of standard entries is provides, but new entries can be created fairly easily. Entries can be as simple as providing the name of a file to include. They can also handle more complex requirements, such as adjusting the input file or reading header information from one entry to control the position of another.
U-Boot's mkimage builds FIT images and various other binaries. Binman augments this by allowing these binaries to be packed together. While FIT should be used where possible, it cannot be used everywhere. For example, many devices require executable code at a particular offset in the image. X86 machines require lots of binary blobs at particular places, and a microcode collection easily accessible at boot.
So far binman has enough functionality to be useful, but apart from a few RFC patches, no attempt is made to switch boards over to use it. There should be enough material to permit review and comments.
The series is available at u-boot-dm/binman-working
Future work and missing features are documented in the README.
Simon Glass (30): x86: Add debugging when a microcode update fails x86: ivybridge: Allow microcode to be collated x86: Add debugging when cpu_common_init() fails patman: Adjust command.Output() to raise an error by default dtoc: Move the struct import into the correct order dtoc: Move the fdt library selection into fdt_select dtoc: Rename fdt.py to fdt_normal.py dtoc: Create a base class for Fdt dtoc: Move BytesToValue() and GetEmpty() into PropBase dtoc: Move Widen() and GetPhandle() into the base class dtoc: Move a few more common functions into fdt.py patman: Add a tools library for using temporary files patman: Add a library to handle logging and progress dtoc: Allow the device tree to be compiled from source dtoc: Drop the convert_dash parameter to GetProps() dtoc: Prepare for supporting changing of device trees dtoc: Move to using bytearray dtoc: Support deleting device tree properties dtoc: Support packing the device tree dtoc: Support finding the offset of a property dtoc: Correct quotes in fdt_util dtoc: Add methods for reading data from properties binman: Introduce binman, a tool for building binary images binman: Add basic entry types for U-Boot binman: Add support for building x86 ROMs binman: Add support for u-boot.img as an input binary binman: Add a build rule for binman binman: Allow configuration options to be used in .dts files RFC: Use binman for a sunxi board RFC: Use binman for an x86 board
Makefile | 55 +- arch/arm/dts/sun7i-a20-pcduino3.dts | 12 + arch/x86/cpu/intel_common/cpu.c | 4 +- arch/x86/cpu/intel_common/microcode.c | 12 +- arch/x86/cpu/ivybridge/cpu.c | 4 +- arch/x86/dts/chromebook_link.dts | 52 ++ lib/libfdt/libfdt.swig | 24 + scripts/Makefile.lib | 5 + tools/binman/.gitignore | 1 + tools/binman/README | 491 ++++++++++++++++ tools/binman/binman | 1 + tools/binman/binman.py | 96 +++ tools/binman/cmdline.py | 48 ++ tools/binman/control.py | 106 ++++ tools/binman/etype/blob.py | 37 ++ tools/binman/etype/entry.py | 186 ++++++ tools/binman/etype/intel_descriptor.py | 55 ++ tools/binman/etype/intel_me.py | 17 + tools/binman/etype/intel_mrc.py | 17 + tools/binman/etype/intel_vga.py | 17 + tools/binman/etype/u_boot.py | 17 + tools/binman/etype/u_boot_dtb.py | 17 + tools/binman/etype/u_boot_dtb_with_ucode.py | 69 +++ tools/binman/etype/u_boot_img.py | 17 + tools/binman/etype/u_boot_nodtb.py | 17 + tools/binman/etype/u_boot_spl.py | 17 + tools/binman/etype/u_boot_ucode.py | 77 +++ tools/binman/etype/u_boot_with_ucode_ptr.py | 71 +++ tools/binman/etype/x86_start16.py | 17 + tools/binman/func_test.py | 652 +++++++++++++++++++++ tools/binman/image.py | 233 ++++++++ tools/binman/test/01_invalid.dts | 5 + tools/binman/test/02_missing_node.dts | 6 + tools/binman/test/03_empty.dts | 9 + tools/binman/test/04_invalid_entry.dts | 11 + tools/binman/test/05_simple.dts | 11 + tools/binman/test/06_dual_image.dts | 22 + tools/binman/test/07_bad_align.dts | 12 + tools/binman/test/08_pack.dts | 30 + tools/binman/test/09_pack_extra.dts | 35 ++ tools/binman/test/10_pack_align_power2.dts | 12 + tools/binman/test/11_pack_align_size_power2.dts | 12 + tools/binman/test/12_pack_inv_align.dts | 13 + tools/binman/test/13_pack_inv_size_align.dts | 13 + tools/binman/test/14_pack_overlap.dts | 16 + tools/binman/test/15_pack_overflow.dts | 12 + tools/binman/test/16_pack_image_overflow.dts | 13 + tools/binman/test/17_pack_image_size.dts | 13 + tools/binman/test/18_pack_image_align.dts | 13 + tools/binman/test/19_pack_inv_image_align.dts | 14 + .../binman/test/20_pack_inv_image_align_power2.dts | 13 + tools/binman/test/21_image_pad.dts | 16 + tools/binman/test/22_image_name.dts | 21 + tools/binman/test/23_blob.dts | 12 + tools/binman/test/24_sorted.dts | 17 + tools/binman/test/25_pack_zero_size.dts | 15 + tools/binman/test/26_pack_u_boot_dtb.dts | 14 + tools/binman/test/27_pack_4gb_no_size.dts | 18 + tools/binman/test/28_pack_4gb_outside.dts | 19 + tools/binman/test/29_x86-rom.dts | 19 + tools/binman/test/30_x86-rom-me-no-desc.dts | 15 + tools/binman/test/31_x86-rom-me.dts | 18 + tools/binman/test/32_intel-vga.dts | 13 + tools/binman/test/33_x86-start16.dts | 13 + tools/binman/test/34_x86_ucode.dts | 29 + tools/binman/test/35_x86_single_ucode.dts | 26 + tools/binman/test/36_u_boot_img.dts | 11 + tools/binman/test/u_boot_ucode_ptr | Bin 0 -> 4175 bytes tools/binman/test/u_boot_ucode_ptr.c | 15 + tools/binman/test/u_boot_ucode_ptr.lds | 18 + tools/buildman/control.py | 2 +- tools/dtoc/dtoc.py | 38 +- tools/dtoc/fdt.py | 219 +++++-- tools/dtoc/fdt_fallback.py | 133 ++--- tools/dtoc/fdt_normal.py | 228 +++++++ tools/dtoc/fdt_select.py | 26 + tools/dtoc/fdt_util.py | 126 ++-- tools/patman/checkpatch.py | 3 +- tools/patman/command.py | 5 +- tools/patman/gitutil.py | 3 +- tools/patman/tools.py | 120 ++++ tools/patman/tout.py | 166 ++++++ 82 files changed, 3821 insertions(+), 286 deletions(-) create mode 100644 tools/binman/.gitignore create mode 100644 tools/binman/README create mode 120000 tools/binman/binman create mode 100755 tools/binman/binman.py create mode 100644 tools/binman/cmdline.py create mode 100644 tools/binman/control.py create mode 100644 tools/binman/etype/blob.py create mode 100644 tools/binman/etype/entry.py create mode 100644 tools/binman/etype/intel_descriptor.py create mode 100644 tools/binman/etype/intel_me.py create mode 100644 tools/binman/etype/intel_mrc.py create mode 100644 tools/binman/etype/intel_vga.py create mode 100644 tools/binman/etype/u_boot.py create mode 100644 tools/binman/etype/u_boot_dtb.py create mode 100644 tools/binman/etype/u_boot_dtb_with_ucode.py create mode 100644 tools/binman/etype/u_boot_img.py create mode 100644 tools/binman/etype/u_boot_nodtb.py create mode 100644 tools/binman/etype/u_boot_spl.py create mode 100644 tools/binman/etype/u_boot_ucode.py create mode 100644 tools/binman/etype/u_boot_with_ucode_ptr.py create mode 100644 tools/binman/etype/x86_start16.py create mode 100644 tools/binman/func_test.py create mode 100644 tools/binman/image.py create mode 100644 tools/binman/test/01_invalid.dts create mode 100644 tools/binman/test/02_missing_node.dts create mode 100644 tools/binman/test/03_empty.dts create mode 100644 tools/binman/test/04_invalid_entry.dts create mode 100644 tools/binman/test/05_simple.dts create mode 100644 tools/binman/test/06_dual_image.dts create mode 100644 tools/binman/test/07_bad_align.dts create mode 100644 tools/binman/test/08_pack.dts create mode 100644 tools/binman/test/09_pack_extra.dts create mode 100644 tools/binman/test/10_pack_align_power2.dts create mode 100644 tools/binman/test/11_pack_align_size_power2.dts create mode 100644 tools/binman/test/12_pack_inv_align.dts create mode 100644 tools/binman/test/13_pack_inv_size_align.dts create mode 100644 tools/binman/test/14_pack_overlap.dts create mode 100644 tools/binman/test/15_pack_overflow.dts create mode 100644 tools/binman/test/16_pack_image_overflow.dts create mode 100644 tools/binman/test/17_pack_image_size.dts create mode 100644 tools/binman/test/18_pack_image_align.dts create mode 100644 tools/binman/test/19_pack_inv_image_align.dts create mode 100644 tools/binman/test/20_pack_inv_image_align_power2.dts create mode 100644 tools/binman/test/21_image_pad.dts create mode 100644 tools/binman/test/22_image_name.dts create mode 100644 tools/binman/test/23_blob.dts create mode 100644 tools/binman/test/24_sorted.dts create mode 100644 tools/binman/test/25_pack_zero_size.dts create mode 100644 tools/binman/test/26_pack_u_boot_dtb.dts create mode 100644 tools/binman/test/27_pack_4gb_no_size.dts create mode 100644 tools/binman/test/28_pack_4gb_outside.dts create mode 100644 tools/binman/test/29_x86-rom.dts create mode 100644 tools/binman/test/30_x86-rom-me-no-desc.dts create mode 100644 tools/binman/test/31_x86-rom-me.dts create mode 100644 tools/binman/test/32_intel-vga.dts create mode 100644 tools/binman/test/33_x86-start16.dts create mode 100644 tools/binman/test/34_x86_ucode.dts create mode 100644 tools/binman/test/35_x86_single_ucode.dts create mode 100644 tools/binman/test/36_u_boot_img.dts create mode 100755 tools/binman/test/u_boot_ucode_ptr create mode 100644 tools/binman/test/u_boot_ucode_ptr.c create mode 100644 tools/binman/test/u_boot_ucode_ptr.lds create mode 100644 tools/dtoc/fdt_normal.py create mode 100644 tools/dtoc/fdt_select.py create mode 100644 tools/patman/tools.py create mode 100644 tools/patman/tout.py

Add a debug() at this point to help figure out what is wrong.
Signed-off-by: Simon Glass sjg@chromium.org ---
arch/x86/cpu/intel_common/cpu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/arch/x86/cpu/intel_common/cpu.c b/arch/x86/cpu/intel_common/cpu.c index 0fdef6f..ae42095 100644 --- a/arch/x86/cpu/intel_common/cpu.c +++ b/arch/x86/cpu/intel_common/cpu.c @@ -42,8 +42,10 @@ int cpu_common_init(void) enable_lapic();
ret = microcode_update_intel(); - if (ret && ret != -EEXIST) + if (ret && ret != -EEXIST) { + debug("%s: Microcode update failure (err=%d)\n", __func__, ret); return ret; + }
/* Enable upper 128bytes of CMOS */ writel(1 << 2, RCB_REG(RC));

Hello Simon,
Am 26.07.2016 um 02:58 schrieb Simon Glass:
Add a debug() at this point to help figure out what is wrong.
Signed-off-by: Simon Glass sjg@chromium.org
arch/x86/cpu/intel_common/cpu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Reviewed-by: Heiko Schocherhs@denx.de
but wondering why this is in the "binman: A tool for creating firmware images" series.
bye, Heiko
diff --git a/arch/x86/cpu/intel_common/cpu.c b/arch/x86/cpu/intel_common/cpu.c index 0fdef6f..ae42095 100644 --- a/arch/x86/cpu/intel_common/cpu.c +++ b/arch/x86/cpu/intel_common/cpu.c @@ -42,8 +42,10 @@ int cpu_common_init(void) enable_lapic();
ret = microcode_update_intel();
- if (ret && ret != -EEXIST)
if (ret && ret != -EEXIST) {
debug("%s: Microcode update failure (err=%d)\n", __func__, ret);
return ret;
}
/* Enable upper 128bytes of CMOS */ writel(1 << 2, RCB_REG(RC));

On Tue, Jul 26, 2016 at 8:58 AM, Simon Glass sjg@chromium.org wrote:
Add a debug() at this point to help figure out what is wrong.
Signed-off-by: Simon Glass sjg@chromium.org
arch/x86/cpu/intel_common/cpu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Reviewed-by: Bin Meng bmeng.cn@gmail.com

On Thu, Jul 28, 2016 at 2:53 PM, Bin Meng bmeng.cn@gmail.com wrote:
On Tue, Jul 26, 2016 at 8:58 AM, Simon Glass sjg@chromium.org wrote:
Add a debug() at this point to help figure out what is wrong.
Signed-off-by: Simon Glass sjg@chromium.org
arch/x86/cpu/intel_common/cpu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Reviewed-by: Bin Meng bmeng.cn@gmail.com
applied to u-boot-x86, thanks!

Generally the microcode is combined into a single block only (and removed from the device tree) when there are multiple blocks. But this is not a requirement.
Adjust the ivybridge code to avoid assuming this.
Signed-off-by: Simon Glass sjg@chromium.org ---
arch/x86/cpu/intel_common/microcode.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/arch/x86/cpu/intel_common/microcode.c b/arch/x86/cpu/intel_common/microcode.c index daf0d69..eac5b78 100644 --- a/arch/x86/cpu/intel_common/microcode.c +++ b/arch/x86/cpu/intel_common/microcode.c @@ -43,7 +43,7 @@ static int microcode_decode_node(const void *blob, int node, { update->data = fdt_getprop(blob, node, "data", &update->size); if (!update->data) - return -EINVAL; + return -ENOENT; update->data += UCODE_HEADER_LEN; update->size -= UCODE_HEADER_LEN;
@@ -145,6 +145,16 @@ int microcode_update_intel(void) }
ret = microcode_decode_node(blob, node, &update); + if (ret == -ENOENT && ucode_base) { + /* + * The microcode has been removed from the device tree + * in the build system. In that case it will have + * already been updated in car_init(). + */ + debug("%s: Microcode data not available\n", __func__); + skipped++; + continue; + } if (ret) { debug("%s: Unable to decode update: %d\n", __func__, ret);

On Tue, Jul 26, 2016 at 8:58 AM, Simon Glass sjg@chromium.org wrote:
Generally the microcode is combined into a single block only (and removed from the device tree) when there are multiple blocks. But this is not a requirement.
Adjust the ivybridge code to avoid assuming this.
Signed-off-by: Simon Glass sjg@chromium.org
arch/x86/cpu/intel_common/microcode.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
Reviewed-by: Bin Meng bmeng.cn@gmail.com

On Thu, Jul 28, 2016 at 2:53 PM, Bin Meng bmeng.cn@gmail.com wrote:
On Tue, Jul 26, 2016 at 8:58 AM, Simon Glass sjg@chromium.org wrote:
Generally the microcode is combined into a single block only (and removed from the device tree) when there are multiple blocks. But this is not a requirement.
Adjust the ivybridge code to avoid assuming this.
Signed-off-by: Simon Glass sjg@chromium.org
arch/x86/cpu/intel_common/microcode.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
Reviewed-by: Bin Meng bmeng.cn@gmail.com
applied to u-boot-x86, thanks!

Add a debug() at this point to help figure out what is wrong.
Signed-off-by: Simon Glass sjg@chromium.org ---
arch/x86/cpu/ivybridge/cpu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/arch/x86/cpu/ivybridge/cpu.c b/arch/x86/cpu/ivybridge/cpu.c index 0f93905..85e361a 100644 --- a/arch/x86/cpu/ivybridge/cpu.c +++ b/arch/x86/cpu/ivybridge/cpu.c @@ -146,8 +146,10 @@ int print_cpuinfo(void) }
ret = cpu_common_init(); - if (ret) + if (ret) { + debug("%s: cpu_common_init() failed\n", __func__); return ret; + }
/* Check PM1_STS[15] to see if we are waking from Sx */ pm1_sts = inw(DEFAULT_PMBASE + PM1_STS);

On Tue, Jul 26, 2016 at 8:58 AM, Simon Glass sjg@chromium.org wrote:
Add a debug() at this point to help figure out what is wrong.
Signed-off-by: Simon Glass sjg@chromium.org
arch/x86/cpu/ivybridge/cpu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Reviewed-by: Bin Meng bmeng.cn@gmail.com

On Thu, Jul 28, 2016 at 2:53 PM, Bin Meng bmeng.cn@gmail.com wrote:
On Tue, Jul 26, 2016 at 8:58 AM, Simon Glass sjg@chromium.org wrote:
Add a debug() at this point to help figure out what is wrong.
Signed-off-by: Simon Glass sjg@chromium.org
arch/x86/cpu/ivybridge/cpu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Reviewed-by: Bin Meng bmeng.cn@gmail.com
applied to u-boot-x86, thanks!

It is more useful to have this method raise an error when something goes wrong. Make this the default and adjust the few callers that don't want to use it this way.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/buildman/control.py | 2 +- tools/patman/checkpatch.py | 3 ++- tools/patman/command.py | 5 +++-- tools/patman/gitutil.py | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/tools/buildman/control.py b/tools/buildman/control.py index aeb128a..51262fe 100644 --- a/tools/buildman/control.py +++ b/tools/buildman/control.py @@ -231,7 +231,7 @@ def DoBuildman(options, args, toolchains=None, make_func=None, boards=None, options.step = len(series.commits) - 1
gnu_make = command.Output(os.path.join(options.git, - 'scripts/show-gnu-make')).rstrip() + 'scripts/show-gnu-make'), raise_on_error=False).rstrip() if not gnu_make: sys.exit('GNU Make not found')
diff --git a/tools/patman/checkpatch.py b/tools/patman/checkpatch.py index 34a3bd2..3eef6de 100644 --- a/tools/patman/checkpatch.py +++ b/tools/patman/checkpatch.py @@ -63,7 +63,8 @@ def CheckPatch(fname, verbose=False): result.problems = [] chk = FindCheckPatch() item = {} - result.stdout = command.Output(chk, '--no-tree', fname) + result.stdout = command.Output(chk, '--no-tree', fname, + raise_on_error=False) #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) #stdout, stderr = pipe.communicate()
diff --git a/tools/patman/command.py b/tools/patman/command.py index d586f11..d1f0ca5 100644 --- a/tools/patman/command.py +++ b/tools/patman/command.py @@ -104,8 +104,9 @@ def RunPipe(pipe_list, infile=None, outfile=None, raise Exception("Error running '%s'" % user_pipestr) return result
-def Output(*cmd): - return RunPipe([cmd], capture=True, raise_on_error=False).stdout +def Output(*cmd, **kwargs): + raise_on_error = kwargs.get('raise_on_error', True) + return RunPipe([cmd], capture=True, raise_on_error=raise_on_error).stdout
def OutputOneLine(*cmd, **kwargs): raise_on_error = kwargs.pop('raise_on_error', True) diff --git a/tools/patman/gitutil.py b/tools/patman/gitutil.py index e088bae..bb7c9e0 100644 --- a/tools/patman/gitutil.py +++ b/tools/patman/gitutil.py @@ -391,7 +391,8 @@ def EmailPatches(series, cover_fname, args, dry_run, raise_on_error, cc_fname, """ to = BuildEmailList(series.get('to'), '--to', alias, raise_on_error) if not to: - git_config_to = command.Output('git', 'config', 'sendemail.to') + git_config_to = command.Output('git', 'config', 'sendemail.to', + raise_on_error=False) if not git_config_to: print ("No recipient.\n" "Please add something like this to a commit\n"

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
It is more useful to have this method raise an error when something goes wrong. Make this the default and adjust the few callers that don't want to use it this way.
Signed-off-by: Simon Glass sjg@chromium.org
tools/buildman/control.py | 2 +- tools/patman/checkpatch.py | 3 ++- tools/patman/command.py | 5 +++-- tools/patman/gitutil.py | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-)
Applied to u-boot-dm/next.

This should be in with the other system includes. Move it.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/dtoc.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/tools/dtoc/dtoc.py b/tools/dtoc/dtoc.py index ec80abe..e9ab46f 100755 --- a/tools/dtoc/dtoc.py +++ b/tools/dtoc/dtoc.py @@ -9,6 +9,7 @@ import copy from optparse import OptionError, OptionParser import os +import struct import sys
import fdt_util @@ -29,8 +30,6 @@ except ImportError: from fdt_fallback import Fdt import fdt_fallback as fdt
-import struct - # When we see these properties we ignore them - i.e. do not create a structure member PROP_IGNORE_LIST = [ '#address-cells',

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
This should be in with the other system includes. Move it.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/dtoc.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Applied to u-boot-dm/next.

Rather than have dtc worry about which fdt library to use, move this into a helper file. Add a function which creates a new Fdt object and scans it, regardless of the implementation.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/dtoc.py | 18 +++--------------- tools/dtoc/fdt_select.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 tools/dtoc/fdt_select.py
diff --git a/tools/dtoc/dtoc.py b/tools/dtoc/dtoc.py index e9ab46f..10f6a12 100755 --- a/tools/dtoc/dtoc.py +++ b/tools/dtoc/dtoc.py @@ -12,23 +12,12 @@ import os import struct import sys
-import fdt_util - # Bring in the patman libraries our_path = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.join(our_path, '../patman'))
-# Bring in either the normal fdt library (which relies on libfdt) or the -# fallback one (which uses fdtget and is slower). Both provide the same -# interfface for this file to use. -try: - from fdt import Fdt - import fdt - have_libfdt = True -except ImportError: - have_libfdt = False - from fdt_fallback import Fdt - import fdt_fallback as fdt +import fdt_select +import fdt_util
# When we see these properties we ignore them - i.e. do not create a structure member PROP_IGNORE_LIST = [ @@ -177,8 +166,7 @@ class DtbPlatdata: Once this is done, self.fdt.GetRoot() can be called to obtain the device tree root node, and progress from there. """ - self.fdt = Fdt(self._dtb_fname) - self.fdt.Scan() + self.fdt = fdt_select.FdtScan(self._dtb_fname)
def ScanTree(self): """Scan the device tree for useful information diff --git a/tools/dtoc/fdt_select.py b/tools/dtoc/fdt_select.py new file mode 100644 index 0000000..5aff297 --- /dev/null +++ b/tools/dtoc/fdt_select.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# +# Copyright (C) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# + +# Bring in either the normal fdt library (which relies on libfdt) or the +# fallback one (which uses fdtget and is slower). Both provide the same +# interface for this file to use. +try: + import fdt + have_libfdt = True +except ImportError: + have_libfdt = False + import fdt_fallback as fdt + +def FdtScan(fname): + """Returns a new Fdt object from the implementation we are using""" + dtb = fdt.Fdt(fname) + dtb.Scan() + return dtb

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
Rather than have dtc worry about which fdt library to use, move this into a helper file. Add a function which creates a new Fdt object and scans it, regardless of the implementation.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/dtoc.py | 18 +++--------------- tools/dtoc/fdt_select.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 tools/dtoc/fdt_select.py
Applied to u-boot-dm/next.

In preparation for creating an Fdt base class, rename this file to indicate it is the normal Fdt implementation.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/{fdt.py => fdt_normal.py} | 0 tools/dtoc/fdt_select.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tools/dtoc/{fdt.py => fdt_normal.py} (100%)
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt_normal.py similarity index 100% rename from tools/dtoc/fdt.py rename to tools/dtoc/fdt_normal.py diff --git a/tools/dtoc/fdt_select.py b/tools/dtoc/fdt_select.py index 5aff297..681dfbf 100644 --- a/tools/dtoc/fdt_select.py +++ b/tools/dtoc/fdt_select.py @@ -10,7 +10,7 @@ # fallback one (which uses fdtget and is slower). Both provide the same # interface for this file to use. try: - import fdt + import fdt_normal as fdt have_libfdt = True except ImportError: have_libfdt = False

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
In preparation for creating an Fdt base class, rename this file to indicate it is the normal Fdt implementation.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/{fdt.py => fdt_normal.py} | 0 tools/dtoc/fdt_select.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tools/dtoc/{fdt.py => fdt_normal.py} (100%)
Applied to u-boot-dm/next.

At present we have two separate implementations of the Fdt library, one which uses fdtget/fdtput and one which uses libfdt (via swig).
Before adding more functionality it makes sense to create a base class for these. This will allow common functions to be shared, and make the Fdt API a little clearer.
Create a new fdt.py file with the base class, and adjust fdt_normal.py and fdt_fallback.py to use it.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/fdt.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++ tools/dtoc/fdt_fallback.py | 61 +++++++++++++++++++++++++---------------- tools/dtoc/fdt_normal.py | 58 +++++++++++++++++++++++++-------------- tools/dtoc/fdt_select.py | 9 ++++-- 4 files changed, 148 insertions(+), 48 deletions(-) create mode 100644 tools/dtoc/fdt.py
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py new file mode 100644 index 0000000..413d45d --- /dev/null +++ b/tools/dtoc/fdt.py @@ -0,0 +1,68 @@ +#!/usr/bin/python +# +# Copyright (C) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# + +import struct +import sys + +import fdt_util + +# This deals with a device tree, presenting it as an assortment of Node and +# Prop objects, representing nodes and properties, respectively. This file +# contains the base classes and defines the high-level API. Most of the +# implementation is in the FdtFallback and FdtNormal subclasses. See +# fdt_select.py for how to create an Fdt object. + +def CheckErr(errnum, msg): + if errnum: + raise ValueError('Error %d: %s: %s' % + (errnum, libfdt.fdt_strerror(errnum), msg)) + +class PropBase: + """A device tree property + + Properties: + name: Property name (as per the device tree) + value: Property value as a string of bytes, or a list of strings of + bytes + type: Value type + """ + def __init__(self, node, offset, name): + self._node = node + self._offset = offset + self.name = name + self.value = None + +class NodeBase: + """A device tree node + + Properties: + offset: Integer offset in the device tree + name: Device tree node tname + path: Full path to node, along with the node name itself + _fdt: Device tree object + subnodes: A list of subnodes for this node, each a Node object + props: A dict of properties for this node, each a Prop object. + Keyed by property name + """ + def __init__(self, fdt, offset, name, path): + self._fdt = fdt + self._offset = offset + self.name = name + self.path = path + self.subnodes = [] + self.props = {} + +class Fdt: + """Provides simple access to a flat device tree blob. + + Properties: + fname: Filename of fdt + _root: Root of device tree (a Node object) + """ + def __init__(self, fname): + self._fname = fname diff --git a/tools/dtoc/fdt_fallback.py b/tools/dtoc/fdt_fallback.py index 14decf3..7d912e2 100644 --- a/tools/dtoc/fdt_fallback.py +++ b/tools/dtoc/fdt_fallback.py @@ -7,6 +7,8 @@ #
import command +import fdt +from fdt import Fdt, NodeBase, PropBase import fdt_util import sys
@@ -17,7 +19,7 @@ import sys # is not very efficient for larger trees. The tool is called once for each # node and property in the tree.
-class Prop: +class Prop(PropBase): """A device tree property
Properties: @@ -26,14 +28,14 @@ class Prop: bytes type: Value type """ - def __init__(self, name, byte_list_str): - self.name = name - self.value = None + def __init__(self, node, name, byte_list_str): + PropBase.__init__(self, node, 0, name) if not byte_list_str.strip(): self.type = fdt_util.TYPE_BOOL return - bytes = [chr(int(byte, 16)) for byte in byte_list_str.strip().split(' ')] - self.type, self.value = fdt_util.BytesToValue(''.join(bytes)) + self.bytes = [chr(int(byte, 16)) + for byte in byte_list_str.strip().split(' ')] + self.type, self.value = fdt_util.BytesToValue(''.join(self.bytes))
def GetPhandle(self): """Get a (single) phandle value from a property @@ -71,7 +73,7 @@ class Prop: if type(newprop.value) == list and type(self.value) != list: self.value = newprop.value
-class Node: +class Node(NodeBase): """A device tree node
Properties: @@ -82,12 +84,8 @@ class Node: props: A dict of properties for this node, each a Prop object. Keyed by property name """ - def __init__(self, fdt, name, path): - self.name = name - self.path = path - self._fdt = fdt - self.subnodes = [] - self.props = {} + def __init__(self, fdt, offset, name, path): + NodeBase.__init__(self, fdt, offset, name, path)
def Scan(self): """Scan a node's properties and subnodes @@ -96,35 +94,34 @@ class Node: searching into subnodes so that the entire tree is built. """ for name, byte_list_str in self._fdt.GetProps(self.path).iteritems(): - prop = Prop(name, byte_list_str) + prop = Prop(self, name, byte_list_str) self.props[name] = prop
for name in self._fdt.GetSubNodes(self.path): sep = '' if self.path[-1] == '/' else '/' path = self.path + sep + name - node = Node(self._fdt, name, path) + node = Node(self._fdt, 0, name, path) self.subnodes.append(node)
node.Scan()
-class Fdt: - """Provides simple access to a flat device tree blob. +class FdtFallback(Fdt): + """Provides simple access to a flat device tree blob using fdtget/fdtput
Properties: - fname: Filename of fdt - _root: Root of device tree (a Node object) + See superclass """
def __init__(self, fname): - self.fname = fname + Fdt.__init__(self, fname)
def Scan(self): """Scan a device tree, building up a tree of Node objects
This fills in the self._root property """ - self._root = Node(self, '/', '/') + self._root = Node(self, 0, '/', '/') self._root.Scan()
def GetRoot(self): @@ -147,7 +144,7 @@ class Fdt: Raises: CmdError: if the node does not exist. """ - out = command.Output('fdtget', self.fname, '-l', node) + out = command.Output('fdtget', self._fname, '-l', node) return out.strip().splitlines()
def GetProps(self, node, convert_dashes=False): @@ -165,7 +162,7 @@ class Fdt: Raises: CmdError: if the node does not exist. """ - out = command.Output('fdtget', self.fname, node, '-p') + out = command.Output('fdtget', self._fname, node, '-p') props = out.strip().splitlines() props_dict = {} for prop in props: @@ -198,10 +195,26 @@ class Fdt: Raises: CmdError: if the property does not exist and no default is provided. """ - args = [self.fname, node, prop, '-t', 'bx'] + args = [self._fname, node, prop, '-t', 'bx'] if default is not None: args += ['-d', str(default)] if typespec is not None: args += ['-t%s' % typespec] out = command.Output('fdtget', *args) return out.strip() + + @classmethod + def Node(self, fdt, offset, name, path): + """Create a new node + + This is used by Fdt.Scan() to create a new node using the correct + class. + + Args: + fdt: Fdt object + offset: Offset of node + name: Node name + path: Full path to node + """ + node = Node(fdt, offset, name, path) + return node diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index 1d913a9..ca5335b 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -6,9 +6,13 @@ # SPDX-License-Identifier: GPL-2.0+ #
+import struct +import sys + +import fdt +from fdt import Fdt, NodeBase, PropBase import fdt_util import libfdt -import sys
# This deals with a device tree, presenting it as a list of Node and Prop # objects, representing nodes and properties, respectively. @@ -16,7 +20,7 @@ import sys # This implementation uses a libfdt Python library to access the device tree, # so it is fairly efficient.
-class Prop: +class Prop(PropBase): """A device tree property
Properties: @@ -25,9 +29,9 @@ class Prop: bytes type: Value type """ - def __init__(self, name, bytes): - self.name = name - self.value = None + def __init__(self, node, offset, name, bytes): + PropBase.__init__(self, node, offset, name) + self.bytes = bytes if not bytes: self.type = fdt_util.TYPE_BOOL self.value = True @@ -76,7 +80,7 @@ class Prop: self.value.append(val)
-class Node: +class Node(NodeBase): """A device tree node
Properties: @@ -89,12 +93,7 @@ class Node: Keyed by property name """ def __init__(self, fdt, offset, name, path): - self.offset = offset - self.name = name - self.path = path - self._fdt = fdt - self.subnodes = [] - self.props = {} + NodeBase.__init__(self, fdt, offset, name, path)
def Scan(self): """Scan a node's properties and subnodes @@ -104,7 +103,7 @@ class Node: """ self.props = self._fdt.GetProps(self.path)
- offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self.offset) + offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self._offset) while offset >= 0: sep = '' if self.path[-1] == '/' else '/' name = libfdt.Name(self._fdt.GetFdt(), offset) @@ -116,17 +115,17 @@ class Node: offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset)
-class Fdt: - """Provides simple access to a flat device tree blob. +class FdtNormal(Fdt): + """Provides simple access to a flat device tree blob using libfdt.
Properties: - fname: Filename of fdt - _root: Root of device tree (a Node object) + _fdt: Device tree contents (bytearray) + _cached_offsets: True if all the nodes have a valid _offset property, + False if something has changed to invalidate the offsets """ - def __init__(self, fname): - self.fname = fname - with open(fname) as fd: + Fdt.__init__(self, fname) + with open(self._fname) as fd: self._fdt = fd.read()
def GetFdt(self): @@ -173,8 +172,25 @@ class Fdt: poffset = libfdt.fdt_first_property_offset(self._fdt, offset) while poffset >= 0: dprop, plen = libfdt.fdt_get_property_by_offset(self._fdt, poffset) - prop = Prop(libfdt.String(self._fdt, dprop.nameoff), libfdt.Data(dprop)) + prop = Prop(node, poffset, libfdt.String(self._fdt, dprop.nameoff), + libfdt.Data(dprop)) props_dict[prop.name] = prop
poffset = libfdt.fdt_next_property_offset(self._fdt, poffset) return props_dict + + @classmethod + def Node(self, fdt, offset, name, path): + """Create a new node + + This is used by Fdt.Scan() to create a new node using the correct + class. + + Args: + fdt: Fdt object + offset: Offset of node + name: Node name + path: Full path to node + """ + node = Node(fdt, offset, name, path) + return node diff --git a/tools/dtoc/fdt_select.py b/tools/dtoc/fdt_select.py index 681dfbf..18a36d8 100644 --- a/tools/dtoc/fdt_select.py +++ b/tools/dtoc/fdt_select.py @@ -10,14 +10,17 @@ # fallback one (which uses fdtget and is slower). Both provide the same # interface for this file to use. try: - import fdt_normal as fdt + import fdt_normal have_libfdt = True except ImportError: have_libfdt = False - import fdt_fallback as fdt + import fdt_fallback
def FdtScan(fname): """Returns a new Fdt object from the implementation we are using""" - dtb = fdt.Fdt(fname) + if have_libfdt: + dtb = fdt_normal.FdtNormal(fname) + else: + dtb = fdt_fallback.FdtFallback(fname) dtb.Scan() return dtb

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
At present we have two separate implementations of the Fdt library, one which uses fdtget/fdtput and one which uses libfdt (via swig).
Before adding more functionality it makes sense to create a base class for these. This will allow common functions to be shared, and make the Fdt API a little clearer.
Create a new fdt.py file with the base class, and adjust fdt_normal.py and fdt_fallback.py to use it.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/fdt.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++ tools/dtoc/fdt_fallback.py | 61 +++++++++++++++++++++++++---------------- tools/dtoc/fdt_normal.py | 58 +++++++++++++++++++++++++-------------- tools/dtoc/fdt_select.py | 9 ++++-- 4 files changed, 148 insertions(+), 48 deletions(-) create mode 100644 tools/dtoc/fdt.py
Applied to u-boot-dm/next.

These functions are currently in a separate fdt_util file. Since they are only used from PropBase and subclasses, it makes sense for them to be in the PropBase class.
Move these functions into fdt.py along with the list of types.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/dtoc.py | 17 ++++++------ tools/dtoc/fdt.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++ tools/dtoc/fdt_fallback.py | 4 +-- tools/dtoc/fdt_normal.py | 6 ++--- tools/dtoc/fdt_util.py | 66 ---------------------------------------------- 5 files changed, 80 insertions(+), 79 deletions(-)
diff --git a/tools/dtoc/dtoc.py b/tools/dtoc/dtoc.py index 10f6a12..518aa51 100755 --- a/tools/dtoc/dtoc.py +++ b/tools/dtoc/dtoc.py @@ -16,6 +16,7 @@ import sys our_path = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.join(our_path, '../patman'))
+import fdt import fdt_select import fdt_util
@@ -33,10 +34,10 @@ PROP_IGNORE_LIST = [
# C type declarations for the tyues we support TYPE_NAMES = { - fdt_util.TYPE_INT: 'fdt32_t', - fdt_util.TYPE_BYTE: 'unsigned char', - fdt_util.TYPE_STRING: 'const char *', - fdt_util.TYPE_BOOL: 'bool', + fdt.TYPE_INT: 'fdt32_t', + fdt.TYPE_BYTE: 'unsigned char', + fdt.TYPE_STRING: 'const char *', + fdt.TYPE_BOOL: 'bool', };
STRUCT_PREFIX = 'dtd_' @@ -138,13 +139,13 @@ class DtbPlatdata: type: Data type (fdt_util) value: Data value, as a string of bytes """ - if type == fdt_util.TYPE_INT: + if type == fdt.TYPE_INT: return '%#x' % fdt_util.fdt32_to_cpu(value) - elif type == fdt_util.TYPE_BYTE: + elif type == fdt.TYPE_BYTE: return '%#x' % ord(value[0]) - elif type == fdt_util.TYPE_STRING: + elif type == fdt.TYPE_STRING: return '"%s"' % value - elif type == fdt_util.TYPE_BOOL: + elif type == fdt.TYPE_BOOL: return 'true'
def GetCompatName(self, node): diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index 413d45d..329d03c 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -17,6 +17,9 @@ import fdt_util # implementation is in the FdtFallback and FdtNormal subclasses. See # fdt_select.py for how to create an Fdt object.
+# A list of types we support +(TYPE_BYTE, TYPE_INT, TYPE_STRING, TYPE_BOOL) = range(4) + def CheckErr(errnum, msg): if errnum: raise ValueError('Error %d: %s: %s' % @@ -37,6 +40,69 @@ class PropBase: self.name = name self.value = None
+ def BytesToValue(self, bytes): + """Converts a string of bytes into a type and value + + Args: + A string containing bytes + + Return: + A tuple: + Type of data + Data, either a single element or a list of elements. Each element + is one of: + TYPE_STRING: string value from the property + TYPE_INT: a byte-swapped integer stored as a 4-byte string + TYPE_BYTE: a byte stored as a single-byte string + """ + size = len(bytes) + strings = bytes.split('\0') + is_string = True + count = len(strings) - 1 + if count > 0 and not strings[-1]: + for string in strings[:-1]: + if not string: + is_string = False + break + for ch in string: + if ch < ' ' or ch > '~': + is_string = False + break + else: + is_string = False + if is_string: + if count == 1: + return TYPE_STRING, strings[0] + else: + return TYPE_STRING, strings[:-1] + if size % 4: + if size == 1: + return TYPE_BYTE, bytes[0] + else: + return TYPE_BYTE, list(bytes) + val = [] + for i in range(0, size, 4): + val.append(bytes[i:i + 4]) + if size == 4: + return TYPE_INT, val[0] + else: + return TYPE_INT, val + + def GetEmpty(self, type): + """Get an empty / zero value of the given type + + Returns: + A single value of the given type + """ + if type == TYPE_BYTE: + return chr(0) + elif type == TYPE_INT: + return struct.pack('<I', 0); + elif type == TYPE_STRING: + return '' + else: + return True + class NodeBase: """A device tree node
diff --git a/tools/dtoc/fdt_fallback.py b/tools/dtoc/fdt_fallback.py index 7d912e2..3b5f3be 100644 --- a/tools/dtoc/fdt_fallback.py +++ b/tools/dtoc/fdt_fallback.py @@ -31,11 +31,11 @@ class Prop(PropBase): def __init__(self, node, name, byte_list_str): PropBase.__init__(self, node, 0, name) if not byte_list_str.strip(): - self.type = fdt_util.TYPE_BOOL + self.type = fdt.TYPE_BOOL return self.bytes = [chr(int(byte, 16)) for byte in byte_list_str.strip().split(' ')] - self.type, self.value = fdt_util.BytesToValue(''.join(self.bytes)) + self.type, self.value = self.BytesToValue(''.join(self.bytes))
def GetPhandle(self): """Get a (single) phandle value from a property diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index ca5335b..c7c86b8 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -33,10 +33,10 @@ class Prop(PropBase): PropBase.__init__(self, node, offset, name) self.bytes = bytes if not bytes: - self.type = fdt_util.TYPE_BOOL + self.type = fdt.TYPE_BOOL self.value = True return - self.type, self.value = fdt_util.BytesToValue(bytes) + self.type, self.value = self.BytesToValue(bytes)
def GetPhandle(self): """Get a (single) phandle value from a property @@ -75,7 +75,7 @@ class Prop(PropBase): self.value = [self.value]
if type(self.value) == list and len(newprop.value) > len(self.value): - val = fdt_util.GetEmpty(self.type) + val = self.GetEmpty(self.type) while len(self.value) < len(newprop.value): self.value.append(val)
diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 929b524..6b57248 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -8,72 +8,6 @@
import struct
-# A list of types we support -(TYPE_BYTE, TYPE_INT, TYPE_STRING, TYPE_BOOL) = range(4) - -def BytesToValue(bytes): - """Converts a string of bytes into a type and value - - Args: - A string containing bytes - - Return: - A tuple: - Type of data - Data, either a single element or a list of elements. Each element - is one of: - TYPE_STRING: string value from the property - TYPE_INT: a byte-swapped integer stored as a 4-byte string - TYPE_BYTE: a byte stored as a single-byte string - """ - size = len(bytes) - strings = bytes.split('\0') - is_string = True - count = len(strings) - 1 - if count > 0 and not strings[-1]: - for string in strings[:-1]: - if not string: - is_string = False - break - for ch in string: - if ch < ' ' or ch > '~': - is_string = False - break - else: - is_string = False - if is_string: - if count == 1: - return TYPE_STRING, strings[0] - else: - return TYPE_STRING, strings[:-1] - if size % 4: - if size == 1: - return TYPE_BYTE, bytes[0] - else: - return TYPE_BYTE, list(bytes) - val = [] - for i in range(0, size, 4): - val.append(bytes[i:i + 4]) - if size == 4: - return TYPE_INT, val[0] - else: - return TYPE_INT, val - -def GetEmpty(type): - """Get an empty / zero value of the given type - - Returns: - A single value of the given type - """ - if type == TYPE_BYTE: - return chr(0) - elif type == TYPE_INT: - return struct.pack('<I', 0); - elif type == TYPE_STRING: - return '' - else: - return True - def fdt32_to_cpu(val): """Convert a device tree cell to an integer

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
These functions are currently in a separate fdt_util file. Since they are only used from PropBase and subclasses, it makes sense for them to be in the PropBase class.
Move these functions into fdt.py along with the list of types.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/dtoc.py | 17 ++++++------ tools/dtoc/fdt.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++ tools/dtoc/fdt_fallback.py | 4 +-- tools/dtoc/fdt_normal.py | 6 ++--- tools/dtoc/fdt_util.py | 66 ---------------------------------------------- 5 files changed, 80 insertions(+), 79 deletions(-)
Applied to u-boot-dm/next.

These functions are identical in both subclasses. Move them into the base class.
Note: In fact there is a bug in one version, which was fixed by this patch:
https://patchwork.ozlabs.org/patch/651697/
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/fdt.py | 41 +++++++++++++++++++++++++++++++++++++++++ tools/dtoc/fdt_fallback.py | 36 ------------------------------------ tools/dtoc/fdt_normal.py | 42 ------------------------------------------ 3 files changed, 41 insertions(+), 78 deletions(-)
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index 329d03c..964ef7c 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -40,6 +40,47 @@ class PropBase: self.name = name self.value = None
+ def GetPhandle(self): + """Get a (single) phandle value from a property + + Gets the phandle valuie from a property and returns it as an integer + """ + return fdt_util.fdt32_to_cpu(self.value[:4]) + + def Widen(self, newprop): + """Figure out which property type is more general + + Given a current property and a new property, this function returns the + one that is less specific as to type. The less specific property will + be ble to represent the data in the more specific property. This is + used for things like: + + node1 { + compatible = "fred"; + value = <1>; + }; + node1 { + compatible = "fred"; + value = <1 2>; + }; + + He we want to use an int array for 'value'. The first property + suggests that a single int is enough, but the second one shows that + it is not. Calling this function with these two propertes would + update the current property to be like the second, since it is less + specific. + """ + if newprop.type < self.type: + self.type = newprop.type + + if type(newprop.value) == list and type(self.value) != list: + self.value = [self.value] + + if type(self.value) == list and len(newprop.value) > len(self.value): + val = self.GetEmpty(self.type) + while len(self.value) < len(newprop.value): + self.value.append(val) + def BytesToValue(self, bytes): """Converts a string of bytes into a type and value
diff --git a/tools/dtoc/fdt_fallback.py b/tools/dtoc/fdt_fallback.py index 3b5f3be..9a03b02 100644 --- a/tools/dtoc/fdt_fallback.py +++ b/tools/dtoc/fdt_fallback.py @@ -37,42 +37,6 @@ class Prop(PropBase): for byte in byte_list_str.strip().split(' ')] self.type, self.value = self.BytesToValue(''.join(self.bytes))
- def GetPhandle(self): - """Get a (single) phandle value from a property - - Gets the phandle valuie from a property and returns it as an integer - """ - return fdt_util.fdt32_to_cpu(self.value[:4]) - - def Widen(self, newprop): - """Figure out which property type is more general - - Given a current property and a new property, this function returns the - one that is less specific as to type. The less specific property will - be ble to represent the data in the more specific property. This is - used for things like: - - node1 { - compatible = "fred"; - value = <1>; - }; - node1 { - compatible = "fred"; - value = <1 2>; - }; - - He we want to use an int array for 'value'. The first property - suggests that a single int is enough, but the second one shows that - it is not. Calling this function with these two propertes would - update the current property to be like the second, since it is less - specific. - """ - if newprop.type < self.type: - self.type = newprop.type - - if type(newprop.value) == list and type(self.value) != list: - self.value = newprop.value - class Node(NodeBase): """A device tree node
diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index c7c86b8..6f019c1 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -38,48 +38,6 @@ class Prop(PropBase): return self.type, self.value = self.BytesToValue(bytes)
- def GetPhandle(self): - """Get a (single) phandle value from a property - - Gets the phandle valuie from a property and returns it as an integer - """ - return fdt_util.fdt32_to_cpu(self.value[:4]) - - def Widen(self, newprop): - """Figure out which property type is more general - - Given a current property and a new property, this function returns the - one that is less specific as to type. The less specific property will - be ble to represent the data in the more specific property. This is - used for things like: - - node1 { - compatible = "fred"; - value = <1>; - }; - node1 { - compatible = "fred"; - value = <1 2>; - }; - - He we want to use an int array for 'value'. The first property - suggests that a single int is enough, but the second one shows that - it is not. Calling this function with these two propertes would - update the current property to be like the second, since it is less - specific. - """ - if newprop.type < self.type: - self.type = newprop.type - - if type(newprop.value) == list and type(self.value) != list: - self.value = [self.value] - - if type(self.value) == list and len(newprop.value) > len(self.value): - val = self.GetEmpty(self.type) - while len(self.value) < len(newprop.value): - self.value.append(val) - - class Node(NodeBase): """A device tree node

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
These functions are identical in both subclasses. Move them into the base class.
Note: In fact there is a bug in one version, which was fixed by this patch:
https://patchwork.ozlabs.org/patch/651697/
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/fdt.py | 41 +++++++++++++++++++++++++++++++++++++++++ tools/dtoc/fdt_fallback.py | 36 ------------------------------------ tools/dtoc/fdt_normal.py | 42 ------------------------------------------ 3 files changed, 41 insertions(+), 78 deletions(-)
Applied to u-boot-dm/next.

Some functions have the same code in the subclasses. Move these into the superclass to avoid duplication.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/fdt.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++ tools/dtoc/fdt_fallback.py | 16 ------------- tools/dtoc/fdt_normal.py | 16 ------------- 3 files changed, 57 insertions(+), 32 deletions(-)
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index 964ef7c..c0ce5af 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -164,6 +164,26 @@ class NodeBase: self.subnodes = [] self.props = {}
+ def _FindNode(self, name): + """Find a node given its name + + Args: + name: Node name to look for + Returns: + Node object if found, else None + """ + for subnode in self.subnodes: + if subnode.name == name: + return subnode + return None + + def Scan(self): + """Scan the subnodes of a node + + This should be implemented by subclasses + """ + raise NotImplementedError() + class Fdt: """Provides simple access to a flat device tree blob.
@@ -173,3 +193,40 @@ class Fdt: """ def __init__(self, fname): self._fname = fname + + def Scan(self, root='/'): + """Scan a device tree, building up a tree of Node objects + + This fills in the self._root property + + Args: + root: Ignored + + TODO(sjg@chromium.org): Implement the 'root' parameter + """ + self._root = self.Node(self, 0, '/', '/') + self._root.Scan() + + def GetRoot(self): + """Get the root Node of the device tree + + Returns: + The root Node object + """ + return self._root + + def GetNode(self, path): + """Look up a node from its path + + Args: + path: Path to look up, e.g. '/microcode/update@0' + Returns: + Node object, or None if not found + """ + node = self._root + for part in path.split('/')[1:]: + node = node._FindNode(part) + if not node: + return None + return node + diff --git a/tools/dtoc/fdt_fallback.py b/tools/dtoc/fdt_fallback.py index 9a03b02..222fd27 100644 --- a/tools/dtoc/fdt_fallback.py +++ b/tools/dtoc/fdt_fallback.py @@ -80,22 +80,6 @@ class FdtFallback(Fdt): def __init__(self, fname): Fdt.__init__(self, fname)
- def Scan(self): - """Scan a device tree, building up a tree of Node objects - - This fills in the self._root property - """ - self._root = Node(self, 0, '/', '/') - self._root.Scan() - - def GetRoot(self): - """Get the root Node of the device tree - - Returns: - The root Node object - """ - return self._root - def GetSubNodes(self, node): """Returns a list of sub-nodes of a given node
diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index 6f019c1..861f60c 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -94,22 +94,6 @@ class FdtNormal(Fdt): """ return self._fdt
- def Scan(self): - """Scan a device tree, building up a tree of Node objects - - This fills in the self._root property - """ - self._root = Node(self, 0, '/', '/') - self._root.Scan() - - def GetRoot(self): - """Get the root Node of the device tree - - Returns: - The root Node object - """ - return self._root - def GetProps(self, node): """Get all properties from a node.

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
Some functions have the same code in the subclasses. Move these into the superclass to avoid duplication.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/fdt.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++ tools/dtoc/fdt_fallback.py | 16 ------------- tools/dtoc/fdt_normal.py | 16 ------------- 3 files changed, 57 insertions(+), 32 deletions(-)
Applied to u-boot-dm/next.

For tools which want to use input files and temporary output, it is useful to have the handling of these dealt with in one place. Add a new library which allows input files to be read, and output files to be written, all based on a common directory structure.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/patman/tools.py | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tools/patman/tools.py
diff --git a/tools/patman/tools.py b/tools/patman/tools.py new file mode 100644 index 0000000..ba24853 --- /dev/null +++ b/tools/patman/tools.py @@ -0,0 +1,120 @@ +# +# Copyright (c) 2016 Google, Inc +# +# SPDX-License-Identifier: GPL-2.0+ +# + +import os +import shutil +import tempfile + +import tout + +outdir = None +indirs = None +preserve_outdir = False + +def PrepareOutputDir(dirname, preserve=False): + """Select an output directory, ensuring it exists. + + This either creates a temporary directory or checks that the one supplied + by the user is valid. For a temporary directory, it makes a note to + remove it later if required. + + Args: + dirname: a string, name of the output directory to use to store + intermediate and output files. If is None - create a temporary + directory. + preserve: a Boolean. If outdir above is None and preserve is False, the + created temporary directory will be destroyed on exit. + + Raises: + OSError: If it cannot create the output directory. + """ + global outdir, preserve_outdir + + preserve_outdir = dirname or preserve + if dirname: + outdir = dirname + if not os.path.isdir(outdir): + try: + os.makedirs(outdir) + except OSError as err: + raise CmdError("Cannot make output directory '%s': '%s'" % + (outdir, err.strerror)) + tout.Debug("Using output directory '%s'" % outdir) + else: + outdir = tempfile.mkdtemp(prefix='binman.') + tout.Debug("Using temporary directory '%s'" % outdir) + +def _RemoveOutputDir(): + global outdir + + shutil.rmtree(outdir) + tout.Debug("Deleted temporary directory '%s'" % outdir) + outdir = None + +def FinaliseOutputDir(): + global outdir, preserve_outdir + + """Tidy up: delete output directory if temporary and not preserved.""" + if outdir and not preserve_outdir: + _RemoveOutputDir() + +def GetOutputFilename(fname): + """Return a filename within the output directory. + + Args: + fname: Filename to use for new file + + Returns: + The full path of the filename, within the output directory + """ + return os.path.join(outdir, fname) + +def _FinaliseForTest(): + """Remove the output directory (for use by tests)""" + global outdir + + if outdir: + _RemoveOutputDir() + +def SetInputDirs(dirname): + """Add a list of input directories, where input files are kept. + + Args: + dirname: a list of paths to input directories to use for obtaining + files needed by binman to place in the image. + """ + global indir + + indir = dirname + tout.Debug("Using input directories %s" % indir) + +def GetInputFilename(fname): + """Return a filename for use as input. + + Args: + fname: Filename to use for new file + + Returns: + The full path of the filename, within the input directory + """ + if not indir: + return fname + for dirname in indir: + pathname = os.path.join(dirname, fname) + if os.path.exists(pathname): + return pathname + + raise ValueError("Filename '%s' not found in input path (%s)" % + (fname, ','.join(indir))) + +def Align(pos, align): + if align: + mask = align - 1 + pos = (pos + mask) & ~mask + return pos + +def NotPowerOfTwo(num): + return num and (num & (num - 1))

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
For tools which want to use input files and temporary output, it is useful to have the handling of these dealt with in one place. Add a new library which allows input files to be read, and output files to be written, all based on a common directory structure.
Signed-off-by: Simon Glass sjg@chromium.org
tools/patman/tools.py | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tools/patman/tools.py
Applied to u-boot-dm/next.

When tools want to display information of varying levels of importance, it helps to provide the user with control over the verbosity of these messages. Progress messages work best if they are displayed and then removed from the display when no-longer relevant.
Add a new tout library (terminal out) to handle these tasks.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/patman/tout.py | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 tools/patman/tout.py
diff --git a/tools/patman/tout.py b/tools/patman/tout.py new file mode 100644 index 0000000..c5fbd80 --- /dev/null +++ b/tools/patman/tout.py @@ -0,0 +1,166 @@ +# Copyright (c) 2016 Google, Inc +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Terminal output logging. +# + +import sys + +import terminal + +# Output verbosity levels that we support +ERROR = 0 +WARNING = 1 +NOTICE = 2 +INFO = 3 +DEBUG = 4 + +""" +This class handles output of progress and other useful information +to the user. It provides for simple verbosity level control and can +output nothing but errors at verbosity zero. + +The idea is that modules set up an Output object early in their years and pass +it around to other modules that need it. This keeps the output under control +of a single class. + +Public properties: + verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug +""" +def __enter__(): + return + +def __exit__(unused1, unused2, unused3): + """Clean up and remove any progress message.""" + ClearProgress() + return False + +def UserIsPresent(): + """This returns True if it is likely that a user is present. + + Sometimes we want to prompt the user, but if no one is there then this + is a waste of time, and may lock a script which should otherwise fail. + + Returns: + True if it thinks the user is there, and False otherwise + """ + return stdout_is_tty and verbose > 0 + +def ClearProgress(): + """Clear any active progress message on the terminal.""" + if verbose > 0 and stdout_is_tty: + _stdout.write('\r%s\r' % (" " * len (_progress))) + _stdout.flush() + +def Progress(msg, warning=False, trailer='...'): + """Display progress information. + + Args: + msg: Message to display. + warning: True if this is a warning.""" + ClearProgress() + if verbose > 0: + _progress = msg + trailer + if stdout_is_tty: + col = _color.YELLOW if warning else _color.GREEN + _stdout.write('\r' + _color.Color(col, _progress)) + _stdout.flush() + else: + _stdout.write(_progress + '\n') + +def _Output(level, msg, color=None): + """Output a message to the terminal. + + Args: + level: Verbosity level for this message. It will only be displayed if + this as high as the currently selected level. + msg; Message to display. + error: True if this is an error message, else False. + """ + if verbose >= level: + ClearProgress() + if color: + msg = _color.Color(color, msg) + _stdout.write(msg + '\n') + +def DoOutput(level, msg): + """Output a message to the terminal. + + Args: + level: Verbosity level for this message. It will only be displayed if + this as high as the currently selected level. + msg; Message to display. + """ + _Output(level, msg) + +def Error(msg): + """Display an error message + + Args: + msg; Message to display. + """ + _Output(0, msg, _color.RED) + +def Warning(msg): + """Display a warning message + + Args: + msg; Message to display. + """ + _Output(1, msg, _color.YELLOW) + +def Notice(msg): + """Display an important infomation message + + Args: + msg; Message to display. + """ + _Output(2, msg) + +def Info(msg): + """Display an infomation message + + Args: + msg; Message to display. + """ + _Output(3, msg) + +def Debug(msg): + """Display a debug message + + Args: + msg; Message to display. + """ + _Output(4, msg) + +def UserOutput(msg): + """Display a message regardless of the current output level. + + This is used when the output was specifically requested by the user. + Args: + msg; Message to display. + """ + _Output(0, msg) + +def Init(_verbose=WARNING, stdout=sys.stdout): + """Initialize a new output object. + + Args: + verbose: Verbosity level (0-4). + stdout: File to use for stdout. + """ + global verbose, _progress, _color, _stdout, stdout_is_tty + + verbose = _verbose + _progress = '' # Our last progress message + _color = terminal.Color() + _stdout = stdout + + # TODO(sjg): Move this into Chromite libraries when we have them + stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() + +def Uninit(): + ClearProgress() + +Init()

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
When tools want to display information of varying levels of importance, it helps to provide the user with control over the verbosity of these messages. Progress messages work best if they are displayed and then removed from the display when no-longer relevant.
Add a new tout library (terminal out) to handle these tasks.
Signed-off-by: Simon Glass sjg@chromium.org
tools/patman/tout.py | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 tools/patman/tout.py
Applied to u-boot-dm/next.

If a source device tree is provide to the Fdt() constructors, compile it automatically. This will be used in tests, where we want to build a particular test .dts file and check that it works correctly in binman.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/fdt_fallback.py | 2 ++ tools/dtoc/fdt_normal.py | 7 +++++-- tools/dtoc/fdt_util.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-)
diff --git a/tools/dtoc/fdt_fallback.py b/tools/dtoc/fdt_fallback.py index 222fd27..ddc4097 100644 --- a/tools/dtoc/fdt_fallback.py +++ b/tools/dtoc/fdt_fallback.py @@ -79,6 +79,8 @@ class FdtFallback(Fdt):
def __init__(self, fname): Fdt.__init__(self, fname) + if self._fname: + self._fname = fdt_util.EnsureCompiled(self._fname)
def GetSubNodes(self, node): """Returns a list of sub-nodes of a given node diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index 861f60c..d9ba4ac 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -83,8 +83,11 @@ class FdtNormal(Fdt): """ def __init__(self, fname): Fdt.__init__(self, fname) - with open(self._fname) as fd: - self._fdt = fd.read() + if self._fname: + self._fname = fdt_util.EnsureCompiled(self._fname) + + with open(self._fname) as fd: + self._fdt = fd.read()
def GetFdt(self): """Get the contents of the FDT diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 6b57248..3e25a8b 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -6,7 +6,12 @@ # SPDX-License-Identifier: GPL-2.0+ #
+import os import struct +import tempfile + +import command +import tools
def fdt32_to_cpu(val): """Convert a device tree cell to an integer @@ -18,3 +23,39 @@ def fdt32_to_cpu(val): A native-endian integer value """ return struct.unpack(">I", val)[0] + +def EnsureCompiled(fname): + """Compile an fdt .dts source file into a .dtb binary blob if needed. + + Args: + fname: Filename (if .dts it will be compiled). It not it will be + left alone + + Returns: + Filename of resulting .dtb file + """ + _, ext = os.path.splitext(fname) + if ext != '.dts': + return fname + + dts_input = tools.GetOutputFilename('source.dts') + dtb_output = tools.GetOutputFilename('source.dtb') + + search_paths = [os.path.join(os.getcwd(), 'include')] + root, _ = os.path.splitext(fname) + args = ['-E', '-P', '-x', 'assembler-with-cpp', '-D__ASSEMBLY__'] + args += ['-Ulinux'] + for path in search_paths: + args.extend(['-I', path]) + args += ['-o', dts_input, fname] + command.Run('cc', *args) + + # If we don't have a directory, put it in the tools tempdir + search_list = [] + for path in search_paths: + search_list.extend(['-i', path]) + args = ['-I', 'dts', '-o', dtb_output, '-O', 'dtb'] + args.extend(search_list) + args.append(dts_input) + command.Run('dtc', *args) + return dtb_output

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
If a source device tree is provide to the Fdt() constructors, compile it automatically. This will be used in tests, where we want to build a particular test .dts file and check that it works correctly in binman.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/fdt_fallback.py | 2 ++ tools/dtoc/fdt_normal.py | 7 +++++-- tools/dtoc/fdt_util.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-)
Applied to u-boot-dm/next.

This is not used anywhere in dtoc, so drop it.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/fdt_fallback.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/tools/dtoc/fdt_fallback.py b/tools/dtoc/fdt_fallback.py index ddc4097..cb97cff 100644 --- a/tools/dtoc/fdt_fallback.py +++ b/tools/dtoc/fdt_fallback.py @@ -97,12 +97,11 @@ class FdtFallback(Fdt): out = command.Output('fdtget', self._fname, '-l', node) return out.strip().splitlines()
- def GetProps(self, node, convert_dashes=False): + def GetProps(self, node): """Get all properties from a node
Args: node: full path to node name to look in - convert_dashes: True to convert - to _ in node names
Returns: A dictionary containing all the properties, indexed by node name. @@ -117,8 +116,6 @@ class FdtFallback(Fdt): props_dict = {} for prop in props: name = prop - if convert_dashes: - prop = re.sub('-', '_', prop) props_dict[prop] = self.GetProp(node, name) return props_dict

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
This is not used anywhere in dtoc, so drop it.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/fdt_fallback.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
Applied to u-boot-dm/next.

For binman we need to support deleting properties in the device tree. This will change the offsets of nodes after the deletion. In preparation, add code to keep track of when the offsets are invalid, and regenerate them.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/fdt_normal.py | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-)
diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index d9ba4ac..4a667a1 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -53,15 +53,24 @@ class Node(NodeBase): def __init__(self, fdt, offset, name, path): NodeBase.__init__(self, fdt, offset, name, path)
+ def Offset(self): + """Returns the offset of a node, after checking the cache + + This should be used instead of self._offset directly, to ensure that + the cache does not contain invalid offsets. + """ + self._fdt.CheckCache() + return self._offset + def Scan(self): """Scan a node's properties and subnodes
This fills in the props and subnodes properties, recursively searching into subnodes so that the entire tree is built. """ - self.props = self._fdt.GetProps(self.path) + self.props = self._fdt.GetProps(self, self.path)
- offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self._offset) + offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self.Offset()) while offset >= 0: sep = '' if self.path[-1] == '/' else '/' name = libfdt.Name(self._fdt.GetFdt(), offset) @@ -72,6 +81,19 @@ class Node(NodeBase): node.Scan() offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset)
+ def Refresh(self, my_offset): + """Fix up the _offset for each node, recursively + + Note: This does not take account of property offsets - these will not + be updated. + """ + if self._offset != my_offset: + #print '%s: %d -> %d\n' % (self.path, self._offset, my_offset) + self._offset = my_offset + offset = libfdt.fdt_first_subnode(self._fdt.GetFdt(), self._offset) + for subnode in self.subnodes: + subnode.Refresh(offset) + offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset)
class FdtNormal(Fdt): """Provides simple access to a flat device tree blob using libfdt. @@ -83,6 +105,7 @@ class FdtNormal(Fdt): """ def __init__(self, fname): Fdt.__init__(self, fname) + self._cached_offsets = False if self._fname: self._fname = fdt_util.EnsureCompiled(self._fname)
@@ -97,7 +120,7 @@ class FdtNormal(Fdt): """ return self._fdt
- def GetProps(self, node): + def GetProps(self, node, path): """Get all properties from a node.
Args: @@ -110,7 +133,7 @@ class FdtNormal(Fdt): Raises: ValueError: if the node does not exist. """ - offset = libfdt.fdt_path_offset(self._fdt, node) + offset = libfdt.fdt_path_offset(self._fdt, path) if offset < 0: libfdt.Raise(offset) props_dict = {} @@ -124,6 +147,21 @@ class FdtNormal(Fdt): poffset = libfdt.fdt_next_property_offset(self._fdt, poffset) return props_dict
+ def Invalidate(self): + """Mark our offset cache as invalid""" + self._cached_offsets = False + + def CheckCache(self): + """Refresh the offset cache if needed""" + if self._cached_offsets: + return + self.Refresh() + self._cached_offsets = True + + def Refresh(self): + """Refresh the offset cache""" + self._root.Refresh(0) + @classmethod def Node(self, fdt, offset, name, path): """Create a new node

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
For binman we need to support deleting properties in the device tree. This will change the offsets of nodes after the deletion. In preparation, add code to keep track of when the offsets are invalid, and regenerate them.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/fdt_normal.py | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-)
Applied to u-boot-dm/next.

Since we want to be able to change the in-memory device tree using libfdt, use a bytearray instead of a string. This makes interfacing from Python easier.
Signed-off-by: Simon Glass sjg@chromium.org ---
lib/libfdt/libfdt.swig | 8 ++++++++ tools/dtoc/fdt_normal.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/lib/libfdt/libfdt.swig b/lib/libfdt/libfdt.swig index 14f583d..26d42fc 100644 --- a/lib/libfdt/libfdt.swig +++ b/lib/libfdt/libfdt.swig @@ -75,6 +75,14 @@ struct fdt_property { } %}
+%typemap(in) (const void *) { + if (!PyByteArray_Check($input)) { + SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname" "', argument " + "$argnum"" of type '" "$type""'"); + } + $1 = (void *) PyByteArray_AsString($input); +} + const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen); int fdt_path_offset(const void *fdt, const char *path); int fdt_first_property_offset(const void *fdt, int nodeoffset); diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index 4a667a1..eb45742 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -110,7 +110,7 @@ class FdtNormal(Fdt): self._fname = fdt_util.EnsureCompiled(self._fname)
with open(self._fname) as fd: - self._fdt = fd.read() + self._fdt = bytearray(fd.read())
def GetFdt(self): """Get the contents of the FDT

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
Since we want to be able to change the in-memory device tree using libfdt, use a bytearray instead of a string. This makes interfacing from Python easier.
Signed-off-by: Simon Glass sjg@chromium.org
lib/libfdt/libfdt.swig | 8 ++++++++ tools/dtoc/fdt_normal.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-)
Applied to u-boot-dm/next.

Add support for deleting a device tree property. With the fallback implementation this uses fdtput. With libfdt it uses the API call and updates the offsets afterwards.
Signed-off-by: Simon Glass sjg@chromium.org ---
lib/libfdt/libfdt.swig | 12 ++++++++++++ tools/dtoc/fdt.py | 10 ++++++++++ tools/dtoc/fdt_fallback.py | 13 +++++++++++++ tools/dtoc/fdt_normal.py | 20 ++++++++++++++++++++ 4 files changed, 55 insertions(+)
diff --git a/lib/libfdt/libfdt.swig b/lib/libfdt/libfdt.swig index 26d42fc..ce516fd 100644 --- a/lib/libfdt/libfdt.swig +++ b/lib/libfdt/libfdt.swig @@ -95,3 +95,15 @@ const char *fdt_get_name(const void *fdt, int nodeoffset, int *OUTPUT); const char *fdt_string(const void *fdt, int stroffset); int fdt_first_subnode(const void *fdt, int offset); int fdt_next_subnode(const void *fdt, int offset); + +%typemap(in) (void *) { + if (!PyByteArray_Check($input)) { + SWIG_exception_fail(SWIG_TypeError, "in method '" "$symname" "', argument " + "$argnum"" of type '" "$type""'"); + } + $1 = PyByteArray_AsString($input); +} + +int fdt_delprop(void *fdt, int nodeoffset, const char *name); + +const char *fdt_strerror(int errval); diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index c0ce5af..f01c7b1 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -184,6 +184,16 @@ class NodeBase: """ raise NotImplementedError()
+ def DeleteProp(self, prop_name): + """Delete a property of a node + + This should be implemented by subclasses + + Args: + prop_name: Name of the property to delete + """ + raise NotImplementedError() + class Fdt: """Provides simple access to a flat device tree blob.
diff --git a/tools/dtoc/fdt_fallback.py b/tools/dtoc/fdt_fallback.py index cb97cff..04cf830 100644 --- a/tools/dtoc/fdt_fallback.py +++ b/tools/dtoc/fdt_fallback.py @@ -69,6 +69,19 @@ class Node(NodeBase):
node.Scan()
+ def DeleteProp(self, prop_name): + """Delete a property of a node + + The property is deleted using fdtput. + + Args: + prop_name: Name of the property to delete + Raises: + CommandError if the property does not exist + """ + args = [self._fdt._fname, '-d', self.path, prop_name] + command.Output('fdtput', *args) + del self.props[prop_name]
class FdtFallback(Fdt): """Provides simple access to a flat device tree blob using fdtget/fdtput diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index eb45742..52d8055 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -20,6 +20,11 @@ import libfdt # This implementation uses a libfdt Python library to access the device tree, # so it is fairly efficient.
+def CheckErr(errnum, msg): + if errnum: + raise ValueError('Error %d: %s: %s' % + (errnum, libfdt.fdt_strerror(errnum), msg)) + class Prop(PropBase): """A device tree property
@@ -95,6 +100,21 @@ class Node(NodeBase): subnode.Refresh(offset) offset = libfdt.fdt_next_subnode(self._fdt.GetFdt(), offset)
+ def DeleteProp(self, prop_name): + """Delete a property of a node + + The property is deleted and the offset cache is invalidated. + + Args: + prop_name: Name of the property to delete + Raises: + ValueError if the property does not exist + """ + CheckErr(libfdt.fdt_delprop(self._fdt.GetFdt(), self.Offset(), prop_name), + "Node '%s': delete property: '%s'" % (self.path, prop_name)) + del self.props[prop_name] + self._fdt.Invalidate() + class FdtNormal(Fdt): """Provides simple access to a flat device tree blob using libfdt.

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
Add support for deleting a device tree property. With the fallback implementation this uses fdtput. With libfdt it uses the API call and updates the offsets afterwards.
Signed-off-by: Simon Glass sjg@chromium.org
lib/libfdt/libfdt.swig | 12 ++++++++++++ tools/dtoc/fdt.py | 10 ++++++++++ tools/dtoc/fdt_fallback.py | 13 +++++++++++++ tools/dtoc/fdt_normal.py | 20 ++++++++++++++++++++ 4 files changed, 55 insertions(+)
Applied to u-boot-dm/next.

After any node/property deletion the device tree can be packed to remove spare space. Add a way to perform this operation.
Note that for fdt_fallback, fdtput automatically packs the device tree after deletion, so no action is required here.
Signed-off-by: Simon Glass sjg@chromium.org ---
lib/libfdt/libfdt.swig | 1 + tools/dtoc/fdt.py | 16 ++++++++++++++++ tools/dtoc/fdt_normal.py | 11 +++++++++++ 3 files changed, 28 insertions(+)
diff --git a/lib/libfdt/libfdt.swig b/lib/libfdt/libfdt.swig index ce516fd..0cb7977 100644 --- a/lib/libfdt/libfdt.swig +++ b/lib/libfdt/libfdt.swig @@ -107,3 +107,4 @@ int fdt_next_subnode(const void *fdt, int offset); int fdt_delprop(void *fdt, int nodeoffset, const char *name);
const char *fdt_strerror(int errval); +int fdt_pack(void *fdt); diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index f01c7b1..403eb1f 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -240,3 +240,19 @@ class Fdt: return None return node
+ def Flush(self): + """Flush device tree changes back to the file + + If the device tree has changed in memory, write it back to the file. + Subclasses can implement this if needed. + """ + pass + + def Pack(self): + """Pack the device tree down to its minimum size + + When nodes and properties shrink or are deleted, wasted space can + build up in the device tree binary. Subclasses can implement this + to remove that spare space. + """ + pass diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index 52d8055..f2cf608 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -140,6 +140,17 @@ class FdtNormal(Fdt): """ return self._fdt
+ def Flush(self): + """Flush device tree changes back to the file""" + with open(self._fname, 'wb') as fd: + fd.write(self._fdt) + + def Pack(self): + """Pack the device tree down to its minimum size""" + CheckErr(libfdt.fdt_pack(self._fdt), 'pack') + fdt_len = libfdt.fdt_totalsize(self._fdt) + del self._fdt[fdt_len:] + def GetProps(self, node, path): """Get all properties from a node.

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
After any node/property deletion the device tree can be packed to remove spare space. Add a way to perform this operation.
Note that for fdt_fallback, fdtput automatically packs the device tree after deletion, so no action is required here.
Signed-off-by: Simon Glass sjg@chromium.org
lib/libfdt/libfdt.swig | 1 + tools/dtoc/fdt.py | 16 ++++++++++++++++ tools/dtoc/fdt_normal.py | 11 +++++++++++ 3 files changed, 28 insertions(+)
Applied to u-boot-dm/next.

Add a way to find the byte offset of a property within the device tree. This is only supported with the normal libfdt implementation since fdtget does not provide this information.
Signed-off-by: Simon Glass sjg@chromium.org ---
lib/libfdt/libfdt.swig | 3 +++ tools/dtoc/fdt.py | 11 +++++++++++ tools/dtoc/fdt_normal.py | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+)
diff --git a/lib/libfdt/libfdt.swig b/lib/libfdt/libfdt.swig index 0cb7977..b24c72b 100644 --- a/lib/libfdt/libfdt.swig +++ b/lib/libfdt/libfdt.swig @@ -108,3 +108,6 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name);
const char *fdt_strerror(int errval); int fdt_pack(void *fdt); + +int fdt_totalsize(const void *fdt); +int fdt_off_dt_struct(const void *fdt); diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index 403eb1f..816fdbe 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -144,6 +144,17 @@ class PropBase: else: return True
+ def GetOffset(self): + """Get the offset of a property + + This can be implemented by subclasses. + + Returns: + The offset of the property (struct fdt_property) within the + file, or None if not known. + """ + return None + class NodeBase: """A device tree node
diff --git a/tools/dtoc/fdt_normal.py b/tools/dtoc/fdt_normal.py index f2cf608..aae258e 100644 --- a/tools/dtoc/fdt_normal.py +++ b/tools/dtoc/fdt_normal.py @@ -43,6 +43,14 @@ class Prop(PropBase): return self.type, self.value = self.BytesToValue(bytes)
+ def GetOffset(self): + """Get the offset of a property + + Returns: + The offset of the property (struct fdt_property) within the file + """ + return self._node._fdt.GetStructOffset(self._offset) + class Node(NodeBase): """A device tree node
@@ -193,6 +201,16 @@ class FdtNormal(Fdt): """Refresh the offset cache""" self._root.Refresh(0)
+ def GetStructOffset(self, offset): + """Get the file offset of a given struct offset + + Args: + offset: Offset within the 'struct' region of the device tree + Returns: + Position of @offset within the device tree binary + """ + return libfdt.fdt_off_dt_struct(self._fdt) + offset + @classmethod def Node(self, fdt, offset, name, path): """Create a new node

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
Add a way to find the byte offset of a property within the device tree. This is only supported with the normal libfdt implementation since fdtget does not provide this information.
Signed-off-by: Simon Glass sjg@chromium.org
lib/libfdt/libfdt.swig | 3 +++ tools/dtoc/fdt.py | 11 +++++++++++ tools/dtoc/fdt_normal.py | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+)
Applied to u-boot-dm/next.

The style is to use single quotes for strings where possible. Adjust this function.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/fdt_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 3e25a8b..32f41d7 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -22,7 +22,7 @@ def fdt32_to_cpu(val): Return: A native-endian integer value """ - return struct.unpack(">I", val)[0] + return struct.unpack('>I', val)[0]
def EnsureCompiled(fname): """Compile an fdt .dts source file into a .dtb binary blob if needed.

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
The style is to use single quotes for strings where possible. Adjust this function.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/fdt_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Applied to u-boot-dm/next.

Provide easy helpers for reading integer, string and boolean values from device-tree properties.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/dtoc/fdt_util.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 32f41d7..3a10838 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -59,3 +59,28 @@ def EnsureCompiled(fname): args.append(dts_input) command.Run('dtc', *args) return dtb_output + +def GetInt(node, propname, default=None): + prop = node.props.get(propname) + if not prop: + return default + value = fdt32_to_cpu(prop.value) + if type(value) == type(list): + raise ValueError("Node '%s' property '%' has list value: expecting" + "a single integer" % (node.name, propname)) + return value + +def GetString(node, propname, default=None): + prop = node.props.get(propname) + if not prop: + return default + value = prop.value + if type(value) == type(list): + raise ValueError("Node '%s' property '%' has list value: expecting" + "a single string" % (node.name, propname)) + return value + +def GetBool(node, propname, default=False): + if propname in node.props: + return True + return default

On 25 July 2016 at 18:59, Simon Glass sjg@chromium.org wrote:
Provide easy helpers for reading integer, string and boolean values from device-tree properties.
Signed-off-by: Simon Glass sjg@chromium.org
tools/dtoc/fdt_util.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
Applied to u-boot-dm/next.

This adds the basic code for binman, including command parsing, processing of entries and generation of images.
So far no entry types are supported. These will be added in future commits as examples of how to add new types.
See the README for documentation.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/binman/.gitignore | 1 + tools/binman/README | 491 ++++++++++++++++++++++++++++++++++++++++++++ tools/binman/binman | 1 + tools/binman/binman.py | 96 +++++++++ tools/binman/cmdline.py | 48 +++++ tools/binman/control.py | 106 ++++++++++ tools/binman/etype/entry.py | 186 +++++++++++++++++ tools/binman/image.py | 233 +++++++++++++++++++++ 8 files changed, 1162 insertions(+) create mode 100644 tools/binman/.gitignore create mode 100644 tools/binman/README create mode 120000 tools/binman/binman create mode 100755 tools/binman/binman.py create mode 100644 tools/binman/cmdline.py create mode 100644 tools/binman/control.py create mode 100644 tools/binman/etype/entry.py create mode 100644 tools/binman/image.py
diff --git a/tools/binman/.gitignore b/tools/binman/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/tools/binman/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/tools/binman/README b/tools/binman/README new file mode 100644 index 0000000..c73fb3c --- /dev/null +++ b/tools/binman/README @@ -0,0 +1,491 @@ +# Copyright (c) 2016 Google, Inc +# +# SPDX-License-Identifier: GPL-2.0+ +# + +Introduction +------------ + +Firmware often consists of several components which must be packaged together. +For example, we may have SPL, U-Boot, a device tree and an environment area +grouped together and placed in MMC flash. When the system starts, it must be +able to find these pieces. + +So far U-Boot has not provided a way to handle creating such images in a +general way. Each SoC does what it needs to build an image, often packing or +concatenating images in the U-Boot build system. + +Binman aims to provide a mechanism for building images, from simple +SPL + U-Boot combinations, to more complex arrangements with many parts. + + +What it does +------------ + +Binman reads your board's device tree and finds a node which describes the +required image layout. It uses this to work out what to place where. The +output file normally contains the device tree, so it is in principle possible +to read an image and extract its constituent parts. + + +Features +-------- + +So far binman is pretty simple. It supports binary blobs, such as 'u-boot', +'spl' and 'fdt'. It supports empty entries (such as setting to 0xff). It can +place entries at a fixed location in the image, or fit them together with +suitable padding and alignment. It provides a way to process binaries before +they are included, by adding a Python plug-in. The device tree is available +to U-Boot at run-time so that the images can be interpreted. + +Binman does not yet update the device tree with the final location of +everything when it is done. A simple C structure could be generated for +constrained environments like SPL (using dtoc) but this is also not +implemented. + +Binman can also support incorporating filesystems in the image if required. +For example x86 platforms may use CBFS in some cases. + +Binman is intended for use with U-Boot but is designed to be general enough +to be useful in other image-packaging situations. + + +Motivation +---------- + +Packaging of firmware is quite a different task from building the various +parts. In many cases the various binaries which go into the image come from +separate build systems. For example, ARM Trusted Firmware is used on ARMv8 +devices but is not built in the U-Boot tree. If a Linux kernel is included +in the firmware image, it is built elsewhere. + +It is of course possible to add more and more build rules to the U-Boot +build system to cover these cases. It can shell out to other Makefiles and +build scripts. But it seems better to create a clear divide between building +software and packaging it. + +At present this is handled by manual instructions, different for each board, +on how to create images that will boot. By turning these instructions into a +standard format, we can support making valid images for any board without +manual effort, lots of READMEs, etc. + +Benefits: +- Each binary can have its own build system and tool chain without creating +any dependencies between them +- Avoids the need for a single-shot build: individual parts can be updated +and brought in as needed +- Provides for a standard image description available in the build and at +run-time +- SoC-specific image-signing tools can be accomodated +- Avoids cluttering the U-Boot build system with image-building code +- The image description is automatically available at run-time in U-Boot, +SPL. It can be made available to other software also +- The image description is easily readable (it's a text file in device-tree +format) and permits flexible packing of binaries + + +Terminology +----------- + +Binman uses the following terms: + +- image - an output file containing a firmware image +- binary - an input binary that goes into the image + + +Relationship to FIT +------------------- + +FIT is U-Boot's official image format. It supports multiple binaries with +load / execution addresses, compression. It also supports verification +through hashing and RSA signatures. + +FIT was originally designed to support booting a Linux kernel (with an +optional ramdisk) and device tree chosen from various options in the FIT. +Now that U-Boot supports configuration via device tree, it is possible to +load U-Boot from a FIT, with the device tree chosen by SPL. + +Binman considers FIT to be one of the binaries it can place in the image. + +Where possible it is best to put as much as possible in the FIT, with binman +used to deal with cases not covered by FIT. Examples include initial +execution (since FIT itself does not have an executable header) and dealing +with device boundaries, such as the read-only/read-write separation in SPI +flash. + +For U-Boot, binman should not be used to create ad-hoc images in place of +FIT. + + +Relationship to mkimage +----------------------- + +The mkimage tool provides a means to create a FIT. Traditionally it has +needed an image description file: a device tree, like binman, but in a +different format. More recently it has started to support a '-f auto' mode +which can generate that automatically. + +More relevant to binman, mkimage also permits creation of many SoC-specific +image types. These can be listed by running 'mkimage -T list'. Examples +include 'rksd', the Rockchip SD/MMC boot format. The mkimage tool is often +called from the U-Boot build system for this reason. + +Binman considers the output files created by mkimage to be binary blobs +which it can place in an image. Binman does not replace the mkimage tool or +this purpose. It would be possible in some situtions to create a new entry +type for the images in mkimage, but this would not add functionality. It +seems better to use the mkiamge tool to generate binaries and avoid blurring +the boundaries between building input files (mkimage) and packaging then +into a final image (binman). + + +Example use of binman in U-Boot +------------------------------- + +Binman aims to replace some of the ad-hoc image creation in the U-Boot +build system. + +Consider sunxi. It has the following steps: + +1. It uses a custom mksunxiboot tool to build an SPL image called +sunxi-spl.bin. This should probably move into mkimage. + +2. It uses mkimage to package U-Boot into a legacy image file (so that it can +hold the load and execution address) called u-boot.img. + +3. It builds a final output image called u-boot-sunxi-with-spl.bin which +consists of sunxi-spl.bin, some padding and u-boot.img. + +Binman is intended to replace the last step. The U-Boot build system builds +u-boot.bin and sunxi-spl.bin. Binman can then take over creation of +sunxi-spl.bin (by calling mksunxiboot, or hopefully one day mkimage). In any +case, it would then create the image from the component parts. + +This simplifies the U-Boot Makefile somewhat, since various pieces of logic +can be replaced by a call to binman. + + +Example use of binman for x86 +----------------------------- + +In most cases x86 images have a lot of binary blobs, 'black-box' code +provided by Intel which must be run for the platform to work. Typically +these blobs are not relocatable and must be placed at fixed areas in the +firmare image. + +Currently this is handled by ifdtool, which places microcode, FSP, MRC, VGA +BIOS, reference code and Intel ME binaries into a u-boot.rom file. + +Binman is intended to replace all of this, with ifdtool left to handle only +the configuration of the Intel-format descriptor. + + +Running binman +-------------- + +Type: + + binman -b <board_name> + +to build an image for a board. The board name is the same name used when +configuring U-Boot (e.g. for sandbox_defconfig the board name is 'sandbox'). +Binman assumes that the input files for the build are in ../b/<board_name>. + +Or you can specify this explicitly: + + binman -I <build_path> + +where <build_path> is the build directory containing the output of the U-Boot +build. + +(Future work will make this more configurable) + +In either case, binman picks up the device tree file (u-boot.dtb) and looks +for its instructions in the 'binman' node. + +Binman has a few other options which you can see by running 'binman -h'. + + +Image description format +------------------------ + +The binman node is called 'binman'. An example image description is shown +below: + + binman { + filename = "u-boot-sunxi-with-spl.bin"; + pad-byte = <0xff>; + blob { + filename = "spl/sunxi-spl.bin"; + }; + u-boot { + pos = <CONFIG_SPL_PAD_TO>; + }; + }; + + +This requests binman to create an image file called u-boot-sunxi-with-spl.bin +consisting of a specially formatted SPL (spl/sunxi-spl.bin, built by the +normal U-Boot Makefile), some 0xff padding, and a U-Boot legacy image. The +padding comes from the fact that the second binary is placed at +CONFIG_SPL_PAD_TO. If that line were omitted then the U-Boot binary would +immediately follow the SPL binary. + +The binman node describes an image. The sub-nodes describe entries in the +image. Each entry represents a region within the overall image. The name of +the entry (blob, u-boot) tells binman what to put there. For 'blob' we must +provide a filename. For 'u-boot', binman knows that this means 'u-boot.bin'. + +Entries are normally placed into the image sequentially, one after the other. +The image size is the total size of all entries. As you can see, you can +specify the start position of an entry using the 'pos' property. + +Note that due to a device tree requirement, all entries must have a unique +name. If you want to put the same binary in the image multiple times, you can +use any unique name, with the 'type' property providing the type. + +The attributes supported for entries are described below. + +pos: + This sets the position of an entry within the image. The first byte + of the image is normally at position 0. If 'pos' is not provided, + binman sets it to the end of the previous region, or the start of + the image's entry area (normally 0) if there is no previous region. + +align: + This sets the alignment of the entry. The entry position is adjusted + so that the entry starts on an aligned boundary within the image. For + example 'align = <16>' means that the entry will start on a 16-byte + boundary. Alignment shold be a power of 2. If 'align' is not + provided, no alignment is performed. + +size: + This sets the size of the entry. The contents will be padded out to + this size. If this is not provided, it will be set to the size of the + contents. + +pad-before: + Padding before the contents of the entry. Normally this is 0, meaning + that the contents start at the beginning of the entry. This can be + offset the entry contents a little. Defaults to 0. + +pad-after: + Padding after the contents of the entry. Normally this is 0, meaning + that the entry ends at the last byte of content (unless adjusted by + other properties). This allows room to be created in the image for + this entry to expand later. Defaults to 0. + +align-size: + This sets the alignment of the entry size. For example, to ensure + that the size of an entry is a multiple of 64 bytes, set this to 64. + If 'align-size' is not provided, no alignment is performed. + +align-end: + This sets the alignment of the end of an entry. Some entries require + that they end on an alignment boundary, regardless of where they + start. If 'align-end' is not provided, no alignment is performed. + + Note: This is not yet implemented in binman. + +filename: + For 'blob' types this provides the filename containing the binary to + put into the entry. If binman knows about the entry type (like + u-boot-bin), then there is no need to specify this. + +type: + Sets the type of an entry. This defaults to the entry name, but it is + possible to use any name, and then add (for example) 'type = "u-boot"' + to specify the type. + + +The attributes supported for images are described below. Several are similar +to those for entries. + +size: + Sets the image size in bytes, for example 'size = <0x100000>' for a + 1MB image. + +align-size: + This sets the alignment of the image size. For example, to ensure + that the image ends on a 512-byte boundary, use 'align-size = <512>'. + If 'align-size' is not provided, no alignment is performed. + +pad-before: + This sets the padding before the image entries. The first entry will + be positionad after the padding. This defaults to 0. + +pad-after: + This sets the padding after the image entries. The padding will be + placed after the last entry. This defaults to 0. + +pad-byte: + This specifies the pad byte to use when padding in the image. It + defaults to 0. To use 0xff, you would add 'pad-byte = <0xff>'. + +filename: + This specifies the image filename. It defaults to 'image.bin'. + +sort-by-pos: + This causes binman to reorder the entries as needed to make sure they + are in increasing positional order. This can be used when your entry + order may not match the positional order. A common situation is where + the 'pos' properties are set by CONFIG options, so their ordering is + not known a priori. + + This is a boolean property so needs no value. To enable it, add a + line 'sort-by-pos;' to your description. + +multiple-images: + Normally only a single image is generated. To create more than one + image, put this property in the binman node. For example, this will + create image1.bin containing u-boot.bin, and image2.bin containing + both spl/u-boot-spl.bin and u-boot.bin: + + binman { + multiple-images; + image1 { + u-boot { + }; + }; + + image2 { + spl { + }; + u-boot { + }; + }; + }; + +end-at-4gb: + For x86 machines the ROM positions start just before 4GB and extend + up so that the image finished at the 4GB boundary. This boolean + option can be enabled to support this. The image size must be + provided so that binman knows when the image should start. For an + 8MB ROM, the position of the first entry would be 0xfff80000 with + this option, instead of 0 without this option. + + +Examples of the above options can be found in the tests. See the +tools/binman/test directory. + + +Order of image creation +----------------------- + +Image creation proceeds in the following order, for each entry in the image. + +1. GetEntryContents() - the contents of each entry are obtained, normally by +reading from a file. This calls the Entry.ObtainContents() to read the +contents. The default version of Entry.ObtainContents() calls +Entry.GetDefaultFilename() and then reads that file. So a common mechanism +to select a file to read is to override that function in the subclass. The +functions must return True when they have read the contents. Binman will +retry calling the functions a few times if False is returned, allowing +dependencies between the contents of different entries. + +2. GetEntryPositions() - calls Entry.GetPositions() for each entry. This can +return a dict containing entries that need updating. The key should be the +entry name and the value is a tuple (pos, size). This allows an entry to +provide the position and size for other entries. The default implementation +of GetEntryPositions() returns {}. + +3. PackEntries() - calls Entry.Pack() which figures out the position and +size of an entry. The 'current' image position is passed in, and the function +returns the position immediately after the entry being packed. The default +implementation of Pack() is usually sufficient. + +4. CheckSize() - checks that the contents of all the entries fits within +the image size. If the image does not have a defined size, the size is set +large enough to hold all the entries. + +5. CheckEntries() - checks that the entries do not overlap, nor extend +outside the image. + +6. ProcessEntryContents() - this calls Entry.ProcessContents() on each entry. +The default implementatoin does nothing. This can be overriden to adjust the +contents of an entry in some way. For example, it would be possible to create +an entry containing a hash of the contents of some other entries. At this +stage the position and size of entries should not be adjusted. + +7. BuildImage() - builds the image and writes it to a file. This is the final +step. + + +Advanced Features / Technical docs +---------------------------------- + +The behaviour of entries is defined by the Entry class. All other entries are +a subclass of this. An important subclass is Entry_blob which takes binary +data from a file and places it in the entry. In fact most entry types are +subclasses of Entry_blob. + +Each entry type is a separate file in the tools/binman/etype directory. Each +file contains a class called Entry_<type> where <type> is the entry type. +New entry types can be supported by adding new files in that directory. +These will automatically be detected by binman when needed. + +Entry properties are documented in entry.py. The entry subclasses are free +to change the values of properties to support special behaviour. For example, +when Entry_blob loads a file, it sets content_size to the size of the file. +Entry classes can adjust other entries. For example, an entry that knows +where other entries should be positioned can set up those entries' positions +so they don't need to be set in the binman decription. It can also adjust +entry contents. + +Most of the time such essoteric behaviour is not needed, but it can be +essential for complex images. + + +History / Credits +----------------- + +Binman takes a lot of inspiration from a Chrome OS tool called +'cros_bundle_firmware', which I wrote some years ago. That tool was based on +a reasonably simple and sound design but has expanded greatly over the +years. In particular its handling of x86 images is convoluted. + +Quite a few lessons have been learned which are hopefully be applied here. + + +Design notes +------------ + +On the face of it, a tool to create firmware images should be fairly simple: +just find all the input binaries and place them at the right place in the +image. The difficulty comes from the wide variety of input types (simple +flat binaries containing code, packaged data with various headers), packing +requirments (alignment, spacing, device boundaries) and other required +features such as hierarchical images. + +The design challenge is to make it easy to create simple images, while +allowing the more complex cases to be supported. For example, for most +images we don't much care exactly where each binary ends up, so we should +not have to specify that unnecessarily. + +New entry types should aim to provide simple usage where possible. If new +core features are needed, they can be added in the Entry base class. + + +To do +----- + +Some ideas: +- Fill out the device tree to include the final position and size of each + entry (since the input file may not always specify these) +- Use of-platdata to make the information available to code that is unable + to use device tree (such as a very small SPL image) +- Write an image map to a text file +- Allow easy building of images by specifying just the board name +- Produce a full Python binding for libfdt (for upstream) +- Add an option to decode an image into the constituent binaries +- Suppoort hierarchical images (packing of binaries into another binary + which is then placed in the image) +- Support building an image for a board (-b) more completely, with a + configurable build directory +- Consider making binman work with buildman, although if it is used in the + Makefile, this will be automatic +- Implement align-end + +-- +Simon Glass sjg@chromium.org +7/7/2016 diff --git a/tools/binman/binman b/tools/binman/binman new file mode 120000 index 0000000..979b7e4 --- /dev/null +++ b/tools/binman/binman @@ -0,0 +1 @@ +binman.py \ No newline at end of file diff --git a/tools/binman/binman.py b/tools/binman/binman.py new file mode 100755 index 0000000..8bf1f1f --- /dev/null +++ b/tools/binman/binman.py @@ -0,0 +1,96 @@ +#!/usr/bin/python + +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Creates binary images from input files controlled by a description +# + +"""See README for more information""" + +import os +import sys +import traceback +import unittest + +# Bring in the patman and dtoc libraries +our_path = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(our_path, '../patman')) +sys.path.append(os.path.join(our_path, '../dtoc')) + +# For testing of the Python libfdt module +# sys.path.append(os.path.join(our_path, '../../b/sandbox_spl/tools')) + +# Also allow entry-type modules to be brought in from the etype directory. +sys.path.append(os.path.join(our_path, 'etype')) + +import cmdline +import command +import control + +def RunTests(): + """Run the functional tests and any embedded doctests""" + import func_test + import test + import doctest + + result = unittest.TestResult() + for module in []: + suite = doctest.DocTestSuite(module) + suite.run(result) + + sys.argv = [sys.argv[0]] + for module in (func_test.TestFunctional,): + suite = unittest.TestLoader().loadTestsFromTestCase(module) + suite.run(result) + + print result + for test, err in result.errors: + print err + for test, err in result.failures: + print err + +def RunBinman(options, args): + """Main entry point to binman once arguments are parsed + + Args: + options: Command-line options + args: Non-option arguments + """ + ret_code = 0 + + # For testing: This enables full exception traces. + #options.debug = True + + if not options.debug: + sys.tracebacklimit = 0 + + if options.test: + RunTests() + + elif options.full_help: + pager = os.getenv('PAGER') + if not pager: + pager = 'more' + fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), + 'README') + command.Run(pager, fname) + + else: + try: + ret_code = control.Binman(options, args) + except Exception as e: + print e + if options.debug: + print + traceback.print_exc() + ret_code = 1 + return ret_code + + +if __name__ == "__main__": + (options, args) = cmdline.ParseArgs(sys.argv) + ret_code = RunBinman(options, args) + sys.exit(ret_code) diff --git a/tools/binman/cmdline.py b/tools/binman/cmdline.py new file mode 100644 index 0000000..0264b2f --- /dev/null +++ b/tools/binman/cmdline.py @@ -0,0 +1,48 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Command-line parser for binman +# + +from optparse import OptionParser + +def ParseArgs(argv): + """Parse the binman command-line arguments + + Args: + argv: List of string arguments + Returns: + Tuple (options, args) with the command-line options and arugments. + options provides access to the options (e.g. option.debug) + args is a list of string arguments + """ + parser = OptionParser() + parser.add_option('-b', '--board', type='string', + help='Board name to build') + parser.add_option('-d', '--dt', type='string', + help='Configuration file (.dtb) to use') + parser.add_option('-D', '--debug', action='store_true', + help='Enabling debugging (provides a full traceback on error)') + parser.add_option('-I', '--indir', action='append', + help='Add a path to a directory to use for input files') + parser.add_option('-H', '--full-help', action='store_true', dest='full_help', + default=False, help='Display the README file') + parser.add_option('-O', '--outdir', dest='outdir', type='string', + action='store', help='Path to directory to use for intermediate and ' + 'output files') + parser.add_option('-p', '--preserve', dest='preserve', action='store_true',\ + help='Preserve temporary output directory even if option -O is not given') + parser.add_option('-t', '--test', action='store_true', dest='test', + default=False, help='run tests') + parser.add_option('-v', '--verbosity', dest='verbosity', default=1, + type='int', help='Control verbosity: 0=silent, 1=progress, 3=full, ' + '4=debug') + + parser.usage += """ + +Create images for a board from a set of binaries. It is controlled by a +description in the board device tree.""" + + return parser.parse_args(argv) diff --git a/tools/binman/control.py b/tools/binman/control.py new file mode 100644 index 0000000..b411d6d --- /dev/null +++ b/tools/binman/control.py @@ -0,0 +1,106 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Creates binary images from input files controlled by a description +# + +from collections import OrderedDict +import os +import sys +import tools + +import fdt_select +import fdt_util +from image import Image +import tout + +# List of images we plan to create +# Make this global so that it can be referenced from tests +images = OrderedDict() + +def _ReadImageDesc(binman_node): + """Read the image descriptions from the /binman node + + This normally produces a single Image object called 'image'. But if + multiple images are present, they will all be returned. + + Args: + binman_node: Node object of the /binman node + Returns: + OrderedDict of Image objects, each of which describes an image + """ + images = OrderedDict() + if 'multiple-images' in binman_node.props: + for node in binman_node.subnodes: + images[node.name] = Image(node.name, node) + else: + images['image'] = Image('image', binman_node) + return images + +def _FindBinmanNode(fdt): + """Find the 'binman' node in the device tree + + Args: + fdt: Fdt object to scan + Returns: + Node object of /binman node, or None if not found + """ + for node in fdt.GetRoot().subnodes: + if node.name == 'binman': + return node + return None + +def Binman(options, args): + """The main control code for binman + + This assumes that help and test options have already been dealt with. It + deals with the core task of building images. + + Args: + options: Command line options object + args: Command line arguments (list of strings) + """ + global images + + # Try to figure out which device tree contains our image description + if options.dt: + dtb_fname = options.dt + else: + board = options.board + if not board: + raise ValueError('Must provide a board to process') + # TODO(sjg@chromium.org): This needs to be configurable + board_fname = os.path.join('b', board) + dtb_fname = os.path.join(board_fname, 'u-boot.dtb') + + try: + tout.Init(options.verbosity) + try: + tools.SetInputDirs(options.indir) + tools.PrepareOutputDir(options.outdir, options.preserve) + fdt = fdt_select.FdtScan(dtb_fname) + node = _FindBinmanNode(fdt) + if not node: + raise ValueError("Device tree '%s' does not have a 'binman' " + "node" % dtb_fname) + images = _ReadImageDesc(node) + for image in images.values(): + # Perform all steps for this image, including checking and + # writing it. This means that errors found with a later + # image will be reported after earlier images are already + # completed and written, but that does not seem important. + image.GetEntryContents() + image.GetEntryPositions() + image.PackEntries() + image.CheckSize() + image.CheckEntries() + image.ProcessEntryContents() + image.BuildImage() + finally: + tools.FinaliseOutputDir() + finally: + tout.Uninit() + + return 0 diff --git a/tools/binman/etype/entry.py b/tools/binman/etype/entry.py new file mode 100644 index 0000000..a4fe53c --- /dev/null +++ b/tools/binman/etype/entry.py @@ -0,0 +1,186 @@ +# Copyright (c) 2016 Google, Inc +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Base class for all entries +# + +import importlib + +import fdt_util +import tools + +modules = {} + +class Entry(object): + """An Entry in the image + + An entry corresponds to a single node in the device-tree description + of the image. Each entry ends up being a part of the final image. + Entries can be placed either right next to each other, or with padding + between them. The type of the entry determines the data that is in it. + + This class is not used by itself. All entry objects are subclasses of + Entry. + + Attributes: + image: The image containing this entry + node: The node that created this entry + pos: Absolute position of entry within the image, None if not known + size: Entry size in bytes, None if not known + contents_size: Size of contents in bytes, 0 by default + align: Entry start position alignment, or None + align_size: Entry size alignment, or None + align_end: Entry end position alignment, or None + pad_before: Number of pad bytes before the contents, 0 if none + pad_after: Number of pad bytes after the contents, 0 if none + data: Contents of entry (string of bytes) + """ + def __init__(self, image, etype, node): + self.image = image + self.etype = etype + self._node = node + self.pos = None + self.size = None + self.contents_size = 0 + self.align = None + self.align_size = None + self.align_end = None + self.pad_before = 0 + self.pad_after = 0 + self.pos_unset = False + self.ReadNode() + + @staticmethod + def Create(image, node): + """Create a new entry for a node. + + Args: + node: Node object containing information about the entry to create + + Returns: + A new Entry object of the correct type (a subclass of Entry) + """ + etype = fdt_util.GetString(node, 'type', node.name) + module_name = etype.replace('-', '_') + module = modules.get(module_name) + + # Import the module if we have not already done so. + if not module: + try: + module = importlib.import_module(module_name) + except ImportError: + raise ValueError("Unknown entry type '%s' in node '%s'" % + (etype, node.path)) + modules[module_name] = module + + # Call its constructor to get the object we want. + obj = getattr(module, 'Entry_%s' % module_name) + return obj(image, etype, node) + + def ReadNode(self): + """Read entry information from the node + + This reads all the fields we recognise from the node, ready for use. + """ + self.pos = fdt_util.GetInt(self._node, 'pos') + self.size = fdt_util.GetInt(self._node, 'size') + self.align = fdt_util.GetInt(self._node, 'align') + if tools.NotPowerOfTwo(self.align): + raise ValueError("Node '%s': Alignment %s must be a power of two" % + (self._node.path, self.align)) + self.pad_before = fdt_util.GetInt(self._node, 'pad-before', 0) + self.pad_after = fdt_util.GetInt(self._node, 'pad-after', 0) + self.align_size = fdt_util.GetInt(self._node, 'align-size') + if tools.NotPowerOfTwo(self.align_size): + raise ValueError("Node '%s': Alignment size %s must be a power " + "of two" % (self._node.path, self.align_size)) + self.align_end = fdt_util.GetInt(self._node, 'align-end') + self.pos_unset = fdt_util.GetBool(self._node, 'pos-unset') + + def ObtainContents(self): + """Figure out the contents of an entry. + + Returns: + True if the contents were found, False if another call is needed + after the other entries are processed. + """ + # No contents by default: subclasses can implement this + return True + + def Pack(self, pos): + """Figure out how to pack the entry into the image + + Most of the time the entries are not fully specified. There may be + an alignment but no size. In that case we take the size from the + contents of the entry. + + If an entry has no hard-coded position, it will be placed at @pos. + + Once this function is complete, both the position and size of the + entry will be know. + + Args: + Current image position pointer + + Returns: + New image position pointer (after this entry) + """ + if self.pos is None: + if self.pos_unset: + self.Raise('No position set with pos-unset: should another ' + 'entry provide this correct position?') + self.pos = tools.Align(pos, self.align) + needed = self.pad_before + self.contents_size + self.pad_after + needed = tools.Align(needed, self.align_size) + size = self.size + if not size: + size = needed + new_pos = self.pos + size + aligned_pos = tools.Align(new_pos, self.align_end) + if aligned_pos != new_pos: + size = aligned_pos - self.pos + new_pos = aligned_pos + + if not self.size: + self.size = size + + if self.size < needed: + self.Raise("Entry contents size is %#x (%d) but entry size is " + "%#x (%d)" % (needed, needed, self.size, self.size)) + # Check that the alignment is correct. It could be wrong if the + # and pos or size values were provided (i.e. not calculated), but + # conflict with the provided alignment values + if self.size != tools.Align(self.size, self.align_size): + self.Raise("Size %#x (%d) does not match align-size %#x (%d)" % + (self.size, self.size, self.align_size, self.align_size)) + if self.pos != tools.Align(self.pos, self.align): + self.Raise("Position %#x (%d) does not match align %#x (%d)" % + (self.pos, self.pos, self.align, self.align)) + + return new_pos + + def Raise(self, msg): + """Convenience function to raise an error referencing a node""" + raise ValueError("Node '%s': %s" % (self._node.path, msg)) + + def GetPath(self): + """Get the path of a node + + Returns: + Full path of the node for this entry + """ + return self._node.path + + def GetData(self): + return self.data + + def GetPositions(self): + return {} + + def SetPositionSize(self, pos, size): + self.pos = pos + self.size = size + + def ProcessContents(self): + pass diff --git a/tools/binman/image.py b/tools/binman/image.py new file mode 100644 index 0000000..ab2ed16 --- /dev/null +++ b/tools/binman/image.py @@ -0,0 +1,233 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Class for an image, the output of binman +# + +from collections import OrderedDict +from operator import attrgetter + +import entry +from entry import Entry +import fdt_util +import tools + +class Image: + """A Image, representing an output from binman + + An image is comprised of a collection of entries each containing binary + data. The image size must be large enough to hold all of this data. + + This class implements the various operations needed for images. + + Atrtributes: + _node: Node object that contains the image definition in device tree + _name: Image name + _size: Image size in bytes, or None if not known yet + _align_size: Image size alignment, or None + _pad_before: Number of bytes before the first entry starts. This + effectively changes the place where entry position 0 starts + _pad_after: Number of bytes after the last entry ends. The last + entry will finish on or before this boundary + _pad_byte: Byte to use to pad the image where there is no entry + _filename: Output filename for image + _sort: True if entries should be sorted by position, False if they + must be in-order in the device tree description + _skip_at_start: Number of bytes before the first entry starts. These + effecively adjust the starting position of entries. For example, + if _pad_before is 16, then the first entry would start at 16. + An entry with pos = 20 would in fact be written at position 4 + in the image file. + _end_4gb: Indicates that the image ends at the 4GB boundary. This is + used for x86 images, which want to use positions such that a + memory address (like 0xff800000) is the first entry position. + This causes _skip_at_start to be set to the starting memory + address. + _entries: OrderedDict() of entries + """ + def __init__(self, name, node): + self._node = node + self._name = name + self._size = None + self._align_size = None + self._pad_before = 0 + self._pad_after = 0 + self._pad_byte = 0 + self._filename = '%s.bin' % self._name + self._sort = False + self._skip_at_start = 0 + self._end_4gb = False + self._entries = OrderedDict() + + self._ReadNode() + self._ReadEntries() + + def _ReadNode(self): + """Read properties from the image node""" + self._size = fdt_util.GetInt(self._node, 'size') + self._align_size = fdt_util.GetInt(self._node, 'align-size') + if tools.NotPowerOfTwo(self._align_size): + self._Raise("Alignment size %s must be a power of two" % + self._align_size) + self._pad_before = fdt_util.GetInt(self._node, 'pad-before', 0) + self._pad_after = fdt_util.GetInt(self._node, 'pad-after', 0) + self._pad_byte = fdt_util.GetInt(self._node, 'pad-byte', 0) + filename = fdt_util.GetString(self._node, 'filename') + if filename: + self._filename = filename + self._sort = fdt_util.GetBool(self._node, 'sort-by-pos') + self._end_4gb = fdt_util.GetBool(self._node, 'end-at-4gb') + if self._end_4gb and not self._size: + self._Raise("Image size must be provided when using end-at-4gb") + if self._end_4gb: + self._skip_at_start = 0x100000000 - self._size + + def __len__(self): + """Helper to return the number of entries""" + return len(self._entries) + + def CheckSize(self): + """Check that the image contents does not exceed its size, etc.""" + contents_size = 0 + for entry in self._entries.values(): + contents_size = max(contents_size, entry.pos + entry.size) + + contents_size -= self._skip_at_start + + size = self._size + if not size: + size = self._pad_before + contents_size + self._pad_after + size = tools.Align(size, self._align_size) + + if self._size and contents_size > self._size: + self._Raise("contents size %#x (%d) exceeds image size %#x (%d)" % + (contents_size, contents_size, self._size, self._size)) + if not self._size: + self._size = size + if self._size != tools.Align(self._size, self._align_size): + self._Raise("Size %#x (%d) does not match align-size %#x (%d)" % + (self._size, self._size, self._align_size, self._align_size)) + + def _Raise(self, msg): + """Raises an error for this image + + Args: + msg: Error message to use in the raise string + Raises: + ValueError() + """ + raise ValueError("Image '%s': %s" % (self._node.path, msg)) + + def _ReadEntries(self): + for node in self._node.subnodes: + self._entries[node.name] = Entry.Create(self, node) + + def FindEntryType(self, etype): + """Find an entry type in the image + + Args: + etype: Entry type to find + Returns: + entry matching that type, or None if not found + """ + for entry in self._entries.values(): + if entry.etype == etype: + return entry + return None + + def GetEntryContents(self): + """Call ObtainContents() for each entry + + This calls each entry's ObtainContents() a few times until they all + return True. We stop calling an entry's function once it returns + True. This allows the contents of one entry to depend on another. + + After 3 rounds we give up since it's likely an error. + """ + todo = self._entries.values() + for passnum in range(3): + next_todo = [] + for entry in todo: + if not entry.ObtainContents(): + next_todo.append(entry) + todo = next_todo + if not todo: + break + + def _SetEntryPosSize(self, name, pos, size): + """Set the position and size of an entry + + Args: + name: Entry name to update + pos: New position + size: New size + """ + entry = self._entries.get(name) + if not entry: + self.Raise("Unable to set pos/size for unknown entry '%s'" % name) + entry.SetPositionSize(self._skip_at_start + pos, size) + + def GetEntryPositions(self): + """Handle entries that want to set the position/size of other entries + + This calls each entry's GetPositions() method. If it returns a list + of entries to update, it updates them. + """ + for entry in self._entries.values(): + pos_dict = entry.GetPositions() + for name, info in pos_dict.iteritems(): + self._SetEntryPosSize(name, *info) + + def PackEntries(self): + """Pack all entries into the image""" + pos = self._skip_at_start + for entry in self._entries.values(): + pos = entry.Pack(pos) + + def _SortEntries(self): + """Sort entries by position""" + entries = sorted(self._entries.values(), key=lambda entry: entry.pos) + self._entries.clear() + for entry in entries: + self._entries[entry._node.name] = entry + + def CheckEntries(self): + """Check that entries do not overlap or extend outside the image""" + if self._sort: + self._SortEntries() + pos = 0 + prev_name = 'None' + for entry in self._entries.values(): + if (entry.pos < self._skip_at_start or + entry.pos >= self._skip_at_start + self._size): + entry.Raise("Position %#x (%d) is outsize the image starting " + "at %#x (%d)" % + (entry.pos, entry.pos, self._skip_at_start, + self._skip_at_start)) + if entry.pos < pos: + entry.Raise("Position %#x (%d) overlaps with previous entry '%s' " + "ending at %#x (%d)" % + (entry.pos, entry.pos, prev_name, pos, pos)) + pos = entry.pos + entry.size + prev_name = entry.GetPath() + + def ProcessEntryContents(self): + """Call the ProcessContents() method for each entry + + This is intended to adjust the contents as needed by the entry type. + """ + for entry in self._entries.values(): + entry.ProcessContents() + + def BuildImage(self): + """Write the image to a file""" + fname = tools.GetOutputFilename(self._filename) + with open(fname, 'wb') as fd: + fd.write(chr(self._pad_byte) * self._size) + + for entry in self._entries.values(): + data = entry.GetData() + fd.seek(self._pad_before + entry.pos - self._skip_at_start) + fd.write(data)

Hello Simon,
Am 26.07.2016 um 02:59 schrieb Simon Glass:
This adds the basic code for binman, including command parsing, processing of entries and generation of images.
So far no entry types are supported. These will be added in future commits as examples of how to add new types.
See the README for documentation.
Signed-off-by: Simon Glass sjg@chromium.org
tools/binman/.gitignore | 1 + tools/binman/README | 491 ++++++++++++++++++++++++++++++++++++++++++++ tools/binman/binman | 1 + tools/binman/binman.py | 96 +++++++++ tools/binman/cmdline.py | 48 +++++ tools/binman/control.py | 106 ++++++++++ tools/binman/etype/entry.py | 186 +++++++++++++++++ tools/binman/image.py | 233 +++++++++++++++++++++ 8 files changed, 1162 insertions(+) create mode 100644 tools/binman/.gitignore create mode 100644 tools/binman/README create mode 120000 tools/binman/binman create mode 100755 tools/binman/binman.py create mode 100644 tools/binman/cmdline.py create mode 100644 tools/binman/control.py create mode 100644 tools/binman/etype/entry.py create mode 100644 tools/binman/image.py
Sounds great. I try to find time to try it, thanks!
Reviewed-by: Heiko Schocherhs@denx.de
just some nitpick...
diff --git a/tools/binman/README b/tools/binman/README new file mode 100644 index 0000000..c73fb3c --- /dev/null +++ b/tools/binman/README @@ -0,0 +1,491 @@
[...]
+To do +-----
+Some ideas: +- Fill out the device tree to include the final position and size of each
- entry (since the input file may not always specify these)
+- Use of-platdata to make the information available to code that is unable
- to use device tree (such as a very small SPL image)
+- Write an image map to a text file +- Allow easy building of images by specifying just the board name +- Produce a full Python binding for libfdt (for upstream) +- Add an option to decode an image into the constituent binaries +- Suppoort hierarchical images (packing of binaries into another binary
typing error: Suppoort -> Support
- which is then placed in the image)
+- Support building an image for a board (-b) more completely, with a
- configurable build directory
+- Consider making binman work with buildman, although if it is used in the
- Makefile, this will be automatic
+- Implement align-end
+-- +Simon Glass sjg@chromium.org +7/7/2016
bye, Heiko

Add entries to support some standard U-Boot binaries, such as u-boot.bin, u-boot.dtb, etc. Also add some tests for these.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/binman/etype/blob.py | 37 ++ tools/binman/etype/u_boot.py | 17 + tools/binman/etype/u_boot_dtb.py | 17 + tools/binman/etype/u_boot_nodtb.py | 17 + tools/binman/etype/u_boot_spl.py | 17 + tools/binman/func_test.py | 509 +++++++++++++++++++++ tools/binman/test/01_invalid.dts | 5 + tools/binman/test/02_missing_node.dts | 6 + tools/binman/test/03_empty.dts | 9 + tools/binman/test/04_invalid_entry.dts | 11 + tools/binman/test/05_simple.dts | 11 + tools/binman/test/06_dual_image.dts | 22 + tools/binman/test/07_bad_align.dts | 12 + tools/binman/test/08_pack.dts | 30 ++ tools/binman/test/09_pack_extra.dts | 35 ++ tools/binman/test/10_pack_align_power2.dts | 12 + tools/binman/test/11_pack_align_size_power2.dts | 12 + tools/binman/test/12_pack_inv_align.dts | 13 + tools/binman/test/13_pack_inv_size_align.dts | 13 + tools/binman/test/14_pack_overlap.dts | 16 + tools/binman/test/15_pack_overflow.dts | 12 + tools/binman/test/16_pack_image_overflow.dts | 13 + tools/binman/test/17_pack_image_size.dts | 13 + tools/binman/test/18_pack_image_align.dts | 13 + tools/binman/test/19_pack_inv_image_align.dts | 14 + .../binman/test/20_pack_inv_image_align_power2.dts | 13 + tools/binman/test/21_image_pad.dts | 16 + tools/binman/test/22_image_name.dts | 21 + tools/binman/test/23_blob.dts | 12 + tools/binman/test/24_sorted.dts | 17 + tools/binman/test/25_pack_zero_size.dts | 15 + tools/binman/test/26_pack_u_boot_dtb.dts | 14 + 32 files changed, 994 insertions(+) create mode 100644 tools/binman/etype/blob.py create mode 100644 tools/binman/etype/u_boot.py create mode 100644 tools/binman/etype/u_boot_dtb.py create mode 100644 tools/binman/etype/u_boot_nodtb.py create mode 100644 tools/binman/etype/u_boot_spl.py create mode 100644 tools/binman/func_test.py create mode 100644 tools/binman/test/01_invalid.dts create mode 100644 tools/binman/test/02_missing_node.dts create mode 100644 tools/binman/test/03_empty.dts create mode 100644 tools/binman/test/04_invalid_entry.dts create mode 100644 tools/binman/test/05_simple.dts create mode 100644 tools/binman/test/06_dual_image.dts create mode 100644 tools/binman/test/07_bad_align.dts create mode 100644 tools/binman/test/08_pack.dts create mode 100644 tools/binman/test/09_pack_extra.dts create mode 100644 tools/binman/test/10_pack_align_power2.dts create mode 100644 tools/binman/test/11_pack_align_size_power2.dts create mode 100644 tools/binman/test/12_pack_inv_align.dts create mode 100644 tools/binman/test/13_pack_inv_size_align.dts create mode 100644 tools/binman/test/14_pack_overlap.dts create mode 100644 tools/binman/test/15_pack_overflow.dts create mode 100644 tools/binman/test/16_pack_image_overflow.dts create mode 100644 tools/binman/test/17_pack_image_size.dts create mode 100644 tools/binman/test/18_pack_image_align.dts create mode 100644 tools/binman/test/19_pack_inv_image_align.dts create mode 100644 tools/binman/test/20_pack_inv_image_align_power2.dts create mode 100644 tools/binman/test/21_image_pad.dts create mode 100644 tools/binman/test/22_image_name.dts create mode 100644 tools/binman/test/23_blob.dts create mode 100644 tools/binman/test/24_sorted.dts create mode 100644 tools/binman/test/25_pack_zero_size.dts create mode 100644 tools/binman/test/26_pack_u_boot_dtb.dts
diff --git a/tools/binman/etype/blob.py b/tools/binman/etype/blob.py new file mode 100644 index 0000000..def2164 --- /dev/null +++ b/tools/binman/etype/blob.py @@ -0,0 +1,37 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for blobs, which are binary objects read from files +# + +from entry import Entry +import fdt_util +import tools + +class Entry_blob(Entry): + def __init__(self, image, etype, node): + Entry.__init__(self, image, etype, node) + self._filename = fdt_util.GetString(self._node, "filename", self.etype) + + def ObtainContents(self): + self._filename = self.GetDefaultFilename() + self._pathname = tools.GetInputFilename(self._filename) + self.ReadContents() + return True + + def ReadContents(self): + with open(self._pathname) as fd: + # We assume the data is small enough to fit into memory. If this + # is used for large filesystem image that might not be true. + # In that case, Image.BuildImage() could be adjusted to use a + # new Entry method which can read in chunks. Then we could copy + # the data in chunks and avoid reading it all at once. For now + # this seems like an unnecessary complication. + self.data = fd.read() + self.contents_size = len(self.data) + return True + + def GetDefaultFilename(self): + return self._filename diff --git a/tools/binman/etype/u_boot.py b/tools/binman/etype/u_boot.py new file mode 100644 index 0000000..1fcff73 --- /dev/null +++ b/tools/binman/etype/u_boot.py @@ -0,0 +1,17 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for U-Boot binary +# + +from entry import Entry +from blob import Entry_blob + +class Entry_u_boot(Entry_blob): + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'u-boot.bin' diff --git a/tools/binman/etype/u_boot_dtb.py b/tools/binman/etype/u_boot_dtb.py new file mode 100644 index 0000000..1122c95 --- /dev/null +++ b/tools/binman/etype/u_boot_dtb.py @@ -0,0 +1,17 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for U-Boot device tree +# + +from entry import Entry +from blob import Entry_blob + +class Entry_u_boot_dtb(Entry_blob): + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'u-boot.dtb' diff --git a/tools/binman/etype/u_boot_nodtb.py b/tools/binman/etype/u_boot_nodtb.py new file mode 100644 index 0000000..3721c3b --- /dev/null +++ b/tools/binman/etype/u_boot_nodtb.py @@ -0,0 +1,17 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for 'u-boot-nodtb.bin' +# + +from entry import Entry +from blob import Entry_blob + +class Entry_u_boot_nodtb(Entry_blob): + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'u-boot-nodtb.bin' diff --git a/tools/binman/etype/u_boot_spl.py b/tools/binman/etype/u_boot_spl.py new file mode 100644 index 0000000..68b0148 --- /dev/null +++ b/tools/binman/etype/u_boot_spl.py @@ -0,0 +1,17 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for spl/u-boot-spl.bin +# + +from entry import Entry +from blob import Entry_blob + +class Entry_u_boot_spl(Entry_blob): + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'spl/u-boot-spl.bin' diff --git a/tools/binman/func_test.py b/tools/binman/func_test.py new file mode 100644 index 0000000..cfae3ae --- /dev/null +++ b/tools/binman/func_test.py @@ -0,0 +1,509 @@ +# +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# To run a single test, change to this directory, and: +# +# python -m unittest func_test.TestFunctional.testHelp + +from optparse import OptionParser +import os +import shutil +import struct +import sys +import tempfile +import unittest + +import binman +import cmdline +import command +import control +import fdt_select +import fdt_util +import tools +import tout + +# Contents of test files, corresponding to different entry types +U_BOOT_DATA = '1234' +U_BOOT_SPL_DATA = '567' +BLOB_DATA = '89' +ME_DATA = '0abcd' +VGA_DATA = 'vga' +U_BOOT_DTB_DATA = 'udtb' +X86_START16_DATA = 'start16' +U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here' + +class TestFunctional(unittest.TestCase): + """Functional tests for binman + + Most of these use a sample .dts file to build an image and then check + that it looks correct. The sample files are in the test/ subdirectory + and are numbered. + + For each entry type a very small test file is created using fixed + string contents. This makes it easy to test that things look right, and + debug problems. + + In some cases a 'real' file must be used - these are also supplied in + the test/ diurectory. + """ + @classmethod + def setUpClass(self): + # Handle the case where argv[0] is 'python' + self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) + self._binman_pathname = os.path.join(self._binman_dir, 'binman') + + # Create a temporary directory for input files + self._indir = tempfile.mkdtemp(prefix='binmant.') + + # Create some test files + TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) + TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) + TestFunctional._MakeInputFile('blobfile', BLOB_DATA) + TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) + TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA) + + @classmethod + def tearDownClass(self): + """Remove the temporary input directory and its contents""" + shutil.rmtree(self._indir) + + def setUp(self): + # Enable this to turn on debugging output + # tout.Init(tout.DEBUG) + command.test_result = None + + def tearDown(self): + """Remove the temporary output directory""" + tools._FinaliseForTest() + + def _RunBinman(self, *args, **kwargs): + """Run binman using the command line + + Args: + Arguments to pass, as a list of strings + kwargs: Arguments to pass to Command.RunPipe() + """ + result = command.RunPipe([[self._binman_pathname] + list(args)], + capture=True, capture_stderr=True, raise_on_error=False) + if result.return_code and kwargs.get('raise_on_error', True): + raise Exception("Error running '%s': %s" % (' '.join(args), + result.stdout + result.stderr)) + return result + + def _DoBinman(self, *args): + """Run binman using directly (in the same process) + + Args: + Arguments to pass, as a list of strings + Returns: + Return value (0 for success) + """ + (options, args) = cmdline.ParseArgs(list(args)) + + # For testing, you can force an increase in verbosity here + # options.verbosity = tout.DEBUG + return control.Binman(options, args) + + def _DoTestFile(self, fname): + """Run binman with a given test file + + Args: + fname: Device tree source filename to use (e.g. 05_simple.dts) + """ + return self._DoBinman('-p', '-I', self._indir, + '-d', self.TestFile(fname)) + + def _DoReadFile(self, fname, use_real_dtb=False): + """Run binman and return the resulting image + + This runs binman with a given test file and then reads the resulting + output file. It is a shortcut function since most tests need to do + these steps. + + Raises an assertion failure if binman returns a non-zero exit code. + + Args: + fname: Device tree source filename to use (e.g. 05_simple.dts) + use_real_dtb: True to use the test file as the contents of + the u-boot-dtb entry. Normally this is not needed and the + test contents (the U_BOOT_DTB_DATA string) can be used. + But in some test we need the real contents. + """ + # Use the compiled test file as the u-boot-dtb input + if use_real_dtb: + tools.PrepareOutputDir(self._indir, True) + dtb = fdt_util.EnsureCompiled(self.TestFile(fname)) + with open(dtb) as fd: + data = fd.read() + TestFunctional._MakeInputFile('u-boot.dtb', data) + + retcode = self._DoTestFile(fname) + self.assertEqual(0, retcode) + + # Find the (only) image, read it and return its contents + image = control.images['image'] + fname = tools.GetOutputFilename('image.bin') + self.assertTrue(os.path.exists(fname)) + with open(fname) as fd: + return fd.read() + + @classmethod + def _MakeInputFile(self, fname, contents): + """Create a new test input file, creating directories as needed + + Args: + fname: Filenaem to create + contents: File contents to write in to the file + """ + pathname = os.path.join(self._indir, fname) + dirname = os.path.dirname(pathname) + if dirname and not os.path.exists(dirname): + os.makedirs(dirname) + with open(pathname, 'wb') as fd: + fd.write(contents) + + @classmethod + def TestFile(self, fname): + return os.path.join(self._binman_dir, 'test', fname) + + def AssertInList(self, grep_list, target): + """Assert that at least one of a list of things is in a target + + Args: + grep_list: List of strings to check + target: Target string + """ + for grep in grep_list: + if grep in target: + return + self.fail("Error: '%' not found in '%s'" % (grep_list, target)) + + def CheckNoGaps(self, entries): + """Check that all entries fit together without gaps + + Args: + entries: List of entries to check + """ + pos = 0 + for entry in entries.values(): + self.assertEqual(pos, entry.pos) + pos += entry.size + + def testRun(self): + """Test a basic run with valid args""" + result = self._RunBinman('-h') + + def testFullHelp(self): + """Test that the full help is displayed with -H""" + result = self._RunBinman('-H') + help_file = os.path.join(self._binman_dir, 'README') + self.assertEqual(len(result.stdout), os.path.getsize(help_file)) + self.assertEqual(0, len(result.stderr)) + self.assertEqual(0, result.return_code) + + def testHelp(self): + """Test that the basic help is displayed with -h""" + result = self._RunBinman('-h') + self.assertTrue(len(result.stdout) > 200) + self.assertEqual(0, len(result.stderr)) + self.assertEqual(0, result.return_code) + + # Not yet available. + def XtestBoard(self): + """Test that we can run it with a specific board""" + result = self._RunBinman('-b', 'sandbox') + self.assertEqual(0, len(result.stderr)) + self.assertEqual(0, result.return_code) + + def testNeedBoard(self): + """Test that we get an error when no board ius supplied""" + result = self._RunBinman(raise_on_error=False) + self.assertEqual(1, result.return_code) + + def testMissingDt(self): + """Test that an invalid device tree file generates an error""" + with self.assertRaises(Exception) as e: + self._RunBinman('-d', 'missing_file') + # We get one error from libfdt, and a different one from fdtget. + self.AssertInList(["Couldn't open blob from 'missing_file'", + 'No such file or directory'], str(e.exception)) + + def testBrokenDt(self): + """Test that an invalid device tree source file generates an error + + Since this is a source file it should be compiled and the error + will come from the device-tree compiler (dtc). + """ + with self.assertRaises(Exception) as e: + self._RunBinman('-d', self.TestFile('01_invalid.dts')) + self.assertIn("FATAL ERROR: Unable to parse input tree", + str(e.exception)) + + def testMissingNode(self): + """Test that a device tree without a 'binman' node generates an error""" + with self.assertRaises(Exception) as e: + self._RunBinman('-d', self.TestFile('02_missing_node.dts')) + self.assertIn("does not have a 'binman' node", str(e.exception)) + + def testEmpty(self): + """Test that an empty binman node works OK (i.e. does nothing)""" + result = self._RunBinman('-d', self.TestFile('03_empty.dts')) + self.assertEqual(0, len(result.stderr)) + self.assertEqual(0, result.return_code) + + def testInvalidEntry(self): + """Test that an invalid entry is flagged""" + with self.assertRaises(Exception) as e: + result = self._RunBinman('-d', + self.TestFile('04_invalid_entry.dts')) + #print e.exception + self.assertIn("Unknown entry type 'not-a-valid-type' in node " + "'/binman/not-a-valid-type'", str(e.exception)) + + def testSimple(self): + """Test a simple binman with a single file""" + data = self._DoReadFile('05_simple.dts') + self.assertEqual(U_BOOT_DATA, data) + + def testDual(self): + """Test that we can handle creating two images + + This also tests image padding. + """ + retcode = self._DoTestFile('06_dual_image.dts') + self.assertEqual(0, retcode) + + image = control.images['image1'] + self.assertEqual(len(U_BOOT_DATA), image._size) + fname = tools.GetOutputFilename('image1.bin') + self.assertTrue(os.path.exists(fname)) + with open(fname) as fd: + data = fd.read() + self.assertEqual(U_BOOT_DATA, data) + + image = control.images['image2'] + self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size) + fname = tools.GetOutputFilename('image2.bin') + self.assertTrue(os.path.exists(fname)) + with open(fname) as fd: + data = fd.read() + self.assertEqual(U_BOOT_DATA, data[3:7]) + self.assertEqual(chr(0) * 3, data[:3]) + self.assertEqual(chr(0) * 5, data[7:]) + + def testBadAlign(self): + """Test that an invalid alignment value is detected""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('07_bad_align.dts') + self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power " + "of two", str(e.exception)) + + def testPackSimple(self): + """Test that packing works as expected""" + retcode = self._DoTestFile('08_pack.dts') + self.assertEqual(0, retcode) + self.assertIn('image', control.images) + image = control.images['image'] + entries = image._entries + self.assertEqual(5, len(entries)) + + # First u-boot + self.assertIn('u-boot', entries) + entry = entries['u-boot'] + self.assertEqual(0, entry.pos) + self.assertEqual(len(U_BOOT_DATA), entry.size) + + # Second u-boot, aligned to 16-byte boundary + self.assertIn('u-boot-align', entries) + entry = entries['u-boot-align'] + self.assertEqual(16, entry.pos) + self.assertEqual(len(U_BOOT_DATA), entry.size) + + # Third u-boot, size 23 bytes + self.assertIn('u-boot-size', entries) + entry = entries['u-boot-size'] + self.assertEqual(20, entry.pos) + self.assertEqual(len(U_BOOT_DATA), entry.contents_size) + self.assertEqual(23, entry.size) + + # Fourth u-boot, placed immediate after the above + self.assertIn('u-boot-next', entries) + entry = entries['u-boot-next'] + self.assertEqual(43, entry.pos) + self.assertEqual(len(U_BOOT_DATA), entry.size) + + # Fifth u-boot, placed at a fixed position + self.assertIn('u-boot-fixed', entries) + entry = entries['u-boot-fixed'] + self.assertEqual(61, entry.pos) + self.assertEqual(len(U_BOOT_DATA), entry.size) + + self.assertEqual(65, image._size) + + def testPackExtra(self): + """Test that extra packing feature works as expected""" + retcode = self._DoTestFile('09_pack_extra.dts') + + self.assertEqual(0, retcode) + self.assertIn('image', control.images) + image = control.images['image'] + entries = image._entries + self.assertEqual(5, len(entries)) + + # First u-boot with padding before and after + self.assertIn('u-boot', entries) + entry = entries['u-boot'] + self.assertEqual(0, entry.pos) + self.assertEqual(3, entry.pad_before) + self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size) + + # Second u-boot has an aligned size, but it has no effect + self.assertIn('u-boot-align-size-nop', entries) + entry = entries['u-boot-align-size-nop'] + self.assertEqual(12, entry.pos) + self.assertEqual(4, entry.size) + + # Third u-boot has an aligned size too + self.assertIn('u-boot-align-size', entries) + entry = entries['u-boot-align-size'] + self.assertEqual(16, entry.pos) + self.assertEqual(32, entry.size) + + # Fourth u-boot has an aligned end + self.assertIn('u-boot-align-end', entries) + entry = entries['u-boot-align-end'] + self.assertEqual(48, entry.pos) + self.assertEqual(16, entry.size) + + # Fifth u-boot immediately afterwards + self.assertIn('u-boot-align-both', entries) + entry = entries['u-boot-align-both'] + self.assertEqual(64, entry.pos) + self.assertEqual(64, entry.size) + + self.CheckNoGaps(entries) + self.assertEqual(128, image._size) + + def testPackAlignPowerOf2(self): + """Test that invalid entry alignment is detected""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('10_pack_align_power2.dts') + self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power " + "of two", str(e.exception)) + + def testPackAlignSizePowerOf2(self): + """Test that invalid entry size alignment is detected""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('11_pack_align_size_power2.dts') + self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a " + "power of two", str(e.exception)) + + def testPackInvalidAlign(self): + """Test detection of an position that does not match its alignment""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('12_pack_inv_align.dts') + self.assertIn("Node '/binman/u-boot': Position 0x5 (5) does not match " + "align 0x4 (4)", str(e.exception)) + + def testPackInvalidSizeAlign(self): + """Test that invalid entry size alignment is detected""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('13_pack_inv_size_align.dts') + self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match " + "align-size 0x4 (4)", str(e.exception)) + + def testPackOverlap(self): + """Test that overlapping regions are detected""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('14_pack_overlap.dts') + self.assertIn("Node '/binman/u-boot-align': Position 0x3 (3) overlaps " + "with previous entry '/binman/u-boot' ending at 0x4 (4)", + str(e.exception)) + + def testPackEntryOverflow(self): + """Test that entries that overflow their size are detected""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('15_pack_overflow.dts') + self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) " + "but entry size is 0x3 (3)", str(e.exception)) + + def testPackImageOverflow(self): + """Test that entries which overflow the image size are detected""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('16_pack_image_overflow.dts') + self.assertIn("Image '/binman': contents size 0x4 (4) exceeds image " + "size 0x3 (3)", str(e.exception)) + + def testPackImageSize(self): + """Test that the image size can be set""" + retcode = self._DoTestFile('17_pack_image_size.dts') + self.assertEqual(0, retcode) + self.assertIn('image', control.images) + image = control.images['image'] + self.assertEqual(7, image._size) + + def testPackImageSizeAlign(self): + """Test that image size alignemnt works as expected""" + retcode = self._DoTestFile('18_pack_image_align.dts') + self.assertEqual(0, retcode) + self.assertIn('image', control.images) + image = control.images['image'] + self.assertEqual(16, image._size) + + def testPackInvalidImageAlign(self): + """Test that invalid image alignment is detected""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('19_pack_inv_image_align.dts') + self.assertIn("Image '/binman': Size 0x7 (7) does not match " + "align-size 0x8 (8)", str(e.exception)) + + def testPackAlignPowerOf2(self): + """Test that invalid image alignment is detected""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('20_pack_inv_image_align_power2.dts') + self.assertIn("Image '/binman': Alignment size 131 must be a power of " + "two", str(e.exception)) + + def testImagePadByte(self): + """Test that the image pad byte can be specified""" + data = self._DoReadFile('21_image_pad.dts') + self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 9) + U_BOOT_DATA, data) + + def testImageName(self): + """Test that image files can be named""" + retcode = self._DoTestFile('22_image_name.dts') + self.assertEqual(0, retcode) + image = control.images['image1'] + fname = tools.GetOutputFilename('test-name') + self.assertTrue(os.path.exists(fname)) + + image = control.images['image2'] + fname = tools.GetOutputFilename('test-name.xx') + self.assertTrue(os.path.exists(fname)) + + def testBlobFilename(self): + """Test that generic blobs can be provided by filename""" + data = self._DoReadFile('23_blob.dts') + self.assertEqual(BLOB_DATA, data) + + def testPackSorted(self): + """Test that entries can be sorted""" + data = self._DoReadFile('24_sorted.dts') + self.assertEqual(chr(0) * 5 + U_BOOT_SPL_DATA + chr(0) * 2 + + U_BOOT_DATA, data) + + def testPackZeroPosition(self): + """Test that an entry at position 0 is not given a new position""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('25_pack_zero_size.dts') + self.assertIn("Node '/binman/u-boot-spl': Position 0x0 (0) overlaps " + "with previous entry '/binman/u-boot' ending at 0x4 (4)", + str(e.exception)) + + def testPackUbootDtb(self): + """Test that a device tree can be added to U-Boot""" + data = self._DoReadFile('26_pack_u_boot_dtb.dts') + self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data) diff --git a/tools/binman/test/01_invalid.dts b/tools/binman/test/01_invalid.dts new file mode 100644 index 0000000..7d00455 --- /dev/null +++ b/tools/binman/test/01_invalid.dts @@ -0,0 +1,5 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; diff --git a/tools/binman/test/02_missing_node.dts b/tools/binman/test/02_missing_node.dts new file mode 100644 index 0000000..3a51ec2 --- /dev/null +++ b/tools/binman/test/02_missing_node.dts @@ -0,0 +1,6 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; +}; diff --git a/tools/binman/test/03_empty.dts b/tools/binman/test/03_empty.dts new file mode 100644 index 0000000..493c9a0 --- /dev/null +++ b/tools/binman/test/03_empty.dts @@ -0,0 +1,9 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + }; +}; diff --git a/tools/binman/test/04_invalid_entry.dts b/tools/binman/test/04_invalid_entry.dts new file mode 100644 index 0000000..b043455 --- /dev/null +++ b/tools/binman/test/04_invalid_entry.dts @@ -0,0 +1,11 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + not-a-valid-type { + }; + }; +}; diff --git a/tools/binman/test/05_simple.dts b/tools/binman/test/05_simple.dts new file mode 100644 index 0000000..3771aa2 --- /dev/null +++ b/tools/binman/test/05_simple.dts @@ -0,0 +1,11 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + }; + }; +}; diff --git a/tools/binman/test/06_dual_image.dts b/tools/binman/test/06_dual_image.dts new file mode 100644 index 0000000..78be16f --- /dev/null +++ b/tools/binman/test/06_dual_image.dts @@ -0,0 +1,22 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + multiple-images; + image1 { + u-boot { + }; + }; + + image2 { + pad-before = <3>; + pad-after = <5>; + + u-boot { + }; + }; + }; +}; diff --git a/tools/binman/test/07_bad_align.dts b/tools/binman/test/07_bad_align.dts new file mode 100644 index 0000000..123bb13 --- /dev/null +++ b/tools/binman/test/07_bad_align.dts @@ -0,0 +1,12 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + align = <23>; + }; + }; +}; diff --git a/tools/binman/test/08_pack.dts b/tools/binman/test/08_pack.dts new file mode 100644 index 0000000..dc63d99 --- /dev/null +++ b/tools/binman/test/08_pack.dts @@ -0,0 +1,30 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + }; + + u-boot-align { + type = "u-boot"; + align = <16>; + }; + + u-boot-size { + type = "u-boot"; + size = <23>; + }; + + u-boot-next { + type = "u-boot"; + }; + + u-boot-fixed { + type = "u-boot"; + pos = <61>; + }; + }; +}; diff --git a/tools/binman/test/09_pack_extra.dts b/tools/binman/test/09_pack_extra.dts new file mode 100644 index 0000000..0765707 --- /dev/null +++ b/tools/binman/test/09_pack_extra.dts @@ -0,0 +1,35 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + pad-before = <3>; + pad-after = <5>; + }; + + u-boot-align-size-nop { + type = "u-boot"; + align-size = <4>; + }; + + u-boot-align-size { + type = "u-boot"; + align = <16>; + align-size = <32>; + }; + + u-boot-align-end { + type = "u-boot"; + align-end = <64>; + }; + + u-boot-align-both { + type = "u-boot"; + align= <64>; + align-end = <128>; + }; + }; +}; diff --git a/tools/binman/test/10_pack_align_power2.dts b/tools/binman/test/10_pack_align_power2.dts new file mode 100644 index 0000000..8f6253a --- /dev/null +++ b/tools/binman/test/10_pack_align_power2.dts @@ -0,0 +1,12 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + align = <5>; + }; + }; +}; diff --git a/tools/binman/test/11_pack_align_size_power2.dts b/tools/binman/test/11_pack_align_size_power2.dts new file mode 100644 index 0000000..04f7672 --- /dev/null +++ b/tools/binman/test/11_pack_align_size_power2.dts @@ -0,0 +1,12 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + align-size = <55>; + }; + }; +}; diff --git a/tools/binman/test/12_pack_inv_align.dts b/tools/binman/test/12_pack_inv_align.dts new file mode 100644 index 0000000..1d9d80a --- /dev/null +++ b/tools/binman/test/12_pack_inv_align.dts @@ -0,0 +1,13 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + pos = <5>; + align = <4>; + }; + }; +}; diff --git a/tools/binman/test/13_pack_inv_size_align.dts b/tools/binman/test/13_pack_inv_size_align.dts new file mode 100644 index 0000000..dfafa13 --- /dev/null +++ b/tools/binman/test/13_pack_inv_size_align.dts @@ -0,0 +1,13 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + size = <5>; + align-size = <4>; + }; + }; +}; diff --git a/tools/binman/test/14_pack_overlap.dts b/tools/binman/test/14_pack_overlap.dts new file mode 100644 index 0000000..611cfd9 --- /dev/null +++ b/tools/binman/test/14_pack_overlap.dts @@ -0,0 +1,16 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + }; + + u-boot-align { + type = "u-boot"; + pos = <3>; + }; + }; +}; diff --git a/tools/binman/test/15_pack_overflow.dts b/tools/binman/test/15_pack_overflow.dts new file mode 100644 index 0000000..6f65433 --- /dev/null +++ b/tools/binman/test/15_pack_overflow.dts @@ -0,0 +1,12 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + size = <3>; + }; + }; +}; diff --git a/tools/binman/test/16_pack_image_overflow.dts b/tools/binman/test/16_pack_image_overflow.dts new file mode 100644 index 0000000..6ae66f3 --- /dev/null +++ b/tools/binman/test/16_pack_image_overflow.dts @@ -0,0 +1,13 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + size = <3>; + + u-boot { + }; + }; +}; diff --git a/tools/binman/test/17_pack_image_size.dts b/tools/binman/test/17_pack_image_size.dts new file mode 100644 index 0000000..2360eb5 --- /dev/null +++ b/tools/binman/test/17_pack_image_size.dts @@ -0,0 +1,13 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + size = <7>; + + u-boot { + }; + }; +}; diff --git a/tools/binman/test/18_pack_image_align.dts b/tools/binman/test/18_pack_image_align.dts new file mode 100644 index 0000000..16cd2a4 --- /dev/null +++ b/tools/binman/test/18_pack_image_align.dts @@ -0,0 +1,13 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + align-size = <16>; + + u-boot { + }; + }; +}; diff --git a/tools/binman/test/19_pack_inv_image_align.dts b/tools/binman/test/19_pack_inv_image_align.dts new file mode 100644 index 0000000..e5ee87b --- /dev/null +++ b/tools/binman/test/19_pack_inv_image_align.dts @@ -0,0 +1,14 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + size = <7>; + align-size = <8>; + + u-boot { + }; + }; +}; diff --git a/tools/binman/test/20_pack_inv_image_align_power2.dts b/tools/binman/test/20_pack_inv_image_align_power2.dts new file mode 100644 index 0000000..a428c4b --- /dev/null +++ b/tools/binman/test/20_pack_inv_image_align_power2.dts @@ -0,0 +1,13 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + align-size = <131>; + + u-boot { + }; + }; +}; diff --git a/tools/binman/test/21_image_pad.dts b/tools/binman/test/21_image_pad.dts new file mode 100644 index 0000000..daf8385 --- /dev/null +++ b/tools/binman/test/21_image_pad.dts @@ -0,0 +1,16 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + pad-byte = <0xff>; + u-boot-spl { + }; + + u-boot { + pos = <12>; + }; + }; +}; diff --git a/tools/binman/test/22_image_name.dts b/tools/binman/test/22_image_name.dts new file mode 100644 index 0000000..94fc069 --- /dev/null +++ b/tools/binman/test/22_image_name.dts @@ -0,0 +1,21 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + multiple-images; + image1 { + filename = "test-name"; + u-boot { + }; + }; + + image2 { + filename = "test-name.xx"; + u-boot { + }; + }; + }; +}; diff --git a/tools/binman/test/23_blob.dts b/tools/binman/test/23_blob.dts new file mode 100644 index 0000000..7dcff69 --- /dev/null +++ b/tools/binman/test/23_blob.dts @@ -0,0 +1,12 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + blob { + filename = "blobfile"; + }; + }; +}; diff --git a/tools/binman/test/24_sorted.dts b/tools/binman/test/24_sorted.dts new file mode 100644 index 0000000..9f4151c --- /dev/null +++ b/tools/binman/test/24_sorted.dts @@ -0,0 +1,17 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + sort-by-pos; + u-boot { + pos = <10>; + }; + + u-boot-spl { + pos = <5>; + }; + }; +}; diff --git a/tools/binman/test/25_pack_zero_size.dts b/tools/binman/test/25_pack_zero_size.dts new file mode 100644 index 0000000..7d2baad --- /dev/null +++ b/tools/binman/test/25_pack_zero_size.dts @@ -0,0 +1,15 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot { + }; + + u-boot-spl { + pos = <0>; + }; + }; +}; diff --git a/tools/binman/test/26_pack_u_boot_dtb.dts b/tools/binman/test/26_pack_u_boot_dtb.dts new file mode 100644 index 0000000..2707a73 --- /dev/null +++ b/tools/binman/test/26_pack_u_boot_dtb.dts @@ -0,0 +1,14 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot-nodtb { + }; + + u-boot-dtb { + }; + }; +};

The structure of x86 ROMs is pretty complex. There are various binary blobs to place in the image. Microcode requires special handling so that it is available to very early code and can be used without any memory whatsoever.
Add support for the various entry types that are currently needed, along with some tests.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/binman/etype/intel_descriptor.py | 55 +++++++++++ tools/binman/etype/intel_me.py | 17 ++++ tools/binman/etype/intel_mrc.py | 17 ++++ tools/binman/etype/intel_vga.py | 17 ++++ tools/binman/etype/u_boot_dtb_with_ucode.py | 69 ++++++++++++++ tools/binman/etype/u_boot_ucode.py | 77 ++++++++++++++++ tools/binman/etype/u_boot_with_ucode_ptr.py | 71 +++++++++++++++ tools/binman/etype/x86_start16.py | 17 ++++ tools/binman/func_test.py | 136 ++++++++++++++++++++++++++++ tools/binman/test/27_pack_4gb_no_size.dts | 18 ++++ tools/binman/test/28_pack_4gb_outside.dts | 19 ++++ tools/binman/test/29_x86-rom.dts | 19 ++++ tools/binman/test/30_x86-rom-me-no-desc.dts | 15 +++ tools/binman/test/31_x86-rom-me.dts | 18 ++++ tools/binman/test/32_intel-vga.dts | 13 +++ tools/binman/test/33_x86-start16.dts | 13 +++ tools/binman/test/34_x86_ucode.dts | 29 ++++++ tools/binman/test/35_x86_single_ucode.dts | 26 ++++++ tools/binman/test/u_boot_ucode_ptr | Bin 0 -> 4175 bytes tools/binman/test/u_boot_ucode_ptr.c | 15 +++ tools/binman/test/u_boot_ucode_ptr.lds | 18 ++++ 21 files changed, 679 insertions(+) create mode 100644 tools/binman/etype/intel_descriptor.py create mode 100644 tools/binman/etype/intel_me.py create mode 100644 tools/binman/etype/intel_mrc.py create mode 100644 tools/binman/etype/intel_vga.py create mode 100644 tools/binman/etype/u_boot_dtb_with_ucode.py create mode 100644 tools/binman/etype/u_boot_ucode.py create mode 100644 tools/binman/etype/u_boot_with_ucode_ptr.py create mode 100644 tools/binman/etype/x86_start16.py create mode 100644 tools/binman/test/27_pack_4gb_no_size.dts create mode 100644 tools/binman/test/28_pack_4gb_outside.dts create mode 100644 tools/binman/test/29_x86-rom.dts create mode 100644 tools/binman/test/30_x86-rom-me-no-desc.dts create mode 100644 tools/binman/test/31_x86-rom-me.dts create mode 100644 tools/binman/test/32_intel-vga.dts create mode 100644 tools/binman/test/33_x86-start16.dts create mode 100644 tools/binman/test/34_x86_ucode.dts create mode 100644 tools/binman/test/35_x86_single_ucode.dts create mode 100755 tools/binman/test/u_boot_ucode_ptr create mode 100644 tools/binman/test/u_boot_ucode_ptr.c create mode 100644 tools/binman/test/u_boot_ucode_ptr.lds
diff --git a/tools/binman/etype/intel_descriptor.py b/tools/binman/etype/intel_descriptor.py new file mode 100644 index 0000000..7f4ea0b --- /dev/null +++ b/tools/binman/etype/intel_descriptor.py @@ -0,0 +1,55 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for 'u-boot' +# + +import struct + +from entry import Entry +from blob import Entry_blob + +FD_SIGNATURE = struct.pack('<L', 0x0ff0a55a) +MAX_REGIONS = 5 + +(REGION_DESCRIPTOR, REGION_BIOS, REGION_ME, REGION_GBE, + REGION_PDATA) = range(5) + +class Region: + def __init__(self, data, frba, region_num): + pos = frba + region_num * 4 + val = struct.unpack('<L', data[pos:pos + 4])[0] + self.base = (val & 0xfff) << 12 + self.limit = ((val & 0x0fff0000) >> 4) | 0xfff + self.size = self.limit - self.base + 1 + +class Entry_intel_descriptor(Entry_blob): + """Intel flash descriptor block (4KB) + + This is placed at the start of flash and provides information about + the SPI flash regions. In particular it provides the base address and + size of the ME region, allowing us to place the ME binary in the right + place. + """ + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + self._regions = [] + + def GetDefaultFilename(self): + return 'descriptor.bin' + + def GetPositions(self): + pos = self.data.find(FD_SIGNATURE) + if pos == -1: + self.Raise('Cannot find FD signature') + flvalsig, flmap0, flmap1, flmap2 = struct.unpack('<LLLL', + self.data[pos:pos + 16]) + frba = ((flmap0 >> 16) & 0xff) << 4 + for i in range(MAX_REGIONS): + self._regions.append(Region(self.data, frba, i)) + + # Set the offset for ME only, for now, since the others are not used + return {'intel-me': [self._regions[REGION_ME].base, + self._regions[REGION_ME].size]} diff --git a/tools/binman/etype/intel_me.py b/tools/binman/etype/intel_me.py new file mode 100644 index 0000000..fbb553a --- /dev/null +++ b/tools/binman/etype/intel_me.py @@ -0,0 +1,17 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for 'u-boot' +# + +from entry import Entry +from blob import Entry_blob + +class Entry_intel_me(Entry_blob): + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'me.bin' diff --git a/tools/binman/etype/intel_mrc.py b/tools/binman/etype/intel_mrc.py new file mode 100644 index 0000000..3bf9dc0 --- /dev/null +++ b/tools/binman/etype/intel_mrc.py @@ -0,0 +1,17 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for 'u-boot' +# + +from entry import Entry +from blob import Entry_blob + +class Entry_intel_mrc(Entry_blob): + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'mrc.bin' diff --git a/tools/binman/etype/intel_vga.py b/tools/binman/etype/intel_vga.py new file mode 100644 index 0000000..be666dd --- /dev/null +++ b/tools/binman/etype/intel_vga.py @@ -0,0 +1,17 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for 'u-boot' +# + +from entry import Entry +from blob import Entry_blob + +class Entry_intel_vga(Entry_blob): + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'vga.bin' diff --git a/tools/binman/etype/u_boot_dtb_with_ucode.py b/tools/binman/etype/u_boot_dtb_with_ucode.py new file mode 100644 index 0000000..1b097cf1 --- /dev/null +++ b/tools/binman/etype/u_boot_dtb_with_ucode.py @@ -0,0 +1,69 @@ +# Copyright (c) 2016 Google, Inc +## Written by Simon Glass sjg@chromium.org + +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for U-Boot device tree with the microcode removed +# + +import fdt_select +import fdt_util +from entry import Entry +from blob import Entry_blob +import tools + +class Entry_u_boot_dtb_with_ucode(Entry_blob): + """A U-Boot device tree file, with the microcode removed + + See Entry_u_boot_ucode for full details of the 3 entries involved in this + process. + """ + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + self.ucode_data = '' + self.collate = False + self.ucode_offset = None + self.ucode_size = None + + def GetDefaultFilename(self): + return 'u-boot.dtb' + + def ObtainContents(self): + Entry_blob.ObtainContents(self) + + # Create a new file to hold the copied device tree + dtb_name = 'u-boot-dtb-with-ucode.dtb' + fname = tools.GetOutputFilename(dtb_name) + with open(fname, 'wb') as fd: + fd.write(self.data) + + # Remove the microcode + fdt = fdt_select.FdtScan(fname) + fdt.Scan() + ucode = fdt.GetNode('/microcode') + if not ucode: + raise self.Raise("No /microcode node found in '%s'" % fname) + self.collate = len(ucode.subnodes) > 1 + for node in ucode.subnodes: + data_prop = node.props.get('data') + if data_prop: + self.ucode_data += ''.join(data_prop.bytes) + if not self.collate: + poffset = data_prop.GetOffset() + if poffset is None: + # We cannot obtain a property offset. Collate instead. + self.collate = True + else: + # Find the offset in the device tree of the ucode data + self.ucode_offset = poffset + 12 + self.ucode_size = len(data_prop.bytes) + if self.collate: + prop = node.DeleteProp('data') + if self.collate: + fdt.Pack() + fdt.Flush() + + # Make this file the contents of this entry + self._pathname = fname + self.ReadContents() + return True diff --git a/tools/binman/etype/u_boot_ucode.py b/tools/binman/etype/u_boot_ucode.py new file mode 100644 index 0000000..8d147d0 --- /dev/null +++ b/tools/binman/etype/u_boot_ucode.py @@ -0,0 +1,77 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for a U-Boot binary with an embedded microcode pointer +# + +from entry import Entry +from blob import Entry_blob +import tools + +class Entry_u_boot_ucode(Entry_blob): + """U-Boot microcode block + + U-Boot on x86 needs a single block of microcode. This is collected from + the various microcode update nodes in the device tree. It is also unable + to read the microcode from the device tree on platforms that use FSP + (Firmware Support Package) binaries, because the API requires that the + microcode is supplied before there is any SRAM available to use (i.e. + the FSP sets up the SRAM / cache-as-RAM but does so in the call that + requires the microcode!). To keep things simple, all x86 platforms handle + microcode the same way in U-Boot (even non-FSP platforms). This is that + a table is placed at _dt_ucode_base_size containing the base address and + size of the microcode. This is either passed to the FSP (for FSP + platforms), or used to set up the microcode (for non-FSP platforms). + This all happens in the build system since it is the only way to get + the microcode into a single blob and accessible without SRAM. + + There are two cases to handle. If there is only one microcode blob in + the device tree, then the ucode pointer it set to point to that. This + entry (u-boot-ucode) is empty. If there is more than one update, then + this entry holds the concatenation of all updates, and the device tree + entry (u-boot-dtb-with-ucode) is updated to remove the microcode. This + last step ensures that that the microcode appears in one contiguous + block in the image and is not unnecessarily duplicated in the device + tree. It is referred to as 'collation' here. + + Entry types that have a part to play in handling microcode: + + Entry_u_boot_with_ucode_ptr: + Contains u-boot-nodtb.bin (i.e. U-Boot without the device tree). + It updates it with the address and size of the microcode so that + U-Boot can find it early on start-up. + Entry_u_boot_dtb_with_ucode: + Contains u-boot.dtb. It stores the microcode in a + 'self.ucode_data' property, which is then read by this class to + obtain the microcode if needed. If collation is performed, it + removes the microcode from the device tree. + Entry_u_boot_ucode: + This class. If collation is enabled it reads the microcode from + the Entry_u_boot_dtb_with_ucode entry, and uses it as the + contents of this entry. + """ + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def ObtainContents(self): + # Get the microcode from the device tree entry + fdt_entry = self.image.FindEntryType('u-boot-dtb-with-ucode') + if not fdt_entry or not fdt_entry.ucode_data: + return False + if not fdt_entry.collate: + # This section can be empty + self.data = '' + return True + + # Write it out to a file + dtb_name = 'u-boot-ucode.bin' + fname = tools.GetOutputFilename(dtb_name) + with open(fname, 'wb') as fd: + fd.write(fdt_entry.ucode_data) + + self._pathname = fname + self.ReadContents() + + return True diff --git a/tools/binman/etype/u_boot_with_ucode_ptr.py b/tools/binman/etype/u_boot_with_ucode_ptr.py new file mode 100644 index 0000000..5092820 --- /dev/null +++ b/tools/binman/etype/u_boot_with_ucode_ptr.py @@ -0,0 +1,71 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for a U-Boot binary with an embedded microcode pointer +# + +import struct + +import command +from entry import Entry +from blob import Entry_blob +import tools + +class Entry_u_boot_with_ucode_ptr(Entry_blob): + """U-Boot with embedded microcode pointer + + See Entry_u_boot_ucode for full details of the 3 entries involved in this + process. + """ + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'u-boot-nodtb.bin' + + def ObtainContents(self): + return Entry_blob.ObtainContents(self) + + def ProcessContents(self): + # Get the position of the microcode + ucode_entry = self.image.FindEntryType('u-boot-ucode') + if not ucode_entry: + self.Raise('Cannot find microcode region u-boot-ucode') + + # Figure out where to put the microcode pointer + fname = tools.GetInputFilename('u-boot') + args = [['nm', fname], ['grep', '_dt_ucode_base_size']] + out = command.RunPipe(args, capture=True).stdout.splitlines() + if len(out) != 1: + self.Raise('Cannot locate _dt_ucode_base_size symbol in u-boot') + target_pos = int(out[0].split()[0], 16) + + # Check that it is in the image. If it is not, then U-Boot is being + # linked incorrectly, or is being placed at the wrong position in the + # image. The image must be set up so that U-Boot is placed at the + # flash address to which it is linked. For example, if + # CONFIG_SYS_TEXT_BASE is 0xfff00000, and the ROM is 8MB, then + # the U-Boot region must start at position 7MB in the image. In this + # case the ROM starts at 0xff800000, so the position of the first + # entry in the image corresponds to that. + if target_pos < self.pos or target_pos >= self.pos + self.size: + self.Raise('Microcode pointer _dt_ucode_base_size at %08x is ' + 'outside the image ranging from %08x to %08x' % + (target_pos, self.pos, self.pos + self.size)) + + if ucode_entry.size: + pos, size = ucode_entry.pos, ucode_entry.size + else: + dtb_entry = self.image.FindEntryType('u-boot-dtb-with-ucode') + if not dtb_entry: + self.Raise('Cannot find microcode region u-boot-dtb-with-ucode') + pos = dtb_entry.pos + dtb_entry.ucode_offset + size = dtb_entry.ucode_size + + # Write the microcode position and size into the entry + pos_and_size = struct.pack('<2L', pos, size) + target_pos -= self.pos + self.data = (self.data[:target_pos] + pos_and_size + + self.data[target_pos + 8:]) diff --git a/tools/binman/etype/x86_start16.py b/tools/binman/etype/x86_start16.py new file mode 100644 index 0000000..8e3eecb --- /dev/null +++ b/tools/binman/etype/x86_start16.py @@ -0,0 +1,17 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for 'u-boot' +# + +from entry import Entry +from blob import Entry_blob + +class Entry_x86_start16(Entry_blob): + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'u-boot-x86-16bit.bin' diff --git a/tools/binman/func_test.py b/tools/binman/func_test.py index cfae3ae..4fc76bd 100644 --- a/tools/binman/func_test.py +++ b/tools/binman/func_test.py @@ -62,9 +62,20 @@ class TestFunctional(unittest.TestCase): TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) TestFunctional._MakeInputFile('blobfile', BLOB_DATA) + TestFunctional._MakeInputFile('me.bin', ME_DATA) + TestFunctional._MakeInputFile('vga.bin', VGA_DATA) TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) + TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA) TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
+ # ELF file with a '_dt_ucode_base_size' symbol + with open(self.TestFile('u_boot_ucode_ptr')) as fd: + TestFunctional._MakeInputFile('u-boot', fd.read()) + + # Intel flash descriptor file + with open(self.TestFile('descriptor.bin')) as fd: + TestFunctional._MakeInputFile('descriptor.bin', fd.read()) + @classmethod def tearDownClass(self): """Remove the temporary input directory and its contents""" @@ -507,3 +518,128 @@ class TestFunctional(unittest.TestCase): """Test that a device tree can be added to U-Boot""" data = self._DoReadFile('26_pack_u_boot_dtb.dts') self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data) + + def testPackX86RomNoSize(self): + """Test that the end-at-4gb property requires a size property""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('27_pack_4gb_no_size.dts') + self.assertIn("Image '/binman': Image size must be provided when " + "using end-at-4gb", str(e.exception)) + + def testPackX86RomOutside(self): + """Test that the end-at-4gb property checks for position boundaries""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('28_pack_4gb_outside.dts') + self.assertIn("Node '/binman/u-boot': Position 0x0 (0) is outsize " + "the image starting at 0xfffffff0 (4294967280)", + str(e.exception)) + + def testPackX86Rom(self): + """Test that a basic x86 ROM can be created""" + data = self._DoReadFile('29_x86-rom.dts') + self.assertEqual(U_BOOT_DATA + chr(0) * 3 + U_BOOT_SPL_DATA + + chr(0) * 6, data) + + def testPackX86RomMeNoDesc(self): + """Test that the Intel ME entry requires a descriptor entry""" + with self.assertRaises(ValueError) as e: + self._DoTestFile('30_x86-rom-me-no-desc.dts') + self.assertIn("Node '/binman/intel-me': No position set with " + "pos-unset: should another entry provide this correct " + "position?", str(e.exception)) + + def testPackX86RomMe(self): + """Test that an x86 ROM with an ME region can be created""" + data = self._DoReadFile('31_x86-rom-me.dts') + self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)]) + + def testPackVga(self): + """Test that an image with a VGA binary can be created""" + data = self._DoReadFile('32_intel-vga.dts') + self.assertEqual(VGA_DATA, data[:len(VGA_DATA)]) + + def testPackStart16(self): + """Test that an image with an x86 start16 region can be created""" + data = self._DoReadFile('33_x86-start16.dts') + self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)]) + + def testPackUbootMicrocode(self): + """Test that x86 microcode can be handled correctly + + We expect to see the following in the image, in order: + u-boot-nodtb.bin with a microcode pointer inserted at the correct + place + u-boot.dtb with the microcode removed + the microcode + """ + data = self._DoReadFile('34_x86_ucode.dts', True) + + # Now check the device tree has no microcode + second = data[len(U_BOOT_NODTB_DATA):] + fname = tools.GetOutputFilename('test.dtb') + with open(fname, 'wb') as fd: + fd.write(second) + fdt = fdt_select.FdtScan(fname) + ucode = fdt.GetNode('/microcode') + self.assertTrue(ucode) + for node in ucode.subnodes: + self.assertFalse(node.props.get('data')) + + # Quick way to get the dtb length - see struct fdt_header: totalsize + # is the second word + fdt_len = struct.unpack('>L', second[4:8])[0] + + third = second[fdt_len:] + + # Check that the microcode appears immediately after the Fdt + # This matches the concatenation of the data properties in + # the /microcode/update@xxx nodes in x86_ucode.dts. + ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, + 0x78235609) + self.assertEqual(ucode_data, third[:len(ucode_data)]) + ucode_pos = len(U_BOOT_NODTB_DATA) + fdt_len + + # Check that the microcode pointer was inserted. It should match the + # expected position and size + pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, + len(ucode_data)) + first = data[:len(U_BOOT_NODTB_DATA)] + self.assertEqual('nodtb with microcode' + pos_and_size + + ' somewhere in here', first) + + def testPackUbootSingleMicrocode(self): + """Test that x86 microcode can be handled correctly + + We expect to see the following in the image, in order: + u-boot-nodtb.bin with a microcode pointer inserted at the correct + place + u-boot.dtb with the microcode + an empty microcode region + """ + # We need the libfdt library to run this test since only that allows + # finding the offset of a property. This is required by + # Entry_u_boot_dtb_with_ucode.ObtainContents(). + if not fdt_select.have_libfdt: + return + data = self._DoReadFile('35_x86_single_ucode.dts', True) + + second = data[len(U_BOOT_NODTB_DATA):] + + # Quick way to get the dtb length - see struct fdt_header: totalsize + # is the second word + fdt_len = struct.unpack('>L', second[4:8])[0] + + third = second[fdt_len:] + second = second[:fdt_len] + + ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) + self.assertIn(ucode_data, second) + ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) + + # Check that the microcode pointer was inserted. It should match the + # expected position and size + pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, + len(ucode_data)) + first = data[:len(U_BOOT_NODTB_DATA)] + self.assertEqual('nodtb with microcode' + pos_and_size + + ' somewhere in here', first) diff --git a/tools/binman/test/27_pack_4gb_no_size.dts b/tools/binman/test/27_pack_4gb_no_size.dts new file mode 100644 index 0000000..e0b6519 --- /dev/null +++ b/tools/binman/test/27_pack_4gb_no_size.dts @@ -0,0 +1,18 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + sort-by-pos; + end-at-4gb; + u-boot { + pos = <0xfffffff0>; + }; + + u-boot-spl { + pos = <0xfffffff7>; + }; + }; +}; diff --git a/tools/binman/test/28_pack_4gb_outside.dts b/tools/binman/test/28_pack_4gb_outside.dts new file mode 100644 index 0000000..ff468c7 --- /dev/null +++ b/tools/binman/test/28_pack_4gb_outside.dts @@ -0,0 +1,19 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + sort-by-pos; + end-at-4gb; + size = <16>; + u-boot { + pos = <0>; + }; + + u-boot-spl { + pos = <0xfffffff7>; + }; + }; +}; diff --git a/tools/binman/test/29_x86-rom.dts b/tools/binman/test/29_x86-rom.dts new file mode 100644 index 0000000..075ede3 --- /dev/null +++ b/tools/binman/test/29_x86-rom.dts @@ -0,0 +1,19 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + sort-by-pos; + end-at-4gb; + size = <16>; + u-boot { + pos = <0xfffffff0>; + }; + + u-boot-spl { + pos = <0xfffffff7>; + }; + }; +}; diff --git a/tools/binman/test/30_x86-rom-me-no-desc.dts b/tools/binman/test/30_x86-rom-me-no-desc.dts new file mode 100644 index 0000000..4578f66 --- /dev/null +++ b/tools/binman/test/30_x86-rom-me-no-desc.dts @@ -0,0 +1,15 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + sort-by-pos; + end-at-4gb; + size = <16>; + intel-me { + pos-unset; + }; + }; +}; diff --git a/tools/binman/test/31_x86-rom-me.dts b/tools/binman/test/31_x86-rom-me.dts new file mode 100644 index 0000000..b484ab3 --- /dev/null +++ b/tools/binman/test/31_x86-rom-me.dts @@ -0,0 +1,18 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + sort-by-pos; + end-at-4gb; + size = <0x800000>; + intel-descriptor { + }; + + intel-me { + pos-unset; + }; + }; +}; diff --git a/tools/binman/test/32_intel-vga.dts b/tools/binman/test/32_intel-vga.dts new file mode 100644 index 0000000..1790833 --- /dev/null +++ b/tools/binman/test/32_intel-vga.dts @@ -0,0 +1,13 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + size = <16>; + + intel-vga { + }; + }; +}; diff --git a/tools/binman/test/33_x86-start16.dts b/tools/binman/test/33_x86-start16.dts new file mode 100644 index 0000000..2e279de --- /dev/null +++ b/tools/binman/test/33_x86-start16.dts @@ -0,0 +1,13 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + size = <16>; + + x86-start16 { + }; + }; +}; diff --git a/tools/binman/test/34_x86_ucode.dts b/tools/binman/test/34_x86_ucode.dts new file mode 100644 index 0000000..64a6c2c --- /dev/null +++ b/tools/binman/test/34_x86_ucode.dts @@ -0,0 +1,29 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + sort-by-pos; + end-at-4gb; + size = <0x200>; + u-boot-with-ucode-ptr { + }; + + u-boot-dtb-with-ucode { + }; + + u-boot-ucode { + }; + }; + + microcode { + update@0 { + data = <0x12345678 0x12345679>; + }; + update@1 { + data = <0xabcd0000 0x78235609>; + }; + }; +}; diff --git a/tools/binman/test/35_x86_single_ucode.dts b/tools/binman/test/35_x86_single_ucode.dts new file mode 100644 index 0000000..973e97f --- /dev/null +++ b/tools/binman/test/35_x86_single_ucode.dts @@ -0,0 +1,26 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + sort-by-pos; + end-at-4gb; + size = <0x200>; + u-boot-with-ucode-ptr { + }; + + u-boot-dtb-with-ucode { + }; + + u-boot-ucode { + }; + }; + + microcode { + update@0 { + data = <0x12345678 0x12345679>; + }; + }; +}; diff --git a/tools/binman/test/u_boot_ucode_ptr b/tools/binman/test/u_boot_ucode_ptr new file mode 100755 index 0000000000000000000000000000000000000000..dbfb184cecfbcf55cf43ed4f4ac0ee90a7364d93 GIT binary patch literal 4175 zcmeHKze~eV5WfDv>Y%NqTOB%ds7Rl!MiB>>qFqFAD7b~B30kpv@e1VLh}je}Vs) zgM)i3QgG0CU(yE=7YE1p!Ew2t@9xWVH@o|LsZ@#-(v%@sqt7rjSl=(i5rZlmsZoxy zQ9SaF!jM>&I0rHVXMs3_>*wPh=u>4I0zc&NRXVJG0rgz2p&8H&Xa+O`ngPv#W<WEb z8PE)91~dczzyR*A5=(}qebAw|qP00Wx+^}sOy2XS&mWD8@+0oQG~%t+cBR&_15XAO zLu?77z7|AQ^SWt>h9TCMV?U7?UiPJBvzCKcpQta-m##SW0$~TeGpF8jNCaKqaY=Oj ze&6*ZKlNvnIWxzC`EXm}&a5V?u^%8<um|=meT89(@6%cSR#15x>_A>)8o(X9qLQXD z#1~o6OQFqqJIY{<8~_@#DLmzgZrQ+Xi@EVGZrnMRWWNeKSJ|ha`YAi9u{Z4aQjhnG z?c~ddtBklhOXCp#9(;g{)Q?Fq+c>PTU-d6wo4~YvUz*V$GtcEfbjfs-ZCgXv9QLkU KGKbO{NcskUZF7hK
literal 0 HcmV?d00001
diff --git a/tools/binman/test/u_boot_ucode_ptr.c b/tools/binman/test/u_boot_ucode_ptr.c new file mode 100644 index 0000000..434c9f4 --- /dev/null +++ b/tools/binman/test/u_boot_ucode_ptr.c @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2016 Google, Inc + * + * SPDX-License-Identifier: GPL-2.0+ + * + * Simple program to create a _dt_ucode_base_size symbol which can be read + * by 'nm'. This is used by binman tests. + * + * Build with: + * cc -march=i386 -m32 -o u_boot_ucode_ptr -T u_boot_ucode_ptr.lds -nostdlib \ + u_boot_ucode_ptr.c + */ + +static unsigned long _dt_ucode_base_size[2] + __attribute__((section(".ucode"))) = {1, 2}; diff --git a/tools/binman/test/u_boot_ucode_ptr.lds b/tools/binman/test/u_boot_ucode_ptr.lds new file mode 100644 index 0000000..167debf --- /dev/null +++ b/tools/binman/test/u_boot_ucode_ptr.lds @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2016 Google, Inc + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") +OUTPUT_ARCH(i386) +ENTRY(_start) + +SECTIONS +{ + . = 0xfffffdf0; + _start = .; + .ucode : { + *(.ucode) + } +}

Add an entry type for u-boot.img (a legacy U-Boot image) and a simple test.
Signed-off-by: Simon Glass sjg@chromium.org ---
tools/binman/etype/u_boot_img.py | 17 +++++++++++++++++ tools/binman/func_test.py | 7 +++++++ tools/binman/test/36_u_boot_img.dts | 11 +++++++++++ 3 files changed, 35 insertions(+) create mode 100644 tools/binman/etype/u_boot_img.py create mode 100644 tools/binman/test/36_u_boot_img.dts
diff --git a/tools/binman/etype/u_boot_img.py b/tools/binman/etype/u_boot_img.py new file mode 100644 index 0000000..744f1b4 --- /dev/null +++ b/tools/binman/etype/u_boot_img.py @@ -0,0 +1,17 @@ +# Copyright (c) 2016 Google, Inc +# Written by Simon Glass sjg@chromium.org +# +# SPDX-License-Identifier: GPL-2.0+ +# +# Entry-type module for U-Boot binary +# + +from entry import Entry +from blob import Entry_blob + +class Entry_u_boot_img(Entry_blob): + def __init__(self, image, etype, node): + Entry_blob.__init__(self, image, etype, node) + + def GetDefaultFilename(self): + return 'u-boot.img' diff --git a/tools/binman/func_test.py b/tools/binman/func_test.py index 4fc76bd..22066be 100644 --- a/tools/binman/func_test.py +++ b/tools/binman/func_test.py @@ -27,6 +27,7 @@ import tout
# Contents of test files, corresponding to different entry types U_BOOT_DATA = '1234' +U_BOOT_IMG_DATA = 'img' U_BOOT_SPL_DATA = '567' BLOB_DATA = '89' ME_DATA = '0abcd' @@ -60,6 +61,7 @@ class TestFunctional(unittest.TestCase):
# Create some test files TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) + TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA) TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) TestFunctional._MakeInputFile('blobfile', BLOB_DATA) TestFunctional._MakeInputFile('me.bin', ME_DATA) @@ -643,3 +645,8 @@ class TestFunctional(unittest.TestCase): first = data[:len(U_BOOT_NODTB_DATA)] self.assertEqual('nodtb with microcode' + pos_and_size + ' somewhere in here', first) + + def testUBootImg(self): + """Test that u-boot.img can be put in a file""" + data = self._DoReadFile('36_u_boot_img.dts') + self.assertEqual(U_BOOT_IMG_DATA, data) diff --git a/tools/binman/test/36_u_boot_img.dts b/tools/binman/test/36_u_boot_img.dts new file mode 100644 index 0000000..aa5a3fe --- /dev/null +++ b/tools/binman/test/36_u_boot_img.dts @@ -0,0 +1,11 @@ +/dts-v1/; + +/ { + #address-cells = <1>; + #size-cells = <1>; + + binman { + u-boot-img { + }; + }; +};

Add a standard command definition for binman so that it can be used in makefiles.
Signed-off-by: Simon Glass sjg@chromium.org ---
Makefile | 6 ++++++ 1 file changed, 6 insertions(+)
diff --git a/Makefile b/Makefile index 7ce933c..6155f11 100644 --- a/Makefile +++ b/Makefile @@ -870,6 +870,12 @@ u-boot.ldr: u-boot $(LDR) -T $(CONFIG_CPU) -c $@ $< $(LDR_FLAGS) $(BOARD_SIZE_CHECK)
+# binman +# --------------------------------------------------------------------------- +quiet_cmd_binman = BINMAN $@ +cmd_binman = $(srctree)/tools/binman/binman -d u-boot.dtb -O . \ + -I . -I $(srctree)/board/$(BOARDDIR) $< + OBJCOPYFLAGS_u-boot.ldr.hex := -I binary -O ihex
OBJCOPYFLAGS_u-boot.ldr.srec := -I binary -O srec

It is sometimes useful to be able to reference configuration options in a device tree source file. Add the necessary includes so that this works.
Signed-off-by: Simon Glass sjg@chromium.org ---
scripts/Makefile.lib | 5 +++++ 1 file changed, 5 insertions(+)
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index e720562..440f871 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -168,6 +168,11 @@ ld_flags = $(LDFLAGS) $(ldflags-y) dtc_cpp_flags = -Wp,-MD,$(depfile).pre.tmp -nostdinc \ -I$(srctree)/arch/$(ARCH)/dts \ -I$(srctree)/arch/$(ARCH)/dts/include \ + -Iinclude \ + -I$(srctree)/include \ + -I$(srctree)/arch/$(ARCH)/include \ + -include $(srctree)/include/linux/kconfig.h \ + -D__ASSEMBLY__ \ -undef -D__DTS__
# Finds the multi-part object the current object will be linked into

Add an example usage of binman for a sunxi board. This involves adding the image definition to the device tree and using it in the Makefile.
This is for example only.
Signed-off-by: Simon Glass sjg@chromium.org ---
Makefile | 4 +--- arch/arm/dts/sun7i-a20-pcduino3.dts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile index 6155f11..8862b8b 100644 --- a/Makefile +++ b/Makefile @@ -1084,10 +1084,8 @@ u-boot-x86-16bit.bin: u-boot FORCE endif
ifneq ($(CONFIG_SUNXI),) -OBJCOPYFLAGS_u-boot-sunxi-with-spl.bin = -I binary -O binary \ - --pad-to=$(CONFIG_SPL_PAD_TO) --gap-fill=0xff u-boot-sunxi-with-spl.bin: spl/sunxi-spl.bin u-boot.img FORCE - $(call if_changed,pad_cat) + $(call if_changed,binman) endif
ifneq ($(CONFIG_TEGRA),) diff --git a/arch/arm/dts/sun7i-a20-pcduino3.dts b/arch/arm/dts/sun7i-a20-pcduino3.dts index 1a8b39b..141044e 100644 --- a/arch/arm/dts/sun7i-a20-pcduino3.dts +++ b/arch/arm/dts/sun7i-a20-pcduino3.dts @@ -42,6 +42,7 @@ */
/dts-v1/; +#include <config.h> #include "sun7i-a20.dtsi" #include "sunxi-common-regulators.dtsi"
@@ -62,6 +63,17 @@ stdout-path = "serial0:115200n8"; };
+ binman { + filename = "u-boot-sunxi-with-spl.bin"; + pad-byte = <0xff>; + blob { + filename = "spl/sunxi-spl.bin"; + }; + u-boot-img { + pos = <CONFIG_SPL_PAD_TO>; + }; + }; + leds { compatible = "gpio-leds"; pinctrl-names = "default";

(adding Hans)
On Mon, 2016-07-25 at 18:59 -0600, Simon Glass wrote:
Add an example usage of binman for a sunxi board. This involves adding the
image definition to the device tree and using it in the Makefile.
My concern with this approach is that the *.dts(i) files used for sunxi are sync'd unmodified from the upstream repo. I think this is a valuable property to retain in terms of maintenance burden for us (resync is trivial only if there are no local changes).
The conflict is then whether the device tree people would consider this new block to be "describing the hardware" or not and therefore whether these changes would be welcomed/allowed upstream. I'm not sure which way it would go TBH.
There is also an issue with duplication, since this stanza is going to be the same for all the dozens of sunxi platforms. I suppose this can be addressed by putting it in a suitable .dtsi file, but that does sort of start to suggest that this information belongs outside the per-board .dts in a per-platform file (at which point I would have have to ask if .dts is the best/most convenient syntax?).
Ian.

Hi Ian,
On 26 July 2016 at 07:35, Ian Campbell ijc@hellion.org.uk wrote:
(adding Hans)
On Mon, 2016-07-25 at 18:59 -0600, Simon Glass wrote:
Add an example usage of binman for a sunxi board. This involves adding the
image definition to the device tree and using it in the Makefile.
My concern with this approach is that the *.dts(i) files used for sunxi are sync'd unmodified from the upstream repo. I think this is a valuable property to retain in terms of maintenance burden for us (resync is trivial only if there are no local changes).
Thanks for looking at it.
One option is to put the local changes in an #include file. We have a similar problem with "u-boot,dm-pre-reloc". We really should be able to push these sorts of things upstream....
The conflict is then whether the device tree people would consider this new block to be "describing the hardware" or not and therefore whether these changes would be welcomed/allowed upstream. I'm not sure which way it would go TBH.
Me neither. Although there is a flash map / partition map device tree binding and this is similar.
The binman description lays out what the boot ROM needs in many cases, which is pretty close to being the 'hardware'.
There is also an issue with duplication, since this stanza is going to be the same for all the dozens of sunxi platforms. I suppose this can be addressed by putting it in a suitable .dtsi file, but that does sort of start to suggest that this information belongs outside the per-board .dts in a per-platform file (at which point I would have have to ask if .dts is the best/most convenient syntax?).
Yes I think we could use the same thing for large groups of boards, but we already have an soc-specific .dtsi in most cases, so that should work quite nicely.
The nice things about device tree are:
- people understand it - it can be accessed at run-time (e.g. so we know where to find the next image to load) - we have robust tools for dealing with device tree source and binaries
Regards, Simon

Add an example usage of binman for an x86 board. This involves adding the image definition to the device tree and using it in the Makefile. The existing ifdtool features are no-longer needed.
This is for example only.
Signed-off-by: Simon Glass sjg@chromium.org ---
Makefile | 45 +++------------------------------- arch/x86/dts/chromebook_link.dts | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 42 deletions(-)
diff --git a/Makefile b/Makefile index 8862b8b..770984c 100644 --- a/Makefile +++ b/Makefile @@ -1023,50 +1023,11 @@ endif
# x86 uses a large ROM. We fill it with 0xff, put the 16-bit stuff (including # reset vector) at the top, Intel ME descriptor at the bottom, and U-Boot in -# the middle. +# the middle. This is handled by binman based on an image description in the +# board's device tree. ifneq ($(CONFIG_X86_RESET_VECTOR),) rom: u-boot.rom FORCE
-IFDTOOL=$(objtree)/tools/ifdtool -IFDTOOL_FLAGS = -f 0:$(objtree)/u-boot.dtb -IFDTOOL_FLAGS += -m 0x$(shell $(NM) u-boot |grep _dt_ucode_base_size |cut -d' ' -f1) -IFDTOOL_FLAGS += -U $(CONFIG_SYS_TEXT_BASE):$(objtree)/u-boot-nodtb.bin -IFDTOOL_FLAGS += -w $(CONFIG_SYS_X86_START16):$(objtree)/u-boot-x86-16bit.bin -IFDTOOL_FLAGS += -C - -ifneq ($(CONFIG_HAVE_INTEL_ME),) -IFDTOOL_ME_FLAGS = -D $(srctree)/board/$(BOARDDIR)/descriptor.bin -IFDTOOL_ME_FLAGS += -i ME:$(srctree)/board/$(BOARDDIR)/me.bin -endif - -ifneq ($(CONFIG_HAVE_MRC),) -IFDTOOL_FLAGS += -w $(CONFIG_X86_MRC_ADDR):$(srctree)/board/$(BOARDDIR)/mrc.bin -endif - -ifneq ($(CONFIG_HAVE_FSP),) -IFDTOOL_FLAGS += -w $(CONFIG_FSP_ADDR):$(srctree)/board/$(BOARDDIR)/$(CONFIG_FSP_FILE) -endif - -ifneq ($(CONFIG_HAVE_CMC),) -IFDTOOL_FLAGS += -w $(CONFIG_CMC_ADDR):$(srctree)/board/$(BOARDDIR)/$(CONFIG_CMC_FILE) -endif - -ifneq ($(CONFIG_HAVE_VGA_BIOS),) -IFDTOOL_FLAGS += -w $(CONFIG_VGA_BIOS_ADDR):$(srctree)/board/$(BOARDDIR)/$(CONFIG_VGA_BIOS_FILE) -endif - -ifneq ($(CONFIG_HAVE_REFCODE),) -IFDTOOL_FLAGS += -w $(CONFIG_X86_REFCODE_ADDR):refcode.bin -endif - -quiet_cmd_ifdtool = IFDTOOL $@ -cmd_ifdtool = $(IFDTOOL) -c -r $(CONFIG_ROM_SIZE) u-boot.tmp; -ifneq ($(CONFIG_HAVE_INTEL_ME),) -cmd_ifdtool += $(IFDTOOL) $(IFDTOOL_ME_FLAGS) u-boot.tmp; -endif -cmd_ifdtool += $(IFDTOOL) $(IFDTOOL_FLAGS) u-boot.tmp; -cmd_ifdtool += mv u-boot.tmp $@ - refcode.bin: $(srctree)/board/$(BOARDDIR)/refcode.bin FORCE $(call if_changed,copy)
@@ -1076,7 +1037,7 @@ cmd_ldr = $(LD) $(LDFLAGS_$(@F)) \
u-boot.rom: u-boot-x86-16bit.bin u-boot.bin FORCE \ $(if $(CONFIG_HAVE_REFCODE),refcode.bin) - $(call if_changed,ifdtool) + $(call if_changed,binman)
OBJCOPYFLAGS_u-boot-x86-16bit.bin := -O binary -j .start16 -j .resetvec u-boot-x86-16bit.bin: u-boot FORCE diff --git a/arch/x86/dts/chromebook_link.dts b/arch/x86/dts/chromebook_link.dts index fb1b31d..50fb2f9 100644 --- a/arch/x86/dts/chromebook_link.dts +++ b/arch/x86/dts/chromebook_link.dts @@ -1,5 +1,6 @@ /dts-v1/;
+#include <config.h> #include <dt-bindings/gpio/x86-gpio.h>
/include/ "skeleton.dtsi" @@ -18,6 +19,57 @@ usb1 = &usb_1; };
+#ifdef CONFIG_ROM_SIZE + binman { + filename = "u-boot.rom"; + end-at-4gb; + sort-by-pos; + pad-byte = <0xff>; + size = <CONFIG_ROM_SIZE>; +#ifdef CONFIG_HAVE_INTEL_ME + intel-descriptor { + }; + intel-me { + }; +#endif + u-boot-with-ucode-ptr { + pos = <CONFIG_SYS_TEXT_BASE>; + }; + u-boot-dtb-with-ucode { + }; + u-boot-ucode { + align = <16>; + }; +#ifdef CONFIG_HAVE_MRC + intel-mrc { + pos = <CONFIG_X86_MRC_ADDR>; + }; +#endif +#ifdef CONFIG_HAVE_FSP + intel-fsp { + pos = <CONFIG_FSP_ADDR>; + }; +#endif +#ifdef CONFIG_HAVE_CMC + intel-cmc { + pos = <CONFIG_CMC_ADDR>; + }; +#endif +#ifdef CONFIG_HAVE_VGA_BIOS + intel-vga { + pos = <CONFIG_VGA_BIOS_ADDR>; + }; +#endif +#ifdef CONFIG_HAVE_REFCODE + intel-refcode { + pos = <CONFIG_X86_REFCODE_ADDR>; + }; +#endif + x86-start16 { + pos = <CONFIG_SYS_X86_START16>; + }; + }; +#endif config { silent_console = <0>; };

Hi,
On 25 July 2016 at 18:58, Simon Glass sjg@chromium.org wrote:
This series introduces binman, a tool designed to create firmware images. It provides a way to bring together various binaries and place them in an image, at particular positions and with configurable alignment.
Packaging of firmware is quite a different task from building the various parts. In many cases the various binaries which go into the image come from separate build systems. For example, ARM Trusted Firmware is used on ARMv8 devices but is not built in the U-Boot tree. If a Linux kernel is included in the firmware image, it is built elsewhere.
It is of course possible to add more and more build rules to the U-Boot build system to cover these cases. It can shell out to other Makefiles and build scripts. But it seems better to create a clear divide between building software and packaging it.
U-Boot supports a very large number of boards. Many of these have their own specific rules for how an image should be put together so that it boots correctly. At present these rules are described by manual instructions, different for each board. By turning these instructions into a standard format, we can support making valid images for any board without manual effort, lots of READMEs, etc.
Images consist of a number of entries which are combined to make up the final image. The image is described in the device tree for the board, meaning that it can be accessed at run-time if desired.
Binman is an extensible tool. A set of standard entries is provides, but new entries can be created fairly easily. Entries can be as simple as providing the name of a file to include. They can also handle more complex requirements, such as adjusting the input file or reading header information from one entry to control the position of another.
U-Boot's mkimage builds FIT images and various other binaries. Binman augments this by allowing these binaries to be packed together. While FIT should be used where possible, it cannot be used everywhere. For example, many devices require executable code at a particular offset in the image. X86 machines require lots of binary blobs at particular places, and a microcode collection easily accessible at boot.
So far binman has enough functionality to be useful, but apart from a few RFC patches, no attempt is made to switch boards over to use it. There should be enough material to permit review and comments.
The series is available at u-boot-dm/binman-working
Future work and missing features are documented in the README.
I'm going to apply the clean-up parts of this series to u-boot-dm/next and then resend just the tool as a shorter series with some updates.
Regards, Simon
participants (4)
-
Bin Meng
-
Heiko Schocher
-
Ian Campbell
-
Simon Glass