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 1/2] bbu: Add a standard file-to-device-copy handler
Date: Wed,  1 Jul 2015 09:11:57 +0200	[thread overview]
Message-ID: <1435734718-18367-1-git-send-email-s.hauer@pengutronix.de> (raw)

The most standard update handler will simply copy a file to a device.
This can be shared across several users, so add a standard handler for
this operation.

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

diff --git a/common/bbu.c b/common/bbu.c
index 7fb154a..4d71fa4 100644
--- a/common/bbu.c
+++ b/common/bbu.c
@@ -20,6 +20,12 @@
 #include <linux/list.h>
 #include <errno.h>
 #include <readkey.h>
+#include <filetype.h>
+#include <libfile.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <malloc.h>
+#include <linux/stat.h>
 
 static LIST_HEAD(bbu_image_handlers);
 
@@ -153,3 +159,110 @@ int bbu_register_handler(struct bbu_handler *handler)
 
 	return 0;
 }
+
+struct bbu_std {
+	struct bbu_handler handler;
+	enum filetype filetype;
+};
+
+static int bbu_std_file_handler(struct bbu_handler *handler,
+					struct bbu_data *data)
+{
+	struct bbu_std *std = container_of(handler, struct bbu_std, handler);
+	int fd, ret;
+	enum filetype filetype;
+	struct stat s;
+	unsigned oflags = O_WRONLY;
+
+	filetype = file_detect_type(data->image, data->len);
+	if (filetype != std->filetype) {
+		if (!bbu_force(data, "incorrect image type. Expected: %s, got %s",
+				file_type_to_string(std->filetype),
+				file_type_to_string(filetype)))
+			return -EINVAL;
+	}
+
+	ret = stat(data->devicefile, &s);
+	if (ret) {
+		oflags |= O_CREAT;
+	} else {
+		if (!S_ISREG(s.st_mode) && s.st_size < data->len) {
+			printf("Image (%lld) is too big for device (%d)\n",
+					s.st_size, data->len);
+		}
+	}
+
+	ret = bbu_confirm(data);
+	if (ret)
+		return ret;
+
+	fd = open(data->devicefile, oflags);
+	if (fd < 0)
+		return fd;
+
+	ret = protect(fd, data->len, 0, 0);
+	if (ret && ret != -ENOSYS) {
+		printf("unprotecting %s failed with %s\n", data->devicefile,
+				strerror(-ret));
+		goto err_close;
+	}
+
+	ret = erase(fd, data->len, 0);
+	if (ret && ret != -ENOSYS) {
+		printf("erasing %s failed with %s\n", data->devicefile,
+				strerror(-ret));
+		goto err_close;
+	}
+
+	ret = write_full(fd, data->image, data->len);
+	if (ret < 0)
+		goto err_close;
+
+	protect(fd, data->len, 0, 1);
+
+	ret = 0;
+
+err_close:
+	close(fd);
+
+	return ret;
+}
+
+/**
+ * bbu_register_std_file_update() - register a barebox update handler for a
+ *                                  standard file-to-device-copy operation
+ * @name:	Name of the handler
+ * @flags:	BBU_HANDLER_FLAG_* flags
+ * @devicefile: the file to write the update image to
+ * @imagetype:	The filetype that the update image must have
+ *
+ * This update handler us suitable for a standard file-to-device copy operation.
+ * The handler checks for a filetype and unprotects/erases the device if
+ * necessary. If devicefile belongs to a device then the device is checkd for
+ * enough space before touching it.
+ *
+ * Return: 0 if successful, negative error code otherwise
+ */
+int bbu_register_std_file_update(const char *name, unsigned long flags,
+		char *devicefile, enum filetype imagetype)
+{
+	struct bbu_std *std;
+	struct bbu_handler *handler;
+	int ret;
+
+	std = xzalloc(sizeof(*std));
+	std->filetype = imagetype;
+
+	handler = &std->handler;
+
+	handler->flags = flags;
+	handler->devicefile = devicefile;
+	handler->name = name;
+	handler->handler = bbu_std_file_handler;
+
+	ret = bbu_register_handler(handler);
+	if (ret)
+		free(std);
+
+	return ret;
+}
diff --git a/include/bbu.h b/include/bbu.h
index 4a3d35e..af2f84a 100644
--- a/include/bbu.h
+++ b/include/bbu.h
@@ -4,6 +4,7 @@
 #include <asm-generic/errno.h>
 #include <linux/list.h>
 #include <linux/types.h>
+#include <filetype.h>
 
 struct bbu_data {
 #define BBU_FLAG_FORCE	(1 << 0)
@@ -41,6 +42,9 @@ void bbu_handlers_list(void);
 
 int bbu_register_handler(struct bbu_handler *);
 
+int bbu_register_std_file_update(const char *name, unsigned long flags,
+		char *devicefile, enum filetype imagetype);
+
 #else
 
 static inline int bbu_register_handler(struct bbu_handler *unused)
@@ -48,6 +52,11 @@ static inline int bbu_register_handler(struct bbu_handler *unused)
 	return -EINVAL;
 }
 
+static inline int bbu_register_std_file_update(const char *name, unsigned long flags,
+		char *devicefile, enum filetype imagetype)
+{
+	return -ENOSYS;
+}
 #endif
 
 #endif /* __INCLUDE_BBU_H */
-- 
2.1.4


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

             reply	other threads:[~2015-07-01  7:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-01  7:11 Sascha Hauer [this message]
2015-07-01  7:11 ` [PATCH 2/2] ARM: AM335x: replace specific barebox update handler with generic one 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=1435734718-18367-1-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