hdr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Kaspar Schleiser <kaspar@schleiser.de>
3  * 2018 Inria
4  * 2018 Freie Universität Berlin
5  *
6  * This file is subject to the terms and conditions of the GNU Lesser
7  * General Public License v2.1. See the file LICENSE in the top level
8  * directory for more details.
9  */
10 
11 #pragma once
12 
34 #include <assert.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
45 #define RIOTBOOT_MAGIC_V1 0x544f4952 /* "RIOT" */
46 
50 #define RIOTBOOT_MAGIC_V2 0x54304952 /* "RI0T" */
51 
52 #ifndef DOXYGEN
53 #if defined(MODULE_RIOTBOOT_HDR_V2)
54 # define RIOTBOOT_MAGIC RIOTBOOT_MAGIC_V2
55 #else
56 # define RIOTBOOT_MAGIC RIOTBOOT_MAGIC_V1
57 #endif
58 #endif
59 
63 #define RIOTBOOT_HDR_IMAGE_BOOT_COUNT_SHIFT 0u
67 #define RIOTBOOT_HDR_IMAGE_BOOT_COUNT_MASK (0x0000000f << RIOTBOOT_HDR_IMAGE_BOOT_COUNT_SHIFT)
71 #define RIOTBOOT_HDR_IMAGE_STATE_SHIFT 4u
75 #define RIOTBOOT_HDR_IMAGE_STATE_MASK (0x0000000f << RIOTBOOT_HDR_IMAGE_STATE_SHIFT)
79 #ifndef CONFIG_RIOTBOOT_MAX_ATTEMPTS
80 # define CONFIG_RIOTBOOT_MAX_ATTEMPTS 3u
81 #endif
82 
84  "CONFIG_RIOTBOOT_MAX_ATTEMPTS must be at least 1");
85 
87  "CONFIG_RIOTBOOT_MAX_ATTEMPTS must be <= 4");
88 
92 typedef enum riotboot_hdr_img_state {
99 
104  uint32_t magic_number;
105  uint32_t version;
106  uint32_t start_addr;
107  uint32_t chksum;
108 };
109 
114  uint32_t magic_number;
115  uint32_t version;
116  uint32_t start_addr;
117  uint32_t chksum;
124  uint32_t flags;
125 };
126 
130 typedef union riotboot_hdr {
131  struct riotboot_hdr_v1 v1;
132  struct riotboot_hdr_v2 v2;
134 
143 
152 
161 
170 
179 
187 
197 
206 
215 
224 
232  unsigned boot_count);
233 
243 
251 
252 #ifdef __cplusplus
253 }
254 #endif
POSIX.1-2008 compliant version of the assert macro.
#define static_assert(cond,...)
static_assert for c-version < c11
Definition: assert.h:163
bool riotboot_hdr_is_v2(const riotboot_hdr_t *riotboot_hdr)
Check if the header is version 2.
union riotboot_hdr riotboot_hdr_t
Union to store riotboot headers in different versions.
uint32_t riotboot_hdr_get_start_addr(const riotboot_hdr_t *riotboot_hdr)
Getter for riotboot start address.
int riotboot_hdr_validate(const riotboot_hdr_t *riotboot_hdr)
Validate image header.
uint32_t riotboot_hdr_get_flags(const riotboot_hdr_t *riotboot_hdr)
Getter for riotboot flags.
unsigned riotboot_hdr_get_boot_count(const riotboot_hdr_t *riotboot_hdr)
Get current image boot count.
void riotboot_hdr_print(const riotboot_hdr_t *riotboot_hdr)
Print formatted riotboot_hdr_t to STDIO.
enum riotboot_hdr_img_state riotboot_hdr_img_state_t
Image state enumeration.
int riotboot_hdr_get_img_state(const riotboot_hdr_t *riotboot_hdr)
Get image state.
uint32_t riotboot_hdr_get_version(const riotboot_hdr_t *riotboot_hdr)
Getter for riotboot version.
uint32_t riotboot_hdr_checksum(const riotboot_hdr_t *riotboot_hdr)
Calculate header checksum.
#define CONFIG_RIOTBOOT_MAX_ATTEMPTS
Maximum number of attempts to boot a tentative image before reverting to the previous one.
Definition: hdr.h:80
int riotboot_hdr_set_img_state(riotboot_hdr_t *riotboot_hdr, riotboot_hdr_img_state_t state)
Set image state.
void riotboot_hdr_set_boot_count(riotboot_hdr_t *riotboot_hdr, unsigned boot_count)
Set image boot count.
uint32_t riotboot_hdr_get_checksum(const riotboot_hdr_t *riotboot_hdr)
Getter for the header checksum.
uint32_t riotboot_hdr_get_magic_number(const riotboot_hdr_t *riotboot_hdr)
Getter for riotboot magic number.
riotboot_hdr_img_state
Image state enumeration.
Definition: hdr.h:92
@ RIOTBOOT_HDR_IMG_STATE_DISMISSED
Image is dismissed (0000)
Definition: hdr.h:97
@ RIOTBOOT_HDR_IMG_STATE_INSTALLED
Image is installed (1111)
Definition: hdr.h:93
@ RIOTBOOT_HDR_IMG_STATE_ACTIVATED
Image is activated (1100)
Definition: hdr.h:95
@ RIOTBOOT_HDR_IMG_STATE_CONFIRMED
Image is confirmed (1000)
Definition: hdr.h:96
@ RIOTBOOT_HDR_IMG_STATE_DEACTIVATED
Image is deactivated (1110)
Definition: hdr.h:94
Structure to store image header - All members are little endian.
Definition: hdr.h:103
uint32_t version
Integer representing the partition version
Definition: hdr.h:105
uint32_t start_addr
Address after the allocated space for the header.
Definition: hdr.h:106
uint32_t magic_number
Header magic number
Definition: hdr.h:104
uint32_t chksum
Checksum of riotboot_hdr
Definition: hdr.h:107
Structure to store image header v2 - All members are little endian.
Definition: hdr.h:113
uint32_t chksum
Checksum of riotboot_hdr
Definition: hdr.h:117
uint32_t version
Integer representing the partition version
Definition: hdr.h:115
uint32_t flags
General purpose flags for image state and boot count.
Definition: hdr.h:124
uint32_t magic_number
Header magic number
Definition: hdr.h:114
uint32_t start_addr
Address after the allocated space for the header
Definition: hdr.h:116
Union to store riotboot headers in different versions.
Definition: hdr.h:130
struct riotboot_hdr_v2 v2
Version 2 header.
Definition: hdr.h:132
struct riotboot_hdr_v1 v1
Version 1 header.
Definition: hdr.h:131