Skip to content

Commit 0639a2a

Browse files
michallencppisaCynerd
authored andcommitted
boot/nxboot: enhance bootloader capabilities and decision logic
This commit enhances the bootloader capabilities. The image's header is extended with header version, size, platform identifier and pointer to optional next header. CRC32 now includes part of the header in its calculation as well. The change also avoids having two different magics for image uploaded over programmer and update image. Both these images have the same magic and this magic is changed internally by the bootloader's logic. The change is needed because image with standard magic is automatically considered as a confirmed image (uploaded with programmer). The current implementation avoids tails at all, therefore the user application uploading the image does not have to erase the tail before new upload. The image is considered as confirmed if it has standard magic or its recovery is present. This means the bootloader has to erase the header of the update image after the update is done (to avoid update loop and to mark the image as unstable). This page is written back during the confirmation. This is a breaking change, but necessary for the future development of the bootloader. The added header version field will allow to add minor/major updates while keeping the backwards compatibility. Signed-off-by: Michal Lenc <[email protected]> Co-authored-by: Pavel Pisa <[email protected]> Co-authored-by: Karel Koci <[email protected]>
1 parent 3b03636 commit 0639a2a

File tree

6 files changed

+409
-271
lines changed

6 files changed

+409
-271
lines changed

boot/nxboot/Kconfig

+10-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ config NXBOOT_HEADER_SIZE
4343
Note that this size should be aligned with the program memory write
4444
page size!
4545

46+
config NXBOOT_PLATFORM_IDENTIFIER
47+
hex "A unique platform identifier"
48+
default 0x0
49+
---help---
50+
This is a unique platform identifier used by the bootloader to
51+
verify whether the image should be run on a given platform. An update
52+
(or even a firmware uploaded via a programmer) is rejected if the
53+
value in image's header doesn't match this option.
54+
4655
config NXBOOT_BOOTLOADER
4756
bool "Build nxboot bootloader application"
4857
default n
@@ -80,7 +89,7 @@ config NXBOOT_PREVENT_DOWNGRADE
8089
performed only for newer versions (according to Semantic Version
8190
preference rules).
8291

83-
WARNING: NXboot currently implementes preferences only for
92+
WARNING: NXboot currently implements preferences only for
8493
MAJOR.MINOR.PATCH and ignores prerelease.
8594

8695
endif # NXBOOT_BOOTLOADER

boot/nxboot/include/nxboot.h

+64-22
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,32 @@
3939
#define NXBOOT_SECONDARY_SLOT_NUM (1)
4040
#define NXBOOT_TERTIARY_SLOT_NUM (2)
4141

42-
/* Offsets to write pages containing confirmed and updated flags. These
43-
* pages are located at the end of the partition, therefore index 0 means
44-
* the first page from the end.
45-
*/
46-
47-
#define NXBOOT_CONFIRMED_PAGE_INDEX (0)
48-
#define NXBOOT_UPDATED_PAGE_INDEX (1)
49-
50-
#define NXBOOT_HEADER_MAGIC 0x534f584e /* NXOS. */
51-
#define NXBOOT_HEADER_MAGIC_INV 0xaca0abb1 /* NXOS inverted. This is used
52-
* for images uploaded directly
53-
* to the primary flash with
54-
* the debugger. These images
55-
* does not have precalculated
56-
* CRC and flags at the
57-
* end of the partition, but
58-
* are considered to be valid.
42+
#define NXBOOT_HEADER_MAGIC 0x534f584e /* NXOS. The NX images, both
43+
* uploaded directly to primary
44+
* partition via debugger and to
45+
* update via some application
46+
* are used with this magic. If
47+
* this image is uploaded to
48+
* primary flash, it is considered
49+
* valid.
5950
*/
51+
#define NXBOOT_HEADER_MAGIC_INT 0xaca0abb0 /* NXOS internal. This is used
52+
* for internal bootloader
53+
* handling and operations. It is
54+
* switch internally to distinguish
55+
* between images uploaded via
56+
* debugger or the ones updated
57+
* after the bootloader performed
58+
* its operation. The first two
59+
* bits are reserved to point
60+
* what partition is a recovery
61+
* for this image.
62+
*/
63+
64+
#define NXBOOT_HEADER_MAGIC_INT_MASK 0xfffffff0
65+
#define NXBOOT_RECOVERY_PTR_MASK 0x3
6066

61-
#define NXBOOT_HEADER_PRERELEASE_MAXLEN 110
67+
#define NXBOOT_HEADER_PRERELEASE_MAXLEN 94
6268

6369
/****************************************************************************
6470
* Public Types
@@ -84,11 +90,30 @@ struct nxboot_img_version
8490
char pre_release[NXBOOT_HEADER_PRERELEASE_MAXLEN]; /* Additional pre-release version */
8591
};
8692

93+
struct nxboot_hdr_version
94+
{
95+
uint8_t major;
96+
uint8_t minor;
97+
};
98+
8799
struct nxboot_img_header
88100
{
89-
uint32_t magic; /* Header magic */
90-
uint32_t size; /* Image size (excluding the header) */
91-
uint32_t crc; /* CRC32 of image (excluding the header). */
101+
uint32_t magic; /* Header magic */
102+
struct nxboot_hdr_version hdr_version; /* Version of the header */
103+
104+
uint16_t header_size; /* Length of the header in bytes */
105+
uint32_t crc; /* CRC32 of image (excluding the previous
106+
* fields in header, but including the following
107+
* ones).
108+
*/
109+
uint32_t size; /* Image size (excluding the header) */
110+
uint64_t identifier; /* Platform identifier. An image is rejected
111+
* if it does not match the one set for
112+
* the bootloader in NXBOOT_PLATFORM_IDENTIFIER.
113+
*/
114+
uint32_t extd_hdr_ptr; /* Address of the next extended header.
115+
* This is a hook for future additional headers.
116+
*/
92117

93118
struct nxboot_img_version img_version; /* Image version */
94119
};
@@ -101,8 +126,9 @@ struct nxboot_state
101126
int update; /* Number of update slot */
102127
int recovery; /* Number of recovery slot */
103128
bool recovery_valid; /* True if recovery image contains valid recovery */
129+
bool recovery_present; /* True if the image in primary has a recovery */
104130
bool primary_confirmed; /* True if primary slot is confirmed */
105-
enum nxboot_update_type next_boot; /* True if update slot has a valid image */
131+
enum nxboot_update_type next_boot; /* nxboot_update_type with next operation */
106132
};
107133

108134
/****************************************************************************
@@ -128,6 +154,22 @@ struct nxboot_state
128154

129155
int nxboot_get_state(struct nxboot_state *state);
130156

157+
/****************************************************************************
158+
* Name: nxboot_open_update_partition
159+
*
160+
* Description:
161+
* Gets the current bootloader state and opens the partition to which an
162+
* update image should be stored. It returns the valid file descriptor to
163+
* this partition, the user is responsible for writing to it and closing
164+
* if afterwards.
165+
*
166+
* Returned Value:
167+
* Valid file descriptor on success, -1 and sets errno on failure.
168+
*
169+
****************************************************************************/
170+
171+
int nxboot_open_update_partition(void);
172+
131173
/****************************************************************************
132174
* Name: nxboot_get_confirm
133175
*

0 commit comments

Comments
 (0)