Not sure if the code is correct, can anyone check it?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Not sure if the code is correct, can anyone check it?

Post by Roman »

Will that code load the first 512 bytes of the first floppy to 0x1000?

Code: Select all

	section .text
	use16
	org 0x7C00  ; Load addr
start:
	mov ax, cs
	mov ds, ax ; Select data segment.

	mov ah, 1 ; Turn on the cursor.
	int 0x10

	mov ah, 00h ; Set video mode.
	mov al, 2
	int 0x10

	mov ah, 13h ; Notify about booloader start.
	mov al, 1
	mov bh, 0
	mov bl, 0_7h
	mov cx, start_msg_len
	mov dh, 0
	mov dl, 0
	mov bp, start_msg
	int 0x10

	mov bl, 0_2h ; Write welcome message.
	mov dl, start_msg_len + 1
	mov cx, welcome_msg_len
	mov bp, welcome_msg
	int 0x10

	mov bl, 0_7h ; Notify about next stage loading.
	mov dh, 2
	mov dl, 0
	mov cx, load_kern_msg_len
	mov bp, load_kern_msg
	int 0x10

	mov ah, 0 ; Let's reset floppy's controller!
	mov dl, 0
	int 0x13
	jc error

	mov ah, 2
	mov al, 1
	mov ch, 1
	mov cl, 1
	mov dh, 1
	mov ebx, 0x1000
	mov dl, 0
	int 0x13
	jc error

	jmp 0x1000

	hlt

start_msg db "SimpleX bootloader started."

start_msg_len equ $ - start_msg


welcome_msg db "Welcome!"

welcome_msg_len equ $ - welcome_msg


load_kern_msg db "Loading next stage..."

load_kern_msg_len equ $ - load_kern_msg


error:
	mov ah, 00h
	mov al, 2
	int 0x10
	
	mov ah, 13h
	mov al, 1
	mov bh, 0
	mov bl, 0_4h
	mov cx, error_msg_len
	mov dh, 0
	mov dl, 0
	mov bp, error_msg
	int 0x10
	hlt

error_msg db "Critical error occured! Cannot continue."

error_msg_len equ $ - error_msg


finish:
	times 0x1FE-finish+start db 0
	db   0x55, 0xAA ; Boot signature
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: Not sure if the code is correct, can anyone check it?

Post by Antti »

Roman wrote:

Code: Select all

   mov ah, 2
   mov al, 1
   mov ch, 1
   mov cl, 1
   mov dh, 1
   mov ebx, 0x1000
   mov dl, 0
   int 0x13
The simple answer is no but I do not think it is the only important thing here. You are reading the first sector (1) from head 1 and cylinder 1. What you want to read is the first sector (1) from head 0 and cylinder 0. Note that sectors start from 1.

There are more important things than that at this point, like setting the stack, not assuming cs is zero and the fact that "hlt" does not stop the execution (a halt is released when an interrupt occurs).
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Not sure if the code is correct, can anyone check it?

Post by bwat »

What Antii wrote plus you can do what you state you want to do without using BIOS calls. Your code - if it's in the boot block - is in the first 512 bytes of the floppy* so you can just copy that from where the bootstrap loader loaded it to 0x1000 but that's not really what you wanted eh? :)

*) Yeah, I'm assuming your booting from the floppy here!
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Re: Not sure if the code is correct, can anyone check it?

Post by Roman »

bwat wrote:What Antii wrote plus you can do what you state you want to do without using BIOS calls. Your code - if it's in the boot block - is in the first 512 bytes of the floppy* so you can just copy that from where the bootstrap loader loaded it to 0x1000 but that's not really what you wanted eh? :)

*) Yeah, I'm assuming your booting from the floppy here!
Could you explain, how can I jump to second stage of my boot loader?

That's my boot.S:

Code: Select all

	section .text
	use16
	org 0x7C00  ; Load addr
start:
	mov ax, cs
	mov ds, ax ; Select data segment.

	mov ah, 1 ; Turn on the cursor.
	int 0x10

	mov ah, 00h ; Set video mode.
	mov al, 2
	int 0x10

	mov ah, 13h ; Notify about booloader start.
	mov al, 1
	mov bh, 0
	mov bl, 0_7h
	mov cx, start_msg_len
	mov dh, 0
	mov dl, 0
	mov bp, start_msg
	int 0x10

	mov bl, 0_2h ; Write welcome message.
	mov dl, start_msg_len + 1
	mov cx, welcome_msg_len
	mov bp, welcome_msg
	int 0x10

	mov bl, 0_7h ; Notify about next stage loading.
	mov dh, 2
	mov dl, 0
	mov cx, next_stg_msg_len
	mov bp, next_stg_msg
	int 0x10

	mov ah, 0 ; Let's reset floppy's controller!
	mov dl, 0
	int 0x13
	jc error

	mov ah, 2
	mov al, 1
	mov ch, 0
	mov cl, 2
	mov dh, 0
	mov ebx, 0x1000
	mov dl, 0
	int 0x13
	jc error

	jmp 0x1000

start_msg db "SimpleX bootloader started."

start_msg_len equ $ - start_msg


welcome_msg db "Welcome!"

welcome_msg_len equ $ - welcome_msg


next_stg_msg db "Loading next stage..."

next_stg_msg_len equ $ - next_stg_msg


error:
	mov ah, 00h
	mov al, 2
	int 0x10
	
	mov ah, 13h
	mov al, 1
	mov bh, 0
	mov bl, 0_4h
	mov cx, error_msg_len
	mov dh, 0
	mov dl, 0
	mov bp, error_msg
	int 0x10

	cli
	hlt

error_msg db "Critical error occured! Cannot continue."

error_msg_len equ $ - error_msg


finish:
	times 0x1FE-finish+start db 0
	db   0x55, 0xAA ; Boot signature
And my boot2.S:

Code: Select all

    section .text
    use16
    org 0x1000
start:
    mov ah, 00h ; Reset screen.
    mov al, 2
    int 0x10

    cli
    hlt
I've used dd to write that both binaries. (dd if=/dev/zero of=boot.img bs=1024 count=1440 && dd if=boot.bin of=boot.img conv=notrunc && dd if=boot2.bin of=boot.img conv=notrunc seek=512)
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Not sure if the code is correct, can anyone check it?

Post by bwat »

Ask yourself what sector you want to load. You say you want to load the very first sector but it look like that's where boot.bin is. Do you not want to load the second sector?

I do something similar with this kernel. Look at the files build.x86/mbr.S (my boot loader that loads from the second sector on the disk) and build.x86/Makefile (where I dd the binary to the harddisk).

Edit: Just noticed my code has the hardcoded definition of 64 sectors per track in the routine load_sector. I actually get the actual number of sector per track from get_disk_geometry but I ignore it!!!
Every universe of discourse has its logical structure --- S. K. Langer.
Post Reply