|
| 1 | +/**************************************************************************** |
| 2 | + * apps/boot/nxboot/include/nxboot.h |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +#ifndef __BOOT_NXBOOT_INCLUDE_NXBOOT_H |
| 22 | +#define __BOOT_NXBOOT_INCLUDE_NXBOOT_H |
| 23 | + |
| 24 | +/**************************************************************************** |
| 25 | + * Included Files |
| 26 | + ****************************************************************************/ |
| 27 | + |
| 28 | +#include <nuttx/config.h> |
| 29 | +#include <assert.h> |
| 30 | +#include <stdbool.h> |
| 31 | + |
| 32 | +/**************************************************************************** |
| 33 | + * Pre-processor Definitions |
| 34 | + ****************************************************************************/ |
| 35 | + |
| 36 | +#define NXBOOT_PRIMARY_SLOT_NUM (0) |
| 37 | +#define NXBOOT_SECONDARY_SLOT_NUM (1) |
| 38 | +#define NXBOOT_TERTIARY_SLOT_NUM (2) |
| 39 | + |
| 40 | +/* Offsets to write pages containing confirmed and updated flags. These |
| 41 | + * pages are located at the end of the partition, therefore index 0 means |
| 42 | + * the first page from the end. |
| 43 | + */ |
| 44 | + |
| 45 | +#define NXBOOT_CONFIRMED_PAGE_INDEX (0) |
| 46 | +#define NXBOOT_UPDATED_PAGE_INDEX (1) |
| 47 | + |
| 48 | +#define NXBOOT_HEADER_MAGIC 0x534f584e /* NXOS. */ |
| 49 | +#define NXBOOT_HEADER_MAGIC_INV 0xaca0abb1 /* NXOS inverted. This is used |
| 50 | + * for images uploaded directly |
| 51 | + * to the primary flash with |
| 52 | + * the debugger. These images |
| 53 | + * does not have precalculated |
| 54 | + * CRC and flags at the |
| 55 | + * end of the partition, but |
| 56 | + * are considered to be valid. |
| 57 | + */ |
| 58 | + |
| 59 | +#define NXBOOT_HEADER_PRERELEASE_MAXLEN 110 |
| 60 | + |
| 61 | +/**************************************************************************** |
| 62 | + * Public Types |
| 63 | + ****************************************************************************/ |
| 64 | + |
| 65 | +enum nxboot_update_type |
| 66 | +{ |
| 67 | + NXBOOT_UPDATE_TYPE_NONE = 0, /* No action to do */ |
| 68 | + NXBOOT_UPDATE_TYPE_UPDATE = 1, /* Update will take place upon reboot */ |
| 69 | + NXBOOT_UPDATE_TYPE_REVERT = 2, /* Revert will take place upon reboot */ |
| 70 | +}; |
| 71 | + |
| 72 | +/* Versioning is according to Semantic Versioning 2.0.0 |
| 73 | + * refer to (https://semver.org/spec/v2.0.0.html) |
| 74 | + */ |
| 75 | + |
| 76 | +struct nxboot_img_version |
| 77 | +{ |
| 78 | + uint16_t major; /* MAJOR version */ |
| 79 | + uint16_t minor; /* MINOR version */ |
| 80 | + uint16_t patch; /* PATCH version */ |
| 81 | + |
| 82 | + char pre_release[NXBOOT_HEADER_PRERELEASE_MAXLEN]; /* Additional pre-release version */ |
| 83 | +}; |
| 84 | + |
| 85 | +struct nxboot_img_header |
| 86 | +{ |
| 87 | + uint32_t magic; /* Header magic */ |
| 88 | + uint32_t size; /* Image size (excluding the header) */ |
| 89 | + uint32_t crc; /* CRC32 of image (excluding the header). */ |
| 90 | + |
| 91 | + struct nxboot_img_version img_version; /* Image version */ |
| 92 | +}; |
| 93 | +static_assert(CONFIG_NXBOOT_HEADER_SIZE > sizeof(struct nxboot_img_header), |
| 94 | + "CONFIG_NXBOOT_HEADER_SIZE has to be larger than" |
| 95 | + "sizeof(struct nxboot_img_header)"); |
| 96 | + |
| 97 | +struct nxboot_state |
| 98 | +{ |
| 99 | + int update; /* Number of update slot */ |
| 100 | + int recovery; /* Number of recovery slot */ |
| 101 | + bool recovery_valid; /* True if recovery image contains valid recovery */ |
| 102 | + bool primary_confirmed; /* True if primary slot is confirmed */ |
| 103 | + enum nxboot_update_type next_boot; /* True if update slot has a valid image */ |
| 104 | +}; |
| 105 | + |
| 106 | +/**************************************************************************** |
| 107 | + * Public Function Prototypes |
| 108 | + ****************************************************************************/ |
| 109 | + |
| 110 | +/**************************************************************************** |
| 111 | + * Name: nxboot_get_state |
| 112 | + * |
| 113 | + * Description: |
| 114 | + * Gets the current bootloader state and stores it in the nxboot_state |
| 115 | + * structure passed as an argument. This function may be used to determine |
| 116 | + * which slot is update slot and where should application save incoming |
| 117 | + * firmware. |
| 118 | + * |
| 119 | + * Input parameters: |
| 120 | + * state: The pointer to nxboot_state structure. The state is stored here. |
| 121 | + * |
| 122 | + * Returned Value: |
| 123 | + * 0 on success, -1 and sets errno on failure. |
| 124 | + * |
| 125 | + ****************************************************************************/ |
| 126 | + |
| 127 | +int nxboot_get_state(struct nxboot_state *state); |
| 128 | + |
| 129 | +/**************************************************************************** |
| 130 | + * Name: nxboot_get_confirm |
| 131 | + * |
| 132 | + * Description: |
| 133 | + * This function can be used to determine whether primary image is |
| 134 | + * confirmed or not. This provides more direct access to confirm |
| 135 | + * state compared to nxboot_get_state function that returns the full |
| 136 | + * state of the bootloader. |
| 137 | + * |
| 138 | + * Returned Value: |
| 139 | + * 1 means confirmed, 0 not confirmed, -1 and sets errno on failure. |
| 140 | + * |
| 141 | + ****************************************************************************/ |
| 142 | + |
| 143 | +int nxboot_get_confirm(void); |
| 144 | + |
| 145 | +/**************************************************************************** |
| 146 | + * Name: nxboot_confirm |
| 147 | + * |
| 148 | + * Description: |
| 149 | + * Confirms the image currently located in primary partition and marks |
| 150 | + * its copy in update partition as a recovery. |
| 151 | + * |
| 152 | + * Returned Value: |
| 153 | + * 0 on success, -1 and sets errno on failure. |
| 154 | + * |
| 155 | + ****************************************************************************/ |
| 156 | + |
| 157 | +int nxboot_confirm(void); |
| 158 | + |
| 159 | +/**************************************************************************** |
| 160 | + * Name: nxboot_perform_swap |
| 161 | + * |
| 162 | + * Description: |
| 163 | + * Checks for the possible firmware update and performs it by copying |
| 164 | + * update image to primary slot or recovery image to primary slot in case |
| 165 | + * of the revert. In any situation, this function ends with the valid |
| 166 | + * image in primary slot. |
| 167 | + * |
| 168 | + * This is an entry point function that should be called from the |
| 169 | + * bootloader application. |
| 170 | + * |
| 171 | + * Input parameters: |
| 172 | + * check_only: Only repairs corrupted update, but do not start another one |
| 173 | + * |
| 174 | + * Returned Value: |
| 175 | + * 0 on success, -1 and sets errno on failure. |
| 176 | + * |
| 177 | + ****************************************************************************/ |
| 178 | + |
| 179 | +int nxboot_perform_update(bool check_only); |
| 180 | + |
| 181 | +#endif /* __BOOT_NXBOOT_INCLUDE_NXBOOT_H */ |
0 commit comments