My silly linux distro, made for RISC-V
After reading the great blogs from Uros Popovic about RISC-V baremetal programming, the boot process, and making a micro linux distro, I had an epiphany and decided to port the old bash that I had programmed in 2021 for an OS course, to use as the init process.
I had a big problem with this idea at first, there were a few modules form the C project that were precompiled binaries, I thought of contacting my old professors to check if they still had the source to be able to compile it, but something stroke me and had the great idea to try and decompile the binaries. In this journey I've learned of the existence of Decompiler Explorer, and decided to go for the route of learning how to use Ghidra.
How to decompile
The trick was compiling the whole binary for mybash with the debugging symbols and opening this on Ghidra to have the most amount of hints available for the functions implemented. Then with the code explorer started guessing the struct sizes and the arguments of the functions, based on the calls to calloc and on the names of the functions.
The finale?
After having the source, or better said something close to it, I found another problem, I had to compile the binaries statically because my distro is so minimal that I didn't even wanted to include glibc GNU's C standard library, this ended up being kind of a pain because some part of mybash where also implemented using glib so after trying a bunch of stuff I solve it doing the following.
I was mainly trying to compile with musl (another C library implementation), but I also needed glib compiled staticly. So I opened a qemu-system-riscv64 Ubuntu VM, and used podman (a Docker replacement) to be able to use alpine (I really couldn't manage to find or compile a version of alpine to use directly with QEMU), and then finally compile everything with musl.
~/install/master-qemu/bin/qemu-system-riscv64 \
-machine virt -nographic -m 2048 -smp 4 \
-kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \
-device virtio-net-device,netdev=eth0 -netdev user,id=eth0,hostfwd=tcp::5555-:22 \
-device virtio-rng-pci \
-drive file=./ubuntu-24.04.3-preinstalled-server-riscv64.img,format=raw,if=virtio
FROM alpine:3.22
RUN apk update
RUN apk add bash git gcc make musl-dev glib-dev
RUN apk add glib-static
podman build -t alpine_image .
podman run -it -v /home/ubuntu/my_bash/:/home alpine_image
Seeing the beatiful creation
I put mybash in /init and /bin/init, sadly had a tiny bug due to not even having a single user. So after creating /etc/passwd I finally managed to boot into mybash with the beautiful theme frutilla.
[ 0.393041] ALSA device list: [ 0.393130] No soundcards found. [ 0.426294] Freeing unused kernel image (initmem) memory: 2308K [ 0.426971] Run /init as init process Hello form the original init: waiting for child ... root:/> echo hola al blog hola al blog root:/>