|
| 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