summaryrefslogtreecommitdiffstats
path: root/arch/sandbox
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2020-10-12 08:26:11 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2020-10-13 08:45:15 +0200
commit606300a214f544403ffbc1c8169abbf89cb2b8c5 (patch)
tree7399c109ca1e042e3291e2085f304c58ba2f2448 /arch/sandbox
parente3363cf8ff06e9f7db60cca2b89b77a62f7f3a7c (diff)
downloadbarebox-606300a214f544403ffbc1c8169abbf89cb2b8c5.tar.gz
barebox-606300a214f544403ffbc1c8169abbf89cb2b8c5.tar.xz
sandbox: hostfile: support anonymous hostfiles in device tree
So far use of hostfile in the device tree required us to hard code a file name. If we instead create a temporary file on demand, we can support: - environment - barebox-state - syscon for reset source and reboot mode out of the box with no dependency on external files. Do the necessary, so a hostfile device tree node without a barebox,filename gets a temporary file generated with the appropriate size. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'arch/sandbox')
-rw-r--r--arch/sandbox/Makefile1
-rw-r--r--arch/sandbox/board/hostfile.c17
-rw-r--r--arch/sandbox/os/common.c28
3 files changed, 41 insertions, 5 deletions
diff --git a/arch/sandbox/Makefile b/arch/sandbox/Makefile
index 09112c3ba8..23832e6b2f 100644
--- a/arch/sandbox/Makefile
+++ b/arch/sandbox/Makefile
@@ -24,6 +24,7 @@ KBUILD_CFLAGS += -Dmalloc=barebox_malloc -Dcalloc=barebox_calloc \
-Dgetenv=barebox_getenv -Dprintf=barebox_printf \
-Dglob=barebox_glob -Dglobfree=barebox_globfree \
-Dioctl=barebox_ioctl -Dfstat=barebox_fstat \
+ -Dftruncate=barebox_ftruncate \
-Dopendir=barebox_opendir -Dreaddir=barebox_readdir \
-Dclosedir=barebox_closedir -Dreadlink=barebox_readlink \
-Doptarg=barebox_optarg -Doptind=barebox_optind
diff --git a/arch/sandbox/board/hostfile.c b/arch/sandbox/board/hostfile.c
index 8990e20f15..2d4e5d55ea 100644
--- a/arch/sandbox/board/hostfile.c
+++ b/arch/sandbox/board/hostfile.c
@@ -226,18 +226,23 @@ static int of_hostfile_map_fixup(struct device_node *root, void *ctx)
for_each_compatible_node_from(node, root, NULL, hostfile_dt_ids->compatible) {
struct hf_info hf = {};
- uint64_t reg[2];
+ uint64_t reg[2] = {};
+ bool no_filename;
hf.devname = node->name;
ret = of_property_read_string(node, "barebox,filename", &hf.filename);
- if (ret)
- goto out;
+ no_filename = ret;
hf.is_blockdev = of_property_read_bool(node, "barebox,blockdev");
hf.is_cdev = of_property_read_bool(node, "barebox,cdev");
hf.is_readonly = of_property_read_bool(node, "barebox,read-only");
+ of_property_read_u64_array(node, "reg", reg, ARRAY_SIZE(reg));
+
+ hf.base = reg[0];
+ hf.size = reg[1];
+
ret = linux_open_hostfile(&hf);
if (ret)
goto out;
@@ -253,6 +258,12 @@ static int of_hostfile_map_fixup(struct device_node *root, void *ctx)
if (ret)
goto out;
+ if (no_filename) {
+ ret = of_property_write_string(node, "barebox,filename", hf.filename);
+ if (ret)
+ goto out;
+ }
+
ret = of_property_write_u32(node, "barebox,fd", hf.fd);
out:
if (ret)
diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 6032a8c26b..5326df4891 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -292,14 +292,36 @@ static int add_image(const char *_str, char *devname_template, int *devname_numb
int linux_open_hostfile(struct hf_info *hf)
{
+ char *buf = NULL;
struct stat s;
int fd;
printf("add %s backed by file %s%s\n", hf->devname,
hf->filename, hf->is_readonly ? "(ro)" : "");
- fd = hf->fd = open(hf->filename, (hf->is_readonly ? O_RDONLY : O_RDWR) | O_CLOEXEC);
- hf->base = (unsigned long)MAP_FAILED;
+ if (hf->filename) {
+ fd = hf->fd = open(hf->filename, (hf->is_readonly ? O_RDONLY : O_RDWR) | O_CLOEXEC);
+ } else {
+ buf = strdup("/tmp/barebox-hostfileXXXXXX");
+ int ret;
+
+ fd = hf->fd = mkstemp(buf);
+ if (fd >= 0) {
+ ret = fcntl(fd, F_SETFD, FD_CLOEXEC);
+ if (ret < 0) {
+ perror("fcntl");
+ goto err_out;
+ }
+
+ ret = ftruncate(fd, hf->size);
+ if (ret < 0) {
+ perror("ftruncate");
+ goto err_out;
+ }
+
+ hf->filename = buf;
+ }
+ }
if (fd < 0) {
perror("open");
@@ -311,6 +333,7 @@ int linux_open_hostfile(struct hf_info *hf)
goto err_out;
}
+ hf->base = (unsigned long)MAP_FAILED;
hf->size = s.st_size;
if (S_ISBLK(s.st_mode)) {
@@ -344,6 +367,7 @@ int linux_open_hostfile(struct hf_info *hf)
err_out:
if (fd > 0)
close(fd);
+ free(buf);
return -1;
}