Limine Bare Bones: Difference between revisions
[unchecked revision] | [unchecked revision] |
Content deleted Content added
Change hdd creation instructions to allow for creation without sudo |
No edit summary |
||
(33 intermediate revisions by 6 users not shown) | |||
Line 3:
{{Template:Kernel designs}}
The Limine Boot Protocol is the native boot protocol provided by the [[Limine]] bootloader.
It provides cutting edge features such as 5-level paging support, 64-bit [[Long Mode]] support, and direct higher half kernel loading.
Line 9:
The Limine boot protocol is firmware and architecture agnostic. The Limine bootloader supports x86-64, IA-32, aarch64, and riscv64.
This article will demonstrate how to write a small Limine-compliant x86-64
For this example, we will create these 2 files
*
* linker.ld
As one may notice, there is no "entry point" assembly stub, as one is not necessary with the Limine protocol when using a language which can make use of a standard SysV x86 [[Calling Conventions|calling convention]].
Furthermore, we will download the header file
Obviously, this is just a bare bones example, and one should always refer to the [https://github.com/limine-bootloader/limine/blob/trunk/PROTOCOL.md Limine protocol specification] for more details and information.
===
This is the kernel "main".
<
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <limine.h>
// Set the base revision to 2, this is recommended as this is the latest
// base revision described by the Limine boot protocol specification.
// See specification for further info.
__attribute__((used, section(".requests")))
static volatile LIMINE_BASE_REVISION(2);
// The Limine requests can be placed anywhere, but it is important that
// the compiler does not optimise them away, so, usually, they should
// be made volatile or equivalent
// once or marked as used with the "used" attribute as done here.
__attribute__((used, section(".requests")))
static volatile struct limine_framebuffer_request framebuffer_request = {
.id = LIMINE_FRAMEBUFFER_REQUEST,
.revision = 0
};
// Finally, define the start and end markers for the Limine requests.
// These can also be moved anywhere, to any .c file, as seen fit.
__attribute__((used, section(".requests_start_marker")))
static volatile LIMINE_REQUESTS_START_MARKER;
__attribute__((used, section(".requests_end_marker")))
static volatile LIMINE_REQUESTS_END_MARKER;
// GCC and Clang reserve the right to generate calls to the following
Line 112 ⟶ 131:
// linker script accordingly.
void _start(void) {
// Ensure the bootloader actually understands our base revision (see spec).
if (LIMINE_BASE_REVISION_SUPPORTED == false) {
hcf();
}
// Ensure we got a framebuffer.
if (framebuffer_request.response == NULL
Line 123 ⟶ 147:
// Note: we assume the framebuffer model is RGB with 32-bit pixels.
for (size_t i = 0; i < 100; i++) {
volatile uint32_t *fb_ptr = framebuffer->address;
fb_ptr[i * (framebuffer->pitch / 4) + i] = 0xffffff;
}
Line 131 ⟶ 155:
}
</syntaxhighlight>
===linker.ld===
Line 137 ⟶ 161:
This is going to be our linker script describing where our sections will end up in memory.
<
/* Tell the linker that we want an x86_64 ELF64 output file */
OUTPUT_FORMAT(elf64-x86-64)
Line 144 ⟶ 168:
/* We want the symbol _start to be our entry point */
ENTRY(_start)
SECTIONS
{
/* We
/* and because that is what the Limine spec mandates. */
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
/* that is the beginning of the region. */
/* Additionally, leave space for the ELF headers by adding SIZEOF_HEADERS to the */
/* base load address. */
. = 0xffffffff80000000 + SIZEOF_HEADERS;
.rodata ALIGN(CONSTANT(MAXPAGESIZE)) : {
*(.rodata .rodata.*)
}
.text ALIGN(CONSTANT(MAXPAGESIZE)) : {
}
.data ALIGN(CONSTANT(MAXPAGESIZE)) : {
*(.data .data.*)
/*
/* output section. */
KEEP(*(.requests_start_marker))
KEEP(*(.requests))
KEEP(*(.requests_end_marker))
}
/* Dynamic section for relocations and other PIE related information. */
.dynamic : {
*(.dynamic)
}
/* NOTE: .bss needs to be the last thing mapped to
/* unnecessary zeros will be written to the binary. */
/* If you need, for example, .init_array and .fini_array, those should be placed */
/* above this. */
.bss ALIGN(CONSTANT(MAXPAGESIZE)) : {
*(.bss .bss.*)
*(COMMON)
}
/* Discard
/* more or less equivalent to the --no-dynamic-linker linker flag, except that it */
/* works with ld.gold. */
/DISCARD/ : {
*(.
}
}
</syntaxhighlight>
==Building the kernel and creating an image==
Line 209 ⟶ 226:
In order to build our kernel, we are going to use a Makefile. Since we're going to use
GNU make specific features, we call this file
GNU make will process it.
<
# Nuke built-in rules and variables.
override MAKEFLAGS += -rR
Line 218 ⟶ 235:
# This is the name that our final kernel executable will have.
# Change as needed.
override KERNEL := myos
# Convenience macro to reliably declare user overridable variables.
Line 230 ⟶ 247:
endef
# It is
# We are
# the host system's toolchain, but this is not guaranteed.
override
$(eval $(call DEFAULT_VAR,
# User controllable C flags.
override
$(eval $(call DEFAULT_VAR,
# User controllable C preprocessor flags. We set none by default.
override
$(eval $(call DEFAULT_VAR,
# User controllable nasm flags.
override
$(eval $(call DEFAULT_VAR,
# User controllable linker flags. We set none by default.
override
$(eval $(call DEFAULT_VAR,
# Internal C flags that should not be changed by the user.
override
-Wall \
-Wextra \
Line 264 ⟶ 277:
-fno-stack-protector \
-fno-stack-check \
-fPIE \
-m64 \
-march=x86-64 \
-mno-80387 \
-mno-mmx \
Line 276 ⟶ 287:
# Internal C preprocessor flags that should not be changed by the user.
override
-I
$(
-MMD \
-MP
# Internal linker flags that should not be changed by the user.
override
-nostdlib \
-
-
-Wl,-
-Wl,-z
-Wl,-z
-Wl,-T
# Internal nasm flags that should not be changed by the user.
override
-Wall \
-f elf64
Line 300 ⟶ 310:
# Use "find" to glob all *.c, *.S, and *.asm files in the tree and obtain the
# object and header dependency file names.
override CFILES := $(shell cd src && find -L
override ASFILES := $(shell cd src && find -L
override NASMFILES := $(shell cd src && find -L
override OBJ := $(addprefix obj/,$(CFILES:.c=.c.o) $(ASFILES:.S=.S.o) $(NASMFILES:.asm=.asm.o))
override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d))
# Default target.
.PHONY: all
all: bin/$(KERNEL)
# Link rules for the final kernel executable.
# The magic printf/dd command is used to force the final ELF file type to ET_DYN.
# GNU binutils, for silly reasons, forces the ELF type to ET_EXEC even for
# relocatable PIEs, if the base load address is non-0.
# See https://sourceware.org/bugzilla/show_bug.cgi?id=31795 for more information.
bin/$(KERNEL): GNUmakefile linker.ld $(OBJ)
mkdir -p "$$(dirname $@)"
$(KCC) $(KCFLAGS) $(OBJ) $(KLDFLAGS) -o $@
printf '\003' | dd of=$@ bs=1 count=1 seek=16 conv=notrunc 2>/dev/null
# Include header dependencies.
Line 318 ⟶ 334:
# Compilation rules for *.c files.
obj/%.c.o: src/%.c GNUmakefile
mkdir -p "$$(dirname $@)"
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
# Compilation rules for *.S files.
obj/%.S.o: src/%.S GNUmakefile
mkdir -p "$$(dirname $@)"
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
# Compilation rules for *.asm (nasm) files.
obj/%.asm.o: src/%.asm GNUmakefile
mkdir -p "$$(dirname $@)"
nasm $(KNASMFLAGS) $< -o $@
# Remove object files and the final executable.
.PHONY: clean
clean:
rm -rf
</syntaxhighlight>
===limine.cfg===
Line 339 ⟶ 358:
This file is parsed by Limine and it describes boot entries and other bootloader configuration variables. Further information [https://github.com/limine-bootloader/limine/blob/trunk/CONFIG.md here].
<
# Timeout in seconds that Limine will use before automatically booting.
TIMEOUT=5
# The entry name that will be displayed in the boot menu.
:myOS
# We use the Limine boot protocol.
PROTOCOL=limine
# Disable KASLR (it is enabled by default for relocatable kernels)
KASLR=no
# Path to the kernel to boot. boot:/// represents the partition on which limine.cfg is located.
KERNEL_PATH=boot:///boot/myos
# Same thing, but
:myOS (
PROTOCOL=limine
KERNEL_PATH=boot:///boot/myos
</syntaxhighlight>
===Compiling the kernel===
We can now build our example kernel by running
===Compiling the kernel on macOS===
''If you are not using macOS, you can skip this section.''
The macOS Xcode toolchain uses Mach-O binaries, and not the ELF binaries required for this Limine-compliant kernel. A solution is to build a [[GCC Cross-Compiler]], or to obtain one from [https://brew.sh homebrew] by installing the <code>x86_64-elf-gcc</code> package. After one of these is done, build using <code>make KCC=x86_64-elf-gcc</code>.
===Creating the image===
Line 373 ⟶ 398:
In this example we are going to create a CD-ROM ISO capable of booting on both [[UEFI]] and legacy [[BIOS]] systems.
For this to work, we will need the
These are shell commands. They can also be compiled into a script or Makefile.
<
# Download the latest Limine binary release for the 7.x branch.
git clone https://github.com/limine-bootloader/limine.git --branch=
# Build "limine" utility.
make -C limine
Line 388 ⟶ 413:
# Copy the relevant files over.
mkdir -p iso_root/boot
cp -v
mkdir -p iso_root/boot/limine
cp -v limine.cfg limine/limine-bios.sys limine/limine-bios-cd.bin \
limine/limine-uefi-cd.bin iso_root/boot/limine/
# Create the EFI boot tree and copy Limine's EFI executables over.
Line 397 ⟶ 425:
# Create the bootable ISO.
xorriso -as mkisofs -b boot/limine/limine-bios-cd.bin \
-no-emul-boot -boot-load-size 4 -boot-info-table \
--efi-boot boot/limine/limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
iso_root -o image.iso
Line 405 ⟶ 433:
# Install Limine stage 1 and 2 for legacy BIOS boot.
./limine/limine bios-install image.iso
</syntaxhighlight>
====Creating a hard disk/USB drive image====
In this example, we'll create a [[GPT]] partition table using
This example is more involved and is made up of more steps than creating an ISO image.
Line 415 ⟶ 443:
These are shell commands. They can also be compiled into a script or Makefile.
<
# Create an empty zeroed-out 64MiB image file.
dd if=/dev/zero bs=1M count=0 seek=64 of=image.hdd
Line 422 ⟶ 450:
sgdisk image.hdd -n 1:2048 -t 1:ef00
# Download the latest Limine binary release for the 7.x branch.
git clone https://github.com/limine-bootloader/limine.git --branch=
# Build "limine" utility.
make -C limine
Line 434 ⟶ 462:
mformat -i image.hdd@@1M
# Make relevant subdirectories.
mmd -i image.hdd@@1M ::/EFI ::/EFI/BOOT ::/boot ::/boot/limine
# Copy over the relevant files.
mcopy -i image.hdd@@1M bin/myos
mcopy -i image.hdd@@1M limine.cfg limine/limine-bios.sys ::/boot/limine
mcopy -i image.hdd@@1M limine/BOOTX64.EFI ::/EFI/BOOT
mcopy -i image.hdd@@1M limine/BOOTIA32.EFI ::/EFI/BOOT
</syntaxhighlight>
==Conclusions==
Line 457 ⟶ 486:
* [https://github.com/limine-bootloader/limine/blob/trunk/PROTOCOL.md Limine protocol specification]
* [https://github.com/limine-bootloader/limine-
[[Category:Bare bones tutorials]]
|