32bit boot using multiboot2

This commit is contained in:
Alex 2022-10-17 23:52:55 +03:00
parent 961d7d7270
commit 3df98c7c8e
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
5 changed files with 282 additions and 324 deletions

View File

@ -1,43 +1,43 @@
#include <types.h>
EXTERNC __attribute__((no_stack_protector, section(".multiboot.text"))) void Multiboot2Initializator()
#include <boot/protocols/multiboot2.h>
struct multiboot_info
{
for (int i = 0; i < 2000; i++)
{
((unsigned int *)0xb8000)[i * 2] = ' ';
((unsigned int *)0xb8000)[i * 2 + 1] = 0;
}
((uint8_t *)0xb8000)[2 * (80) * (25) - 2] = 'W';
((uint8_t *)0xb8000)[2 * (80) * (25) - 1] = 6;
unsigned char *SMBIOSAddress = (unsigned char *)0xF0000;
while ((unsigned int)(unsigned long)SMBIOSAddress < 0x100000)
{
if (SMBIOSAddress[0] == '_' &&
SMBIOSAddress[1] == 'S' &&
SMBIOSAddress[2] == 'M' &&
SMBIOSAddress[3] == '_')
{
unsigned char Checksum = 0;
int Length = SMBIOSAddress[5];
for (int i = 0; i < Length; i++)
Checksum += SMBIOSAddress[i];
multiboot_uint32_t Size;
multiboot_uint32_t Reserved;
struct multiboot_tag *Tag;
};
if (Checksum == 0)
break;
}
SMBIOSAddress += 16;
}
if ((unsigned int)(unsigned long)SMBIOSAddress == 0x100000)
EXTERNC __attribute__((no_stack_protector, section(".mb2bootcode.text"))) void Multiboot2Initializator(multiboot_info *Info, unsigned int Magic)
{
if (Info == NULL || Magic == NULL)
{
// No SMBIOS found
((uint8_t *)0xb8000)[2 * (80) * (25) - 4] = 'S';
((uint8_t *)0xb8000)[2 * (80) * (25) - 3] = 4;
if (Magic == NULL)
{
((unsigned char *)0xb8000)[2 * (80) * (25) - 4] = 'E';
((unsigned char *)0xb8000)[2 * (80) * (25) - 3] = 4;
}
if (Info == NULL)
{
((unsigned char *)0xb8000)[2 * (80) * (25) - 2] = 'R';
((unsigned char *)0xb8000)[2 * (80) * (25) - 1] = 4;
}
while (1)
asmv("hlt");
}
else if (Magic != MULTIBOOT2_BOOTLOADER_MAGIC)
{
((unsigned char *)0xb8000)[2 * (80) * (25) - 2] = 'M';
((unsigned char *)0xb8000)[2 * (80) * (25) - 1] = 4;
while (1)
asmv("hlt");
}
((uint8_t *)0xb8000)[2 * (80) * (25) - 10] = 'Y';
((uint8_t *)0xb8000)[2 * (80) * (25) - 9] = 2;
return;
/* TODO */
((unsigned char *)0xb8000)[2 * (80) * (25) - 2] = 'Y';
((unsigned char *)0xb8000)[2 * (80) * (25) - 1] = 2;
while (1)
asmv("hlt");
}

View File

@ -1,84 +1,33 @@
KERNEL_VIRTUAL_BASE EQU 0xE0000000
section .multiboot2.data
section .multiboot2
align 8
HEADER_START:
dd 0xE85250D6
dd 0
dd (HEADER_END - HEADER_START)
dd -(0xE85250D6 + (HEADER_END - HEADER_START))
dd -(0xE85250D6 + 0 + (HEADER_END - HEADER_START))
align 8
MB2_TAG_START:
dw 0
dw 0
dd 8
dd MB2_TAG_END - MB2_TAG_START
MB2_TAG_END:
HEADER_END:
section .bss
STACK_BOTTOM: resb 16384
STACK_TOP:
align 4096
BOOT_PAGE_DIR0: resb 4096
BOOT_PAGE_TBL0: resb 4096
BOOT_PAGE_TBL1: resb 4096
section .multiboot2.text
extern x32Entry
extern Multiboot2Initializator
extern _kernel_start
extern _kernel_end
global _start
_start:
mov word [0xb8000], 0x074C ; L
mov word [0xb8002], 0x076F ; o
mov word [0xb8004], 0x0761 ; a
mov word [0xb8006], 0x0764 ; d
mov word [0xb8008], 0x0769 ; i
mov word [0xb800a], 0x076E ; n
mov word [0xb800c], 0x0767 ; g
mov word [0xb800e], 0x072E ; .
mov word [0xb8010], 0x072E ; .
mov word [0xb8012], 0x072E ; .
mov esp, STACK_TOP - KERNEL_VIRTUAL_BASE
mov edi, BOOT_PAGE_TBL0 - KERNEL_VIRTUAL_BASE
mov esi, 0
mov ecx, 2048 - 301
.PagingLoop:
cmp esi, _kernel_start - KERNEL_VIRTUAL_BASE
jl .LoopInside
cmp esi, _kernel_end - KERNEL_VIRTUAL_BASE
jge .LoopEnd
mov eax, esi
or eax, 3
mov [edi], eax
.LoopInside:
add esi, 4096
add edi, 4
loop .PagingLoop
.LoopEnd:
call Multiboot2Initializator
push ebx
mov dword [BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE + (000 * 4)], (BOOT_PAGE_TBL0 - KERNEL_VIRTUAL_BASE + 3)
mov dword [BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE + (001 * 4)], (BOOT_PAGE_TBL1 - KERNEL_VIRTUAL_BASE + 3)
mov dword [BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE + (896 * 4)], (BOOT_PAGE_TBL0 - KERNEL_VIRTUAL_BASE + 3)
mov dword [BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE + (897 * 4)], (BOOT_PAGE_TBL1 - KERNEL_VIRTUAL_BASE + 3)
mov ecx, BOOT_PAGE_DIR0 - KERNEL_VIRTUAL_BASE
mov cr3, ecx
mov ecx, cr0
or ecx, 0x80010000
mov cr0, ecx
add esp, KERNEL_VIRTUAL_BASE
mov eax, CallKernelMain
jmp eax
section .text
CallKernelMain:
push ebx
call x32Entry
cli
.hang:
mov esp, STACK_TOP
push eax
push ebx
call Multiboot2Initializator
.Hang:
hlt
jmp .hang
jmp .Hang
section .mb2bootcode.bss
STACK_BOTTOM: resb 16384
STACK_TOP:

View File

@ -3,24 +3,36 @@ OUTPUT_ARCH(i386)
ENTRY(_start)
KERNEL_VIRTUAL_BASE = 0xE0000000;
KERNEL_VIRTUAL_BASE = 0xC0000000;
SECTIONS
{
. = 0x00100000;
_kernel_start = . + KERNEL_VIRTUAL_BASE;
. = 1M;
.multiboot.data :
.boot :
{
*(.multiboot2.data)
*(.multiboot2 .multiboot2.*)
}
.multiboot.text :
.mb2bootcode.text :
{
*(.multiboot2.text)
*(.mb2bootcode.text)
}
.mb2bootcode.data :
{
*(.mb2bootcode.data)
}
.mb2bootcode.bss :
{
*(.mb2bootcode.bss)
}
. += ALIGN(4096);
. += KERNEL_VIRTUAL_BASE;
_kernel_start = .;
.text ALIGN(4096) : AT(ADDR(.text) - KERNEL_VIRTUAL_BASE)
{
*(.text .text.*)
@ -41,10 +53,8 @@ SECTIONS
.bss ALIGN (4096) : AT(ADDR(.bss) - KERNEL_VIRTUAL_BASE)
{
_sbss = .;
*(COMMON)
*(.bss .bss.*)
_ebss = .;
}
_kernel_end = .;

View File

@ -1491,7 +1491,6 @@ namespace CPU
: "memory");
#endif
}
}
}