I've recently all but completed my bootloader. I successfully managed to enable the A20 line, load the GDT (excluding interrupts, which I'll be working on as soon as I resolve this), and enabled PMode. However, I don't think that my data segment is set properly, and I can't find the cause.
I draw this from the fact that a method I use for printing to the screen in PMode (which worked when my bootloader was only single-stage) is no longer working, and it depends on the DS register.
I need to resolve this before I add in my kernel, to prevent all sorts of problems from appearing, but I can't find the cause (or, what I'm doing wrong, perhaps). For reference, I use the ISO9660 Filesystem, compile ASM in NASM, use a 2-stage bootloader, create my ISOs in MagicISO, and virtualize in VirtualBox.
This applies specifically to NASM in PMode, of course.
And here's the code:
Stage One:
Code: Select all
BITS 16 ; 16-bit mode
ORG 0x7C00 ; Bootloader loads at 0x7C00 in memory
%include "iso9660fs.inc" ; Filesystem data
JMP start ; Jumps to main method
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Bootloader entry point. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
start:
MOV SI, msg_booting
CALL print
CLI ; CLears Interrupts
XOR AX, AX ; Reset AX
MOV DS, AX ; Set DS to 0Ah (AX default?)
MOV SP, AX ; Same with SP
MOV AX, 9000h ; Set AX to 9000h
MOV SS, AX ; Setting our stack
MOV AX, 1000h ; Set AX to 1000h
MOV ES, AX ; Setting a different segment
STI ; STarts Interrupts
MOV BYTE[bootdrv], DL ; Our bootdrive
CALL load_vol_desc ; Loads the volume data
TEST AX, AX ; Test AX
JZ .err_read ; Jumps to error code if AX == 0
CALL load_root_dir ; Loads root directory of CD
TEST AX, AX ; Test AX
JZ .err_read ; Jumps to error code if AX == 0
CALL load_loader ; Loads the loader image
TEST AX, AX ; Test AX
JZ .err_no_loader ; Jumps to error code if AX == 0
MOV DL, BYTE[bootdrv] ; Restores our bootdrive
MOV SI, msg_jumping ; Sets jumping msg to print
CALL print ; Prints message.
JMP 1000h:0000h ; Jumps to the loaded boot image
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Called if the image file couldn't be found. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.err_no_loader:
MOV SI, msg_no_loader ; Set up to print error
CALL print ; Prints the error
JMP hang ; Hangs the system
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Called if an error occured while reading the disc. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.err_read:
MOV SI, msg_read ; Set up to print error
CALL print ; Prints the error
JMP hang ; Hangs the system
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Loads the volume's data, in preparation for image loading. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
load_vol_desc:
.loop:
MOV SI, dap ; Loads disc data from dap function
MOV AH, 42h ; Size of the currently looped file
MOV DL, [bootdrv] ; Bootdrive loaded
INT 13h ; Interrupt for LBA
JC .end_err ; Error if carry flag is set by INT 13h
CMP BYTE[ES:0],1 ; Primary descriptor?
JE .end ; If yes, end the search
CMP BYTE[ES:0], 0ffh ; Last descriptor?
JE .end_err ; If yes, call an error.
INC DWORD[dap.lba] ; Grabs the next LBA to check.
JMP .loop ; Try again!
.end:
MOV AX, 1 ; Ensures that AX checks will fail
RET ; Return
.end_err:
XOR AX, AX ; Resets AX (checks will succeed)
RET ; Return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Loads the root dir. Uses the data in iso9660fs.inc for this. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
load_root_dir:
MOV SI, iso9660vol_desc.root_dir_rec ; Loads root data
MOV EAX, DWORD[ES:SI+iso9660dir_desc.loc_data]
MOV ECX, DWORD[ES:SI+iso9660dir_desc.data_len] ; Loads root dir length
SHR ECX, 11 ; Length of root dir / 2048 to get block # (right bit shift) TODO
ADD CX, 1 ; Not entirely sure what's happening here. TODO
CALL load_lba ; Gets the LBA for the root block
.end:
RET ; Return on ending (hopefully with AX == 1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Loads the LBA for the root dir. ;
; Note: EAX is the LBA we are on. ;
; CX is the number of LBAs to load for the root directory. ;
; Defined in load_root_dir. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
load_lba:
PUSHA ; Save AH register
MOV [dap.blocks], CX ; Saves the number of blocks to load.
MOV [dap.lba], EAX ; Saves the LBA we will load.
MOV SI, dap ; Loads the data
MOV AH, 42h ; Size of the currently looped block/lba
MOV DL, [bootdrv] ; Creates boot drive data
INT 13h ; Interrupt, we now have the LBA.
JC .end_err ; Error if carry flag is set
.end:
POPA ; Restores AH register
MOV AX, 1 ; FUQ DA ERRORS
RET ; Return
.end_err:
POPA ; Restores AH register
XOR AX, AX ; CONFOUND THESE BUGS
RET ; Return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Loads the image file. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
load_loader:
XOR SI, SI ; Restore SI
SHL ECX, 11 ; Gets size of root dir
.loop_search:
CMP BYTE[ES:SI], 0 ; Checks if is root dir entry
JE .end_err ; If not, error.
CMP SI, CX ; Checks if SI == root dir size
JE .end_err ; If yes, error.
MOV DI, SI ; Saves DI
ADD DI, iso9660dir_desc.file_ident ; Points DI to file name/address
PUSH CX ; Save CX
PUSH SI ; Save SI
MOVZX CX, BYTE[ES:SI+iso9660dir_desc.len_file_ident] ; Size of file ID
MOV SI, os_img ; Load our image name
REP CMPSB ; Compares string bytes until CX == 0
MOV AL, [SI] ; If strings equal, AL == 0
POP SI ; Restore SI
POP CX ; Restore CX
TEST AL, AL ; Tests if AL is 0
JZ .load ; If so, load the file
MOVZX AX, BYTE[ES:SI+iso9660dir_desc.len_dir_rec] ; Size of directory
ADD SI, AX ; Make SI point to next entry
JMP .loop_search ; Search again
.load:
MOV EAX, DWORD[ES:SI+iso9660dir_desc.loc_data] ; LBA of file
MOV ECX, DWORD[ES:SI+iso9660dir_desc.data_len] ; Length of file
SHR ECX, 11 ; Switch back to 16-bit
INC CX ; Increment in case we miss a block
CALL load_lba ; Reload all LBAs
.end:
RET ; File loaded into memory - return
.end_err:
XOR AX, AX ; Null AX
RET ; Return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; DAP stuff. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
align 4 ; 4 bits
dap:
.size DB 10h ; Disc size I believe
.reserved DB 0 ; Reserved sectors/blocks
.blocks DW 1 ; Number of blocks
.offset DW 0 ; Offset at beginning of search
.segment DW 1000h ; Segments in disc
.lba DD 10h, 0 ; Number of LBAs
bootdrv DB 0 ; Our drive number
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Stage1 subroutines. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Prints a string to the screen. ;
; Takes the value of SI to do this. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
print: ; Routine: output string in SI to screen
PUSHA ; Saves value of AH
MOV AH, 0Eh ; int 10h 'print char' function
.repeat:
LODSB ; Get character from string
CMP AL, 0
JE .done ; If char is zero, end of string
INT 10h ; Otherwise, print it
JMP .repeat
.done:
POPA ; Loads AH back into memory
RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Hangs the system. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hang:
CLI
HLT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Stage1 declarations. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
msg_booting DB 'ZidOS is booting...',13,10,0
msg_jumping DB 'Preparing to jump to Stage2.',13,10,0
msg_no_loader DB 'Could not find the Stage2 image.',13,10,0
msg_read DB 'Could not read the disc.',13,10,0
os_img DB 'BOOTTWO.BIN',0
TIMES 510-($-$$) DB 0
DW 0AA55hCode: Select all
[BITS 16] ; Working in 16-bit mode
PUSH CS
POP DS ; Reloads DS register because it seems to have been messed up in Stage1
MOV SI, boot_msg ; Sets up to print the boot message
CALL print ; Prints the contents of SI
CALL enable_a20 ; Enables A20 for OVER 4GB of RAM!
MOV SI, pmode_msg ; Sets up to print the Entering PMode message
CALL print ; Prints the contents of SI
CLI ; Clears those pesky interrupts for GDT!
XOR AX, AX ; Logical OR (basically resets, I think?)
MOV DS, AX ; Loads DS register
lgdt[gdt_desc] ; Loads the GDT.
MOV EAX, CR0 ; Setting up PMode, dumps CR0 into EAX
OR EAX, 1 ; Sets bit 0 on CR0
MOV CR0, EAX ; Dumps EAX into CR0, with bits set
JMP 08h:clear_pipe ; Jumps to clear 16-bit instructions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; STAGE2 SUBROUTINES ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; GDT section. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
gdt: ; Address for the GDT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Null segment, reserved by Intel. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
gdt_null: ; Null Segment
dd 0
dd 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Code segment, read and execute, nonconforming. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
gdt_code:
dw 0FFFFh
dw 0
db 0
db 10011010b
db 11001111b
db 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Data segment, read and write, expand down. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
gdt_data:
dw 0FFFFh
dw 0
db 0
db 10010010b
db 11001111b
db 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Calculates the GDT size. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
gdt_end:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; GDT descriptor. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
gdt_desc:
dw gdt_end - gdt - 1 ; Limit (size)
dd gdt ; Address of the GDT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Prints a string to the screen. ;
; Takes the value of SI to do this. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
print: ; Routine: output string in SI to screen
mov ah, 0Eh ; int 10h 'print char' function
.repeat:
lodsb ; Get character from string
cmp al, 0
je .done ; If char is zero, end of string
int 10h ; Otherwise, print it
jmp .repeat
.done:
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Sets up the A20 line for up to 4GB of memory. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
enable_a20:
cli ;Disables interrupts
push ax ;Saves AX
mov al, 0xdd ;Look at the command list
out 0x64, al ;Command Register
pop ax ;Restore's AX
sti ;Enables interrupts
mov si, a20_msg
call print
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 16-bit Declarations. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
boot_msg DB 'Stage Two Loaded.',13,10,0 ; Boot message
pmode_msg DB 'Now entering Protected Mode...',13,10,0 ; PMode message
a20_msg DB 'Address line active.',13,10,0 ; A20 active
[BITS 32] ; Enter 32-bit mode.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Clears previous 16-bit instructions in PMode. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
clear_pipe:
MOV AX, 10h ; Sets AX for segments
MOV DS, AX ; Sets data segment
MOV SS, AX ; Sets stack segment
MOV ESP, 090000h ; Sets our stack
MOV BYTE [DS:0B8000h], 'P'
MOV BYTE [DS:0B8001h], 1Bh
JMP hang
hang:
JMP hang


