Commit 212a35bc authored by Øyvind Rønningstad's avatar Øyvind Rønningstad Committed by Fabio Utzig
Browse files

boot_serial: Update cddl-gen

Update and rename submodule.
Regenerate code and copy updated files.
Update regeneration script.

Signed-off-by: default avatarØyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
parent 55918edc
Loading
Loading
Loading
Loading
+3 −3
Changes for .gitmodules: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -19,6 +19,6 @@
[submodule "boot/cypress/libs/cy-mbedtls-acceleration"]
	path = boot/cypress/libs/cy-mbedtls-acceleration
	url = https://github.com/cypresssemiconductorco/cy-mbedtls-acceleration.git
[submodule "boot/boot_serial/src/cddl_gen"]
	path = ext/cddl_gen
	url = https://github.com/oyvindronningstad/cddl_gen.git
[submodule "ext/cddl-gen"]
	path = ext/cddl-gen
	url = https://github.com/NordicSemiconductor/cddl-gen.git
+7 −4
Changes for boot/boot_serial/src/boot_serial.c: 7 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@
#include "bootutil/bootutil_log.h"

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

    Upload_t upload;
    if (!cbor_decode_Upload((const uint8_t *)buf, len, &upload)) {
    struct Upload upload;
    size_t decoded_len;
    bool result = cbor_decode_Upload((const uint8_t *)buf, len, &upload, &decoded_len);

    if (!result || (len != decoded_len)) {
        goto out_invalid_data;
    }

    for (int i = 0; i < upload._Upload_members_count; i++) {
        _Member_t *member = &upload._Upload_members[i];
        struct Member_ *member = &upload._Upload_members[i];
        switch(member->_Member_choice) {
            case _Member_image:
                img_num = member->_Member_image;
+125 −0
Changes for boot/boot_serial/src/cbor_common.c: 125 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * This file has been copied from the cddl-gen submodule.
 * Commit 8f9358a0b4b0e9b0cd579f0988056ef0b60760e4
 */

/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "cbor_common.h"

_Static_assert((sizeof(size_t) == sizeof(void *)),
	"This code needs size_t to be the same length as pointers.");

bool new_backup(cbor_state_t *state, uint32_t new_elem_count)
{
	if ((state->backups->current_backup + 1)
		>= state->backups->num_backups) {
		FAIL();
	}

	uint32_t i = ++(state->backups->current_backup);
	memcpy(&state->backups->backup_list[i], state,
		sizeof(cbor_state_t));

	state->elem_count = new_elem_count;

	return true;
}


bool restore_backup(cbor_state_t *state, uint32_t flags,
		uint32_t max_elem_count)
{
	const uint8_t *payload = state->payload;
	const uint32_t elem_count = state->elem_count;

	if (state->backups->current_backup == 0) {
		FAIL();
	}

	if (flags & FLAG_RESTORE) {
		uint32_t i = state->backups->current_backup;

		memcpy(state, &state->backups->backup_list[i],
			sizeof(cbor_state_t));
	}

	if (flags & FLAG_DISCARD) {
		state->backups->current_backup--;
	}

	if (elem_count > max_elem_count) {
		cbor_print("elem_count: %d (expected max %d)\r\n",
			elem_count, max_elem_count);
		FAIL();
	}

	if (flags & FLAG_TRANSFER_PAYLOAD) {
		state->payload = payload;
	}

	return true;
}


bool union_start_code(cbor_state_t *state)
{
	if (!new_backup(state, state->elem_count)) {
		FAIL();
	}
	return true;
}


bool union_elem_code(cbor_state_t *state)
{
	if (!restore_backup(state, FLAG_RESTORE, state->elem_count)) {
		FAIL();
	}
	return true;
}

bool union_end_code(cbor_state_t *state)
{
	if (!restore_backup(state, FLAG_DISCARD, state->elem_count)) {
		FAIL();
	}
	return true;
}

bool entry_function(const uint8_t *payload, uint32_t payload_len,
		const void *struct_ptr, uint32_t *payload_len_out,
		cbor_encoder_t func, uint32_t elem_count, uint32_t num_backups)
{
	cbor_state_t state = {
		.payload = payload,
		.payload_end = payload + payload_len,
		.elem_count = elem_count,
	};

	cbor_state_t state_backups[num_backups + 1];

	cbor_state_backups_t backups = {
		.backup_list = state_backups,
		.current_backup = 0,
		.num_backups = num_backups + 1,
	};

	state.backups = &backups;

	bool result = func(&state, struct_ptr);

	if (result && (payload_len_out != NULL)) {
		*payload_len_out = MIN(payload_len,
				(size_t)state.payload - (size_t)payload);
	}
	return result;
}
+145 −0
Changes for boot/boot_serial/src/cbor_common.h: 145 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * This file has been copied from the cddl-gen submodule.
 * Commit 8f9358a0b4b0e9b0cd579f0988056ef0b60760e4
 */

/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef CBOR_COMMON_H__
#define CBOR_COMMON_H__
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>


/** Convenience type that allows pointing to strings directly inside the payload
 *  without the need to copy out.
 */
typedef struct
{
	const uint8_t *value;
	uint32_t len;
} cbor_string_type_t;

#ifdef CDDL_CBOR_VERBOSE
#include <sys/printk.h>
#define cbor_trace() (printk("bytes left: %d, byte: 0x%x, elem_count: 0x%zx, %s:%d\n",\
	(uint32_t)state->payload_end - (uint32_t)state->payload, *state->payload, state->elem_count,\
	__FILE__, __LINE__))
#define cbor_assert(expr, ...) \
do { \
	if (!(expr)) { \
		printk("ASSERTION \n  \"" #expr \
			"\"\nfailed at %s:%d with message:\n  ", \
			__FILE__, __LINE__); \
		printk(__VA_ARGS__);\
		return false; \
	} \
} while(0)
#define cbor_print(...) printk(__VA_ARGS__)
#else
#define cbor_trace() ((void)state)
#define cbor_assert(...)
#define cbor_print(...)
#endif

#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif


struct cbor_state_backups_s;

typedef struct cbor_state_backups_s cbor_state_backups_t;

typedef struct{
union {
	uint8_t *payload_mut;
	uint8_t const *payload; /**< The current place in the payload. Will be
	                             updated when an element is correctly
	                             processed. */
};
	uint8_t const *payload_bak; /**< Temporary backup of payload. */
	uint32_t elem_count; /**< The current element is part of a LIST or a MAP,
	                          and this keeps count of how many elements are
	                          expected. This will be checked before processing
	                          and decremented if the element is correctly
	                          processed. */
	uint8_t const *payload_end; /**< The end of the payload. This will be
	                                 checked against payload before
	                                 processing each element. */
	cbor_state_backups_t *backups;
} cbor_state_t;

struct cbor_state_backups_s{
	cbor_state_t *backup_list;
	uint32_t current_backup;
	uint32_t num_backups;
};

/** Function pointer type used with multi_decode.
 *
 * This type is compatible with all decoding functions here and in the generated
 * code, except for multi_decode.
 */
typedef bool(cbor_encoder_t)(cbor_state_t *, const void *);
typedef bool(cbor_decoder_t)(cbor_state_t *, void *);

/** Enumeration representing the major types available in CBOR.
 *
 * The major type is represented in the 3 first bits of the header byte.
 */
typedef enum
{
	CBOR_MAJOR_TYPE_PINT = 0, ///! Positive Integer
	CBOR_MAJOR_TYPE_NINT = 1, ///! Negative Integer
	CBOR_MAJOR_TYPE_BSTR = 2, ///! Byte String
	CBOR_MAJOR_TYPE_TSTR = 3, ///! Text String
	CBOR_MAJOR_TYPE_LIST = 4, ///! List
	CBOR_MAJOR_TYPE_MAP  = 5, ///! Map
	CBOR_MAJOR_TYPE_TAG  = 6, ///! Semantic Tag
	CBOR_MAJOR_TYPE_PRIM = 7, ///! Primitive Type
} cbor_major_type_t;

/** Shorthand macro to check if a result is within min/max constraints.
 */
#define PTR_VALUE_IN_RANGE(type, res, min, max) \
		(((min == NULL) || (*(type *)res >= *(type *)min)) \
		&& ((max == NULL) || (*(type *)res <= *(type *)max)))

#define FAIL() \
do {\
	cbor_trace(); \
	return false; \
} while(0)


#define VALUE_IN_HEADER 23 /**! For values below this, the value is encoded
                                directly in the header. */

#define BOOL_TO_PRIM 20 ///! In CBOR, false/true have the values 20/21

#define FLAG_RESTORE 1UL
#define FLAG_DISCARD 2UL
#define FLAG_TRANSFER_PAYLOAD 4UL

bool new_backup(cbor_state_t *state, uint32_t new_elem_count);

bool restore_backup(cbor_state_t *state, uint32_t flags,
		uint32_t max_elem_count);

bool union_start_code(cbor_state_t *state);

bool union_elem_code(cbor_state_t *state);

bool union_end_code(cbor_state_t *state);

bool entry_function(const uint8_t *payload, uint32_t payload_len,
		const void *struct_ptr, uint32_t *payload_len_out,
		cbor_encoder_t func, uint32_t elem_count, uint32_t num_backups);

#endif /* CBOR_COMMON_H__ */
+377 −176

File changed.

Preview size limit exceeded, changes collapsed.

Loading