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