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