logo
down
shadow

Bootloader - Loading assembly kernel, problems with memory addresses and copying data


Bootloader - Loading assembly kernel, problems with memory addresses and copying data

By : infamouswhiteknight
Date : November 17 2020, 11:58 AM
Hope that helps int 13/02 expects es:bx as input, but you only set up es, bx is uninitialized. You should set it to point where you want to load your data, careful not to overwrite your own boot sector. As such, mov bx, 512 sounds like a good idea.
Another problem is that your READSECTORS returns the final value of bx, that is the next available address and not the original load address.
code :
;;;
;;; Header information
;;;

BITS 16

ORG 0

START:  jmp MAIN

;;;
;;; Parameters
;;;

SectorsPerTrack     dw  18
HeadsPerTrack       dw  2
BytesPerSector      dw  512
DriveNumber         db  0

;;;
;;; Variables
;;;

absSector           db  0x00
absHead             db  0x00
absTrack            db  0x00

;;;
;;; Print function
;;; Input:
;;; - SI => Null terminated string
;;; Output:
;;; - None
;;;

PRINT:
    lodsb
    or al, al
    jz PRINTDONE
    mov ah, 0eh
    int 10h
    jmp PRINT
PRINTDONE:
    ret

;;;
;;; LBA to CHS
;;; Input:
;;; - AX => LBA address
;;; - SectorsPerTrack => Sectors per track
;;; - HeadsPerTrack => Heads per track
;;; Output:
;;; - absSector => CHS sector address
;;; - absHead => CHS head address
;;; - absTrack => CHS track address
;;;

LBACHS:
    xor dx, dx
    div WORD [SectorsPerTrack]
    inc dl
    mov BYTE [absSector], dl
    xor dx, dx
    div WORD [HeadsPerTrack]
    mov BYTE [absHead], dl
    mov BYTE [absTrack], al
    ret

;;;
;;; Read sectors
;;; Input:
;;; - CX => Number of sectors to read
;;; - AX => LBA address to start from
;;; Output:
;;; - ES:BX => Loaded sector address:offset
;;;

READSECTORS:
READSECTORSMAIN:
          mov     di, 0x0005
READSECTORSLOOP:
          push    ax
          push    bx
          push    cx
          call    LBACHS
          mov     ah, 0x02
          mov     al, 0x01
          mov     ch, BYTE [absTrack]
          mov     cl, BYTE [absSector]
          mov     dh, BYTE [absHead]
          mov     dl, BYTE [DriveNumber]
          int     0x13
          jnc     READSECTORSDONE
          xor     ax, ax
          int     0x13
          dec     di
          pop     cx
          pop     bx
          pop     ax
          jnz     READSECTORSLOOP
          int     0x18
READSECTORSDONE:
          pop     cx
          pop     bx
          pop     ax
          add     bx, WORD [BytesPerSector]
          inc     ax
          loop    READSECTORSMAIN
          ret

;;;
;;; Main section
;;;

MAIN:
    cli                 ; Move registers for offset of BIOS 0x07C0 load point
    mov ax, 0x07C0      ; OFFSET
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    mov ax, 0x0000      ; Initialize the stack
    mov ss, ax
    mov sp, 0xFFFF
    sti

    mov ax, 0x01        ; LBA number 1 for sector number 2
    mov cx, 0x01        ; Read one sector from the floppy disk
    mov bx, 0x200

    call READSECTORS    ; Call the read sectors function

    jmp 0x7e0:0

;;;
;;; Footer information
;;;

times 510-($-$$) db 0
dw 0xAA55
BITS 16

ORG 0

START:  jmp MAIN

message db 'Kernel Loaded', 13, 10, 0

PRINT:
    lodsb
    or al, al
    jz PRINTDONE
    mov ah, 0eh
    int 10h
    jmp PRINT
PRINTDONE:
    ret

MAIN:
    mov ax, cs
    mov ds, ax
    mov si, message
    call PRINT

    jmp $

times 512-($-$$) db 0


Share : facebook icon twitter icon
Assembly Bootloader Not Jumping to Kernel

Assembly Bootloader Not Jumping to Kernel


By : Rachel
Date : March 29 2020, 07:55 AM
Hope this helps At the risk of pointing out the obvious, your script doesn't copy the right file. In some places you refer to kernel.bin and in others kernel_2.bin. Try this instead:
code :
nasm bootloader_2.asm -f bin -o bootloader_2.bin 
nasm kernel_2.asm -f bin -o kernel_2.bin 
partcopy bootloader_2.bin bootloader_2.img 0d 511d 
partcopy kernel_2.bin kernel_2.img 0d 511d 
copy bootloader.img /b + kernel_2.img POS_2.img
read_kernel:
    mov si, loading_msg
    call print_string
    mov si, 0x0

    mov ax, 0x1000   ; setting up the address to read into
    mov es, ax       ; moving the value in to es
    xor bx, bx       ; clearing bx
    mov ah, 0x02     ; floppy function
    mov al, 1        ; read 1 sector
    mov ch, 0        ; cylinder
    mov cl, 2        ; sector to read
    mov dh, 0        ; head number
    mov dl, 0        ; drive number

    int 0x13         ; BIOS Interrupt Call

    jc read_kernel
    push es          ; either push the address and retf or use far jmp
    push bx          ;

    mov si, loading_sucess
    call print_string

    ; jmp  0x1000:0  ; alternative to push/retf is simple long jump

    retf
k_main:
    push cs          ; save the cs reg
    pop  ds          ; use as ds also

    mov si, welcome_msg
    call print_string

    .k_main_loop:

    jmp .k_main_loop
assembly bootloader won't jump to kernel

assembly bootloader won't jump to kernel


By : Cherif Sylla
Date : March 29 2020, 07:55 AM
this one helps. For high level languages there's lots of clues about what the programmer intended contained in the structure loops, how variable names were chosen, defines/enums, etc; and it's easy to write maintainable code without comments.
For assembly language there's no well chosen variable names and no variable types (e.g. ax doesn't tell the reader if it's a pointer to a string or a giraffe's height or ...), instructions often don't show the intent (e.g. lea might be used to multiply by a constant and might not be used to load an effective address), control flow is far more flexible (e.g. something like a do(condition1) { } while(condition2) is perfectly fine) and goto (both jmp and conditional branches like jc) are used a lot.
code :
;Memory Layout
;
; 0x009C:0x1000 = 0x000019C0 = stack top
; 0x07C0:0x0000 = 0x00007C00 = load address
; 0x0000:0x7E00 = 0x00007E00 = kernel address

%define STACK_SEGMENT      0x009C
%define STACK_TOP_OFFSET   0x1000
%define LOAD_SEGMENT       0x07C0
%define KERNEL_SEGMENT     0x0000
%define KERNEL_OFFSET      0x7E00

;_______________________________________________

;Entry point
;
;Input
; dl = BIOS boot device number

    mov ax, STACK_SEGMENT
    mov ss, ax
    mov sp, STACK_TOP_OFFSET
    mov ax, LOAD_SEGMENT
    mov ds, ax
    mov es, ax

;Reset disk system
;
;Note: This should be completely unnecessary. We know the BIOS
;      disk services are working correctly and don't need
;      to be reset because the BIOS just used it successfully
;      to load this code into memory.

    xor ah, ah            ;ah = BIOS "reset disk system" function number
    int 13h               ;Call BIOS disk services
    clc                   ;Unnecessary

;Display welcome message

    mov si, msg2
    call print

;Load kernel from disk
; dl = BIOS boot device number

    mov ah, 02h           ;ah = BIOS "read sectors" function number
    xor ax, ax            ;ax = KERNEL_SEGMENT
    mov es, ax
    mov bx, KERNEL_OFFSET ;es:bx = address to load kernel
    mov al, 1h            ;al = number of sectors to read
    mov ch, 0             ;ch = cylinder number for first sector
    mov cl, 2h            ;cl = sector number for first sector
    mov dh, 0             ;dh = head number for first sector
    int 13h               ;Call BIOS disk services

    jc error              ;Handle error if there was one

;Pass control to "kernel"

    jmp KERNEL_SEGMENT:KERNEL_OFFSET
                          ;ah = BIOS "read sectors" function number
                          ;ax = KERNEL_SEGMENT
How to compile a Rust kernel with an Assembly (NASM) bootloader

How to compile a Rust kernel with an Assembly (NASM) bootloader


By : user2624606
Date : March 29 2020, 07:55 AM
may help you . After a couple of hours I compiled the code.
The solution was (like Michael Petch suggested), to compile the assembly code into static .o files, then compile the rust code using xargo and a custom target. I compiled the Rust code as a static library, not a binary, so the output was a .a object file, and not an executable.
Bootloader is Not loading the kernel

Bootloader is Not loading the kernel


By : Jobish George
Date : March 29 2020, 07:55 AM
wish of those help If you intend on using the MIKEOS bootloader then you need to create a FAT12 formatted 1.44MiB floppy disk image to be compatible with the volume boot record (VBR) in boot.asm. You then need to copy your KERNEL.BIN into that image.
This process is quite simple with the mtools package. mtools has online documentation. This answer isn't a complete guide to using mtools but enough to do what you need to boot. If you are using Debian or a Debian based distro like Ubuntu you can install the mtools package as root user with:
code :
apt-get install mtools
mformat -f1440 -B boot.bin -C -i disk.img
mcopy -D o -i disk.img KERNEL.BIN ::/
mdir -i disk.img ::/
 Volume in drive : has no label
 Volume Serial Number is 6F67-C526
Directory for ::/

KERNEL   BIN        74 2019-12-20  13:25
        1 file                   74 bytes
                          1 457 152 bytes free
ORG 0x0000

start:
    mov ax, 0x2000             ; Mike OS'es bootloader loads us at 0x2000:0x0000
    mov ds, ax                 ; Set DS = ES = 0x2000
    mov es, ax

    ; Stack just below 0x2000:0x0000 starting at 0x1000:0x0000.
    ; First push will set SS:SP to 0x1000:0xfffe because SP will wrap.
    mov ax, 0x1000
    mov ss, ax
    xor sp, sp

    cld                        ; Clear Direction Flag (DF=0 is forward string movement)

    mov si, testCodeStr
    call print_string

    cli
.end_loop:
    hlt
    jmp .end_loop

testCodeStr: db 0x0d, 0x0a, "KERNEL.BIN loaded and running...", 0

; Function: print_string
;           Display a string to the console on display page 0
;
; Inputs:   SI = Offset of address to print
; Clobbers: AX, BX, SI

print_string:
    mov ah, 0x0e                ; BIOS tty Print
    xor bx, bx                  ; Set display page to 0 (BL)
    jmp .getch
.repeat:
    int 0x10                    ; print character
.getch:
    lodsb                       ; Get character from string
    test al,al                  ; Have we reached end of string?
    jnz .repeat                 ;     if not process next character
.end:
    ret
nasm -f bin boot.asm -o boot.bin
nasm -f bin kernel.asm -o KERNEL.BIN
mformat -f1440 -B boot.bin -C -i disk.img
mcopy -D o -i disk.img KERNEL.BIN ::/

qemu-system-i386 -fda disk.img
mov ah, 9      ; Unclear what you were trying to do here?
mov bl, 9
mov cx, 11
int 10h        ; ????
[bits 16]
ORG 0x0000

   mov ax, 0x2000 ; set up segments to match CS=0x2000
   mov ds, ax
   mov es, ax
   xor ax, ax
   mov ss, ax     ; set stack segment to 0x0000
   mov sp, 0x7C00 ; stack grows downwards from 0x0000:0x7C00
   cld            ; Clear Direction Flag (DF=0 is forward string movement)    

   mov ah, 9      ; Unclear what you were trying to do here?
   mov bl, 9
   mov cx, 11
   int 10h        ; ????

   mov si, boota  ; Print boot message
   call print_string
   [snip]
cmd_shut db 'shutdown', 0
Call C kernel from assembly bootloader

Call C kernel from assembly bootloader


By : Shannon
Date : March 29 2020, 07:55 AM
Hope that helps Oh, this definitely works. It just seems to be your knowledge of C, really.
I changed around your C kernel a bit and got this:
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org