Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 4 | * Copyright (c) 2019 JUUL Labs |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame^] | 5 | * Copyright (c) 2020 Arm Limited |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 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 | * |
Sam Bristow | d0ca0ff | 2019-10-30 20:51:35 +1300 | [diff] [blame] | 35 | * @returns 0 if the TLV iterator was successfully started |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 36 | * -1 on errors |
| 37 | */ |
| 38 | int |
| 39 | bootutil_tlv_iter_begin(struct image_tlv_iter *it, const struct image_header *hdr, |
David Brown | d13318a | 2019-12-04 17:28:40 -0700 | [diff] [blame] | 40 | const struct flash_area *fap, uint16_t type, bool prot) |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 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); |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame^] | 50 | if (LOAD_IMAGE_DATA(hdr, fap, off_, &info, sizeof(info))) { |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 51 | return -1; |
| 52 | } |
| 53 | |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 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 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame^] | 59 | if (LOAD_IMAGE_DATA(hdr, fap, off_ + info.it_tlv_tot, |
| 60 | &info, sizeof(info))) { |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 61 | return -1; |
| 62 | } |
| 63 | } else if (hdr->ih_protect_tlv_size != 0) { |
| 64 | return -1; |
| 65 | } |
| 66 | |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 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; |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 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); |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 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 |
| 95 | bootutil_tlv_iter_next(struct image_tlv_iter *it, uint32_t *off, uint16_t *len, |
David Brown | d13318a | 2019-12-04 17:28:40 -0700 | [diff] [blame] | 96 | uint16_t *type) |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 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) { |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 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 | |
Tamas Ban | fe03109 | 2020-09-10 17:32:39 +0200 | [diff] [blame^] | 110 | rc = LOAD_IMAGE_DATA(it->hdr, it->fap, it->tlv_off, &tlv, sizeof tlv); |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 111 | if (rc) { |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | /* No more TLVs in the protected area */ |
Fabio Utzig | e52c08e | 2019-09-11 19:32:00 -0300 | [diff] [blame] | 116 | if (it->prot && it->tlv_off >= it->prot_end) { |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 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 | } |