From 4b4d6f42ca92edfdefadcc997a9b70c3d589e320 Mon Sep 17 00:00:00 2001 From: Nikita Shubin Date: Thu, 13 Mar 2025 11:59:31 +0300 Subject: [PATCH] Add Makefile and stuff to build and operate Signed-off-by: Nikita Shubin --- Makefile | 264 +++++++++++++++++++++++++++++ configs/aspeed/buildroot_defconfig | 50 ++++++ configs/x86_64/buildroot_defconfig | 1 + etc/inittab | 41 +++++ etc/qemu/bridge.conf | 14 ++ scripts/init | 17 ++ scripts/loginroot | 2 + tools/cleansim.sh | 8 + tools/gpiosim.sh | 27 +++ tools/qemu-aspeed.sh | 3 + 10 files changed, 427 insertions(+) create mode 100644 Makefile create mode 100644 configs/aspeed/buildroot_defconfig create mode 100644 configs/x86_64/buildroot_defconfig create mode 100644 etc/inittab create mode 100644 etc/qemu/bridge.conf create mode 100755 scripts/init create mode 100755 scripts/loginroot create mode 100755 tools/cleansim.sh create mode 100755 tools/gpiosim.sh create mode 100755 tools/qemu-aspeed.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fc07582 --- /dev/null +++ b/Makefile @@ -0,0 +1,264 @@ +# -*- GNUMakefile -*- +# Requirements: +# /bin/bash as SHELL + +export SHELL = /bin/bash + +ifndef PARALLEL +ifndef NOPARALLEL +PARALLEL := -j$(shell echo $$((`nproc` + 2))) +endif +endif + +KERNEL_TREE ?= ${CURDIR}/linux +SYSROOT ?= ${CURDIR}/initramfs + +${SYSROOT}: + mkdir -p $@ + +${SYSROOT}/.mount-stamp: | ${SYSROOT} + touch $@ + +${SYSROOT_x86_64}: + mkdir -p $@ + +${SYSROOT_x86_64}/.mount-stamp: | ${SYSROOT_x86_64} + touch $@ + +.PHONY: world + +all: world + +world: \ + .build-linux \ + .initramfs \ + build-image/initramfs.cpio.xz \ + .guse + +# --- qemu + +build-qemu: + mkdir -p $@ + +build-qemu/Makefile: | build-qemu + (cd build-qemu && \ + PKG_CONFIG_PATH="${CURDIR}/tmp/lib64/pkgconfig/" \ + ../qemu/configure \ + --target-list="x86_64-softmmu,arm-softmmu" \ + --disable-rdma \ + --enable-fuse \ + --enable-debug \ + --cc=clang --cxx=clang++ \ + $(TSAN) \ + $(CFLAGS)) + +build-qemu/qemu-system-arm build-qemu/qemu-system-x86_64 build-qemu/qemu-bridge-helper&: qemu | build-qemu/Makefile + make -C build-qemu ${PARALLEL} -s + +build-qemu/qemu-bundle/usr/local/etc/qemu: + mkdir -p $@ + +build-qemu/qemu-bundle/usr/local/etc/qemu/bridge.conf: etc/qemu/bridge.conf build-qemu/qemu-bundle/usr/local/etc/qemu + cp etc/qemu/bridge.conf $@ + +.bridge-helper: build-qemu/qemu-bridge-helper build-qemu/qemu-bundle/usr/local/etc/qemu/bridge.conf + sudo setcap cap_net_admin+ep build-qemu/qemu-bridge-helper + +.PHONY: .build-qemu .bridge-helper + +.build-qemu: build-qemu/qemu-system-arm + +.build-qemu-asan: CFLAGS += --extra-cflags="-fsanitize=address -fsanitize=leak" +.build-qemu-asan: .build-qemu + +.build-qemu-tsan: CFLAGS += --extra-cflags="-fsanitize-blacklist=${CURDIR}/qemu/tests/tsan/blacklist.tsan" +.build-qemu-tsan: TSAN += --enable-tsan +.build-qemu-tsan: .build-qemu + +clean:: + -make -C build-qemu clean + +distclean:: + -rm -rf build-qemu + + +# --- kernel + +build-linux/arch/x86/configs: + mkdir -p $@ + +build-linux/arch/x86/configs/linux-qemu_x86_64_defconfig: configs/linux-qemu_x86_64_defconfig | build-linux/arch/x86/configs + cp $< $@ + +build-linux/.config: build-linux/arch/x86/configs/linux-qemu_x86_64_defconfig + make -C ${KERNEL_TREE} O=${CURDIR}/build-linux linux-qemu_x86_64_defconfig -s + +build-linux/defconfig: build-linux/.config + make -C build-linux savedefconfig + +build-linux/arch/x86_64/boot/bzImage: linux build-linux/.config + make ${PARALLEL} -C build-linux V=1 -s + make -C build-linux scripts_gdb + +${SYSROOT}/lib/modules: build-linux/arch/x86_64/boot/bzImage linux build-linux + make ${PARALLEL} -s -C build-linux INSTALL_MOD_PATH=${SYSROOT} INSTALL_MOD_STRIP=1 modules_install + +${CURDIR}/overlay/qemu/merged/lib/modules:: build-linux/arch/x86_64/boot/bzImage linux build-linux + make ${PARALLEL} -s -C build-linux INSTALL_MOD_PATH=${CURDIR}/overlay/qemu/merged/ modules_install + +.PHONY: .build-linux .install-modules + +.build-linux: build-linux/arch/x86_64/boot/bzImage + +.install-modules:: ${SYSROOT}/lib/modules ${CURDIR}/overlay/qemu/merged/lib/modules + +distclean:: + -rm -rf build-linux + +# --- guse + +guse/guse.ko: build-linux/arch/x86_64/boot/bzImage + make -C guse KDIR=${CURDIR}/build-linux + +# ${CURDIR}/lib/modules/6.14.0-rc3+/updates/guse.ko + +.PHONY: .install-guse + +.install-guse: guse/guse.ko + make -C guse KDIR=${CURDIR}/build-linux INSTALL_MOD_PATH=${SYSROOT} modules_install + make -C guse KDIR=${CURDIR}/build-linux INSTALL_MOD_PATH=${CURDIR}/overlay/qemu/merged/ modules_install + +.install-modules:: .install-guse + +# --- libfuse + +libfuse/build: + mkdir -p libfuse/build + +libfuse/build/build.ninja: libfuse/build + (cd libfuse/build \ + && meson setup .. \ + && meson configure --prefix=${CURDIR}/tmp -Ddefault_library=static) + +libfuse/build/example/guse: libfuse | libfuse/build/build.ninja + (cd libfuse/build && ninja) + +${CURDIR}/overlay/qemu/merged/root/guse: libfuse/build/example/guse + cp $< $@ + +.install-example: ${CURDIR}/overlay/qemu/merged/root/guse + +# --- buildroot aspeed + +.PHONY: .build-buildroot-aspeed + +buildroot_aspeed: | buildroot + mkdir -p $@ + +buildroot_aspeed/.config: configs/aspeed/buildroot_defconfig | buildroot + make -C buildroot O=../buildroot_aspeed aspeed_ast2600evb_defconfig + +buildroot_aspeed/images/rootfs.tar: buildroot buildroot_aspeed/.config + make -C buildroot_aspeed ${PARALLEL} -s + +.build-buildroot-aspeed: buildroot_aspeed/images/rootfs.tar + +clean:: + -rm -rf buildroot_aspeed/images/rootfs.tar + +# --- buildroot x86_64 + +.PHONY: .build-buildroot-x86-64 + +buildroot/configs/x86_64_guse_defconfig: configs/x86_64/buildroot_defconfig + cp $< $@ + +buildroot_x86_64: + mkdir -p $@ + +buildroot_x86_64/.config: buildroot/configs/x86_64_guse_defconfig | buildroot_x86_64 + make -C buildroot O=../buildroot_x86_64 x86_64_guse_defconfig + +buildroot_x86_64/images/rootfs.tar: buildroot_x86_64 buildroot_x86_64/.config build-linux/defconfig + make -C buildroot_x86_64 ${PARALLEL} -s + +.build-buildroot-x86-64: buildroot_x86_64/images/rootfs.tar + +clean:: + -rm -rf buildroot_x86_64/images/rootfs.tar + +# --- initramfs + +.PHONY: .initramfs + +${SYSROOT}/init: scripts/init | ${SYSROOT} + install -m 755 $< $@ + +${SYSROOT}/loginroot: scripts/loginroot | ${SYSROOT} + install -m 755 $< $@ + +${SYSROOT}/lib/modules: build-linux/arch/x86_64/boot/bzImage linux build-linux + make ${PARALLEL} -s -C build-linux INSTALL_MOD_PATH=${SYSROOT} INSTALL_MOD_STRIP=1 modules_install + +.initramfs: buildroot_x86_64/images/rootfs.tar | ${SYSROOT}/init ${SYSROOT}/loginroot ${SYSROOT}/lib/modules ${SYSROOT}/.mount-stamp + tar xf $< -C ${SYSROOT} + cp etc/inittab ${SYSROOT}/etc/ + cp -r scripts/root ${SYSROOT} + +# --- initrd image + +.PHONY: build-image-clean + +build-image: + mkdir -p $@ + +build-image-clean: ${SYSROOT} | build-image .initramfs + rm -rf build-image/rootfs + mkdir build-image/rootfs + (cd ${SYSROOT} && tar cf - . ) | (cd build-image/rootfs; tar xf - ) + rm -rf build-image/rootfs/usr/{include,share/doc,share/info} + rm -rf build-image/rootfs/var/db/* + rm -rf build-image/rootfs/var/lib/* + rm -rf build-image/rootfs/var/log/* + rm -rf build-image/rootfs/var/run/* + rm -rf build-image/rootfs/usr/share/man + rm -rf build-image/rootfs/usr/local/share/man + rm -rf build-image/rootfs/usr/share/pkgconfig build-image/rootfs/usr/lib/pkgconfig + rm -rf build-image/rootfs/usr/share/locale/{el,ko,sl,be,eo,bs,nb,pt,tr,ro,uk,it,hu,sv,id,kk,es,zh_CN,da,de,vi,pt_BR,nl,en_GB,ia,sq,sr,af,ru,pl,cs,tl,dz,sk,ja,nn,he,fr,zh_TW,km,gl,fi,eu,ga,lt,hr,bg,rw,ca,et,ne} + rm -rf build-image/rootfs/etc/ssl/man + find build-image/rootfs \( -name "*.a" -o -name "*.la" -o -name "*.o" \) -exec rm -rf {} \; + rm -f build-image/rootfs/usr/bin/{strings,strip,ranlib,readelf,objdump,objcopy,nm,ld.gold,ld.bfd,ld} + rm -f build-image/rootfs/usr/bin/{gdb,gdbserver,gprof,flex++,flex,c++filt,as,ar,addr2line} + rm -f build-image/rootfs/usr/bin/{strace,strace-graph,strace-log-merge} + rm -f build-image/rootfs/bin/udevadm + rm -f build-image/rootfs/sbin/{udevd,udevadm} + rm -rf build-image/rootfs/etc/udev + rm -rf build-image/rootfs/lib/udev + find build-image/rootfs -path build-image/rootfs/lib/modules -prune -o -type f -print | while read f; do file $$f | grep -q 'ELF 32-bit MSB' && { ${TARGET_CROSS_PREFIX}-strip -s -p $$f || true; } || true; done + +build-image/initramfs.cpio.xz: build-image-clean + (cd build-image/rootfs && find . -print0 | cpio --null -ov --format=newc | xz -C crc32 > ../initramfs.cpio.xz) + +clean:: + -rm -rf build-image/initramfs.cpio.xz + +distclean:: + -rm -rf ${SYSROOT} + +# --- overlay + +.PHONY: .overlayfs + +overlay/qemu/merged: + mkdir -p $@ + +overlay/qemu/upper: + mkdir -p $@ + +overlay/qemu/work: + mkdir -p $@ + +.overlayfs: \ + overlay/qemu/merged \ + overlay/qemu/upper \ + overlay/qemu/work diff --git a/configs/aspeed/buildroot_defconfig b/configs/aspeed/buildroot_defconfig new file mode 100644 index 0000000..d25c62e --- /dev/null +++ b/configs/aspeed/buildroot_defconfig @@ -0,0 +1,50 @@ +# +# Automatically generated make config: don't edit +# Mon Feb 18 09:11:56 2008 +# +BR2_HAVE_DOT_CONFIG=y +# BR2_alpha is not set +# BR2_arm is not set +# BR2_armeb is not set +# BR2_cris is not set +# BR2_ia64 is not set +BR2_i386=y +# BR2_m68k is not set +# BR2_mips is not set +# BR2_mipsel is not set +# BR2_powerpc is not set +# BR2_sh is not set +# BR2_sparc is not set +# BR2_sparc64 is not set +# BR2_x86_64 is not set +# BR2_x86_i486 is not set +# BR2_x86_i586 is not set +BR2_x86_i686=y +# BR2_x86_pentiumpro is not set +# BR2_x86_pentium_mmx is not set +# BR2_x86_pentium_m is not set +# BR2_x86_pentium2 is not set +# BR2_x86_pentium3 is not set +# BR2_x86_pentium4 is not set +# BR2_x86_prescott is not set +# BR2_x86_nocona is not set +# BR2_x86_core2 is not set +# BR2_x86_k6 is not set +# BR2_x86_k6_2 is not set +# BR2_x86_athlon is not set +# BR2_x86_athlon_4 is not set +# BR2_x86_opteron is not set +# BR2_x86_opteron_sse3 is not set +# BR2_x86_barcelona is not set +# BR2_x86_geode is not set +# BR2_x86_c3 is not set +# BR2_x86_winchip_c6 is not set +# BR2_x86_winchip2 is not set +BR2_ARCH="i686" +BR2_ENDIAN="LITTLE" +BR2_GCC_TARGET_TUNE="i686" +BR2_GCC_TARGET_ARCH="i686" + +# +# Target options +# diff --git a/configs/x86_64/buildroot_defconfig b/configs/x86_64/buildroot_defconfig new file mode 100644 index 0000000..1d303e2 --- /dev/null +++ b/configs/x86_64/buildroot_defconfig @@ -0,0 +1 @@ +BR2_x86_i686=y diff --git a/etc/inittab b/etc/inittab new file mode 100644 index 0000000..a5b2140 --- /dev/null +++ b/etc/inittab @@ -0,0 +1,41 @@ +# /etc/inittab +# +# Copyright (C) 2001 Erik Andersen +# +# Note: BusyBox init doesn't support runlevels. The runlevels field is +# completely ignored by BusyBox init. If you want runlevels, use +# sysvinit. +# +# Format for each entry: ::: +# +# id == tty to run on, or empty for /dev/console +# runlevels == ignored +# action == one of sysinit, respawn, askfirst, wait, and once +# process == program to run + +# Startup the system +::sysinit:/bin/mount -t proc proc /proc +::sysinit:/bin/mount -o remount,rw / +::sysinit:/bin/mkdir -p /dev/pts /dev/shm +::sysinit:/bin/mount -a +::sysinit:/bin/mkdir -p /run/lock/subsys +::sysinit:/sbin/swapon -a +null::sysinit:/bin/ln -sf /proc/self/fd /dev/fd +null::sysinit:/bin/ln -sf /proc/self/fd/0 /dev/stdin +null::sysinit:/bin/ln -sf /proc/self/fd/1 /dev/stdout +null::sysinit:/bin/ln -sf /proc/self/fd/2 /dev/stderr +::sysinit:/bin/hostname -F /etc/hostname +# now run any rc scripts +::sysinit:/etc/init.d/rcS + +# Put a getty on the serial port +::respawn:/sbin/getty -n -L -l /loginroot 115200 ttyS0 vt100 +#console::respawn:/sbin/getty -L -l /loginroot console 0 vt100 # GENERIC_SERIAL + +# Stuff to do for the 3-finger salute +#::ctrlaltdel:/sbin/reboot + +# Stuff to do before rebooting +::shutdown:/etc/init.d/rcK +::shutdown:/sbin/swapoff -a +::shutdown:/bin/umount -a -r diff --git a/etc/qemu/bridge.conf b/etc/qemu/bridge.conf new file mode 100644 index 0000000..5897ae3 --- /dev/null +++ b/etc/qemu/bridge.conf @@ -0,0 +1,14 @@ +# This should have the following permissions: root:qemu 0640 + +allow br0 +# Uncommenting the above would allow users in the 'qemu' group +# to add devices to 'br0' + +# allow virbr0 +# Uncommenting the above would allow users in the 'qemu' group +# to add devices to 'virbr0' + +# include /etc/qemu/bob.conf +# Uncommenting the above would allow users in the 'bob' group +# to have permissions defined in it, iff it has the following +# permissions: root:bob 0640 diff --git a/scripts/init b/scripts/init new file mode 100755 index 0000000..8dd8dd3 --- /dev/null +++ b/scripts/init @@ -0,0 +1,17 @@ +#!/bin/sh +# devtmpfs does not get automounted for initramfs +/bin/mount -t devtmpfs devtmpfs /dev + +# use the /dev/console device node from devtmpfs if possible to not +# confuse glibc's ttyname_r(). +# This may fail (E.G. booted with console=), and errors from exec will +# terminate the shell, so use a subshell for the test +if (exec 0/dev/null; then + exec 0/dev/console + exec 2>/dev/console +fi + +export PS1="qemu-irqs # " + +exec /sbin/init "$@" diff --git a/scripts/loginroot b/scripts/loginroot new file mode 100755 index 0000000..86e766c --- /dev/null +++ b/scripts/loginroot @@ -0,0 +1,2 @@ +#!/bin/sh +exec /bin/login -p -f root diff --git a/tools/cleansim.sh b/tools/cleansim.sh new file mode 100755 index 0000000..a15c8f1 --- /dev/null +++ b/tools/cleansim.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +echo 0 > /sys/kernel/config/gpio-sim/basic/live +SIMDIR="/sys/kernel/config/gpio-sim" +find $SIMDIR -type d -name hog -exec rmdir '{}' '+' +find $SIMDIR -type d -name "line*" -exec rmdir '{}' '+' +find $SIMDIR -type d -name "bank*" -exec rmdir '{}' '+' +rmdir $SIMDIR/* \ No newline at end of file diff --git a/tools/gpiosim.sh b/tools/gpiosim.sh new file mode 100755 index 0000000..fde7c73 --- /dev/null +++ b/tools/gpiosim.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +mountpoint /sys/kernel/config > /dev/null || mount -t configfs configfs /sys/kernel/config +modprobe gpio-sim +chown -R $USER /sys/kernel/config/gpio-sim +mkdir /sys/kernel/config/gpio-sim/basic +mkdir /sys/kernel/config/gpio-sim/basic/bank0 +echo "test" > /sys/kernel/config/gpio-sim/basic/bank0/label +echo 8 > /sys/kernel/config/gpio-sim/basic/bank0/num_lines +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line0 +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line0/hog +echo "output-low" > /sys/kernel/config/gpio-sim/basic/bank0/line0/hog/direction +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line1 +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line1/hog +echo "output-low" > /sys/kernel/config/gpio-sim/basic/bank0/line1/hog/direction +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line2 +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line2/hog +echo "output-low" > /sys/kernel/config/gpio-sim/basic/bank0/line2/hog/direction +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line3 +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line3/hog +echo "output-low" > /sys/kernel/config/gpio-sim/basic/bank0/line3/hog/direction +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line4 +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line5 +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line6 +mkdir /sys/kernel/config/gpio-sim/basic/bank0/line7 +echo 1 > /sys/kernel/config/gpio-sim/basic/live + diff --git a/tools/qemu-aspeed.sh b/tools/qemu-aspeed.sh new file mode 100755 index 0000000..9db4348 --- /dev/null +++ b/tools/qemu-aspeed.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +build-qemu/qemu-system-arm -M ast2600-evb,bmc-console=uart5 -kernel buildroot/output/build/linux-6.6.14/arch/arm/boot/zImage -dtb buildroot/output/build/linux-6.6.14/arch/arm/boot/dts/aspeed/aspeed-ast2600-evb.dtb -initrd buildroot/output/images/rootfs.cpio -nographic -serial mon:stdio -gpiodev chardev,id=aspeed-gpio0,chardev=gpio0 -chardev socket,path=/tmp/gpio0,id=gpio0,server=on,wait=off -- 2.30.2