1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:52:59 +01:00

Merge pull request #10253 from kYc0o/pr/riot_hdr

sys: add riotboot_hdr module
This commit is contained in:
Kaspar Schleiser 2018-12-04 09:44:45 +01:00 committed by GitHub
commit 90db0bf253
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 287 additions and 1 deletions

View File

@ -792,6 +792,11 @@ ifneq (,$(filter uuid,$(USEMODULE)))
USEMODULE += fmt
endif
ifneq (,$(filter riotboot_hdr, $(USEMODULE)))
USEMODULE += checksum
USEMODULE += riotboot
endif
# Enable periph_gpio when periph_gpio_irq is enabled
ifneq (,$(filter periph_gpio_irq,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio

View File

@ -55,6 +55,7 @@ PSEUDOMODULES += pktqueue
PSEUDOMODULES += printf_float
PSEUDOMODULES += prng
PSEUDOMODULES += prng_%
PSEUDOMODULES += riotboot_%
PSEUDOMODULES += saul_adc
PSEUDOMODULES += saul_default
PSEUDOMODULES += saul_gpio

View File

@ -134,7 +134,6 @@ ifneq (,$(filter cord_ep,$(USEMODULE)))
DIRS += net/application_layer/cord/ep
endif
DIRS += $(dir $(wildcard $(addsuffix /Makefile, $(USEMODULE))))
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,89 @@
/*
* Copyright (C) 2018 Kaspar Schleiser <kaspar@schleiser.de>
* Inria
*
* 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_riotboot_hdr RIOT header helpers and tools
* @ingroup sys
* @{
*
* The header contains
*
* - "RIOT" as magic number
* - the application version
* - the address where the RIOT firmware is found
* - the checksum of the three previous fields
*
* @file
* @brief RIOT "partition" header and tools
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Francisco Acosta <francisco.acosta@inria.fr>
*
* @}
*/
#ifndef RIOTBOOT_HDR_H
#define RIOTBOOT_HDR_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/**
* @brief Magic number for riotboot_hdr
*
*/
#define RIOTBOOT_MAGIC 0x544f4952 /* "RIOT" */
/**
* @brief Structure to store image header - All members are little endian
* @{
*/
typedef struct {
uint32_t magic_number; /**< Header magic number (always "RIOT") */
uint32_t version; /**< Integer representing the partition version */
uint32_t start_addr; /**< Address after the allocated space for the header */
uint32_t chksum; /**< Checksum of riotboot_hdr */
} riotboot_hdr_t;
/** @} */
/**
* @brief Print formatted riotboot_hdr_t to STDIO
*
* @param[in] riotboot_hdr ptr to image header
*
*/
void riotboot_hdr_print(const riotboot_hdr_t *riotboot_hdr);
/**
* @brief Validate image header
*
* @param[in] riotboot_hdr ptr to image header
*
* @returns 0 if OK
* @returns -1 if not OK
*/
int riotboot_hdr_validate(const riotboot_hdr_t *riotboot_hdr);
/**
* @brief Calculate header checksum
*
* @param[in] riotboot_hdr ptr to image header
*
* @returns the checksum of the given riotboot_hdr
*/
uint32_t riotboot_hdr_checksum(const riotboot_hdr_t *riotboot_hdr);
#ifdef __cplusplus
}
#endif
#endif /* RIOTBOOT_HDR_H */

3
sys/riotboot/Makefile Normal file
View File

@ -0,0 +1,3 @@
SUBMODULES := 1
include $(RIOTBASE)/Makefile.base

68
sys/riotboot/hdr.c Normal file
View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
* Inria
*
* 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_riotboot_hdr
* @{
*
* @file
* @brief RIOT header helpers and tools
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Francisco Acosta <francisco.acosta@inria.fr>
*
* @}
*/
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#ifdef RIOT_VERSION
#include "log.h"
#else
#define LOG_INFO(...) printf(__VA_ARGS__)
#endif
#include "riotboot/hdr.h"
#include "checksum/fletcher32.h"
#include "byteorder.h"
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
# error "This code is implementented in a way that it will only work for little-endian systems!"
#endif
void riotboot_hdr_print(const riotboot_hdr_t *riotboot_hdr)
{
printf("Image magic_number: 0x%08x\n", (unsigned)riotboot_hdr->magic_number);
printf("Image Version: 0x%08x\n", (unsigned)riotboot_hdr->version);
printf("Image start address: 0x%08x\n", (unsigned)riotboot_hdr->start_addr);
printf("Header chksum: 0x%08x\n", (unsigned)riotboot_hdr->chksum);
printf("\n");
}
int riotboot_hdr_validate(const riotboot_hdr_t *riotboot_hdr)
{
if (riotboot_hdr->magic_number != RIOTBOOT_MAGIC) {
LOG_INFO("%s: riotboot_hdr magic number invalid\n", __func__);
return -1;
}
int res = riotboot_hdr_checksum(riotboot_hdr) == riotboot_hdr->chksum ? 0 : -1;
if (res) {
LOG_INFO("%s: riotboot_hdr checksum invalid\n", __func__);
}
return res;
}
uint32_t riotboot_hdr_checksum(const riotboot_hdr_t *riotboot_hdr)
{
return fletcher32((uint16_t *)riotboot_hdr, offsetof(riotboot_hdr_t, chksum) / sizeof(uint16_t));
}

View File

@ -0,0 +1,10 @@
include ../Makefile.tests_common
USEMODULE += riotboot_hdr
USEMODULE += embunit
HDR_LOG_LEVEL ?= LOG_NONE
CFLAGS += -DLOG_LEVEL=$(HDR_LOG_LEVEL)
include $(RIOTBASE)/Makefile.include

93
tests/riotboot_hdr/main.c Normal file
View File

@ -0,0 +1,93 @@
/*
* Copyright (C) 2018 Inria
*
* 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 tests
* @{
*
* @file
* @brief Tests for module riotboot_hdr
*
* @author Francisco Acosta <francisco.acosta@inria.fr>
*/
#include <stdio.h>
#include "riotboot/hdr.h"
#include "embUnit.h"
const riotboot_hdr_t riotboot_hdr_good = {
.magic_number = RIOTBOOT_MAGIC,
.version = 0x5bd19bff,
.start_addr = 0x00001100,
.chksum = 0x02eda672
};
const riotboot_hdr_t riotboot_hdr_bad_magic = {
.magic_number = 0x12345678,
.version = 0x5bd19bff,
.start_addr = 0x00001100,
.chksum = 0x02eda672
};
const riotboot_hdr_t riotboot_hdr_bad_chksum = {
.magic_number = RIOTBOOT_MAGIC,
.version = 0x5bd19bff,
.start_addr = 0x00001100,
.chksum = 0x02000000
};
static void test_riotboot_hdr_01(void)
{
int ret = riotboot_hdr_validate(&riotboot_hdr_good);
TEST_ASSERT_EQUAL_INT(0, ret);
}
static void test_riotboot_hdr_02(void)
{
int ret = riotboot_hdr_validate(&riotboot_hdr_bad_magic);
TEST_ASSERT_EQUAL_INT(-1, ret);
}
static void test_riotboot_hdr_03(void)
{
int ret = riotboot_hdr_validate(&riotboot_hdr_bad_chksum);
TEST_ASSERT_EQUAL_INT(-1, ret);
}
static void test_riotboot_hdr_04(void)
{
uint32_t chksum = riotboot_hdr_checksum(&riotboot_hdr_good);
TEST_ASSERT_EQUAL_INT(0x02eda672, chksum);
}
Test *tests_riotboot_hdr(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_riotboot_hdr_01),
new_TestFixture(test_riotboot_hdr_02),
new_TestFixture(test_riotboot_hdr_03),
new_TestFixture(test_riotboot_hdr_04),
};
EMB_UNIT_TESTCALLER(riotboot_hdr_tests, NULL, NULL, fixtures);
return (Test *)&riotboot_hdr_tests;
}
int main(void)
{
TESTS_START();
TESTS_RUN(tests_riotboot_hdr());
TESTS_END();
return 0;
}

View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
# Copyright (C) 2018 Francisco Acosta <francisco.acosta@inria.fr>
#
# 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.
import sys
from testrunner import run
def testfunc(child):
child.expect(r"OK \(\d+ tests\)")
if __name__ == "__main__":
sys.exit(run(testfunc))