Skip to content

Commit fc4c042

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 ee21ade commit fc4c042

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
@@ -126,6 +126,59 @@ if(DEFINED CONFIG_MEASURED_BOOT OR DEFINED CONFIG_BOOT_SHARE_DATA)
126126
)
127127
endif()
128128

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

boot/zephyr/Kconfig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,44 @@ config MCUBOOT_HW_DOWNGRADE_PREVENTION
10441044

10451045
endchoice
10461046

1047+
config MCUBOOT_UUID_VID
1048+
bool "Expect vendor unique identifier in image's TLV"
1049+
help
1050+
Provide a vendor identification scheme to prevent processing images
1051+
generated by a different vendor.
1052+
1053+
config MCUBOOT_UUID_CID
1054+
bool "Expect image class unique identifier in image's TLV"
1055+
help
1056+
Provide an image class identification scheme to prevent processing
1057+
images for a different CPU or device produced by the same vendor.
1058+
1059+
config MCUBOOT_UUID_VID_VALUE
1060+
string "Vendor name"
1061+
default ""
1062+
depends on MCUBOOT_UUID_VID || MCUBOOT_UUID_CID
1063+
help
1064+
The vendor unique identifier.
1065+
The following formats are supported:
1066+
- Domain name (i.e. amce.corp) used to generate RFC 9562 UUID5
1067+
identifier.
1068+
- Raw UUID (i.e. 12345678-1234-5678-1234-567812345678)
1069+
1070+
if MCUBOOT_UUID_CID
1071+
1072+
image=0
1073+
rsource "Kconfig.uuid.template"
1074+
image=1
1075+
rsource "Kconfig.uuid.template"
1076+
image=2
1077+
rsource "Kconfig.uuid.template"
1078+
image=3
1079+
rsource "Kconfig.uuid.template"
1080+
image=4
1081+
rsource "Kconfig.uuid.template"
1082+
1083+
endif # MCUBOOT_UUID_CID
1084+
10471085
config BOOT_WATCHDOG_FEED
10481086
bool "Feed the watchdog while doing swap"
10491087
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
@@ -220,6 +220,14 @@
220220
#define MCUBOOT_HW_ROLLBACK_PROT
221221
#endif
222222

223+
#ifdef CONFIG_MCUBOOT_UUID_VID
224+
#define MCUBOOT_UUID_VID
225+
#endif
226+
227+
#ifdef CONFIG_MCUBOOT_UUID_CID
228+
#define MCUBOOT_UUID_CID
229+
#endif
230+
223231
#ifdef CONFIG_MEASURED_BOOT
224232
#define MCUBOOT_MEASURED_BOOT
225233
#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

@@ -540,6 +544,14 @@ int main(void)
540544

541545
mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
542546

547+
#if defined(CONFIG_MCUBOOT_UUID_VID) || defined(CONFIG_MCUBOOT_UUID_CID)
548+
FIH_CALL(boot_uuid_init, fih_rc);
549+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
550+
BOOT_LOG_ERR("Unable to initialize UUID module: %d", fih_rc);
551+
FIH_PANIC;
552+
}
553+
#endif /* CONFIG_MCUBOOT_UUID_VID || CONFIG_MCUBOOT_UUID_CID */
554+
543555
#ifdef CONFIG_BOOT_SERIAL_ENTRANCE_GPIO
544556
BOOT_LOG_DBG("Checking GPIO for serial recovery");
545557
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)