Skip to content

Commit 54dd37c

Browse files
committed
zephyr: Add Kconfig-based VID and CID checks
Add a possibility to configure vendor ID and image class ID through Kconfig. Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent 43840aa commit 54dd37c

File tree

6 files changed

+203
-0
lines changed

6 files changed

+203
-0
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,59 @@ if(DEFINED CONFIG_MEASURED_BOOT OR DEFINED CONFIG_BOOT_SHARE_DATA)
129129
)
130130
endif()
131131

132+
# Include the sample implementation.
133+
if(CONFIG_MCUBOOT_UUID_VID OR CONFIG_MCUBOOT_UUID_CID)
134+
zephyr_library_sources(
135+
mcuboot_uuid.c
136+
)
137+
endif()
138+
139+
# Generate VID value and raw value definition
140+
if(CONFIG_MCUBOOT_UUID_VID OR CONFIG_MCUBOOT_UUID_CID)
141+
string(REGEX MATCHALL "^([0-9A-F][0-9A-F]|\-)+$" match_parts ${CONFIG_MCUBOOT_UUID_VID_VALUE})
142+
if ("${match_parts}" STREQUAL "${CONFIG_MCUBOOT_UUID_VID_VALUE}")
143+
set(UUID_VID ${match_parts})
144+
else()
145+
set(UUID_DNS_NAMESPACE 6ba7b810-9dad-11d1-80b4-00c04fd430c8)
146+
string(
147+
UUID UUID_VID
148+
NAMESPACE ${UUID_DNS_NAMESPACE}
149+
NAME ${CONFIG_MCUBOOT_UUID_VID_VALUE}
150+
TYPE SHA1 UPPER
151+
)
152+
endif()
153+
154+
# Convert UUID into C array.
155+
string(REGEX REPLACE "([0-9A-F][0-9A-F])\-?" "0x\\1, " UUID_VID_RAW ${UUID_VID})
156+
add_compile_definitions(MCUBOOT_UUID_VID_VALUE=${UUID_VID_RAW})
157+
endif()
158+
159+
# Generate VID value(s) and raw value definition(s)
160+
if(CONFIG_MCUBOOT_UUID_CID)
161+
set(MCUBOOT_IMAGES_COUNT 4)
162+
foreach(image_id RANGE ${MCUBOOT_IMAGES_COUNT})
163+
if(CONFIG_MCUBOOT_UUID_CID_IMAGE_${image_id})
164+
# Check if RAW UUID format is used
165+
string(REGEX MATCHALL "^([0-9A-F][0-9A-F]|\-)+$" match_parts ${CONFIG_MCUBOOT_UUID_CID_IMAGE_${image_id}_VALUE})
166+
if ("${match_parts}" STREQUAL "${CONFIG_MCUBOOT_UUID_CID_IMAGE_${image_id}_VALUE}")
167+
set(UUID_CID_IMAGE_${image_id} ${match_parts})
168+
else()
169+
# If not - generate UUID based on VID and CID values
170+
string(
171+
UUID UUID_CID_IMAGE_${image_id}
172+
NAMESPACE ${UUID_VID}
173+
NAME ${CONFIG_MCUBOOT_UUID_CID_IMAGE_${image_id}_VALUE}
174+
TYPE SHA1 UPPER
175+
)
176+
endif()
177+
178+
# Convert UUID into C array.
179+
string(REGEX REPLACE "([0-9A-F][0-9A-F])\-?" "0x\\1, " UUID_CID_IMAGE_${image_id}_RAW ${UUID_CID_IMAGE_${image_id}})
180+
add_compile_definitions(MCUBOOT_UUIC_CID_IMAGE_${image_id}_VALUE=${UUID_CID_IMAGE_${image_id}_RAW})
181+
endif()
182+
endforeach()
183+
endif()
184+
132185
# library which might be common source code for MCUBoot and an application
133186
zephyr_link_libraries(MCUBOOT_BOOTUTIL)
134187

boot/zephyr/Kconfig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,44 @@ config MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_LIMITED
10711071

10721072
endchoice
10731073

1074+
config MCUBOOT_UUID_VID
1075+
bool "Expect vendor unique identifier in image's TLV"
1076+
help
1077+
Provide a vendor identification scheme to prevent processing images
1078+
generated by a different vendor.
1079+
1080+
config MCUBOOT_UUID_CID
1081+
bool "Expect image class unique identifier in image's TLV"
1082+
help
1083+
Provide an image class identification scheme to prevent processing
1084+
images for a different CPU or device produced by the same vendor.
1085+
1086+
config MCUBOOT_UUID_VID_VALUE
1087+
string "Vendor name"
1088+
default ""
1089+
depends on MCUBOOT_UUID_VID || MCUBOOT_UUID_CID
1090+
help
1091+
The vendor unique identifier.
1092+
The following formats are supported:
1093+
- Domain name (i.e. amce.corp) used to generate RFC 9562 UUID5
1094+
identifier.
1095+
- Raw UUID (i.e. 12345678-1234-5678-1234-567812345678)
1096+
1097+
if MCUBOOT_UUID_CID
1098+
1099+
image=0
1100+
rsource "Kconfig.uuid.template"
1101+
image=1
1102+
rsource "Kconfig.uuid.template"
1103+
image=2
1104+
rsource "Kconfig.uuid.template"
1105+
image=3
1106+
rsource "Kconfig.uuid.template"
1107+
image=4
1108+
rsource "Kconfig.uuid.template"
1109+
1110+
endif # MCUBOOT_UUID_CID
1111+
10741112
config BOOT_WATCHDOG_FEED
10751113
bool "Feed the watchdog while doing swap"
10761114
default y if WATCHDOG

boot/zephyr/Kconfig.uuid.template

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
config MCUBOOT_UUID_CID_IMAGE_$(image)_VALUE
2+
string "Image class name (image $(image))"
3+
default ""
4+
help
5+
The image class unique identifier.
6+
The following formats are supported:
7+
- Image class name (i.e. nRF5340_door_lock_btperipheral).
8+
This format requires MCUBOOT_UUID_VID_VALUE to be defined
9+
as the VID UUID is used as the namespace for generating RFC 9562
10+
UUID5 identifier.
11+
- Raw UUID (i.e. 12345678-1234-5678-1234-567812345678)
12+
13+
config MCUBOOT_UUID_CID_IMAGE_$(image)
14+
bool
15+
default y
16+
depends on MCUBOOT_UUID_CID_IMAGE_$(image)_VALUE != ""
17+
help
18+
Helper symbol to simplify the active CId list generation.
19+

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@
232232
#define MCUBOOT_HW_ROLLBACK_PROT_COUNTER_LIMITED
233233
#endif
234234

235+
#ifdef CONFIG_MCUBOOT_UUID_VID
236+
#define MCUBOOT_UUID_VID
237+
#endif
238+
239+
#ifdef CONFIG_MCUBOOT_UUID_CID
240+
#define MCUBOOT_UUID_CID
241+
#endif
242+
235243
#ifdef CONFIG_MEASURED_BOOT
236244
#define MCUBOOT_MEASURED_BOOT
237245
#endif

boot/zephyr/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
#include "bootutil/mcuboot_status.h"
4949
#include "flash_map_backend/flash_map_backend.h"
5050

51+
#if defined(CONFIG_MCUBOOT_UUID_VID) || defined(CONFIG_MCUBOOT_UUID_CID)
52+
#include "bootutil/mcuboot_uuid.h"
53+
#endif /* CONFIG_MCUBOOT_UUID_VID || CONFIG_MCUBOOT_UUID_CID */
54+
5155
/* Check if Espressif target is supported */
5256
#ifdef CONFIG_SOC_FAMILY_ESPRESSIF_ESP32
5357

@@ -521,6 +525,14 @@ int main(void)
521525

522526
mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
523527

528+
#if defined(CONFIG_MCUBOOT_UUID_VID) || defined(CONFIG_MCUBOOT_UUID_CID)
529+
FIH_CALL(boot_uuid_init, fih_rc);
530+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
531+
BOOT_LOG_ERR("Unable to initialize UUID module: %d", fih_rc);
532+
FIH_PANIC;
533+
}
534+
#endif /* CONFIG_MCUBOOT_UUID_VID || CONFIG_MCUBOOT_UUID_CID */
535+
524536
#ifdef CONFIG_BOOT_SERIAL_ENTRANCE_GPIO
525537
BOOT_LOG_DBG("Checking GPIO for serial recovery");
526538
if (io_detect_pin() &&

boot/zephyr/mcuboot_uuid.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <bootutil/mcuboot_uuid.h>
7+
8+
#define IMAGE_ID_COUNT 5
9+
#define CID_INIT(index, label) \
10+
static const struct image_uuid label = {{ \
11+
MCUBOOT_UUIC_CID_IMAGE_## index ##_VALUE \
12+
}}
13+
#define CID_CONFIG(index) UTIL_CAT(CONFIG_MCUBOOT_UUID_CID_IMAGE_, index)
14+
#define CID_DEFINE(index, prefix) \
15+
IF_ENABLED(CID_CONFIG(index), (CID_INIT(index, prefix##index)))
16+
17+
#define CID_CONDITION(index, label) \
18+
if (image_id == index) { \
19+
*uuid_cid = &label; \
20+
FIH_RET(FIH_SUCCESS); \
21+
}
22+
#define CID_CHECK(index, prefix) \
23+
IF_ENABLED(CID_CONFIG(index), (CID_CONDITION(index, prefix##index)))
24+
25+
static fih_ret boot_uuid_compare(const struct image_uuid *uuid1, const struct image_uuid *uuid2)
26+
{
27+
return fih_ret_encode_zero_equality(memcmp(uuid1->raw, uuid2->raw,
28+
ARRAY_SIZE(uuid1->raw)));
29+
}
30+
31+
#ifdef CONFIG_MCUBOOT_UUID_CID
32+
LISTIFY(IMAGE_ID_COUNT, CID_DEFINE, (;), uuid_cid_image_);
33+
34+
static fih_ret boot_uuid_cid_get(uint32_t image_id, const struct image_uuid **uuid_cid)
35+
{
36+
if (uuid_cid != NULL) {
37+
LISTIFY(IMAGE_ID_COUNT, CID_CHECK, (), uuid_cid_image_)
38+
}
39+
40+
FIH_RET(FIH_FAILURE);
41+
}
42+
#endif /* CONFIG_MCUBOOT_UUID_CID */
43+
44+
fih_ret boot_uuid_init(void)
45+
{
46+
FIH_RET(FIH_SUCCESS);
47+
}
48+
49+
#ifdef CONFIG_MCUBOOT_UUID_VID
50+
fih_ret boot_uuid_vid_match(uint32_t image_id, const struct image_uuid *uuid_vid)
51+
{
52+
const struct image_uuid uuid_vid_c = {{
53+
MCUBOOT_UUID_VID_VALUE
54+
}};
55+
56+
return boot_uuid_compare(uuid_vid, &uuid_vid_c);
57+
}
58+
#endif /* CONFIG_MCUBOOT_UUID_VID */
59+
60+
#ifdef CONFIG_MCUBOOT_UUID_CID
61+
fih_ret boot_uuid_cid_match(uint32_t image_id, const struct image_uuid *uuid_cid)
62+
{
63+
FIH_DECLARE(ret_code, FIH_FAILURE);
64+
const struct image_uuid *exp_uuid_cid = NULL;
65+
66+
FIH_CALL(boot_uuid_cid_get, ret_code, image_id, &exp_uuid_cid);
67+
if (FIH_NOT_EQ(ret_code, FIH_SUCCESS) && FIH_NOT_EQ(ret_code, FIH_FAILURE)) {
68+
FIH_RET(FIH_FAILURE);
69+
}
70+
71+
return boot_uuid_compare(uuid_cid, exp_uuid_cid);
72+
}
73+
#endif /* CONFIG_MCUBOOT_UUID_CID */

0 commit comments

Comments
 (0)