mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 7/7] bbu: print and evaluate image Metadata
Date: Tue, 29 Mar 2016 10:50:54 +0200	[thread overview]
Message-ID: <1459241454-21155-7-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1459241454-21155-1-git-send-email-s.hauer@pengutronix.de>

With imd we can store metadata in barebox images. Let's use this
information to further verify that the image that is to be flashed
is the correct one. This patch extracts the device tree compatible
from the image and compares it with the one from the currently
running barebox. If it doesn't match the update is aborted with a
warning.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/bbu.c  | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/bbu.h |  1 +
 2 files changed, 74 insertions(+)

diff --git a/common/bbu.c b/common/bbu.c
index 68812a7..b49fbe6 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -26,6 +26,7 @@
 #include <fcntl.h>
 #include <malloc.h>
 #include <linux/stat.h>
+#include <image-metadata.h>
 
 static LIST_HEAD(bbu_image_handlers);
 
@@ -125,6 +126,76 @@ bool barebox_update_handler_exists(struct bbu_data *data)
 	return !bbu_find_handler(data->handler_name);
 }
 
+static int bbu_check_of_compat(struct bbu_data *data)
+{
+	struct device_node *root_node;
+	const char *machine, *str;
+	int ret;
+	struct imd_header *of_compat;
+
+	if (!IS_ENABLED(CONFIG_OFDEVICE) || !IS_ENABLED(CONFIG_IMD))
+		return 0;
+
+	of_compat = imd_find_type(data->imd_data, IMD_TYPE_OF_COMPATIBLE);
+	if (!of_compat)
+		return 0;
+
+	root_node = of_get_root_node();
+	if (!root_node)
+		return 0;
+
+	str = imd_string_data(of_compat, 0);
+
+	if (of_machine_is_compatible(str)) {
+		pr_info("Devicetree compatible \"%s\" matches current machine\n", str);
+		return 0;
+	}
+
+	ret = of_property_read_string(root_node, "compatible", &machine);
+	if (ret)
+		return 0;
+
+	if (!bbu_force(data, "machine is incompatible with \"%s\", have \"%s\"\n", str, machine))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int bbu_check_metadata(struct bbu_data *data)
+{
+	struct imd_header *imd;
+	int ret;
+	char *str;
+
+	if (!data->image)
+		return 0;
+
+	data->imd_data = imd_get(data->image, data->len);
+	if (IS_ERR(data->imd_data)) {
+		data->imd_data = NULL;
+		return 0;
+	}
+
+	printf("Image Metadata:\n");
+	imd_for_each(data->imd_data, imd) {
+		uint32_t type = imd_read_type(imd);
+
+		if (!imd_is_string(type))
+			continue;
+
+		str = imd_concat_strings(imd);
+
+		printf("  %s: %s\n", imd_type_to_name(type), str);
+		free(str);
+	}
+
+	ret = bbu_check_of_compat(data);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
 /*
  * do a barebox update with data from *data
  */
@@ -147,6 +218,8 @@ int barebox_update(struct bbu_data *data)
 	if (!data->devicefile)
 		data->devicefile = handler->devicefile;
 
+	bbu_check_metadata(data);
+
 	ret = handler->handler(handler, data);
 	if (ret == -EINTR)
 		printf("update aborted\n");
diff --git a/include/bbu.h b/include/bbu.h
index 0fe7a1a..701d7f6 100644
--- a/include/bbu.h
+++ b/include/bbu.h
@@ -16,6 +16,7 @@ struct bbu_data {
 	const char *devicefile;
 	size_t len;
 	const char *handler_name;
+	struct imd_header *imd_data;
 };
 
 struct bbu_handler {
-- 
2.7.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2016-03-29  8:51 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-29  8:50 [PATCH 1/7] imd: use struct imd_header * as argument Sascha Hauer
2016-03-29  8:50 ` [PATCH 2/7] imd: string returned from imd_string_data should be const Sascha Hauer
2016-03-29  9:34   ` Stefan Christ
2016-04-01  8:43     ` Sascha Hauer
2016-03-29  8:50 ` [PATCH 3/7] scripts: Add scripts/include/ to include path for target programs Sascha Hauer
2016-03-29  8:50 ` [PATCH 4/7] imd: rename imd_search_validate to imd_get Sascha Hauer
2016-03-29  8:50 ` [PATCH 5/7] imd: export functions Sascha Hauer
2016-03-29  8:50 ` [PATCH 6/7] imd: Add function to read parameters Sascha Hauer
2016-03-29  8:50 ` Sascha Hauer [this message]
2016-03-29  9:29   ` [PATCH 7/7] bbu: print and evaluate image Metadata Stefan Christ
2016-04-01  8:41     ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1459241454-21155-7-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox