How do I write and then execute kernel code from a file in protected mode?

133 views Asked by At

When using rep insw in 32-bit protected mode to load my kernel from disk to memory, how do I properly write the data to memory, and then execute it?

If I understand correctly, the data segment can be written to, but not executed, and the code segment can be executed but not written to.

I'm not having any problems doing this below the 1MB memory mark, but I want to load it right at 0x00100000.

My loading subroutine:

edi - destination in memory

esi - source LBA

ecx - number of sectors to write

ebx - return point

load32_source dd 0

load32:
    mov dword [load32_source], esi

    mov al, byte [load32_source + 3]
    or al, 0xe0
    mov dx, 0x1f6
    out dx, al

    mov dx, 0x1f1
    mov al, 0x00                    ; null to error
    out dx, al

    mov dx, 0x1f2                   ; sector count
    mov al, cl
    out dx, al

    mov al, byte [load32_source]
    mov dx, 0x1f3
    out dx, al

    mov al, byte [load32_source + 1]
    mov dx, 0x1f4
    out dx, al

    mov al, byte [load32_source + 2]
    mov dx, 0x1f5
    out dx, al

    mov dx, 0x1f7
    mov al, 0x20                    ; read sectors
    out dx, al

waitforit:
    in      al, dx
    test    al, 0x08
    jz      waitforit
    mov     eax, ecx
    mov     ecx, 0x00000100
    mul     ecx
    mov     ecx, eax
    mov     edx, 0x000001f0
    rep     insw

    jmp     ebx
0

There are 0 answers