From 7ffcdca098ecc0e67baef3a2e398c2d07ba9a87b Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 3 Apr 2020 14:10:16 +0200 Subject: [PATCH] External, emulated non-volatile memory over UART --- Makefile | 11 + arch.mk | 3 + .../stm32f4-small-blocks-uart-update.config | 35 ++ hal/uart/uart_drv_lpc.c | 113 +++++ hal/uart/uart_drv_stm32f4.c | 148 +++++++ src/loader.c | 5 + src/uart_flash.c | 179 ++++++++ test-app/Makefile | 6 + tools/config.mk | 3 +- tools/uart-flash-server/Makefile | 10 + tools/uart-flash-server/README.md | 44 ++ tools/uart-flash-server/ufserver.c | 417 ++++++++++++++++++ 12 files changed, 973 insertions(+), 1 deletion(-) create mode 100644 config/examples/stm32f4-small-blocks-uart-update.config create mode 100644 hal/uart/uart_drv_lpc.c create mode 100644 hal/uart/uart_drv_stm32f4.c create mode 100644 src/uart_flash.c create mode 100644 tools/uart-flash-server/Makefile create mode 100644 tools/uart-flash-server/README.md create mode 100644 tools/uart-flash-server/ufserver.c diff --git a/Makefile b/Makefile index 7e3c10c6..a174f5da 100644 --- a/Makefile +++ b/Makefile @@ -118,13 +118,24 @@ ifeq ($(SPI_FLASH),1) WOLFCRYPT_OBJS+=hal/spi/spi_drv_$(SPI_TARGET).o endif +ifeq ($(UART_FLASH),1) + EXT_FLASH=1 +endif + ifeq ($(EXT_FLASH),1) CFLAGS+= -DEXT_FLASH=1 -DPART_UPDATE_EXT=1 -DPART_SWAP_EXT=1 ifeq ($(NO_XIP),1) CFLAGS+=-DPART_BOOT_EXT=1 endif + ifeq ($(UART_FLASH),1) + CFLAGS+=-DUART_FLASH=1 + OBJS+=src/uart_flash.o + WOLFCRYPT_OBJS+=hal/uart/uart_drv_$(UART_TARGET).o + endif endif + + ifeq ($(ALLOW_DOWNGRADE),1) CFLAGS+= -DALLOW_DOWNGRADE endif diff --git a/arch.mk b/arch.mk index 0be09893..68aa7b02 100644 --- a/arch.mk +++ b/arch.mk @@ -15,6 +15,8 @@ ARCH_FLASH_OFFSET=0x0 # Default SPI driver name SPI_TARGET=$(TARGET) +# Default UART driver name +UART_TARGET=$(TARGET) ## Hash settings ifeq ($(HASH),SHA256) @@ -129,6 +131,7 @@ ifeq ($(TARGET),lpc) CFLAGS+=-I$(MCUXPRESSO_DRIVERS)/drivers -I$(MCUXPRESSO_DRIVERS) -DCPU_$(MCUXPRESSO_CPU) -I$(MCUXPRESSO_CMSIS)/Include -DDEBUG_CONSOLE_ASSERT_DISABLE=1 OBJS+=$(MCUXPRESSO_DRIVERS)/drivers/fsl_clock.o $(MCUXPRESSO_DRIVERS)/drivers/fsl_flashiap.o $(MCUXPRESSO_DRIVERS)/drivers/fsl_power.o $(MCUXPRESSO_DRIVERS)/drivers/fsl_reset.o OBJS+=$(MCUXPRESSO_DRIVERS)/mcuxpresso/libpower_softabi.a $(MCUXPRESSO_DRIVERS)/drivers/fsl_common.o + OBJS+=$(MCUXPRESSO_DRIVERS)/drivers/fsl_usart.o $(MCUXPRESSO_DRIVERS)/drivers/fsl_flexcomm.o endif diff --git a/config/examples/stm32f4-small-blocks-uart-update.config b/config/examples/stm32f4-small-blocks-uart-update.config new file mode 100644 index 00000000..d5f78ac7 --- /dev/null +++ b/config/examples/stm32f4-small-blocks-uart-update.config @@ -0,0 +1,35 @@ +ARCH?=ARM +TARGET?=stm32f4 +SIGN?=ECC256 +HASH?=SHA256 +MCUXPRESSO?=/home/dan/src/FRDM-K64F +MCUXPRESSO_CPU?=MK64FN1M0VLL12 +MCUXPRESSO_DRIVERS?=/home/dan/src/FRDM-K64F/devices/MK64F12 +MCUXPRESSO_CMSIS?=/home/dan/src/FRDM-K64F/CMSIS +FREEDOM_E_SDK?=/home/dan/src/freedom-e-sdk +STM32CUBE?=/home/dan/STM32Cube/Repository/STM32Cube_FW_WB_V1.3.0 +DEBUG?=1 +VTOR?=1 +CORTEX_M0?=0 +NO_ASM?=0 +EXT_FLASH?=0 +SPI_FLASH?=0 +NO_XIP?=0 +UART_FLASH?=1 +ALLOW_DOWNGRADE?=0 +NVM_FLASH_WRITEONCE?=0 +WOLFBOOT_VERSION?=0 +V?=0 +SPMATH?=1 +RAM_CODE?=0 +DUALBANK_SWAP?=0 +IMAGE_HEADER_SIZE?=256 +PKA?=1 +WOLFTPM?=0 +WOLFBOOT_PARTITION_SIZE?=0x4000 +WOLFBOOT_SECTOR_SIZE?=0x4000 +WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x8000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x00000 +WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x4000 +WOLFBOOT_LOAD_ADDRESS?=0x200000 +WOLFBOOT_LOAD_DTS_ADDRESS?=0x400000 diff --git a/hal/uart/uart_drv_lpc.c b/hal/uart/uart_drv_lpc.c new file mode 100644 index 00000000..acf519e7 --- /dev/null +++ b/hal/uart/uart_drv_lpc.c @@ -0,0 +1,113 @@ +/* uart_drv_lpc.c + * + * Driver for the back-end of the UART_FLASH module. + * + * Example implementation for LPC54xxx, using UART0. + * + * Copyright (C) 2020 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include "fsl_common.h" +#include "fsl_iocon.h" +#include "fsl_usart.h" +#include "target.h" +#include "wolfboot/wolfboot.h" +#include "hal.h" + +#define IOCON_PIO_DIGITAL_EN 0x0100u /*!<@brief Enables digital function */ +#define IOCON_PIO_FUNC1 0x01u /*!<@brief Selects pin function 1 */ +#define IOCON_PIO_INPFILT_OFF 0x0200u /*!<@brief Input filter disabled */ +#define IOCON_PIO_INV_DI 0x00u /*!<@brief Input function is not inverted */ +#define IOCON_PIO_MODE_INACT 0x00u /*!<@brief No addition pin function */ +#define IOCON_PIO_OPENDRAIN_DI 0x00u /*!<@brief Open drain is disabled */ +#define IOCON_PIO_SLEW_STANDARD 0x00u /*!<@brief Standard mode, output slew rate control is enabled */ + +static void uart_pin_init(void) +{ + /* Enables the clock for the IOCON block. 0 = Disable; 1 = Enable.: 0x01u */ + CLOCK_EnableClock(kCLOCK_Iocon); + + const uint32_t port0_pin29_config = (/* Pin is configured as FC0_RXD_SDA_MOSI */ + IOCON_PIO_FUNC1 | + /* No addition pin function */ + IOCON_PIO_MODE_INACT | + /* Input function is not inverted */ + IOCON_PIO_INV_DI | + /* Enables digital function */ + IOCON_PIO_DIGITAL_EN | + /* Input filter disabled */ + IOCON_PIO_INPFILT_OFF | + /* Standard mode, output slew rate control is enabled */ + IOCON_PIO_SLEW_STANDARD | + /* Open drain is disabled */ + IOCON_PIO_OPENDRAIN_DI); + /* PORT0 PIN29 (coords: B13) is configured as FC0_RXD_SDA_MOSI */ + IOCON_PinMuxSet(IOCON, 0U, 29U, port0_pin29_config); + + const uint32_t port0_pin30_config = (/* Pin is configured as FC0_TXD_SCL_MISO */ + IOCON_PIO_FUNC1 | + /* No addition pin function */ + IOCON_PIO_MODE_INACT | + /* Input function is not inverted */ + IOCON_PIO_INV_DI | + /* Enables digital function */ + IOCON_PIO_DIGITAL_EN | + /* Input filter disabled */ + IOCON_PIO_INPFILT_OFF | + /* Standard mode, output slew rate control is enabled */ + IOCON_PIO_SLEW_STANDARD | + /* Open drain is disabled */ + IOCON_PIO_OPENDRAIN_DI); + /* PORT0 PIN30 (coords: A2) is configured as FC0_TXD_SCL_MISO */ + IOCON_PinMuxSet(IOCON, 0U, 30U, port0_pin30_config); +} + +int uart_tx(const uint8_t c) +{ + while((USART_GetStatusFlags(USART0) & kUSART_TxFifoEmptyFlag) == 0) + ; + USART_WriteByte(USART0, c); + return 1; +} + +int uart_rx(uint8_t *c) +{ + if ((USART_GetStatusFlags(USART0) & kUSART_RxFifoNotEmptyFlag) != 0) { + *c = USART_ReadByte(USART0); + return 1; + } + return 0; +} + +int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop) +{ + uint8_t ch; + usart_config_t config; + uart_pin_init(); + USART_GetDefaultConfig(&config); + config.baudRate_Bps = bitrate; + config.enableTx = true; + config.enableRx = true; + CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0); + USART_Init(USART0, &config, CLOCK_GetFlexCommClkFreq(0)); + + uart_send_current_version(); + return 0; +} + diff --git a/hal/uart/uart_drv_stm32f4.c b/hal/uart/uart_drv_stm32f4.c new file mode 100644 index 00000000..c7e723ec --- /dev/null +++ b/hal/uart/uart_drv_stm32f4.c @@ -0,0 +1,148 @@ +/* uart_drv_stm32.c + * + * Driver for the back-end of the UART_FLASH module. + * + * Example implementation for stm32F4, using UART3. + * + * Pinout: RX=PD9, TX=PD8 + * + * Copyright (C) 2020 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include + +/* Driver hardcoded to work on UART3 (PD8/PD9) */ +#define UART3 (0x40004800) +#define UART3_PIN_AF 7 +#define UART3_RX_PIN 9 +#define UART3_TX_PIN 8 + +#define UART3_SR (*(volatile uint32_t *)(UART3)) +#define UART3_DR (*(volatile uint32_t *)(UART3 + 0x04)) +#define UART3_BRR (*(volatile uint32_t *)(UART3 + 0x08)) +#define UART3_CR1 (*(volatile uint32_t *)(UART3 + 0x0c)) +#define UART3_CR2 (*(volatile uint32_t *)(UART3 + 0x10)) + +#define UART_CR1_UART_ENABLE (1 << 13) +#define UART_CR1_SYMBOL_LEN (1 << 12) +#define UART_CR1_PARITY_ENABLED (1 << 10) +#define UART_CR1_PARITY_ODD (1 << 9) +#define UART_CR1_TX_ENABLE (1 << 3) +#define UART_CR1_RX_ENABLE (1 << 2) +#define UART_CR2_STOPBITS (3 << 12) +#define UART_SR_TX_EMPTY (1 << 7) +#define UART_SR_RX_NOTEMPTY (1 << 5) + + +#define CLOCK_SPEED (168000000) + +#define APB1_CLOCK_ER (*(volatile uint32_t *)(0x40023840)) +#define UART3_APB1_CLOCK_ER_VAL (1 << 18) + +#define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830)) +#define GPIOD_AHB1_CLOCK_ER (1 << 3) +#define GPIOD_BASE 0x40020c00 +#define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00)) +#define GPIOD_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20)) +#define GPIOD_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24)) +#define GPIO_MODE_AF (2) + +static void uart_pins_setup(void) +{ + uint32_t reg; + AHB1_CLOCK_ER |= GPIOD_AHB1_CLOCK_ER; + /* Set mode = AF */ + reg = GPIOD_MODE & ~ (0x03 << (UART3_RX_PIN * 2)); + GPIOD_MODE = reg | (2 << (UART3_RX_PIN * 2)); + reg = GPIOD_MODE & ~ (0x03 << (UART3_TX_PIN * 2)); + GPIOD_MODE = reg | (2 << (UART3_TX_PIN * 2)); + + /* Alternate function: use high pins (8 and 9) */ + reg = GPIOD_AFH & ~(0xf << ((UART3_TX_PIN - 8) * 4)); + GPIOD_AFH = reg | (UART3_PIN_AF << ((UART3_TX_PIN - 8) * 4)); + reg = GPIOD_AFH & ~(0xf << ((UART3_RX_PIN - 8) * 4)); + GPIOD_AFH = reg | (UART3_PIN_AF << ((UART3_RX_PIN - 8) * 4)); +} + +int uart_tx(const uint8_t c) +{ + uint32_t reg; + do { + reg = UART3_SR; + } while ((reg & UART_SR_TX_EMPTY) == 0); + UART3_DR = c; + return 1; +} + +int uart_rx(uint8_t *c) +{ + volatile uint32_t reg = UART3_SR; + if ((reg & UART_SR_RX_NOTEMPTY) != 0) { + reg = UART3_DR; + *c = (uint8_t)(reg & 0xff); + return 1; + } + return 0; +} + +int uart_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop) +{ + uint32_t reg; + /* Enable pins and configure for AF7 */ + uart_pins_setup(); + /* Turn on the device */ + APB1_CLOCK_ER |= UART3_APB1_CLOCK_ER_VAL; + UART3_CR1 &= ~(UART_CR1_UART_ENABLE); + + /* Configure for TX + RX */ + UART3_CR1 |= (UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE); + + /* Configure clock */ + UART3_BRR = CLOCK_SPEED / bitrate; + + /* Configure data bits */ + if (data == 8) + UART3_CR1 &= ~UART_CR1_SYMBOL_LEN; + else + UART3_CR1 |= UART_CR1_SYMBOL_LEN; + + /* Configure parity */ + switch (parity) { + case 'O': + UART3_CR1 |= UART_CR1_PARITY_ODD; + /* fall through to enable parity */ + /* FALL THROUGH */ + case 'E': + UART3_CR1 |= UART_CR1_PARITY_ENABLED; + break; + default: + UART3_CR1 &= ~(UART_CR1_PARITY_ENABLED | UART_CR1_PARITY_ODD); + } + /* Set stop bits */ + reg = UART3_CR2 & ~UART_CR2_STOPBITS; + if (stop > 1) + UART3_CR2 = reg & (2 << 12); + else + UART3_CR2 = reg; + + /* Turn on uart */ + UART3_CR1 |= UART_CR1_UART_ENABLE; + return 0; +} + diff --git a/src/loader.c b/src/loader.c index 9a6320f2..3ffbd667 100644 --- a/src/loader.c +++ b/src/loader.c @@ -23,6 +23,7 @@ #include "image.h" #include "hal.h" #include "spi_flash.h" +#include "uart_flash.h" #include "wolfboot/wolfboot.h" #ifdef RAM_CODE @@ -172,6 +173,10 @@ int main(void) { hal_init(); spi_flash_probe(); +#ifdef UART_FLASH + uart_init((4 * 115200), 8, 'N', 1); + uart_send_current_version(); +#endif #ifdef WOLFTPM2_NO_WOLFCRYPT wolfBoot_tpm2_init(); #endif diff --git a/src/uart_flash.c b/src/uart_flash.c new file mode 100644 index 00000000..4063bac4 --- /dev/null +++ b/src/uart_flash.c @@ -0,0 +1,179 @@ +/* uart_flash.c + * + * Generic implementation of the read/write/erase + * functionalities, on top of the spi_drv_*.c API. + * + * This interface creates the communication to access an emulated + * non-volatile memory, hosted on a remote machine, through the UART + * interface. + * + * + * Copyright (C) 2020 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ +#include "wolfboot/wolfboot.h" +#include "hal.h" +#include +#include + +#define CMD_HDR_WOLF 'W' +#define CMD_HDR_WRITE 0x01 +#define CMD_HDR_READ 0x02 +#define CMD_HDR_ERASE 0x03 +#define CMD_ACK 0x06 + +#define WAIT_CYCLES 500000 +#define ERASE_TIMEOUT 5 +#define READ_TIMEOUT 1 + +int uart_tx(const uint8_t c); +int uart_rx(uint8_t *c); + +static int wait_ack(void) +{ + volatile int count = 0; + while(++count < WAIT_CYCLES) { + uint8_t c; + if ((uart_rx(&c) == 1) && (c == CMD_ACK)) + return 0; + } + return -1; +} + +static int uart_rx_timeout(uint8_t *c) +{ + volatile int count = 0; + while(++count < (WAIT_CYCLES * READ_TIMEOUT)) { + if (uart_rx(c) == 1) /* Success */ + return 0; + } + *c = 0x00; + return -1; +} + + +int ext_flash_write(uintptr_t address, const uint8_t *data, int len) +{ + int i; + uint8_t cmd[10]; + cmd[0] = CMD_HDR_WOLF; + cmd[1] = CMD_HDR_WRITE; + cmd[2] = address & 0xFF; + cmd[3] = (address >> 8) & 0xFF; + cmd[4] = (address >> 16) & 0xFF; + cmd[5] = (address >> 24) & 0xFF; + cmd[6] = len & 0xFF; + cmd[7] = (len >> 8) & 0xFF; + cmd[8] = (len >> 16) & 0xFF; + cmd[9] = (len >> 24) & 0xFF; + for (i = 0; i < 10; i++) { + uart_tx(cmd[i]); + if (wait_ack() != 0) + return -1; + } + for (i = 0; i < len; i++) { + uart_tx(data[i]); + if (wait_ack() != 0) + return -1; + } + return i; +} + +int ext_flash_read(uintptr_t address, uint8_t *data, int len) +{ + int i; + uint8_t cmd[10]; + cmd[0] = CMD_HDR_WOLF; + cmd[1] = CMD_HDR_READ; + cmd[2] = address & 0xFF; + cmd[3] = (address >> 8) & 0xFF; + cmd[4] = (address >> 16) & 0xFF; + cmd[5] = (address >> 24) & 0xFF; + cmd[6] = len & 0xFF; + cmd[7] = (len >> 8) & 0xFF; + cmd[8] = (len >> 16) & 0xFF; + cmd[9] = (len >> 24) & 0xFF; + for (i = 0; i < 10; i++) { + uart_tx(cmd[i]); + if (wait_ack() != 0) + return -1; + } + for (i = 0; i < len; i++) { + if (uart_rx_timeout(&data[i]) != 0) + return 0; + uart_tx(CMD_ACK); + } + return len; +} + +int ext_flash_erase(uintptr_t address, int len) +{ + int i; + uint8_t cmd[10]; + cmd[0] = CMD_HDR_WOLF; + cmd[1] = CMD_HDR_ERASE; + cmd[2] = address & 0xFF; + cmd[3] = (address >> 8) & 0xFF; + cmd[4] = (address >> 16) & 0xFF; + cmd[5] = (address >> 24) & 0xFF; + cmd[6] = len & 0xFF; + cmd[7] = (len >> 8) & 0xFF; + cmd[8] = (len >> 16) & 0xFF; + cmd[9] = (len >> 24) & 0xFF; + for (i = 0; i < 10; i++) { + uart_tx(cmd[i]); + if (wait_ack() != 0) + return -1; + } + /* Wait for extra ack at the end of Erase */ + if (wait_ack() == 0) + return 0; + return -1; +} + +void ext_flash_lock(void) +{ + wait_ack(); +} + +void ext_flash_unlock(void) +{ + wait_ack(); +} + +void uart_send_current_version(void) +{ + uint32_t version = wolfBoot_current_firmware_version(); + uart_tx('V'); + if (wait_ack() != 0) + return; + uart_tx(version & 0x000000FF); + if (wait_ack() != 0) + return; + uart_tx((version >> 8) & 0x000000FF); + if (wait_ack() != 0) + return; + uart_tx((version >> 16) & 0x000000FF); + if (wait_ack() != 0) + return; + uart_tx((version >> 24) & 0x000000FF); + if (wait_ack() != 0) + return; +} + + diff --git a/test-app/Makefile b/test-app/Makefile index a0fbb06e..ee306f41 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -16,6 +16,7 @@ ifeq ($(HASH),SHA256) CFLAGS+=-DWOLFBOOT_HASH_SHA256 endif + ifeq ($(HASH),SHA3_384) WOLFCRYPT_OBJS+=./lib/wolfssl/wolfcrypt/src/sha3.o CFLAGS+=-DWOLFBOOT_HASH_SHA3_384 @@ -56,6 +57,11 @@ ifeq ($(SPI_FLASH),1) APP_OBJS+=../hal/spi/spi_drv_$(SPI_TARGET).o ../src/spi_flash.o endif +ifeq ($(UART_FLASH),1) + CFLAGS+=-DUART_FLASH=1 + APP_OBJS+= ../src/uart_flash.o ../hal/uart/uart_drv_$(UART_TARGET).o +endif + ifeq ($(TARGET),kinetis) CFLAGS+= -I$(MCUXPRESSO_DRIVERS)/drivers -I$(MCUXPRESSO_DRIVERS) -DCPU_$(MCUXPRESSO_CPU) -I$(MCUXPRESSO_CMSIS)/Include -DDEBUG_CONSOLE_ASSERT_DISABLE=1 -DNVM_FLASH_WRITEONCE=1 APP_OBJS+= $(MCUXPRESSO_DRIVERS)/drivers/fsl_clock.o $(MCUXPRESSO_DRIVERS)/drivers/fsl_ftfx_flash.o $(MCUXPRESSO_DRIVERS)/drivers/fsl_ftfx_cache.o \ diff --git a/tools/config.mk b/tools/config.mk index 84db8861..9e51e75f 100644 --- a/tools/config.mk +++ b/tools/config.mk @@ -17,6 +17,7 @@ ifeq ($(ARCH),) EXT_FLASH?=0 SPI_FLASH?=0 NO_XIP?=0 + UART_FLASH?=0 ALLOW_DOWNGRADE?=0 NVM_FLASH_WRITEONCE?=0 WOLFBOOT_VERSION?=0 @@ -41,7 +42,7 @@ endif CONFIG_VARS:= ARCH TARGET SIGN HASH MCUXPRESSO MCUXPRESSO_CPU MCUXPRESSO_DRIVERS \ MCUXPRESSO_CMSIS FREEDOM_E_SDK STM32CUBE DEBUG VTOR CORTEX_M0 NO_ASM EXT_FLASH \ - SPI_FLASH NO_XIP ALLOW_DOWNGRADE NVM_FLASH_WRITEONCE WOLFBOOT_VERSION V \ + SPI_FLASH NO_XIP UART_FLASH ALLOW_DOWNGRADE NVM_FLASH_WRITEONCE WOLFBOOT_VERSION V \ SPMATH RAM_CODE DUALBANK_SWAP IMAGE_HEADER_SIZE PKA WOLFTPM \ WOLFBOOT_PARTITION_SIZE WOLFBOOT_SECTOR_SIZE \ WOLFBOOT_PARTITION_BOOT_ADDRESS WOLFBOOT_PARTITION_UPDATE_ADDRESS \ diff --git a/tools/uart-flash-server/Makefile b/tools/uart-flash-server/Makefile new file mode 100644 index 00000000..6ad63f09 --- /dev/null +++ b/tools/uart-flash-server/Makefile @@ -0,0 +1,10 @@ +CC=$(CROSS_COMPILE)gcc +CFLAGS=-Wall -DWOLFSSL_DTLS -DWOLFSSL_DEBUG -DTFM_TIMING_RESISTANT -DWOLFBOOT_SIGN_ECC256 -DWOLFBOOT_HASH_SHA256 -g -ggdb -I../../include -I../../hal -Wextra + +EXE=ufserver + +$(EXE): $(EXE).o ../../src/libwolfboot.o + $(CC) -o $@ $^ $(CFLAGS) $(LIBS) + +clean: + rm -f *.o $(EXE) ../../src/libwolfboot.o diff --git a/tools/uart-flash-server/README.md b/tools/uart-flash-server/README.md new file mode 100644 index 00000000..1d722163 --- /dev/null +++ b/tools/uart-flash-server/README.md @@ -0,0 +1,44 @@ +# UART interface for secure local updates + +This directory contains a daemon that can be used on a host machine to emulate a remote +non-volatile memory on the target, using a UART channel. + +## wolfBoot interface + +The [Uart Flash](../../src/uart_flash.c) driver uses the same API as other external flash +memories. The target can be connected through a serial line to a host that can provide update +images by emulating the UPDATE partition on the target. + +wolfBoot must be compiled with the option `UART_FLASH=1`. The back-end drivers to access the +UART interfaces are in the [uart drivers](../../hal/uart/) directory on this repository. + + +## ufserver usage + +The 'uart flash server' (ufserver) requires two arguments: + + - The path to the signed firmware update image (e.g. `../../../test-app/image_v2_signed.bin`) + - The serial port connected to the target (e.g. `/dev/ttyS0`) + +When a new image is processed for the first time, an update flag is set automatically to indicate +that the update is available. + +The bootloader will use the image file as its update+swap partition, so the file will be modified +by wolfboot during and after an update. + +## Authentication + +The daemon does not perform any signature verification, nor it checks the integrity of the firmware +image passed as command line argument. wolfBoot is still in charge of verifying integrity and authenticity +of the received update, and more in general, of every image before it is staged to boot. + + +## Swap and fail-safe operations + +When installing an update using this mechanism, wolfBoot will erase the content of the file on the +filesystem and will overwrite its content with a copy of the older version. +As long as the daemon is running, wolfBoot will still be able to roll-back to a previous version if +booting the update fails. + + + diff --git a/tools/uart-flash-server/ufserver.c b/tools/uart-flash-server/ufserver.c new file mode 100644 index 00000000..19dd8120 --- /dev/null +++ b/tools/uart-flash-server/ufserver.c @@ -0,0 +1,417 @@ +/* ufserver.c + * + * UART flash server + * + * Run on HOST machine to export an emulated external, non-volatile memory. + * + * Copyright (C) 2020 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include +#include +#include +#include "wolfboot/wolfboot.h" +#include "hal.h" +#include +#include +#include +#include +#include +#include + + +#define CMD_HDR_WOLF 'W' +#define CMD_HDR_VER 'V' +#define CMD_HDR_WRITE 0x01 +#define CMD_HDR_READ 0x02 +#define CMD_HDR_ERASE 0x03 +#define CMD_ACK 0x06 + +#define FIRMWARE_PARTITION_SIZE 0x4000 +#define SWAP_SIZE 0x4000 + +const char msgSha[] = "Verifying SHA digest..."; +const char msgReadUpdate[] = "Fetching update blocks "; +const char msgReadSwap[] = "Reading SWAP blocks "; +const char msgWriteUpdate[] = "Writing backup blocks "; +const char msgWriteSwap[] = "Writing SWAP blocks "; +const char msgEraseUpdate[] = "Erase update blocks "; +const char msgEraseSwap[] = "Erase swap blocks "; + +extern uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr); + +const char blinker[]="-\\|/"; + +void printmsg(const char *msg) +{ + static const char *cur_msg = NULL; + static int b_idx = 0; + if (cur_msg != msg) { + cur_msg = msg; + } + b_idx++; + if (b_idx > 3) + b_idx = 0; + printf("\r[%c] %s\t\t\t", blinker[b_idx], msg); + fflush(stdout); +} + + +int hal_flash_write(uint32_t address, const uint8_t *data, int len) +{ + (void)address; + (void)data; + (void)len; + return -1; +} + +int hal_flash_erase(uint32_t address, int len) +{ + (void)address; + (void)len; + return -1; +} +void hal_flash_unlock(void) +{ +} +void hal_flash_lock(void) +{ +} + +#include +#include +#include +#include + +static int rate_to_constant(int baudrate) { +#define B(x) case x: return B##x + switch(baudrate) { + B(50); B(75); B(110); B(134); B(150); + B(200); B(300); B(600); B(1200); B(1800); + B(2400); B(4800); B(9600); B(19200); B(38400); + B(57600); B(115200); B(230400); B(460800); B(500000); + B(576000); B(921600); B(1000000);B(1152000);B(1500000); +default: return 0; +} +#undef B +} + +/* Open serial port in raw mode, with custom baudrate if necessary */ +static int serial_open(const char *device, int rate) +{ + struct termios options; + struct serial_struct serinfo; + int fd; + int speed = 0; + + /* Open and configure serial port */ + if ((fd = open(device,O_RDWR|O_NOCTTY)) == -1) + return -1; + + speed = rate_to_constant(rate); + + if (speed == 0) { + /* Custom divisor */ + serinfo.reserved_char[0] = 0; + if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) + return -1; + serinfo.flags &= ~ASYNC_SPD_MASK; + serinfo.flags |= ASYNC_SPD_CUST; + serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate; + if (serinfo.custom_divisor < 1) + serinfo.custom_divisor = 1; + if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0) + return -1; + if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) + return -1; + if (serinfo.custom_divisor * rate != serinfo.baud_base) { + warnx("actual baudrate is %d / %d = %f", + serinfo.baud_base, serinfo.custom_divisor, + (float)serinfo.baud_base / serinfo.custom_divisor); + } + } + + fcntl(fd, F_SETFL, 0); + tcgetattr(fd, &options); + cfsetispeed(&options, speed ?: B38400); + cfsetospeed(&options, speed ?: B38400); + cfmakeraw(&options); + options.c_cflag |= (CLOCAL | CREAD); + options.c_cflag &= ~CRTSCTS; + if (tcsetattr(fd, TCSANOW, &options) != 0) + return -1; + + return fd; +} + + +uint8_t *mmap_firmware(const char *fname) +{ + uint8_t *base_fw; + struct stat st; + int fd; + if (stat(fname, &st) != 0) { + perror ("stat"); + return (void *)-1; + } + fd = open(fname, O_RDWR); + if (fd < 0) + { + perror("open"); + return (void *)-1; + } + if (st.st_size < FIRMWARE_PARTITION_SIZE) { + uint8_t pad = 0xFF; + int i; + const char update_flags[] = "pBOOT"; + int fsize = st.st_size; + lseek(fd, FIRMWARE_PARTITION_SIZE + SWAP_SIZE, SEEK_SET); + lseek(fd, fsize, SEEK_SET); + for (i = 0; i < (FIRMWARE_PARTITION_SIZE - (fsize + 5)); i++) + write(fd, &pad, 1); + write(fd, update_flags, 5); + for (i = 0; i < SWAP_SIZE; i++) + write(fd, &pad, 1); + } + base_fw = mmap(NULL, FIRMWARE_PARTITION_SIZE + SWAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (base_fw == (void *)(-1)) { + perror("mmap"); + return (void *)-1; + } + return base_fw; +} + + +uint32_t fw_version(uint8_t *fw) +{ + uint16_t l; + uint8_t *p; + uint32_t ver; + l = wolfBoot_find_header(fw + 8, HDR_VERSION, &p); + if (l != 4) + return (uint32_t)-1; + ver = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); + return ver; +} + +int open_uart(const char *uart_dev) +{ + return serial_open(uart_dev, 4 * 115200); +} + +static void send_ack(int ud) +{ + uint8_t ack = CMD_ACK; + write(ud, &ack, 1); + fsync(ud); +} + +static int read_word(int ud, uint32_t *w) +{ + uint32_t i; + uint8_t *b = (uint8_t *)w; + int ret; + for (i = 0; i < 4; i++) { + ret = read(ud, &b[i], 1); + if (ret != 1) + return 0; + send_ack(ud); + } + return 4; +} + +static void uart_flash_erase(uint8_t *base, int ud) +{ + uint32_t address; + uint32_t len; + uint32_t i; + if (read_word(ud,&address) != 4) + return; + if (read_word(ud,&len) != 4) + return; + if (address + len > (FIRMWARE_PARTITION_SIZE + SWAP_SIZE)) + return; + if (address < FIRMWARE_PARTITION_SIZE) { + printmsg(msgEraseUpdate); + } else { + printmsg(msgEraseSwap); + } + for (i = 0; i < len; i++) { + base[address + i] = 0xFF; + } + /* Send additional ack at the end of erase */ + send_ack(ud); + msync(base, FIRMWARE_PARTITION_SIZE + SWAP_SIZE, MS_SYNC); +} + + +static void uart_flash_read(uint8_t *base, int ud) +{ + uint32_t address; + uint32_t len; + uint32_t i; + uint8_t ack; + if (read_word(ud,&address) != 4) + return; + if (read_word(ud,&len) != 4) + return; + if (len == 16) { + printmsg(msgSha); + } else if (address < FIRMWARE_PARTITION_SIZE) { + printmsg(msgReadUpdate); + } else { + printmsg(msgReadSwap); + } + if (address + len > (FIRMWARE_PARTITION_SIZE + SWAP_SIZE)) + return; + for (i = 0; i < len; i++) { + write(ud, base + address + i, 1); + read(ud, &ack, 1); + } +} + +static void uart_flash_write(uint8_t *base, int ud) +{ + uint32_t address; + uint32_t len; + uint32_t i; + if (read_word(ud,&address) != 4) + return; + if (read_word(ud,&len) != 4) + return; + if (address < FIRMWARE_PARTITION_SIZE) { + printmsg(msgWriteUpdate); + } else { + printmsg(msgWriteSwap); + } + if (address + len > (FIRMWARE_PARTITION_SIZE + SWAP_SIZE)) + return; + for (i = 0; i < len; i++) { + read(ud, base + address + i, 1); + send_ack(ud); + } + msync(base, FIRMWARE_PARTITION_SIZE + SWAP_SIZE, MS_SYNC); +} + + +static void serve_update(uint8_t *base, const char *uart_dev) +{ + int ret = 0; + uint8_t buf[8]; + int ud = open_uart(uart_dev); + if (ud < 0) { + fprintf(stderr, "Cannot open serial port %s: %s.\n", uart_dev, strerror(errno)); + exit(3); + } + while (1) { + /* read STX */ + ret = read(ud, buf, 1); + if (ret < 0) { + return; + } + if (ret == 0) + continue; + + if ((buf[0] != CMD_HDR_WOLF) && (buf[0] != CMD_HDR_VER)) { + printf("bad hdr: %02x\n", buf[0]); + continue; + } + if (buf[0] == CMD_HDR_VER) { + uint32_t v; + int idx = 1; + send_ack(ud); + while (idx < 5) { + ret = read(ud, buf + idx, 1); + if (ret > 0) { + send_ack(ud); + idx++; + } + else { + printf("UART error while reading target version\n"); + break; + } + } + if (idx == 5) { + printf("\r\n** TARGET REBOOT **\n"); + v = buf[1] + (buf[2] << 8) + (buf[3] << 16) + (buf[4] << 24); + printf("Version running on target: %u\n", v); + } + continue; + } + /* send ack */ + send_ack(ud); + ret = read(ud, buf, 1); + if (ret < 0) { + return; + } + if (ret == 0) { + printf("Timeout!\n"); + continue; + } + /* Read command code */ + switch(buf[0]) { + case CMD_HDR_ERASE: + send_ack(ud); + uart_flash_erase(base, ud); + break; + case CMD_HDR_READ: + send_ack(ud); + uart_flash_read(base, ud); + break; + case CMD_HDR_WRITE: + send_ack(ud); + uart_flash_write(base, ud); + break; + default: + fprintf(stderr, "Unrecognized command: %02X\n", buf[0]); + break; + } + } +} + +void usage(char *pname) +{ + printf("Usage: %s binary_file serial_port\nExample:\n%s firmware_v3_signed.bin /dev/ttyUSB0\n", pname, pname); + exit(1); +} + +int main(int argc, char *argv[]) +{ + uint8_t *base_fw; + uint32_t base_fw_ver = 0; + if (argc != 3) { + usage(argv[0]); + } + base_fw = mmap_firmware(argv[1]); + if (base_fw == (void *)(-1)) { + fprintf(stderr, "Error opening binary file '%s'.\n", argv[1]); + exit(2); + } + if (strncmp((char *)base_fw, "WOLF", 4) != 0) { + fprintf(stderr, "Warning: the binary file provided does not appear to contain a valid firmware partition file.\n"); + } else { + printf("%s has a wolfboot manifest header\n", basename(argv[1])); + base_fw_ver = fw_version(base_fw); + printf("%s contains version %u\n", basename(argv[1]), base_fw_ver); + } + serve_update(base_fw, argv[2]); + return 0; +}