1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/riotboot/hdr.c
Francisco Acosta 00adbd69f6 sys: add riotboot_hdr submodule
riotboot_hdr enables to partition the internal flash memory
into "slots", each one with a header providing information
about the partition. The concept for now is limited to
firmware partitions, which are recognised by the riotboot
bootloader. In the future the concept might be extended to
represent other content.

Co-authored-by: Kaspar Schleiser <kaspar@schleiser.de>
2018-12-03 23:58:45 +01:00

69 lines
1.8 KiB
C

/*
* 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));
}