Page 1 of 1

bootloader works in VirtualBox, but not on real machine

Posted: Sat Oct 22, 2011 3:32 pm
by pirateofserbia
Hi all,

I`ve written some bootloader which should enable a20, set gdt table, enter protected mode, etc. The problem is that it works in VirtualBox, but on real machine in starts, but at the point it should load my kernel, it just breaks...

Here is look of my bootloader:

Image

Uploaded with ImageShack.us

Sorry it`s not on eng, but the red marked string simple wont show up on real machine...
The thing is that I first write string "enabling a20 line" (1st string) and after that i write DONE, now that works, but when I try to write loading kernel... it wont write it, but after trying to load kernel he writes DONE...

heare is code 4 my bootloader

Code: Select all

;defines
%DEFINE kernelAddress 0x1000
%DEFINE kernelSector 3	;1st is bootloader, 2nd is filetable
%DEFINE drive 0x80		;HDD

[BITS 16]				;16bit code
[ORG 0x7C00]			;memory adress where the code will be loaded




;copy content of CS to DS and ES
MOV AX, CS
MOV DS, AX
MOV ES, AX

;set video mode to 80 x 25, 16 colors
MOV AL,03h
MOV AH,0
INT 10h	

;print welcome message
MOV SI, WelcomeMsg		;string pointer goes to SI
CALL strPrint			;print string procedure

MOV SI, A20			;string pointer goes to SI
CALL strPrint		;print string procedure

;Let BIOS enable A20
in al, 0x92
or al, 2
out 0x92, al
		
CALL printDone


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;THIS PART IS NOT PRINTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

MOV SI, loadSectorStr	;string pointer goes to SI
CALL strPrint			;print string procedure

;
;main()
;
resetDrive:
	MOV AH, 0           ;reset command
	INT 13h             ;call interrupt 13h
	OR AH, AH           ;check for error code
	JNZ resetDrive		;try again if ah != 0
	
;;;;;;;;;;;;;;;;;;;
;load to kernel
MOV AX, 0
MOV ES, AX
MOV CL, kernelSector	;sector to be loaded
MOV AL, 30	 			;!!!important number of sectors

CALL loadSector

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;BUT THIS PART IS PRINTED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

CALL printDone
;JMP kernelAddress:0000 	;go to kernel

MOV SI, plzWait			;string pointer goes to SI
CALL strPrint			;print string procedure
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	
;read keyboard scancode (blocking) 
MOV AH, 0
INT 16h

;start protected mode
CLI						;disable interupts
XOR AX, AX				;AX = 0
MOV DS, AX              ;DS = 0 - needed for lgdt

LGDT [gdtDescriptor]    ;load the GDT descriptor

;set bit0 into CR0
MOV EAX, CR0            
OR EAX, 1               
MOV CR0, EAX     


JMP 08h:clearPipe      ;clear pipe from 16bit instructions by going to code segment by specified offset
	   

loadSector:
	;MOV BX,0			;offset from ES
	MOV BX, kernelAddress
	MOV DL, drive 		;drive
	MOV DH, 0 			;head
	MOV CH, 0 			;track
	
	;read floppy/hard disk in CHS mode
	MOV AH, 2
	INT 0x13
	JC exception
	RET

;write error string if something went wrong, and reset drive
exception:
	MOV SI, errStr
	CALL strPrint
	JMP resetDrive
	


;
;printing to screen
;	
charPrint:				;character print
	MOV AH, 0x0E		;inform BIOS that 1 character willbe printed
	MOV BH, 0x00		;page number
	MOV BL, 0x0F		;color
	INT 0x10			;video interupt
	RET					;go back to calling procedure

strPrint:				;string print
nextChar:				;loop			
	MOV AL, [SI]		;take byte from SI and store in AL
	INC SI				;increment pointer in SI
	OR AL, AL			;if(AL == 0)
	JZ exit				;go to exit - end of function
	CALL charPrint		;print character
	JMP nextChar		;do all again
	exit:
	RET
	
;
;print DONE in green color
;
printDone:
MOV SI, done
call strPrint
RET


;data - 10=line feed, 13=carriage return - 10,13 = \n
WelcomeMsg db 'CSL@TFC bootloader v0.1', 10, 13, 10, 13, 0
plzWait db 10, 13, 'Pritisnite bilo koji taster za startovanje 32-bitnog moda i jezgra OS-a...', 10, 13, 0
errStr db 'Greska pri ucitavanju sektora.', 10, 13, 0
A20 db 'Omogucavanje maksimalne velicine memorije na 4GB ........................ ', 0
loadSectorStr db 'Ucitavanje jezgra ....................................................... ', 0
done db 'GOTOVO', 0

;GDT - goes between code and AA55 signature
[BITS 32]				;32-bit instructions
clearPipe:
	MOV AX, 10h         ;Save data segment identifyer
	MOV DS, AX          ;move a valid data segment into the data segment register
	MOV SS, AX          ;move a valid data segment into the stack segment register
	MOV ESP, 090000h    ;move the stack pointer to 090000h


	
JMP 08h:01000h


;JMP $					;jump to here - infinite loop
;instead of JMP $(eats cpu time) we use cli(already activated) and hlt
;cli  ; stop interrupts
here: 
	hlt
	jmp here

;address of the GDT start
gdtStart:				

;null segment reserved by intel, size of Qword
dq 0

;code segment, read/execute, non-conforming
dw 0FFFFh
dw 0
db 0
db 10011010b
db 11001111b
db 0

;data segment, read/write, expand down
dw 0FFFFh
dw 0
db 0
db 10010010b
db 11001111b
db 0

;address of the GDT end
gdtEnd:						



gdtDescriptor:					;GDT descriptor
dw gdtEnd - gdtStart - 1	;size
dd gdtStart              	;address of the GDT


TIMES 510 - ($ - $$) db 0	;fill rest with 0
DW 0xAA55					;signature that this is bootloader
So I guess that the problem is loading kernel... I tried this on 64bit CPU, could that be the reason (I currently doesn`t have 32bit machine to test it). And i`ve been loading from USB, just to let u know...

Thanks

Re: bootloader works in VirtualBox, but not on real machine

Posted: Sat Oct 22, 2011 5:05 pm
by mark3094
It may possibly be the way you are enabling A20. I suspect this, as it is the last thing you do before things go haywire.

Check out this link:
http://www.brokenthorn.com/Resources/OSDev9.html

Under the section 'Gate A20 - Enabling', there are several methods to enable A20. You appear to be using 'method1', where you use the system control port to enable A20. Note that the author says that while it is easy, it may conflict with some hardware devices.

Try using 'method 3', which is the keyboard controller method.

Re: bootloader works in VirtualBox, but not on real machine

Posted: Sun Oct 23, 2011 2:45 am
by pirateofserbia
tnx allot, it was problem with A20. I set up some stack, used keyboard method to enable it, and now it works :)
But, now after entering protected mode, screen blinks :) I guess this has nothing to do with A20...

In my kernel i just change values in mem at address 0xB8000, and in bootloader i`ve set video mode to
80 x 25, 16 colors

Code: Select all

MOV AL,03h
MOV AH,0
INT 10h	
Is there something i`m missing???

once again thanks for A20 tip.

Re: bootloader works in VirtualBox, but not on real machine

Posted: Sun Oct 23, 2011 2:48 pm
by mark3094
Not too sure myself, as I don't change the video mode.
Try leaving it as default, and see what happens.

Re: bootloader works in VirtualBox, but not on real machine

Posted: Sun Oct 23, 2011 3:22 pm
by neon
Hello,

The blink bit in the attribute mode control register might be enabled. It can be disabled by using INT 0x10 function 0x1003. Alternatively, dont use 0xF for your characters color attribute.

Re: bootloader works in VirtualBox, but not on real machine

Posted: Mon Oct 24, 2011 7:56 am
by pirateofserbia
Tnx, I will try it tonight, and I`ll let you know... Now I have some other bug, but i`ll post it in another thread, cuz this has nothing to do with bootloader...

Re: bootloader works in VirtualBox, but not on real machine

Posted: Mon Oct 24, 2011 10:29 am
by pirateofserbia
It works, thank you very much.