AlpacaSolitario wrote:Hi to everyone,
i'm trying to write a two stage bootloader, but I have some problem with the loading of the second stage in memory.
This is the code of the first stage of bootloader (the labels LABEL_RESET_FLOPPY and LABEL_READ_STAGE_UNO are the ones used to read from the floppy drive):
Your actual error is further below, but I will make comments here as well.
Code: Select all
.org 0x0
.text
.code16
.globl _start
_start:
;; this is not needed. Neither the 'cli' nor the jmp. First of all, there is no need
;; to clear interrupts. If your BIOS (the only thing that can interrupt you at the moment)
;; doesn't restore the registers correctly, there is no need to continue further.
;; Second, a relative short jump does nothing for the boot code. In fact, the
;; only time I ever have seen that it is needed was with a specific BIOS manufacturer
;; and version. They would check that the first instruction was indeed a jump.
LABEL_INIT: cli
jmp LABEL_STAGE_ZERO
LABEL_STAGE_ZERO:
#Set %ds %es %ss %si to 0
xor %ax,%ax
mov %ax,%ds
mov %ax,%es
mov %ax,%ss
mov %ax,%si
#Set stackpoint to 0x7c00
movw $0x7c00,%sp
sti
;; again, no need for the cli/sti. The processor will not allow an interrupt
;; after the setting of 'ss' between it and the next instruction, usually the
;; setting of 'sp'.
#Here the floppy drive is resetted
LABEL_RESET_FLOPPY:
;; The need for this is debatable. The BIOS has already read your sector to
;; 0x07C00 so the disk system obviously works okay, so no need to reset it
;; However, there really isn't anything wrong with doing so.
mov $0x00,%ah
mov $0x00,%dl
int $0x13
jc LABEL_RESET_FLOPPY
;; This is your error:
;; There is still BIOS stuff at 0x00500. If you load your second stage to 0x00500,
;; you have overwritten necessary data that the BIOS needs to use for disk and
;; other operations.
#Set es to 0x50 and bx to 0x0
#The second stage will be loaded at 0x500
mov $0x50,%ax
mov %ax,%es
xor %bx,%bx
LABEL_READ_STAGE_UNO:
#Read the second sectory of the floppy disc
mov $0x02,%ah
mov $1,%al #number of sector to read
mov $2,%ch #cylinder number
mov $2,%cl #sector number
mov $0,%dh #head number
mov $0,%dl
int $0x13
#If there is an error try again
jc LABEL_READ_STAGE_UNO
LABEL_NEXT_STAGE:
#Jump to the second stage
ljmp $0x50,$0x0
The best way to find out what is going on is to run it through an emulator with the debugger set. For example, Bochs will let you single step through instructions. It will even let you break at a known instruction offset, say 0x07C00, then single step through the code (skipping the BIOS calls with other break codes).
Ben
-
http://www.fysnet.net/osdesign_book_series.htm