Skip to content

Commit af0c0b3

Browse files
a3fsaschahauer
authored andcommitted
sandbox: store stickypage in runtime dir
The stickypage is a hack to have a 4K hostfile persist over reboot. This was so far done by compiling the stickypage separately and referencing it from the device tree via the magic $build variable that expands to the working directory. This breaks a number of assumptions: - KBUILD_IMAGE: should only have a single entry, e.g. barebox-flash-images ends up with two files per one line - stickypage must be writable, which may fail if barebox is installed, e.g. in r/o Nix Store Signed-off-by: Ahmad Fatoum <[email protected]> Link: https://lore.barebox.org/[email protected] Signed-off-by: Sascha Hauer <[email protected]>
1 parent c63f4e8 commit af0c0b3

File tree

10 files changed

+86
-39
lines changed

10 files changed

+86
-39
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ Module.symvers
4545
/TAGS
4646
/barebox*
4747
/System.map
48-
/stickypage.bin
4948

5049
#
5150
# git files that we don't want to ignore even it they are dot-files
@@ -95,4 +94,3 @@ GTAGS
9594
/allrandom.config
9695
/allyes.config
9796
/compile_commands.json
98-
/stickypage.bin

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ endif # CONFIG_MODULES
11541154

11551155
# Directories & files removed with 'make clean'
11561156
CLEAN_DIRS += $(MODVERDIR)
1157-
CLEAN_FILES += barebox System.map stickypage.bin include/generated/barebox_default_env.h \
1157+
CLEAN_FILES += barebox System.map include/generated/barebox_default_env.h \
11581158
.tmp_version .tmp_barebox* barebox.bin barebox.map \
11591159
.tmp_kallsyms* barebox.ldr compile_commands.json \
11601160
barebox-flash-image \

arch/sandbox/Makefile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ KBUILD_CFLAGS += -Dmalloc=barebox_malloc -Dcalloc=barebox_calloc \
3131
-Dclosedir=barebox_closedir -Dreadlink=barebox_readlink \
3232
-Doptarg=barebox_optarg -Doptind=barebox_optind \
3333
-Dsetjmp=barebox_setjmp -Dlongjmp=barebox_longjmp \
34-
-Dputchar=barebox_putchar
34+
-Dmkdir=barebox_mkdir -Ddirname=barebox_dirname \
35+
-Dremove=barebox_remove -Dputchar=barebox_putchar
3536

3637
machdirs := $(patsubst %,arch/sandbox/mach-%/,$(machine-y))
3738

@@ -75,11 +76,7 @@ cmd_barebox__ = $(CC) -o $@ $(BAREBOX_LDFLAGS)
7576

7677
common-y += $(BOARD) arch/sandbox/os/ arch/sandbox/lib/
7778

78-
stickypage.bin:
79-
@$(kecho) " LN stickypage.bin"
80-
@ln -fs arch/sandbox/board/stickypage.bin stickypage.bin
81-
82-
KBUILD_IMAGE := barebox stickypage.bin
79+
KBUILD_IMAGE := barebox
8380

8481
common-$(CONFIG_OFTREE) += arch/sandbox/dts/
8582

arch/sandbox/board/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22

33
barebox.lds
4-
stickypage.bin

arch/sandbox/board/Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,4 @@ obj-$(CONFIG_LED) += led.o
1313

1414
extra-y += barebox.lds
1515

16-
extra-y += stickypage.bin
17-
18-
OBJCOPYFLAGS_stickypage.bin = -O binary
19-
20-
%.bin: %.o
21-
$(call if_changed,objcopy)
16+
obj-y += stickypage.o

arch/sandbox/board/hostfile.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,20 @@ static int of_hostfile_map_fixup(struct device_node *root, void *ctx)
248248
struct device_node *node;
249249
int ret;
250250

251-
for_each_compatible_node_from(node, root, NULL, hostfile_dt_ids->compatible) {
251+
for_each_compatible_node_from(node, root, NULL, "barebox,stickypage") {
252+
char *filename;
253+
254+
filename = linux_get_stickypage_path();
255+
if (!filename) {
256+
pr_err("error allocating stickypage\n");
257+
continue;
258+
}
259+
260+
of_property_write_string(node, "barebox,filename", filename);
261+
of_property_write_string(node, "compatible", "barebox,hostfile");
262+
}
263+
264+
for_each_compatible_node_from(node, root, NULL, "barebox,hostfile") {
252265
struct hf_info hf = {};
253266
uint64_t reg[2] = {};
254267

@@ -260,13 +273,6 @@ static int of_hostfile_map_fixup(struct device_node *root, void *ctx)
260273
continue;
261274
}
262275

263-
if (memcmp(hf.filename, "$build/", 7) == 0) {
264-
char *fullpath = xasprintf("%s/%s", linux_get_builddir(),
265-
hf.filename + sizeof "$build/" - 1);
266-
267-
hf.filename = fullpath;
268-
}
269-
270276
hf.is_blockdev = of_property_read_bool(node, "barebox,blockdev");
271277
hf.is_cdev = of_property_read_bool(node, "barebox,cdev");
272278
hf.is_readonly = of_property_read_bool(node, "barebox,read-only");

arch/sandbox/board/stickypage.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22

3+
.section .note.GNU-stack,"",%progbits
4+
.section .rodata.stickypage,"a"
5+
36
.globl stickypage;
47
stickypage:
58

arch/sandbox/dts/sandbox.dts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@
5656
};
5757

5858
stickypage: stickypage {
59-
compatible = "barebox,hostfile", "syscon";
60-
barebox,filename = "$build/stickypage.bin";
59+
compatible = "barebox,stickypage", "syscon";
6160
reg = <0 0 0 4096>;
6261
barebox,cdev; /* no caching allowed */
6362
barebox,feature-controller;

arch/sandbox/mach-sandbox/include/mach/linux.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ int linux_register_device(const char *name, void *start, void *end);
1515
int tap_alloc(const char *dev);
1616
uint64_t linux_get_time(void);
1717
int linux_open(const char *filename, int readwrite);
18-
const char *linux_get_builddir(void);
18+
char *linux_get_stickypage_path(void);
1919
int linux_open_hostfile(struct hf_info *hf);
2020
int linux_read(int fd, void *buf, size_t count);
2121
int linux_read_nonblock(int fd, void *buf, size_t count);

arch/sandbox/os/common.c

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ static void cookmode(void)
7575
tcsetattr(0, TCSANOW, &term_orig);
7676
}
7777

78+
static char *stickypage_path;
79+
80+
static void prepare_exit(void)
81+
{
82+
cookmode();
83+
if (stickypage_path)
84+
remove(stickypage_path);
85+
}
86+
7887
int linux_tstc(int fd)
7988
{
8089
struct timeval tv = {
@@ -122,7 +131,7 @@ uint64_t linux_get_time(void)
122131

123132
void __attribute__((noreturn)) linux_exit(void)
124133
{
125-
cookmode();
134+
prepare_exit();
126135
exit(0);
127136
}
128137

@@ -167,7 +176,7 @@ void linux_reexec(void)
167176

168177
void linux_hang(void)
169178
{
170-
cookmode();
179+
prepare_exit();
171180
/* falls through to generic hang() */
172181
}
173182

@@ -329,19 +338,60 @@ static int add_image(const char *_str, char *devname_template, int *devname_numb
329338
return ret;
330339
}
331340

332-
const char *linux_get_builddir(void)
341+
extern uint8_t stickypage[4096];
342+
343+
char *linux_get_stickypage_path(void)
333344
{
334-
static char path[4097];
335-
int ret;
345+
size_t nwritten;
346+
ssize_t ret;
347+
int fd;
336348

337-
if (!path[0]) {
338-
ret = selfpath(path, sizeof(path));
339-
if (ret < 0)
340-
return NULL;
341-
dirname(path);
349+
ret = asprintf(&stickypage_path, "%s/barebox/stickypage.%lu",
350+
getenv("XDG_RUNTIME_DIR") ?: "/run", (long)getpid());
351+
if (ret < 0)
352+
goto err_asprintf;
353+
354+
ret = mkdir(dirname(stickypage_path), 0755);
355+
if (ret < 0 && errno != EEXIST) {
356+
perror("mkdir");
357+
goto err_creat;
342358
}
343359

344-
return path;
360+
stickypage_path[strlen(stickypage_path)] = '/';
361+
362+
fd = open(stickypage_path, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
363+
if (fd < 0) {
364+
if (errno == EEXIST)
365+
return stickypage_path;
366+
367+
perror("open");
368+
goto err_creat;
369+
}
370+
371+
for (nwritten = 0; nwritten < sizeof(stickypage); ) {
372+
ret = write(fd, &stickypage[nwritten], sizeof(stickypage) - nwritten);
373+
if (ret < 0) {
374+
if (errno == EINTR || errno == EAGAIN)
375+
continue;
376+
perror("write");
377+
goto err_write;
378+
}
379+
380+
nwritten += ret;
381+
}
382+
383+
close(fd);
384+
385+
return stickypage_path;
386+
387+
err_write:
388+
close(fd);
389+
err_creat:
390+
free(stickypage_path);
391+
err_asprintf:
392+
stickypage_path = NULL;
393+
394+
return NULL;
345395
}
346396

347397
int linux_open_hostfile(struct hf_info *hf)
@@ -469,7 +519,7 @@ int main(int argc, char *argv[])
469519
char *aux;
470520

471521
#ifdef CONFIG_ASAN
472-
__sanitizer_set_death_callback(cookmode);
522+
__sanitizer_set_death_callback(prepare_exit);
473523
#endif
474524

475525
while (1) {

0 commit comments

Comments
 (0)