preinit: parse rootfstype from kernel command line
This commit is contained in:
parent
f3225c2bd5
commit
61dc5beca8
5 changed files with 116 additions and 13 deletions
4
pkgs/preinit/Makefile
Normal file
4
pkgs/preinit/Makefile
Normal file
|
@ -0,0 +1,4 @@
|
|||
CFLAGS+=-Os -DPREINIT_USE_LIBC -fno-stack-protector -fpic -fPIC
|
||||
LDFLAGS=-static
|
||||
|
||||
preinit: preinit.o parseopts.o
|
|
@ -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
|
||||
|
|
97
pkgs/preinit/parseopts.c
Normal file
97
pkgs/preinit/parseopts.c
Normal file
|
@ -0,0 +1,97 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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 <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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
|
|
@ -10,6 +10,8 @@
|
|||
#endif
|
||||
#include <asm/setup.h>
|
||||
|
||||
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));
|
||||
|
||||
|
|
5
pkgs/preinit/shell.nix
Normal file
5
pkgs/preinit/shell.nix
Normal file
|
@ -0,0 +1,5 @@
|
|||
with import <nixpkgs> {};
|
||||
mkShell {
|
||||
name = "preinit-env";
|
||||
src = ./.;
|
||||
}
|
Loading…
Reference in a new issue