* [PATCH v2 1/4] imd: replace magicvalue with sizeof(struct)
@ 2019-12-09 10:42 Steffen Trumtrar
2019-12-09 10:42 ` [PATCH v2 2/4] image-metadata: add flags field to imd_header Steffen Trumtrar
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Steffen Trumtrar @ 2019-12-09 10:42 UTC (permalink / raw)
To: Barebox List
Instead of using "8" as the size of an imd_header, use the sizeof operator.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
common/imd.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/common/imd.c b/common/imd.c
index 913a01de87bf..e0dab69644c0 100644
--- a/common/imd.c
+++ b/common/imd.c
@@ -41,7 +41,7 @@ const struct imd_header *imd_next(const struct imd_header *imd)
length = imd_read_length(imd);
length = ALIGN(length, 4);
- length += 8;
+ length += sizeof(struct imd_header);
return (const void *)imd + length;
}
@@ -63,14 +63,14 @@ static int imd_next_validate(const void *buf, int bufsize, int start_ofs)
size = bufsize - start_ofs;
- if (size < 8) {
+ if (size < sizeof(struct imd_header)) {
debug("trunkated tag at offset %dd\n", start_ofs);
return -EINVAL;
}
length = imd_read_length(imd);
length = ALIGN(length, 4);
- length += 8;
+ length += sizeof(struct imd_header);
if (size < length) {
debug("tag at offset %d with size %d exceeds bufsize %d\n",
--
2.24.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/4] image-metadata: add flags field to imd_header
2019-12-09 10:42 [PATCH v2 1/4] imd: replace magicvalue with sizeof(struct) Steffen Trumtrar
@ 2019-12-09 10:42 ` Steffen Trumtrar
2019-12-09 11:39 ` Sascha Hauer
2019-12-09 10:42 ` [PATCH v2 3/4] imd: add support for checksum generation/verification Steffen Trumtrar
2019-12-09 10:42 ` [PATCH v2 4/4] commands: bbu: add support for imd checksum Steffen Trumtrar
2 siblings, 1 reply; 5+ messages in thread
From: Steffen Trumtrar @ 2019-12-09 10:42 UTC (permalink / raw)
To: Barebox List
To allow marking an imd tag with properties, add a flags field to the imd_header.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
include/image-metadata.h | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/include/image-metadata.h b/include/image-metadata.h
index 5904d95acd37..ca73e6cf6ec6 100644
--- a/include/image-metadata.h
+++ b/include/image-metadata.h
@@ -28,6 +28,8 @@
#define IMD_TYPE_END 0x640c7fff
#define IMD_TYPE_INVALID 0xffffffff
+#define IMD_FLAG_TAG_VALID (1 << 0)
+
/*
* The IMD header. All data is stored in little endian format in the image.
* The next header starts at the next 4 byte boundary after the data.
@@ -35,6 +37,7 @@
struct imd_header {
uint32_t type; /* One of IMD_TYPE_* above */
uint32_t datalength; /* Length of the data (exluding the header) */
+ uint32_t flags;
};
/*
@@ -51,6 +54,11 @@ static inline int imd_is_string(uint32_t type)
return (type & 0x8000) ? 1 : 0;
}
+static inline int imd_tag_is_valid(uint32_t flags)
+{
+ return (flags & IMD_FLAG_TAG_VALID) ? 1 : 0;
+}
+
static inline int imd_type_valid(uint32_t type)
{
return (type & 0xffff0000) == 0x640c0000;
@@ -78,6 +86,11 @@ static inline uint32_t imd_read_length(const struct imd_header *imd)
return imd_read_le32(&imd->datalength);
}
+static inline uint32_t imd_read_flags(const struct imd_header *imd)
+{
+ return imd_read_le32(&imd->flags);
+}
+
const struct imd_header *imd_find_type(const struct imd_header *imd,
uint32_t type);
--
2.24.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 3/4] imd: add support for checksum generation/verification
2019-12-09 10:42 [PATCH v2 1/4] imd: replace magicvalue with sizeof(struct) Steffen Trumtrar
2019-12-09 10:42 ` [PATCH v2 2/4] image-metadata: add flags field to imd_header Steffen Trumtrar
@ 2019-12-09 10:42 ` Steffen Trumtrar
2019-12-09 10:42 ` [PATCH v2 4/4] commands: bbu: add support for imd checksum Steffen Trumtrar
2 siblings, 0 replies; 5+ messages in thread
From: Steffen Trumtrar @ 2019-12-09 10:42 UTC (permalink / raw)
To: Barebox List
Add a new imd type "checksum". This type consists of the CRC32 checksum
of the whole barebox image minus the checksum itself.
The checksum can be written to the imd field with the bareboximd host-tool.
It can be verified with said tool or with "imd" on the target.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
Changes since V1:
- add some comments
- change the crc32 calculation over the image for better readability
commands/imd.c | 1 +
common/imd-barebox.c | 1 +
common/imd.c | 135 ++++++++++++++++++++++++++++++++++++++-
include/image-metadata.h | 22 +++++++
scripts/bareboximd.c | 32 ++++++++++
5 files changed, 190 insertions(+), 1 deletion(-)
diff --git a/commands/imd.c b/commands/imd.c
index f1a22cef96bd..16ab7290c920 100644
--- a/commands/imd.c
+++ b/commands/imd.c
@@ -46,6 +46,7 @@ BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-t <type>", "only show information of <type>")
BAREBOX_CMD_HELP_OPT ("-n <no>", "for tags with multiple strings only show string <no>")
BAREBOX_CMD_HELP_OPT ("-s VARNAME", "set variable VARNAME instead of showing information")
+BAREBOX_CMD_HELP_OPT ("-V", "Verify checksum of image")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Without options all information available is printed. Valid types are:")
BAREBOX_CMD_HELP_TEXT("release, build, model, of_compatible")
diff --git a/common/imd-barebox.c b/common/imd-barebox.c
index e9cd37d83ec8..4aec51bfbdc2 100644
--- a/common/imd-barebox.c
+++ b/common/imd-barebox.c
@@ -23,3 +23,4 @@ __BAREBOX_IMD_SECTION(.barebox_imd_end) = {
BAREBOX_IMD_TAG_STRING(imd_build_tag, IMD_TYPE_BUILD, UTS_VERSION, 1);
BAREBOX_IMD_TAG_STRING(imd_release_tag, IMD_TYPE_RELEASE, UTS_RELEASE, 1);
+BAREBOX_IMD_CRC(imd_checksum, 0x0, 1);
diff --git a/common/imd.c b/common/imd.c
index e0dab69644c0..280d055d2bab 100644
--- a/common/imd.c
+++ b/common/imd.c
@@ -22,6 +22,7 @@
#include <getopt.h>
#include <malloc.h>
#include <fs.h>
+#include <crc.h>
#ifndef CONFIG_CMD_IMD
int imd_command_setenv(const char *variable_name, const char *value)
@@ -167,6 +168,9 @@ static struct imd_type_names imd_types[] = {
}, {
.type = IMD_TYPE_OF_COMPATIBLE,
.name = "of_compatible",
+ }, {
+ .type = IMD_TYPE_CHECKSUM,
+ .name = "checksum",
},
};
@@ -287,6 +291,118 @@ const char *imd_get_param(const struct imd_header *imd, const char *name)
return NULL;
}
+static int imd_calculate_crc32(void *input, const struct imd_header *imd_start,
+ struct imd_header **imd_crc, uint32_t *crc,
+ size_t size)
+{
+ const struct imd_header *imd;
+ int length;
+ int end_ofs = (char *)imd_start - (char *)input + sizeof(char) * 8;
+
+ /* search the checksum imd token */
+ imd_for_each(imd_start, imd) {
+ length = imd_read_length(imd);
+ length = ALIGN(length, 4);
+ length += sizeof(struct imd_header);
+
+ if (imd_read_type(imd) == IMD_TYPE_CHECKSUM) {
+ *imd_crc = (struct imd_header *)imd;
+ debug("Found crc token at %d\n", end_ofs);
+ break;
+ }
+
+ end_ofs += length;
+ }
+
+ /*
+ * Calculate checksum from start of input up to the checksum.
+ * The checksum and the flags field in the header are modified
+ * after the checksum is calculated. Therefore skip them here
+ * or the checksum will become invalid once it is written to the
+ * checksum tag.
+ */
+ length = imd_read_length(*imd_crc);
+ length = ALIGN(length, 4);
+ length += sizeof(struct imd_header) - sizeof(uint32_t);
+ end_ofs += length;
+
+ *crc = crc32(*crc, input, end_ofs);
+ debug("Calculated checksum from %d to %d: 0x%08x\n", 0, end_ofs, *crc);
+
+ /* move past the imd_header flags and the actual checksum data field */
+ end_ofs += sizeof(uint32_t) + sizeof(uint32_t);
+ input += end_ofs;
+
+ *crc = crc32(*crc, input, size - end_ofs);
+ debug("Calculated checksum from %d to %d: 0x%08x\n", end_ofs,
+ end_ofs + (size - end_ofs), *crc);
+
+ return 0;
+}
+
+static int imd_write_crc32(void *buf, const struct imd_header *imd_start,
+ const char *filename, size_t size)
+{
+ struct imd_header *imd_crc;
+ uint32_t crc = 0;
+
+ imd_calculate_crc32(buf, imd_start, &imd_crc, &crc, size);
+ debug("Calculated crc: 0x%08x\n", crc);
+
+ if (!imd_crc) {
+ debug("No tag of type 0x%08x found\n", IMD_TYPE_CHECKSUM);
+
+ return -ENODATA;
+ } else {
+ uint32_t *p = (uint32_t *)(imd_crc + 1);
+
+ if (*p != crc) {
+ imd_crc->flags |= IMD_FLAG_TAG_VALID;
+ debug("Update crc token from 0x%08x to 0x%08x\n", *p, crc);
+ *p = crc;
+
+ write_file(filename, buf, size);
+ }
+ }
+
+ return 0;
+};
+
+int imd_verify_crc32(void *buf, size_t size)
+{
+ const struct imd_header *imd_start;
+ struct imd_header *imd_crc;
+ uint32_t crc = 0;
+
+ imd_start = imd_get(buf, size);
+ if (IS_ERR(imd_start))
+ return PTR_ERR(imd_start);
+
+ imd_calculate_crc32(buf, imd_start, &imd_crc, &crc, size);
+ debug("Calculated crc: 0x%08x\n", crc);
+
+ if (!imd_crc) {
+ debug("No tag of type 0x%08x found\n", IMD_TYPE_CHECKSUM);
+
+ return -ENOENT;
+ } else {
+ uint32_t *p = (uint32_t *)(imd_crc + 1);
+
+ if (*p != crc && imd_tag_is_valid(imd_crc->flags)) {
+ eprintf("CRC: image corrupted. Found checksum 0x%08x instead of 0x%08x\n",
+ *p, crc);
+ return -EILSEQ;
+ } else if (*p != crc && !imd_tag_is_valid(imd_crc->flags)) {
+ printf("CRC: is invalid, but the checksum tag is not enabled\n");
+ return -EINVAL;
+ } else {
+ printf("CRC: valid\n");
+ }
+ }
+
+ return 0;
+};
+
int imd_command_verbose;
int imd_command(int argc, char *argv[])
@@ -299,10 +415,12 @@ int imd_command(int argc, char *argv[])
const char *filename;
const char *variable_name = NULL;
char *str;
+ uint32_t checksum = 0;
+ uint32_t verify = 0;
imd_command_verbose = 0;
- while ((opt = getopt(argc, argv, "vt:s:n:")) > 0) {
+ while ((opt = getopt(argc, argv, "vt:s:n:cV")) > 0) {
switch(opt) {
case 't':
type = imd_name_to_type(optarg);
@@ -320,6 +438,12 @@ int imd_command(int argc, char *argv[])
case 'n':
strno = simple_strtoul(optarg, NULL, 0);
break;
+ case 'c':
+ checksum = 1;
+ break;
+ case 'V':
+ verify = 1;
+ break;
default:
return -ENOSYS;
}
@@ -342,6 +466,11 @@ int imd_command(int argc, char *argv[])
goto out;
}
+ if (checksum)
+ imd_write_crc32(buf, imd_start, filename, size);
+ if (verify)
+ imd_verify_crc32(buf, size);
+
if (type == IMD_TYPE_INVALID) {
imd_for_each(imd_start, imd) {
uint32_t type = imd_read_type(imd);
@@ -350,6 +479,10 @@ int imd_command(int argc, char *argv[])
str = imd_concat_strings(imd);
printf("%s: %s\n", imd_type_to_name(type), str);
+ } else if (imd_is_uint32(type)) {
+ uint32_t *p = (uint32_t *)(imd + 1);
+
+ printf("%s: 0x%08x\n", imd_type_to_name(type), *p);
} else {
debug("Unknown tag 0x%08x\n", type);
}
diff --git a/include/image-metadata.h b/include/image-metadata.h
index ca73e6cf6ec6..bf6327f565d4 100644
--- a/include/image-metadata.h
+++ b/include/image-metadata.h
@@ -25,6 +25,7 @@
#define IMD_TYPE_MODEL 0x640c8004 /* The board name this image is for */
#define IMD_TYPE_OF_COMPATIBLE 0x640c8005 /* the device tree compatible string */
#define IMD_TYPE_PARAMETER 0x640c8006 /* A generic parameter. Use key=value as data */
+#define IMD_TYPE_CHECKSUM 0x640c1007 /* the checksum of the barebox images */
#define IMD_TYPE_END 0x640c7fff
#define IMD_TYPE_INVALID 0xffffffff
@@ -54,11 +55,24 @@ static inline int imd_is_string(uint32_t type)
return (type & 0x8000) ? 1 : 0;
}
+static inline int imd_is_uint32(uint32_t type)
+{
+ return (type & 0x1000) ? 1 : 0;
+}
+
static inline int imd_tag_is_valid(uint32_t flags)
{
return (flags & IMD_FLAG_TAG_VALID) ? 1 : 0;
}
+/*
+ * A IMD int.
+ */
+struct imd_entry_uint32 {
+ struct imd_header header;
+ uint32_t data;
+};
+
static inline int imd_type_valid(uint32_t type)
{
return (type & 0xffff0000) == 0x640c0000;
@@ -103,6 +117,7 @@ const char *imd_get_param(const struct imd_header *imd, const char *name);
extern int imd_command_verbose;
int imd_command_setenv(const char *variable_name, const char *value);
int imd_command(int argc, char *argv[]);
+int imd_verify_crc32(void *buf, size_t size);
#ifdef __BAREBOX__
@@ -120,6 +135,13 @@ int imd_command(int argc, char *argv[]);
.data = _string, \
}
+#define BAREBOX_IMD_CRC(_name, _crc, _keep_if_unused) \
+ const struct imd_entry_uint32 __barebox_imd_##__name \
+ __BAREBOX_IMD_SECTION(.barebox_imd_ ## _keep_if_unused ## _ ## _name) = { \
+ .header.type = cpu_to_le32(IMD_TYPE_CHECKSUM), \
+ .header.datalength = cpu_to_le32(sizeof(uint32_t)), \
+ .data = _crc, \
+ }
#ifdef CONFIG_IMD
void imd_used(const void *);
diff --git a/scripts/bareboximd.c b/scripts/bareboximd.c
index 5ef91831c457..b9056cdca7d9 100644
--- a/scripts/bareboximd.c
+++ b/scripts/bareboximd.c
@@ -33,6 +33,7 @@
#include <errno.h>
#include <stdarg.h>
#include <linux/err.h>
+#include <linux/kernel.h>
#include "../include/image-metadata.h"
@@ -57,6 +58,35 @@ int imd_command_setenv(const char *variable_name, const char *value)
return -EINVAL;
}
+static int write_file(const char *filename, const void *buf, size_t size)
+{
+ int fd, ret;
+ int now;
+
+ fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT);
+ if (fd < 0)
+ return fd;
+
+ while (size) {
+ now = write(fd, buf, size);
+ if (now == 0) {
+ errno = ENOSPC;
+ return -1;
+ }
+ if (now < 0)
+ return now;
+ size -= now;
+ buf += now;
+ }
+
+ close(fd);
+
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
static int read_file_2(const char *filename, size_t *size, void **outbuf, size_t max_size)
{
off_t fsize;
@@ -129,6 +159,8 @@ static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int ba
return strtoul(cp, endp, base);
}
+#include "../include/xfuncs.h"
+#include "../crypto/crc32.c"
#include "../common/imd.c"
static void usage(const char *prgname)
--
2.24.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 4/4] commands: bbu: add support for imd checksum
2019-12-09 10:42 [PATCH v2 1/4] imd: replace magicvalue with sizeof(struct) Steffen Trumtrar
2019-12-09 10:42 ` [PATCH v2 2/4] image-metadata: add flags field to imd_header Steffen Trumtrar
2019-12-09 10:42 ` [PATCH v2 3/4] imd: add support for checksum generation/verification Steffen Trumtrar
@ 2019-12-09 10:42 ` Steffen Trumtrar
2 siblings, 0 replies; 5+ messages in thread
From: Steffen Trumtrar @ 2019-12-09 10:42 UTC (permalink / raw)
To: Barebox List
Add support for verifying an image by use of the imd checksum.
If the checksum that is saved in the image and the one that is
calculated over the image differ, barebox_update aborts.
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
common/bbu.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/common/bbu.c b/common/bbu.c
index 00bec32a860a..b976b99d7c95 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -206,6 +206,10 @@ static int bbu_check_metadata(struct bbu_data *data)
if (ret)
return ret;
+ ret = imd_verify_crc32((void *)data->image, data->len);
+ if (ret == -EILSEQ && !(data->flags & BBU_FLAG_FORCE))
+ return ret;
+
return 0;
}
--
2.24.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/4] image-metadata: add flags field to imd_header
2019-12-09 10:42 ` [PATCH v2 2/4] image-metadata: add flags field to imd_header Steffen Trumtrar
@ 2019-12-09 11:39 ` Sascha Hauer
0 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2019-12-09 11:39 UTC (permalink / raw)
To: Steffen Trumtrar; +Cc: Barebox List
On Mon, Dec 09, 2019 at 11:42:36AM +0100, Steffen Trumtrar wrote:
> To allow marking an imd tag with properties, add a flags field to the imd_header.
>
> Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
> ---
> include/image-metadata.h | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
> diff --git a/include/image-metadata.h b/include/image-metadata.h
> index 5904d95acd37..ca73e6cf6ec6 100644
> --- a/include/image-metadata.h
> +++ b/include/image-metadata.h
> @@ -28,6 +28,8 @@
> #define IMD_TYPE_END 0x640c7fff
> #define IMD_TYPE_INVALID 0xffffffff
>
> +#define IMD_FLAG_TAG_VALID (1 << 0)
> +
> /*
> * The IMD header. All data is stored in little endian format in the image.
> * The next header starts at the next 4 byte boundary after the data.
> @@ -35,6 +37,7 @@
> struct imd_header {
> uint32_t type; /* One of IMD_TYPE_* above */
> uint32_t datalength; /* Length of the data (exluding the header) */
> + uint32_t flags;
> };
Erm, no. struct imd_header shouldn't be changed, this breaks the imd
tool. What I meant is that the valid flag should be added to the crc
tag.
Sascha
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2019-12-09 11:39 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-09 10:42 [PATCH v2 1/4] imd: replace magicvalue with sizeof(struct) Steffen Trumtrar
2019-12-09 10:42 ` [PATCH v2 2/4] image-metadata: add flags field to imd_header Steffen Trumtrar
2019-12-09 11:39 ` Sascha Hauer
2019-12-09 10:42 ` [PATCH v2 3/4] imd: add support for checksum generation/verification Steffen Trumtrar
2019-12-09 10:42 ` [PATCH v2 4/4] commands: bbu: add support for imd checksum Steffen Trumtrar
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox