2023-04-15 18:35:02 +02:00
|
|
|
#include <string.h>
|
2023-11-05 00:53:11 +01:00
|
|
|
#include <stdlib.h>
|
2023-04-15 18:35:02 +02:00
|
|
|
#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>
|
2024-09-07 23:28:50 +02:00
|
|
|
#include <sys/stat.h>
|
2023-04-15 18:35:02 +02:00
|
|
|
#include <string.h>
|
2023-11-05 00:53:11 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
2024-09-07 23:28:50 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <dirent.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
|
|
|
|
2024-01-08 00:52:01 +01:00
|
|
|
#include "opts.h"
|
2023-10-19 19:56:09 +02:00
|
|
|
|
2023-04-15 18:35:02 +02:00
|
|
|
#define ERR(x) write(2, x, strlen(x))
|
2024-04-21 15:38:44 +02:00
|
|
|
#define AVER(c) do { if(c < 0) { ERR("failed: " #c ": error=0x" ); pr_u32(errno); ERR ( " - "); ERR(strerror(errno)); ERR("\n"); } } while(0)
|
2023-11-05 00:58:33 +01:00
|
|
|
|
|
|
|
char * pr_u32(int32_t input);
|
2023-04-15 18:35:02 +02:00
|
|
|
|
2023-11-05 00:56:05 +01:00
|
|
|
static void die() {
|
|
|
|
/* if init exits, it causes a kernel panic. On the Turris
|
|
|
|
* Omnia (and maybe other hardware, I don't know), the kernel
|
|
|
|
* panics _before_ any of the messages from AVER are printed,
|
|
|
|
* which makes it really hard to tell what went wrong. So
|
|
|
|
* let's wait a little here to give the console a chance to
|
|
|
|
* catch up.
|
|
|
|
*
|
|
|
|
* Yes, I know that file descriptor IO is supposedly
|
|
|
|
* non-buffered. Empirical observation suggests that there
|
|
|
|
* must be a buffer of some kind somewhere though.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sleep(10);
|
|
|
|
exit(1);
|
2023-04-15 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-12 20:00:57 +02:00
|
|
|
static int fork_exec(char * command, char *args[])
|
2023-04-15 18:35:02 +02:00
|
|
|
{
|
2023-10-19 20:07:13 +02:00
|
|
|
int fork_pid = fork();
|
|
|
|
AVER(fork_pid);
|
|
|
|
if(fork_pid > 0)
|
2023-11-05 00:53:11 +01:00
|
|
|
return wait(NULL);
|
2023-10-19 20:07:13 +02:00
|
|
|
else
|
|
|
|
return execve(command, args, NULL);
|
2023-04-15 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 23:28:50 +02:00
|
|
|
static void debug_listdir(const char * path)
|
|
|
|
{
|
|
|
|
DIR *mydir;
|
|
|
|
struct dirent *myfile;
|
|
|
|
struct stat mystat;
|
|
|
|
|
|
|
|
char buf[512];
|
|
|
|
mydir = opendir(path);
|
|
|
|
while((myfile = readdir(mydir)) != NULL)
|
|
|
|
{
|
|
|
|
sprintf(buf, "%s/%s", path, myfile->d_name);
|
|
|
|
stat(buf, &mystat);
|
|
|
|
printf("%llu", mystat.st_size);
|
|
|
|
printf(" %s\n", myfile->d_name);
|
|
|
|
}
|
|
|
|
closedir(mydir);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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[])
|
|
|
|
{
|
2024-01-08 00:52:01 +01:00
|
|
|
struct root_opts opts = {
|
|
|
|
.device = NULL,
|
|
|
|
.fstype = NULL,
|
|
|
|
.mount_opts = NULL
|
|
|
|
};
|
2023-11-05 00:53:11 +01:00
|
|
|
|
2023-10-19 20:07:13 +02:00
|
|
|
write(1, banner, strlen(banner));
|
2023-04-15 18:35:02 +02:00
|
|
|
|
2023-11-05 00:58:33 +01:00
|
|
|
AVER(mount("none", "/proc", "proc", 0, NULL));
|
2023-12-07 21:03:03 +01:00
|
|
|
AVER(mount("none", "/dev", "devtmpfs", 0, NULL));
|
2023-04-15 18:35:02 +02:00
|
|
|
|
2023-10-19 20:07:13 +02:00
|
|
|
int cmdline = open("/proc/cmdline", O_RDONLY, 0);
|
2023-04-15 18:35:02 +02:00
|
|
|
|
2023-10-19 20:07:13 +02:00
|
|
|
if(cmdline>=0) {
|
|
|
|
int len = read(cmdline, buf, sizeof buf - 1);
|
|
|
|
buf[len]='\0';
|
2023-11-03 21:49:24 +01:00
|
|
|
while(buf[len-1]=='\n') {
|
|
|
|
buf[len-1]='\0';
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
write(1, "cmdline: \"", 10);
|
2023-10-19 20:07:13 +02:00
|
|
|
write(1, buf, len);
|
2023-11-03 21:49:24 +01:00
|
|
|
write(1, "\"\n", 2);
|
2023-11-05 00:58:33 +01:00
|
|
|
} else {
|
|
|
|
ERR("failed: open(\"/proc/cmdline\")\n");
|
|
|
|
die();
|
|
|
|
}
|
2024-01-08 00:52:01 +01:00
|
|
|
parseopts(buf, &opts);
|
2023-11-05 00:58:33 +01:00
|
|
|
|
2024-01-08 00:52:01 +01:00
|
|
|
if(opts.device) {
|
|
|
|
if(!opts.fstype) opts.fstype = "jffs2"; /* backward compatibility */
|
2023-10-19 20:07:13 +02:00
|
|
|
write(1, "rootdevice ", 11);
|
2024-01-08 00:52:01 +01:00
|
|
|
write(1, opts.device, strlen(opts.device));
|
2023-10-19 20:07:13 +02:00
|
|
|
write(1, " (", 2);
|
2024-01-08 00:52:01 +01:00
|
|
|
write(1, opts.fstype, strlen(opts.fstype));
|
|
|
|
if(opts.mount_opts) {
|
2024-01-08 19:54:49 +01:00
|
|
|
write(1, ", opts=", 7);
|
2024-01-08 00:52:01 +01:00
|
|
|
write(1, opts.mount_opts, strlen(opts.mount_opts));
|
|
|
|
}
|
2024-04-21 15:38:44 +02:00
|
|
|
if(opts.altdevice) {
|
|
|
|
write(1, ", altdevice=", 12);
|
|
|
|
write(1, opts.altdevice, strlen(opts.altdevice));
|
|
|
|
}
|
2023-11-05 00:58:33 +01:00
|
|
|
write(1, ")\n", 2);
|
2024-04-21 15:38:44 +02:00
|
|
|
|
|
|
|
if(!opts.altdevice) {
|
|
|
|
AVER(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts));
|
|
|
|
} else {
|
|
|
|
if(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts) < 0) {
|
2024-09-07 22:28:05 +02:00
|
|
|
ERR("failed to mount primary device, mount the alternative device\n");
|
2024-04-21 15:38:44 +02:00
|
|
|
AVER(mount(opts.altdevice, "/target/persist", opts.fstype, 0, opts.mount_opts));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FUTUREWORK: any failure using `opts.device` should force us to consider rerunning this with the alternative rootfs.
|
2023-10-19 20:07:13 +02:00
|
|
|
AVER(mount("/target/persist/nix", "/target/nix",
|
|
|
|
"bind", MS_BIND, NULL));
|
2023-04-15 18:35:02 +02:00
|
|
|
|
2023-10-19 20:07:13 +02:00
|
|
|
char *exec_args[] = { "activate", "/target", NULL };
|
2024-09-07 23:28:50 +02:00
|
|
|
if (fork_exec("/target/persist/activate", exec_args) < 0) {
|
|
|
|
ERR("failed to activate the system\n");
|
|
|
|
pr_u32(errno); ERR ( " - "); ERR(strerror(errno)); ERR("\n");
|
|
|
|
goto failsafe;
|
|
|
|
}
|
|
|
|
|
2023-10-19 20:07:13 +02:00
|
|
|
AVER(chdir("/target"));
|
2023-04-15 18:35:02 +02:00
|
|
|
|
2023-10-19 20:07:13 +02:00
|
|
|
AVER(mount("/target", "/", "bind", MS_BIND | MS_REC, NULL));
|
2023-11-05 00:58:33 +01:00
|
|
|
AVER(chroot("."));
|
2023-04-15 18:35:02 +02:00
|
|
|
|
2023-10-19 20:07:13 +02:00
|
|
|
argv[0] = "init";
|
|
|
|
argv[1] = NULL;
|
2024-04-21 15:38:44 +02:00
|
|
|
|
2023-10-19 20:07:13 +02:00
|
|
|
AVER(execve("/persist/init", argv, envp));
|
|
|
|
}
|
2024-09-07 22:28:05 +02:00
|
|
|
|
2024-09-07 23:28:50 +02:00
|
|
|
failsafe:
|
|
|
|
debug_listdir("/");
|
|
|
|
debug_listdir("/target");
|
|
|
|
|
2024-09-07 22:28:05 +02:00
|
|
|
ERR("failed to mount the rootfs\n");
|
|
|
|
ERR("final stand using the failsafe initialization method\n");
|
|
|
|
ERR("the boot process is manual from now on\n");
|
|
|
|
|
|
|
|
argv[0] = "init";
|
|
|
|
argv[1] = NULL;
|
2024-09-07 23:28:50 +02:00
|
|
|
// Attempt to unmount the /target mount-bind.
|
|
|
|
AVER(umount("/target"));
|
2024-09-07 22:28:05 +02:00
|
|
|
AVER(execve("/failsafe-init", argv, envp));
|
|
|
|
|
2024-09-07 23:28:50 +02:00
|
|
|
debug_listdir("/");
|
|
|
|
debug_listdir("/target");
|
|
|
|
|
2023-11-05 00:56:05 +01:00
|
|
|
die();
|
2023-04-15 18:35:02 +02:00
|
|
|
}
|