Skip to content

Commit f42a820

Browse files
oyvindronningstadd3zd3z
authored andcommitted
serial_recovery: Replace CBOR decoding code with generated code
- Add the cddl_gen repository as a submodule. - Add a CDDL description file for the serial recovery packets to be decoded. - Add generated code files and cddl_gen's CBOR library to CMakeList.txt for Zephyr. - Convert boot_serial.c to use the new code. - Add a bash script to (re)generate code files using cddl_gen.py. Serial recovery should work exactly as before, but the binary should be about 1k smaller. Signed-off-by: Øyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
1 parent fab12e0 commit f42a820

File tree

6 files changed

+94
-97
lines changed

6 files changed

+94
-97
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
[submodule "boot/cypress/libs/cy-mbedtls-acceleration"]
1717
path = boot/cypress/libs/cy-mbedtls-acceleration
1818
url = https://github.com/cypresssemiconductorco/cy-mbedtls-acceleration.git
19+
[submodule "boot/boot_serial/src/cddl_gen"]
20+
path = ext/cddl_gen
21+
url = https://github.com/oyvindronningstad/cddl_gen.git

boot/boot_serial/src/boot_serial.c

+29-97
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include <crc/crc16.h>
4444
#include <base64/base64.h>
4545
#include <tinycbor/cbor.h>
46-
#include <tinycbor/cbor_buf_reader.h>
4746
#endif /* __ZEPHYR__ */
4847

4948
#include <flash_map_backend/flash_map_backend.h>
@@ -60,6 +59,8 @@
6059
#include "bootutil_priv.h"
6160
#endif
6261

62+
#include "serial_recovery_cbor.h"
63+
6364
MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
6465

6566
#define BOOT_SERIAL_INPUT_MAX 512
@@ -226,26 +227,20 @@ bs_list(char *buf, int len)
226227
static void
227228
bs_upload(char *buf, int len)
228229
{
229-
CborParser parser;
230-
struct cbor_buf_reader reader;
231-
struct CborValue root_value;
232-
struct CborValue value;
233-
uint8_t img_data[512];
230+
const uint8_t *img_data = NULL;
234231
long long int off = UINT_MAX;
235232
size_t img_blen = 0;
236233
uint8_t rem_bytes;
237234
long long int data_len = UINT_MAX;
238235
int img_num;
239236
size_t slen;
240-
char name_str[8];
241237
const struct flash_area *fap = NULL;
242238
int rc;
243239
#ifdef CONFIG_BOOT_ERASE_PROGRESSIVELY
244240
static off_t off_last = -1;
245241
struct flash_sector sector;
246242
#endif
247243

248-
memset(img_data, 0, sizeof(img_data));
249244
img_num = 0;
250245

251246
/*
@@ -258,99 +253,36 @@ bs_upload(char *buf, int len)
258253
* }
259254
*/
260255

261-
/*
262-
* Object comes within { ... }
263-
*/
264-
cbor_buf_reader_init(&reader, (uint8_t *)buf, len);
265-
cbor_parser_init(&reader.r, 0, &parser, &root_value);
266-
267-
if (!cbor_value_is_container(&root_value)) {
268-
goto out_invalid_data;
269-
}
270-
if (cbor_value_enter_container(&root_value, &value)) {
256+
Upload_t upload;
257+
if (!cbor_decode_Upload((const uint8_t *)buf, len, &upload)) {
271258
goto out_invalid_data;
272259
}
273-
while (cbor_value_is_valid(&value)) {
274-
/*
275-
* Decode key.
276-
*/
277-
if (cbor_value_calculate_string_length(&value, &slen)) {
278-
goto out_invalid_data;
279-
}
280-
if (!cbor_value_is_text_string(&value) ||
281-
slen >= sizeof(name_str) - 1) {
282-
goto out_invalid_data;
283-
}
284-
if (cbor_value_copy_text_string(&value, name_str, &slen, &value)) {
285-
goto out_invalid_data;
286-
}
287-
name_str[slen] = '\0';
288-
if (!strcmp(name_str, "data")) {
289-
/*
290-
* Image data
291-
*/
292-
if (value.type != CborByteStringType) {
293-
goto out_invalid_data;
294-
}
295-
if (cbor_value_calculate_string_length(&value, &slen) ||
296-
slen >= sizeof(img_data)) {
297-
goto out_invalid_data;
298-
}
299-
if (cbor_value_copy_byte_string(&value, img_data, &slen, &value)) {
300-
goto out_invalid_data;
301-
}
302-
img_blen = slen;
303-
} else if (!strcmp(name_str, "off")) {
304-
/*
305-
* Offset of the data.
306-
*/
307-
if (value.type != CborIntegerType) {
308-
goto out_invalid_data;
309-
}
310-
if (cbor_value_get_int64(&value, &off)) {
311-
goto out_invalid_data;
312-
}
313-
if (cbor_value_advance(&value)) {
314-
goto out_invalid_data;
315-
}
316-
} else if (!strcmp(name_str, "len")) {
317-
/*
318-
* Length of the image. This should only be present in the first
319-
* block of data; when offset is 0.
320-
*/
321-
if (value.type != CborIntegerType) {
322-
goto out_invalid_data;
323-
}
324-
if (cbor_value_get_int64(&value, &data_len)) {
325-
goto out_invalid_data;
326-
}
327-
if (cbor_value_advance(&value)) {
328-
goto out_invalid_data;
329-
}
330-
} else if (!strcmp(name_str, "image")) {
331-
/*
332-
* In a multi-image system, image number to upload to, if not
333-
* present will upload to slot 0 of image set 0.
334-
*/
335-
if (value.type != CborIntegerType) {
336-
goto out_invalid_data;
337-
}
338-
if (cbor_value_get_int(&value, &img_num)) {
339-
goto out_invalid_data;
340-
}
341-
if (cbor_value_advance(&value)) {
342-
goto out_invalid_data;
343-
}
344-
} else {
345-
/*
346-
* Unknown keys.
347-
*/
348-
if (cbor_value_advance(&value)) {
349-
goto out_invalid_data;
350-
}
260+
261+
for (int i = 0; i < upload._Upload_members_count; i++) {
262+
_Member_t *member = &upload._Upload_members[i];
263+
switch(member->_Member_choice) {
264+
case _Member_image:
265+
img_num = member->_Member_image;
266+
break;
267+
case _Member_data:
268+
img_data = member->_Member_data.value;
269+
slen = member->_Member_data.len;
270+
img_blen = slen;
271+
break;
272+
case _Member_len:
273+
data_len = member->_Member_len;
274+
break;
275+
case _Member_off:
276+
off = member->_Member_off;
277+
break;
278+
case _Member_sha:
279+
default:
280+
/* Nothing to do. */
281+
break;
351282
}
352283
}
353-
if (off == UINT_MAX) {
284+
285+
if (off == UINT_MAX || img_data == NULL) {
354286
/*
355287
* Offset must be set in every block.
356288
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
3+
if [ "$1" == "--help" ] || [ "$1" == "" ]; then
4+
echo "Regenerate serial_recovery_cbor.c|h if the cddl_gen submodule is updated."
5+
echo "Usage: $0 <copyright>"
6+
echo " e.g. $0 \"2020 Nordic Semiconductor ASA\""
7+
exit -1
8+
fi
9+
10+
add_copy_notice() {
11+
echo "$(printf '/*
12+
* This file has been %s from the cddl_gen submodule.
13+
* Commit %s
14+
*/
15+
16+
' "$2" "$(git -C ../../../ext/cddl_gen rev-parse HEAD)"; cat $1;)" > $1
17+
}
18+
19+
echo "Copying cbor_decode.c|h"
20+
copy_with_copy_notice() {
21+
cp $1 $2
22+
add_copy_notice $2 "copied"
23+
}
24+
25+
copy_with_copy_notice ../../../ext/cddl_gen/src/cbor_decode.c cbor_decode.c
26+
copy_with_copy_notice ../../../ext/cddl_gen/include/cbor_decode.h cbor_decode.h cbor_decode.h
27+
28+
echo "Generating serial_recovery_cbor.c|h"
29+
python3 ../../../ext/cddl_gen/scripts/cddl_gen.py -i serial_recovery.cddl -t Upload --oc serial_recovery_cbor.c --oh serial_recovery_cbor.h --time-header
30+
31+
add_copyright() {
32+
echo "$(printf '/*
33+
* Copyright (c) %s
34+
*
35+
* SPDX-License-Identifier: Apache-2.0
36+
*/
37+
38+
' "$2"; cat $1;)" > $1
39+
}
40+
41+
add_copyright serial_recovery_cbor.c "$1"
42+
add_copyright serial_recovery_cbor.h "$1"
43+
add_copy_notice serial_recovery_cbor.c "generated"
44+
add_copy_notice serial_recovery_cbor.h "generated"
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
;
2+
; Copyright (c) 2020 Nordic Semiconductor ASA
3+
;
4+
; SPDX-License-Identifier: Apache-2.0
5+
;
6+
7+
Member = ("image" => int) /
8+
("data" => bstr) /
9+
("len" => int) /
10+
("off" => int) /
11+
("sha" => bstr)
12+
13+
Upload = {
14+
3**5members: Member
15+
}

boot/zephyr/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ endif()
191191
if(CONFIG_MCUBOOT_SERIAL)
192192
zephyr_sources(${BOOT_DIR}/zephyr/serial_adapter.c)
193193
zephyr_sources(${BOOT_DIR}/boot_serial/src/boot_serial.c)
194+
zephyr_sources(${BOOT_DIR}/boot_serial/src/serial_recovery_cbor.c)
195+
zephyr_sources(${BOOT_DIR}/boot_serial/src/cbor_decode.c)
194196

195197
zephyr_include_directories(${BOOT_DIR}/bootutil/include)
196198
zephyr_include_directories(${BOOT_DIR}/boot_serial/include)

ext/cddl_gen

Submodule cddl_gen added at 9d911cf

0 commit comments

Comments
 (0)