|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * Copyright (c) 2017-2019 Linaro LTD |
| 5 | + * Copyright (c) 2016-2019 JUUL Labs |
| 6 | + * Copyright (c) 2019-2020 Arm Limited |
| 7 | + * Copyright (c) 2020 Nordic Semiconductor ASA |
| 8 | + * |
| 9 | + * Original license: |
| 10 | + * |
| 11 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 12 | + * or more contributor license agreements. See the NOTICE file |
| 13 | + * distributed with this work for additional information |
| 14 | + * regarding copyright ownership. The ASF licenses this file |
| 15 | + * to you under the Apache License, Version 2.0 (the |
| 16 | + * "License"); you may not use this file except in compliance |
| 17 | + * with the License. You may obtain a copy of the License at |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | + * |
| 21 | + * Unless required by applicable law or agreed to in writing, |
| 22 | + * software distributed under the License is distributed on an |
| 23 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 24 | + * KIND, either express or implied. See the License for the |
| 25 | + * specific language governing permissions and limitations |
| 26 | + * under the License. |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * @file |
| 31 | + * @brief Public MCUBoot interface API |
| 32 | + * |
| 33 | + * This file contains API which can be combined with the application in order |
| 34 | + * to interact with the MCUBoot bootloader. This API are shared code-base betwen |
| 35 | + * MCUBoot and the application which controls DFU process. |
| 36 | + */ |
| 37 | + |
| 38 | +#ifndef H_BOOTUTIL_PUBLIC |
| 39 | +#define H_BOOTUTIL_PUBLIC |
| 40 | + |
| 41 | +#include <inttypes.h> |
| 42 | +#include <string.h> |
| 43 | +#include <flash_map_backend/flash_map_backend.h> |
| 44 | + |
| 45 | +#ifdef __cplusplus |
| 46 | +extern "C" { |
| 47 | +#endif |
| 48 | + |
| 49 | +/** Attempt to boot the contents of the primary slot. */ |
| 50 | +#define BOOT_SWAP_TYPE_NONE 1 |
| 51 | + |
| 52 | +/** |
| 53 | + * Swap to the secondary slot. |
| 54 | + * Absent a confirm command, revert back on next boot. |
| 55 | + */ |
| 56 | +#define BOOT_SWAP_TYPE_TEST 2 |
| 57 | + |
| 58 | +/** |
| 59 | + * Swap to the secondary slot, |
| 60 | + * and permanently switch to booting its contents. |
| 61 | + */ |
| 62 | +#define BOOT_SWAP_TYPE_PERM 3 |
| 63 | + |
| 64 | +/** Swap back to alternate slot. A confirm changes this state to NONE. */ |
| 65 | +#define BOOT_SWAP_TYPE_REVERT 4 |
| 66 | + |
| 67 | +/** Swap failed because image to be run is not valid */ |
| 68 | +#define BOOT_SWAP_TYPE_FAIL 5 |
| 69 | + |
| 70 | +/** Swapping encountered an unrecoverable error */ |
| 71 | +#define BOOT_SWAP_TYPE_PANIC 0xff |
| 72 | + |
| 73 | +#define BOOT_MAX_ALIGN 8 |
| 74 | + |
| 75 | +#define BOOT_MAGIC_GOOD 1 |
| 76 | +#define BOOT_MAGIC_BAD 2 |
| 77 | +#define BOOT_MAGIC_UNSET 3 |
| 78 | +#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */ |
| 79 | +#define BOOT_MAGIC_NOTGOOD 5 /* NOTE: control only, not dependent on sector */ |
| 80 | + |
| 81 | +/* |
| 82 | + * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash! |
| 83 | + */ |
| 84 | +#define BOOT_FLAG_SET 1 |
| 85 | +#define BOOT_FLAG_BAD 2 |
| 86 | +#define BOOT_FLAG_UNSET 3 |
| 87 | +#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */ |
| 88 | + |
| 89 | +#define BOOT_MAGIC_SZ (sizeof boot_img_magic) |
| 90 | + |
| 91 | +struct boot_swap_state { |
| 92 | + uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */ |
| 93 | + uint8_t swap_type; /* One of the BOOT_SWAP_TYPE_[...] values. */ |
| 94 | + uint8_t copy_done; /* One of the BOOT_FLAG_[...] values. */ |
| 95 | + uint8_t image_ok; /* One of the BOOT_FLAG_[...] values. */ |
| 96 | + uint8_t image_num; /* Boot status belongs to this image */ |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Determines the action, if any, that mcuboot will take on a image pair. |
| 101 | + * |
| 102 | + * @param image_index Image pair index. |
| 103 | + * |
| 104 | + * @return a BOOT_SWAP_TYPE_[...] constant on success, negative errno code on |
| 105 | + * fail. |
| 106 | + */ |
| 107 | +int boot_swap_type_multi(int image_index); |
| 108 | + |
| 109 | +/** |
| 110 | + * @brief Determines the action, if any, that mcuboot will take. |
| 111 | + * |
| 112 | + * Works the same as a boot_swap_type_multi(0) call; |
| 113 | + * |
| 114 | + * @return a BOOT_SWAP_TYPE_[...] constant on success, negative errno code on |
| 115 | + * fail. |
| 116 | + */ |
| 117 | +int boot_swap_type(void); |
| 118 | + |
| 119 | +/** |
| 120 | + * Marks the image in the secondary slot as pending. On the next reboot, |
| 121 | + * the system will perform a one-time boot of the the secondary slot image. |
| 122 | + * |
| 123 | + * @param permanent Whether the image should be used permanently or |
| 124 | + * only tested once: |
| 125 | + * 0=run image once, then confirm or revert. |
| 126 | + * 1=run image forever. |
| 127 | + * |
| 128 | + * @return 0 on success; nonzero on failure. |
| 129 | + */ |
| 130 | +int boot_set_pending(int permanent); |
| 131 | + |
| 132 | +/** |
| 133 | + * @brief Marks the image in the primary slot as confirmed. |
| 134 | + * |
| 135 | + * The system will continue booting into the image in the primary slot until |
| 136 | + * told to boot from a different slot. |
| 137 | + * |
| 138 | + * @return 0 on success; nonzero on failure. |
| 139 | + */ |
| 140 | +int boot_set_confirmed(void); |
| 141 | + |
| 142 | +/** |
| 143 | + * @brief Get offset of the swap info field in the image trailer. |
| 144 | + * |
| 145 | + * @param fap Flash are for which offset is determined. |
| 146 | + * |
| 147 | + * @retval offset of the swap info field. |
| 148 | + */ |
| 149 | +uint32_t boot_swap_info_off(const struct flash_area *fap); |
| 150 | + |
| 151 | +/** |
| 152 | + * @brief Get value of image-ok flag of the image. |
| 153 | + * |
| 154 | + * If called from chin-loaded image the image-ok flag value can be used to check |
| 155 | + * whether application itself is already confirmed. |
| 156 | + * |
| 157 | + * @param fap Flash area of the image. |
| 158 | + * @param image_ok[out] image-ok value. |
| 159 | + * |
| 160 | + * @return 0 on success; nonzero on failure. |
| 161 | + */ |
| 162 | +int boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok); |
| 163 | + |
| 164 | +/** |
| 165 | + * @brief Read the image swap state |
| 166 | + * |
| 167 | + * @param flash_area_id Id of flash parttition from which trailer will be read. |
| 168 | + * @param state Struture for holding swap state. |
| 169 | + * |
| 170 | + * @return 0 on success, nonzero errno code on fail. |
| 171 | + */ |
| 172 | +int |
| 173 | +boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state); |
| 174 | + |
| 175 | +#define BOOT_MAGIC_ARR_SZ \ |
| 176 | + (sizeof boot_img_magic / sizeof boot_img_magic[0]) |
| 177 | + |
| 178 | +extern const uint32_t boot_img_magic[4]; |
| 179 | + |
| 180 | +#ifdef __cplusplus |
| 181 | +} |
| 182 | +#endif |
| 183 | + |
| 184 | +#endif |
0 commit comments