/* * SPDX-License-Identifier: Apache-2.0 * * Copyright (c) 2017-2019 Linaro LTD * Copyright (c) 2016-2019 JUUL Labs * Copyright (c) 2019-2025 Arm Limited * Copyright (c) 2025 Nordic Semiconductor ASA * * Original license: * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ #include #include #include "bootutil/image.h" #include "bootutil/security_cnt.h" #include "bootutil_priv.h" #include "mcuboot_config/mcuboot_config.h" /** * Reads the value of an image's security counter. * * @param state Pointer to the boot state object. * @param slot Slot of the current image to get the security counter of. * @param fap Pointer to a description structure of the image's * flash area. * @param security_cnt Pointer to store the security counter value. * * @return 0 on success; nonzero on failure. */ int32_t bootutil_get_img_security_cnt(struct boot_loader_state *state, int slot, const struct flash_area *fap, uint32_t *img_security_cnt) { struct image_tlv_iter it; uint32_t off; uint16_t len; int32_t rc; if ((state == NULL) || (boot_img_hdr(state, slot) == NULL) || (fap == NULL) || (img_security_cnt == NULL)) { /* Invalid parameter. */ return BOOT_EBADARGS; } /* The security counter TLV is in the protected part of the TLV area. */ if (boot_img_hdr(state, slot)->ih_protect_tlv_size == 0) { return BOOT_EBADIMAGE; } #if defined(MCUBOOT_SWAP_USING_OFFSET) it.start_off = boot_get_state_secondary_offset(state, fap); #endif rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap, IMAGE_TLV_SEC_CNT, true); if (rc) { return rc; } /* Traverse through the protected TLV area to find * the security counter TLV. */ rc = bootutil_tlv_iter_next(&it, &off, &len, NULL); if (rc != 0) { /* Security counter TLV has not been found. */ return -1; } if (len != sizeof(*img_security_cnt)) { /* Security counter is not valid. */ return BOOT_EBADIMAGE; } rc = LOAD_IMAGE_DATA(boot_img_hdr(state, slot), fap, off, img_security_cnt, len); if (rc != 0) { return BOOT_EFLASH; } return 0; }