summaryrefslogtreecommitdiff
path: root/src/init/ps1
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-07-07 13:22:53 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-07-25 14:16:41 +0200
commit14f60e4fd65c42f126eaee7e09cb4251c167c6ed (patch)
tree313b5e16d7d99cf1518c953e2efe5e5fc920dfbf /src/init/ps1
parent48a61c16eaa6dcfc75d00dba302537ce1492db98 (diff)
downloadwnix-tty.tar.gz
wiptty
Diffstat (limited to 'src/init/ps1')
-rw-r--r--src/init/ps1/CMakeLists.txt33
-rw-r--r--src/init/ps1/private_include/init/ps1/types.h37
-rw-r--r--src/init/ps1/src/CMakeLists.txt22
-rw-r--r--src/init/ps1/src/boot.c188
-rw-r--r--src/init/ps1/src/globalvars.c21
-rw-r--r--src/init/ps1/src/run.c27
-rw-r--r--src/init/ps1/src/time.c47
7 files changed, 375 insertions, 0 deletions
diff --git a/src/init/ps1/CMakeLists.txt b/src/init/ps1/CMakeLists.txt
new file mode 100644
index 0000000..2cdc2de
--- /dev/null
+++ b/src/init/ps1/CMakeLists.txt
@@ -0,0 +1,33 @@
+# wanix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+add_library(init_ps1)
+add_subdirectory(src)
+target_include_directories(init_ps1 PRIVATE private_include)
+target_link_libraries(init_ps1
+ PUBLIC
+ c
+ init
+ PRIVATE
+ aio
+ drv_ps1_bios
+ drv_ps1_rcnt
+ drv_ps1_time
+ iso9660
+ kprintf
+ loop
+ state
+)
diff --git a/src/init/ps1/private_include/init/ps1/types.h b/src/init/ps1/private_include/init/ps1/types.h
new file mode 100644
index 0000000..118f37b
--- /dev/null
+++ b/src/init/ps1/private_include/init/ps1/types.h
@@ -0,0 +1,37 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef INIT_PS1_TYPES_H
+#define INIT_PS1_TYPES_H
+
+#include <aio.h>
+#include <sys/stat.h>
+#include <time.h>
+
+struct init_ps1
+{
+ struct aio *aio;
+ struct stat *sb;
+ unsigned retries;
+ struct timespec last_retry;
+ int (*next)(struct init_ps1 *);
+};
+
+extern struct init_ps1 init_ps1;
+
+#endif
diff --git a/src/init/ps1/src/CMakeLists.txt b/src/init/ps1/src/CMakeLists.txt
new file mode 100644
index 0000000..5a4af14
--- /dev/null
+++ b/src/init/ps1/src/CMakeLists.txt
@@ -0,0 +1,22 @@
+# wanix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+target_sources(init_ps1 PRIVATE
+ boot.c
+ globalvars.c
+ run.c
+ time.c
+)
diff --git a/src/init/ps1/src/boot.c b/src/init/ps1/src/boot.c
new file mode 100644
index 0000000..9b42d47
--- /dev/null
+++ b/src/init/ps1/src/boot.c
@@ -0,0 +1,188 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <init.h>
+#include <init/ps1/types.h>
+#include <aio.h>
+#include <iso9660.h>
+#include <kprintf.h>
+#include <loop.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <state.h>
+#include <time.h>
+
+struct mount
+{
+ struct aio *aio;
+};
+
+enum {RETRIES = 5};
+
+static int retry(struct init_ps1 *);
+
+static int mount_done(const enum state state, void *const args)
+{
+ struct mount *const m = args;
+
+ loop_rm_aio(m->aio);
+ aio_free(m->aio);
+ free(m);
+
+ if (state)
+ kprintf("Failed to mount ISO9660 filesystem\n");
+ else
+ kprintf("ISO996 filesystem mounted successfully\n");
+
+ return 0;
+}
+
+static int mount_cd(struct init_ps1 *const p)
+{
+ struct aio *aio = NULL;
+ struct mount *const m = malloc(sizeof *m);
+
+ if (!m)
+ goto failure;
+
+ const struct aio_mount fm =
+ {
+ .mount =
+ {
+ .src = "/dev/cd0",
+ .tgt = "/mnt/cdrom",
+ .mode = 0755
+ },
+
+ .type = "iso9660"
+ };
+
+ const struct aio_done d =
+ {
+ .args = m,
+ .f = mount_done
+ };
+
+ if (!(aio = aio_mount(&fm, &d)) || loop_add_aio(aio))
+ goto failure;
+
+ *m = (const struct mount){.aio = aio};
+ p->next = NULL;
+ kprintf("Mounting ISO996 filesystem from %s to %s\n",
+ fm.mount.src, fm.mount.tgt);
+ return 0;
+
+failure:
+ aio_free(aio);
+ free(m);
+ kprintf("Failed to mount ISO996 filesystem from %s to %s\n",
+ fm.mount.src, fm.mount.tgt);
+ 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 = 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 = mount_cd;
+ kprintf("Successfully initialized CD-ROM (took %u attempts)\n",
+ RETRIES - p->retries + 1);
+ }
+
+end:
+ loop_rm_aio(p->aio);
+ aio_free(p->aio);
+ free(p->sb);
+ return ret;
+}
+
+static int retry(struct init_ps1 *const p)
+{
+ struct aio *aio = NULL;
+ struct stat *const sb = malloc(sizeof *sb);
+
+ if (!sb)
+ goto failure;
+
+ const struct fs_stat s = {.path = "/dev/cd0", .sb = sb};
+ const struct aio_done d = {.f = stat_done, .args = p};
+ const unsigned retries = p->retries;
+
+ if (!(aio = aio_stat(&s, &d)) || loop_add_aio(aio))
+ goto failure;
+
+ *p = (const struct init_ps1)
+ {
+ .aio = aio,
+ .sb = sb,
+ .retries = retries,
+ .last_retry = {.tv_sec = (time_t)-1}
+ };
+
+ kprintf("Initializing CD-ROM to %s (attempt %u)\n", s.path,
+ RETRIES - p->retries + 1);
+ return 0;
+
+failure:
+ free(aio);
+ free(sb);
+ return -1;
+}
+
+int init_port_boot(void)
+{
+ if (iso9660_register())
+ return -1;
+
+ init_ps1 = (const struct init_ps1)
+ {
+ .retries = RETRIES,
+ .next = retry
+ };
+
+ return 0;
+}
diff --git a/src/init/ps1/src/globalvars.c b/src/init/ps1/src/globalvars.c
new file mode 100644
index 0000000..6d4d2aa
--- /dev/null
+++ b/src/init/ps1/src/globalvars.c
@@ -0,0 +1,21 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <init/ps1/types.h>
+
+struct init_ps1 init_ps1;
diff --git a/src/init/ps1/src/run.c b/src/init/ps1/src/run.c
new file mode 100644
index 0000000..a0d022f
--- /dev/null
+++ b/src/init/ps1/src/run.c
@@ -0,0 +1,27 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <init.h>
+#include <init/ps1/types.h>
+
+int init_port_run(void)
+{
+ struct init_ps1 *const p = &init_ps1;
+
+ return p->next ? p->next(p) : 0;
+}
diff --git a/src/init/ps1/src/time.c b/src/init/ps1/src/time.c
new file mode 100644
index 0000000..72183c0
--- /dev/null
+++ b/src/init/ps1/src/time.c
@@ -0,0 +1,47 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <init.h>
+#include <init/ps1/types.h>
+#include <drv/ps1/bios.h>
+#include <drv/ps1/rcnt.h>
+#include <drv/ps1/time.h>
+#include <time.h>
+
+int init_time(void)
+{
+ struct timespec ts;
+ const union drv_ps1_rcnt_cfg cfg =
+ {
+ .bits =
+ {
+ .reset = 1,
+ .repeat = 1,
+ .irq_tgt = 1,
+ .clocksrc = 3
+ }
+ };
+
+ if (clock_getres(CLOCK_REALTIME, &ts))
+ return -1;
+
+ const unsigned fcpu = 33000000;
+ const uint16_t target = (fcpu / 8) / (ts.tv_nsec / 1000);
+
+ return drv_ps1_rcnt_init(DRV_PS1_RCNT2, target, &cfg, drv_ps1_time_tick);
+}