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