Page 1 of 1

Boot loader does not load more than 52 Kb

Posted: Tue Jul 03, 2012 7:48 am
by AlexanderSilvaB
Hi, i'm having a problem with my Operational System, I have the boot loader that load a file called SET32 that basically set the 32 bit mode, active a20 keyboard and load the 32 bit kernel. The problem is that if the kernel file have more than 52 Kb of size, it is never loaded and the system stop. Please, help me if you can.

Here is the code of SET32 file.

Code: Select all

SET32 - Souce code
-----------------------------

; *********************************************
; Carregador 32 bit
; Lemon X OS
; *********************************************

; Esse arquivo irá carregar o kernel de 32 bits

bits 16 								; Estaremos no modo real

org 0x500

jmp principal

; -------------------------------------
; Diretivas do processador
; -------------------------------------


%include "stdio.inc"					; Rotinas i/o basicas
%include "Gdt.inc"						; Rotinas Gdt
%include "A20.inc"						; A20
%include "Fat12.inc"                    ; Drive FAT12
%include "common.inc"   

; -------------------------------------
; Secao de dados
; -------------------------------------


msgCarregando db 0x0D, 0x0A, "Prepando para carregar o sistema...", 0x00
msgFalha db 0x0D, 0x0A, "Erro! Kernel inexistente ou corrompido. Pressione uma tecla para reiniciar.", 0x0D, 0x0A, 0x0A, 0x00
msgPergunta db 0x0D, 0x0A, "Deseja habilitar o modo grafico? 1 = SIM", 0x00

setVideo:
		mov ah, 00h
		mov al, 13h
		int 10h
		;mov ax, 4f02h
		;mov bx, 101h
		;int 10h
		ret

principal:
	cli									; Limpar interruptores
	xor ax, ax							; Segmentos nulos
	mov ds, ax
	mov es, ax
	mov ax, 0x9000						; Pilha comecando em 0x9000-0xffff
	mov ss, ax
	mov sp, 0xFFFF
	sti
	
	; Instalando Gdt
	call InstallGDT
	
	; Ativar A20
	
	call EnableA20_KKbrd_Out
	
	; Mensagem
	mov si, msgCarregando
	call Puts16
	
	;-------------------------------;
	; Iniciando sistema de arquivos ;
	;-------------------------------;

	call    LoadRoot                ; Carregando tabela root

	;-------------------------------;
	; Carregando Kernel              ;
	;-------------------------------;

	mov     ebx, 0                  
	mov     bp, IMAGE_RMODE_BASE
	mov     si, ImageName           ; Arquivo a ser carregado
	call    LoadFile                ; Carregando arquivo
	mov     dword [ImageSize], ecx  ; salvando tamanho do kernel
	cmp     ax, 0                   ; Teste de sucesso
	je      EntrarEstagio3          ; Sim - entre em estagio 3
	mov     si, msgFalha            ; Nao - Mostrar msg erro
	call    Puts16
	mov     ah, 0
	int     0x16                    ; Esperar tecla
	jmp 0FFFFh:0000h                ; reiniciar
	cli                             
	hlt

	
	; Ir a modo protegido
	EntrarEstagio3:
		mov si, msgPergunta
		call Puts16
		mov ah, 00h
		int 16h
		push ax
		mov ah, 0eh
		int 10h
		pop ax
		cmp al, '1'
		jnz pularSetVideo
		call setVideo
		pularSetVideo:
		cli									; Limpar interruptores
		mov eax, cr0						; Colocar 0 em cr0 -- Entrar em modo protegido
		or eax, 1
		mov cr0, eax
		
		jmp CODE_DESC:Estagio3					;Pulo longo para consertar Cs. Lembre-se que o seletor de codigo e 0x8
	
	; Nota: Não reabilitar interruptores
	; Isso sera feito no estagio 32
	
; ---------------------------
; Estagio 3
; ---------------------------
	
; Iniciando sistema de 32 bits
	
bits 32

Estagio3:
	; Setar registradores
	mov ax, DATA_DESC						; Setar segmentos de dados para seletor de dados 0x10
	mov ds, ax
	mov ss, ax
	mov es, ax
	mov esp, 90000h						; Iniciando pilha em 90000h
	

	call ClrScr32

	;-------------------------------;
	; Copiar kernel para 1MB            ;
	;-------------------------------;

	CopiarImagem:
			 mov    eax, dword [ImageSize]
			 movzx  ebx, word [bpbBytesPerSector]
			 mul    ebx
			 mov    ebx, 4
			 div    ebx
			 cld
			 mov    esi, IMAGE_RMODE_BASE
			 mov    edi, IMAGE_PMODE_BASE
			 mov    ecx, eax
			 rep    movsd                  ; copiar imagem ao endereco protegido

			;---------------------------------------;
			;   Executar Kernel                      ;
			;---------------------------------------;

			jmp     CODE_DESC:IMAGE_PMODE_BASE; Ir para Kernel
	
	; Parar
	
	cli
	hlt								; Retornar


Cammon.inc - Source code
---------------------


%ifndef _COMMON_INC_INCLUDED
%define _COMMON_INC_INCLUDED

; where the kernel is to be loaded to in protected mode
%define IMAGE_PMODE_BASE 0x100000

; where the kernel is to be loaded to in real mode
%define IMAGE_RMODE_BASE 0x3000

; kernel name (Must be 11 bytes)
ImageName     db "KERNEL  LMS"

; size of kernel image in bytes
ImageSize     dw 0

%endif
--------------------------------------------------------------------
Thank you.


edit:
added code tags
JAAman

Re: Boot loader does not load more than 52 Kb

Posted: Tue Jul 03, 2012 7:55 am
by Combuster
Code tags, please?

Re: Boot loader does not load more than 52 Kb

Posted: Tue Jul 03, 2012 8:07 am
by Solar
Commenting in English is also a nice feat.

Re: Boot loader does not load more than 52 Kb

Posted: Tue Jul 03, 2012 8:27 am
by JAAman
i added code tags for you, this time, but please use them yourself in the future

Re: Boot loader does not load more than 52 Kb

Posted: Tue Jul 03, 2012 10:29 am
by AlexanderSilvaB
Sorry for not posting the code with comments in English. It is that this is not my native language, so I just copied my code, so a thousand apologies. The source code above is from the Stage2 of Operating System Development Series (http://www.brokenthorn.com/Resources/OSDevIndex.html). Thank you.

Re: Boot loader does not load more than 52 Kb

Posted: Tue Jul 03, 2012 11:34 am
by Griwes
The obvious reason seems to be that after loading 52 KiB of data using interrupts, you tell BIOS "load my data above the highest address you can address". AFAIR, this limit varies between BIOSes. The solution is to load it chunk-by-chunk, jumping between real mode and protected mode, and copying it at some higher address, where it will be used after final jump to protected mode.

Re: Boot loader does not load more than 52 Kb

Posted: Tue Jul 03, 2012 11:43 am
by AlexanderSilvaB
Thank Griwes, I understood what he meant. I decided to use Grub as my boot loader, so my system can run in Multiboot ... But anyway thank you guys ...

Re: Boot loader does not load more than 52 Kb

Posted: Tue Jul 03, 2012 11:52 am
by egos
- Move even value into sp.
- Don't destroy EBDA.

Look for details:
Loading Large Kernel's
Bootsector problem(need help)
Problems with a beginner bootloader.

- Use well done stage 1.

If you use FAT12 on the floppy (image) 2x80x18 you can take my old boot loader for the floppy. It can load two files starting from address 8000h and up to top of available base memory (~600 kb).

Re: Boot loader does not load more than 52 Kb

Posted: Tue Jul 03, 2012 12:09 pm
by AlexanderSilvaB
Thank you egos... :D