blob: 37d12a31d4e5ab697eed5c0e6ebaaa286a8b2c70 [file] [log] [blame]
Fabio Utzig61fd8882019-09-14 20:00:20 -03001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
Fabio Utzig61fd8882019-09-14 20:00:20 -03004 * Copyright (c) 2019 JUUL Labs
Tamas Banfe031092020-09-10 17:32:39 +02005 * Copyright (c) 2020 Arm Limited
Fabio Utzig61fd8882019-09-14 20:00:20 -03006 *
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 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +130035 * @returns 0 if the TLV iterator was successfully started
Fabio Utzig61fd8882019-09-14 20:00:20 -030036 * -1 on errors
37 */
38int
39bootutil_tlv_iter_begin(struct image_tlv_iter *it, const struct image_header *hdr,
David Brownd13318a2019-12-04 17:28:40 -070040 const struct flash_area *fap, uint16_t type, bool prot)
Fabio Utzig61fd8882019-09-14 20:00:20 -030041{
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);
Tamas Banfe031092020-09-10 17:32:39 +020050 if (LOAD_IMAGE_DATA(hdr, fap, off_, &info, sizeof(info))) {
Fabio Utzig61fd8882019-09-14 20:00:20 -030051 return -1;
52 }
53
Fabio Utzige52c08e2019-09-11 19:32:00 -030054 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
Tamas Banfe031092020-09-10 17:32:39 +020059 if (LOAD_IMAGE_DATA(hdr, fap, off_ + info.it_tlv_tot,
60 &info, sizeof(info))) {
Fabio Utzige52c08e2019-09-11 19:32:00 -030061 return -1;
62 }
63 } else if (hdr->ih_protect_tlv_size != 0) {
64 return -1;
65 }
66
Fabio Utzig61fd8882019-09-14 20:00:20 -030067 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;
Fabio Utzige52c08e2019-09-11 19:32:00 -030075 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);
Fabio Utzig61fd8882019-09-14 20:00:20 -030079 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 */
94int
95bootutil_tlv_iter_next(struct image_tlv_iter *it, uint32_t *off, uint16_t *len,
David Brownd13318a2019-12-04 17:28:40 -070096 uint16_t *type)
Fabio Utzig61fd8882019-09-14 20:00:20 -030097{
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) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300106 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
Tamas Banfe031092020-09-10 17:32:39 +0200110 rc = LOAD_IMAGE_DATA(it->hdr, it->fap, it->tlv_off, &tlv, sizeof tlv);
Fabio Utzig61fd8882019-09-14 20:00:20 -0300111 if (rc) {
112 return -1;
113 }
114
115 /* No more TLVs in the protected area */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300116 if (it->prot && it->tlv_off >= it->prot_end) {
Fabio Utzig61fd8882019-09-14 20:00:20 -0300117 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}