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 | #ifndef H_BOOTUTIL_PRIV_ |
| 21 | #define H_BOOTUTIL_PRIV_ |
| 22 | |
| 23 | #include "syscfg/syscfg.h" |
| 24 | #include "bootutil/image.h" |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | struct flash_area; |
| 31 | |
| 32 | #define BOOT_EFLASH 1 |
| 33 | #define BOOT_EFILE 2 |
| 34 | #define BOOT_EBADIMAGE 3 |
| 35 | #define BOOT_EBADVECT 4 |
| 36 | #define BOOT_EBADSTATUS 5 |
| 37 | #define BOOT_ENOMEM 6 |
| 38 | #define BOOT_EBADARGS 7 |
| 39 | |
| 40 | #define BOOT_TMPBUF_SZ 256 |
| 41 | |
| 42 | /* |
| 43 | * Maintain state of copy progress. |
| 44 | */ |
| 45 | struct boot_status { |
| 46 | uint32_t idx; /* Which area we're operating on */ |
| 47 | uint8_t state; /* Which part of the swapping process are we at */ |
| 48 | }; |
| 49 | |
| 50 | #define BOOT_MAGIC_GOOD 1 |
| 51 | #define BOOT_MAGIC_BAD 2 |
| 52 | #define BOOT_MAGIC_UNSET 3 |
| 53 | |
| 54 | /** |
| 55 | * End-of-image slot structure. |
| 56 | * |
| 57 | * 0 1 2 3 |
| 58 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 59 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 60 | * ~ MAGIC (16 octets) ~ |
| 61 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 62 | * ~ ~ |
| 63 | * ~ Swap status (variable, aligned) ~ |
| 64 | * ~ ~ |
| 65 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | * | Copy done | 0xff padding (up to min-write-sz - 1) ~ |
| 67 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 68 | * | Image OK | 0xff padding (up to min-write-sz - 1) ~ |
| 69 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | */ |
| 71 | |
| 72 | extern const uint32_t boot_img_magic[4]; |
| 73 | |
| 74 | struct boot_swap_state { |
| 75 | uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */ |
| 76 | uint8_t copy_done; |
| 77 | uint8_t image_ok; |
| 78 | }; |
| 79 | |
| 80 | #define BOOT_STATUS_STATE_COUNT 3 |
| 81 | #define BOOT_STATUS_MAX_ENTRIES 128 |
| 82 | |
| 83 | #define BOOT_STATUS_SOURCE_NONE 0 |
| 84 | #define BOOT_STATUS_SOURCE_SCRATCH 1 |
| 85 | #define BOOT_STATUS_SOURCE_SLOT0 2 |
| 86 | |
| 87 | int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen, |
| 88 | uint8_t key_id); |
| 89 | |
| 90 | uint32_t boot_trailer_sz(uint8_t min_write_sz); |
| 91 | uint32_t boot_status_off(const struct flash_area *fap); |
| 92 | int boot_read_swap_state(const struct flash_area *fap, |
| 93 | struct boot_swap_state *state); |
| 94 | int boot_read_swap_state_img(int slot, struct boot_swap_state *state); |
| 95 | int boot_read_swap_state_scratch(struct boot_swap_state *state); |
| 96 | int boot_write_magic(const struct flash_area *fap); |
| 97 | int boot_write_status(struct boot_status *bs); |
| 98 | int boot_schedule_test_swap(void); |
| 99 | int boot_write_copy_done(const struct flash_area *fap); |
| 100 | int boot_write_image_ok(const struct flash_area *fap); |
| 101 | |
| 102 | uint32_t boot_status_sz(uint8_t min_write_sz); |
| 103 | |
| 104 | #ifdef __cplusplus |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | #endif |
| 109 | |