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