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