1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  *
4  * Copyright (c) 2019 JUUL Labs
5  * Copyright (c) 2020 Arm Limited
6  * Copyright (c) 2025 Nordic Semiconductor ASA
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #include <stddef.h>
22 
23 #include "bootutil/bootutil.h"
24 #include "bootutil/image.h"
25 #include "bootutil_priv.h"
26 
27 /*
28  * Initialize a TLV iterator.
29  *
30  * @param it An iterator struct
31  * @param hdr image_header of the slot's image
32  * @param fap flash_area of the slot which is storing the image
33  * @param type Type of TLV to look for
34  * @param prot true if TLV has to be stored in the protected area, false otherwise
35  *
36  * @returns 0 if the TLV iterator was successfully started
37  *          -1 on errors
38  */
39 int
bootutil_tlv_iter_begin(struct image_tlv_iter * it,const struct image_header * hdr,const struct flash_area * fap,uint16_t type,bool prot)40 bootutil_tlv_iter_begin(struct image_tlv_iter *it, const struct image_header *hdr,
41                         const struct flash_area *fap, uint16_t type, bool prot)
42 {
43     uint32_t off_;
44     struct image_tlv_info info;
45 
46     if (it == NULL || hdr == NULL || fap == NULL) {
47         return -1;
48     }
49 
50 #if defined(MCUBOOT_SWAP_USING_OFFSET)
51     off_ = BOOT_TLV_OFF(hdr) + it->start_off;
52 #else
53     off_ = BOOT_TLV_OFF(hdr);
54 #endif
55 
56     if (LOAD_IMAGE_DATA(hdr, fap, off_, &info, sizeof(info))) {
57         return -1;
58     }
59 
60     if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
61         if (hdr->ih_protect_tlv_size != info.it_tlv_tot) {
62             return -1;
63         }
64 
65         if (LOAD_IMAGE_DATA(hdr, fap, off_ + info.it_tlv_tot,
66                             &info, sizeof(info))) {
67             return -1;
68         }
69     } else if (hdr->ih_protect_tlv_size != 0) {
70         return -1;
71     }
72 
73     if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
74         return -1;
75     }
76 
77     it->hdr = hdr;
78     it->fap = fap;
79     it->type = type;
80     it->prot = prot;
81     it->prot_end = off_ + it->hdr->ih_protect_tlv_size;
82     it->tlv_end = off_ + it->hdr->ih_protect_tlv_size + info.it_tlv_tot;
83     // position on first TLV
84     it->tlv_off = off_ + sizeof(info);
85     return 0;
86 }
87 
88 /*
89  * Find next TLV
90  *
91  * @param it The image TLV iterator struct
92  * @param off The offset of the TLV's payload in flash
93  * @param len The length of the TLV's payload
94  * @param type If not NULL returns the type of TLV found
95  *
96  * @returns 0 if a TLV with with matching type was found
97  *          1 if no more TLVs with matching type are available
98  *          -1 on errors
99  */
100 int
bootutil_tlv_iter_next(struct image_tlv_iter * it,uint32_t * off,uint16_t * len,uint16_t * type)101 bootutil_tlv_iter_next(struct image_tlv_iter *it, uint32_t *off, uint16_t *len,
102                        uint16_t *type)
103 {
104     struct image_tlv tlv;
105     int rc;
106 
107     if (it == NULL || it->hdr == NULL || it->fap == NULL) {
108         return -1;
109     }
110 
111     while (it->tlv_off < it->tlv_end) {
112         if (it->hdr->ih_protect_tlv_size > 0 && it->tlv_off == it->prot_end) {
113             it->tlv_off += sizeof(struct image_tlv_info);
114         }
115 
116         rc = LOAD_IMAGE_DATA(it->hdr, it->fap, it->tlv_off, &tlv, sizeof tlv);
117         if (rc) {
118             return -1;
119         }
120 
121         /* No more TLVs in the protected area */
122         if (it->prot && it->tlv_off >= it->prot_end) {
123             return 1;
124         }
125 
126         if (it->type == IMAGE_TLV_ANY || tlv.it_type == it->type) {
127             if (type != NULL) {
128                 *type = tlv.it_type;
129             }
130             *off = it->tlv_off + sizeof(tlv);
131             *len = tlv.it_len;
132             it->tlv_off += sizeof(tlv) + tlv.it_len;
133             return 0;
134         }
135 
136         it->tlv_off += sizeof(tlv) + tlv.it_len;
137     }
138 
139     return 1;
140 }
141 
142 /*
143  * Return if a TLV entry is in the protected area.
144  *
145  * @param it The image TLV iterator struct
146  * @param off The offset of the entry to check.
147  *
148  * @return 0 if this TLV iterator entry is not protected.
149  *         1 if this TLV iterator entry is in the protected region
150  *         -1 if the iterator is invalid.
151  */
152 int
bootutil_tlv_iter_is_prot(struct image_tlv_iter * it,uint32_t off)153 bootutil_tlv_iter_is_prot(struct image_tlv_iter *it, uint32_t off)
154 {
155     if (it == NULL || it->hdr == NULL || it->fap == NULL) {
156         return -1;
157     }
158 
159     return off < it->prot_end;
160 }
161