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 | |
| 20 | #include <assert.h> |
| 21 | #include <string.h> |
| 22 | #include <inttypes.h> |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 23 | #include <stddef.h> |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 24 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 25 | #include "sysflash/sysflash.h" |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 26 | #include "flash_map_backend/flash_map_backend.h" |
| 27 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 28 | #include "bootutil/image.h" |
| 29 | #include "bootutil/bootutil.h" |
| 30 | #include "bootutil_priv.h" |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 31 | #include "bootutil/bootutil_log.h" |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 32 | #ifdef MCUBOOT_ENC_IMAGES |
| 33 | #include "bootutil/enc_key.h" |
| 34 | #endif |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 35 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 36 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 37 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 38 | int boot_current_slot; |
| 39 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 40 | const uint32_t boot_img_magic[] = { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 41 | 0xf395c277, |
| 42 | 0x7fefd260, |
| 43 | 0x0f505235, |
| 44 | 0x8079b62c, |
| 45 | }; |
| 46 | |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 47 | #define BOOT_MAGIC_ARR_SZ \ |
| 48 | (sizeof boot_img_magic / sizeof boot_img_magic[0]) |
| 49 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 50 | const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 51 | const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN; |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 52 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 53 | struct boot_swap_table { |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 54 | uint8_t magic_slot0; |
| 55 | uint8_t magic_slot1; |
| 56 | uint8_t image_ok_slot0; |
| 57 | uint8_t image_ok_slot1; |
Fabio Utzig | d7d2075 | 2017-07-13 16:03:25 -0300 | [diff] [blame] | 58 | uint8_t copy_done_slot0; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 59 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 60 | uint8_t swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * This set of tables maps image trailer contents to swap operation type. |
| 65 | * When searching for a match, these tables must be iterated sequentially. |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 66 | * |
| 67 | * NOTE: the table order is very important. The settings in Slot 1 always |
| 68 | * are priority to Slot 0 and should be located earlier in the table. |
| 69 | * |
| 70 | * The table lists only states where there is action needs to be taken by |
| 71 | * the bootloader, as in starting/finishing a swap operation. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 72 | */ |
| 73 | static const struct boot_swap_table boot_swap_tables[] = { |
| 74 | { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 75 | .magic_slot0 = BOOT_MAGIC_ANY, |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 76 | .magic_slot1 = BOOT_MAGIC_GOOD, |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 77 | .image_ok_slot0 = BOOT_FLAG_ANY, |
| 78 | .image_ok_slot1 = BOOT_FLAG_UNSET, |
| 79 | .copy_done_slot0 = BOOT_FLAG_ANY, |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 80 | .swap_type = BOOT_SWAP_TYPE_TEST, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 81 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 82 | { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 83 | .magic_slot0 = BOOT_MAGIC_ANY, |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 84 | .magic_slot1 = BOOT_MAGIC_GOOD, |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 85 | .image_ok_slot0 = BOOT_FLAG_ANY, |
| 86 | .image_ok_slot1 = BOOT_FLAG_SET, |
| 87 | .copy_done_slot0 = BOOT_FLAG_ANY, |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 88 | .swap_type = BOOT_SWAP_TYPE_PERM, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 89 | }, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 90 | { |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 91 | .magic_slot0 = BOOT_MAGIC_GOOD, |
| 92 | .magic_slot1 = BOOT_MAGIC_UNSET, |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 93 | .image_ok_slot0 = BOOT_FLAG_UNSET, |
| 94 | .image_ok_slot1 = BOOT_FLAG_ANY, |
| 95 | .copy_done_slot0 = BOOT_FLAG_SET, |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 96 | .swap_type = BOOT_SWAP_TYPE_REVERT, |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 97 | }, |
| 98 | }; |
| 99 | |
| 100 | #define BOOT_SWAP_TABLES_COUNT \ |
| 101 | (sizeof boot_swap_tables / sizeof boot_swap_tables[0]) |
| 102 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 103 | static int |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 104 | boot_magic_decode(const uint32_t *magic) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 105 | { |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 106 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 107 | return BOOT_MAGIC_GOOD; |
| 108 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 109 | return BOOT_MAGIC_BAD; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 112 | static int |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 113 | boot_flag_decode(uint8_t flag) |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 114 | { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 115 | if (flag != BOOT_FLAG_SET) { |
| 116 | return BOOT_FLAG_BAD; |
| 117 | } |
| 118 | return BOOT_FLAG_SET; |
| 119 | } |
| 120 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 121 | uint32_t |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 122 | boot_slots_trailer_sz(uint8_t min_write_sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 123 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 124 | return /* state for all sectors */ |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 125 | BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz + |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 126 | #ifdef MCUBOOT_ENC_IMAGES |
| 127 | /* encryption keys */ |
| 128 | BOOT_ENC_KEY_SIZE * 2 + |
| 129 | #endif |
| 130 | /* copy_done + image_ok + swap_size */ |
| 131 | BOOT_MAX_ALIGN * 3 + |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 132 | BOOT_MAGIC_SZ; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 135 | static uint32_t |
| 136 | boot_scratch_trailer_sz(uint8_t min_write_sz) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 137 | { |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 138 | /* state for one sector */ |
| 139 | return BOOT_STATUS_STATE_COUNT * min_write_sz + |
| 140 | #ifdef MCUBOOT_ENC_IMAGES |
| 141 | /* encryption keys */ |
| 142 | BOOT_ENC_KEY_SIZE * 2 + |
| 143 | #endif |
| 144 | /* image_ok + swap_size */ |
| 145 | BOOT_MAX_ALIGN * 2 + |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 146 | BOOT_MAGIC_SZ; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static uint32_t |
| 150 | boot_magic_off(const struct flash_area *fap) |
| 151 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 152 | assert(offsetof(struct image_trailer, magic) == 16); |
| 153 | return fap->fa_size - BOOT_MAGIC_SZ; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 156 | int |
| 157 | boot_status_entries(const struct flash_area *fap) |
| 158 | { |
| 159 | switch (fap->fa_id) { |
| 160 | case FLASH_AREA_IMAGE_0: |
| 161 | case FLASH_AREA_IMAGE_1: |
| 162 | return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES; |
| 163 | case FLASH_AREA_IMAGE_SCRATCH: |
| 164 | return BOOT_STATUS_STATE_COUNT; |
| 165 | default: |
| 166 | return BOOT_EBADARGS; |
| 167 | } |
| 168 | } |
| 169 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 170 | uint32_t |
| 171 | boot_status_off(const struct flash_area *fap) |
| 172 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 173 | uint32_t off_from_end; |
| 174 | uint8_t elem_sz; |
| 175 | |
| 176 | elem_sz = flash_area_align(fap); |
| 177 | |
| 178 | if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) { |
| 179 | off_from_end = boot_scratch_trailer_sz(elem_sz); |
| 180 | } else { |
| 181 | off_from_end = boot_slots_trailer_sz(elem_sz); |
| 182 | } |
| 183 | |
| 184 | assert(off_from_end <= fap->fa_size); |
| 185 | return fap->fa_size - off_from_end; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static uint32_t |
| 189 | boot_copy_done_off(const struct flash_area *fap) |
| 190 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 191 | assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH); |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 192 | assert(offsetof(struct image_trailer, copy_done) == 0); |
| 193 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static uint32_t |
| 197 | boot_image_ok_off(const struct flash_area *fap) |
| 198 | { |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 199 | assert(offsetof(struct image_trailer, image_ok) == 8); |
| 200 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 203 | static uint32_t |
| 204 | boot_swap_size_off(const struct flash_area *fap) |
| 205 | { |
| 206 | /* |
| 207 | * The "swap_size" field if located just before the trailer. |
| 208 | * The scratch slot doesn't store "copy_done"... |
| 209 | */ |
| 210 | if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) { |
| 211 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2; |
| 212 | } |
| 213 | |
| 214 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3; |
| 215 | } |
| 216 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 217 | #ifdef MCUBOOT_ENC_IMAGES |
| 218 | static uint32_t |
| 219 | boot_enc_key_off(const struct flash_area *fap, uint8_t slot) |
| 220 | { |
| 221 | if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) { |
| 222 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2 - |
| 223 | ((slot + 1) * BOOT_ENC_KEY_SIZE); |
| 224 | } |
| 225 | |
| 226 | return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3 - |
| 227 | ((slot + 1) * BOOT_ENC_KEY_SIZE); |
| 228 | } |
| 229 | #endif |
| 230 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 231 | int |
| 232 | boot_read_swap_state(const struct flash_area *fap, |
| 233 | struct boot_swap_state *state) |
| 234 | { |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 235 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 236 | uint32_t off; |
| 237 | int rc; |
| 238 | |
| 239 | off = boot_magic_off(fap); |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 240 | rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ); |
| 241 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 242 | return BOOT_EFLASH; |
| 243 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 244 | if (rc == 1) { |
| 245 | state->magic = BOOT_MAGIC_UNSET; |
| 246 | } else { |
| 247 | state->magic = boot_magic_decode(magic); |
| 248 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 249 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 250 | if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) { |
| 251 | off = boot_copy_done_off(fap); |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 252 | rc = flash_area_read_is_empty(fap, off, &state->copy_done, |
| 253 | sizeof state->copy_done); |
| 254 | if (rc < 0) { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 255 | return BOOT_EFLASH; |
| 256 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 257 | if (rc == 1) { |
| 258 | state->copy_done = BOOT_FLAG_UNSET; |
| 259 | } else { |
| 260 | state->copy_done = boot_flag_decode(state->copy_done); |
| 261 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | off = boot_image_ok_off(fap); |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 265 | rc = flash_area_read_is_empty(fap, off, &state->image_ok, sizeof state->image_ok); |
| 266 | if (rc < 0) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 267 | return BOOT_EFLASH; |
| 268 | } |
Fabio Utzig | 178be54 | 2018-09-19 08:12:56 -0300 | [diff] [blame] | 269 | if (rc == 1) { |
| 270 | state->image_ok = BOOT_FLAG_UNSET; |
| 271 | } else { |
| 272 | state->image_ok = boot_flag_decode(state->image_ok); |
| 273 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Reads the image trailer from the scratch area. |
| 280 | */ |
| 281 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 282 | 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] | 283 | { |
| 284 | const struct flash_area *fap; |
| 285 | int rc; |
| 286 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 287 | switch (flash_area_id) { |
| 288 | case FLASH_AREA_IMAGE_SCRATCH: |
| 289 | case FLASH_AREA_IMAGE_0: |
| 290 | case FLASH_AREA_IMAGE_1: |
| 291 | rc = flash_area_open(flash_area_id, &fap); |
| 292 | if (rc != 0) { |
| 293 | return BOOT_EFLASH; |
| 294 | } |
| 295 | break; |
| 296 | default: |
Fabio Utzig | 856f783 | 2017-05-22 11:04:44 -0400 | [diff] [blame] | 297 | return BOOT_EBADARGS; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | rc = boot_read_swap_state(fap, state); |
Fabio Utzig | acfba2e | 2017-05-22 11:06:29 -0400 | [diff] [blame] | 301 | flash_area_close(fap); |
| 302 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | int |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 306 | boot_read_swap_size(uint32_t *swap_size) |
| 307 | { |
Hovland, Sigvart | 1d96f36 | 2018-09-25 13:23:42 +0200 | [diff] [blame] | 308 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 309 | uint32_t off; |
| 310 | const struct flash_area *fap; |
| 311 | int rc; |
| 312 | |
| 313 | /* |
| 314 | * In the middle a swap, tries to locate the saved swap size. Looks |
| 315 | * for a valid magic, first on Slot 0, then on scratch. Both "slots" |
| 316 | * can end up being temporary storage for a swap and it is assumed |
| 317 | * that if magic is valid then swap size is too, because magic is |
| 318 | * always written in the last step. |
| 319 | */ |
| 320 | |
| 321 | rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); |
| 322 | if (rc != 0) { |
| 323 | return BOOT_EFLASH; |
| 324 | } |
| 325 | |
| 326 | off = boot_magic_off(fap); |
| 327 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 328 | if (rc != 0) { |
| 329 | rc = BOOT_EFLASH; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) { |
| 334 | /* |
| 335 | * If Slot 0 's magic is not valid, try scratch... |
| 336 | */ |
| 337 | |
| 338 | flash_area_close(fap); |
| 339 | |
| 340 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap); |
| 341 | if (rc != 0) { |
| 342 | return BOOT_EFLASH; |
| 343 | } |
| 344 | |
| 345 | off = boot_magic_off(fap); |
| 346 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 347 | if (rc != 0) { |
| 348 | rc = BOOT_EFLASH; |
| 349 | goto out; |
| 350 | } |
| 351 | |
| 352 | assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0); |
| 353 | } |
| 354 | |
| 355 | off = boot_swap_size_off(fap); |
| 356 | rc = flash_area_read(fap, off, swap_size, sizeof *swap_size); |
| 357 | if (rc != 0) { |
| 358 | rc = BOOT_EFLASH; |
| 359 | } |
| 360 | |
| 361 | out: |
| 362 | flash_area_close(fap); |
| 363 | return rc; |
| 364 | } |
| 365 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 366 | #ifdef MCUBOOT_ENC_IMAGES |
| 367 | int |
| 368 | boot_read_enc_key(uint8_t slot, uint8_t *enckey) |
| 369 | { |
| 370 | uint32_t magic[BOOT_MAGIC_SZ]; |
| 371 | uint32_t off; |
| 372 | const struct flash_area *fap; |
| 373 | int rc; |
| 374 | |
| 375 | rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); |
| 376 | if (rc != 0) { |
| 377 | return BOOT_EFLASH; |
| 378 | } |
| 379 | |
| 380 | off = boot_magic_off(fap); |
| 381 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 382 | if (rc != 0) { |
| 383 | rc = BOOT_EFLASH; |
| 384 | goto out; |
| 385 | } |
| 386 | |
| 387 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) { |
| 388 | /* |
| 389 | * If Slot 0 's magic is not valid, try scratch... |
| 390 | */ |
| 391 | |
| 392 | flash_area_close(fap); |
| 393 | |
| 394 | rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap); |
| 395 | if (rc != 0) { |
| 396 | return BOOT_EFLASH; |
| 397 | } |
| 398 | |
| 399 | off = boot_magic_off(fap); |
| 400 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
| 401 | if (rc != 0) { |
| 402 | rc = BOOT_EFLASH; |
| 403 | goto out; |
| 404 | } |
| 405 | |
| 406 | assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0); |
| 407 | } |
| 408 | |
| 409 | off = boot_enc_key_off(fap, slot); |
| 410 | rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE); |
| 411 | if (rc != 0) { |
| 412 | rc = BOOT_EFLASH; |
| 413 | } |
| 414 | |
| 415 | out: |
| 416 | flash_area_close(fap); |
| 417 | return rc; |
| 418 | } |
| 419 | #endif |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 420 | |
| 421 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 422 | boot_write_magic(const struct flash_area *fap) |
| 423 | { |
| 424 | uint32_t off; |
| 425 | int rc; |
| 426 | |
| 427 | off = boot_magic_off(fap); |
| 428 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 429 | rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 430 | if (rc != 0) { |
| 431 | return BOOT_EFLASH; |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 437 | static int |
| 438 | boot_write_flag(int flag, const struct flash_area *fap) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 439 | { |
| 440 | uint32_t off; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 441 | int rc; |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 442 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 443 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 444 | uint8_t erased_val; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 445 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 446 | switch (flag) { |
| 447 | case BOOT_FLAG_COPY_DONE: |
| 448 | off = boot_copy_done_off(fap); |
| 449 | break; |
| 450 | case BOOT_FLAG_IMAGE_OK: |
| 451 | off = boot_image_ok_off(fap); |
| 452 | break; |
| 453 | default: |
Fabio Utzig | 856f783 | 2017-05-22 11:04:44 -0400 | [diff] [blame] | 454 | return BOOT_EBADARGS; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 455 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 456 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 457 | align = flash_area_align(fap); |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 458 | assert(align <= BOOT_MAX_ALIGN); |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 459 | erased_val = flash_area_erased_val(fap); |
| 460 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 461 | buf[0] = BOOT_FLAG_SET; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 462 | |
| 463 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 464 | if (rc != 0) { |
| 465 | return BOOT_EFLASH; |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 472 | boot_write_copy_done(const struct flash_area *fap) |
| 473 | { |
| 474 | return boot_write_flag(BOOT_FLAG_COPY_DONE, fap); |
| 475 | } |
| 476 | |
| 477 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 478 | boot_write_image_ok(const struct flash_area *fap) |
| 479 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 480 | return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | int |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 484 | boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size) |
| 485 | { |
| 486 | uint32_t off; |
| 487 | int rc; |
| 488 | uint8_t buf[BOOT_MAX_ALIGN]; |
| 489 | uint8_t align; |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 490 | uint8_t erased_val; |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 491 | |
| 492 | off = boot_swap_size_off(fap); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 493 | align = flash_area_align(fap); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 494 | assert(align <= BOOT_MAX_ALIGN); |
| 495 | if (align < sizeof swap_size) { |
| 496 | align = sizeof swap_size; |
| 497 | } |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 498 | erased_val = flash_area_erased_val(fap); |
| 499 | memset(buf, erased_val, BOOT_MAX_ALIGN); |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 500 | memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size); |
| 501 | |
| 502 | rc = flash_area_write(fap, off, buf, align); |
| 503 | if (rc != 0) { |
| 504 | return BOOT_EFLASH; |
| 505 | } |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
Fabio Utzig | ba82904 | 2018-09-18 08:29:34 -0300 | [diff] [blame] | 510 | #ifdef MCUBOOT_ENC_IMAGES |
| 511 | int |
| 512 | boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey) |
| 513 | { |
| 514 | uint32_t off; |
| 515 | int rc; |
| 516 | |
| 517 | off = boot_enc_key_off(fap, slot); |
| 518 | rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE); |
| 519 | if (rc != 0) { |
| 520 | return BOOT_EFLASH; |
| 521 | } |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | #endif |
| 526 | |
Fabio Utzig | 4649072 | 2017-09-04 15:34:32 -0300 | [diff] [blame] | 527 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 528 | boot_swap_type(void) |
| 529 | { |
| 530 | const struct boot_swap_table *table; |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 531 | struct boot_swap_state slot0; |
| 532 | struct boot_swap_state slot1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 533 | int rc; |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 534 | size_t i; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 535 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 536 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0); |
| 537 | if (rc) { |
| 538 | return BOOT_SWAP_TYPE_PANIC; |
| 539 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 540 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 541 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1); |
| 542 | if (rc) { |
| 543 | return BOOT_SWAP_TYPE_PANIC; |
| 544 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 545 | |
| 546 | for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { |
| 547 | table = boot_swap_tables + i; |
| 548 | |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 549 | if ((table->magic_slot0 == BOOT_MAGIC_ANY || |
| 550 | table->magic_slot0 == slot0.magic) && |
| 551 | (table->magic_slot1 == BOOT_MAGIC_ANY || |
| 552 | table->magic_slot1 == slot1.magic) && |
| 553 | (table->image_ok_slot0 == BOOT_FLAG_ANY || |
| 554 | table->image_ok_slot0 == slot0.image_ok) && |
| 555 | (table->image_ok_slot1 == BOOT_FLAG_ANY || |
| 556 | table->image_ok_slot1 == slot1.image_ok) && |
| 557 | (table->copy_done_slot0 == BOOT_FLAG_ANY || |
| 558 | table->copy_done_slot0 == slot0.copy_done)) { |
Fabio Utzig | 34e393e | 2017-05-22 11:07:07 -0400 | [diff] [blame] | 559 | BOOT_LOG_INF("Swap type: %s", |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 560 | table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : |
| 561 | table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : |
| 562 | table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : |
| 563 | "BUG; can't happen"); |
| 564 | assert(table->swap_type == BOOT_SWAP_TYPE_TEST || |
| 565 | table->swap_type == BOOT_SWAP_TYPE_PERM || |
| 566 | table->swap_type == BOOT_SWAP_TYPE_REVERT); |
| 567 | return table->swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 571 | BOOT_LOG_INF("Swap type: none"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 572 | return BOOT_SWAP_TYPE_NONE; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Marks the image in slot 1 as pending. On the next reboot, the system will |
| 577 | * perform a one-time boot of the slot 1 image. |
| 578 | * |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 579 | * @param permanent Whether the image should be used permanently or |
| 580 | * only tested once: |
| 581 | * 0=run image once, then confirm or revert. |
| 582 | * 1=run image forever. |
| 583 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 584 | * @return 0 on success; nonzero on failure. |
| 585 | */ |
| 586 | int |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 587 | boot_set_pending(int permanent) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 588 | { |
| 589 | const struct flash_area *fap; |
| 590 | struct boot_swap_state state_slot1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 591 | int rc; |
| 592 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 593 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 594 | if (rc != 0) { |
| 595 | return rc; |
| 596 | } |
| 597 | |
| 598 | switch (state_slot1.magic) { |
| 599 | case BOOT_MAGIC_GOOD: |
| 600 | /* Swap already scheduled. */ |
| 601 | return 0; |
| 602 | |
| 603 | case BOOT_MAGIC_UNSET: |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 604 | rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 605 | if (rc != 0) { |
| 606 | rc = BOOT_EFLASH; |
| 607 | } else { |
| 608 | rc = boot_write_magic(fap); |
| 609 | } |
| 610 | |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 611 | if (rc == 0 && permanent) { |
| 612 | rc = boot_write_image_ok(fap); |
| 613 | } |
| 614 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 615 | flash_area_close(fap); |
| 616 | return rc; |
| 617 | |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 618 | case BOOT_MAGIC_BAD: |
| 619 | /* The image slot is corrupt. There is no way to recover, so erase the |
| 620 | * slot to allow future upgrades. |
| 621 | */ |
| 622 | rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap); |
| 623 | if (rc != 0) { |
| 624 | return BOOT_EFLASH; |
| 625 | } |
| 626 | |
| 627 | flash_area_erase(fap, 0, fap->fa_size); |
| 628 | flash_area_close(fap); |
| 629 | return BOOT_EBADIMAGE; |
| 630 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 631 | default: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 632 | assert(0); |
Christopher Collins | ae01f15 | 2019-01-30 09:56:37 -0800 | [diff] [blame] | 633 | return BOOT_EBADIMAGE; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Marks the image in slot 0 as confirmed. The system will continue booting into the image in slot 0 until told to boot from a different slot. |
| 639 | * |
| 640 | * @return 0 on success; nonzero on failure. |
| 641 | */ |
| 642 | int |
| 643 | boot_set_confirmed(void) |
| 644 | { |
| 645 | const struct flash_area *fap; |
| 646 | struct boot_swap_state state_slot0; |
| 647 | int rc; |
| 648 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 649 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 650 | if (rc != 0) { |
| 651 | return rc; |
| 652 | } |
| 653 | |
| 654 | switch (state_slot0.magic) { |
| 655 | case BOOT_MAGIC_GOOD: |
| 656 | /* Confirm needed; proceed. */ |
| 657 | break; |
| 658 | |
| 659 | case BOOT_MAGIC_UNSET: |
| 660 | /* Already confirmed. */ |
| 661 | return 0; |
| 662 | |
| 663 | case BOOT_MAGIC_BAD: |
| 664 | /* Unexpected state. */ |
| 665 | return BOOT_EBADVECT; |
| 666 | } |
| 667 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 668 | rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); |
| 669 | if (rc) { |
| 670 | rc = BOOT_EFLASH; |
| 671 | goto done; |
| 672 | } |
| 673 | |
Fabio Utzig | 08fa267 | 2018-09-26 12:16:18 -0300 | [diff] [blame] | 674 | if (state_slot0.copy_done == BOOT_FLAG_UNSET) { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 675 | /* Swap never completed. This is unexpected. */ |
| 676 | rc = BOOT_EBADVECT; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 677 | goto done; |
| 678 | } |
| 679 | |
Ćukasz Rymanowski | a1927f4 | 2018-09-26 15:31:50 +0200 | [diff] [blame] | 680 | if (state_slot0.image_ok != BOOT_FLAG_UNSET) { |
Fabio Utzig | 3900001 | 2018-07-30 12:40:20 -0300 | [diff] [blame] | 681 | /* Already confirmed. */ |
| 682 | goto done; |
| 683 | } |
| 684 | |
| 685 | rc = boot_write_image_ok(fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 686 | |
| 687 | done: |
| 688 | flash_area_close(fap); |
| 689 | return rc; |
| 690 | } |