2023-04-15 18:35:02 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2023-06-19 00:02:08 +02:00
|
|
|
#include <fcntl.h>
|
2023-04-15 18:35:02 +02:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <string.h>
|
2023-10-19 19:59:02 +02:00
|
|
|
|
|
|
|
#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
|
2023-04-15 18:35:02 +02:00
|
|
|
|
2023-10-19 19:56:09 +02:00
|
|
|
void parseopts(char * cmdline, char **root, char **rootfstype);
|
|
|
|
|
2023-04-15 18:35:02 +02:00
|
|
|
#define ERR(x) write(2, x, strlen(x))
|
|
|
|
#define AVER(c) do { if(c < 0) ERR("failed: " #c); } while(0)
|
|
|
|
|
|
|
|
static int begins_with(char * str, char * prefix)
|
|
|
|
{
|
|
|
|
while(*prefix) {
|
|
|
|
if(*str == '\0') return 0;
|
|
|
|
if(*str != *prefix) return 0;
|
|
|
|
str++;
|
|
|
|
prefix++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-10-12 20:00:57 +02:00
|
|
|
static int fork_exec(char * command, char *args[])
|
2023-04-15 18:35:02 +02:00
|
|
|
{
|
|
|
|
int fork_pid = fork();
|
|
|
|
AVER(fork_pid);
|
|
|
|
if(fork_pid > 0)
|
|
|
|
wait(NULL);
|
|
|
|
else
|
2023-10-12 20:00:57 +02:00
|
|
|
return execve(command, args, NULL);
|
2023-04-15 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char banner[] = "Running pre-init...\n";
|
|
|
|
char buf[COMMAND_LINE_SIZE];
|
|
|
|
|
|
|
|
int main(int argc, char *argv[], char *envp[])
|
|
|
|
{
|
|
|
|
char *rootdevice = 0;
|
2023-10-19 19:56:09 +02:00
|
|
|
char *rootfstype = 0;
|
2023-04-15 18:35:02 +02:00
|
|
|
char *p = buf;
|
|
|
|
write(1, banner, strlen(banner));
|
|
|
|
|
|
|
|
mount("none", "/proc", "proc", 0, NULL);
|
|
|
|
|
|
|
|
int cmdline = open("/proc/cmdline", O_RDONLY, 0);
|
|
|
|
|
|
|
|
if(cmdline>=0) {
|
|
|
|
int len = read(cmdline, buf, sizeof buf - 1);
|
|
|
|
buf[len]='\0';
|
|
|
|
write(1, "cmdline ", 8);
|
|
|
|
write(1, buf, len);
|
|
|
|
};
|
|
|
|
|
2023-10-19 19:56:09 +02:00
|
|
|
parseopts(buf, &rootdevice, &rootfstype);
|
|
|
|
|
2023-04-15 18:35:02 +02:00
|
|
|
if(rootdevice) {
|
2023-10-19 19:56:09 +02:00
|
|
|
if(!rootfstype) rootfstype = "jffs2"; /* backward compatibility */
|
2023-04-15 18:35:02 +02:00
|
|
|
write(1, "rootdevice ", 11);
|
|
|
|
write(1, rootdevice, strlen(rootdevice));
|
2023-10-19 19:56:09 +02:00
|
|
|
write(1, " (", 2);
|
|
|
|
write(1, rootfstype, strlen(rootfstype));
|
|
|
|
write(1, ")\n", 1);
|
2023-04-15 18:35:02 +02:00
|
|
|
|
2023-10-19 19:56:09 +02:00
|
|
|
AVER(mount(rootdevice, "/target/persist", rootfstype, 0, NULL));
|
2023-04-15 18:35:02 +02:00
|
|
|
AVER(mount("/target/persist/nix", "/target/nix",
|
|
|
|
"bind", MS_BIND, NULL));
|
|
|
|
|
2023-06-22 10:29:44 +02:00
|
|
|
char *exec_args[] = { "activate", "/target", NULL };
|
2023-10-12 20:00:57 +02:00
|
|
|
AVER(fork_exec("/target/persist/activate", exec_args));
|
2023-04-15 18:35:02 +02:00
|
|
|
AVER(chdir("/target"));
|
|
|
|
|
|
|
|
AVER(mount("/target", "/", "bind", MS_BIND | MS_REC, NULL));
|
|
|
|
AVER(chroot("."));
|
|
|
|
argv[0] = "init";
|
|
|
|
argv[1] = NULL;
|
|
|
|
|
|
|
|
AVER(execve("/persist/init", argv, envp));
|
|
|
|
}
|
|
|
|
}
|