aboutsummaryrefslogtreecommitdiff
path: root/src/init
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-11-12 00:37:26 +0100
committerXavier Del Campo Romero <xavi92@disroot.org>2025-11-16 22:57:45 +0100
commit2ce58c995946f85666e793c4f06efff683e76ae4 (patch)
treefbf2658bb0b0f61dadcf4ca27f997eaded78aae5 /src/init
parent5ce25ae3b5d8666d373f7d7e336546ce8508c213 (diff)
Diffstat (limited to 'src/init')
-rw-r--r--src/init/private_include/init/types.h1
-rw-r--r--src/init/ps1/private_include/init/ps1/types.h3
-rw-r--r--src/init/ps1/src/boot.c166
-rw-r--r--src/init/src/boot.c35
4 files changed, 145 insertions, 60 deletions
diff --git a/src/init/private_include/init/types.h b/src/init/private_include/init/types.h
index 7f6114d..9bbb5bd 100644
--- a/src/init/private_include/init/types.h
+++ b/src/init/private_include/init/types.h
@@ -25,6 +25,7 @@
struct init
{
+ size_t path_i;
struct aio *aio;
struct stat sb;
unsigned retries;
diff --git a/src/init/ps1/private_include/init/ps1/types.h b/src/init/ps1/private_include/init/ps1/types.h
index 66702d1..33c2281 100644
--- a/src/init/ps1/private_include/init/ps1/types.h
+++ b/src/init/ps1/private_include/init/ps1/types.h
@@ -20,16 +20,19 @@
#define INIT_PS1_TYPES_H
#include <aio.h>
+#include <fs/fs.h>
#include <sys/stat.h>
#include <time.h>
struct init_ps1
{
+ size_t stream_i;
struct aio *aio;
struct stat *sb;
unsigned retries;
const char *path;
struct timespec last_retry;
+ struct fs_stdstreams ss;
int (*next)(struct init_ps1 *);
int (*success)(struct init_ps1 *);
int (*retry)(struct init_ps1 *);
diff --git a/src/init/ps1/src/boot.c b/src/init/ps1/src/boot.c
index 062078c..2b5e600 100644
--- a/src/init/ps1/src/boot.c
+++ b/src/init/ps1/src/boot.c
@@ -20,93 +20,112 @@
#include <init/ps1/types.h>
#include <aio.h>
#include <bin.h>
+#include <fs/fs.h>
#include <iso9660.h>
#include <kprintf.h>
#include <loop.h>
#include <net.h>
+#include <state.h>
#include <fcntl.h>
#include <sys/stat.h>
+#include <errno.h>
#include <stdlib.h>
-#include <state.h>
+#include <string.h>
#include <time.h>
-struct boot
-{
- struct aio *aio;
- struct init_ps1 *p;
-};
-
enum {RETRIES = 5};
static const char serial[] = "/dev/ttyS0";
+static const struct stream
+{
+ const char *path;
+ int flags;
+} streams[] =
+{
+ {.path = "/dev/null", .flags = O_RDONLY},
+ {.path = "/dev/tty", .flags = O_WRONLY},
+ {.path = "/dev/tty", .flags = O_WRONLY}
+};
static int retry_cd(struct init_ps1 *);
+static int open_stream(struct init_ps1 *p);
-static int wait_retry(struct init_ps1 *const p)
+static int exec(struct init_ps1 *const p)
{
- struct timespec ts;
- const time_t last = p->last_retry.tv_sec, timeout = 5;
+ const char *const args[] = {NULL};
- if (clock_gettime(CLOCK_REALTIME, &ts))
- return -1;
- else if (ts.tv_sec - last >= timeout)
- p->next = p->retry;
+ kprintf("Opening second-stage initd\n");
+ p->next = NULL;
- return 0;
+ /* TODO: create symbolic from /mnt/cdrom/bin to /bin */
+ return bin_exec("/mnt/cdrom/bin/initd", args, &p->ss, 0, 0, 0);
}
-static int stat_done(const enum state s, void *const args)
+static int stream_opened(const enum state state, void *const args)
{
- int ret = 0;
struct init_ps1 *const p = args;
+ const struct stream *const s = &streams[p->stream_i];
- if (s)
- {
- struct timespec ts;
+ if (loop_rm_aio(p->aio))
+ return -1;
- if (!--p->retries || clock_gettime(CLOCK_REALTIME, &ts))
- {
- ret = -1;
- goto end;
- }
+ aio_free(p->aio);
- p->last_retry = ts;
- p->next = wait_retry;
- }
+ if (state)
+ kprintf("Failed to open %s: %s\n", s->path, strerror(errno));
else
{
- p->next = p->success;
- kprintf("Successfully initialized %s (took %u attempts)\n",
- p->path, RETRIES - p->retries + 1);
+ kprintf("Successfully opened %s\n", s->path);
+
+ if (++p->stream_i >= sizeof streams / sizeof *streams)
+ p->next = exec;
+ else
+ p->next = open_stream;
}
-end:
- loop_rm_aio(p->aio);
- aio_free(p->aio);
- free(p->sb);
- return ret;
+ return 0;
+}
+
+static int open_stream(struct init_ps1 *const p)
+{
+ const struct stream *const s = &streams[p->stream_i];
+ const struct fs_open o =
+ {
+ .fd = &p->ss.streams[p->stream_i],
+ .path = s->path,
+ .flags = s->flags
+ };
+
+ const struct aio_done d =
+ {
+ .f = stream_opened,
+ .args = p
+ };
+
+ struct aio *const aio = aio_open(&o, &d);
+
+ if (!aio || loop_add_aio(aio))
+ return -1;
+
+ p->aio = aio;
+ p->next = NULL;
+ return 0;
}
static int mount_done(const enum state state, void *const args)
{
- struct boot *const b = args;
+ struct init_ps1 *const p = args;
- if (loop_rm_aio(b->aio))
+ if (loop_rm_aio(p->aio))
return -1;
- aio_free(b->aio);
- free(b);
+ aio_free(p->aio);
if (state)
kprintf("Failed to mount ISO9660 filesystem\n");
else
{
- const char *const args[] = {NULL};
-
kprintf("ISO9660 filesystem mounted successfully\n");
- kprintf("Opening second-stage initd\n");
-
- /* TODO: create symbolic from /mnt/cdrom/bin to /bin */
- return bin_exec("/mnt/cdrom/bin/initd", args, 0, 0);
+ p->next = open_stream;
}
return 0;
@@ -115,11 +134,6 @@ static int mount_done(const enum state state, void *const args)
static int mount_cd(struct init_ps1 *const p)
{
struct aio *aio = NULL;
- struct boot *const b = malloc(sizeof *b);
-
- if (!b)
- goto failure;
-
const struct aio_mount fm =
{
.mount =
@@ -134,7 +148,7 @@ static int mount_cd(struct init_ps1 *const p)
const struct aio_done d =
{
- .args = b,
+ .args = p,
.f = mount_done
};
@@ -142,7 +156,7 @@ static int mount_cd(struct init_ps1 *const p)
goto failure;
p->next = NULL;
- *b = (const struct boot){.aio = aio, .p = p};
+ p->aio = aio;
kprintf("Mounting ISO9660 filesystem from %s to %s\n",
fm.mount.src, fm.mount.tgt);
return 0;
@@ -150,10 +164,54 @@ static int mount_cd(struct init_ps1 *const p)
failure:
kprintf("Failed to mount ISO9660 filesystem\n");
aio_free(aio);
- free(b);
return -1;
}
+static int wait_retry(struct init_ps1 *const p)
+{
+ struct timespec ts;
+ const time_t last = p->last_retry.tv_sec, timeout = 5;
+
+ if (clock_gettime(CLOCK_REALTIME, &ts))
+ return -1;
+ else if (ts.tv_sec - last >= timeout)
+ p->next = p->retry;
+
+ return 0;
+}
+
+static int stat_done(const enum state s, void *const args)
+{
+ int ret = 0;
+ struct init_ps1 *const p = args;
+
+ if (s)
+ {
+ struct timespec ts;
+
+ if (!--p->retries || clock_gettime(CLOCK_REALTIME, &ts))
+ {
+ ret = -1;
+ goto end;
+ }
+
+ p->last_retry = ts;
+ p->next = wait_retry;
+ }
+ else
+ {
+ p->next = p->success;
+ kprintf("Successfully initialized %s (took %u attempts)\n",
+ p->path, RETRIES - p->retries + 1);
+ }
+
+end:
+ loop_rm_aio(p->aio);
+ aio_free(p->aio);
+ free(p->sb);
+ return ret;
+}
+
static int retry_cd(struct init_ps1 *const p)
{
static const char path[] = "/dev/cd0";
diff --git a/src/init/src/boot.c b/src/init/src/boot.c
index 30d9a9e..4c3e6ad 100644
--- a/src/init/src/boot.c
+++ b/src/init/src/boot.c
@@ -27,6 +27,14 @@
static int retry(struct init *);
enum {RETRIES = 5};
+static const char *const paths[] =
+{
+ "/dev/tty",
+ "/dev/null",
+ "/dev/stdout",
+ "/dev/stdin",
+ "/dev/stderr"
+};
static int run(struct init *const init)
{
@@ -35,13 +43,26 @@ static int run(struct init *const init)
static int port(struct init *const init)
{
- kprintf("tty initilization successful (took %u attempts)\n",
- RETRIES - init->retries + 1);
+ const size_t path_i = init->path_i + 1;
- if (init_port_boot())
- return -1;
+ kprintf("%s initilization successful (took %u attempts)\n",
+ paths[init->path_i], RETRIES - init->retries + 1);
+
+ if (path_i >= sizeof paths / sizeof *paths)
+ {
+ if (init_port_boot())
+ return -1;
+
+ init->next = run;
+ }
+ else
+ init_vars = (const struct init)
+ {
+ .retries = RETRIES,
+ .next = retry,
+ .path_i = path_i
+ };
- init->next = run;
return 0;
}
@@ -88,7 +109,8 @@ end:
static int retry(struct init *const init)
{
struct aio *aio = NULL;
- const struct fs_stat s = {.path = "/dev/tty", .sb = &init->sb};
+ const size_t path_i = init->path_i;
+ const struct fs_stat s = {.path = paths[init->path_i], .sb = &init->sb};
const struct aio_done d = {.f = stat_done, .args = init};
const unsigned retries = init->retries;
@@ -99,6 +121,7 @@ static int retry(struct init *const init)
{
.aio = aio,
.retries = retries,
+ .path_i = path_i,
.last_retry = {.tv_sec = (time_t)-1}
};