mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep()
@ 2020-09-14 13:37 Ahmad Fatoum
  2020-09-14 13:37 ` [PATCH 2/5] ls: don't print . and .. on recursive ls Ahmad Fatoum
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2020-09-14 13:37 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

With the recent changes to ARCH=sandbox, there are no remaining
in-tree users for strtok() anymore. Out-of-tree users are better
served by using the reentrant strsep(), which has existed in-tree
for as long.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Should be applied after <20200914100553.24808-1-a.fatoum@pengutronix.de>
---
 include/linux/string.h |  2 --
 lib/string.c           | 35 +++--------------------------------
 2 files changed, 3 insertions(+), 34 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index 763ef500e574..2b699957e824 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -8,9 +8,7 @@
 extern "C" {
 #endif
 
-extern char * ___strtok;
 extern char * strpbrk(const char *,const char *);
-extern char * strtok(char *,const char *);
 extern char * strsep(char **,const char *);
 extern char * strsep_unescaped(char **,const char *);
 extern __kernel_size_t strspn(const char *,const char *);
diff --git a/lib/string.c b/lib/string.c
index 50f8e2f87c9f..733b56730070 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -13,6 +13,9 @@
  * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
  * -  Added strsep() which will replace strtok() soon (because strsep() is
  *    reentrant and should be faster). Use only strsep() in new code, please.
+ * * Mon Sep 14 2020, Ahmad Fatoum <a.fatoum@pengutronix.de>
+ * -  Kissed strtok() goodbye
+ *
  */
 
 #include <linux/types.h>
@@ -20,8 +23,6 @@
 #include <linux/ctype.h>
 #include <malloc.h>
 
-char * ___strtok;
-
 #ifndef __HAVE_ARCH_STRNICMP
 /**
  * strnicmp - Case insensitive, length-limited string comparison
@@ -396,36 +397,6 @@ char * strpbrk(const char * cs,const char * ct)
 #endif
 EXPORT_SYMBOL(strpbrk);
 
-#ifndef __HAVE_ARCH_STRTOK
-/**
- * strtok - Split a string into tokens
- * @s: The string to be searched
- * @ct: The characters to search for
- *
- * WARNING: strtok is deprecated, use strsep instead.
- */
-char * strtok(char * s, const char * ct)
-{
-	char *sbegin, *send;
-
-	sbegin  = s ? s : ___strtok;
-	if (!sbegin) {
-		return NULL;
-	}
-	sbegin += strspn(sbegin,ct);
-	if (*sbegin == '\0') {
-		___strtok = NULL;
-		return( NULL );
-	}
-	send = strpbrk( sbegin, ct);
-	if (send && *send != '\0')
-		*send++ = '\0';
-	___strtok = send;
-	return (sbegin);
-}
-#endif
-EXPORT_SYMBOL(strtok);
-
 #ifndef __HAVE_ARCH_STRSEP
 /**
  * strsep - Split a string into tokens
-- 
2.28.0


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/5] ls: don't print . and .. on recursive ls
  2020-09-14 13:37 [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep() Ahmad Fatoum
@ 2020-09-14 13:37 ` Ahmad Fatoum
  2020-09-14 13:37 ` [PATCH 3/5] sandbox: implement simple, ^C-interruptible, restart handler Ahmad Fatoum
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2020-09-14 13:37 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

They are already omitted on normal ls and they don't really add a new
information, so drop them for the recursive case as well.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/ls.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/commands/ls.c b/commands/ls.c
index 6a5475d094e5..59163f678d51 100644
--- a/commands/ls.c
+++ b/commands/ls.c
@@ -75,8 +75,13 @@ int ls(const char *path, ulong flags)
 	if (!dir)
 		return -errno;
 
-	while ((d = readdir(dir)))
+	while ((d = readdir(dir))) {
+		if (!strcmp(d->d_name, "."))
+			continue;
+		if (!strcmp(d->d_name, ".."))
+			continue;
 		string_list_add_sorted(&sl, d->d_name);
+	}
 
 	closedir(dir);
 
@@ -99,10 +104,6 @@ int ls(const char *path, ulong flags)
 		goto out;
 
 	string_list_for_each_entry(entry, &sl) {
-		if (!strcmp(entry->str, "."))
-			continue;
-		if (!strcmp(entry->str, ".."))
-			continue;
 		sprintf(tmp, "%s/%s", path, entry->str);
 
 		ret = lstat(tmp, &s);
-- 
2.28.0


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/5] sandbox: implement simple, ^C-interruptible, restart handler
  2020-09-14 13:37 [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep() Ahmad Fatoum
  2020-09-14 13:37 ` [PATCH 2/5] ls: don't print . and .. on recursive ls Ahmad Fatoum
@ 2020-09-14 13:37 ` Ahmad Fatoum
  2020-09-14 13:37 ` [PATCH 4/5] sandbox: implement actual sandbox reset via exec(2) Ahmad Fatoum
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2020-09-14 13:37 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Typing reset in sandbox results in hang() while the terminal is not
cooked and ^C is ineffective. Only way to terminate barebox then is
via kill. Reinstate cooked mode on reset, so ^C termination is
possible.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/sandbox/board/poweroff.c                  | 12 ++++++++++++
 arch/sandbox/mach-sandbox/include/mach/linux.h |  1 +
 arch/sandbox/os/common.c                       |  6 ++++++
 3 files changed, 19 insertions(+)

diff --git a/arch/sandbox/board/poweroff.c b/arch/sandbox/board/poweroff.c
index 6b5a6dff15f9..5072b756e108 100644
--- a/arch/sandbox/board/poweroff.c
+++ b/arch/sandbox/board/poweroff.c
@@ -1,6 +1,7 @@
 #include <common.h>
 #include <init.h>
 #include <poweroff.h>
+#include <restart.h>
 #include <mach/linux.h>
 
 static void sandbox_poweroff(struct poweroff_handler *poweroff)
@@ -8,9 +9,20 @@ static void sandbox_poweroff(struct poweroff_handler *poweroff)
 	linux_exit();
 }
 
+static void sandbox_rst_hang(struct restart_handler *rst)
+{
+	linux_hang();
+}
+
+static struct restart_handler rst_hang = {
+	.name = "hang",
+	.restart = sandbox_rst_hang
+};
+
 static int poweroff_register_feature(void)
 {
 	poweroff_handler_register_fn(sandbox_poweroff);
+	restart_handler_register(&rst_hang);
 
 	return 0;
 }
diff --git a/arch/sandbox/mach-sandbox/include/mach/linux.h b/arch/sandbox/mach-sandbox/include/mach/linux.h
index 9759a376ecb3..f0a3a7b510fc 100644
--- a/arch/sandbox/mach-sandbox/include/mach/linux.h
+++ b/arch/sandbox/mach-sandbox/include/mach/linux.h
@@ -17,6 +17,7 @@ ssize_t linux_write(int fd, const void *buf, size_t count);
 off_t linux_lseek(int fildes, off_t offset);
 int linux_tstc(int fd);
 void __attribute__((noreturn)) linux_exit(void);
+void linux_hang(void);
 
 int linux_execve(const char * filename, char *const argv[], char *const envp[]);
 
diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 437fe3ecdff8..bbab3bd2312d 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -122,6 +122,12 @@ void __attribute__((noreturn)) linux_exit(void)
 	exit(0);
 }
 
+void linux_hang(void)
+{
+	cookmode();
+	/* falls through to generic hang() */
+}
+
 int linux_open(const char *filename, int readwrite)
 {
 	return open(filename, readwrite ? O_RDWR : O_RDONLY);
-- 
2.28.0


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 4/5] sandbox: implement actual sandbox reset via exec(2)
  2020-09-14 13:37 [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep() Ahmad Fatoum
  2020-09-14 13:37 ` [PATCH 2/5] ls: don't print . and .. on recursive ls Ahmad Fatoum
  2020-09-14 13:37 ` [PATCH 3/5] sandbox: implement simple, ^C-interruptible, restart handler Ahmad Fatoum
@ 2020-09-14 13:37 ` Ahmad Fatoum
  2020-09-15  5:21   ` [PATCH] fixup! " Ahmad Fatoum
  2020-09-14 13:37 ` [PATCH 5/5] sandbox: hostfile: support registering images as barebox block devices Ahmad Fatoum
  2020-09-15  7:26 ` [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep() Sascha Hauer
  4 siblings, 1 reply; 7+ messages in thread
From: Ahmad Fatoum @ 2020-09-14 13:37 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum, Ahmad Fatoum

From: Ahmad Fatoum <ahmad@a3f.at>

On Linux, /proc/self/exe is a symlink to the originally exec(2)d
executable. We can exec that with the original argv to simulate
a reset. This is useful for shorter development cycles on sandbox
and in future, could be used to test barebox behavior around resets
(e.g. reset reason can be passed through via libc environment).

We leave the original hanging reset in place though, because:

 - Many boards have multiple reset providers and incoming patches
    will allow users to select a specific one. Having this on
    sandbox as well makes testing easier.

 - /proc/self/exe is Linux-specific and wouldn't work when being
   run on e.g. BSDs or macOS

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/sandbox/Kconfig                          |  7 +++
 arch/sandbox/Makefile                         |  2 +-
 arch/sandbox/board/poweroff.c                 | 13 ++++++
 .../sandbox/mach-sandbox/include/mach/linux.h |  1 +
 arch/sandbox/os/common.c                      | 43 ++++++++++++++++---
 arch/sandbox/os/tap.c                         |  2 +-
 6 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
index c4d0ab4dbcde..b5213c662b1b 100644
--- a/arch/sandbox/Kconfig
+++ b/arch/sandbox/Kconfig
@@ -24,6 +24,13 @@ config SANDBOX_UNWIND
 	select ARCH_HAS_STACK_DUMP
 	depends on KASAN
 
+config SANDBOX_REEXEC
+	prompt "exec(2) reset handler"
+	def_bool y
+	help
+	  The normal reset handler hangs barebox. On Linux, barebox
+	  instead can exec itself to simulate a reset.
+
 config PHYS_ADDR_T_64BIT
 	bool
 
diff --git a/arch/sandbox/Makefile b/arch/sandbox/Makefile
index 27021222dc48..eb678b72dbcd 100644
--- a/arch/sandbox/Makefile
+++ b/arch/sandbox/Makefile
@@ -25,7 +25,7 @@ KBUILD_CFLAGS += -Dmalloc=barebox_malloc -Dcalloc=barebox_calloc \
 		-Dglob=barebox_glob -Dglobfree=barebox_globfree \
 		-Dioctl=barebox_ioctl -Dfstat=barebox_fstat \
 		-Dopendir=barebox_opendir -Dreaddir=barebox_readdir \
-		-Dclosedir=barebox_closedir \
+		-Dclosedir=barebox_closedir -Dreadlink=barebox_readlink \
 		-Doptarg=barebox_optarg -Doptind=barebox_optind
 
 machdirs := $(patsubst %,arch/sandbox/mach-%/,$(machine-y))
diff --git a/arch/sandbox/board/poweroff.c b/arch/sandbox/board/poweroff.c
index 5072b756e108..8ce739af72c1 100644
--- a/arch/sandbox/board/poweroff.c
+++ b/arch/sandbox/board/poweroff.c
@@ -19,11 +19,24 @@ static struct restart_handler rst_hang = {
 	.restart = sandbox_rst_hang
 };
 
+static void sandbox_rst_reexec(struct restart_handler *rst)
+{
+	linux_reexec();
+}
+
+static struct restart_handler rst_reexec = {
+	.name = "reexec", .priority = 200,
+	.restart = sandbox_rst_reexec,
+};
+
 static int poweroff_register_feature(void)
 {
 	poweroff_handler_register_fn(sandbox_poweroff);
 	restart_handler_register(&rst_hang);
 
+	if (IS_ENABLED(CONFIG_SANDBOX_REEXEC))
+		restart_handler_register(&rst_reexec);
+
 	return 0;
 }
 coredevice_initcall(poweroff_register_feature);
diff --git a/arch/sandbox/mach-sandbox/include/mach/linux.h b/arch/sandbox/mach-sandbox/include/mach/linux.h
index f0a3a7b510fc..1ab48e52a00a 100644
--- a/arch/sandbox/mach-sandbox/include/mach/linux.h
+++ b/arch/sandbox/mach-sandbox/include/mach/linux.h
@@ -18,6 +18,7 @@ off_t linux_lseek(int fildes, off_t offset);
 int linux_tstc(int fd);
 void __attribute__((noreturn)) linux_exit(void);
 void linux_hang(void);
+void linux_reexec(void);
 
 int linux_execve(const char * filename, char *const argv[], char *const envp[]);
 
diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index bbab3bd2312d..63839a1ccc68 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -44,6 +44,8 @@
 #include <mach/linux.h>
 #include <mach/hostfile.h>
 
+#define DELETED_OFFSET (sizeof(" (deleted)") - 1)
+
 void __sanitizer_set_death_callback(void (*callback)(void));
 
 int sdl_xres;
@@ -122,6 +124,31 @@ void __attribute__((noreturn)) linux_exit(void)
 	exit(0);
 }
 
+static char **saved_argv;
+
+void linux_reexec(void)
+{
+	char buf[4097];
+	ssize_t ret;
+
+	cookmode();
+
+	/* we must follow the symlink, so we can exec an updated executable */
+	ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
+	if (0 < ret && ret < sizeof(buf) - 1) {
+		buf[ret] = '\0';
+		execv(buf, saved_argv);
+		if (!strcmp(&buf[ret - DELETED_OFFSET], " (deleted)")) {
+			printf("barebox image on disk changed. Loading new.\n");
+			buf[ret - DELETED_OFFSET] = '\0';
+			execv(buf, saved_argv);
+		}
+	}
+
+	printf("exec(%s) failed: %d\n", buf, errno);
+	/* falls through to generic hang() */
+}
+
 void linux_hang(void)
 {
 	cookmode();
@@ -130,7 +157,7 @@ void linux_hang(void)
 
 int linux_open(const char *filename, int readwrite)
 {
-	return open(filename, readwrite ? O_RDWR : O_RDONLY);
+	return open(filename, (readwrite ? O_RDWR : O_RDONLY) | O_CLOEXEC);
 }
 
 int linux_read(int fd, void *buf, size_t count)
@@ -252,7 +279,7 @@ static int add_image(char *str, char *devname_template, int *devname_number)
 	printf("add %s backed by file %s%s\n", devname,
 	       filename, readonly ? "(ro)" : "");
 
-	fd = open(filename, readonly ? O_RDONLY : O_RDWR);
+	fd = open(filename, (readonly ? O_RDONLY : O_RDWR) | O_CLOEXEC);
 	hf->fd = fd;
 	hf->filename = filename;
 
@@ -303,7 +330,7 @@ static int add_dtb(const char *file)
 	void *dtb = NULL;
 	int fd;
 
-	fd = open(file, O_RDONLY);
+	fd = open(file, O_RDONLY | O_CLOEXEC);
 	if (fd < 0) {
 		perror("open");
 		goto err_out;
@@ -363,6 +390,8 @@ int main(int argc, char *argv[])
 	__sanitizer_set_death_callback(cookmode);
 #endif
 
+	saved_argv = argv;
+
 	while (1) {
 		option_index = 0;
 		opt = getopt_long(argc, argv, optstring,
@@ -434,7 +463,7 @@ int main(int argc, char *argv[])
 				exit(1);
 			break;
 		case 'O':
-			fd = open(optarg, O_WRONLY);
+			fd = open(optarg, O_WRONLY | O_CLOEXEC);
 			if (fd < 0) {
 				perror("open");
 				exit(1);
@@ -443,7 +472,7 @@ int main(int argc, char *argv[])
 			barebox_register_console(-1, fd);
 			break;
 		case 'I':
-			fd = open(optarg, O_RDWR);
+			fd = open(optarg, O_RDWR | O_CLOEXEC);
 			if (fd < 0) {
 				perror("open");
 				exit(1);
@@ -459,7 +488,7 @@ int main(int argc, char *argv[])
 			}
 
 			/* open stdout file */
-			fd = open(aux + 1, O_WRONLY);
+			fd = open(aux + 1, O_WRONLY | O_CLOEXEC);
 			if (fd < 0) {
 				perror("open stdout");
 				exit(1);
@@ -467,7 +496,7 @@ int main(int argc, char *argv[])
 
 			/* open stdin file */
 			aux = strndup(optarg, aux - optarg);
-			fd2 = open(aux, O_RDWR);
+			fd2 = open(aux, O_RDWR | O_CLOEXEC);
 			if (fd2 < 0) {
 				perror("open stdin");
 				exit(1);
diff --git a/arch/sandbox/os/tap.c b/arch/sandbox/os/tap.c
index 72b7fbb5ac4b..83b97ffd49c7 100644
--- a/arch/sandbox/os/tap.c
+++ b/arch/sandbox/os/tap.c
@@ -30,7 +30,7 @@ int tap_alloc(const char *dev)
 	struct ifreq ifr;
 	int fd, err;
 
-	if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
+	if ((fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC)) < 0) {
 		perror("could not open /dev/net/tun");
 		return -1;
 	}
-- 
2.28.0


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 5/5] sandbox: hostfile: support registering images as barebox block devices
  2020-09-14 13:37 [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep() Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2020-09-14 13:37 ` [PATCH 4/5] sandbox: implement actual sandbox reset via exec(2) Ahmad Fatoum
@ 2020-09-14 13:37 ` Ahmad Fatoum
  2020-09-15  7:26 ` [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep() Sascha Hauer
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2020-09-14 13:37 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

While we can mount file systems on cdevs in barebox, partition table
parsing only works for block devices. Allow for
--image=argument,blkdev to try to mount an image as block device.

This will fail for files that aren't of a multiple of the 512 byte
block size. Host OS block devices are suitable for use as barebox
block devices always, so that's the default unless overridden with
a ,cdev suffix.

The initcall level has been changed to occur after fs initcall level.
This is required, because we can't have automounts without / mounted.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/sandbox/Kconfig                          |   2 +
 arch/sandbox/board/hostfile.c                 | 116 ++++++++++++++----
 .../mach-sandbox/include/mach/hostfile.h      |   1 +
 arch/sandbox/os/common.c                      |  15 ++-
 4 files changed, 108 insertions(+), 26 deletions(-)

diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
index b5213c662b1b..0c1c7bd73a01 100644
--- a/arch/sandbox/Kconfig
+++ b/arch/sandbox/Kconfig
@@ -7,6 +7,8 @@ config SANDBOX
 	select ARCH_HAS_UBSAN_SANITIZE_ALL
 	select HAVE_ARCH_KASAN
 	select HAS_DMA
+	select BLOCK
+	select BLOCK_WRITE
 	default y
 
 config ARCH_TEXT_BASE
diff --git a/arch/sandbox/board/hostfile.c b/arch/sandbox/board/hostfile.c
index 07287fc0b4a1..63530bd25e1b 100644
--- a/arch/sandbox/board/hostfile.c
+++ b/arch/sandbox/board/hostfile.c
@@ -16,6 +16,8 @@
 
 #include <common.h>
 #include <driver.h>
+#include <block.h>
+#include <disks.h>
 #include <malloc.h>
 #include <mach/linux.h>
 #include <init.h>
@@ -27,14 +29,16 @@
 #include <linux/err.h>
 
 struct hf_priv {
-	struct cdev cdev;
+	union {
+		struct block_device blk;
+		struct cdev cdev;
+	};
 	const char *filename;
 	int fd;
 };
 
-static ssize_t hf_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulong flags)
+static ssize_t hf_read(struct hf_priv *priv, void *buf, size_t count, loff_t offset, ulong flags)
 {
-	struct hf_priv *priv= cdev->priv;
 	int fd = priv->fd;
 
 	if (linux_lseek(fd, offset) != offset)
@@ -43,9 +47,8 @@ static ssize_t hf_read(struct cdev *cdev, void *buf, size_t count, loff_t offset
 	return linux_read(fd, buf, count);
 }
 
-static ssize_t hf_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset, ulong flags)
+static ssize_t hf_write(struct hf_priv *priv, const void *buf, size_t count, loff_t offset, ulong flags)
 {
-	struct hf_priv *priv = cdev->priv;
 	int fd = priv->fd;
 
 	if (linux_lseek(fd, offset) != offset)
@@ -54,6 +57,40 @@ static ssize_t hf_write(struct cdev *cdev, const void *buf, size_t count, loff_t
 	return linux_write(fd, buf, count);
 }
 
+static ssize_t hf_cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulong flags)
+{
+	return hf_read(cdev->priv, buf, count, offset, flags);
+}
+
+static ssize_t hf_cdev_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset, ulong flags)
+{
+	return hf_write(cdev->priv, buf, count, offset, flags);
+}
+
+static struct cdev_operations hf_cdev_ops = {
+	.read  = hf_cdev_read,
+	.write = hf_cdev_write,
+};
+
+static int hf_blk_read(struct block_device *blk, void *buf, int block, int num_blocks)
+{
+	ssize_t ret = hf_read(container_of(blk, struct hf_priv, blk), buf,
+			      num_blocks << SECTOR_SHIFT, block << SECTOR_SHIFT, 0);
+	return ret > 0 ? 0 : ret;
+}
+
+static int hf_blk_write(struct block_device *blk, const void *buf, int block, int num_blocks)
+{
+	ssize_t ret = hf_write(container_of(blk, struct hf_priv, blk), buf,
+			       num_blocks << SECTOR_SHIFT, block << SECTOR_SHIFT, 0);
+	return ret > 0 ? 0 : ret;
+}
+
+static struct block_device_ops hf_blk_ops = {
+	.read  = hf_blk_read,
+	.write = hf_blk_write,
+};
+
 static void hf_info(struct device_d *dev)
 {
 	struct hf_priv *priv = dev->priv;
@@ -61,29 +98,28 @@ static void hf_info(struct device_d *dev)
 	printf("file: %s\n", priv->filename);
 }
 
-static struct cdev_operations hf_fops = {
-	.read  = hf_read,
-	.write = hf_write,
-};
-
 static int hf_probe(struct device_d *dev)
 {
+	struct device_node *np = dev->device_node;
 	struct hf_priv *priv = xzalloc(sizeof(*priv));
 	struct resource *res;
+	struct cdev *cdev;
+	bool is_blockdev;
+	resource_size_t size;
 	int err;
 
 	res = dev_get_resource(dev, IORESOURCE_MEM, 0);
 	if (IS_ERR(res))
 		return PTR_ERR(res);
 
-	priv->cdev.size = resource_size(res);
+	size = resource_size(res);
 
-	if (!dev->device_node)
+	if (!np)
 		return -ENODEV;
 
-	of_property_read_u32(dev->device_node, "barebox,fd", &priv->fd);
+	of_property_read_u32(np, "barebox,fd", &priv->fd);
 
-	err = of_property_read_string(dev->device_node, "barebox,filename",
+	err = of_property_read_string(np, "barebox,filename",
 				      &priv->filename);
 	if (err)
 		return err;
@@ -94,20 +130,47 @@ static int hf_probe(struct device_d *dev)
 	if (priv->fd < 0)
 		return priv->fd;
 
-	priv->cdev.name = dev->device_node->name;
-	priv->cdev.dev = dev;
-	priv->cdev.ops = &hf_fops;
-	priv->cdev.priv = priv;
-
 	dev->info = hf_info;
 	dev->priv = priv;
 
-	err = devfs_create(&priv->cdev);
-	if (err)
-		return err;
+	is_blockdev = of_property_read_bool(np, "barebox,blockdev");
+
+	cdev = is_blockdev ? &priv->blk.cdev : &priv->cdev;
+
+	cdev->device_node = np;
 
-	of_parse_partitions(&priv->cdev, dev->device_node);
-	of_partitions_register_fixup(&priv->cdev);
+	if (is_blockdev) {
+		cdev->name = np->name;
+		priv->blk.dev = dev;
+		priv->blk.ops = &hf_blk_ops;
+		priv->blk.blockbits = SECTOR_SHIFT;
+		priv->blk.num_blocks = size / SECTOR_SIZE;
+
+		err = blockdevice_register(&priv->blk);
+		if (err)
+			return err;
+
+		err = parse_partition_table(&priv->blk);
+		if (err)
+			dev_warn(dev, "No partition table found\n");
+
+		dev_info(dev, "registered as block device\n");
+	} else {
+		cdev->name = np->name;
+		cdev->dev = dev;
+		cdev->ops = &hf_cdev_ops;
+		cdev->size = size;
+		cdev->priv = priv;
+
+		err = devfs_create(cdev);
+		if (err)
+			return err;
+
+		dev_info(dev, "registered as character device\n");
+	}
+
+	of_parse_partitions(cdev, np);
+	of_partitions_register_fixup(cdev);
 
 	return 0;
 }
@@ -125,7 +188,7 @@ static struct driver_d hf_drv = {
 	.of_compatible = DRV_OF_COMPAT(hostfile_dt_ids),
 	.probe = hf_probe,
 };
-coredevice_platform_driver(hf_drv);
+device_platform_driver(hf_drv);
 
 static int of_hostfile_fixup(struct device_node *root, void *ctx)
 {
@@ -155,6 +218,9 @@ static int of_hostfile_fixup(struct device_node *root, void *ctx)
 
 	ret = of_property_write_string(node, "barebox,filename", hf->filename);
 
+	if (hf->is_blockdev)
+		ret = of_property_write_bool(node, "barebox,blockdev", true);
+
 	return ret;
 }
 
diff --git a/arch/sandbox/mach-sandbox/include/mach/hostfile.h b/arch/sandbox/mach-sandbox/include/mach/hostfile.h
index e2f44c4f7b0c..c3f9af97c451 100644
--- a/arch/sandbox/mach-sandbox/include/mach/hostfile.h
+++ b/arch/sandbox/mach-sandbox/include/mach/hostfile.h
@@ -7,6 +7,7 @@ struct hf_info {
 	unsigned long long size;
 	const char *devname;
 	const char *filename;
+	unsigned int is_blockdev:1;
 };
 
 int barebox_register_filedev(struct hf_info *hf);
diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 63839a1ccc68..778ac3d6e60f 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -252,7 +252,7 @@ static int add_image(char *str, char *devname_template, int *devname_number)
 	struct hf_info *hf = malloc(sizeof(struct hf_info));
 	char *filename, *devname;
 	char tmp[16];
-	int readonly = 0;
+	int readonly = 0, cdev = 0, blkdev = 0;
 	struct stat s;
 	char *opt;
 	int fd, ret;
@@ -264,6 +264,10 @@ static int add_image(char *str, char *devname_template, int *devname_number)
 	while ((opt = strsep_unescaped(&str, ","))) {
 		if (!strcmp(opt, "ro"))
 			readonly = 1;
+		if (!strcmp(opt, "cdev"))
+			cdev = 1;
+		if (!strcmp(opt, "blkdev"))
+			blkdev = 1;
 	}
 
 	/* parses: "devname=filename" */
@@ -282,6 +286,7 @@ static int add_image(char *str, char *devname_template, int *devname_number)
 	fd = open(filename, (readonly ? O_RDONLY : O_RDWR) | O_CLOEXEC);
 	hf->fd = fd;
 	hf->filename = filename;
+	hf->is_blockdev = blkdev;
 
 	if (fd < 0) {
 		perror("open");
@@ -301,6 +306,8 @@ static int add_image(char *str, char *devname_template, int *devname_number)
 			perror("ioctl");
 			goto err_out;
 		}
+		if (!cdev)
+			hf->is_blockdev = 1;
 	}
 	if (hf->size <= SIZE_MAX)
 		hf->base = (unsigned long)mmap(NULL, hf->size,
@@ -312,6 +319,12 @@ static int add_image(char *str, char *devname_template, int *devname_number)
 	if (hf->base == (unsigned long)MAP_FAILED)
 		printf("warning: mmapping %s failed: %s\n", filename, strerror(errno));
 
+	if (blkdev && hf->size % 512 != 0) {
+		printf("warning: registering %s as block device failed: invalid block size\n",
+		       filename);
+		return -EINVAL;
+	}
+
 	ret = barebox_register_filedev(hf);
 	if (ret)
 		goto err_out;
-- 
2.28.0


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] fixup! sandbox: implement actual sandbox reset via exec(2)
  2020-09-14 13:37 ` [PATCH 4/5] sandbox: implement actual sandbox reset via exec(2) Ahmad Fatoum
@ 2020-09-15  5:21   ` Ahmad Fatoum
  0 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2020-09-15  5:21 UTC (permalink / raw)
  To: barebox

add_image modifies the argument passed to it, we want to reexec with
the original argv however, thus make a copy of the argument.

---
 arch/sandbox/os/common.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 778ac3d6e60f..d0addef5af49 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -247,10 +247,10 @@ extern void mem_malloc_init(void *start, void *end);
 
 extern char * strsep_unescaped(char **s, const char *ct);
 
-static int add_image(char *str, char *devname_template, int *devname_number)
+static int add_image(const char *_str, char *devname_template, int *devname_number)
 {
 	struct hf_info *hf = malloc(sizeof(struct hf_info));
-	char *filename, *devname;
+	char *str, *filename, *devname;
 	char tmp[16];
 	int readonly = 0, cdev = 0, blkdev = 0;
 	struct stat s;
@@ -260,6 +260,8 @@ static int add_image(char *str, char *devname_template, int *devname_number)
 	if (!hf)
 		return -1;
 
+	str = strdup(_str);
+
 	filename = strsep_unescaped(&str, ",");
 	while ((opt = strsep_unescaped(&str, ","))) {
 		if (!strcmp(opt, "ro"))
-- 
2.28.0


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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep()
  2020-09-14 13:37 [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep() Ahmad Fatoum
                   ` (3 preceding siblings ...)
  2020-09-14 13:37 ` [PATCH 5/5] sandbox: hostfile: support registering images as barebox block devices Ahmad Fatoum
@ 2020-09-15  7:26 ` Sascha Hauer
  4 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2020-09-15  7:26 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Mon, Sep 14, 2020 at 03:37:44PM +0200, Ahmad Fatoum wrote:
> With the recent changes to ARCH=sandbox, there are no remaining
> in-tree users for strtok() anymore. Out-of-tree users are better
> served by using the reentrant strsep(), which has existed in-tree
> for as long.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
> Should be applied after <20200914100553.24808-1-a.fatoum@pengutronix.de>
> ---
>  include/linux/string.h |  2 --
>  lib/string.c           | 35 +++--------------------------------
>  2 files changed, 3 insertions(+), 34 deletions(-)

Applied, thanks

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] 7+ messages in thread

end of thread, other threads:[~2020-09-15  7:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-14 13:37 [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep() Ahmad Fatoum
2020-09-14 13:37 ` [PATCH 2/5] ls: don't print . and .. on recursive ls Ahmad Fatoum
2020-09-14 13:37 ` [PATCH 3/5] sandbox: implement simple, ^C-interruptible, restart handler Ahmad Fatoum
2020-09-14 13:37 ` [PATCH 4/5] sandbox: implement actual sandbox reset via exec(2) Ahmad Fatoum
2020-09-15  5:21   ` [PATCH] fixup! " Ahmad Fatoum
2020-09-14 13:37 ` [PATCH 5/5] sandbox: hostfile: support registering images as barebox block devices Ahmad Fatoum
2020-09-15  7:26 ` [PATCH 1/5] lib: string: retire deprecated strtok() in favor of reentrant strsep() Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox