Skip to content

Commit

Permalink
suit/transport/vfs: add VFS as source for firmware updates
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed May 2, 2022
1 parent 3175813 commit a4e1cd6
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 3 deletions.
4 changes: 4 additions & 0 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,10 @@ ifneq (,$(filter suit_transport_coap, $(USEMODULE)))
USEMODULE += sock_util
endif

ifneq (,$(filter suit_transport_vfs, $(USEMODULE)))
USEMODULE += vfs_util
endif

ifneq (,$(filter suit_storage_%, $(USEMODULE)))
USEMODULE += suit_storage
endif
Expand Down
47 changes: 47 additions & 0 deletions sys/include/suit/transport/vfs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2022 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup sys_suit_transport_vfs SUIT secure firmware OTA VFS transport
* @ingroup sys_suit
* @brief SUIT firmware VFS transport
*
* @{
*
* @brief VFS transport backend definitions for SUIT manifests
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*
*/

#ifndef SUIT_TRANSPORT_VFS_H
#define SUIT_TRANSPORT_VFS_H

#include "net/nanocoap.h"
#include "suit.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief fetch a payload from the filesystem
*
* @param[in] manifest suit manifest context
* @param[in] cb filesystem block callback
* @param[in] ctx callback context
*
* @returns SUIT_OK if valid
* @returns negative otherwise
*/
int suit_transport_vfs_fetch(const suit_manifest_t *manifest, coap_blockwise_cb_t cb, void *ctx);

#ifdef __cplusplus
}
#endif

#endif /* SUIT_TRANSPORT_VFS_H */
/** @} */
8 changes: 8 additions & 0 deletions sys/suit/handlers_command_seq.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include "suit/transport/coap.h"
#include "net/nanocoap_sock.h"
#endif
#ifdef MODULE_SUIT_TRANSPORT_VFS
#include "suit/transport/vfs.h"
#endif
#include "suit/transport/mock.h"

#if defined(MODULE_PROGRESS_BAR)
Expand Down Expand Up @@ -441,6 +444,11 @@ static int _dtv_fetch(suit_manifest_t *manifest, int key,
else if (strncmp(manifest->urlbuf, "test://", 7) == 0) {
res = suit_transport_mock_fetch(manifest);
}
#endif
#ifdef MODULE_SUIT_TRANSPORT_VFS
else if (strncmp(manifest->urlbuf, "file:/", 6) == 0) {
res = suit_transport_vfs_fetch(manifest, _storage_helper, manifest);
}
#endif
else {
LOG_WARNING("suit: unsupported URL scheme!\n)");
Expand Down
28 changes: 25 additions & 3 deletions sys/suit/transport/coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@
#include "periph/pm.h"
#include "ztimer.h"

#ifdef MODULE_SUIT_TRANSPORT_COAP
#include "suit/transport/coap.h"
#include "net/sock/util.h"
#endif
#ifdef MODULE_SUIT_TRANSPORT_VFS
#include "vfs_util.h"
#endif

#ifdef MODULE_RIOTBOOT_SLOT
#include "riotboot/slot.h"
Expand Down Expand Up @@ -81,10 +86,27 @@ static kernel_pid_t _suit_coap_pid;

static void _suit_handle_url(const char *url, coap_blksize_t blksize)
{
ssize_t size;
LOG_INFO("suit_coap: downloading \"%s\"\n", url);
ssize_t size = nanocoap_get_blockwise_url_to_buf(url, blksize,
_manifest_buf,
SUIT_MANIFEST_BUFSIZE);

if (0) {}
#ifdef MODULE_SUIT_TRANSPORT_COAP
else if (strncmp(url, "coap://", 7) == 0) {
size = nanocoap_get_blockwise_url_to_buf(url, blksize,
_manifest_buf,
sizeof(_manifest_buf));
}
#endif
#ifdef MODULE_SUIT_TRANSPORT_VFS
else if (strncmp(url, "file:/", 6) == 0) {
size = vfs_file_to_buffer(&url[6], _manifest_buf, sizeof(_manifest_buf));
}
#endif
else {
LOG_WARNING("suit_coap: unsupported URL scheme!\n)");
return;
}

if (size >= 0) {
LOG_INFO("suit_coap: got manifest with size %u\n", (unsigned)size);

Expand Down
60 changes: 60 additions & 0 deletions sys/suit/transport/vfs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2022 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup sys_suit_transport_vfs
* @{
*
* @fil
* @brief SUIT VFS
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
* @}
*/

#include <fcntl.h>
#include <string.h>

#include "suit/transport/vfs.h"
#include "vfs.h"

#define ENABLE_DEBUG 0
#include "debug.h"

int suit_transport_vfs_fetch(const suit_manifest_t *manifest, coap_blockwise_cb_t cb, void *ctx)
{
const char *file = &manifest->urlbuf[6];
size_t offset = 0;
int res, fd;

fd = vfs_open(file, O_RDONLY, 0);
if (fd < 0) {
return fd;
}

uint8_t buf[128];
while ((res = vfs_read(fd, buf, sizeof(buf))) > 0) {
size_t len = res;
res = cb(ctx, offset, buf, len, 1);
if (res < 0) {
DEBUG("suit_vfs: write failed with %d\n", res);
break;
}
offset += len;
}

if (res < 0) {
DEBUG("suit_vfs: read failed with %d\n", res);
} else {
res = cb(ctx, offset, buf, 0, 0);
}

vfs_close(fd);

return res;
}

0 comments on commit a4e1cd6

Please sign in to comment.