ext4 patch for Limine 12.11.2
We have a ext 2/3/4 patch for Limine v12, tested on Limine 12.3.3 and 12.9.4; however. compile fails on limine 12.11.2, as reported:
https://github.com/Limine-Bootloader/Limine/issues/721
A bit beyond me to rework the ext patch, so took the help of Claude AI, asked this:
Limine is a bootloader, there is a third-party patch,
attached, and a compile script. Compile version 12.11.2 fails
with the message reported at the Limine github issues:
https://github.com/Limine-Bootloader/Limine/issues/721
Can you think of any way to reduce the size of the Limine code,
or the patch, without compromising functionality?
Here is Claude's reply:
I'll start by unpacking the files into separate directories
and reading the compile script and the issue.
Quick status: I've reproduced the setup. The patched stage 2 is
about 27.2 KB compressed against a 28,160-byte limit, so I'm now
working out the exact overshoot and what to trim.
My build is 188 bytes over the limit (the reporter's toolchain
was 1 byte over, so toolchain variance alone is about 190
bytes). Now I'll read the ext driver for savings.
Allocations are zero-filled, so I can rely on that. Now I'll
write the compact driver: keep the struct definitions (they cost
no code), keep the first ~219 lines, and replace all the logic.
`-Os` makes no real difference (26,903 vs 26,913), so build
flags aren't a lever here. I'll look at where the remaining
bytes in the ext driver actually go.
`mtools` isn't installed here, which only affects the UEFI CD
image and not the code. I'll leave that one option out and keep
both UEFI ports so the driver is still compiled for them.
I rewrote the ext driver more compactly. With it, 12.11.2 builds
in my environment with a 124-byte margin. I did not boot it,
only built it and tested the driver on a host machine.
**Cause:** the failing `TIMES value -1 is negative` line is a
size check in `stage1/hdd/bootsect.asm`. The decompressor plus
the compressed stage 2 must fit in 28,160 bytes (`63*512 -
4096`), because stage 2 has to fit in the 63-sector gap before
the first partition. Unpatched Limine 12.11.2 is 26,078 bytes,
so the ext driver was the 2 KB that tipped it over. In my build
the original patch came to 28,348 bytes, 188 over. The
reporter's toolchain was only 1 byte over, so it differs from
mine by about 190 bytes.
**Result:** the patched build is now 28,036 bytes. That is 312
bytes smaller than before, and in the reporter's toolchain it
should leave about 300 bytes of headroom. It built cleanly with
your exact `--enable-bios --enable-bios-cd --enable-bios-pxe
--enable-uefi-ia32 --enable-uefi-x86-64`, but I left out
`--enable-uefi-cd`, which needs `mtools` (not installed here).
**What I changed** (only `ext2.s2.c` and a one-liner in
`file.s2.c`):
- **Block lookup:** the whole-file block map is gone. A block is
now looked up on demand, with one loop covering single, double
and triple indirection, so the map-building and cleanup code
disappears.
- **Group descriptors:** the two near-identical 32-bit and
64-bit inode lookups are now one.
- **Shared helpers:**
- A single function follows symlinks for both directory
components and the final file.
- One helper checks the ext superblock.
- One `goto fail` exit replaces five copies of the
cleanup code.
- **Memory:** the per-directory-entry allocation is replaced
with a static buffer, and the label and GUID queries share one
static superblock instead of two 1 KB stack copies.
- **What didn't help:** `-Os`, per-function `minsize`, and
replacing divisions with shifts each saved under 25 bytes or
made it larger, so I left them out.
**Testing:** I built a host harness and compared the original
driver and the new one on real ext2, ext3 and ext4 images, with
1K and 4K blocks and a 64-bit ext4. The new driver passes all
114 checks (original: 94 of 114). The checks cover:
- files up to 70 MB;
- partial reads;
- absolute, relative, chained and mid-path symlinks;
- case-insensitive lookup;
- label and GUID;
- failure cases.
The negative cases and label/GUID output are identical to the
original's.
**Behaviour changes:**
- **Fixed, relative symlinks:** these panicked or resolved
wrongly in the original, including a relative symlink directly
under `/`.
- **Fixed, large ext2/3 files:** reads past about 66 MB were
wrong.
- **Fixed, directory lookup:** it no longer matches deleted
entries (inode 0).
- **Fixed, 64-bit ext4:** it now reads the correct high half of
the inode-table address (offset `0x28`).
- **Fixed, labels:** a 16-character label is no longer
over-read.
- **Changed, error messages:** the "not a directory/regular
file" messages are merged into one and slightly reworded.
The patch is attached; it applies cleanly to the pristine
tarball. The one thing I couldn't do here is boot-test it, so
please try it in QEMU or on a real BIOS machine with an ext4
`/boot`.
Still open:
- **Stack protector:** exempting the driver from
`-fstack-protector-strong` would save a bit more, but it weakens
hardening, so I didn't.
- **Symlink loops:** the driver has no loop guard (`a`→`b`→`a`),
as in the original.
- **Headroom:** upstream says stage 2 space is tight, so the
next release may overflow again.
Interesting sizes:
With original ext patch:
limine-12.9.4/common-bios/stage2.bin.limlz size: 25546 bytes
With new "-slim" patch:
limine-12.11.2/common-bios/stage2.bin.limlz size: 26842 bytes
allowed max size: 28160 bytes (63*512 - 4096)
So, the original patch was fine for limine 12.9.4. The stage-2
size grew a lot after that
For reference, a fix for v12 was posted by zepto here, for the original ext patch:
https://forum.puppylinux.com/viewtopic.php?t=17629
Ongoing discussion here:
https://forum.puppylinux.com/viewtopic.php?t=17781
Important, I have not yet tested this patch! Will report back to
the above forum thread.
Tags: easy