Commit 3d51e430 authored by Dominik Ermel's avatar Dominik Ermel Committed by Andrzej Puzdrowski
Browse files

boot_serial: Add optional processing of PERUSER group

The commit adds optional processing of MGMT_GROUP_ID_PERUSER,
as defined by mcumgr library, and above; the processing requires
systems to provide own functions as these groups are system
specific.

Signed-off-by: default avatarDominik Ermel <dominik.ermel@nordicsemi.no>
parent 260ae090
Loading
Loading
Loading
Loading
+19 −0
Changes for boot/boot_serial/src/boot_serial.c: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -102,6 +102,20 @@ static cbor_state_t cbor_state = {
    .backups = &dummy_backups
};

/**
 * Function that processes MGMT_GROUP_ID_PERUSER mcumgr commands.
 * The function is system specific as the PERUSER commands are system specific.
 *
 * @param[in] hdr -- the decoded header of mcumgr message;
 * @param[in] buffer -- buffer with first mcumgr message;
 * @param[in] len -- length of of data in buffer;
 * @param[out] *cs -- object with encoded response.
 *
 * @return 0 on success; non-0 error code otherwise.
 */
extern int bs_peruser_system_specific(const struct nmgr_hdr *hdr,
                                      const char *buffer,
                                      int len, cbor_state_t *cs);

/*
 * Convert version into string without use of snprintf().
@@ -486,6 +500,11 @@ boot_serial_input(char *buf, int len)
        default:
            break;
        }
    } else if (MCUBOOT_PERUSER_MGMT_GROUP_ENABLED == 1 &&
               hdr->nh_group >= MGMT_GROUP_ID_PERUSER) {
        if (bs_peruser_system_specific(hdr, buf, len, &cbor_state) == 0) {
            boot_serial_output();
        }
    }
}

+4 −0
Changes for boot/boot_serial/src/boot_serial_priv.h: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -36,13 +36,17 @@ extern "C" {
/*
 * From newtmgr.h
 */
#define MGMT_ERR_OK             0
#define MGMT_ERR_EUNKNOWN       2
#define MGMT_ERR_EINVAL         3
#define MGMT_ERR_ENOTSUP        8

#define NMGR_OP_READ            0
#define NMGR_OP_WRITE           2

#define MGMT_GROUP_ID_DEFAULT   0
#define MGMT_GROUP_ID_IMAGE     1
#define MGMT_GROUP_ID_PERUSER  64

#define NMGR_ID_CONS_ECHO_CTRL  1
#define NMGR_ID_RESET           5
+10 −0
Changes for boot/zephyr/Kconfig: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -552,6 +552,16 @@ config RECOVERY_UART_DEV_NAME
	  This option specifies the name of UART device to be used for
	  serial recovery.

config ENABLE_MGMT_PERUSER
	bool "Enables system specific mcumgr commands"
	depends on BOOT_SERIAL_UART
	help
	  The option enables processing of system specific mcumgr commands;
	  system specific commands are within group MGMT_GROUP_ID_PERUSER (64)
	  and above, as defined within mcumgr library.
	  These are system specific command and system specific implementation
	  function is required to process these commands.

endif # MCUBOOT_SERIAL

config BOOT_INTR_VEC_RELOC
+46 −0
Changes for boot/zephyr/boot_serial_extensions.c: 46 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * Copyright (c) 2021 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>
#include <drivers/flash.h>
#include <mgmt/mcumgr/zephyr_groups.h>

#include <flash_map_backend/flash_map_backend.h>
#include <sysflash/sysflash.h>

#include "bootutil/bootutil_log.h"
#include "../boot_serial/src/boot_serial_priv.h"
#include "../boot_serial/src/cbor_encode.h"

MCUBOOT_LOG_MODULE_DECLARE(mcuboot);

int bs_peruser_system_specific(const struct nmgr_hdr *hdr, const char *buffer,
                               int len, cbor_state_t *cs)
{
    const struct flash_area *fa;
    int rc = flash_area_open(FLASH_AREA_ID(storage), &fa);

    if (rc < 0) {
        LOG_ERR("failed to open flash area");
    } else {
	rc = flash_area_erase(fa, 0, FLASH_AREA_SIZE(storage));
	if (rc < 0) {
		LOG_ERR("failed to erase flash area");
	}
	flash_area_close(fa);
    }

    map_start_encode(cs, 10);
    tstrx_put(cs, "rc");
    if (rc != 0) {
        uintx32_put(cs, MGMT_ERR_EUNKNOWN);
    } else {
        uintx32_put(cs, 0);
    }
    map_end_encode(cs, 10);

    return rc;
}