blob: 9108be41df78db64459b0812bcd654f36adbc441 [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
Fabio Utzige52c08e2019-09-11 19:32:00 -030051 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
52 if (hdr->ih_protect_tlv_size != info.it_tlv_tot) {
53 return -1;
54 }
55
56 if (flash_area_read(fap, off_ + info.it_tlv_tot, &info, sizeof(info))) {
57 return -1;
58 }
59 } else if (hdr->ih_protect_tlv_size != 0) {
60 return -1;
61 }
62
Fabio Utzig61fd8882019-09-14 20:00:20 -030063 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
64 return -1;
65 }
66
67 it->hdr = hdr;
68 it->fap = fap;
69 it->type = type;
70 it->prot = prot;
Fabio Utzige52c08e2019-09-11 19:32:00 -030071 it->prot_end = off_ + it->hdr->ih_protect_tlv_size;
72 it->tlv_end = off_ + it->hdr->ih_protect_tlv_size + info.it_tlv_tot;
73 // position on first TLV
74 it->tlv_off = off_ + sizeof(info);
Fabio Utzig61fd8882019-09-14 20:00:20 -030075 return 0;
76}
77
78/*
79 * Find next TLV
80 *
81 * @param it The image TLV iterator struct
82 * @param off The offset of the TLV's payload in flash
83 * @param len The length of the TLV's payload
84 * @param type If not NULL returns the type of TLV found
85 *
86 * @returns 0 if a TLV with with matching type was found
87 * 1 if no more TLVs with matching type are available
88 * -1 on errors
89 */
90int
91bootutil_tlv_iter_next(struct image_tlv_iter *it, uint32_t *off, uint16_t *len,
92 uint8_t *type)
93{
94 struct image_tlv tlv;
95 int rc;
96
97 if (it == NULL || it->hdr == NULL || it->fap == NULL) {
98 return -1;
99 }
100
101 while (it->tlv_off < it->tlv_end) {
Fabio Utzige52c08e2019-09-11 19:32:00 -0300102 if (it->hdr->ih_protect_tlv_size > 0 && it->tlv_off == it->prot_end) {
103 it->tlv_off += sizeof(struct image_tlv_info);
104 }
105
Fabio Utzig61fd8882019-09-14 20:00:20 -0300106 rc = flash_area_read(it->fap, it->tlv_off, &tlv, sizeof tlv);
107 if (rc) {
108 return -1;
109 }
110
111 /* No more TLVs in the protected area */
Fabio Utzige52c08e2019-09-11 19:32:00 -0300112 if (it->prot && it->tlv_off >= it->prot_end) {
Fabio Utzig61fd8882019-09-14 20:00:20 -0300113 return 1;
114 }
115
116 if (it->type == IMAGE_TLV_ANY || tlv.it_type == it->type) {
117 if (type != NULL) {
118 *type = tlv.it_type;
119 }
120 *off = it->tlv_off + sizeof(tlv);
121 *len = tlv.it_len;
122 it->tlv_off += sizeof(tlv) + tlv.it_len;
123 return 0;
124 }
125
126 it->tlv_off += sizeof(tlv) + tlv.it_len;
127 }
128
129 return 1;
130}