|
| 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 | +#define NXBOOT_IMAGE_STATE_CONFIRM 1 /* Flag set -> image confirmed */ |
| 41 | +#define NXBOOT_IMAGE_STATE_UPDATED 3 /* Flag set -> image was updated */ |
| 42 | +#define NXBOOT_IMAGE_STATE_AVAIL 4 /* Flag set -> partition available */ |
| 43 | +#define NXBOOT_IMAGE_STATE_UNSET 0xff /* Flag not set -> image not confirmed */ |
| 44 | + |
| 45 | +#define NXBOOT_HEADER_MAGIC 0x534f584e /* NXOS */ |
| 46 | +#define NXBOOT_HEADER_PRERELEASE_MAXLEN 109 |
| 47 | + |
| 48 | +/**************************************************************************** |
| 49 | + * Public Types |
| 50 | + ****************************************************************************/ |
| 51 | + |
| 52 | +enum nxboot_update_type |
| 53 | +{ |
| 54 | + NXBOOT_UPDATE_TYPE_NONE = 0, /* No action to do */ |
| 55 | + NXBOOT_UPDATE_TYPE_UPDATE = 1, /* Update will take place upon reboot */ |
| 56 | + NXBOOT_UPDATE_TYPE_REVERT = 2, /* Revert will take place upon reboot */ |
| 57 | +}; |
| 58 | + |
| 59 | +/* Versioning is according to Semantic Versioning 2.0.0 |
| 60 | + * refer to (https://semver.org/spec/v2.0.0.html) |
| 61 | + */ |
| 62 | + |
| 63 | +struct nxboot_img_version |
| 64 | +{ |
| 65 | + uint16_t major; /* MAJOR version */ |
| 66 | + uint16_t minor; /* MINOR version */ |
| 67 | + uint16_t patch; /* PATCH version */ |
| 68 | + |
| 69 | + char pre_release[NXBOOT_HEADER_PRERELEASE_MAXLEN]; /* Additional pre-release version */ |
| 70 | +}; |
| 71 | + |
| 72 | +struct nxboot_img_header |
| 73 | +{ |
| 74 | + uint32_t magic; /* Header magic */ |
| 75 | + uint32_t size; /* Image size (excluding the header) */ |
| 76 | + uint32_t crc32; /* CRC32 of image (excluding the header) */ |
| 77 | + uint8_t state; /* Image state */ |
| 78 | + |
| 79 | + struct nxboot_img_version img_version; /* Image version */ |
| 80 | +}; |
| 81 | +static_assert(CONFIG_NXBOOT_HEADER_SIZE > sizeof(struct nxboot_img_header), |
| 82 | + "CONFIG_NXBOOT_HEADER_SIZE has to be larger than" |
| 83 | + "sizeof(struct nxboot_img_header)"); |
| 84 | + |
| 85 | +struct nxboot_state |
| 86 | +{ |
| 87 | + int update; /* Number of update slot */ |
| 88 | + int recovery; /* Number of recovery slot */ |
| 89 | + bool recovery_valid; /* True if recovery image contains valid recovery */ |
| 90 | + bool primary_confirmed; /* True if primary slot is confirmed */ |
| 91 | + enum nxboot_update_type next_boot; /* True if update slot has a valid image */ |
| 92 | +}; |
| 93 | + |
| 94 | +/**************************************************************************** |
| 95 | + * Public Function Prototypes |
| 96 | + ****************************************************************************/ |
| 97 | + |
| 98 | +/**************************************************************************** |
| 99 | + * Name: nxboot_get_state |
| 100 | + * |
| 101 | + * Description: |
| 102 | + * Gets the current bootloader state and stores it in the nxboot_state |
| 103 | + * structure passed as an argument. This function may be used to determine |
| 104 | + * which slot is update slot and where should application save incoming |
| 105 | + * firmware. |
| 106 | + * |
| 107 | + * Input parameters: |
| 108 | + * state: The pointer to nxboot_state structure. The state is stored here. |
| 109 | + * |
| 110 | + * Returned Value: |
| 111 | + * 0 on success, -1 and sets errno on failure. |
| 112 | + * |
| 113 | + ****************************************************************************/ |
| 114 | + |
| 115 | +int nxboot_get_state(struct nxboot_state *state); |
| 116 | + |
| 117 | +/**************************************************************************** |
| 118 | + * Name: nxboot_get_confirm |
| 119 | + * |
| 120 | + * Description: |
| 121 | + * This function can be used to determine whether primary image is |
| 122 | + * confirmed or not. This provides more direct access to confirm |
| 123 | + * state compared to nxboot_get_state function that returns the full |
| 124 | + * state of the bootloader. |
| 125 | + * |
| 126 | + * Returned Value: |
| 127 | + * 1 means confirmed, 0 not confirmed, -1 and sets errno on failure. |
| 128 | + * |
| 129 | + ****************************************************************************/ |
| 130 | + |
| 131 | +int nxboot_get_confirm(void); |
| 132 | + |
| 133 | +/**************************************************************************** |
| 134 | + * Name: nxboot_confirm |
| 135 | + * |
| 136 | + * Description: |
| 137 | + * Confirms the image currently located in primary partition and marks |
| 138 | + * its copy in update partition as a recovery. |
| 139 | + * |
| 140 | + * Returned Value: |
| 141 | + * 0 on success, -1 and sets errno on failure. |
| 142 | + * |
| 143 | + ****************************************************************************/ |
| 144 | + |
| 145 | +int nxboot_confirm(void); |
| 146 | + |
| 147 | +/**************************************************************************** |
| 148 | + * Name: nxboot_perform_swap |
| 149 | + * |
| 150 | + * Description: |
| 151 | + * Checks for the possible firmware update and performs it by copying |
| 152 | + * update image to primary slot or recovery image to primary slot in case |
| 153 | + * of the revert. In any situation, this function ends with the valid |
| 154 | + * image in primary slot. |
| 155 | + * |
| 156 | + * This is an entry point function that should be called from the |
| 157 | + * bootloader application. |
| 158 | + * |
| 159 | + * Input parameters: |
| 160 | + * check_only: Only repairs corrupted update, but do not start another one |
| 161 | + * |
| 162 | + * Returned Value: |
| 163 | + * 0 on success, -1 and sets errno on failure. |
| 164 | + * |
| 165 | + ****************************************************************************/ |
| 166 | + |
| 167 | +int nxboot_perform_update(bool check_only); |
| 168 | + |
| 169 | +#endif /* __BOOT_NXBOOT_INCLUDE_NXBOOT_H */ |
0 commit comments