From 61dc5beca8804b70fb10559d38017526523452be Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 19 Oct 2023 18:56:09 +0100 Subject: [PATCH] preinit: parse rootfstype from kernel command line --- pkgs/preinit/Makefile | 4 ++ pkgs/preinit/default.nix | 1 - pkgs/preinit/parseopts.c | 97 ++++++++++++++++++++++++++++++++++++++++ pkgs/preinit/preinit.c | 22 +++++---- pkgs/preinit/shell.nix | 5 +++ 5 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 pkgs/preinit/Makefile create mode 100644 pkgs/preinit/parseopts.c create mode 100644 pkgs/preinit/shell.nix diff --git a/pkgs/preinit/Makefile b/pkgs/preinit/Makefile new file mode 100644 index 0000000..8d09ce0 --- /dev/null +++ b/pkgs/preinit/Makefile @@ -0,0 +1,4 @@ +CFLAGS+=-Os -DPREINIT_USE_LIBC -fno-stack-protector -fpic -fPIC +LDFLAGS=-static + +preinit: preinit.o parseopts.o diff --git a/pkgs/preinit/default.nix b/pkgs/preinit/default.nix index 232f2f4..4aab639 100644 --- a/pkgs/preinit/default.nix +++ b/pkgs/preinit/default.nix @@ -15,7 +15,6 @@ stdenv.mkDerivation { # NIX_DEBUG=2; hardeningDisable = [ "all" ]; - CFLAGS = "-Os -static -DPREINIT_USE_LIBC -fno-stack-protector -fpic -fPIC -I ./ -I ${kernel}/tools/include/nolibc"; postBuild = '' $STRIP --remove-section=.note --remove-section=.comment preinit diff --git a/pkgs/preinit/parseopts.c b/pkgs/preinit/parseopts.c new file mode 100644 index 0000000..d21255a --- /dev/null +++ b/pkgs/preinit/parseopts.c @@ -0,0 +1,97 @@ +#include +#include + +static int begins_with(char * str, char * prefix) +{ + while(*prefix) { + if(*str == '\0') return 0; + if(*str != *prefix) return 0; + str++; + prefix++; + } + return 1; +} + + +void parseopts(char * cmdline, char **root, char **rootfstype) { + *root = 0; + *rootfstype = 0; + + for(char *p = cmdline; *p; p++) { + if(begins_with(p, "root=")) { + *root = p + strlen("root="); + while(*p && (*p != ' ')) p++; + + if(*p) { + *p = '\0'; + p++; + }; + }; + if(begins_with(p, "rootfstype=")) { + *rootfstype = p + strlen("rootfstype="); + while(*p && (*p != ' ')) p++; + if(*p) { + *p = '\0'; + p++; + }; + }; + }; +} + +#ifdef TESTS +#include +#include +#include + +#define die(fmt, ...) do { printf(fmt, __VA_ARGS__); exit(1); } while(0) +#define S(x) #x +#define expect_equal(actual, expected) \ + if(!actual || strcmp(actual, expected)) die("%d: expected \"%s\", got \"%s\"", __LINE__, expected, actual); + + +int main() +{ + char * root = "/dev/hda1"; + char * rootfstype = "xiafs"; + char *buf; + + // finds root= and rootfstype= options + buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 root=/dev/ubi0_4 rootfstype=ubifs fw_devlink=off mtdparts=phram0:18M(rootfs) phram.phram=phram0,0x40400000,18874368,65536 root=/dev/mtdblock0 foo"); + parseopts(buf, &root, &rootfstype); + expect_equal(root, "/dev/mtdblock0"); + expect_equal(rootfstype, "ubifs"); + + // in case of duplicates, chooses the latter + // also: works if the option is end of string + buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 root=/dev/ubi0_4 rootfstype=ubifs fw_devlink=off mtdparts=phram0:18M(rootfs) phram.phram=phram0,0x40400000,18874368,65536 root=/dev/mtdblock0"); + parseopts(buf, &root, &rootfstype); + expect_equal(root, "/dev/mtdblock0"); + expect_equal(rootfstype, "ubifs"); + + // options may appear in either order + buf = strdup("liminix fw_devlink=off root=/dev/hda1 rootfstype=ubifs foo"); + parseopts(buf, &root, &rootfstype); + expect_equal(root, "/dev/hda1"); + expect_equal(rootfstype, "ubifs"); + + buf = strdup("liminix rootfstype=ubifs fw_devlink=off root=/dev/hda1 foo"); + parseopts(buf, &root, &rootfstype); + expect_equal(rootfstype, "ubifs"); + expect_equal(root, "/dev/hda1"); + + // provides NULL for missing options + buf = strdup("liminix rufustype=ubifs fw_devlink=off foot=/dev/hda1"); + parseopts(buf, &root, &rootfstype); + if(rootfstype) die("expected null rootfstype, got \"%s\"", rootfstype); + if(root) die("expected null root, got \"%s\"", root); + + // provides empty strings for empty options + buf = strdup("liminix rootfstype= fw_devlink=off root= /dev/hda1"); + parseopts(buf, &root, &rootfstype); + if(strlen(rootfstype)) die("expected empty rootfstype, got \"%s\"", rootfstype); + if(strlen(root)) die("expected null root, got \"%s\"", root); + +} + + +#endif diff --git a/pkgs/preinit/preinit.c b/pkgs/preinit/preinit.c index 023c428..7b95c55 100644 --- a/pkgs/preinit/preinit.c +++ b/pkgs/preinit/preinit.c @@ -10,6 +10,8 @@ #endif #include +void parseopts(char * cmdline, char **root, char **rootfstype); + #define ERR(x) write(2, x, strlen(x)) #define AVER(c) do { if(c < 0) ERR("failed: " #c); } while(0) @@ -40,6 +42,7 @@ char buf[COMMAND_LINE_SIZE]; int main(int argc, char *argv[], char *envp[]) { char *rootdevice = 0; + char *rootfstype = 0; char *p = buf; write(1, banner, strlen(banner)); @@ -54,22 +57,17 @@ int main(int argc, char *argv[], char *envp[]) write(1, buf, len); }; - while(*p) { - if(begins_with(p, "root=")) { - rootdevice = p + 5; - while(*p && (*p != ' ')) p++; - *p= '\0'; - } - while(*p && (*p != ' ')) p++; - p++; - } - + parseopts(buf, &rootdevice, &rootfstype); + if(rootdevice) { + if(!rootfstype) rootfstype = "jffs2"; /* backward compatibility */ write(1, "rootdevice ", 11); write(1, rootdevice, strlen(rootdevice)); - write(1, "\n", 1); + write(1, " (", 2); + write(1, rootfstype, strlen(rootfstype)); + write(1, ")\n", 1); - AVER(mount(rootdevice, "/target/persist", "ubifs", 0, NULL)); + AVER(mount(rootdevice, "/target/persist", rootfstype, 0, NULL)); AVER(mount("/target/persist/nix", "/target/nix", "bind", MS_BIND, NULL)); diff --git a/pkgs/preinit/shell.nix b/pkgs/preinit/shell.nix new file mode 100644 index 0000000..b6da65d --- /dev/null +++ b/pkgs/preinit/shell.nix @@ -0,0 +1,5 @@ +with import {}; +mkShell { + name = "preinit-env"; + src = ./.; +}