Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 20 | /* |
| 21 | * Modifications are Copyright (c) 2019 Arm Limited. |
| 22 | */ |
| 23 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <string.h> |
| 26 | #include <inttypes.h> |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 27 | #include <stddef.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 28 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 29 | #include "sysflash/sysflash.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 30 | #include "flash_map_backend/flash_map_backend.h" |
| 31 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 32 | #include "bootutil/image.h" |
| 33 | #include "bootutil/bootutil.h" |
| 34 | #include "bootutil_priv.h" |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 35 | #include "bootutil/bootutil_log.h" |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 36 | #ifdef MCUBOOT_ENC_IMAGES |
| 37 | #include "bootutil/enc_key.h" |
| 38 | #endif |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 39 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 40 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 41 | |
Fabio Utzig | 10ee648 | 2019-08-01 12:04:52 -0300 | [diff] [blame] | 42 | /* Currently only used by imgmgr */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 43 | int boot_current_slot; |
| 44 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 45 | const uint32_t boot_img_magic[] = { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 46 | 0xf395c277, |
| 47 | 0x7fefd260, |
| 48 | 0x0f505235, |
| 49 | 0x8079b62c, |
| 50 | }; |
| 51 | |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 52 | #define BOOT_MAGIC_ARR_SZ \ |
| 53 | (sizeof boot_img_magic / sizeof boot_img_magic[0]) |
| 54 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 55 | const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 56 | const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN; |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 57 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 58 | struct boot_swap_table { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 59 | uint8_t magic_primary_slot; |
| 60 | uint8_t magic_secondary_slot; |
| 61 | uint8_t image_ok_primary_slot; |
| 62 | uint8_t image_ok_secondary_slot; |
| 63 | uint8_t copy_done_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 64 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 65 | uint8_t swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | /** |
| 69 | * This set of tables maps image trailer contents to swap operation type. |
| 70 | * When searching for a match, these tables must be iterated sequentially. |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 71 | * |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 72 | * NOTE: the table order is very important. The settings in the secondary |
| 73 | * slot always are priority to the primary slot and should be located |
| 74 | * earlier in the table. |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 75 | * |
| 76 | * The table lists only states where there is action needs to be taken by |
| 77 | * the bootloader, as in starting/finishing a swap operation. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 78 | */ |
| 79 | static const struct boot_swap_table boot_swap_tables[] = { |
| 80 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 81 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 82 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 83 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 84 | .image_ok_secondary_slot = BOOT_FLAG_UNSET, |
| 85 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 86 | .swap_type = BOOT_SWAP_TYPE_TEST, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 87 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 88 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 89 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 90 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 91 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 92 | .image_ok_secondary_slot = BOOT_FLAG_SET, |
| 93 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 94 | .swap_type = BOOT_SWAP_TYPE_PERM, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 95 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 96 | { |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 97 | .magic_primary_slot = BOOT_MAGIC_GOOD, |
| 98 | .magic_secondary_slot = BOOT_MAGIC_UNSET, |
| 99 | .image_ok_primary_slot = BOOT_FLAG_UNSET, |
| 100 | .image_ok_secondary_slot = BOOT_FLAG_ANY, |
| 101 | .copy_done_primary_slot = BOOT_FLAG_SET, |
| 102 | .swap_type = BOOT_SWAP_TYPE_REVERT, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 103 | }, |
| 104 | }; |
| 105 | |
| 106 | #define BOOT_SWAP_TABLES_COUNT \ |
| 107 | (sizeof boot_swap_tables / sizeof boot_swap_tables[0]) |
| 108 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 109 | static int |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 110 | boot_magic_decode(const uint32_t *magic) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 111 | { |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 112 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 113 | return BOOT_MAGIC_GOOD; |
| 114 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 115 | return BOOT_MAGIC_BAD; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 118 | static int |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 119 | boot_flag_decode(uint8_t flag) |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 120 | { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 121 | if (flag != BOOT_FLAG_SET) { |
| 122 | return BOOT_FLAG_BAD; |
| 123 | } |
| 124 | return BOOT_FLAG_SET; |
| 125 | } |
| 126 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Determines if a status source table is satisfied by the specified magic |
| 129 | * code. |
| 130 | * |
| 131 | * @param tbl_val A magic field from a status source table. |
| 132 | * @param val The magic value in a trailer, encoded as a |
| 133 | * BOOT_MAGIC_[...]. |
| 134 | * |
| 135 | * @return 1 if the two values are compatible; |
| 136 | * 0 otherwise. |
| 137 | */ |
| 138 | int |
| 139 | boot_magic_compatible_check(uint8_t tbl_val, uint8_t val) |
| 140 | { |
| 141 | switch (tbl_val) { |
| 142 | case BOOT_MAGIC_ANY: |
| 143 | return 1; |
| 144 | |
| 145 | case BOOT_MAGIC_NOTGOOD: |
| 146 | return val != BOOT_MAGIC_GOOD; |
| 147 | |
| 148 | default: |
| 149 | return tbl_val == val; |
| 150 | } |
| 151 | } |
| 152 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 153 | uint32_t |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 154 | boot_trailer_sz(uint8_t min_write_sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 155 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 156 | return /* state for all sectors */ |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 157 | BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz + |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 158 | #ifdef MCUBOOT_ENC_IMAGES |
| 159 | /* encryption keys */ |
| 160 | BOOT_ENC_KEY_SIZE * 2 + |
| 161 | #endif |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 162 | /* swap_type + copy_done + image_ok + swap_size */ |
| 163 | BOOT_MAX_ALIGN * 4 + |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 164 | BOOT_MAGIC_SZ; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static uint32_t |
| 168 | boot_magic_off(const struct flash_area *fap) |
| 169 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 170 | return fap->fa_size - BOOT_MAGIC_SZ; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 173 | int |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 174 | boot_status_entries(int image_index, const struct flash_area *fap) |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 175 | { |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 176 | if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) { |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 177 | return BOOT_STATUS_STATE_COUNT; |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 178 | } else if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) || |
| 179 | fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) { |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 180 | return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES; |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 181 | } |
Fabio Utzig | 9d16009 | 2019-08-09 07:46:34 -0300 | [diff] [blame^] | 182 | return -1; |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 185 | uint32_t |
| 186 | boot_status_off(const struct flash_area *fap) |
| 187 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 188 | uint32_t off_from_end; |
| 189 | uint8_t elem_sz; |
| 190 | |
| 191 | elem_sz = flash_area_align(fap); |
| 192 | |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 193 | off_from_end = boot_trailer_sz(elem_sz); |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 194 | |
| 195 | assert(off_from_end <= fap->fa_size); |
| 196 | return fap->fa_size - off_from_end; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 199 | uint32_t |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 200 | boot_swap_info_off(const struct flash_area *fap) |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 201 | { |
| 202 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3; |
| 203 | } |
| 204 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 205 | static uint32_t |
| 206 | boot_copy_done_off(const struct flash_area *fap) |
| 207 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 208 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static uint32_t |
| 212 | boot_image_ok_off(const struct flash_area *fap) |
| 213 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 214 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 217 | static uint32_t |
| 218 | boot_swap_size_off(const struct flash_area *fap) |
| 219 | { |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 220 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 221 | } |
| 222 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 223 | #ifdef MCUBOOT_ENC_IMAGES |
| 224 | static uint32_t |
| 225 | boot_enc_key_off(const struct flash_area *fap, uint8_t slot) |
| 226 | { |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 227 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4 - |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 228 | ((slot + 1) * BOOT_ENC_KEY_SIZE); |
| 229 | } |
| 230 | #endif |
| 231 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 232 | int |
| 233 | boot_read_swap_state(const struct flash_area *fap, |
| 234 | struct boot_swap_state *state) |
| 235 | { |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 236 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 237 | uint32_t off; |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 238 | uint8_t swap_info; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 239 | int rc; |
| 240 | |
| 241 | off = boot_magic_off(fap); |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 242 | rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ); |
| 243 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 244 | return BOOT_EFLASH; |
| 245 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 246 | if (rc == 1) { |
| 247 | state->magic = BOOT_MAGIC_UNSET; |
| 248 | } else { |
| 249 | state->magic = boot_magic_decode(magic); |
| 250 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 251 | |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 252 | off = boot_swap_info_off(fap); |
| 253 | rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 254 | if (rc < 0) { |
| 255 | return BOOT_EFLASH; |
| 256 | } |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 257 | |
| 258 | /* Extract the swap type and image number */ |
| 259 | state->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
| 260 | state->image_num = BOOT_GET_IMAGE_NUM(swap_info); |
| 261 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 262 | if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) { |
| 263 | state->swap_type = BOOT_SWAP_TYPE_NONE; |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 264 | state->image_num = 0; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 267 | off = boot_copy_done_off(fap); |
| 268 | rc = flash_area_read_is_empty(fap, off, &state->copy_done, |
| 269 | sizeof state->copy_done); |
| 270 | if (rc < 0) { |
| 271 | return BOOT_EFLASH; |
| 272 | } |
| 273 | if (rc == 1) { |
| 274 | state->copy_done = BOOT_FLAG_UNSET; |
| 275 | } else { |
| 276 | state->copy_done = boot_flag_decode(state->copy_done); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | off = boot_image_ok_off(fap); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 280 | rc = flash_area_read_is_empty(fap, off, &state->image_ok, |
| 281 | sizeof state->image_ok); |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 282 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 283 | return BOOT_EFLASH; |
| 284 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 285 | if (rc == 1) { |
| 286 | state->image_ok = BOOT_FLAG_UNSET; |
| 287 | } else { |
| 288 | state->image_ok = boot_flag_decode(state->image_ok); |
| 289 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Reads the image trailer from the scratch area. |
| 296 | */ |
| 297 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 298 | boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 299 | { |
| 300 | const struct flash_area *fap; |
| 301 | int rc; |
| 302 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 303 | rc = flash_area_open(flash_area_id, &fap); |
| 304 | if (rc != 0) { |
| 305 | return BOOT_EFLASH; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | rc = boot_read_swap_state(fap, state); |
Fabio Utzig | acfba2e | 2017-05-22 11:06:29 -0400 | [diff] [blame] | 309 | flash_area_close(fap); |
| 310 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | int |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 314 | boot_read_swap_size(int image_index, uint32_t *swap_size) |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 315 | { |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 316 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 317 | uint32_t off; |
| 318 | const struct flash_area *fap; |
| 319 | int rc; |
| 320 | |
| 321 | /* |
| 322 | * In the middle a swap, tries to locate the saved swap size. Looks |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 323 | * for a valid magic, first on the primary slot, then on scratch. |
| 324 | * Both "slots" can end up being temporary storage for a swap and it |
| 325 | * is assumed that if magic is valid then swap size is too, because |
| 326 | * magic is always written in the last step. |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 327 | */ |
| 328 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 329 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 330 | if (rc != 0) { |
| 331 | return BOOT_EFLASH; |
| 332 | } |
| 333 | |
| 334 | off = boot_magic_off(fap); |
| 335 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 336 | if (rc != 0) { |
| 337 | rc = BOOT_EFLASH; |
| 338 | goto out; |
| 339 | } |
| 340 | |
| 341 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) { |
| 342 | /* |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 343 | * If the primary slot's magic is not valid, try scratch... |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 344 | */ |
| 345 | |
| 346 | flash_area_close(fap); |
| 347 | |
| 348 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap); |
| 349 | if (rc != 0) { |
| 350 | return BOOT_EFLASH; |
| 351 | } |
| 352 | |
| 353 | off = boot_magic_off(fap); |
| 354 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 355 | if (rc != 0) { |
| 356 | rc = BOOT_EFLASH; |
| 357 | goto out; |
| 358 | } |
| 359 | |
| 360 | assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0); |
| 361 | } |
| 362 | |
| 363 | off = boot_swap_size_off(fap); |
| 364 | rc = flash_area_read(fap, off, swap_size, sizeof *swap_size); |
| 365 | if (rc != 0) { |
| 366 | rc = BOOT_EFLASH; |
| 367 | } |
| 368 | |
| 369 | out: |
| 370 | flash_area_close(fap); |
| 371 | return rc; |
| 372 | } |
| 373 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 374 | #ifdef MCUBOOT_ENC_IMAGES |
| 375 | int |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 376 | boot_read_enc_key(int image_index, uint8_t slot, uint8_t *enckey) |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 377 | { |
| 378 | uint32_t magic[BOOT_MAGIC_SZ]; |
| 379 | uint32_t off; |
| 380 | const struct flash_area *fap; |
| 381 | int rc; |
| 382 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 383 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap); |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 384 | if (rc != 0) { |
| 385 | return BOOT_EFLASH; |
| 386 | } |
| 387 | |
| 388 | off = boot_magic_off(fap); |
| 389 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 390 | if (rc != 0) { |
| 391 | rc = BOOT_EFLASH; |
| 392 | goto out; |
| 393 | } |
| 394 | |
| 395 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) { |
| 396 | /* |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 397 | * If the primary slot's magic is not valid, try scratch... |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 398 | */ |
| 399 | |
| 400 | flash_area_close(fap); |
| 401 | |
| 402 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap); |
| 403 | if (rc != 0) { |
| 404 | return BOOT_EFLASH; |
| 405 | } |
| 406 | |
| 407 | off = boot_magic_off(fap); |
| 408 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 409 | if (rc != 0) { |
| 410 | rc = BOOT_EFLASH; |
| 411 | goto out; |
| 412 | } |
| 413 | |
| 414 | assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0); |
| 415 | } |
| 416 | |
| 417 | off = boot_enc_key_off(fap, slot); |
| 418 | rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE); |
| 419 | if (rc != 0) { |
| 420 | rc = BOOT_EFLASH; |
| 421 | } |
| 422 | |
| 423 | out: |
| 424 | flash_area_close(fap); |
| 425 | return rc; |
| 426 | } |
| 427 | #endif |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 428 | |
| 429 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 430 | boot_write_magic(const struct flash_area *fap) |
| 431 | { |
| 432 | uint32_t off; |
| 433 | int rc; |
| 434 | |
| 435 | off = boot_magic_off(fap); |
| 436 | |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 437 | BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)", |
| 438 | fap->fa_id, off, fap->fa_off + off); |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 439 | rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 440 | if (rc != 0) { |
| 441 | return BOOT_EFLASH; |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 447 | static int |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 448 | boot_write_trailer_byte(const struct flash_area *fap, uint32_t off, |
| 449 | uint8_t val) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 450 | { |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 451 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 452 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 453 | uint8_t erased_val; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 454 | int rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 455 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 456 | align = flash_area_align(fap); |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 457 | assert(align <= BOOT_MAX_ALIGN); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 458 | erased_val = flash_area_erased_val(fap); |
| 459 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 460 | buf[0] = val; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 461 | |
| 462 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 463 | if (rc != 0) { |
| 464 | return BOOT_EFLASH; |
| 465 | } |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 471 | boot_write_copy_done(const struct flash_area *fap) |
| 472 | { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 473 | uint32_t off; |
| 474 | |
| 475 | off = boot_copy_done_off(fap); |
| 476 | BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)", |
| 477 | fap->fa_id, off, fap->fa_off + off); |
| 478 | return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 482 | boot_write_image_ok(const struct flash_area *fap) |
| 483 | { |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 484 | uint32_t off; |
| 485 | |
| 486 | off = boot_image_ok_off(fap); |
| 487 | BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)", |
| 488 | fap->fa_id, off, fap->fa_off + off); |
| 489 | return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Writes the specified value to the `swap-type` field of an image trailer. |
| 494 | * This value is persisted so that the boot loader knows what swap operation to |
| 495 | * resume in case of an unexpected reset. |
| 496 | */ |
| 497 | int |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 498 | boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type, |
| 499 | uint8_t image_num) |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 500 | { |
| 501 | uint32_t off; |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 502 | uint8_t swap_info; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 503 | |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 504 | BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type); |
| 505 | off = boot_swap_info_off(fap); |
| 506 | BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x" |
| 507 | " image_num=0x%x", |
| 508 | fap->fa_id, off, fap->fa_off + off, swap_type, image_num); |
| 509 | return boot_write_trailer_byte(fap, off, swap_info); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | int |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 513 | boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size) |
| 514 | { |
| 515 | uint32_t off; |
| 516 | int rc; |
| 517 | uint8_t buf[BOOT_MAX_ALIGN]; |
| 518 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 519 | uint8_t erased_val; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 520 | |
| 521 | off = boot_swap_size_off(fap); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 522 | align = flash_area_align(fap); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 523 | assert(align <= BOOT_MAX_ALIGN); |
| 524 | if (align < sizeof swap_size) { |
| 525 | align = sizeof swap_size; |
| 526 | } |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 527 | erased_val = flash_area_erased_val(fap); |
| 528 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 529 | memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size); |
| 530 | |
Christopher Collins | 2c88e69 | 2019-05-22 15:10:14 -0700 | [diff] [blame] | 531 | BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)", |
| 532 | fap->fa_id, off, fap->fa_off + off); |
| 533 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 534 | rc = flash_area_write(fap, off, buf, align); |
| 535 | if (rc != 0) { |
| 536 | return BOOT_EFLASH; |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 542 | #ifdef MCUBOOT_ENC_IMAGES |
| 543 | int |
| 544 | boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey) |
| 545 | { |
| 546 | uint32_t off; |
| 547 | int rc; |
| 548 | |
| 549 | off = boot_enc_key_off(fap, slot); |
| 550 | rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE); |
| 551 | if (rc != 0) { |
| 552 | return BOOT_EFLASH; |
| 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | #endif |
| 558 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 559 | int |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 560 | boot_swap_type_multi(int image_index) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 561 | { |
| 562 | const struct boot_swap_table *table; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 563 | struct boot_swap_state primary_slot; |
| 564 | struct boot_swap_state secondary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 565 | int rc; |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 566 | size_t i; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 567 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 568 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 569 | &primary_slot); |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 570 | if (rc) { |
| 571 | return BOOT_SWAP_TYPE_PANIC; |
| 572 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 573 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 574 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 575 | &secondary_slot); |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 576 | if (rc) { |
| 577 | return BOOT_SWAP_TYPE_PANIC; |
| 578 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 579 | |
| 580 | for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { |
| 581 | table = boot_swap_tables + i; |
| 582 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 583 | if (boot_magic_compatible_check(table->magic_primary_slot, |
| 584 | primary_slot.magic) && |
| 585 | boot_magic_compatible_check(table->magic_secondary_slot, |
| 586 | secondary_slot.magic) && |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 587 | (table->image_ok_primary_slot == BOOT_FLAG_ANY || |
| 588 | table->image_ok_primary_slot == primary_slot.image_ok) && |
| 589 | (table->image_ok_secondary_slot == BOOT_FLAG_ANY || |
| 590 | table->image_ok_secondary_slot == secondary_slot.image_ok) && |
| 591 | (table->copy_done_primary_slot == BOOT_FLAG_ANY || |
| 592 | table->copy_done_primary_slot == primary_slot.copy_done)) { |
Fabio Utzig | 34e393e | 2017-05-22 11:07:07 -0400 | [diff] [blame] | 593 | BOOT_LOG_INF("Swap type: %s", |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 594 | table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : |
| 595 | table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : |
| 596 | table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : |
| 597 | "BUG; can't happen"); |
| 598 | assert(table->swap_type == BOOT_SWAP_TYPE_TEST || |
| 599 | table->swap_type == BOOT_SWAP_TYPE_PERM || |
| 600 | table->swap_type == BOOT_SWAP_TYPE_REVERT); |
| 601 | return table->swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 605 | BOOT_LOG_INF("Swap type: none"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 606 | return BOOT_SWAP_TYPE_NONE; |
| 607 | } |
| 608 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 609 | int |
| 610 | boot_swap_type(void) |
| 611 | { |
| 612 | return boot_swap_type_multi(0); |
| 613 | } |
| 614 | |
| 615 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 616 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 617 | * Marks the image in the secondary slot as pending. On the next reboot, |
| 618 | * the system will perform a one-time boot of the the secondary slot image. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 619 | * |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 620 | * @param permanent Whether the image should be used permanently or |
| 621 | * only tested once: |
| 622 | * 0=run image once, then confirm or revert. |
| 623 | * 1=run image forever. |
| 624 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 625 | * @return 0 on success; nonzero on failure. |
| 626 | */ |
| 627 | int |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 628 | boot_set_pending(int permanent) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 629 | { |
| 630 | const struct flash_area *fap; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 631 | struct boot_swap_state state_secondary_slot; |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 632 | uint8_t swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 633 | int rc; |
| 634 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 635 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0), |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 636 | &state_secondary_slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 637 | if (rc != 0) { |
| 638 | return rc; |
| 639 | } |
| 640 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 641 | switch (state_secondary_slot.magic) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 642 | case BOOT_MAGIC_GOOD: |
| 643 | /* Swap already scheduled. */ |
| 644 | return 0; |
| 645 | |
| 646 | case BOOT_MAGIC_UNSET: |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 647 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 648 | if (rc != 0) { |
| 649 | rc = BOOT_EFLASH; |
| 650 | } else { |
| 651 | rc = boot_write_magic(fap); |
| 652 | } |
| 653 | |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 654 | if (rc == 0 && permanent) { |
| 655 | rc = boot_write_image_ok(fap); |
| 656 | } |
| 657 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 658 | if (rc == 0) { |
| 659 | if (permanent) { |
| 660 | swap_type = BOOT_SWAP_TYPE_PERM; |
| 661 | } else { |
| 662 | swap_type = BOOT_SWAP_TYPE_TEST; |
| 663 | } |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 664 | rc = boot_write_swap_info(fap, swap_type, 0); |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 667 | flash_area_close(fap); |
| 668 | return rc; |
| 669 | |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 670 | case BOOT_MAGIC_BAD: |
| 671 | /* The image slot is corrupt. There is no way to recover, so erase the |
| 672 | * slot to allow future upgrades. |
| 673 | */ |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 674 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap); |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 675 | if (rc != 0) { |
| 676 | return BOOT_EFLASH; |
| 677 | } |
| 678 | |
| 679 | flash_area_erase(fap, 0, fap->fa_size); |
| 680 | flash_area_close(fap); |
| 681 | return BOOT_EBADIMAGE; |
| 682 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 683 | default: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 684 | assert(0); |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 685 | return BOOT_EBADIMAGE; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | /** |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 690 | * Marks the image in the primary slot as confirmed. The system will continue |
| 691 | * booting into the image in the primary slot until told to boot from a |
| 692 | * different slot. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 693 | * |
| 694 | * @return 0 on success; nonzero on failure. |
| 695 | */ |
| 696 | int |
| 697 | boot_set_confirmed(void) |
| 698 | { |
| 699 | const struct flash_area *fap; |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 700 | struct boot_swap_state state_primary_slot; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 701 | int rc; |
| 702 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 703 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0), |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 704 | &state_primary_slot); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 705 | if (rc != 0) { |
| 706 | return rc; |
| 707 | } |
| 708 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 709 | switch (state_primary_slot.magic) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 710 | case BOOT_MAGIC_GOOD: |
| 711 | /* Confirm needed; proceed. */ |
| 712 | break; |
| 713 | |
| 714 | case BOOT_MAGIC_UNSET: |
| 715 | /* Already confirmed. */ |
| 716 | return 0; |
| 717 | |
| 718 | case BOOT_MAGIC_BAD: |
| 719 | /* Unexpected state. */ |
| 720 | return BOOT_EBADVECT; |
| 721 | } |
| 722 | |
Fabio Utzig | b0f0473 | 2019-07-31 09:49:19 -0300 | [diff] [blame] | 723 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 724 | if (rc) { |
| 725 | rc = BOOT_EFLASH; |
| 726 | goto done; |
| 727 | } |
| 728 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 729 | if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 730 | /* Swap never completed. This is unexpected. */ |
| 731 | rc = BOOT_EBADVECT; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 732 | goto done; |
| 733 | } |
| 734 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 735 | if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 736 | /* Already confirmed. */ |
| 737 | goto done; |
| 738 | } |
| 739 | |
| 740 | rc = boot_write_image_ok(fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 741 | |
| 742 | done: |
| 743 | flash_area_close(fap); |
| 744 | return rc; |
| 745 | } |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 746 | |
| 747 | #if (BOOT_IMAGE_NUMBER > 1) |
| 748 | /** |
| 749 | * Check if the version of the image is not older than required. |
| 750 | * |
| 751 | * @param req Required minimal image version. |
| 752 | * @param ver Version of the image to be checked. |
| 753 | * |
| 754 | * @return 0 if the version is sufficient, nonzero otherwise. |
| 755 | */ |
| 756 | int |
| 757 | boot_is_version_sufficient(struct image_version *req, |
| 758 | struct image_version *ver) |
| 759 | { |
| 760 | if (ver->iv_major > req->iv_major) { |
| 761 | return 0; |
| 762 | } |
| 763 | if (ver->iv_major < req->iv_major) { |
| 764 | return BOOT_EBADVERSION; |
| 765 | } |
| 766 | /* The major version numbers are equal. */ |
| 767 | if (ver->iv_minor > req->iv_minor) { |
| 768 | return 0; |
| 769 | } |
| 770 | if (ver->iv_minor < req->iv_minor) { |
| 771 | return BOOT_EBADVERSION; |
| 772 | } |
| 773 | /* The minor version numbers are equal. */ |
| 774 | if (ver->iv_revision < req->iv_revision) { |
| 775 | return BOOT_EBADVERSION; |
| 776 | } |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | #endif /* BOOT_IMAGE_NUMBER > 1 */ |