tools: mkimage: support shared image data through the image-data property
cover tools: mkimage: support shared image data through the image-data property
The 'Shared image data' section of the Flat Image Tree specification [1] defines an image-data property: an image node names another image node under /images whose binary payload it shares, while carrying its own load, entry and remaining metadata. A multi-configuration FIT can then offer the same binary at different addresses without storing it twice. The normative text is in the specification's main branch; a refinement of the cipher interaction is under review in [2].
mkimage resolves the references right after external data has been
imported: the target's data is copied into the referencing node, so the later ciphering, hashing, signing and overlap checking steps see normal image nodes, and the property is retained in the resolved FIT as a record of the sharing relationship. Invalid references are rejected:
an undefined target, a chained reference, data that disagrees with the target, external data properties mixed with image-data, and cipher settings that would produce different ciphertexts. Consumers need no changes, since bootloaders ignore the property and the data properties remain authoritative.
Patch 1 makes fit_image_setup_cipher() use an explicit iv from the cipher node when no iv-name-hint is given, checking its length, and fall back to a random IV only when neither is present. This matches the specification, makes encryption reproducible, and lets encrypted data be shared without relying on an IV file in the key directory.
Patch 2 adds the resolution and validation described above, covering inline FITs completely.
Patch 3 makes external data mode (-E and -p) store the shared payload only once: sharers skip the extraction and are pointed at the referenced image's region through identical data-offset or data-position and data-size values.
Patch 4 teaches the configuration load overlap check to size data-less sharers from their target. The check runs before the references are resolved, so such images previously escaped it and their load regions went unvalidated.
Patch 5 adds the sandbox tests: inline and external sharing, mkimage -F re-processing including conversion between the two forms, the rejection cases, encrypted sharing through a shared iv-name-hint and through a shared explicit iv, and the overlap rejection.
Each patch builds and works on its own. On sandbox the new tests pass together with the FIT, vboot and mkimage pytest selection (77 passed) and the complete binman suite (606 tests). The feature is also verified on hardware: an i.MX6ULL EVK boots an Ubuntu Core style multi-board FIT whose i.MX6ULL variants share the kernel, ramdisk and FDT payloads through image-data, keeping the image at roughly 40 MB instead of the 78 MB it would take with the payloads duplicated.
[1] https://github.com/open-source-firmware/flat-image-tree
[2] https://github.com/open-source-firmware/flat-image-tree/pull/58
Aristo Chen (5):
tools: mkimage: use an explicit iv from the cipher node when
encrypting
tools: mkimage: resolve image-data references between FIT images
tools: mkimage: share the external-data region between image-data
images
tools: mkimage: include image-data sharers in the load overlap check
test: py: add tests for the FIT image-data property
include/image.h | 1 + test/py/tests/test_fit_image_data.py | 373 +++++++++++++++++++++++++++ tools/fit_image.c | 370 +++++++++++++++++++++++++- tools/image-host.c | 18 +- 4 files changed, 757 insertions(+), 5 deletions(-) create mode 100644 test/py/tests/test_fit_image_data.py
Signature · 1 line
2.43.0
PATCH 1/5 tools: mkimage: use an explicit iv from the cipher node when encrypting
fit_image_setup_cipher() only consumes iv-name-hint: without the hint a fresh random IV is generated for every image, and any iv property the cipher node already carries is ignored and later overwritten. The FIT specification defines iv as the raw initialisation vector carried in the FIT and requires at least one of iv-name-hint or iv to be present, so an .its author who supplies an explicit iv reasonably expects it to be used.
Use the iv property when no iv-name-hint is given, after checking that its length matches the cipher's IV length, and only fall back to a random IV when the node provides neither. Besides matching the specification this makes encryption reproducible, since the same source, key and IV now produce the same ciphertext on every run. It also allows images that share data through the image-data property, added later in this series, to be encrypted consistently without relying on an IV file in the key directory.
--- tools/image-host.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
tools/image-host.c +16 -2
@@ -421,7 +421,9 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, const char *image_name, int image_noffset, int noffset) { + const void *iv;char *algo_name; +int ivlen;int ret =-1 ;if (fit_image_cipher_get_algo (fit, noffset, &algo_name)) {
@@ -443,8 +445,9 @@ static int fit_image_setup_cipher(struct image_cipher_info *info,/* * Read the IV name * -* If this property is not provided then mkimage will generate -* a random IV and store it in the FIT image +* If this property is not provided then mkimage will use the iv +* property of the cipher node, if present, or generate a random +* IV and store it in the FIT image */ info->ivname =fdt_getprop (fit, noffset,"iv-name-hint" ,NULL );
@@ -479,6 +482,7 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, goto out; } + iv =fdt_getprop (fit, noffset,"iv" , &ivlen);if (info->ivname) {/* Read the IV in the file */ ret = fit_image_read_key_iv_data(info->keydir, info->ivname,
@@ -486,6 +490,16 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, info->cipher->iv_len);if (ret <0 ) goto out; + }else if (iv) { +/* Use the explicit IV from the cipher node */ +if (ivlen != info->cipher->iv_len) { +fprintf (stderr, +"Wrong iv length for cipher in image '%s' (expected %d bytes, got %d)\n" , + image_name, info->cipher->iv_len, ivlen); + ret =-1 ; + goto out; + } +memcpy ((void *)info->iv, iv, ivlen); }else {/* Generate an ramdom IV */ ret =get_random_data ((void *)info->iv, info->cipher->iv_len);
Signature · 1 line
2.43.0
Re: [PATCH v1 1/5] tools: mkimage: use an explicit iv from the cipher node when encrypting
Quoted 19 lines · Aristo Chen
tools: mkimage: use an explicit iv from the cipher node when encrypting
fit_image_setup_cipher()only consumes iv-name-hint: without the hint a fresh random IV is generated for every image, and any iv property the cipher node already carries is ignored and later overwritten. The FIT specification defines iv as the raw initialisation vector carried in the FIT and requires at least one of iv-name-hint or iv to be present, so an .its author who supplies an explicit iv reasonably expects it to be used.Use the iv property when no iv-name-hint is given, after checking that its length matches the cipher's IV length, and only fall back to a random IV when the node provides neither. Besides matching the specification this makes encryption reproducible, since the same source, key and IV now produce the same ciphertext on every run. It also allows images that share data through the image-data property, added later in this series, to be encrypted consistently without relying on an IV file in the key directory.
Quoted 4 lines
Signed-off-by Aristo Chen <[email protected]>
tools/image-host.c| 18 ++++++++++++++++--1 file changed, 16 insertions(+), 2 deletions(-)
PATCH 2/5 tools: mkimage: resolve image-data references between FIT images
In reply to Aristo ChenAn image node may carry an 'image-data' property naming another image node under /images whose binary data it shares. This lets several configurations load the same payload at different addresses without storing it twice, as described in the 'Shared image data' section of the Flat Image Tree specification [1].
Resolve the references in mkimage after any external data has been
imported: the target's data is copied into the referencing node, so the later ciphering, hashing and signing steps see a normal image node. The property itself is retained in the resolved FIT as a record of the sharing relationship. When mkimage re-processes a FIT that already contains resolved references, the node's data must match the target's data exactly.
A reference is rejected when the target does not exist, when the target itself carries an image-data property (chained references), when the node's existing data disagrees with the target, or when the node mixes image-data with external data properties. Since encryption happens per node, sharing encrypted data is only accepted when both nodes use the same algorithm and key together with the same iv-name-hint or, when neither uses a hint, the same explicit iv; anything else, including the independent random IV generated when a node provides no IV at all, would produce different ciphertexts.
[1] https://github.com/open-source-firmware/flat-image-tree
--- include/image.h | 1 + tools/fit_image.c | 286 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 287 insertions(+)
include/image.h +1 -0
@@ -1180,6 +1180,7 @@ int booti_setup(ulong image, ulong *relocated_addr, ulong *size,#define FIT_DATA_POSITION_PROP "data-position"#define FIT_DATA_OFFSET_PROP "data-offset"#define FIT_DATA_SIZE_PROP "data-size" +#define FIT_IMAGE_DATA_PROP "image-data"#define FIT_TIMESTAMP_PROP "timestamp"#define FIT_DESC_PROP "description"#define FIT_ARCH_PROP "arch"
tools/fit_image.c +286 -0
298 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v1 2/5] tools: mkimage: resolve image-data references between FIT images
Hi Aristo,
Quoted 28 lines · Aristo Chen
tools: mkimage: resolve image-data references between FIT images
An image node may carry an 'image-data' property naming another image node under /images whose binary data it shares. This lets several configurations load the same payload at different addresses without storing it twice, as described in the 'Shared image data' section of the Flat Image Tree specification [1].
Resolve the references in mkimage after any external data has been
imported: the target's data is copied into the referencing node, so the later ciphering, hashing and signing steps see a normal image node. The property itself is retained in the resolved FIT as a record of the sharing relationship. When mkimage re-processes a FIT that already contains resolved references, the node's data must match the target's data exactly.A reference is rejected when the target does not exist, when the target itself carries an image-data property (chained references), when the node's existing data disagrees with the target, or when the node mixes image-data with external data properties. Since encryption happens per [...]
include/image.h| 1 +tools/fit_image.c | 286 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 287 insertions(+)
tools/fit_image.c err: · line 1175
Quoted 7 lines
tools/fit_image.c +7
-0@@ -1169,6 +1169,287 @@ err: + ret =fdt_open_into (old_fdt, fdt, size); +if (ret) { +debug ("%s: Failed to expand FIT: %s\n" , __func__, +fdt_strerror (ret)); + ret = -EINVAL ; + goto err_munmap; + }
In practice this function should not fail if the DT is corrupt, so debug() seems good here.
tools/fit_image.c err: · line 1177
Quoted 11 lines
tools/fit_image.c +9
-0@@ -1169,6 +1169,287 @@ err: + extra =0 ; +fdt_for_each_subnode (node, old_fdt, images) { +const char *ref; +int target, len; + + ref =fdt_getprop (old_fdt, node,FIT_IMAGE_DATA_PROP ,NULL ); +if (!ref) +continue ; + found = true;
When mkimage -F re-processes an already-resolved FIT, every reference hits the 'Already resolved' branch and no data is copied, yet the function still allocates a full copy, runs fdt_open_into(), fdt_pack() and rewrites the file. Please track whether anything was actually copied and skip the rewrite when nothing changed.
tools/fit_image.c err: · line 1172
Quoted 6 lines
tools/fit_image.c +4
-0@@ -1169,6 +1169,287 @@ err: +/** +* fit_check_shared_cipher() - Check cipher settings of images sharing data +* +* Two image nodes can only share binary data if their cipher settings
The kernel-doc blocks on fit_props_equal(), fit_check_shared_cipher() and fit_resolve_image_data() are missing @param descriptions and a Return line. Please spell out the parameters and the return value, particularly for the cipher one which conflates the source-.its and re-processed-FIT cases.
tools/fit_image.c err: · line 1174
Quoted 8 lines
tools/fit_image.c +6
-0@@ -1169,6 +1169,287 @@ err: +if (fdt_getprop (fdt, node,FIT_DATA_OFFSET_PROP ,NULL ) || +fdt_getprop (fdt, node,FIT_DATA_POSITION_PROP ,NULL ) || +fdt_getprop (fdt, node,FIT_DATA_SIZE_PROP ,NULL )) { +fprintf (stderr, +"Error: image '%s': image-data must not be combined with external data properties\n" , + name);
Just to check: fit_import_data() runs before this and, on success, deletes data-offset/data-position/data-size after importing. So this check only fires when the source .its declared those properties in a form fit_import_data() refused to import (e.g. offset without size). Is that the intent? If you meant to also catch a well-formed 'image-data plus external data' combination in the source, this needs to happen either before fit_import_data() or by updating fit_import_data() to skip nodes that carry image-data ... otherwise the external payload is silently absorbed and the mixing goes unreported.
tools/fit_image.c err: · line 1171
Quoted 5 lines
tools/fit_image.c +3
-0@@ -1169,6 +1169,287 @@ err: +if (fdt_getprop (fdt, ncipher,"iv-name-hint" ,NULL ) || +fdt_getprop (fdt, tcipher,"iv-name-hint" ,NULL )) { +if (!fit_props_equal (fdt, ncipher, tcipher,"iv-name-hint" )) {
Since you are adding a new feature that leans heavily on iv-name-hint, iv and data-size-unciphered, please add FIT_IV_HINT / FIT_IV_PROP / FIT_DATA_SIZE_UNCIPHERED_PROP alongside FIT_KEY_HINT in include/image.h and use those here. It keeps the cipher-related property names in one place and makes future renames tractable.
Regards,
Simon
PATCH 3/5 tools: mkimage: share the external-data region between image-data images
In reply to Aristo ChenWhen building with external data (-E or -p), an image sharing another image's data through image-data still receives its own copy of the payload in the external area. Skip such images while the data is moved out of the FIT and point them at the referenced image's region afterwards, through identical data-offset or data-position and data-size values, so the payload is stored only once. This is the resolved form the specification describes for external data: both nodes reference the same region.
An image whose reference did not resolve cannot reach this point, since fit_resolve_image_data() has already rejected the FIT.
--- tools/fit_image.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
tools/fit_image.c +46 -0
@@ -813,6 +813,14 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) data =fdt_getprop (fdt, node,FIT_DATA_PROP , &len);if (!data)continue ; +if (fdt_getprop (fdt, node,FIT_IMAGE_DATA_PROP ,NULL )) { +/* +* This node shares another image's data, which gets +* its own offset below; don't store a second copy +*/ + len =0 ; +continue ; + } ret =fit_copy_image_data (fdt, node, buf, buf_ptr, data, &len);if (ret)
@@ -849,6 +857,44 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname)/* Increment 'buf_ptr' for the trailing image. */ buf_ptr +=ALIGN (len, align_size); +/* +* Images sharing another image's data (image-data) reference the +* same region instead of storing a second copy +*/ +fdt_for_each_subnode (node, fdt, images) { +const char *prop, *ref; +int target, offset, size; + + ref =fdt_getprop (fdt, node,FIT_IMAGE_DATA_PROP ,NULL ); +if (!ref || !fdt_getprop (fdt, node,FIT_DATA_PROP ,NULL )) +continue ; + + prop = params->external_offset >0 ?FIT_DATA_POSITION_PROP : +FIT_DATA_OFFSET_PROP ; + target =fdt_subnode_offset (fdt, images, ref); + offset = target >=0 ?fdtdec_get_int (fdt, target, prop,-1 ) :-1 ; + size = target >=0 ? +fdtdec_get_int (fdt, target,FIT_DATA_SIZE_PROP ,-1 ) :-1 ; +if (offset ==-1 || size ==-1 ) { +fprintf (stderr, +"Error: image '%s': no extracted data to share from image '%s'\n" , +fdt_get_name (fdt, node,NULL ), ref); + ret = -EINVAL ; + goto err_munmap; + } + + ret =fdt_delprop (fdt, node,FIT_DATA_PROP ); +if (!ret) + ret =fdt_setprop_u32 (fdt, node, prop, offset); +if (!ret) + ret =fdt_setprop_u32 (fdt, node,FIT_DATA_SIZE_PROP , + size); +if (ret) { + ret = -EINVAL ; + goto err_munmap; + } + } +/* Pack the FDT and place the data after it */ fdt_pack (fdt);
Signature · 1 line
2.43.0
Re: [PATCH v1 3/5] tools: mkimage: share the external-data region between image-data images
Quoted 11 lines · Aristo Chen
tools: mkimage: share the external-data region between image-data images
When building with external data (-E or -p), an image sharing another image's data through image-data still receives its own copy of the payload in the external area. Skip such images while the data is moved out of the FIT and point them at the referenced image's region afterwards, through identical data-offset or data-position and data-size values, so the payload is stored only once. This is the resolved form the specification describes for external data: both nodes reference the same region.
Quoted 7 lines
An image whose reference did not resolve cannot reach this point, since
fit_resolve_image_data()has already rejected the FIT.Signed-off-by Aristo Chen <[email protected]>
tools/fit_image.c| 46 ++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 46 insertions(+)
PATCH 4/5 tools: mkimage: include image-data sharers in the load overlap check
In reply to Aristo ChenThe overlap check at the end of fit_import_data() sizes each referenced image from its data property and skips images without data, since such images are never copied anywhere. That assumption no longer holds for an image that shares another image's data through image-data: the reference is resolved only after the import, so the image has no data yet when the check runs and escaped it entirely, leaving its load region unvalidated even though the resolved image is loaded like any other.
Size such images from their target's data instead. References that do not resolve are left alone here; fit_resolve_image_data() rejects them right after the import.
--- tools/fit_image.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-)
tools/fit_image.c +35 -3
@@ -951,6 +951,34 @@ err:return ret; } +/** +* fit_image_effective_data() - Get the data an image will carry once resolved +* +* Returns the image's own data or, for an image that shares another +* image's data through a not yet resolved image-data reference, the +* target's data. References that do not resolve are left alone here, +* since fit_resolve_image_data() rejects them right after the import. +*/ +static const char *fit_image_effective_data (const void *fdt,int images, +int img,int *sizep) +{ +const char *data, *ref; +int target; + + data =fdt_getprop (fdt, img,FIT_DATA_PROP , sizep); +if (data) +return data; + + ref =fdt_getprop (fdt, img,FIT_IMAGE_DATA_PROP ,NULL ); +if (!ref) +return NULL ; + target =fdt_subnode_offset (fdt, images, ref); +if (target <0 ) +return NULL ; + +return fdt_getprop (fdt, target,FIT_DATA_PROP , sizep); +} +static int fit_import_data (struct image_tool_params *params,const char *fname) {void *fdt, *old_fdt;
@@ -1100,12 +1128,16 @@ static int fit_import_data(struct image_tool_params *params, const char *fname)* Collect the memory region the image is * loaded to. Images without a load address or * without data are never copied anywhere, so -* they cannot conflict. +* they cannot conflict. An image-data +* reference is resolved only after the +* import, but the image ends up with the +* target's data, so size its region from the +* target. */ if (fit_image_get_load (fdt, img, &img_load))continue ; - img_data =fdt_getprop (fdt, img,FIT_DATA_PROP , - &img_size); + img_data =fit_image_effective_data (fdt, + images, img, &img_size);if (!img_data || img_size <=0 )continue ;
Signature · 1 line
2.43.0
Re: [PATCH v1 4/5] tools: mkimage: include image-data sharers in the load overlap check
Quoted 11 lines · Aristo Chen
tools: mkimage: include image-data sharers in the load overlap check
The overlap check at the end of
fit_import_data()sizes each referenced image from its data property and skips images without data, since such images are never copied anywhere. That assumption no longer holds for an image that shares another image's data through image-data: the reference is resolved only after the import, so the image has no data yet when the check runs and escaped it entirely, leaving its load region unvalidated even though the resolved image is loaded like any other.
Quoted 8 lines
Size such images from their target's data instead. References that do not resolve are left alone here;
fit_resolve_image_data()rejects them right after the import.Signed-off-by Aristo Chen <[email protected]>
tools/fit_image.c| 38 +++++++++++++++++++++++++++++++++++---1 file changed, 35 insertions(+), 3 deletions(-)
PATCH 5/5 test: py: add tests for the FIT image-data property
In reply to Aristo ChenCover mkimage's resolution of image-data references between images. The positive tests check that an inline FIT carries identical data and hash values on the sharer and the target while their own properties remain independent, that external data mode points both nodes at a single copy of the payload, that a resolved FIT can be re-processed with -F including conversion to external data, and that two nodes with identical cipher settings share the same ciphertext both inline and external, whether the IV comes from an iv-name-hint or from an explicit iv that is honored and retained in the FIT.
The negative tests check that mkimage rejects references to undefined images, chained references, data that disagrees with the target, image-data combined with external data properties, a configuration whose image-data sharers have overlapping load regions, sharing where only one node is encrypted, and encrypted sharing where the nodes disagree on the IV, through different iv-name-hints, different explicit ivs or no shared IV source at all.
--- test/py/tests/test_fit_image_data.py | 373 +++++++++++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 test/py/tests/test_fit_image_data.py
test/py/tests/test_fit_image_data.py new file +373 -0
@@ -0,0 +1,373 @@ +# SPDX-License-Identifier: GPL-2.0+ +# Copyright 2026 Canonical Ltd. +# +# Test mkimage handling of the 'image-data' FIT property + +"""Tests for the FIT 'image-data' property. + +An image node may name another image node under /images in an +'image-data' property to share its binary data. mkimage resolves the +reference by copying the data when building an inline FIT, and by +pointing both nodes at the same region when building with external data +(-E). Invalid references (undefined target, chained references, +mismatching data or incompatible cipher settings) must be rejected. +""" + +import os +import struct +import subprocess + +import pytest + +import fit_util + +BASE_ITS = ''' +/dts-v1/; + +/ { + description = "image-data test"; + + images { + kernel-1 { + description = "test kernel"; + data = /incbin/("%(kernel)s"); + type = "kernel"; + arch = "sandbox"; + os = "linux"; + compression = "none"; + load = <0x40000>; + entry = <0x40000>; + hash-1 { + algo = "sha256"; + }; + }; + kernel-alt { + description = "same kernel at another address"; + %(alt_props)s + type = "kernel"; + arch = "sandbox"; + os = "linux"; + compression = "none"; + load = <0x80000>; + entry = <0x80000>; + hash-1 { + algo = "sha256"; + }; + }; + %(extra_images)s + }; + + configurations { + default = "conf-1"; + conf-1 { + kernel = "kernel-1"; + }; + conf-2 { + kernel = "kernel-alt"; + }; + }; +}; +''' + +CHAIN_IMAGE = ''' + kernel-mid { + description = "middle image in a chain"; + image-data = "kernel-1"; + type = "kernel"; + arch = "sandbox"; + os = "linux"; + compression = "none"; + load = <0x60000>; + entry = <0x60000>; + }; +''' + +CIPHER = ''' + cipher { + algo = "aes256"; + key-name-hint = "kern"; + iv-name-hint = "kiv"; + }; +''' + +CIPHER_NO_IV = ''' + cipher { + algo = "aes256"; + key-name-hint = "kern"; + }; +''' + +CIPHER_IV = ''' + cipher { + algo = "aes256"; + key-name-hint = "kern"; + iv = [11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 00]; + }; +''' + +CIPHER_IV_ALT = ''' + cipher { + algo = "aes256"; + key-name-hint = "kern"; + iv = [00 ff ee dd cc bb aa 99 88 77 66 55 44 33 22 11]; + }; +''' + +OVERLAP_SHARER = ''' + filler { + description = "second sharer overlapping kernel-alt"; + image-data = "kernel-1"; + type = "loadable"; + arch = "sandbox"; + os = "linux"; + compression = "none"; + load = <0x80400>; + }; +''' + + +def mkimage(ubman, *args): + """Run mkimage with the given arguments, returning the process result""" + tool = os.path.join(ubman.config.build_dir, 'tools/mkimage') + return subprocess.run([tool] + list(args), capture_output=True, text=True) + + +def fdtget(itb, node, prop, ftype=None): + """Read a property from the FIT, returning None if it is missing""" + cmd = ['fdtget'] + (['-t', ftype] if ftype else []) + [itb, node, prop] + result = subprocess.run(cmd, capture_output=True, text=True) + if result.returncode: + return None + return result.stdout.strip() + + +def fdt_bytes(itb, node, prop): + """Read a property from the FIT as bytes, returning None if missing""" + value = fdtget(itb, node, prop, 'bx') + if value is None: + return None + return bytes(int(tok, 16) for tok in value.split()) + + +def build_fit_text(ubman, its_text, basename, *args, alt_props=None, + extra_images=''): + """Build a FIT from a full .its template, returning (result, itb, kdata)""" + kernel = fit_util.make_kernel(ubman, f'{basename}-kernel.bin', 'kernel') + params = { + 'kernel': kernel, + 'alt_props': alt_props or 'image-data = "kernel-1";', + 'extra_images': extra_images, + } + its = fit_util.make_its(ubman, its_text, params, f'{basename}.its') + itb = fit_util.make_fname(ubman, f'{basename}.itb') + result = mkimage(ubman, *args, '-f', its, itb) + with open(kernel, 'rb') as inf: + kdata = inf.read() + return result, itb, kdata + + +def build_fit(ubman, alt_props, extra_images='', basename='idata', *args): + """Build a FIT from BASE_ITS, returning (result, itb, kernel_data)""" + return build_fit_text(ubman, BASE_ITS, basename, *args, + alt_props=alt_props, extra_images=extra_images) + + [email protected]('sandbox') [email protected]('dtc') [email protected]('fdtget') +def test_fit_image_data_inline(ubman): + """Inline FIT: the data is copied and the reference is retained""" + result, itb, kdata = build_fit(ubman, 'image-data = "kernel-1";') + assert result.returncode == 0, result.stderr + + assert fdt_bytes(itb, '/images/kernel-1', 'data') == kdata + assert fdt_bytes(itb, '/images/kernel-alt', 'data') == kdata + + # The reference and the node's own properties are retained + assert fdtget(itb, '/images/kernel-alt', 'image-data', 's') == 'kernel-1' + assert fdtget(itb, '/images/kernel-alt', 'load', 'x') == '80000' + + # Each node hashes the same bytes + hash1 = fdtget(itb, '/images/kernel-1/hash-1', 'value', 'bx') + hash2 = fdtget(itb, '/images/kernel-alt/hash-1', 'value', 'bx') + assert hash1 and hash1 == hash2 + + [email protected]('sandbox') [email protected]('dtc') [email protected]('fdtget') +def test_fit_image_data_external(ubman): + """External FIT (-E): both nodes reference a single copy of the data""" + result, itb, kdata = build_fit(ubman, 'image-data = "kernel-1";', + '', 'idata-ext', '-E') + assert result.returncode == 0, result.stderr + + assert fdtget(itb, '/images/kernel-1', 'data-offset') == \ + fdtget(itb, '/images/kernel-alt', 'data-offset') + assert fdtget(itb, '/images/kernel-1', 'data-size') == \ + fdtget(itb, '/images/kernel-alt', 'data-size') == str(len(kdata)) + assert fdtget(itb, '/images/kernel-alt', 'data') is None + + # The payload appears exactly once, at the shared offset + with open(itb, 'rb') as inf: + blob = inf.read() + assert blob.count(kdata) == 1 + totalsize = struct.unpack('>I', blob[4:8])[0] + offset = int(fdtget(itb, '/images/kernel-1', 'data-offset')) + assert blob[totalsize + offset:totalsize + offset + len(kdata)] == kdata + + [email protected]('sandbox') [email protected]('dtc') [email protected]('fdtget') +def test_fit_image_data_reprocess(ubman): + """A resolved FIT can be re-processed with -F, including to -E""" + result, itb, kdata = build_fit(ubman, 'image-data = "kernel-1";', + '', 'idata-re') + assert result.returncode == 0, result.stderr + + result = mkimage(ubman, '-F', itb) + assert result.returncode == 0, result.stderr + + result = mkimage(ubman, '-F', '-E', itb) + assert result.returncode == 0, result.stderr + assert fdtget(itb, '/images/kernel-1', 'data-offset') == \ + fdtget(itb, '/images/kernel-alt', 'data-offset') + with open(itb, 'rb') as inf: + assert inf.read().count(kdata) == 1 + + [email protected]('sandbox') [email protected]('dtc') +def test_fit_image_data_invalid(ubman): + """Invalid image-data references are rejected""" + # Reference to an undefined image + result, itb, kdata = build_fit(ubman, 'image-data = "nope";', + '', 'idata-undef') + assert result.returncode != 0 + assert "references undefined image 'nope'" in result.stderr + + # Chained reference + result, itb, kdata = build_fit(ubman, 'image-data = "kernel-mid";', + CHAIN_IMAGE, 'idata-chain') + assert result.returncode != 0 + assert 'chained references are not permitted' in result.stderr + + # Own data disagreeing with the target's data + other = fit_util.make_kernel(ubman, 'idata-other.bin', 'other') + props = f'image-data = "kernel-1";\ndata = /incbin/("{other}");' + result, itb, kdata = build_fit(ubman, props, '', 'idata-mismatch') + assert result.returncode != 0 + assert "data does not match image-data target 'kernel-1'" in result.stderr + + # image-data combined with external data properties + result, itb, kdata = build_fit( + ubman, 'image-data = "kernel-1";\ndata-size = <0x10>;', + '', 'idata-extprop') + assert result.returncode != 0 + assert 'must not be combined with external data properties' in \ + result.stderr + + [email protected]('sandbox') [email protected]('dtc') +def test_fit_image_data_cipher_invalid(ubman): + """Sharing encrypted data without compatible cipher settings fails""" + # Only the sharing node is encrypted + its_text = BASE_ITS.replace('entry = <0x80000>;', + 'entry = <0x80000>;' + CIPHER) + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-oneside') + assert result.returncode != 0 + assert 'only one of them is encrypted' in result.stderr + + # Both encrypted but without any IV source, so each would get a + # random IV and the ciphertexts would differ + its_text = BASE_ITS.replace('hash-1 {', CIPHER_NO_IV + 'hash-1 {') + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-noiv') + assert result.returncode != 0 + assert 'must use the same iv-name-hint or iv' in result.stderr + + # Different explicit IVs would produce different ciphertexts + its_text = BASE_ITS.replace('entry = <0x40000>;', + 'entry = <0x40000>;' + CIPHER_IV) + its_text = its_text.replace('entry = <0x80000>;', + 'entry = <0x80000>;' + CIPHER_IV_ALT) + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-diff-iv') + assert result.returncode != 0 + assert 'must use the same iv-name-hint or iv' in result.stderr + + # Different iv-name-hints as well, since a hint drives the + # encryption whenever it is present + its_text = BASE_ITS.replace('entry = <0x40000>;', + 'entry = <0x40000>;' + CIPHER) + its_text = its_text.replace('entry = <0x80000>;', + 'entry = <0x80000>;' + + CIPHER.replace('kiv', 'kiv2')) + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-diff-hint') + assert result.returncode != 0 + assert 'must use the same iv-name-hint' in result.stderr + + [email protected]('sandbox') [email protected]('dtc') [email protected]('fdtget') +def test_fit_image_data_cipher(ubman): + """Nodes with identical cipher settings share the same ciphertext""" + keydir = fit_util.make_fname(ubman, 'idata-keys') + os.makedirs(keydir, exist_ok=True) + with open(os.path.join(keydir, 'kern.bin'), 'wb') as outf: + outf.write(os.urandom(32)) + with open(os.path.join(keydir, 'kiv.bin'), 'wb') as outf: + outf.write(os.urandom(16)) + + its_text = BASE_ITS.replace('hash-1 {', CIPHER + 'hash-1 {') + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-cipher', + '-k', keydir) + assert result.returncode == 0, result.stderr + + ciphertext = fdt_bytes(itb, '/images/kernel-1', 'data') + assert ciphertext == fdt_bytes(itb, '/images/kernel-alt', 'data') + assert ciphertext != kdata + assert fdtget(itb, '/images/kernel-1', 'data-size-unciphered') == \ + fdtget(itb, '/images/kernel-alt', 'data-size-unciphered') == \ + str(len(kdata)) + + # The shared ciphertext can also be made external + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-cipher-ext', + '-k', keydir, '-E') + assert result.returncode == 0, result.stderr + assert fdtget(itb, '/images/kernel-1', 'data-offset') == \ + fdtget(itb, '/images/kernel-alt', 'data-offset') + + # A shared explicit iv works as well: it is honored by the + # encryption, retained in the FIT and yields identical ciphertext + its_text = BASE_ITS.replace('hash-1 {', CIPHER_IV + 'hash-1 {') + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-cipher-iv', + '-k', keydir) + assert result.returncode == 0, result.stderr + ciphertext = fdt_bytes(itb, '/images/kernel-1', 'data') + assert ciphertext == fdt_bytes(itb, '/images/kernel-alt', 'data') + assert ciphertext != kdata + authored_iv = bytes.fromhex('112233445566778899aabbccddeeff00') + assert fdt_bytes(itb, '/images/kernel-1/cipher', 'iv') == authored_iv + assert fdt_bytes(itb, '/images/kernel-alt/cipher', 'iv') == authored_iv + + result, itb, kdata = build_fit_text(ubman, its_text, + 'idata-cipher-iv-ext', '-k', keydir, + '-E') + assert result.returncode == 0, result.stderr + assert fdtget(itb, '/images/kernel-1', 'data-offset') == \ + fdtget(itb, '/images/kernel-alt', 'data-offset') + + [email protected]('sandbox') [email protected]('dtc') +def test_fit_image_data_overlap(ubman): + """Sharers' load regions are covered by the configuration overlap check""" + its_text = BASE_ITS.replace( + 'kernel = "kernel-alt";', + 'kernel = "kernel-alt";\n loadables = "filler";') + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-overlap', + extra_images=OVERLAP_SHARER) + assert result.returncode != 0 + assert 'overlapping load regions' in result.stderr
Signature · 1 line
2.43.0
Re: [PATCH v1 5/5] test: py: add tests for the FIT image-data property
Quoted 20 lines · Aristo Chen
test: py: add tests for the FIT image-data property
Cover mkimage's resolution of image-data references between images. The positive tests check that an inline FIT carries identical data and hash values on the sharer and the target while their own properties remain independent, that external data mode points both nodes at a single copy of the payload, that a resolved FIT can be re-processed with -F including conversion to external data, and that two nodes with identical cipher settings share the same ciphertext both inline and external, whether the IV comes from an iv-name-hint or from an explicit iv that is honored and retained in the FIT.
The negative tests check that mkimage rejects references to undefined images, chained references, data that disagrees with the target, image-data combined with external data properties, a configuration whose image-data sharers have overlapping load regions, sharing where only one node is encrypted, and encrypted sharing where the nodes disagree on the IV, through different iv-name-hints, different explicit ivs or no shared IV source at all.
Quoted 4 lines
Signed-off-by Aristo Chen <[email protected]>
test/py/tests/test_fit_image_data.py| 373 +++++++++++++++++++++++++++++++++++1 file changed, 373 insertions(+)
Hi Aristo,
Quoted 2 lines · Aristo Chen
The 'Shared image data' section of the Flat Image Tree specification [1] defines an image-data property [...]
Thanks for the thorough cover letter and the hardware numbers. Also for taking this on while I am on a patch holiday :-)
Please add user-facing documentation under doc/usage/fit/ describing image-data. A short section (either in multi.rst, since this lands most naturally in multi-configuration FITs, or a new file) covering what the property means, the resolved form mkimage produces, the encrypted-sharing rules and a pointer to the external spec. Right now someone trying to shrink a multi-board FIT has no way to discover this from the U-Boot docs.
Also we have example files and this feels like a case where a new example would help - see doc/usage/fit/
Just to check - patch 4 is closing a validation gap opened by patch 2 rather than fixing a pre-existing bug, since data-less images did not exist before image-data was introduced. Would folding it into patch 2 make things easier to follow? Either way is fine.
Regards,
Simon
Hi Simon,
Quoted 19 lines · Simon Glass
Hi Aristo,
Quoted 2 lines · Aristo Chen
The 'Shared image data' section of the Flat Image Tree specification [1] defines an image-data property [...]
Thanks for the thorough cover letter and the hardware numbers. Also for taking this on while I am on a patch holiday :-)
Please add user-facing documentation under doc/usage/fit/ describing image-data. A short section (either in
multi.rst, since this lands most naturally in multi-configuration FITs, or a new file) covering what the property means, the resolved form mkimage produces, the encrypted-sharing rules and a pointer to the external spec. Right now someone trying to shrink a multi-board FIT has no way to discover this from the U-Boot docs.
Quoted 2 lines
Also we have example files and this feels like a case where a new example would help - see doc/usage/fit/
I will fix this in v2
Quoted 5 lines
Just to check - patch 4 is closing a validation gap opened by patch 2 rather than fixing a pre-existing bug, since data-less images did not exist before image-data was introduced. Would folding it into patch 2 make things easier to follow? Either way is fine.
That is the right reading. I kept it separate mainly for review size:
the resolve patch is already the largest change in the series, at 360 added lines, while this is a 45-line edit to a different function, the region collection in fit_import_data(), with its own reasoning. A small follow-on patch seemed easier to follow than growing the big one further. In v2, I will update the commit message to explicitly state that it completes the validation for the new property rather than fixing a pre-existing problem. If you would still rather see it folded into the resolve patch for v2, that is easy to do.
Thanks for the review and for the patch-holiday exception! I will also fix other concerns you pointed out for patch 2.
Quoted 3 lines
Regards,
Simon
Best regards,
Aristo
v2 cover tools: mkimage: support shared image data through the image-data property
In reply to Aristo ChenLink to v1:
https://lore.kernel.org/u-boot/[email protected]/
Changes in v2 (from Simon's review of v1, plus one cleanup noticed while addressing it):
- new patch 1: drop the duplicate FIT_ALGO_PROP definition in the cipher node block of include/image.h, noticed while adding the cipher property defines requested in review.
- patch 2 (was 1): add FIT_IV_HINT and FIT_IV_PROP to include/image.h and use them in fit_image_setup_cipher(). Non-functional cleanup, so the Reviewed-by is kept.
- patch 3 (was 2): fit_resolve_image_data() skips its own pack and rewrite when re-processing a FIT that needs nothing resolved, while still validating every reference; reject image-data combined with a complete external data reference before the import when compiling a source file, where the combination cannot be legitimate, while re-processing keeps import-then-compare for the resolved form; complete the kernel-doc of the three new helpers, with the cipher check describing the source-file and re-processed cases separately; explain in the kernel-doc and at the remaining check why it runs after the import; add FIT_DATA_SIZE_UNCIPHERED_PROP and use the property defines.
- patch 5 (was 4): the commit message now states that it completes the validation for the new property rather than fixing a pre-existing problem, and the kernel-doc of fit_image_effective_data() gains its parameter and return descriptions. Comment-only, so the Reviewed-by is kept; still a separate patch to stay reviewable on its own.
- patch 6: new, user documentation with a worked example under doc/usage/fit/.
- patch 7 (was 5): two cases for the complete offset and position forms of the mixing check; the Reviewed-by is dropped because of the added cases.
- patch 4 (was 3): unchanged.
---
The 'Shared image data' section of the Flat Image Tree specification [1] defines an image-data property: an image node names another image node under /images whose binary payload it shares, while carrying its own load, entry and remaining metadata. A multi-configuration FIT can then offer the same binary at different addresses without storing it twice. The normative text is in the specification's main branch; a refinement of the cipher interaction is under review in [2].
mkimage resolves the references right after external data has been
imported: the target's data is copied into the referencing node, so the later ciphering, hashing, signing and overlap checking steps see normal image nodes, and the property is retained in the resolved FIT as a record of the sharing relationship. Invalid references are rejected:
an undefined target, a chained reference, data that disagrees with the target, external data properties mixed with image-data in a source file, and cipher settings that would produce different ciphertexts. Consumers need no changes, since bootloaders ignore the property and the data properties remain authoritative.
Patch 1 drops a duplicate definition of FIT_ALGO_PROP in the cipher node block of include/image.h, a leftover from the cipher support that compiles silently because the redefinition is identical.
Patch 2 makes fit_image_setup_cipher() use an explicit iv from the cipher node when no iv-name-hint is given, checking its length, and fall back to a random IV only when neither is present. This matches the specification, makes encryption reproducible, and lets encrypted data be shared without relying on an IV file in the key directory.
Patch 3 adds the resolution and validation described above, covering inline FITs completely; when re-processing a FIT that needs nothing resolved, the resolution step leaves the file to the later stages without a rewrite of its own.
Patch 4 makes external data mode (-E and -p) store the shared payload only once: sharers skip the extraction and are pointed at the referenced image's region through identical data-offset or data-position and data-size values.
Patch 5 completes the validation for the new property by teaching the configuration load overlap check to size data-less sharers from their target. The check runs before the references are resolved, so such images would otherwise escape it.
Patch 6 adds user documentation: a worked example page under doc/usage/fit/ showing a kernel offered at two load addresses by two configurations while the payload is stored once, followed by the rules the producer enforces, the resolved form, the interaction with hashing, signing and encryption, and a pointer to the specification.
Patch 7 adds the sandbox tests: inline and external sharing, mkimage -F re-processing including conversion between the two forms, the rejection cases, encrypted sharing through a shared iv-name-hint and through a shared explicit iv, and the overlap rejection.
Each patch builds and works on its own. On sandbox the new tests pass together with the FIT, vboot, mkimage and cipher pytest selection and the complete binman suite. The feature is also verified on hardware: an i.MX6ULL EVK boots an Ubuntu Core style multi-board FIT whose i.MX6ULL variants share the kernel, ramdisk and FDT payloads through image-data, keeping the image at roughly 40 MB instead of the 78 MB it would take with the payloads duplicated.
[1] https://github.com/open-source-firmware/flat-image-tree
[2] https://github.com/open-source-firmware/flat-image-tree/pull/58
Aristo Chen (7):
include: image: drop the duplicate FIT_ALGO_PROP definition
tools: mkimage: use an explicit iv from the cipher node when
encrypting
tools: mkimage: resolve image-data references between FIT images
tools: mkimage: share the external-data region between image-data
images
tools: mkimage: include image-data sharers in the load overlap check
doc: fit: document shared image data through the image-data property
test: py: add tests for the FIT image-data property
doc/usage/fit/index.rst | 1 + doc/usage/fit/multi-with-shared-data.rst | 114 ++++++ include/image.h | 5 +- test/py/tests/test_fit_image_data.py | 391 ++++++++++++++++++++ tools/fit_image.c | 449 ++++++++++++++++++++++- tools/image-host.c | 20 +- 6 files changed, 973 insertions(+), 7 deletions(-) create mode 100644 doc/usage/fit/multi-with-shared-data.rst create mode 100644 test/py/tests/test_fit_image_data.py
Signature · 1 line
2.43.0
PATCH v2 1/7 include: image: drop the duplicate FIT_ALGO_PROP definition
FIT_ALGO_PROP is defined once with the hash and signature node properties and a second time, with the same value, in the cipher node block added by commit 7298e422504e ("mkimage: fit: add support to encrypt image with aes"). The identical redefinition compiles silently, which is how it went unnoticed, but it suggests a separate cipher property where there is none: the 'algo' property name is shared by hash, signature and cipher nodes. Keep the first definition only.
---
Changes in v2:
v2: new patch
include/image.h | 1 - 1 file changed, 1 deletion(-)
include/image.h +0 -1
@@ -1158,7 +1158,6 @@ int booti_setup(ulong image, ulong *relocated_addr, ulong *size,/* cipher node */ #define FIT_CIPHER_NODENAME "cipher" -#define FIT_ALGO_PROP "algo"/* dm-verity node */ #define FIT_VERITY_NODENAME "dm-verity"
Signature · 1 line
2.43.0
Re: [PATCH v2 1/7] include: image: drop the duplicate FIT_ALGO_PROP definition
Quoted 10 lines · Aristo Chen
include: image: drop the duplicate
FIT_ALGO_PROPdefinition
FIT_ALGO_PROPis defined once with the hash and signature node properties and a second time, with the same value, in the cipher node block added by commit7298e422504e("mkimage: fit: add support to encrypt image with aes"). The identical redefinition compiles silently, which is how it went unnoticed, but it suggests a separate cipher property where there is none: the 'algo' property name is shared by hash, signature and cipher nodes. Keep the first definition only.
Quoted 4 lines
Signed-off-by Aristo Chen <[email protected]>
include/image.h| 1 -1 file changed, 1 deletion(-)
PATCH v2 2/7 tools: mkimage: use an explicit iv from the cipher node when encrypting
In reply to Aristo Chenfit_image_setup_cipher() only consumes iv-name-hint: without the hint a fresh random IV is generated for every image, and any iv property the cipher node already carries is ignored and later overwritten. The FIT specification defines iv as the raw initialisation vector carried in the FIT and requires at least one of iv-name-hint or iv to be present, so an .its author who supplies an explicit iv reasonably expects it to be used.
Use the iv property when no iv-name-hint is given, after checking that its length matches the cipher's IV length, and only fall back to a random IV when the node provides neither. Besides matching the specification this makes encryption reproducible, since the same source, key and IV now produce the same ciphertext on every run. It also allows images that share data through the image-data property, added later in this series, to be encrypted consistently without relying on an IV file in the key directory.
---
Changes in v2:
v2: use the new FIT_IV_HINT and FIT_IV_PROP defines in fit_image_setup_cipher(); non-functional cleanup requested in review, Reviewed-by kept
include/image.h | 2 ++ tools/image-host.c | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-)
include/image.h +2 -0
@@ -1158,6 +1158,8 @@ int booti_setup(ulong image, ulong *relocated_addr, ulong *size,/* cipher node */ #define FIT_CIPHER_NODENAME "cipher" +#define FIT_IV_HINT "iv-name-hint" +#define FIT_IV_PROP "iv"/* dm-verity node */ #define FIT_VERITY_NODENAME "dm-verity"
tools/image-host.c +17 -3
@@ -421,7 +421,9 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, const char *image_name, int image_noffset, int noffset) { + const void *iv;char *algo_name; +int ivlen;int ret =-1 ;if (fit_image_cipher_get_algo (fit, noffset, &algo_name)) {
@@ -443,10 +445,11 @@ static int fit_image_setup_cipher(struct image_cipher_info *info,/* * Read the IV name * -* If this property is not provided then mkimage will generate -* a random IV and store it in the FIT image +* If this property is not provided then mkimage will use the iv +* property of the cipher node, if present, or generate a random +* IV and store it in the FIT image */ - info->ivname =fdt_getprop (fit, noffset,"iv-name-hint" ,NULL ); + info->ivname =fdt_getprop (fit, noffset,FIT_IV_HINT ,NULL ); info->fit = fit; info->node_noffset = noffset;
@@ -479,6 +482,7 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, goto out; } + iv =fdt_getprop (fit, noffset,FIT_IV_PROP , &ivlen);if (info->ivname) {/* Read the IV in the file */ ret = fit_image_read_key_iv_data(info->keydir, info->ivname,
@@ -486,6 +490,16 @@ static int fit_image_setup_cipher(struct image_cipher_info *info, info->cipher->iv_len);if (ret <0 ) goto out; + }else if (iv) { +/* Use the explicit IV from the cipher node */ +if (ivlen != info->cipher->iv_len) { +fprintf (stderr, +"Wrong iv length for cipher in image '%s' (expected %d bytes, got %d)\n" , + image_name, info->cipher->iv_len, ivlen); + ret =-1 ; + goto out; + } +memcpy ((void *)info->iv, iv, ivlen); }else {/* Generate an ramdom IV */ ret =get_random_data ((void *)info->iv, info->cipher->iv_len);
Signature · 1 line
2.43.0
PATCH v2 3/7 tools: mkimage: resolve image-data references between FIT images
In reply to Aristo ChenAn image node may carry an 'image-data' property naming another image node under /images whose binary data it shares. This lets several configurations load the same payload at different addresses without storing it twice, as described in the 'Shared image data' section of the Flat Image Tree specification [1].
Resolve the references in mkimage after any external data has been
imported: the target's data is copied into the referencing node, so the later ciphering, hashing and signing steps see a normal image node. The property itself is retained in the resolved FIT as a record of the sharing relationship. When mkimage re-processes a FIT that already contains resolved references, the node's data must match the target's data exactly, and fit_resolve_image_data() skips its own pack and rewrite of the file when no payload needed copying.
A reference is rejected when the target does not exist, when the target itself carries an image-data property (chained references), when the node's existing data disagrees with the target, or when the node mixes image-data with external data properties. The last case is caught before the import when compiling a source file, where the combination cannot be legitimate; a re-processed FIT carries both on purpose, since that is the resolved form with external data. Since encryption happens per node, sharing encrypted data is only accepted when both nodes use the same algorithm and key together with the same iv-name-hint or, when neither uses a hint, the same explicit iv; anything else, including the independent random IV generated when a node provides no IV at all, would produce different ciphertexts.
[1] https://github.com/open-source-firmware/flat-image-tree
---
Changes in v2:
v2: fit_resolve_image_data() skips its own pack and rewrite when nothing needed resolving; reject image-data combined with a complete external data reference before the import when compiling a source file; complete the kernel-doc of the three new helpers; document why the remaining check runs after the import; add FIT_DATA_SIZE_UNCIPHERED_PROP and use the property defines
include/image.h | 2 + tools/fit_image.c | 358 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 360 insertions(+)
include/image.h +2 -0
@@ -1160,6 +1160,7 @@ int booti_setup(ulong image, ulong *relocated_addr, ulong *size,#define FIT_CIPHER_NODENAME "cipher"#define FIT_IV_HINT "iv-name-hint"#define FIT_IV_PROP "iv" +#define FIT_DATA_SIZE_UNCIPHERED_PROP "data-size-unciphered"/* dm-verity node */ #define FIT_VERITY_NODENAME "dm-verity"
@@ -1183,6 +1184,7 @@ int booti_setup(ulong image, ulong *relocated_addr, ulong *size,#define FIT_DATA_POSITION_PROP "data-position"#define FIT_DATA_OFFSET_PROP "data-offset"#define FIT_DATA_SIZE_PROP "data-size" +#define FIT_IMAGE_DATA_PROP "image-data"#define FIT_TIMESTAMP_PROP "timestamp"#define FIT_DESC_PROP "description"#define FIT_ARCH_PROP "arch"
tools/fit_image.c +358 -0
376 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 3/7] tools: mkimage: resolve image-data references between FIT images
Quoted 30 lines · Aristo Chen
tools: mkimage: resolve image-data references between FIT images
An image node may carry an 'image-data' property naming another image node under /images whose binary data it shares. This lets several configurations load the same payload at different addresses without storing it twice, as described in the 'Shared image data' section of the Flat Image Tree specification [1].
Resolve the references in mkimage after any external data has been
imported: the target's data is copied into the referencing node, so the later ciphering, hashing and signing steps see a normal image node. The property itself is retained in the resolved FIT as a record of the sharing relationship. When mkimage re-processes a FIT that already contains resolved references, the node's data must match the target's data exactly, andfit_resolve_image_data()skips its own pack and rewrite of the file when no payload needed copying.A reference is rejected when the target does not exist, when the target itself carries an image-data property (chained references), when the node's existing data disagrees with the target, or when the node mixes image-data with external data properties. The last case is caught before the import when compiling a source file, where the combination cannot be legitimate; a re-processed FIT carries both on purpose, since that is the resolved form with external data. Since encryption happens per node, sharing encrypted data is only accepted when both nodes use the same algorithm and key together with the same iv-name-hint or, when neither uses a hint, the same explicit iv; anything else, including the independent random IV generated when a node provides no IV at all, would produce different ciphertexts.
Quoted 7 lines
[1] https://github.com/open-source-firmware/flat-image-tree
Signed-off-by Aristo Chen <[email protected]>
include/image.h| 2 +tools/fit_image.c | 358 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 360 insertions(+)
PATCH v2 4/7 tools: mkimage: share the external-data region between image-data images
In reply to Aristo ChenWhen building with external data (-E or -p), an image sharing another image's data through image-data still receives its own copy of the payload in the external area. Skip such images while the data is moved out of the FIT and point them at the referenced image's region afterwards, through identical data-offset or data-position and data-size values, so the payload is stored only once. This is the resolved form the specification describes for external data: both nodes reference the same region.
An image whose reference did not resolve cannot reach this point, since fit_resolve_image_data() has already rejected the FIT.
---
Changes in v2:
v2: no changes
tools/fit_image.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+)
tools/fit_image.c +46 -0
@@ -813,6 +813,14 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname) data =fdt_getprop (fdt, node,FIT_DATA_PROP , &len);if (!data)continue ; +if (fdt_getprop (fdt, node,FIT_IMAGE_DATA_PROP ,NULL )) { +/* +* This node shares another image's data, which gets +* its own offset below; don't store a second copy +*/ + len =0 ; +continue ; + } ret =fit_copy_image_data (fdt, node, buf, buf_ptr, data, &len);if (ret)
@@ -849,6 +857,44 @@ static int fit_extract_data(struct image_tool_params *params, const char *fname)/* Increment 'buf_ptr' for the trailing image. */ buf_ptr +=ALIGN (len, align_size); +/* +* Images sharing another image's data (image-data) reference the +* same region instead of storing a second copy +*/ +fdt_for_each_subnode (node, fdt, images) { +const char *prop, *ref; +int target, offset, size; + + ref =fdt_getprop (fdt, node,FIT_IMAGE_DATA_PROP ,NULL ); +if (!ref || !fdt_getprop (fdt, node,FIT_DATA_PROP ,NULL )) +continue ; + + prop = params->external_offset >0 ?FIT_DATA_POSITION_PROP : +FIT_DATA_OFFSET_PROP ; + target =fdt_subnode_offset (fdt, images, ref); + offset = target >=0 ?fdtdec_get_int (fdt, target, prop,-1 ) :-1 ; + size = target >=0 ? +fdtdec_get_int (fdt, target,FIT_DATA_SIZE_PROP ,-1 ) :-1 ; +if (offset ==-1 || size ==-1 ) { +fprintf (stderr, +"Error: image '%s': no extracted data to share from image '%s'\n" , +fdt_get_name (fdt, node,NULL ), ref); + ret = -EINVAL ; + goto err_munmap; + } + + ret =fdt_delprop (fdt, node,FIT_DATA_PROP ); +if (!ret) + ret =fdt_setprop_u32 (fdt, node, prop, offset); +if (!ret) + ret =fdt_setprop_u32 (fdt, node,FIT_DATA_SIZE_PROP , + size); +if (ret) { + ret = -EINVAL ; + goto err_munmap; + } + } +/* Pack the FDT and place the data after it */ fdt_pack (fdt);
Signature · 1 line
2.43.0
PATCH v2 5/7 tools: mkimage: include image-data sharers in the load overlap check
In reply to Aristo ChenThe overlap check at the end of fit_import_data() sizes each referenced image from its data property and skips images without data, since such images are never copied anywhere. The previous patch introduces the first images for which that assumption does not hold: an image sharing another image's data has no data of its own until the reference is resolved after the import, so it escaped the check and its load region went unvalidated even though the resolved image is loaded like any other. This completes the validation for the new property rather than fixing a pre-existing problem, since a configuration could not reference a data-less image in a meaningful way before.
Size such images from their target's data instead. References that do not resolve are left alone here; fit_resolve_image_data() rejects them right after the import.
---
Changes in v2:
v2: commit message states that this completes the validation for the new property; complete the kernel-doc of fit_image_effective_data(); comment-only change, Reviewed-by kept
tools/fit_image.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-)
tools/fit_image.c +42 -3
@@ -951,6 +951,41 @@ err:return ret; } +/** +* fit_image_effective_data() - Get the data an image will carry once resolved +* +* Returns the image's own data or, for an image that shares another +* image's data through a not yet resolved image-data reference, the +* target's data. References that do not resolve are left alone here, +* since fit_resolve_image_data() rejects them right after the import. +* +* @fdt: FIT blob containing the image nodes +* @images: offset of the /images node +* @img: offset of the image node to look at +* @sizep: returns the length of the data on success +* Return: pointer to the data, or NULL if neither the image nor its +* image-data target carries any +*/ +static const char *fit_image_effective_data (const void *fdt,int images, +int img,int *sizep) +{ +const char *data, *ref; +int target; + + data =fdt_getprop (fdt, img,FIT_DATA_PROP , sizep); +if (data) +return data; + + ref =fdt_getprop (fdt, img,FIT_IMAGE_DATA_PROP ,NULL ); +if (!ref) +return NULL ; + target =fdt_subnode_offset (fdt, images, ref); +if (target <0 ) +return NULL ; + +return fdt_getprop (fdt, target,FIT_DATA_PROP , sizep); +} +static int fit_import_data (struct image_tool_params *params,const char *fname) {void *fdt, *old_fdt;
@@ -1118,12 +1153,16 @@ static int fit_import_data(struct image_tool_params *params, const char *fname)* Collect the memory region the image is * loaded to. Images without a load address or * without data are never copied anywhere, so -* they cannot conflict. +* they cannot conflict. An image-data +* reference is resolved only after the +* import, but the image ends up with the +* target's data, so size its region from the +* target. */ if (fit_image_get_load (fdt, img, &img_load))continue ; - img_data =fdt_getprop (fdt, img,FIT_DATA_PROP , - &img_size); + img_data =fit_image_effective_data (fdt, + images, img, &img_size);if (!img_data || img_size <=0 )continue ;
Signature · 1 line
2.43.0
PATCH v2 6/7 doc: fit: document shared image data through the image-data property
In reply to Aristo ChenSomeone trying to shrink a multi-board FIT has no way to discover the image-data property from the U-Boot documentation, since the format reference lives in the external specification. Add a worked example in the style of the other FIT example pages: a kernel offered at two load addresses by two configurations while the payload is stored once, together with the rules the producer enforces, the resolved form, the interaction with hashing, signing and encryption, and a pointer to the specification.
---
Changes in v2:
v2: new patch
doc/usage/fit/index.rst | 1 + doc/usage/fit/multi-with-shared-data.rst | 114 +++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 doc/usage/fit/multi-with-shared-data.rst
doc/usage/fit/index.rst +1 -0
@@ -21,6 +21,7 @@ images that it reads and boots. Documentation about FIT is available in multi_spl multi-with-fpga multi-with-loadables + multi-with-shared-data overlay-fdt-boot sec_firmware_ppa signature
doc/usage/fit/multi-with-shared-data.rst new file +114 -0
114 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v2 6/7] doc: fit: document shared image data through the image-data property
Hi Aristo,
Quoted 21 lines · Aristo Chen
doc: fit: document shared image data through the image-data property
Someone trying to shrink a multi-board FIT has no way to discover the image-data property from the U-Boot documentation, since the format reference lives in the external specification. Add a worked example in the style of the other FIT example pages: a kernel offered at two load addresses by two configurations while the payload is stored once, together with the rules the producer enforces, the resolved form, the interaction with hashing, signing and encryption, and a pointer to the specification.
Suggested-by Simon Glass <[email protected]>Signed-off-by Aristo Chen <[email protected]>
doc/usage/fit/index.rst| 1 +doc/usage/fit/multi-with-shared-data.rst | 114 +++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+)doc/usage/fit/multi-with-shared-data.rst new file +3
-0+ kernel-1 { + description = "Linux kernel"; + data = /incbin/("./Image");
...
doc/usage/fit/multi-with-shared-data.rst line 39
Quoted 3 lines
doc/usage/fit/multi-with-shared-data.rst new file +3
-0+ kernel-board-b { + description = "Linux kernel at the board B address"; + image-data = "kernel-1";
Naming is asymmetric: the target is kernel-1 but the sharer is kernel-board-b. Renaming the target to kernel-board-a would make the pairing with conf-board-a / conf-board-b obvious, and match the fdt-board-a / fdt-board-b naming just below. Minor, feel free to ignore.
doc/usage/fit/multi-with-shared-data.rst line 100
Quoted 7 lines
doc/usage/fit/multi-with-shared-data.rst new file +7
-0+mkimage resolves the reference while building the FIT. With embedded +data the payload is copied into both nodes, so sharing pays off +together with external data (-E or -p), where both nodes point at a +single copy of the payload through identical data-offset or +data-position and data-size values. The 'image-data' property is +retained in the resolved FIT as a record of the relationship, and +re-processing such a FIT with 'mkimage -F' keeps it intact. Each node
The 'pays off together with external data' phrasing is hard to parse.
Perhaps: "With embedded data the payload is copied into both nodes in the resolved FIT, so the size saving only appears with external data (-E or -p), where both nodes point at a single copy through identical data-offset (or data-position) and data-size values." What do you think?
Regards,
Simon
PATCH v2 7/7 test: py: add tests for the FIT image-data property
In reply to Aristo ChenCover mkimage's resolution of image-data references between images. The positive tests check that an inline FIT carries identical data and hash values on the sharer and the target while their own properties remain independent, that external data mode points both nodes at a single copy of the payload, that a resolved FIT can be re-processed with -F including conversion to external data, and that two nodes with identical cipher settings share the same ciphertext both inline and external, whether the IV comes from an iv-name-hint or from an explicit iv that is honored and retained in the FIT.
The negative tests check that mkimage rejects references to undefined images, chained references, data that disagrees with the target, image-data combined with external data properties in partial and in complete form, a configuration whose image-data sharers have overlapping load regions, sharing where only one node is encrypted, and encrypted sharing where the nodes disagree on the IV, through different iv-name-hints, different explicit ivs or no shared IV source at all.
---
Changes in v2:
v2: add cases for the complete offset and position forms of the mixing check; Reviewed-by dropped because of the added cases
test/py/tests/test_fit_image_data.py | 391 +++++++++++++++++++++++++++ 1 file changed, 391 insertions(+) create mode 100644 test/py/tests/test_fit_image_data.py
test/py/tests/test_fit_image_data.py new file +391 -0
@@ -0,0 +1,391 @@ +# SPDX-License-Identifier: GPL-2.0+ +# Copyright 2026 Canonical Ltd. +# +# Test mkimage handling of the 'image-data' FIT property + +"""Tests for the FIT 'image-data' property. + +An image node may name another image node under /images in an +'image-data' property to share its binary data. mkimage resolves the +reference by copying the data when building an inline FIT, and by +pointing both nodes at the same region when building with external data +(-E). Invalid references (undefined target, chained references, +mismatching data or incompatible cipher settings) must be rejected. +""" + +import os +import struct +import subprocess + +import pytest + +import fit_util + +BASE_ITS = ''' +/dts-v1/; + +/ { + description = "image-data test"; + + images { + kernel-1 { + description = "test kernel"; + data = /incbin/("%(kernel)s"); + type = "kernel"; + arch = "sandbox"; + os = "linux"; + compression = "none"; + load = <0x40000>; + entry = <0x40000>; + hash-1 { + algo = "sha256"; + }; + }; + kernel-alt { + description = "same kernel at another address"; + %(alt_props)s + type = "kernel"; + arch = "sandbox"; + os = "linux"; + compression = "none"; + load = <0x80000>; + entry = <0x80000>; + hash-1 { + algo = "sha256"; + }; + }; + %(extra_images)s + }; + + configurations { + default = "conf-1"; + conf-1 { + kernel = "kernel-1"; + }; + conf-2 { + kernel = "kernel-alt"; + }; + }; +}; +''' + +CHAIN_IMAGE = ''' + kernel-mid { + description = "middle image in a chain"; + image-data = "kernel-1"; + type = "kernel"; + arch = "sandbox"; + os = "linux"; + compression = "none"; + load = <0x60000>; + entry = <0x60000>; + }; +''' + +CIPHER = ''' + cipher { + algo = "aes256"; + key-name-hint = "kern"; + iv-name-hint = "kiv"; + }; +''' + +CIPHER_NO_IV = ''' + cipher { + algo = "aes256"; + key-name-hint = "kern"; + }; +''' + +CIPHER_IV = ''' + cipher { + algo = "aes256"; + key-name-hint = "kern"; + iv = [11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 00]; + }; +''' + +CIPHER_IV_ALT = ''' + cipher { + algo = "aes256"; + key-name-hint = "kern"; + iv = [00 ff ee dd cc bb aa 99 88 77 66 55 44 33 22 11]; + }; +''' + +OVERLAP_SHARER = ''' + filler { + description = "second sharer overlapping kernel-alt"; + image-data = "kernel-1"; + type = "loadable"; + arch = "sandbox"; + os = "linux"; + compression = "none"; + load = <0x80400>; + }; +''' + + +def mkimage(ubman, *args): + """Run mkimage with the given arguments, returning the process result""" + tool = os.path.join(ubman.config.build_dir, 'tools/mkimage') + return subprocess.run([tool] + list(args), capture_output=True, text=True) + + +def fdtget(itb, node, prop, ftype=None): + """Read a property from the FIT, returning None if it is missing""" + cmd = ['fdtget'] + (['-t', ftype] if ftype else []) + [itb, node, prop] + result = subprocess.run(cmd, capture_output=True, text=True) + if result.returncode: + return None + return result.stdout.strip() + + +def fdt_bytes(itb, node, prop): + """Read a property from the FIT as bytes, returning None if missing""" + value = fdtget(itb, node, prop, 'bx') + if value is None: + return None + return bytes(int(tok, 16) for tok in value.split()) + + +def build_fit_text(ubman, its_text, basename, *args, alt_props=None, + extra_images=''): + """Build a FIT from a full .its template, returning (result, itb, kdata)""" + kernel = fit_util.make_kernel(ubman, f'{basename}-kernel.bin', 'kernel') + params = { + 'kernel': kernel, + 'alt_props': alt_props or 'image-data = "kernel-1";', + 'extra_images': extra_images, + } + its = fit_util.make_its(ubman, its_text, params, f'{basename}.its') + itb = fit_util.make_fname(ubman, f'{basename}.itb') + result = mkimage(ubman, *args, '-f', its, itb) + with open(kernel, 'rb') as inf: + kdata = inf.read() + return result, itb, kdata + + +def build_fit(ubman, alt_props, extra_images='', basename='idata', *args): + """Build a FIT from BASE_ITS, returning (result, itb, kernel_data)""" + return build_fit_text(ubman, BASE_ITS, basename, *args, + alt_props=alt_props, extra_images=extra_images) + + [email protected]('sandbox') [email protected]('dtc') [email protected]('fdtget') +def test_fit_image_data_inline(ubman): + """Inline FIT: the data is copied and the reference is retained""" + result, itb, kdata = build_fit(ubman, 'image-data = "kernel-1";') + assert result.returncode == 0, result.stderr + + assert fdt_bytes(itb, '/images/kernel-1', 'data') == kdata + assert fdt_bytes(itb, '/images/kernel-alt', 'data') == kdata + + # The reference and the node's own properties are retained + assert fdtget(itb, '/images/kernel-alt', 'image-data', 's') == 'kernel-1' + assert fdtget(itb, '/images/kernel-alt', 'load', 'x') == '80000' + + # Each node hashes the same bytes + hash1 = fdtget(itb, '/images/kernel-1/hash-1', 'value', 'bx') + hash2 = fdtget(itb, '/images/kernel-alt/hash-1', 'value', 'bx') + assert hash1 and hash1 == hash2 + + [email protected]('sandbox') [email protected]('dtc') [email protected]('fdtget') +def test_fit_image_data_external(ubman): + """External FIT (-E): both nodes reference a single copy of the data""" + result, itb, kdata = build_fit(ubman, 'image-data = "kernel-1";', + '', 'idata-ext', '-E') + assert result.returncode == 0, result.stderr + + assert fdtget(itb, '/images/kernel-1', 'data-offset') == \ + fdtget(itb, '/images/kernel-alt', 'data-offset') + assert fdtget(itb, '/images/kernel-1', 'data-size') == \ + fdtget(itb, '/images/kernel-alt', 'data-size') == str(len(kdata)) + assert fdtget(itb, '/images/kernel-alt', 'data') is None + + # The payload appears exactly once, at the shared offset + with open(itb, 'rb') as inf: + blob = inf.read() + assert blob.count(kdata) == 1 + totalsize = struct.unpack('>I', blob[4:8])[0] + offset = int(fdtget(itb, '/images/kernel-1', 'data-offset')) + assert blob[totalsize + offset:totalsize + offset + len(kdata)] == kdata + + [email protected]('sandbox') [email protected]('dtc') [email protected]('fdtget') +def test_fit_image_data_reprocess(ubman): + """A resolved FIT can be re-processed with -F, including to -E""" + result, itb, kdata = build_fit(ubman, 'image-data = "kernel-1";', + '', 'idata-re') + assert result.returncode == 0, result.stderr + + result = mkimage(ubman, '-F', itb) + assert result.returncode == 0, result.stderr + + result = mkimage(ubman, '-F', '-E', itb) + assert result.returncode == 0, result.stderr + assert fdtget(itb, '/images/kernel-1', 'data-offset') == \ + fdtget(itb, '/images/kernel-alt', 'data-offset') + with open(itb, 'rb') as inf: + assert inf.read().count(kdata) == 1 + + [email protected]('sandbox') [email protected]('dtc') +def test_fit_image_data_invalid(ubman): + """Invalid image-data references are rejected""" + # Reference to an undefined image + result, itb, kdata = build_fit(ubman, 'image-data = "nope";', + '', 'idata-undef') + assert result.returncode != 0 + assert "references undefined image 'nope'" in result.stderr + + # Chained reference + result, itb, kdata = build_fit(ubman, 'image-data = "kernel-mid";', + CHAIN_IMAGE, 'idata-chain') + assert result.returncode != 0 + assert 'chained references are not permitted' in result.stderr + + # Own data disagreeing with the target's data + other = fit_util.make_kernel(ubman, 'idata-other.bin', 'other') + props = f'image-data = "kernel-1";\ndata = /incbin/("{other}");' + result, itb, kdata = build_fit(ubman, props, '', 'idata-mismatch') + assert result.returncode != 0 + assert "data does not match image-data target 'kernel-1'" in result.stderr + + # image-data combined with a partial external data reference + result, itb, kdata = build_fit( + ubman, 'image-data = "kernel-1";\ndata-size = <0x10>;', + '', 'idata-extprop') + assert result.returncode != 0 + assert 'must not be combined with external data properties' in \ + result.stderr + + # A complete reference is rejected before the import when compiling + # a source file, whether it uses an offset or a position + result, itb, kdata = build_fit( + ubman, + 'image-data = "kernel-1";\ndata-offset = <0>;\ndata-size = <0x10>;', + '', 'idata-extpair') + assert result.returncode != 0 + assert 'must not be combined with external data properties' in \ + result.stderr + result, itb, kdata = build_fit( + ubman, + 'image-data = "kernel-1";\ndata-position = <0x1000>;\n' + 'data-size = <0x10>;', + '', 'idata-extpos') + assert result.returncode != 0 + assert 'must not be combined with external data properties' in \ + result.stderr + + [email protected]('sandbox') [email protected]('dtc') +def test_fit_image_data_cipher_invalid(ubman): + """Sharing encrypted data without compatible cipher settings fails""" + # Only the sharing node is encrypted + its_text = BASE_ITS.replace('entry = <0x80000>;', + 'entry = <0x80000>;' + CIPHER) + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-oneside') + assert result.returncode != 0 + assert 'only one of them is encrypted' in result.stderr + + # Both encrypted but without any IV source, so each would get a + # random IV and the ciphertexts would differ + its_text = BASE_ITS.replace('hash-1 {', CIPHER_NO_IV + 'hash-1 {') + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-noiv') + assert result.returncode != 0 + assert 'must use the same iv-name-hint or iv' in result.stderr + + # Different explicit IVs would produce different ciphertexts + its_text = BASE_ITS.replace('entry = <0x40000>;', + 'entry = <0x40000>;' + CIPHER_IV) + its_text = its_text.replace('entry = <0x80000>;', + 'entry = <0x80000>;' + CIPHER_IV_ALT) + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-diff-iv') + assert result.returncode != 0 + assert 'must use the same iv-name-hint or iv' in result.stderr + + # Different iv-name-hints as well, since a hint drives the + # encryption whenever it is present + its_text = BASE_ITS.replace('entry = <0x40000>;', + 'entry = <0x40000>;' + CIPHER) + its_text = its_text.replace('entry = <0x80000>;', + 'entry = <0x80000>;' + + CIPHER.replace('kiv', 'kiv2')) + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-diff-hint') + assert result.returncode != 0 + assert 'must use the same iv-name-hint' in result.stderr + + [email protected]('sandbox') [email protected]('dtc') [email protected]('fdtget') +def test_fit_image_data_cipher(ubman): + """Nodes with identical cipher settings share the same ciphertext""" + keydir = fit_util.make_fname(ubman, 'idata-keys') + os.makedirs(keydir, exist_ok=True) + with open(os.path.join(keydir, 'kern.bin'), 'wb') as outf: + outf.write(os.urandom(32)) + with open(os.path.join(keydir, 'kiv.bin'), 'wb') as outf: + outf.write(os.urandom(16)) + + its_text = BASE_ITS.replace('hash-1 {', CIPHER + 'hash-1 {') + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-cipher', + '-k', keydir) + assert result.returncode == 0, result.stderr + + ciphertext = fdt_bytes(itb, '/images/kernel-1', 'data') + assert ciphertext == fdt_bytes(itb, '/images/kernel-alt', 'data') + assert ciphertext != kdata + assert fdtget(itb, '/images/kernel-1', 'data-size-unciphered') == \ + fdtget(itb, '/images/kernel-alt', 'data-size-unciphered') == \ + str(len(kdata)) + + # The shared ciphertext can also be made external + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-cipher-ext', + '-k', keydir, '-E') + assert result.returncode == 0, result.stderr + assert fdtget(itb, '/images/kernel-1', 'data-offset') == \ + fdtget(itb, '/images/kernel-alt', 'data-offset') + + # A shared explicit iv works as well: it is honored by the + # encryption, retained in the FIT and yields identical ciphertext + its_text = BASE_ITS.replace('hash-1 {', CIPHER_IV + 'hash-1 {') + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-cipher-iv', + '-k', keydir) + assert result.returncode == 0, result.stderr + ciphertext = fdt_bytes(itb, '/images/kernel-1', 'data') + assert ciphertext == fdt_bytes(itb, '/images/kernel-alt', 'data') + assert ciphertext != kdata + authored_iv = bytes.fromhex('112233445566778899aabbccddeeff00') + assert fdt_bytes(itb, '/images/kernel-1/cipher', 'iv') == authored_iv + assert fdt_bytes(itb, '/images/kernel-alt/cipher', 'iv') == authored_iv + + result, itb, kdata = build_fit_text(ubman, its_text, + 'idata-cipher-iv-ext', '-k', keydir, + '-E') + assert result.returncode == 0, result.stderr + assert fdtget(itb, '/images/kernel-1', 'data-offset') == \ + fdtget(itb, '/images/kernel-alt', 'data-offset') + + [email protected]('sandbox') [email protected]('dtc') +def test_fit_image_data_overlap(ubman): + """Sharers' load regions are covered by the configuration overlap check""" + its_text = BASE_ITS.replace( + 'kernel = "kernel-alt";', + 'kernel = "kernel-alt";\n loadables = "filler";') + result, itb, kdata = build_fit_text(ubman, its_text, 'idata-overlap', + extra_images=OVERLAP_SHARER) + assert result.returncode != 0 + assert 'overlapping load regions' in result.stderr
Signature · 1 line
2.43.0
Re: [PATCH v2 7/7] test: py: add tests for the FIT image-data property
Quoted 20 lines · Aristo Chen
test: py: add tests for the FIT image-data property
Cover mkimage's resolution of image-data references between images. The positive tests check that an inline FIT carries identical data and hash values on the sharer and the target while their own properties remain independent, that external data mode points both nodes at a single copy of the payload, that a resolved FIT can be re-processed with -F including conversion to external data, and that two nodes with identical cipher settings share the same ciphertext both inline and external, whether the IV comes from an iv-name-hint or from an explicit iv that is honored and retained in the FIT.
The negative tests check that mkimage rejects references to undefined images, chained references, data that disagrees with the target, image-data combined with external data properties in partial and in complete form, a configuration whose image-data sharers have overlapping load regions, sharing where only one node is encrypted, and encrypted sharing where the nodes disagree on the IV, through different iv-name-hints, different explicit ivs or no shared IV source at all.
Quoted 4 lines
Signed-off-by Aristo Chen <[email protected]>
test/py/tests/test_fit_image_data.py| 391 +++++++++++++++++++++++++++++++++++1 file changed, 391 insertions(+)
Quoted 35 lines · Aristo Chen
Link to v1:
https://lore.kernel.org/u-boot/[email protected]/Changes in v2 (from Simon's review of v1, plus one cleanup noticed while addressing it):
- new patch 1: drop the duplicate
FIT_ALGO_PROPdefinition in the cipher node block ofinclude/image.h, noticed while adding the cipher property defines requested in review.
- patch 2 (was 1): addFIT_IV_HINTandFIT_IV_PROPtoinclude/image.hand use them infit_image_setup_cipher(). Non-functional cleanup, so the Reviewed-by is kept.
- patch 3 (was 2):fit_resolve_image_data()skips its own pack and rewrite when re-processing a FIT that needs nothing resolved, while still validating every reference; reject image-data combined with a complete external data reference before the import when compiling a source file, where the combination cannot be legitimate, while re-processing keeps import-then-compare for the resolved form; complete the kernel-doc of the three new helpers, with the cipher check describing the source-file and re-processed cases separately; explain in the kernel-doc and at the remaining check why it runs after the import; addFIT_DATA_SIZE_UNCIPHERED_PROPand use the property defines.
- patch 5 (was 4): the commit message now states that it completes the validation for the new property rather than fixing a pre-existing problem, and the kernel-doc offit_image_effective_data()gains its parameter and return descriptions. Comment-only, so the Reviewed-by is kept; still a separate patch to stay reviewable on its own.
- patch 6: new, user documentation with a worked example under doc/usage/fit/.
- patch 7 (was 5): two cases for the complete offset and position forms of the mixing check; the Reviewed-by is dropped because of the added cases.
- patch 4 (was 3): unchanged.
Quoted 1 line
[...]
Applied to u-boot/main, thanks!
[1/7] include: image: drop the duplicate FIT_ALGO_PROP definition
commit: 15b4b73766f9eb9c42e67c81b6585cf68e4699e5
[2/7] tools: mkimage: use an explicit iv from the cipher node when encrypting
commit: d0215d8718dab91a964d3989f143156f121211c5
[3/7] tools: mkimage: resolve image-data references between FIT images
commit: 195f273b6c86a42af6bca8c830f9ac0c9bb920dd
[4/7] tools: mkimage: share the external-data region between image-data images
commit: 4436d98341a6a8862ac73e121cd56b7031b38030
[5/7] tools: mkimage: include image-data sharers in the load overlap check
commit: d510292811e352f21ec358647305fabc53d14e06
[6/7] doc: fit: document shared image data through the image-data property
commit: 3272136ca701ac37402df2d34e6093cb53a9a6ba
[7/7] test: py: add tests for the FIT image-data property
commit: baf80458c7fe3d3bf67cd6d3ec11800bbe4003beSignature · 1 line
Tom