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 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 23 | #include "bootutil/image.h" |
| 24 | |
| 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | struct flash_area; |
| 30 | |
| 31 | #define BOOT_EFLASH 1 |
| 32 | #define BOOT_EFILE 2 |
| 33 | #define BOOT_EBADIMAGE 3 |
| 34 | #define BOOT_EBADVECT 4 |
| 35 | #define BOOT_EBADSTATUS 5 |
| 36 | #define BOOT_ENOMEM 6 |
| 37 | #define BOOT_EBADARGS 7 |
| 38 | |
| 39 | #define BOOT_TMPBUF_SZ 256 |
| 40 | |
Fabio Utzig | 644b8d4 | 2017-04-20 07:56:05 -0300 | [diff] [blame] | 41 | #define BOOT_MAX_ALIGN 8 |
| 42 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 43 | /* |
| 44 | * Maintain state of copy progress. |
| 45 | */ |
| 46 | struct boot_status { |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 47 | uint32_t idx; /* Which area we're operating on */ |
| 48 | uint8_t state; /* Which part of the swapping process are we at */ |
| 49 | uint8_t use_scratch; /* Are status bytes ever written to scratch? */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | #define BOOT_MAGIC_GOOD 1 |
| 53 | #define BOOT_MAGIC_BAD 2 |
| 54 | #define BOOT_MAGIC_UNSET 3 |
| 55 | |
| 56 | /** |
| 57 | * End-of-image slot structure. |
| 58 | * |
| 59 | * 0 1 2 3 |
| 60 | * 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 |
| 61 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 62 | * ~ MAGIC (16 octets) ~ |
| 63 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 64 | * ~ ~ |
| 65 | * ~ Swap status (variable, aligned) ~ |
| 66 | * ~ ~ |
| 67 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 68 | * | Copy done | 0xff padding (up to min-write-sz - 1) ~ |
| 69 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | * | Image OK | 0xff padding (up to min-write-sz - 1) ~ |
| 71 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 72 | */ |
| 73 | |
| 74 | extern const uint32_t boot_img_magic[4]; |
| 75 | |
| 76 | struct boot_swap_state { |
| 77 | uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */ |
| 78 | uint8_t copy_done; |
| 79 | uint8_t image_ok; |
| 80 | }; |
| 81 | |
| 82 | #define BOOT_STATUS_STATE_COUNT 3 |
| 83 | #define BOOT_STATUS_MAX_ENTRIES 128 |
| 84 | |
| 85 | #define BOOT_STATUS_SOURCE_NONE 0 |
| 86 | #define BOOT_STATUS_SOURCE_SCRATCH 1 |
| 87 | #define BOOT_STATUS_SOURCE_SLOT0 2 |
| 88 | |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 89 | #define BOOT_FLAG_IMAGE_OK 0 |
| 90 | #define BOOT_FLAG_COPY_DONE 1 |
| 91 | |
Fabio Utzig | de8a38a | 2017-05-23 11:15:01 -0400 | [diff] [blame] | 92 | #define BOOT_FLAG_SET 0x01 |
| 93 | #define BOOT_FLAG_UNSET 0xff |
Fabio Utzig | ba49f84 | 2017-05-22 10:52:38 -0400 | [diff] [blame] | 94 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 95 | extern const uint32_t BOOT_MAGIC_SZ; |
| 96 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 97 | int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen, |
| 98 | uint8_t key_id); |
| 99 | |
Fabio Utzig | 7ebb7c2 | 2017-04-26 10:59:31 -0300 | [diff] [blame] | 100 | uint32_t boot_slots_trailer_sz(uint8_t min_write_sz); |
Fabio Utzig | 4cee4f7 | 2017-05-22 10:59:57 -0400 | [diff] [blame] | 101 | int boot_status_entries(const struct flash_area *fap); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 102 | uint32_t boot_status_off(const struct flash_area *fap); |
| 103 | int boot_read_swap_state(const struct flash_area *fap, |
| 104 | struct boot_swap_state *state); |
Fabio Utzig | 2473ac0 | 2017-05-02 12:45:02 -0300 | [diff] [blame] | 105 | int boot_read_swap_state_by_id(int flash_area_id, |
| 106 | struct boot_swap_state *state); |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 107 | int boot_write_magic(const struct flash_area *fap); |
| 108 | int boot_write_status(struct boot_status *bs); |
| 109 | int boot_schedule_test_swap(void); |
| 110 | int boot_write_copy_done(const struct flash_area *fap); |
| 111 | int boot_write_image_ok(const struct flash_area *fap); |
| 112 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 113 | #ifdef __cplusplus |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | #endif |
| 118 | |