mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
dist/tools/riotboot_gen_hdr/genhdr: add update command
Sometimes we want to roll-back to a previous firmware version. To do so we need a higher riotbot version numbers still. Add an update command to the riotboot_gen_hdr tool so that an existing firmware image can be re-rolled out with a new version number.
This commit is contained in:
parent
45bc3bbcdc
commit
9d350f08c0
23
dist/tools/riotboot_gen_hdr/common.c
vendored
23
dist/tools/riotboot_gen_hdr/common.c
vendored
@ -17,7 +17,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int to_file(const char *filename, void *buf, size_t len)
|
||||
int to_file(const char *filename, const void *buf, size_t len)
|
||||
{
|
||||
int fd;
|
||||
|
||||
@ -37,3 +37,24 @@ int to_file(const char *filename, void *buf, size_t len)
|
||||
return fd;
|
||||
}
|
||||
}
|
||||
|
||||
int from_file(const char *filename, void *buf, size_t len)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (strcmp("-", filename)) {
|
||||
fd = open(filename, O_RDONLY, 0);
|
||||
}
|
||||
else {
|
||||
fd = STDIN_FILENO;
|
||||
}
|
||||
|
||||
if (fd > 0) {
|
||||
ssize_t res = read(fd, buf, len);
|
||||
close(fd);
|
||||
return res;
|
||||
}
|
||||
else {
|
||||
return fd;
|
||||
}
|
||||
}
|
||||
|
15
dist/tools/riotboot_gen_hdr/common.h
vendored
15
dist/tools/riotboot_gen_hdr/common.h
vendored
@ -17,12 +17,23 @@
|
||||
/**
|
||||
* @brief Write len bytes of a given buffer into a file
|
||||
*
|
||||
* @param[out] filename name of the file to be written
|
||||
* @param[in] filename name of the file to be written
|
||||
* @param[in] buf a pointer to the buffer which needs to be written
|
||||
* @param[in] len the number of bytes from buf to be written
|
||||
*
|
||||
*/
|
||||
int to_file(const char *filename, void *buf, size_t len);
|
||||
int to_file(const char *filename, const void *buf, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Read len bytes from a given file into a buffer
|
||||
*
|
||||
* @param[in] filename name of the file to be read
|
||||
* @param[out] buf a pointer to the buffer to store the content
|
||||
* @param[out] len the number of bytes to be read
|
||||
*
|
||||
* @return Number of bytes read, or negative error
|
||||
*/
|
||||
int from_file(const char *filename, const void *buf, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* end extern "C" */
|
||||
|
48
dist/tools/riotboot_gen_hdr/genhdr.c
vendored
48
dist/tools/riotboot_gen_hdr/genhdr.c
vendored
@ -112,3 +112,51 @@ int genhdr(int argc, char *argv[])
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int updatehdr(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "usage: genhdr update <file> <new_version>\n");
|
||||
return -1;
|
||||
}
|
||||
const char *file = argv[1];
|
||||
|
||||
riotboot_hdr_t hdr = { 0 };
|
||||
int res = from_file(file, &hdr, sizeof(hdr));
|
||||
if (res < (int)sizeof(hdr)) {
|
||||
fprintf(stderr, "Can't read header from %s\n", file);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (hdr.magic_number != RIOTBOOT_MAGIC) {
|
||||
fprintf(stderr, "Invalid magic: %x\n", hdr.magic_number);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
hdr.version = atoi(argv[2]);
|
||||
hdr.chksum = riotboot_hdr_checksum(&hdr);
|
||||
to_file(file, &hdr, sizeof(hdr));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int readhdr(const char *file)
|
||||
{
|
||||
riotboot_hdr_t hdr = { 0 };
|
||||
int res = from_file(file, &hdr, sizeof(hdr));
|
||||
if (res < (int)sizeof(hdr)) {
|
||||
fprintf(stderr, "Can't read header from %s\n", file);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (hdr.magic_number != RIOTBOOT_MAGIC) {
|
||||
fprintf(stderr, "Invalid magic: %x\n", hdr.magic_number);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
printf("version: %u\n", hdr.version);
|
||||
printf("address: 0x%x\n", hdr.start_addr);
|
||||
printf("checksum: %svalid\n", riotboot_hdr_validate(&hdr) ? "in" : "");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
14
dist/tools/riotboot_gen_hdr/main.c
vendored
14
dist/tools/riotboot_gen_hdr/main.c
vendored
@ -18,10 +18,14 @@
|
||||
#include <string.h>
|
||||
|
||||
int genhdr(int argc, char *argv[]);
|
||||
int updatehdr(int argc, char *argv[]);
|
||||
int readhdr(const char *file);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *usage = "genhdr generate [args]";
|
||||
char *usage = "genhdr generate [args]\n\t"
|
||||
"genhdr update [file] [new_version]\n\t"
|
||||
"genhdr show [file]";
|
||||
|
||||
if (argc < 2) {
|
||||
goto usage;
|
||||
@ -29,8 +33,14 @@ int main(int argc, char *argv[])
|
||||
else if (!strcmp(argv[1], "generate")) {
|
||||
return genhdr(argc - 1, &argv[1]);
|
||||
}
|
||||
else if (!strcmp(argv[1], "update")) {
|
||||
return updatehdr(argc - 1, &argv[1]);
|
||||
}
|
||||
else if (!strcmp(argv[1], "show")) {
|
||||
return readhdr(argv[2]);
|
||||
}
|
||||
|
||||
usage:
|
||||
fprintf(stderr, "usage: %s\n", usage);
|
||||
fprintf(stderr, "usage:\t%s\n", usage);
|
||||
return 1;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user