Skip to content

Commit 212a35b

Browse files
oyvindronningstadutzig
authored andcommitted
boot_serial: Update cddl-gen
Update and rename submodule. Regenerate code and copy updated files. Update regeneration script. Signed-off-by: Øyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
1 parent 55918ed commit 212a35b

13 files changed

+920
-436
lines changed

.gitmodules

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
[submodule "boot/cypress/libs/cy-mbedtls-acceleration"]
2020
path = boot/cypress/libs/cy-mbedtls-acceleration
2121
url = https://github.com/cypresssemiconductorco/cy-mbedtls-acceleration.git
22-
[submodule "boot/boot_serial/src/cddl_gen"]
23-
path = ext/cddl_gen
24-
url = https://github.com/oyvindronningstad/cddl_gen.git
22+
[submodule "ext/cddl-gen"]
23+
path = ext/cddl-gen
24+
url = https://github.com/NordicSemiconductor/cddl-gen.git

boot/boot_serial/src/boot_serial.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "bootutil/bootutil_log.h"
2828

2929
#ifdef __ZEPHYR__
30-
#include <sys/reboot.h>
30+
#include <power/reboot.h>
3131
#include <sys/byteorder.h>
3232
#include <sys/__assert.h>
3333
#include <drivers/flash.h>
@@ -254,13 +254,16 @@ bs_upload(char *buf, int len)
254254
* }
255255
*/
256256

257-
Upload_t upload;
258-
if (!cbor_decode_Upload((const uint8_t *)buf, len, &upload)) {
257+
struct Upload upload;
258+
size_t decoded_len;
259+
bool result = cbor_decode_Upload((const uint8_t *)buf, len, &upload, &decoded_len);
260+
261+
if (!result || (len != decoded_len)) {
259262
goto out_invalid_data;
260263
}
261264

262265
for (int i = 0; i < upload._Upload_members_count; i++) {
263-
_Member_t *member = &upload._Upload_members[i];
266+
struct Member_ *member = &upload._Upload_members[i];
264267
switch(member->_Member_choice) {
265268
case _Member_image:
266269
img_num = member->_Member_image;

boot/boot_serial/src/cbor_common.c

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* This file has been copied from the cddl-gen submodule.
3+
* Commit 8f9358a0b4b0e9b0cd579f0988056ef0b60760e4
4+
*/
5+
6+
/*
7+
* Copyright (c) 2020 Nordic Semiconductor ASA
8+
*
9+
* SPDX-License-Identifier: Apache-2.0
10+
*/
11+
12+
#include <stddef.h>
13+
#include <stdbool.h>
14+
#include <stdint.h>
15+
#include <string.h>
16+
#include "cbor_common.h"
17+
18+
_Static_assert((sizeof(size_t) == sizeof(void *)),
19+
"This code needs size_t to be the same length as pointers.");
20+
21+
bool new_backup(cbor_state_t *state, uint32_t new_elem_count)
22+
{
23+
if ((state->backups->current_backup + 1)
24+
>= state->backups->num_backups) {
25+
FAIL();
26+
}
27+
28+
uint32_t i = ++(state->backups->current_backup);
29+
memcpy(&state->backups->backup_list[i], state,
30+
sizeof(cbor_state_t));
31+
32+
state->elem_count = new_elem_count;
33+
34+
return true;
35+
}
36+
37+
38+
bool restore_backup(cbor_state_t *state, uint32_t flags,
39+
uint32_t max_elem_count)
40+
{
41+
const uint8_t *payload = state->payload;
42+
const uint32_t elem_count = state->elem_count;
43+
44+
if (state->backups->current_backup == 0) {
45+
FAIL();
46+
}
47+
48+
if (flags & FLAG_RESTORE) {
49+
uint32_t i = state->backups->current_backup;
50+
51+
memcpy(state, &state->backups->backup_list[i],
52+
sizeof(cbor_state_t));
53+
}
54+
55+
if (flags & FLAG_DISCARD) {
56+
state->backups->current_backup--;
57+
}
58+
59+
if (elem_count > max_elem_count) {
60+
cbor_print("elem_count: %d (expected max %d)\r\n",
61+
elem_count, max_elem_count);
62+
FAIL();
63+
}
64+
65+
if (flags & FLAG_TRANSFER_PAYLOAD) {
66+
state->payload = payload;
67+
}
68+
69+
return true;
70+
}
71+
72+
73+
bool union_start_code(cbor_state_t *state)
74+
{
75+
if (!new_backup(state, state->elem_count)) {
76+
FAIL();
77+
}
78+
return true;
79+
}
80+
81+
82+
bool union_elem_code(cbor_state_t *state)
83+
{
84+
if (!restore_backup(state, FLAG_RESTORE, state->elem_count)) {
85+
FAIL();
86+
}
87+
return true;
88+
}
89+
90+
bool union_end_code(cbor_state_t *state)
91+
{
92+
if (!restore_backup(state, FLAG_DISCARD, state->elem_count)) {
93+
FAIL();
94+
}
95+
return true;
96+
}
97+
98+
bool entry_function(const uint8_t *payload, uint32_t payload_len,
99+
const void *struct_ptr, uint32_t *payload_len_out,
100+
cbor_encoder_t func, uint32_t elem_count, uint32_t num_backups)
101+
{
102+
cbor_state_t state = {
103+
.payload = payload,
104+
.payload_end = payload + payload_len,
105+
.elem_count = elem_count,
106+
};
107+
108+
cbor_state_t state_backups[num_backups + 1];
109+
110+
cbor_state_backups_t backups = {
111+
.backup_list = state_backups,
112+
.current_backup = 0,
113+
.num_backups = num_backups + 1,
114+
};
115+
116+
state.backups = &backups;
117+
118+
bool result = func(&state, struct_ptr);
119+
120+
if (result && (payload_len_out != NULL)) {
121+
*payload_len_out = MIN(payload_len,
122+
(size_t)state.payload - (size_t)payload);
123+
}
124+
return result;
125+
}

boot/boot_serial/src/cbor_common.h

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* This file has been copied from the cddl-gen submodule.
3+
* Commit 8f9358a0b4b0e9b0cd579f0988056ef0b60760e4
4+
*/
5+
6+
/*
7+
* Copyright (c) 2020 Nordic Semiconductor ASA
8+
*
9+
* SPDX-License-Identifier: Apache-2.0
10+
*/
11+
12+
#ifndef CBOR_COMMON_H__
13+
#define CBOR_COMMON_H__
14+
#include <stdint.h>
15+
#include <stdbool.h>
16+
#include <stddef.h>
17+
18+
19+
/** Convenience type that allows pointing to strings directly inside the payload
20+
* without the need to copy out.
21+
*/
22+
typedef struct
23+
{
24+
const uint8_t *value;
25+
uint32_t len;
26+
} cbor_string_type_t;
27+
28+
#ifdef CDDL_CBOR_VERBOSE
29+
#include <sys/printk.h>
30+
#define cbor_trace() (printk("bytes left: %d, byte: 0x%x, elem_count: 0x%zx, %s:%d\n",\
31+
(uint32_t)state->payload_end - (uint32_t)state->payload, *state->payload, state->elem_count,\
32+
__FILE__, __LINE__))
33+
#define cbor_assert(expr, ...) \
34+
do { \
35+
if (!(expr)) { \
36+
printk("ASSERTION \n \"" #expr \
37+
"\"\nfailed at %s:%d with message:\n ", \
38+
__FILE__, __LINE__); \
39+
printk(__VA_ARGS__);\
40+
return false; \
41+
} \
42+
} while(0)
43+
#define cbor_print(...) printk(__VA_ARGS__)
44+
#else
45+
#define cbor_trace() ((void)state)
46+
#define cbor_assert(...)
47+
#define cbor_print(...)
48+
#endif
49+
50+
#ifndef MIN
51+
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
52+
#endif
53+
54+
55+
struct cbor_state_backups_s;
56+
57+
typedef struct cbor_state_backups_s cbor_state_backups_t;
58+
59+
typedef struct{
60+
union {
61+
uint8_t *payload_mut;
62+
uint8_t const *payload; /**< The current place in the payload. Will be
63+
updated when an element is correctly
64+
processed. */
65+
};
66+
uint8_t const *payload_bak; /**< Temporary backup of payload. */
67+
uint32_t elem_count; /**< The current element is part of a LIST or a MAP,
68+
and this keeps count of how many elements are
69+
expected. This will be checked before processing
70+
and decremented if the element is correctly
71+
processed. */
72+
uint8_t const *payload_end; /**< The end of the payload. This will be
73+
checked against payload before
74+
processing each element. */
75+
cbor_state_backups_t *backups;
76+
} cbor_state_t;
77+
78+
struct cbor_state_backups_s{
79+
cbor_state_t *backup_list;
80+
uint32_t current_backup;
81+
uint32_t num_backups;
82+
};
83+
84+
/** Function pointer type used with multi_decode.
85+
*
86+
* This type is compatible with all decoding functions here and in the generated
87+
* code, except for multi_decode.
88+
*/
89+
typedef bool(cbor_encoder_t)(cbor_state_t *, const void *);
90+
typedef bool(cbor_decoder_t)(cbor_state_t *, void *);
91+
92+
/** Enumeration representing the major types available in CBOR.
93+
*
94+
* The major type is represented in the 3 first bits of the header byte.
95+
*/
96+
typedef enum
97+
{
98+
CBOR_MAJOR_TYPE_PINT = 0, ///! Positive Integer
99+
CBOR_MAJOR_TYPE_NINT = 1, ///! Negative Integer
100+
CBOR_MAJOR_TYPE_BSTR = 2, ///! Byte String
101+
CBOR_MAJOR_TYPE_TSTR = 3, ///! Text String
102+
CBOR_MAJOR_TYPE_LIST = 4, ///! List
103+
CBOR_MAJOR_TYPE_MAP = 5, ///! Map
104+
CBOR_MAJOR_TYPE_TAG = 6, ///! Semantic Tag
105+
CBOR_MAJOR_TYPE_PRIM = 7, ///! Primitive Type
106+
} cbor_major_type_t;
107+
108+
/** Shorthand macro to check if a result is within min/max constraints.
109+
*/
110+
#define PTR_VALUE_IN_RANGE(type, res, min, max) \
111+
(((min == NULL) || (*(type *)res >= *(type *)min)) \
112+
&& ((max == NULL) || (*(type *)res <= *(type *)max)))
113+
114+
#define FAIL() \
115+
do {\
116+
cbor_trace(); \
117+
return false; \
118+
} while(0)
119+
120+
121+
#define VALUE_IN_HEADER 23 /**! For values below this, the value is encoded
122+
directly in the header. */
123+
124+
#define BOOL_TO_PRIM 20 ///! In CBOR, false/true have the values 20/21
125+
126+
#define FLAG_RESTORE 1UL
127+
#define FLAG_DISCARD 2UL
128+
#define FLAG_TRANSFER_PAYLOAD 4UL
129+
130+
bool new_backup(cbor_state_t *state, uint32_t new_elem_count);
131+
132+
bool restore_backup(cbor_state_t *state, uint32_t flags,
133+
uint32_t max_elem_count);
134+
135+
bool union_start_code(cbor_state_t *state);
136+
137+
bool union_elem_code(cbor_state_t *state);
138+
139+
bool union_end_code(cbor_state_t *state);
140+
141+
bool entry_function(const uint8_t *payload, uint32_t payload_len,
142+
const void *struct_ptr, uint32_t *payload_len_out,
143+
cbor_encoder_t func, uint32_t elem_count, uint32_t num_backups);
144+
145+
#endif /* CBOR_COMMON_H__ */

0 commit comments

Comments
 (0)