The Cathedral and the Bizarre,
or
Dual-Booting for Fun and Prophet

Using Linux to Enhance TempleOS

Toby Betts

LinuxFest Northwest, 2024-04-27

Disclaimer

Difficulty Level

Technical:

1/5 multitools: Mastering boot records and boot loaders

Difficulty Level

Booting:

4/5 boots: This talk was made for bootin' and that's just what we'll do

Genesis - In the beginning...

In the beginning God created the heaven and the earth and the earth was without form and void and darkness was upon the face of the deep

Genesis - In the beginning...

Genesis

Genesis

Genesis

Genesis

Genesis

An average disk layout

A basic DOS-era disk layout defines contiguous blocks of storage called partitions
  • Define the partition table with fdisk, gparted
  • Reserve space at beginning to store the Master Boot Record
  • Different partitions are treated as "volumes" in DOS/Windows
  • Different partitions can store different operating systems
https://pikchr.org/home/pikchrshow boxht = .2; boxwid = 1.3; down box "MBR"; box ht 0.7*boxht "safety gap"; box ht 2*boxht "partition 1"; box ht 3*boxht "." "." "." L: box ht 2*boxht "partition 4"; box ht 0.8*boxht "unallocated space"; box invis wid .1*boxwid "0" with .e at 1st box .nw box invis wid .2*boxwid "512" with .e at 2nd box .nw box invis wid .2*boxwid "1M" with .e at 3rd box .nw box invis wid .2*boxwid "1G" with .e at 3rd box .sw box invis wid .2*boxwid "end" with .e at 6th box .sw

An average dual-boot layout

The traditional way to dual-boot
  • Create two partitions
  • Install DOS/Windows to first partition
  • Install Linux to second partition
  • Install Linux-aware boot loader
https://pikchr.org/home/pikchrshow boxht = .2; boxwid = 1.3; down box "MBR"; box ht 0.7*boxht "safety gap"; box ht 3*boxht "DOS/Windows (FAT32)"; L: box ht 2*boxht "Linux (ext2)"; box ht 0.8*boxht "unallocated space"; box invis wid .1*boxwid "0" with .e at 1st box .nw box invis wid .2*boxwid "512" with .e at 2nd box .nw box invis wid .2*boxwid "1M" with .e at 3rd box .nw box invis wid .2*boxwid "1G" with .e at 3rd box .sw

Why Boot Loaders are important

The first 512 bytes of the hard disk are essential

Why Boot Loaders are important

MBR points to the boot loader

Exodus

Boot loaders let you run multiple operating systems. Maybe something:

Exodus

TempleOS is:

Exodus

TempleOS has:

Exodus - Transparent File Compression

Exodus - RedSea

Doc/Strategy.DD, Doc/RedSea.DD

Exodus - POSIX time

Doc/Strategy.DD, Doc/RedSea.DD

Exodus - The TempleOS datetime object

public I64 class CDate
{
  U32 time;
  I32 date;
};

Doc/Strategy.DD, Doc/RedSea.DD

Numbers - Gregorian calendars

Western secular timekeeping is based on the Gregorian calendar.

Numbers - Why is TempleOS?

Vice - God's Lonely Programmer

https://knowyourmeme.com/memes/people/terry-davis-templeos

Numbers - Be not afraid


"My vision is a souped-up C64, not a 1970's mainframe"
— Terry A. Davis

Lamentations - Switches, lights, and knobs

"We've all got our switches, lights, and knobs to deal with, Stryker. I mean down here there are literally hundreds and thousands of blinking, beeping, and flashing lights, blinking and beeping and flashing. They're flashing and they're beeping... I can't stand it anymore, the blinking and beeping and flashing!"

Acts - We can fix the blinking

Find and change $$BK,1$$ to $$BK,0$$ in:

Rebuild the kernel and restart

Acts - TipOfDay();

/Doc/Tips.DD contains helpful hints, print one with TipOfDay;

Lamentations - Problem Statement

TempleOS is a platform for coding, gaming, learning, and exploring, but:

Solution? Add another operating system!

Disk partitioning options

Option #1
Option #2

1 Samuel - No more Davids, only Goliaths

Multiple alternative OSes considered:

Pre-reqs: must be (a) tiny, (b) FAT32-support in core

1 Samuel - Five smooth stones

"And David prevailed over the Philistine, with a sling and a stone"

Tiny Core Linux: http://tinycorelinux.net

Ideal TempleOS dual-boot layout

  • Single FAT32 partition
  • Combines TempleOS files & full Linux install
  • In the same file system
  • OS chosen at boot time

Install TempleOS install (the right way)

  • Boot a Linux live session
  • fdisk /dev/sda
  • mkfs.vfat -F32 /dev/sda1
  • Boot TempleOS ISO
  • Install (#include /Misc/OSInstall.HC)
  • Choose NOT to format C: drive
  • Choose NOT to install TempleOS bootloader
  • Reboot

Install TempleOS install (the right way)

  • Boot a Linux live session
  • Mount hard drive under /mnt/sda1
  • mkdir -p /mnt/sda1/tce/boot
  • copy corepure64.gz & vmlinuz64 to /mnt/sda1/tce/boot
  • Install syslinux bootloader

syslinux - The key to all of this

syslinux: the bootloader you could always use but never have

  • Included by default in most distros
  • cd /usr/lib/syslinux/modules/bios
  • cp menu.c32 libutil.c32 /mnt/sda1
  • vi /mnt/sda1/syslinux.cfg
  • dd if=/usr/lib/syslinux/mbr/mbr.bin \
       of=/dev/sda bs=440 count=1 conv=notrunc
  • syslinux -i /dev/sda1

syslinux.cfg

UI menu.c32
PROMPT 1

MENU TITLE Boot Menu
MENU ROWS 2

LABEL TempleOS
  MENU LABEL TempleOS
  BOOT /Kernel.BIN.C

LABEL tc
  MENU LABEL Tiny Core Linux
  KERNEL /tce/boot/vmlinuz64
  INITRD /tce/boot/corepure64.gz
  APPEND loglevel=3 vga=789 lang=en_US.UTF-8

syslinux - This also works on Windows

  • Install Windows to FAT32 partition
  • dd if=/dev/sda1 of=sda1.bss bs=512 count=1
  • LABEL Win98
              MENU LABEL Windows 98
              BOOT sda1.bss
  • Boot Linux from DOS with LOADLIN.EXE
  • Use Tiny Core v13

Why do this?

Advantages to merging TempleOS with Linux

  • Goldilocks partition table: not too small, not too large
  • No need for intermediary space to transfer files
  • Best of both worlds: use TempleOS & Linux tools at will

But what about data integrity?

Data integrity

All TempleOS data is stored in either RedSea or FAT32.

  • File systems have bugs
  • Bespoke file systems are hard to debug
  • It sure would be nice to know if a file is corrupt before you need it

TempleOS needs a file checksum tool. Linux has plenty!

What is a checksum?

Read every byte of a file & compute a mathematical equation with those bytes. If any of the bytes change, or their order changes, the result of the equation will be different. Example (don't do this):

  • Convert "DOG" to ASCII: 68, 79, 71
  • For each value ai
  • Take the prime numbers P in order, compute Piai
  • Sum each Piai
  • 268 + 379 + 571
  • "DOG" =>
    42,351,647,362,764,286,563,220,907,008,715,940,411,151,060,669,848

Different answers for different inputs

"DOG" => 268 + 379 + 571
42,351,647,362,764,286,563,220,907,008,715,940,411,151,060,669,848

"CAT" => 267 + 365 + 584
51,698,788,284,564,229,679,463,043,264,673,729,808,888,368,337,868,155,321,396

"GOD" => 271 + 379 + 568
338,813,178,950,989,745,432,110,977,071,734,429,657,507,013,340

Some algorithms make reversing output back into input difficult

Checksums in Linux

Multiple checksum utilities

  • MD5: md5sum
  • SHA-1: sha1sum
  • Many others, but these are still popular and prevalent

These are old & insecure algorithms, but still functional, still useful for file checking

But there are no TempleOS equivalents!

TempleOS: Now with md5sum

MD5 in HolyC

Tracking files in TempleOS

But what is there besides MD5?

  • MD5 is deprecated
  • Common protocols use SHA-based hashes (BitTorrent, git)
  • SHA-1 uses a expansion function to create longer digest
  • But there is no TempleOS equivalent!

TempleOS: Now with sha1sum, too!

Metadata matters

Checksums are only part of the equation:

  • git computes checksums, detects corruption...
  • ...but git only worries about content
  • files aren't only content, have modification times, modes
  • these are things a file system tracks, if you trust it
  • git is not an ideal archiving tool

mtree

mtree is a BSD utility, ported to Linux

  • apt-get install mtree-netbsd
  • https://github.com/vbatts/go-mtree
  • given a path, creates "spec file" of names, modes, mtimes, and more; like "ls -lR"
  • a spec file can be used to check or repair a file tree
  • file tree metadata in a spec file is portable
  • But there is no TempleOS equivalent!

TempleOS: Now with mtree, too!

Revelation - New TempleOS apps

  • MD5
  • SHA1
  • Mtree

Available at https://su.bze.ro/software.html

Revelation - What's next?

"OSS maxim: Is there something you are looking for and it's not available? Then build it."
UNIX utilities are still lacking native TempleOS ports

  • head, tail, grep
  • rsync-like incremental file copy
  • git, zlib compression

Porting third-party programs to TempleOS - jwhitham.org
Tiny Core NTFS integration - yalis.fr via Wayback Machine

Revelation - What's next?

Revelation - What's next?

Revelation - What's next?

Slides available online

https://su.bze.ro/talks.html