diff --git a/boot.asm b/boot.asm index 616b048..0dbc1a1 100644 --- a/boot.asm +++ b/boot.asm @@ -8,7 +8,7 @@ bpOEMid db "XNOE " bpBytesPerSector dw 512 bpSectorsPerCluster db 1 -bpReservedSectors dw 10 ; We should reserve 10 sectors. Boot sector + stage2 +bpReservedSectors dw 32 ; We should reserve some sectors. Boot sector + stage2 bpNoFATs db 2 bpRootDirEntries dw 256 bpLVSectors dw 8586 @@ -50,14 +50,14 @@ bootcode: mov ax, 2 ; Begin with the 2nd sector call prep_i13 - mov al, 9 ; Load the next 9 sectors (4.5k) + mov al, 31 ; Load the next 9 sectors (4.5k) int 13h ; We need to get the memory configuration from the BIOS now ; To do this we can use int 15h eax=e820h - ; We will load the e820 data to 0x10000 + ; We will load the e820 data to 0x20000 - mov ax, 1000h + mov ax, 2000h mov es, ax xor di, di xor ebx, ebx diff --git a/src/boot_stage2/main.c b/src/boot_stage2/main.c index 6137248..663a601 100644 --- a/src/boot_stage2/main.c +++ b/src/boot_stage2/main.c @@ -1,15 +1,75 @@ #include "../kernel/atapio.h" #include "../kernel/screenstuff.h" +typedef struct { + uint32_t base_low; + uint32_t base_high; + uint32_t length_low; + uint32_t length_high; + uint32_t type; + uint32_t acpi3_extended; +}__attribute__((packed)) e820entry; + +void set_bit(uint32_t offset, uint8_t* buffer) { + uint32_t index = offset / 8; + uint32_t bit = offset % 8; + + buffer[index] |= (1<<(7 - bit)); +} void main() { init_term(); init_atapio(); + // e820 memory map exists at 0x20000 + e820entry* e820_entries = 0x20000; + + uint8_t* bitmap = 0x100000; + + // Zero out the bitmap. + for (int i=0; i<0x20000; i++) + bitmap[i] = 0; + // Ensure the bitmap data is clear + + for (int i=0; i<0x20000; i++) + if (bitmap[i]) + printf("Found data in bitmap at %x!\n", (bitmap+i)); + + for (int i=0; e820_entries[i].length_low != 0 || e820_entries[i].length_high != 0; i++) { + e820entry entry = e820_entries[i]; + printf("BIOS-e820: Starting %x%x, length %x%x is %s\n", entry.base_high, entry.base_low, entry.length_high, entry.length_low, entry.type == 1 ? "Available" : "Reserved"); + + if (entry.type != 1) + continue; + + uint32_t base = entry.base_low; + uint32_t length = entry.length_low; + + if (base % 4096) { + // Memory isn't page aligned, we need to fix that. + uint32_t add_offset = 4096 - (base % 4096); + if (length > add_offset) { + base += add_offset; + length -= add_offset; + } + } + uint32_t page_index = base / 4096; + + printf("Page Index: %d\nLength (Pages): %d\n", page_index, length / 4096); + + for (int j=0; length > 4096; length -= 4096, j++) { + uint32_t index = (page_index+j) / 8; + uint32_t bit = (page_index+j) % 8; + + bitmap[index] |= (1<<(7 - bit)); + } + } + uint8_t* kernel_location = 0x80000; load_file("KERNEL BIN", kernel_location); printf("Stage2 success!\n"); + while (1); ((void(*)(void))kernel_location)(); } \ No newline at end of file diff --git a/src/kernel/screenstuff.c b/src/kernel/screenstuff.c index a2746eb..7700d2a 100644 --- a/src/kernel/screenstuff.c +++ b/src/kernel/screenstuff.c @@ -71,8 +71,9 @@ int int_to_decimal(unsigned int number, char* string_buffer) { char dec_to_hex[16] = "0123456789abcdef"; int int_to_hex(unsigned int number, char* string_buffer) { - for (int i=0; i<9; i++) - string_buffer[i] = 0; + for (int i=0; i<8; i++) + string_buffer[i] = '0'; + string_buffer[8] = 0; int index = 7; unsigned int acc = number; @@ -128,7 +129,7 @@ void printf(const char* string, ...) { case 'x': char hex_buffer[8]; offset = int_to_hex(va_arg(ptr, int), hex_buffer); - printf(hex_buffer + offset); + printf(hex_buffer); break; case 's': printf(va_arg(ptr, const char*));