Verified Commit f8aafe36 authored by Hugo L'hostis's avatar Hugo L'hostis Committed by Vincent Coubard
Browse files

Use SDK version of the AWS client in keyword


Signed-off-by: Vincent Coubard's avatarVincent Coubard <vincent.coubard@arm.com>
parent 99d8b33c
......@@ -35,7 +35,7 @@ FetchContent_Declare(
FetchContent_Declare(
open_iot_sdk
GIT_REPOSITORY https://git.gitlab.oss.arm.com/engineering/iot-m-sw/open-iot-sdk/sdk.git
GIT_TAG ed118fea4b03ffa339ad7a9a5ee7d476f6a5b2d5
GIT_TAG 4b764827ef181b0c08c4f9d3b6bb244cc2b32e3b
)
# Declare the Open IoT SDK configuration flags
......@@ -49,6 +49,13 @@ set(TFM_PLATFORM "${CMAKE_CURRENT_SOURCE_DIR}/${TFM_PLATFORM_LOCAL_PATH}")
set(TFM_PSA_API OFF)
set(TFM_PARTITION_FIRMWARE_UPDATE ON)
set(TFM_LIB_MODEL ON)
set(IOTSDK_MBEDTLS ON)
set(IOTSDK_AWS_CLIENT ON)
set(IOTSDK_LWIP ON)
set(IOTSDK_LWIP_SOCKETS ON)
set(IOTSDK_IP_NETWORK ON)
set(IOTSDK_LWIP_CMSIS_IP_NETWORK ON)
set(IOTSDK_EXAMPLES OFF)
# Extra arguments for TF-M and ML
set(TFM_CMAKE_ARGS "-DNS=ON;-DPLATFORM_DEFAULT_UART_STDOUT=ON;-DMCUBOOT_SIGNATURE_KEY_LEN=2048;-DCONFIG_TFM_FP=hard;-DCONFIG_TFM_FP_ARCH=fpv5-sp-d16;TFM_SYSTEM_FP=ON;-DTFM_PARTITION_AUDIT_LOG=ON;-DITS_MAX_ASSET_SIZE=1300;-DCRYPTO_ENGINE_BUF_SIZE=0x8000;-DMCUBOOT_LOG_LEVEL=INFO;-DTFM_SPM_LOG_LEVEL=TFM_SPM_LOG_LEVEL_INFO")
......@@ -61,7 +68,7 @@ set(MBED_C_LIB "std")
set(VARIANT "FVP")
# Set platform
set(MDH_PLATFORM "ARM_AN547_MPS3")
set(MDH_PLATFORM "ARM_AN552_MPS3")
# Apply TF-M patch
find_package(Git)
......@@ -113,7 +120,7 @@ target_include_directories(freertos-kernel
)
# Patch the MDH target to enable multicast
target_compile_definitions(mdh-arm-an547-mps3
target_compile_definitions(mdh-arm-an552-mps3
INTERFACE
LAN91C111_RFS_MULTICAST_SUPPORT
)
......@@ -131,6 +138,11 @@ target_compile_definitions(cmsis-core
$<$<STREQUAL:${CMAKE_SYSTEM_PROCESSOR},cortex-m55>:CMSIS_device_header="ARMCM55.h">
)
target_compile_definitions(mcu-driver-hal
INTERFACE
DOMAIN_NS=1
)
# Compilation Database
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_custom_target(database COMMAND ${CMAKE_COMMAND} -E copy_if_different "${INT_DIR}/compile_commands.json" "${CMAKE_BINARY_DIR}")
......@@ -141,7 +153,6 @@ set(HTTP_CONFIG_PATH ${PRJ_DIR}/bsp/aws_configs)
set(PKCS11_CONFIG_PATH ${PRJ_DIR}/bsp/aws_configs)
set(MBEDTLS_CONFIG_PATH ${PRJ_DIR}/bsp/aws_libraries/3rdparty/mbedtls_config)
add_subdirectory(lib/lwip)
add_subdirectory(lib/AWS)
add_subdirectory(bsp)
......
......@@ -8,7 +8,7 @@
#include "bsp_serial.h"
#include "cmsis_os2.h"
#include "hal/gpio_api.h"
#include "mps3_io.h"
extern uint32_t tfm_ns_interface_init(void);
......@@ -16,15 +16,15 @@ static void app_task(void *arg)
{
(void)arg;
gpio_t led_gpio;
gpio_init_out(&led_gpio, LED1);
mps3_io_t led_gpio;
mps3_io_init(&led_gpio, &MPS3_IO_DEV_NS, LED1);
printf("The LED started blinking...\r\n");
while (1) {
gpio_write(&led_gpio, 1);
mdh_gpio_write(&led_gpio.gpio, 1U);
printf("LED on\r\n");
osDelay(1000u);
gpio_write(&led_gpio, 0);
mdh_gpio_write(&led_gpio.gpio, 0U);
printf("LED off\r\n");
osDelay(1000u);
}
......
......@@ -98,6 +98,7 @@ target_sources(${LIB_NAME}
target_link_libraries(${LIB_NAME}
PRIVATE
mcu-driver-hal
FreeRTOS-Plus-TCP
freertos-kernel
tfm-ns-interface
......@@ -186,7 +187,7 @@ add_library(startup
)
set(link_definitions_file ${CMAKE_CURRENT_BINARY_DIR}/link_definitions.txt)
mdh_generate_definitions_for_linker(mdh-arm-an547-mps3-startup ${link_definitions_file})
mdh_generate_definitions_for_linker(mdh-arm-corstone-300-startup ${link_definitions_file})
mdh_set_linker_script(startup "${PRJ_DIR}/bsp/an547_ns.sct" ${link_definitions_file})
FetchContent_GetProperties(tf-m)
......
......@@ -26,24 +26,6 @@
#ifndef __THREADING_ALT_H__
#define __THREADING_ALT_H__
#include "FreeRTOS.h"
#include "semphr.h"
/**
* @brief Mutex struct used to synchronize mbed TLS operations.
*
*/
typedef struct
{
SemaphoreHandle_t mutex; /**< @brief FreeRTOS semaphore. */
char is_valid; /**< @brief Flag used by mbedTLS to track wether a mutex is valid. */
} mbedtls_threading_mutex_t;
extern void mbedtls_threading_set_alt( void ( * mutex_init )( mbedtls_threading_mutex_t * ),
void ( * mutex_free )( mbedtls_threading_mutex_t * ),
int ( * mutex_lock )( mbedtls_threading_mutex_t * ),
int ( * mutex_unlock )( mbedtls_threading_mutex_t * ) );
#include "mbedtls_threading_cmsis_rtos.h"
#endif /* ifndef __THREADING_ALT_H__ */
......@@ -24,7 +24,7 @@
*/
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "cmsis_os2.h"
#include "iot_crypto.h"
/* mbedTLS includes. */
......@@ -86,95 +86,6 @@ typedef struct SignatureVerificationState
}
#endif /* ifdef CONFIG_MEDTLS_USE_AFR_MEMORY */
/*-----------------------------------------------------------*/
/*--------- mbedTLS threading functions for FreeRTOS --------*/
/*--------------- See MBEDTLS_THREADING_ALT -----------------*/
/*-----------------------------------------------------------*/
/**
* @brief Implementation of mbedtls_mutex_init for thread-safety.
*
*/
void aws_mbedtls_mutex_init( mbedtls_threading_mutex_t * mutex )
{
mutex->mutex = xSemaphoreCreateMutex();
if( mutex->mutex != NULL )
{
mutex->is_valid = 1;
}
else
{
mutex->is_valid = 0;
CRYPTO_PRINT( ( "Failed to initialize mbedTLS mutex.\r\n" ) );
}
}
/**
* @brief Implementation of mbedtls_mutex_free for thread-safety.
*
*/
void aws_mbedtls_mutex_free( mbedtls_threading_mutex_t * mutex )
{
if( mutex->is_valid == 1 )
{
vSemaphoreDelete( mutex->mutex );
mutex->is_valid = 0;
}
}
/**
* @brief Implementation of mbedtls_mutex_lock for thread-safety.
*
* @return 0 if successful, MBEDTLS_ERR_THREADING_MUTEX_ERROR if timeout,
* MBEDTLS_ERR_THREADING_BAD_INPUT_DATA if the mutex is not valid.
*/
int aws_mbedtls_mutex_lock( mbedtls_threading_mutex_t * mutex )
{
int ret = MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
if( mutex->is_valid == 1 )
{
if( xSemaphoreTake( mutex->mutex, portMAX_DELAY ) )
{
ret = 0;
}
else
{
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
CRYPTO_PRINT( ( "Failed to obtain mbedTLS mutex.\r\n" ) );
}
}
return ret;
}
/**
* @brief Implementation of mbedtls_mutex_unlock for thread-safety.
*
* @return 0 if successful, MBEDTLS_ERR_THREADING_MUTEX_ERROR if timeout,
* MBEDTLS_ERR_THREADING_BAD_INPUT_DATA if the mutex is not valid.
*/
int aws_mbedtls_mutex_unlock( mbedtls_threading_mutex_t * mutex )
{
int ret = MBEDTLS_ERR_THREADING_BAD_INPUT_DATA;
if( mutex->is_valid == 1 )
{
if( xSemaphoreGive( mutex->mutex ) )
{
ret = 0;
}
else
{
ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
CRYPTO_PRINT( ( "Failed to unlock mbedTLS mutex.\r\n" ) );
}
}
return ret;
}
/*-----------------------------------------------------------*/
/**
......@@ -252,10 +163,7 @@ void CRYPTO_Init( void )
void CRYPTO_ConfigureThreading( void )
{
/* Configure mbedtls to use FreeRTOS mutexes. */
mbedtls_threading_set_alt( aws_mbedtls_mutex_init,
aws_mbedtls_mutex_free,
aws_mbedtls_mutex_lock,
aws_mbedtls_mutex_unlock );
mbedtls_threading_set_cmsis_rtos();
}
/**
......
......@@ -9,12 +9,20 @@
#include "FreeRTOS.h"
#include "task.h"
#include "FreeRTOS_IP.h"
/* includes for TFM */
#include "tfm_ns_interface.h"
#include "psa/protected_storage.h"
#include "psa/crypto.h"
#include "emac_cs300.h"
/* Provide EMAC interface to LwIP. This is required until MDH provides a factory
* function for it.
*/
mdh_emac_t *lwip_emac_get_default_instance(void)
{
return cs300_emac_get_default_instance();
}
/* -----------------------------------------------------------------------------
* Helper declarations
......
/* Copyright (c) 2021 Arm Limited
* SPDX-License-Identifier: Apache-2.0
*/
#include "lwip/opt.h"
#include "lwip/netif.h"
#include "lwip/pbuf.h"
#include "lwip/tcpip.h"
#include "lwip/ethip6.h"
#include "lwip/etharp.h"
#include "netif/ppp/pppoe.h"
#include "lwip_memory_manager.h"
#include "lwip_stack.h"
#define US_TO_TICKS( xTimeInMs ) ( ( TickType_t ) ( ( ( TickType_t ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000000U ) )
typedef struct pbuf pbuf_t;
extern mdh_emac_t *cs300_emac_get_default_instance(void);
#if LWIP_IGMP
#include "lwip/igmp.h"
/**
* IPv4 address filtering setup.
*
* \param[in] netif the lwip network interface structure
* \param[in] group IPv4 group to modify
* \param[in] action
* \return ERR_OK or error code
*/
static err_t emac_igmp_mac_filter(struct netif *netif, const ip4_addr_t *group, enum netif_mac_filter_action action)
{
netif_context_t *netif_context_t = netif->state;
switch (action) {
case NETIF_DEL_MAC_FILTER:
case NETIF_ADD_MAC_FILTER: {
uint32_t group23 = ntohl(group->addr) & 0x007FFFFF;
uint8_t addr[6];
addr[0] = LL_IP4_MULTICAST_ADDR_0;
addr[1] = LL_IP4_MULTICAST_ADDR_1;
addr[2] = LL_IP4_MULTICAST_ADDR_2;
addr[3] = group23 >> 16;
addr[4] = group23 >> 8;
addr[5] = group23;
mdh_emac_status_t ret;
if (action == NETIF_ADD_MAC_FILTER) {
ret = mdh_emac_add_to_multicast_group(netif_context_t->emac, addr);
} else {
ret = mdh_emac_remove_from_multicast_group(netif_context_t->emac, addr);
}
return (ret == MDH_EMAC_STATUS_NO_ERROR) ? ERR_OK : ERR_IF;
}
default:
return ERR_ARG;
}
}
#endif
#if LWIP_IPV6_MLD
#include "lwip/mld6.h"
/**
* IPv6 address filtering setup.
*
* \param[in] netif the lwip network interface structure
* \param[in] group IPv6 group to modify
* \param[in] action
* \return ERR_OK or error code
*/
static err_t emac_mld_mac_filter(struct netif *netif, const ip6_addr_t *group, enum netif_mac_filter_action action)
{
netif_context_t *netif_context_t = netif->state;
switch (action) {
case NETIF_DEL_MAC_FILTER:
case NETIF_ADD_MAC_FILTER: {
uint32_t group32 = ntohl(group->addr[3]);
uint8_t addr[6];
addr[0] = LL_IP6_MULTICAST_ADDR_0;
addr[1] = LL_IP6_MULTICAST_ADDR_1;
addr[2] = group32 >> 24;
addr[3] = group32 >> 16;
addr[4] = group32 >> 8;
addr[5] = group32;
mdh_emac_status_t ret;
if (action == NETIF_ADD_MAC_FILTER) {
ret = mdh_emac_add_to_multicast_group(netif_context_t->emac, addr);
} else {
ret = mdh_emac_remove_from_multicast_group(netif_context_t->emac, addr);
}
return (ret == MDH_EMAC_STATUS_NO_ERROR) ? ERR_OK : ERR_IF;
}
default:
return ERR_ARG;
}
}
#endif
/* this is called from ISR to let us know that input is ready*/
static void ethernetif_input(mdh_emac_t *self, void *ctx, mdh_emac_receive_t status)
{
if (status == MDH_EMAC_RECEIVE_DONE) {
netif_context_t *context = (netif_context_t *)(ctx);
signal_receive(context);
}
}
static err_t ethernetif_output(struct netif *netif, struct pbuf *p)
{
netif_context_t *context = netif->state;
mdh_network_stack_buffer_t *buf = (mdh_network_stack_buffer_t *)p;
if (p->next) {
/* if it's not a single chunk, allocate a new buf to hold the whole payload */
mdh_network_stack_memory_manager_t *memory_manager = lwip_mm_get_instance();
buf = mdh_network_stack_memory_manager_alloc_from_heap(
memory_manager,
mdh_network_stack_memory_manager_get_payload_len(memory_manager, buf),
mdh_emac_get_align(context->emac)
);
if (!buf) {
return ERR_IF;
}
mdh_network_stack_memory_manager_copy(memory_manager, (mdh_network_stack_buffer_t *)p, buf);
} else {
/* Increase reference counter since lwip stores handle to pbuf and frees it after output */
pbuf_ref(p);
}
mdh_emac_status_t ret = mdh_emac_transmit(context->emac, buf);
return (ret == MDH_EMAC_STATUS_NO_ERROR) ? ERR_OK : ERR_IF;
}
static void ethernetif_sent(mdh_emac_t *self, void *ctx, mdh_emac_transfer_t status, const mdh_network_stack_buffer_t *buffer)
{
if (status == MDH_EMAC_TRANSFER_ERROR) {
//@TODO react?
}
mdh_network_stack_memory_manager_free(lwip_mm_get_instance(), buffer);
}
static void emac_state_change(mdh_emac_t *self, void *ctx, mdh_emac_event_t event)
{
/* this does not work because the handler fails to give us the context */
netif_context_t *context = (netif_context_t *)(ctx);
if (event == MDH_EMAC_EVENT_LINK_STATUS_CHANGE) {
/* @TODO missing event types so we infer state based on netif state instead,
* if we were down this means we just went up */
if (netif_is_link_up(&context->lwip_netif)) {
tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_down, &context->lwip_netif, 1);
} else {
tcpip_callback_with_block((tcpip_callback_fn)netif_set_link_up, &context->lwip_netif, 1);
}
}
}
void ethernetif_process_input(netif_context_t *context)
{
struct pbuf *buf = mdh_network_stack_memory_manager_alloc_from_heap(
lwip_mm_get_instance(),
mdh_emac_get_mtu(context->emac),
mdh_emac_get_align(context->emac)
);
if (!buf) {
LWIP_DEBUGF(NETIF_DEBUG, ("Emac LWIP: pbuf allocation failed in ethernetif_process_input\n"));
return;
}
mdh_emac_receive(context->emac, buf);
/* pass all packets to ethernet_input, which decides what packets it supports */
if (context->lwip_netif.input(buf, &context->lwip_netif) != ERR_OK) {
mdh_network_stack_memory_manager_free(lwip_mm_get_instance(), buf);
}
}
err_t ethernetif_init(struct netif *netif)
{
int err = ERR_OK;
LWIP_ASSERT("netif != NULL", (netif != NULL));
netif_context_t *context = netif->state;
mdh_emac_t *emac = cs300_emac_get_default_instance();
context->emac = emac;
/* Interface capabilities */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET;
mdh_network_stack_memory_manager_t *memory_manager = lwip_mm_get_instance();
static const mdh_emac_callbacks_t cbks = {
.rx = ethernetif_input, .tx = ethernetif_sent, .event = emac_state_change
};
if (MDH_EMAC_STATUS_NO_ERROR != mdh_emac_power_up(context->emac, &cbks, memory_manager, context)) {
err = ERR_IF;
}
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif->hostname = "lwip";
#endif /* LWIP_NETIF_HOSTNAME */
netif->name[0] = 'e';
netif->name[1] = 'n';
netif->mtu = mdh_emac_get_mtu(emac);
/* We have a default MAC address, so do don't force them to supply one */
netif->hwaddr_len = mdh_emac_get_mac_addr_size(emac);
/* They may or may not update hwaddr with their address */
mdh_emac_get_mac_addr(emac, netif->hwaddr);
/* Then we write back either what they gave us, or our default */
mdh_emac_set_mac_addr(emac, netif->hwaddr);
#if LWIP_IPV4
netif->output = etharp_output;
#if LWIP_IGMP
netif->igmp_mac_filter = emac_igmp_mac_filter;
netif->flags |= NETIF_FLAG_IGMP;
#endif /* LWIP_IGMP */
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#if LWIP_IPV6_MLD
netif->mld_mac_filter = emac_mld_mac_filter;
netif->flags |= NETIF_FLAG_MLD6;
#else
// Would need to enable all multicasts here - no API in fsl_enet to do that
#error "IPv6 multicasts won't be received if LWIP_IPV6_MLD is disabled, breaking the system"
#endif
#endif
netif->linkoutput = ethernetif_output;
return err;
}
/* Temporarily override definitions from MCU driver HAL as they don't work in NS
mode */
void $Sub$$us_ticker_init(void)
{
}
void $Sub$$us_ticker_util_wait(uint32_t us)
{
vTaskDelay(US_TO_TICKS(us));
}
/* Copyright (c) 2021 Arm Limited
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdbool.h>
#include <string.h>
#include "pbuf.h"
#include "lwipopts.h"
#include "hal/emac_api.h"
#include "lwip_memory_manager.h"
static mdh_network_stack_buffer_t *
lwip_mm_alloc_from_heap(mdh_network_stack_memory_manager_t *const self, size_t size, size_t align);
static mdh_network_stack_buffer_t *
lwip_mm_alloc_from_static_pool(mdh_network_stack_memory_manager_t *const self, size_t size, size_t align);
static size_t lwip_mm_get_static_pool_alloc_unit(mdh_network_stack_memory_manager_t *const self, size_t align);
static void lwip_mm_free(mdh_network_stack_memory_manager_t *const self, const mdh_network_stack_buffer_t *buf);
static void lwip_mm_copy(mdh_network_stack_memory_manager_t *const self,
const mdh_network_stack_buffer_t *const source,
const mdh_network_stack_buffer_t *destination);
static size_t lwip_mm_copy_to_buf(mdh_network_stack_memory_manager_t *const self,
const void *const ptr,
size_t size,
const mdh_network_stack_buffer_t *buf);
static size_t lwip_mm_copy_from_buf(mdh_network_stack_memory_manager_t *const self,
const mdh_network_stack_buffer_t *const buf,
void *const ptr,
size_t size);
static void *lwip_mm_get_payload(mdh_network_stack_memory_manager_t *const self,
const mdh_network_stack_buffer_t *const buf);
static size_t lwip_mm_get_payload_len(mdh_network_stack_memory_manager_t *const self,
const mdh_network_stack_buffer_t *const buf);
static void lwip_mm_set_payload_len(mdh_network_stack_memory_manager_t *const self,
const mdh_network_stack_buffer_t *buf,
size_t size);
typedef struct pbuf pbuf_t;
typedef struct lwip_mm_instance {
mdh_network_stack_memory_manager_t memory_manager;
} lwip_mm_instance;
static const mdh_network_stack_memory_manager_vtable_t memory_manager_vtable = {
.alloc_from_heap = lwip_mm_alloc_from_heap,
.alloc_from_static_pool = lwip_mm_alloc_from_static_pool,
.get_static_pool_alloc_unit = lwip_mm_get_static_pool_alloc_unit,
.free = lwip_mm_free,
.copy = lwip_mm_copy,
.copy_to_buf = lwip_mm_copy_to_buf,
.copy_from_buf = lwip_mm_copy_from_buf,
.get_payload = lwip_mm_get_payload,
.get_payload_len = lwip_mm_get_payload_len,
.set_payload_len = lwip_mm_set_payload_len,
};
static lwip_mm_instance instance = {.memory_manager.vtable = &memory_manager_vtable};
static uint32_t count_total_align(uint32_t size, uint32_t align)
{
uint32_t buffers = size / (LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) - align);
if (size % (LWIP_MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE) - align) != 0) {
buffers++;
}
return buffers * align;
}
static void set_total_len(pbuf_t *pbuf)
{
if (!pbuf->next) {
pbuf->tot_len = pbuf->len;