diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8f0d69d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +p.vim +result +bin/ +build/ diff --git a/Makefile b/Makefile index 76f8e84..d4ff47a 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,13 @@ PCH_RELEASE := $(PCH_DIR_RELEASE)/pch.h.gch MRUBY_CFLAGS ?= -I./libs/mruby/build/host/include MRUBY_LIBS ?= -L./libs/mruby/build/host/lib -lmruby +BUSYBOX ?= $(shell which busybox) + +INITRAMFS_DIR_DEBUG := $(OBJ_DIR)/initramfs-dbg +INITRAMFS_ARCHIVE_DEBUG := $(BIN_DIR)/initramfs-dbg.cpio.gz +INITRAMFS_DIR_RELEASE := $(OBJ_DIR)/initramfs +INITRAMFS_ARCHIVE_RELEASE := $(BIN_DIR)/initramfs.cpio.gz + CFLAGS_DEBUG :=\ -std=c++23 -Wall -Wextra -fno-rtti \ -Og -fno-inline -gsplit-dwarf -DDEBUG \ @@ -46,9 +53,31 @@ DEP_RELEASE := $(OBJ_RELEASE:.o=.d) all: release -debug: $(TARGET_DEBUG) +debug: $(INITRAMFS_ARCHIVE_DEBUG) -release: $(TARGET_RELEASE) +release: $(INITRAMFS_ARCHIVE_RELEASE) + +$(INITRAMFS_ARCHIVE_DEBUG): $(TARGET_DEBUG) + @set -eu; \ + root="$(INITRAMFS_DIR_DEBUG)"; \ + mkdir -p "$$root/bin" "$$root/dev"; \ + install -m 0755 "$(TARGET_DEBUG)" "$$root/init"; \ + install -m 0755 "$(BUSYBOX)" "$$root/bin/busybox"; \ + ln -sfn busybox "$$root/bin/sh"; \ + cd "$$root"; \ + find . -print0 | cpio --null -o --format=newc | gzip -9 > "$(abspath $(INITRAMFS_ARCHIVE_DEBUG))"; \ + echo "Initramfs generated: $(INITRAMFS_ARCHIVE_DEBUG)" + +$(INITRAMFS_ARCHIVE_RELEASE): $(TARGET_RELEASE) + @set -eu; \ + root="$(INITRAMFS_DIR_RELEASE)"; \ + mkdir -p "$$root/bin" "$$root/dev"; \ + install -m 0755 "$(TARGET_RELEASE)" "$$root/init"; \ + install -m 0755 "$(BUSYBOX)" "$$root/bin/busybox"; \ + ln -sfn busybox "$$root/bin/sh"; \ + cd "$$root"; \ + find . -print0 | cpio --null -o --format=newc | gzip -9 > "$(abspath $(INITRAMFS_ARCHIVE_RELEASE))"; \ + echo "Initramfs generated: $(INITRAMFS_ARCHIVE_RELEASE)" $(PCH_DEBUG): $(INCLUDE_DIR)/pch.h @mkdir -p $(PCH_DIR_DEBUG) diff --git a/src/init/init.cc b/src/init/init.cc index 5567143..808e244 100644 --- a/src/init/init.cc +++ b/src/init/init.cc @@ -1,3 +1,9 @@ +#include +#include +#include +#include +#include +#include #include #include #include @@ -5,38 +11,39 @@ #include #include -int main() -{ - // PID 1 +int main() { + // PID 1 - int console = open("/dev/console", O_RDWR); + if (mount("devtmpfs", "/dev", "devtmpfs", 0, nullptr) < 0) { + perror("mount /dev"); + return 1; + } - if (console < 0) - { - perror("open /dev/console"); - return 1; - } + int console = open("/dev/console", O_RDWR); - dup2(console, STDIN_FILENO); - dup2(console, STDOUT_FILENO); - dup2(console, STDERR_FILENO); + if (console < 0) { + perror("open /dev/console"); + return 1; + } - close(console); + dup2(console, STDIN_FILENO); + dup2(console, STDOUT_FILENO); + dup2(console, STDERR_FILENO); - while (true) - { - pid_t pid = fork(); + close(console); - if (pid == 0) - { - execl("/bin/sh", "sh", nullptr); - perror("exec"); - _exit(127); - } + pid_t pid = fork(); - if (pid > 0) - { - waitpid(pid, nullptr, 0); - } - } + if (pid == 0) { + execl("/bin/sh", "sh", nullptr); + perror("exec"); + _exit(127); + } + + if (pid > 0) { + waitpid(pid, nullptr, 0); + } + + sync(); + reboot(RB_POWER_OFF); }