This is a text-only version of the following page on https://raymii.org: --- Title : Bare Metal Vi, boot into Vi without an OS! Author : Remy van Elst Date : 22-04-2023 22:30 URL : https://raymii.org/s/blog/Bare_Metal_Boot_to_Vi.html Format : Markdown/HTML --- This guide shows you how to run `Vi` without an operating system, bare metal. This is a follow up on my article from 2014 where I made a custom linux distro that would [Boot to Vim, VIM as PID 1](https://raymii.org/s/blog/Vim_as_PID_1_Boot_to_Vim.html). This time we go further, we boot into `Vi` without an operating system. This is made possible by [Cosmopolitan](https://justine.lol/cosmopolitan/index.html), a `libc` that outputs a POSIX-approved polyglot format that runs natively on Linux + Mac + Windows + FreeBSD + OpenBSD + NetBSD + BIOS with the best possible performance and the tiniest footprint imaginable. Here is a screenshot of bare metal `Vi`: ![bare metal vi][5]
The article [Vim as PID 1, Boot to Vim][1] recently hit the front page of HackerNews and [one of the commenters stated][3] the following: > I bet it wouldn't be too extremely hard to port Vim to [cosmopolitan][2] and run it without an OS (BIOS mode in cosmopolitan). (by `etaioinshrdlu` ) Turns out, that's mostly possible. Not actual Vim, but a minimal Vi clone compiles just fine and runs without any OS. Super cool, fast startup time, the only thing it lacks is filesystem support, so you cannot save or load your files. But hey, who needs that? I'm compiling a small Vi clone, [viless][4], which is the BusyBox Vi clone but patched to run without BusyBox, and a few patches by me to make it compile inside cosmopolitan. ### Compiling Vi with Cosmopolitan These instructions are based on the [instructions on the cosmopolitan source][2]. I'm running them on a Debian 11 system, but Ubuntu should work too. Make sure you can compile C code and have `git`: apt install build-essential git unzip Clone the `Vi` code: git clone https://github.com/RaymiiOrg/viless Test if you can compile it: cd viless make You should have an executable `vi` binary in the current folder. Try to run it: ./vi Exit with `:q!`. If that all worked you can continue with the cosmopolitan part. Download the required files: wget https://justine.lol/cosmopolitan/cosmopolitan-amalgamation-2.2.zip Unzip cosmopolitan: unzip cosmopolitan-amalgamation-2.2.zip Compile `Vi` with the cosmopolitan runtime: gcc -g -Os -static -nostdlib -nostdinc -fno-pie -no-pie -mno-red-zone \ -fno-omit-frame-pointer -pg -mnop-mcount -mno-tls-direct-seg-refs \ -gdwarf-4 \ -o vi.com.dbg \ vi.c \ -fuse-ld=bfd -Wl,-T,ape.lds -Wl,--gc-sections \ -include cosmopolitan.h crt.o ape-no-modify-self.o cosmopolitan.a Use `objcopy` to make the actual binary you can run: objcopy -S -O binary vi.com.dbg vi.com The file `vi.com` should be executable on your linux system, but also on all the platforms stated above. ### Run Vi in QEMU On the same Ubuntu machine, make sure you've installed `qemu`: sudo apt install qemu-system-x86 Then execute the following command to boot your freshly created `vi.com` disk(?): qemu-system-x86_64 -m 16 -nographic -drive file=vi.com,format=raw,index=0,media=disk This will launch you into `Vi`. Exit with `CTRL+A, X` (to exit QEMU). If you want to have a separate window as I did in the screenshots, remove the `-nographic` option and in the window that pops up, select the `View` menu, then choose `serial0`. ![bare metal vi][5] I currently only have [a Raspberry Pi 4][6] as my main desktop so I cannot try booting from a floppy or USB drive on real hardware. If you can, please let me know if it works. You will probably not see anything on the screen but on the serial port. Update: I've loaned an x86 laptop and the `vi.com` file runs when written to disk using `dd`: # replace /dev/sdX with your USB device dd if=vi.com of=/dev/sdX conv=notrunc The [vi.com][7] file I compiled is available [for download][7]. You could try to compile the actual `Vim` and see if that works. If you do, please let me know about it! ### Patches for viless These are the changes I made to [viless][4] to make it compile with Cosmopolitan: From 96aa85f886249ad7fd097a413c28ca4771c0326e Mon Sep 17 00:00:00 2001 From: Remy van Elst