Skip to content

Commit

Permalink
dist/tools/riotboot_gen_hdr/genhdr: add update command
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benpicco committed Jan 9, 2024
1 parent 45bc3bb commit 4e0af10
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
23 changes: 22 additions & 1 deletion dist/tools/riotboot_gen_hdr/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 changes: 13 additions & 2 deletions dist/tools/riotboot_gen_hdr/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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" */
Expand Down
48 changes: 48 additions & 0 deletions dist/tools/riotboot_gen_hdr/genhdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,51 @@ int genhdr(int argc, char *argv[])

return 0;
}

int updatehdr(int argc, char *argv[])
{
if (argc < 2) {
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;
}
8 changes: 8 additions & 0 deletions dist/tools/riotboot_gen_hdr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#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[])
{
Expand All @@ -29,6 +31,12 @@ 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);
Expand Down

0 comments on commit 4e0af10

Please sign in to comment.