CeDOS - Commit c48b963f

Bootloader: Bugfixes Fixed bootloader bug (hard drive index overwritten by bootloader) and eliminated warnings
Celina Sophie Kalus
Sat, 08 Apr 2023 18:22:46 +0200
10 files changed, 81 insertions(+), 83 deletions(-)
D link.txt

@@ -1,58 +0,0 @@

-OUTPUT_ARCH(i386) -OUTPUT_FORMAT(elf32-i386) - -PAGE_SIZE = 1 << 12; - -MEMORY -{ - BOOT_VMA : ORIGIN = 0x00007C00, LENGTH = 0x00001000 - KERNEL_VMA : ORIGIN = 0xC0000000, LENGTH = 0x30000000 - APPLICATION_VMA : ORIGIN = 0x10000000, LENGTH = 0xB0000000 -} - -SECTIONS -{ - BOOT : AT(0x0000) - { - */boot.o(.*) - } >BOOT_VMA - - KERNEL : AT(0x5E00) - { - */kernel.o(.*) - */kernel.o(.bss) - } >KERNEL_VMA - - APPLICATION : AT(LOADADDR(KERNEL) + SIZEOF(KERNEL)) - { - */apps.o(.*) - } >APPLICATION_VMA - - ELF : AT(0xB600) - { - */apps_raw.o(.*) - } >APPLICATION_VMA - - .bss : AT(LOADADDR(ELF) + SIZEOF(ELF)) - { - - } >KERNEL_VMA -} - -__BOOT_LMA = LOADADDR(BOOT); -__BOOT_VMA = ADDR(BOOT); -__BOOT_SIZE = SIZEOF(BOOT); - -__KERNEL_LMA = LOADADDR(KERNEL); -__KERNEL_VMA = ADDR(KERNEL); -__KERNEL_SIZE = SIZEOF(KERNEL); - -__ELF_LMA = LOADADDR(ELF); -__ELF_VMA = ADDR(ELF); -__ELF_SIZE = SIZEOF(ELF); - -__KERNEL_STACK_ADDR = 0xC0400000; - -ASSERT(__BOOT_SIZE <= 0x1000, "bootloader too big!"); -ASSERT(__KERNEL_SIZE <= 0x5000, "bootloader too big!"); -ASSERT(__ELF_SIZE <= 0x3000, "bootloader too big!");
M src/boot/a20.ssrc/boot/a20.s

@@ -146,4 +146,4 @@ .byte 0

a20_enable_fasta20_msg: .ascii " fast A20 method: " - .byte 0+ .byte 0
M src/boot/drives.ssrc/boot/drives.s

@@ -101,6 +101,9 @@ push %cx

movw $reset_msg, %si call print + lea bootdriv_id, %si + movb (%si), %dl + movb $0x00, %ah int $0x13 pop %cx

@@ -142,19 +145,26 @@ # call print_hex_int16

movw %bx, %di + # divide LBA by number of sectors lea num_sectors, %si + xor %dx, %dx movw (%si), %bx - # inc %ax - divb %bl - movb %ah, %cl + divw %bx + + andw $0x003F, %dx + movb %dl, %cl incb %cl - xor %ah, %ah + xor %dx, %dx lea num_heads, %si movw (%si), %bx - divb %bl - movb %ah, %dh + divw %bx movb %al, %ch + movb %ah, %al + xorb %ah, %ah + shl $6, %al + orb %al, %cl + movb %dl, %dh lea bootdriv_id, %si movb (%si), %dl

@@ -316,6 +326,7 @@ .byte 13

.byte 10 .byte 0 + .section .bss .global bootdriv_id bootdriv_id:

@@ -331,4 +342,4 @@ num_cylinders:

.word 0 num_heads: - .word 0+ .word 0
M src/boot/entry.ssrc/boot/entry.s

@@ -161,15 +161,29 @@ # perform long jump to set code segment

ljmp $0x18, $protected +myhexdump: + movb (%si), %al + call print_hex_char + inc %si + push %si + movw $space_msg, %si + call print + pop %si + loop myhexdump + movw $newline_msg, %si + call print + ret + + .code32 protected: # setup registers with appropriate GDT values mov $0x20, %eax + mov %eax, %ss mov %eax, %ds mov %eax, %es mov %eax, %fs mov %eax, %gs - mov %eax, %ss call load_kernel

@@ -194,6 +208,15 @@ jmp loop

.section .data +space_msg: + .ascii " " + .byte 0 + +newline_msg: + .byte 13 + .byte 10 + .byte 0 + gdt_msg: .ascii "Setting GDT..." .byte 0

@@ -227,9 +250,9 @@ .byte 10

.byte 0 GDT_DESCRIPTOR: - .word 0x39 + .word 0x37 .int GDT IDT_DESCRIPTOR: .word 0 - .int 0+ .int 0
M src/boot/fat.csrc/boot/fat.c

@@ -52,11 +52,7 @@ uint32_t FAT2_lba;

uint32_t root_lba; uint32_t data_lba; -void *FAT_init() { - const int sector_size = 512; - const int sector_num = 128; - const int part_size = sector_size * sector_num; - +void FAT_init() { // open image file FAT_addr = (void*)(0x10000);

@@ -109,7 +105,7 @@ return -1;

} // deleted file - if (dir_entry->name[0] == 0xE5) { + if (dir_entry->name[0] == (char)(0xE5)) { index++; continue; }

@@ -169,7 +165,7 @@ }

uint16_t FAT_next_cluster(uint16_t cluster) { // assuming FAT12 - int offset = (cluster >> 1) * 3; + uint32_t offset = (cluster >> 1) * 3; uint8_t *sect = FAT_read_sector_offset(FAT1_lba, &offset); sect += offset;
M src/boot/fat.hsrc/boot/fat.h

@@ -3,7 +3,7 @@ #define FAT_H

#include <stdint.h> -void *FAT_init(); +void FAT_init(); void *FAT_read_sector_offset(uint32_t lba, uint32_t *offset); void *FAT_read_cluster(uint16_t cluster, void *buffer); uint16_t FAT_next_cluster(uint16_t cluster);
M src/boot/link.txtsrc/boot/link.txt

@@ -8,6 +8,7 @@

MEMORY { BOOT_VMA : ORIGIN = 0x00007C00, LENGTH = 0x00001000 + BOOT_DATA : ORIGIN = 0x00009000, LENGTH = 0x00001000 KERNEL_VMA : ORIGIN = 0xC0000000, LENGTH = 0x30000000 }

@@ -20,7 +21,16 @@ . = 510;

BYTE(0x55) BYTE(0xAA) */*.o(.text) - */*.o(.data) + } >BOOT_VMA + + .data : + { */*.o(.rodata*) + */*.o(.data) } >BOOT_VMA + + .bss : + { + */*.o(.bss) + } >BOOT_DATA }
M src/boot/main.csrc/boot/main.c

@@ -16,6 +16,22 @@ }

} int load_kernel() { + // debug output + uint8_t *dbuf = (uint8_t *)(0x10000); + for (int i = 0; i < 16; i++) { + uint8_t value = dbuf[i]; + char lut[] = "0123456789ABCDEF"; + + uint8_t low = value & 0x0F; + uint8_t high = value >> 4; + + printc(lut[high]); + printc(lut[low]); + printc(' '); + } + + //while (1); + FAT_init(); int i = 0;

@@ -38,10 +54,8 @@ }

// copy all clusters uint16_t cluster = first_cluster; - void *buffer = (void *)(0x100000); + uint8_t *buffer = (uint8_t *)(0x100000); while (1) { - uint8_t *str = (uint8_t*)(0xB8000); - buffer = FAT_read_cluster(cluster, buffer); cluster = FAT_next_cluster(cluster);
M src/boot/mbr.ssrc/boot/mbr.s

@@ -86,4 +86,4 @@

newline: .byte 13 .byte 10 - .byte 0+ .byte 0
M src/boot/string.csrc/boot/string.c

@@ -29,7 +29,9 @@ void *memcpy(void *_dest, const void *_src, uint32_t n) {

uint8_t *src = (uint8_t*)(_src); uint8_t *dest = (uint8_t*)(_dest); - for (int i = 0; i < n; i++) { + for (uint32_t i = 0; i < n; i++) { dest[i] = src[i]; } + + return _dest; }