Page 2 of 4

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 9:28 am
by czlowieczek
I tried to change my bootlader setting to load my code in 0000h segment with offset (real mode) 500h (5000h normal mem ), and my code got org 5000h and it worked in real mode now, but when i loaded gdt and turned in pmode i can't jump in my code to change cs.

Code: Select all

[BITS 16]
[ORG 5000h]

start:


cli
lgdt [gdt_descr]
mov eax, cr0
or eax, 1
mov cr0, eax

;mov ax, 0x10
;mov ds, ax
;mov es, ax
;mov ss, ax
;mov gs, ax
;mov fs, ax

[bits 32]
jmp 0x08:start32

start32:  

mov al, 'x'
mov edi, 0B8000h   ;print x to check am i here
stosb

petla:
nop                      ;infinite loop
jmp petla


times 430 db 0

gdt:
  ; NULL Descriptor
  dd 0
  dd 0
 
  ;code 4gb
  dw 0xFFFF   
  dw 0         
  db 0         
  db 10011010b 
  db 11001111b 
  db 0         
 
  ;data 4 gb
  dw 0xFFFF
  dw 0        
  db 0         
  db 10010010b 
  db 11001111b 
  db 0         
gdt_end:    
 
; naglowek
gdt_descr:
  dw gdt_end - gdt - 1    
  dd gdt 
  

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 9:30 am
by AJ
Hi,

Probably the most commonly used are GRUB/GRUB2. There are pages about bootloaders on the wiki and searching for GRUB will lead you to the downloads / manual.

Cheers,
Adam

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 9:53 am
by Minoto
czlowieczek wrote:I tried to change my bootlader setting to load my code in 0000h segment with offset (real mode) 500h (5000h normal mem ), and my code got org 5000h and it worked in real mode now, but when i loaded gdt and turned in pmode i can't jump in my code to change cs.
In real mode, 0000h:0500h is 500h linear, not 5000h. Was that a typo, or is your addressing off?

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 9:58 am
by czlowieczek
But in real mode segment part [i_mean_this]:[not_this] is shl 4 500h shl 4 = 5000h, but doesn't matter It wasn't working with org 5000h :( , I am trying to install grub on floppy, but I don't know will it work :-|

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:24 pm
by egos
AJ gave good advice. Load your kernel somewhere in first 64 kb. In this case you can use linear addressing in RM as well as in PM. For example:

Code: Select all

  org 8000h

  mov ax,3
  int 10h ; I hope your stack is enough

  xor ax,ax
  mov ds,ax
  lgdt [GDTR]

  cli

  mov eax,cr0
  or al,1
  mov cr0,eax

  jmp CODESEL:start32 ; reset cs first

  use32
start32:
  mov eax,DATASEL
  mov ds,ax
  mov es,ax
  mov fs,ax
  mov gs,ax
  mov ss,ax
  mov esp,$$

  mov word [0B8000h],"x" + 7 shl 8
@@:
  hlt
  jmp @b
  ...

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:28 pm
by czlowieczek
I created new kernel with max size 512 ( i load it as bootloader), it works, but when i'm trying too put character 'x' it doesn't :(

There is fat12 header, beacuse I'm doing this on fat 12 formatted floppy

Code: Select all

[BITS 16]
[ORG 7C00h]


jmp     short   start
nop

bsOemName               DB      "DONTOST "      ; 0x03
bpbBytesPerSector       DW      512               ; 0x0B
bpbSectorsPerCluster    DB      1               ; 0x0D
bpbReservedSectors      DW      1               ; 0x0E
bpbNumberOfFATs         DB      2               ; 0x10
bpbRootEntries          DW      224               ; 0x11
bpbTotalSectors         DW      2880               ; 0x13
bpbMedia                DB      240               ; 0x15
bpbSectorsPerFAT        DW      9               ; 0x16
bpbSectorsPerTrack      DW      18               ; 0x18
bpbHeadsPerCylinder     DW      2               ; 0x1A
bpbHiddenSectors        DD      0               ; 0x1C
bpbTotalSectorsBig      DD      0               ; 0x20

bsDriveNumber           DB      0               ; 0x24
bsUnused                DB      0               ; 0x25
bsExtBootSignature      DB      41               ; 0x26
bsSerialNumber          DD      0x11            ; 0x27
bsVolumeLabel           DB      "DONTOSTBOOT"   ; 0x2B
bsFileSystem            DB      "FAT12   "      ; 0x36

start:

cli
lgdt [gdt_descr]
mov eax, cr0
or eax, 1
mov cr0, eax

mov ax, 0x10
mov ds, ax
mov es, ax
mov ss, ax
mov gs, ax
mov fs, ax

mov esp, 8000h

push word 0x08
push word start32

[bits 32]
retf

start32: 

mov al, 'x'
mov edi, 0B8000h
stosb
 
petla:
nop
hlt
jmp petla

times 365 db 0

gdt:
  ; NULL Descriptor
  dd 0
  dd 0
 
  ; kod, baza: 0, limit: 4GB, DPL: 0
  dw 0xFFFF    ; mlodsze slowo limitu
  dw 0         ; mlodsze slowo bazy
  db 0         ; wlodszy bajt starszego slowa bazy
  db 10011010b ; kod / exec-read
  db 11001111b ; flagi i 4 bity limitu
  db 0         ; najstarszy bajt bazy
 
  ; dane (odczyt/zapis), baza: 0, limit: 4GB, DPL: 0
  dw 0xFFFF
  dw 0        
  db 0         
  db 10010010b 
  db 11001111b 
  db 0         
gdt_end:    
 
; naglowek
gdt_descr:
  dw gdt_end - gdt - 1    ; rozmiar gdt
  dd gdt 
  
 dw      0AA55h]

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:34 pm
by egos

Code: Select all

push word 0x08
push word start32

[bits 32]
retf
Wow, you're crazy programmer :D

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:39 pm
by czlowieczek
Why you think so, When I was trying

Code: Select all

jmp 0x08:start32
I had error in my bosch console and hardware restart :D

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 12:47 pm
by egos
Your trick has no effect. In my code jump instruction is not so good too. Try this or something like this:

Code: Select all

jmp fword CODESEL:start32

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 1:03 pm
by czlowieczek
But my "trick" works good, it goes into infinite loop according to my bosch console :)

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 1:18 pm
by egos
I meant that the following code gives the same result:

Code: Select all

push word 0x08
push word start32
retf
But using 32-bit offset in this case is more preferable.
czlowieczek wrote:but when i'm trying too put character 'x' it doesn't :(
My code works fine.

Re: 32bit protected mode init problem

Posted: Wed Jul 10, 2013 1:40 pm
by Casm
If you are using flat protected mode, with all the segments based at zero, then before switching to protected mode the cs:ip at the entry point to your code should (in theory) be 0:0x80000 and the org (in theory) should be 0x80000 - so that offset addresses before and after switching to protected mode were the same. Except that ip can't be loaded with 0x80000, because it is a sixteen bit register.

The obvious solution to your problem is to switch to protected mode before leaving the boot loader. Then the eip register will be available for any jumps you want to make. You can set up a temporary GDT for the purpose.

Re: 32bit protected mode init problem

Posted: Thu Jul 11, 2013 1:30 am
by czlowieczek
I wrote new bootloader with protected mode enabling, but i have error in nasm 'mov bp, ??' :FCFD My error is at eip 0x7D6A, I think that the last error in my code :)

Code: Select all

[BITS 16]
[ORG 7C00h]


jmp     short   start
nop

bsOemName               DB      "DONTOST "      ; 0x03
bpbBytesPerSector       DW      512               ; 0x0B
bpbSectorsPerCluster    DB      1               ; 0x0D
bpbReservedSectors      DW      1               ; 0x0E
bpbNumberOfFATs         DB      2               ; 0x10
bpbRootEntries          DW      224               ; 0x11
bpbTotalSectors         DW      2880               ; 0x13
bpbMedia                DB      240               ; 0x15
bpbSectorsPerFAT        DW      9               ; 0x16
bpbSectorsPerTrack      DW      18               ; 0x18
bpbHeadsPerCylinder     DW      2               ; 0x1A
bpbHiddenSectors        DD      0               ; 0x1C
bpbTotalSectorsBig      DD      0               ; 0x20

bsDriveNumber           DB      0               ; 0x24
bsUnused                DB      0               ; 0x25
bsExtBootSignature      DB      41               ; 0x26
bsSerialNumber          DD      0x11            ; 0x27
bsVolumeLabel           DB      "DONTOSTBOOT"   ; 0x2B
bsFileSystem            DB      "FAT12   "      ; 0x36

start:
  xor dl, dl
  mov ah, 02h
  mov al, 3
  mov ch, 1
  mov cl, 16
  mov dh, 00h
  mov bx, 0x1000
  mov es, bx
  mov bx, 00h
  int 0x13
  
  xor dl, dl
  mov ah, 02h
  mov al, 16
  mov ch, 2
  mov cl, 1
  mov dh, 00h
  mov bx, 0x1096
  mov es, bx
  mov bx, 00h
  int 0x13  

cli
lgdt [gdt_descr]
mov eax, cr0
or eax, 1
mov cr0, eax

mov ax, 0x10
mov ds, ax
mov es, ax
mov ss, ax      ;it works  fine
mov gs, ax
mov fs, ax

mov esp, 8000h ;that too

push word 0x08
push word start32 ;It is working good (cs is reloading)

[bits 32]
retf

start32: 

jmp 0x10000 ;jmp to code

times 328 db 0

gdt:
  
  dd 0
  dd 0
 
  
  dw 0xFFFF    
  dw 0        
  db 0         
  db 10011010b 
  db 11001111b 
  db 0        
 
  
  dw 0xFFFF
  dw 0        
  db 0         
  db 10010010b 
  db 11001111b 
  db 0         
gdt_end:    
 

gdt_descr:
  dw gdt_end - gdt - 1    
  dd gdt 
  
 dw      0AA55h
and my little "kernel"

Code: Select all

[Bits 32]
[org 10000h]

petla:
nop
hlt
jmp petla

Re: 32bit protected mode init problem

Posted: Thu Jul 11, 2013 2:16 am
by egos
czlowieczek wrote:I wrote new bootloader with protected mode enabling
Very bad design.

Re: 32bit protected mode init problem

Posted: Thu Jul 11, 2013 2:36 am
by Combuster
The whole point of this whole thing is that you learn how to debug. Not throw away code and rewrite something until it just happens to work.

In other words, I'm getting the idea you haven't quite learned how to program yet and you're trying something way above your league.