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