moving bootsector & loading kernel

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
aladdin

moving bootsector & loading kernel

Post by aladdin »

since now i was able to load my kernel at 0x9000, but now the kernel is too big and it overwrite the video ram (at 0xa000)
so i decide to move my bootsector to 0x9000 at boot time, then load kernel at 0x1000.
but the kernel crashs, I thik it's coze of a wrong value in ds or es

here is the first part of my boot sector code

Code: Select all

[ORG 0x7C00]
[BITS 16]

         jmp  start     ;jmp to executable code 3bytes

;**********************************************************************
;* boot record : NE PAS MODIFIER                                      *
;**********************************************************************
_OEM_NAME__________    db 'GNU X-OS'
_bytes_per_sector__    dw 512
_sect_per_cluster__    db 1
_reserved_sectors__    dw 1
_number_of_FATs____    db 2
_number_of_entries_    dw 224
_nb_total_sectors__    dw 2880
_media_descriptor__    db 0xF0
_sectors_per_FAT___    dw 9
_sector_per_track__    dw 18
_number_of_heads___    dw 2
_nb_hidden_sectors_    dw 0
_nb_part_sectors___    dd 0
_drive_number______    db 0
_reserved1_________    db 0
_signature_________    db 29h
_serial_number_____    dd 0
_volume_label______    db 'XOS-LOADER ' ;11bytes
;_reserved2_________    db 0, 0, 0, 0, 0, 0, 0, 0 ;8bytes
;**********************************************************************
;**********************************************************************

%DEFINE LOAD_ADDR 100h


%define buffer 0x7e00
%define file   0x8000


%include "boot.inc"

MAGIC_NUMBER        dw 0AA55h

;CONST_and_VAR
bootdrive:    db 0
kername       db 'XOSKER32XSS',0   ;le format du nom ici est en 8x3
               ;exemple: 'NOMFIC00EXT'


;MESSAGES
MSG_init      db 'XLoader v2.2',10,13,0
              ;db 'By AlAdDiN',10,13,0
MSG_pix     db '.',0
MSG_ok     db 10,13,'OK',0
MSG_notfound  db 'ERR',10,13,0
;MSG_reboot    db 'Hit key',10,13,0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
start:

      ;moving boot sector to 0x9000
      cli

         ;mov ax, 0a000h
         ;mov ss, ax
         ;mov sp, 0bf00h

         mov ax, 9000h
         mov es, ax

      push ax

         xor ax, ax
      mov ds, ax

      mov ax, 7c00h
      mov si, ax
      mov di, ax

      mov cx, 0100h
      rep movsw


      mov ax, go
      push ax

      retf




go:
      sti
             mov ax, cs
              mov ds, ax
              mov es, ax
      ...
this code works fine since the bochs log shows that after boot i'm running the loader code...

here is the 1st part of loader code (loader is the first part of my kernel, loader is an asm code, kernel : a C code)

Code: Select all

[BITS 16]

%DEFINE sw32sig 0xABEF


extern main     ;main kernel function

global _start

_start:

mov ax, 0x100      ;I think here is the problem ?????????
mov es, ax
mov ds, ax

....


call EnableA20
; initialisation du pointeur sur la GDT

   mov ax,gdtend   ; calcule la limite de GDT
   mov bx,gdt
   sub ax,bx
   mov word [gdtptr],ax

   xor eax,eax      ; calcule l'adresse lineaire de GDT
   xor ebx,ebx
   mov ax,ds
   mov ecx,eax
   shl ecx,4
   mov bx,gdt
   add ecx,ebx
   mov dword [gdtptr+2],ecx



; passage en pmode
   cli
   lgdt [gdtptr]   ; charge la gdt
   mov eax,cr0
   or ax,1
   mov cr0,eax      ; PE mis a 1 (CR0)

   jmp next
next:
   mov ax,0x10      ; segment de donne
   mov ds,ax
   mov fs,ax
   mov gs,ax
   mov es,ax
   mov ss,ax
   mov esp,0x9F000 ;9F

   jmp dword 0x8:main    ; reinitialise le segment de code


;--------------------------------------------------------------------
gdt:
   db 0,0,0,0,0,0,0,0
gdt_cs:
   db 0xFF,0xFF,0x0,0x0,0x0,10011011b,11011111b,0x0
gdt_ds:
   db 0xFF,0xFF,0x0,0x0,0x0,10010011b,11011111b,0x0
gdtend:

;--------------------------------------------------------------------
gdtptr:
   dw   0   ; limite
   dd   0   ; base

srg

Re:moving bootsector & loading kernel

Post by srg »

Hmm

Most people load their Kernels at 0x100000 which is 1MB

srg
aladdin

Re:moving bootsector & loading kernel

Post by aladdin »

I think it's not possible, since the bootsector runs on real mode, we can't load somthing up to 1Mb.
...
i foud the bug in my code, here is the correct version

in loader

Code: Select all

global _start

_start:
/**************************/
cli                        //I forgot to init the stack  :P
mov ax, 9000h       //   
mov ss, ax
mov sp, 0
sti
/**************************/
mov ax, 0x100      ;I think here is the problem ?????????
mov es, ax
mov ds, ax

srg

Re:moving bootsector & loading kernel

Post by srg »

Oh it's possible.

The Unelegant way is to load your kernel a bit at a time and after loading each bit, switch to protected mode, load it in and then switch back to real mode.

The Elegant way is to use unreal mode - real mode with 32-bit segments - using a quirk of the CPU (There's lots of info on the web about it).

srg
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:moving bootsector & loading kernel

Post by Pype.Clicker »

and the final "quick&small" method consisting of calling INT 15h to move memory blocks, letting the dull work of switching to pmode&back to the BIOS :)
User avatar
bubach
Member
Member
Posts: 1224
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:moving bootsector & loading kernel

Post by bubach »

imho: i think that _all_ code should be commented in english even if that isn?t the primary lang.

/ Christoffer
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Post Reply