Compare commits
3
Commits
73223fb521
...
9705e59f9e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9705e59f9e
|
||
|
|
674c339d37
|
||
|
|
759640f519
|
@@ -0,0 +1,4 @@
|
|||||||
|
p.vim
|
||||||
|
result
|
||||||
|
bin/
|
||||||
|
build/
|
||||||
@@ -18,6 +18,13 @@ PCH_RELEASE := $(PCH_DIR_RELEASE)/pch.h.gch
|
|||||||
MRUBY_CFLAGS ?= -I./libs/mruby/build/host/include
|
MRUBY_CFLAGS ?= -I./libs/mruby/build/host/include
|
||||||
MRUBY_LIBS ?= -L./libs/mruby/build/host/lib -lmruby
|
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 :=\
|
CFLAGS_DEBUG :=\
|
||||||
-std=c++23 -Wall -Wextra -fno-rtti \
|
-std=c++23 -Wall -Wextra -fno-rtti \
|
||||||
-Og -fno-inline -gsplit-dwarf -DDEBUG \
|
-Og -fno-inline -gsplit-dwarf -DDEBUG \
|
||||||
@@ -46,9 +53,31 @@ DEP_RELEASE := $(OBJ_RELEASE:.o=.d)
|
|||||||
|
|
||||||
all: release
|
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
|
$(PCH_DEBUG): $(INCLUDE_DIR)/pch.h
|
||||||
@mkdir -p $(PCH_DIR_DEBUG)
|
@mkdir -p $(PCH_DIR_DEBUG)
|
||||||
|
|||||||
@@ -59,35 +59,64 @@
|
|||||||
gnumake
|
gnumake
|
||||||
findutils
|
findutils
|
||||||
binutils
|
binutils
|
||||||
|
cpio
|
||||||
|
gzip
|
||||||
];
|
];
|
||||||
|
|
||||||
buildInputs = [
|
buildInputs = [
|
||||||
|
pkgs.pkgsStatic.busybox
|
||||||
mruby
|
mruby
|
||||||
];
|
];
|
||||||
|
|
||||||
buildPhase = ''
|
buildPhase = ''
|
||||||
make \
|
make \
|
||||||
MRUBY_CFLAGS="-I${mruby}/include" \
|
MRUBY_CFLAGS="-I${mruby}/include" \
|
||||||
MRUBY_LIBS="-L${mruby}/lib -lmruby"
|
MRUBY_LIBS="-L${mruby}/lib -lmruby" \
|
||||||
|
BUSYBOX="${pkgs.pkgsStatic.busybox}/bin/busybox"
|
||||||
'';
|
'';
|
||||||
|
|
||||||
installPhase = ''
|
installPhase = ''
|
||||||
mkdir -p $out/bin
|
mkdir -p $out/bin
|
||||||
cp bin/blade $out/bin/blade
|
cp bin/blade $out/bin/blade
|
||||||
|
cp bin/initramfs.cpio.gz $out/bin/initramfs.cpio.gz
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
run-blade = hostPkgs.writeShellScriptBin "run-blade" ''
|
||||||
|
set -euo pipefail
|
||||||
|
exec ${hostPkgs.qemu}/bin/qemu-system-x86_64 \
|
||||||
|
--enable-kvm \
|
||||||
|
-m 512M \
|
||||||
|
-smp 2 \
|
||||||
|
-kernel "${hostPkgs.linux}/bzImage" \
|
||||||
|
-initrd ${blade}/bin/initramfs.cpio.gz \
|
||||||
|
-append "console=ttyS0 rdinit=/init" \
|
||||||
|
-nographic
|
||||||
|
'';
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
packages.${system} = {
|
packages.${system} = {
|
||||||
default = blade;
|
default = blade;
|
||||||
blade = blade;
|
blade = blade;
|
||||||
mruby = mruby;
|
mruby = mruby;
|
||||||
|
run-blade = run-blade;
|
||||||
};
|
};
|
||||||
|
|
||||||
devShells.${system}.default = hostPkgs.mkShell {
|
apps.${system} = {
|
||||||
|
default = {
|
||||||
|
type = "app";
|
||||||
|
program = "${run-blade}/bin/run-blade";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
devShells.${system}.default = pkgs.mkShell {
|
||||||
inputsFrom = [ blade ];
|
inputsFrom = [ blade ];
|
||||||
|
|
||||||
packages = with hostPkgs; [
|
packages = with hostPkgs; [
|
||||||
|
linux
|
||||||
|
qemu
|
||||||
|
cpio
|
||||||
|
gzip
|
||||||
clang-tools
|
clang-tools
|
||||||
pkg-config
|
pkg-config
|
||||||
ccache
|
ccache
|
||||||
@@ -105,6 +134,8 @@
|
|||||||
|
|
||||||
export MRUBY_CFLAGS=-I${mruby}/include
|
export MRUBY_CFLAGS=-I${mruby}/include
|
||||||
export MRUBY_LIBS="-L${mruby}/lib -lmruby"
|
export MRUBY_LIBS="-L${mruby}/lib -lmruby"
|
||||||
|
export BUSYBOX="${pkgs.pkgsStatic.busybox}/bin/busybox"
|
||||||
|
export LINUX="${hostPkgs.linux}/bzImage"
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
+18
-11
@@ -1,3 +1,9 @@
|
|||||||
|
#include <sys/reboot.h>
|
||||||
|
#include <linux/reboot.h>
|
||||||
|
#include <sys/mount.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/sysmacros.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -5,14 +11,17 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
// PID 1
|
// PID 1
|
||||||
|
|
||||||
|
if (mount("devtmpfs", "/dev", "devtmpfs", 0, nullptr) < 0) {
|
||||||
|
perror("mount /dev");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int console = open("/dev/console", O_RDWR);
|
int console = open("/dev/console", O_RDWR);
|
||||||
|
|
||||||
if (console < 0)
|
if (console < 0) {
|
||||||
{
|
|
||||||
perror("open /dev/console");
|
perror("open /dev/console");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -23,20 +32,18 @@ int main()
|
|||||||
|
|
||||||
close(console);
|
close(console);
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
if (pid == 0)
|
if (pid == 0) {
|
||||||
{
|
|
||||||
execl("/bin/sh", "sh", nullptr);
|
execl("/bin/sh", "sh", nullptr);
|
||||||
perror("exec");
|
perror("exec");
|
||||||
_exit(127);
|
_exit(127);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pid > 0)
|
if (pid > 0) {
|
||||||
{
|
|
||||||
waitpid(pid, nullptr, 0);
|
waitpid(pid, nullptr, 0);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
sync();
|
||||||
|
reboot(RB_POWER_OFF);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user