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