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 | { |
| 103 | int i; |
| 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 | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 123 | BOOT_MAX_ALIGN * 2 /* copy_done + image_ok */ + |
| 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 */ |
| 131 | BOOT_MAX_ALIGN + /* image_ok */ |
| 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 | |
| 189 | int |
| 190 | boot_read_swap_state(const struct flash_area *fap, |
| 191 | struct boot_swap_state *state) |
| 192 | { |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 193 | uint32_t magic[BOOT_MAGIC_SZ]; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 194 | uint32_t off; |
| 195 | int rc; |
| 196 | |
| 197 | off = boot_magic_off(fap); |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 198 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 199 | if (rc != 0) { |
| 200 | return BOOT_EFLASH; |
| 201 | } |
| 202 | state->magic = boot_magic_code(magic); |
| 203 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 204 | if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) { |
| 205 | off = boot_copy_done_off(fap); |
| 206 | rc = flash_area_read(fap, off, &state->copy_done, 1); |
| 207 | if (rc != 0) { |
| 208 | return BOOT_EFLASH; |
| 209 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | off = boot_image_ok_off(fap); |
| 213 | rc = flash_area_read(fap, off, &state->image_ok, 1); |
| 214 | if (rc != 0) { |
| 215 | return BOOT_EFLASH; |
| 216 | } |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Reads the image trailer from the scratch area. |
| 223 | */ |
| 224 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 225 | 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] | 226 | { |
| 227 | const struct flash_area *fap; |
| 228 | int rc; |
| 229 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 230 | switch (flash_area_id) { |
| 231 | case FLASH_AREA_IMAGE_SCRATCH: |
| 232 | case FLASH_AREA_IMAGE_0: |
| 233 | case FLASH_AREA_IMAGE_1: |
| 234 | rc = flash_area_open(flash_area_id, &fap); |
| 235 | if (rc != 0) { |
| 236 | return BOOT_EFLASH; |
| 237 | } |
| 238 | break; |
| 239 | default: |
Fabio Utzig | 856f783 | 2017-05-22 11:04:44 -0400 | [diff] [blame] | 240 | return BOOT_EBADARGS; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | rc = boot_read_swap_state(fap, state); |
Fabio Utzig | acfba2e | 2017-05-22 11:06:29 -0400 | [diff] [blame] | 244 | flash_area_close(fap); |
| 245 | return rc; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | int |
| 249 | boot_write_magic(const struct flash_area *fap) |
| 250 | { |
| 251 | uint32_t off; |
| 252 | int rc; |
| 253 | |
| 254 | off = boot_magic_off(fap); |
| 255 | |
Fabio Utzig | 24a273d | 2017-04-20 08:21:31 -0300 | [diff] [blame] | 256 | rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 257 | if (rc != 0) { |
| 258 | return BOOT_EFLASH; |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 264 | static int |
| 265 | boot_write_flag(int flag, const struct flash_area *fap) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 266 | { |
| 267 | uint32_t off; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 268 | int rc; |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 269 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 270 | uint8_t align; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 271 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 272 | switch (flag) { |
| 273 | case BOOT_FLAG_COPY_DONE: |
| 274 | off = boot_copy_done_off(fap); |
| 275 | break; |
| 276 | case BOOT_FLAG_IMAGE_OK: |
| 277 | off = boot_image_ok_off(fap); |
| 278 | break; |
| 279 | default: |
Fabio Utzig | 856f783 | 2017-05-22 11:04:44 -0400 | [diff] [blame] | 280 | return BOOT_EBADARGS; |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 281 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 282 | |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 283 | align = hal_flash_align(fap->fa_device_id); |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 284 | assert(align <= BOOT_MAX_ALIGN); |
| 285 | memset(buf, 0xFF, BOOT_MAX_ALIGN); |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 286 | buf[0] = BOOT_FLAG_SET; |
David Brown | 9d72546 | 2017-01-23 15:50:58 -0700 | [diff] [blame] | 287 | |
| 288 | rc = flash_area_write(fap, off, buf, align); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 289 | if (rc != 0) { |
| 290 | return BOOT_EFLASH; |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | int |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 297 | boot_write_copy_done(const struct flash_area *fap) |
| 298 | { |
| 299 | return boot_write_flag(BOOT_FLAG_COPY_DONE, fap); |
| 300 | } |
| 301 | |
| 302 | int |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 303 | boot_write_image_ok(const struct flash_area *fap) |
| 304 | { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 305 | return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | int |
| 309 | boot_swap_type(void) |
| 310 | { |
| 311 | const struct boot_swap_table *table; |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 312 | struct boot_swap_state slot0; |
| 313 | struct boot_swap_state slot1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 314 | int rc; |
| 315 | int i; |
| 316 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 317 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &slot0); |
| 318 | if (rc) { |
| 319 | return BOOT_SWAP_TYPE_PANIC; |
| 320 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 321 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 322 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &slot1); |
| 323 | if (rc) { |
| 324 | return BOOT_SWAP_TYPE_PANIC; |
| 325 | } |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 326 | |
| 327 | for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { |
| 328 | table = boot_swap_tables + i; |
| 329 | |
Fabio Utzig | d7d2075 | 2017-07-13 16:03:25 -0300 | [diff] [blame^] | 330 | if ((!table->magic_slot0 || table->magic_slot0 == slot0.magic ) && |
| 331 | (!table->magic_slot1 || table->magic_slot1 == slot1.magic ) && |
| 332 | (!table->image_ok_slot0 || table->image_ok_slot0 == slot0.image_ok ) && |
| 333 | (!table->image_ok_slot1 || table->image_ok_slot1 == slot1.image_ok ) && |
| 334 | (!table->copy_done_slot0 || table->copy_done_slot0 == slot0.copy_done)) { |
Fabio Utzig | 34e393e | 2017-05-22 11:07:07 -0400 | [diff] [blame] | 335 | BOOT_LOG_INF("Swap type: %s", |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 336 | table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : |
| 337 | table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : |
| 338 | table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : |
| 339 | "BUG; can't happen"); |
| 340 | assert(table->swap_type == BOOT_SWAP_TYPE_TEST || |
| 341 | table->swap_type == BOOT_SWAP_TYPE_PERM || |
| 342 | table->swap_type == BOOT_SWAP_TYPE_REVERT); |
| 343 | return table->swap_type; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Fabio Utzig | b5b2f55 | 2017-06-30 10:03:47 -0300 | [diff] [blame] | 347 | BOOT_LOG_INF("Swap type: none"); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 348 | return BOOT_SWAP_TYPE_NONE; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Marks the image in slot 1 as pending. On the next reboot, the system will |
| 353 | * perform a one-time boot of the slot 1 image. |
| 354 | * |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 355 | * @param permanent Whether the image should be used permanently or |
| 356 | * only tested once: |
| 357 | * 0=run image once, then confirm or revert. |
| 358 | * 1=run image forever. |
| 359 | * |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 360 | * @return 0 on success; nonzero on failure. |
| 361 | */ |
| 362 | int |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 363 | boot_set_pending(int permanent) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 364 | { |
| 365 | const struct flash_area *fap; |
| 366 | struct boot_swap_state state_slot1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 367 | int rc; |
| 368 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 369 | 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] | 370 | if (rc != 0) { |
| 371 | return rc; |
| 372 | } |
| 373 | |
| 374 | switch (state_slot1.magic) { |
| 375 | case BOOT_MAGIC_GOOD: |
| 376 | /* Swap already scheduled. */ |
| 377 | return 0; |
| 378 | |
| 379 | case BOOT_MAGIC_UNSET: |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 380 | rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 381 | if (rc != 0) { |
| 382 | rc = BOOT_EFLASH; |
| 383 | } else { |
| 384 | rc = boot_write_magic(fap); |
| 385 | } |
| 386 | |
Christopher Collins | 7835c1e | 2016-12-21 10:10:51 -0800 | [diff] [blame] | 387 | if (rc == 0 && permanent) { |
| 388 | rc = boot_write_image_ok(fap); |
| 389 | } |
| 390 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 391 | flash_area_close(fap); |
| 392 | return rc; |
| 393 | |
| 394 | default: |
| 395 | /* XXX: Temporary assert. */ |
| 396 | assert(0); |
| 397 | return -1; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * 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. |
| 403 | * |
| 404 | * @return 0 on success; nonzero on failure. |
| 405 | */ |
| 406 | int |
| 407 | boot_set_confirmed(void) |
| 408 | { |
| 409 | const struct flash_area *fap; |
| 410 | struct boot_swap_state state_slot0; |
| 411 | int rc; |
| 412 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 413 | 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] | 414 | if (rc != 0) { |
| 415 | return rc; |
| 416 | } |
| 417 | |
| 418 | switch (state_slot0.magic) { |
| 419 | case BOOT_MAGIC_GOOD: |
| 420 | /* Confirm needed; proceed. */ |
| 421 | break; |
| 422 | |
| 423 | case BOOT_MAGIC_UNSET: |
| 424 | /* Already confirmed. */ |
| 425 | return 0; |
| 426 | |
| 427 | case BOOT_MAGIC_BAD: |
| 428 | /* Unexpected state. */ |
| 429 | return BOOT_EBADVECT; |
| 430 | } |
| 431 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 432 | if (state_slot0.copy_done == BOOT_FLAG_UNSET) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 433 | /* Swap never completed. This is unexpected. */ |
| 434 | return BOOT_EBADVECT; |
| 435 | } |
| 436 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 437 | if (state_slot0.image_ok != BOOT_FLAG_UNSET) { |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 438 | /* Already confirmed. */ |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap); |
| 443 | if (rc) { |
| 444 | rc = BOOT_EFLASH; |
| 445 | goto done; |
| 446 | } |
| 447 | |
| 448 | rc = boot_write_image_ok(fap); |
| 449 | if (rc != 0) { |
| 450 | goto done; |
| 451 | } |
| 452 | |
| 453 | rc = 0; |
| 454 | |
| 455 | done: |
| 456 | flash_area_close(fap); |
| 457 | return rc; |
| 458 | } |