Limine Bare Bones: Difference between revisions
m
Wording fix.
[unchecked revision] | [unchecked revision] |
(Backports from limine-c-template) |
m (Wording fix.) |
||
(13 intermediate revisions by 4 users not shown) | |||
Line 11:
This article will demonstrate how to write a small Limine-compliant x86-64 kernel in (GNU) [[C]], and boot it using the [[Limine]] bootloader.
For this example, we will create these 2 files to create the basic directory tree of our project:
Line 29:
This is the kernel "main".
<
#include <stdint.h>
#include <stddef.h>
Line 155:
}
</syntaxhighlight>
===linker.ld===
Line 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 173:
PHDRS
{
}
SECTIONS
{
/*
/* and because that is what the Limine spec mandates. */
/*
/*
. = 0xffffffff80000000;
/* Define a section to contain the Limine requests and assign it to its own PHDR */▼
KEEP(*(.requests_start_marker))▼
KEEP(*(.requests))▼
KEEP(*(.requests_end_marker))▼
.text : {
Line 215 ⟶ 203:
.data : {
*(.data .data.*)
▲ /*
/* output section. */
▲ KEEP(*(.requests_start_marker))
▲ KEEP(*(.requests))
▲ KEEP(*(.requests_end_marker))
} :data
Line 231 ⟶ 225:
} :data
/* Discard .note.* and .eh_frame* since they may cause issues on some hosts. */
/* Also discard the program interpreter section since we do not need one. This is */
/* more or less equivalent to the --no-dynamic-linker linker flag, except that it */
/* works with ld.gold. */
/DISCARD/ : {
*(.eh_frame*)
*(.note .note.*)
*(.interp)
}
}
</syntaxhighlight>
==Building the kernel and creating an image==
Line 248 ⟶ 246:
GNU make will process it.
<
# Nuke built-in rules and variables.
override MAKEFLAGS += -rR
Line 321 ⟶ 319:
-m elf_x86_64 \
-nostdlib \
-pie \
-z text \
-z max-page-size=0x1000 \
Line 346 ⟶ 342:
# 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 $@)"
$(KLD) $(OBJ) $(KLDFLAGS) -o $@
printf '\003' | dd of=$@ bs=1 count=1 seek=16 conv=notrunc
# Include header dependencies.
Line 372 ⟶ 373:
clean:
rm -rf bin obj
</syntaxhighlight>
===limine.cfg===
Line 378 ⟶ 379:
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
▲ # Disable KASLR (it is enabled by default for relocatable kernels)
▲ KASLR=no
KERNEL_PATH=boot:///boot/myos
</syntaxhighlight>
===Compiling the kernel===
Line 422 ⟶ 423:
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=v7.x-binary --depth=1
Line 453 ⟶ 454:
# Install Limine stage 1 and 2 for legacy BIOS boot.
./limine/limine bios-install image.iso
</syntaxhighlight>
====Creating a hard disk/USB drive image====
Line 463 ⟶ 464:
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 490 ⟶ 491:
mcopy -i image.hdd@@1M limine/BOOTX64.EFI ::/EFI/BOOT
mcopy -i image.hdd@@1M limine/BOOTIA32.EFI ::/EFI/BOOT
</syntaxhighlight>
==Conclusions==
|