mirror of
https://github.com/tailix/libkernaux.git
synced 2025-04-07 17:32:45 -04:00
Generate Multiboot 2 headers
This commit is contained in:
parent
3eca55c41e
commit
e5f850ec40
14 changed files with 92 additions and 53 deletions
|
@ -30,7 +30,7 @@ CFLAGS = \
|
|||
-fno-stack-protector \
|
||||
-I../../include
|
||||
|
||||
OBJS = main.c.o start.S.o
|
||||
OBJS = main.c.o multiboot2.c.o start.S.o
|
||||
|
||||
test: run
|
||||
$(DIFF) -a -Z $(EXPECTED) $(OUTPUT)
|
||||
|
|
Binary file not shown.
|
@ -7,13 +7,23 @@
|
|||
|
||||
void poweroff();
|
||||
|
||||
extern const struct KernAux_Multiboot2_Header multiboot2_header;
|
||||
|
||||
static void panic(const char *str);
|
||||
|
||||
void main(
|
||||
const uint32_t multiboot2_magic,
|
||||
const struct KernAux_Multiboot2_Info *const multiboot2_info
|
||||
) {
|
||||
if (multiboot2_magic != KERNAUX_MULTIBOOT2_MAGIC) {
|
||||
if (!KernAux_Multiboot2_Header_is_valid(&multiboot2_header)) {
|
||||
panic("Multiboot 2 header is invalid");
|
||||
} else {
|
||||
kernaux_console_printf("Multiboot 2 header is valid\n");
|
||||
}
|
||||
|
||||
KernAux_Multiboot2_Header_print(&multiboot2_header, kernaux_console_printf);
|
||||
|
||||
if (multiboot2_magic != KERNAUX_MULTIBOOT2_INFO_MAGIC) {
|
||||
panic("Multiboot 2 magic number is invalid");
|
||||
} else {
|
||||
kernaux_console_printf("Multiboot 2 magic number is valid\n");
|
||||
|
|
25
examples/kernel-multiboot2-grub/multiboot2.c
Normal file
25
examples/kernel-multiboot2-grub/multiboot2.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include <kernaux/multiboot2.h>
|
||||
|
||||
__attribute__((section(".multiboot"), used))
|
||||
__attribute__((aligned(4)))
|
||||
const struct {
|
||||
struct KernAux_Multiboot2_Header multiboot2_header;
|
||||
struct KernAux_Multiboot2_HTag_None tag_none;
|
||||
} multiboot2_header = {
|
||||
.multiboot2_header = {
|
||||
.magic = KERNAUX_MULTIBOOT2_HEADER_MAGIC,
|
||||
.arch = KERNAUX_MULTIBOOT2_ARCH_NONE,
|
||||
.total_size = sizeof(multiboot2_header),
|
||||
.checksum = KERNAUX_MULTIBOOT2_HEADER_CHECKSUM(
|
||||
KERNAUX_MULTIBOOT2_ARCH_NONE,
|
||||
sizeof(multiboot2_header)
|
||||
),
|
||||
},
|
||||
.tag_none = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_HTAG_NONE,
|
||||
.flags = 0,
|
||||
.size = sizeof(multiboot2_header.tag_none),
|
||||
},
|
||||
},
|
||||
};
|
|
@ -1,21 +1,3 @@
|
|||
.set MULTIBOOT_MAGIC, 0xE85250D6
|
||||
.set MULTIBOOT_ARCH, 0
|
||||
.set MULTIBOOT_LENGTH, 16 + 8
|
||||
.set MULTIBOOT_CHECKSUM, -(MULTIBOOT_MAGIC + MULTIBOOT_ARCH + MULTIBOOT_LENGTH)
|
||||
|
||||
|
||||
|
||||
.section .multiboot
|
||||
.align 4
|
||||
.long MULTIBOOT_MAGIC
|
||||
.long MULTIBOOT_ARCH
|
||||
.long MULTIBOOT_LENGTH
|
||||
.long MULTIBOOT_CHECKSUM
|
||||
.long 0
|
||||
.long 8
|
||||
|
||||
|
||||
|
||||
.section .bss
|
||||
|
||||
.global _kernel_stack_start
|
||||
|
|
|
@ -45,7 +45,7 @@ CFLAGS = \
|
|||
-fno-stack-protector \
|
||||
-I../../include
|
||||
|
||||
OBJS = main.c.o start.S.o
|
||||
OBJS = main.c.o multiboot2.c.o start.S.o
|
||||
|
||||
test: run
|
||||
$(DIFF) -a -Z $(EXPECTED) $(OUTPUT)
|
||||
|
|
Binary file not shown.
|
@ -7,13 +7,23 @@
|
|||
|
||||
void poweroff();
|
||||
|
||||
extern const struct KernAux_Multiboot2_Header multiboot2_header;
|
||||
|
||||
static void panic(const char *str);
|
||||
|
||||
void main(
|
||||
const uint32_t multiboot2_magic,
|
||||
const struct KernAux_Multiboot2_Info *const multiboot2_info
|
||||
) {
|
||||
if (multiboot2_magic != KERNAUX_MULTIBOOT2_MAGIC) {
|
||||
if (!KernAux_Multiboot2_Header_is_valid(&multiboot2_header)) {
|
||||
panic("Multiboot 2 header is invalid");
|
||||
} else {
|
||||
kernaux_console_printf("Multiboot 2 header is valid\n");
|
||||
}
|
||||
|
||||
KernAux_Multiboot2_Header_print(&multiboot2_header, kernaux_console_printf);
|
||||
|
||||
if (multiboot2_magic != KERNAUX_MULTIBOOT2_INFO_MAGIC) {
|
||||
panic("Multiboot 2 magic number is invalid");
|
||||
} else {
|
||||
kernaux_console_printf("Multiboot 2 magic number is valid\n");
|
||||
|
|
25
examples/kernel-multiboot2-limine/multiboot2.c
Normal file
25
examples/kernel-multiboot2-limine/multiboot2.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include <kernaux/multiboot2.h>
|
||||
|
||||
__attribute__((section(".multiboot"), used))
|
||||
__attribute__((aligned(4)))
|
||||
const struct {
|
||||
struct KernAux_Multiboot2_Header multiboot2_header;
|
||||
struct KernAux_Multiboot2_HTag_None tag_none;
|
||||
} multiboot2_header = {
|
||||
.multiboot2_header = {
|
||||
.magic = KERNAUX_MULTIBOOT2_HEADER_MAGIC,
|
||||
.arch = KERNAUX_MULTIBOOT2_ARCH_NONE,
|
||||
.total_size = sizeof(multiboot2_header),
|
||||
.checksum = KERNAUX_MULTIBOOT2_HEADER_CHECKSUM(
|
||||
KERNAUX_MULTIBOOT2_ARCH_NONE,
|
||||
sizeof(multiboot2_header)
|
||||
),
|
||||
},
|
||||
.tag_none = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_HTAG_NONE,
|
||||
.flags = 0,
|
||||
.size = sizeof(multiboot2_header.tag_none),
|
||||
},
|
||||
},
|
||||
};
|
|
@ -1,21 +1,3 @@
|
|||
.set MULTIBOOT_MAGIC, 0xE85250D6
|
||||
.set MULTIBOOT_ARCH, 0
|
||||
.set MULTIBOOT_LENGTH, 16 + 8
|
||||
.set MULTIBOOT_CHECKSUM, -(MULTIBOOT_MAGIC + MULTIBOOT_ARCH + MULTIBOOT_LENGTH)
|
||||
|
||||
|
||||
|
||||
.section .multiboot
|
||||
.align 4
|
||||
.long MULTIBOOT_MAGIC
|
||||
.long MULTIBOOT_ARCH
|
||||
.long MULTIBOOT_LENGTH
|
||||
.long MULTIBOOT_CHECKSUM
|
||||
.long 0
|
||||
.long 8
|
||||
|
||||
|
||||
|
||||
.section .bss
|
||||
|
||||
.global _kernel_stack_start
|
||||
|
|
|
@ -8,13 +8,14 @@ extern "C" {
|
|||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define KERNAUX_MULTIBOOT2_MAGIC 0x36d76289
|
||||
#define KERNAUX_MULTIBOOT2_HEADER_MAGIC 0xE85250D6
|
||||
#define KERNAUX_MULTIBOOT2_INFO_MAGIC 0x36D76289
|
||||
|
||||
#define KERNAUX_MULTIBOOT2_CHECKSUM(arch, total_size) \
|
||||
((uint32_t)(-( \
|
||||
((uint32_t)KERNAUX_MULTIBOOT2_MAGIC) + \
|
||||
((uint32_t)(arch)) + \
|
||||
((uint32_t)(total_size)) \
|
||||
#define KERNAUX_MULTIBOOT2_HEADER_CHECKSUM(arch, total_size) \
|
||||
((uint32_t)(-( \
|
||||
((uint32_t)KERNAUX_MULTIBOOT2_HEADER_MAGIC) + \
|
||||
((uint32_t)(arch)) + \
|
||||
((uint32_t)(total_size)) \
|
||||
)))
|
||||
|
||||
#define KERNAUX_MULTIBOOT2_DATA(ptr) (((uint8_t*)(ptr)) + sizeof(*(ptr)))
|
||||
|
@ -46,6 +47,7 @@ enum KernAux_Multiboot2_HTag_RelocatableHeader_Preference {
|
|||
****************/
|
||||
|
||||
enum KernAux_Multiboot2_Arch {
|
||||
KERNAUX_MULTIBOOT2_ARCH_NONE = 0,
|
||||
KERNAUX_MULTIBOOT2_ARCH_I386 = 1,
|
||||
KERNAUX_MULTIBOOT2_ARCH_MIPS32 = 4,
|
||||
};
|
||||
|
|
|
@ -11,9 +11,12 @@
|
|||
bool KernAux_Multiboot2_Header_is_valid(
|
||||
const struct KernAux_Multiboot2_Header *const multiboot2_header
|
||||
) {
|
||||
if (multiboot2_header->magic != KERNAUX_MULTIBOOT2_MAGIC) return false;
|
||||
if (multiboot2_header->magic != KERNAUX_MULTIBOOT2_HEADER_MAGIC) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (multiboot2_header->arch != KERNAUX_MULTIBOOT2_ARCH_I386 &&
|
||||
if (multiboot2_header->arch != KERNAUX_MULTIBOOT2_ARCH_NONE &&
|
||||
multiboot2_header->arch != KERNAUX_MULTIBOOT2_ARCH_I386 &&
|
||||
multiboot2_header->arch != KERNAUX_MULTIBOOT2_ARCH_MIPS32)
|
||||
{
|
||||
return false;
|
||||
|
@ -31,7 +34,7 @@ bool KernAux_Multiboot2_Header_is_valid(
|
|||
}
|
||||
|
||||
if (multiboot2_header->checksum !=
|
||||
KERNAUX_MULTIBOOT2_CHECKSUM(
|
||||
KERNAUX_MULTIBOOT2_HEADER_CHECKSUM(
|
||||
multiboot2_header->arch,
|
||||
multiboot2_header->total_size
|
||||
)
|
||||
|
|
|
@ -32,10 +32,10 @@ static const struct {
|
|||
struct KernAux_Multiboot2_HTag_None tag_none;
|
||||
} multiboot2_header_example2 = {
|
||||
.multiboot2_header = {
|
||||
.magic = KERNAUX_MULTIBOOT2_MAGIC,
|
||||
.magic = KERNAUX_MULTIBOOT2_HEADER_MAGIC,
|
||||
.arch = KERNAUX_MULTIBOOT2_ARCH_I386,
|
||||
.total_size = sizeof(multiboot2_header_example2),
|
||||
.checksum = KERNAUX_MULTIBOOT2_CHECKSUM(
|
||||
.checksum = KERNAUX_MULTIBOOT2_HEADER_CHECKSUM(
|
||||
KERNAUX_MULTIBOOT2_ARCH_I386,
|
||||
sizeof(multiboot2_header_example2)
|
||||
),
|
||||
|
|
|
@ -12,10 +12,10 @@
|
|||
// TODO: add more tags
|
||||
static const char output2[] =
|
||||
"Multiboot 2 header\n"
|
||||
" magic: 920085129\n"
|
||||
" magic: 3897708758\n"
|
||||
" arch: 1\n"
|
||||
" size: 272\n"
|
||||
" checksum: 3374881894\n"
|
||||
" checksum: 397258265\n"
|
||||
"Multiboot 2 header tag\n"
|
||||
" type: 1 (information request)\n"
|
||||
" flags: 0\n"
|
||||
|
|
Loading…
Add table
Reference in a new issue