C os programming

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.
AR

Re:C os programming

Post by AR »

You left out the BIOS Extended Data Area, it extends down from 0x9FFFF, it's length varies from BIOS to BIOS (which is why you're meant to use the "Get Conventional Memory Size" function).
SpiglerG

Re:C os programming

Post by SpiglerG »

I'll try it. Actually, the bootloader loads the kernel into 0x0100.
The kernel has its own loader(the asm file) which switch into protected mode and then start the c function.
So it doesn't work because, when it switches, the protected mode addresses ovelap and delete from ram the kernel at 0x0100?
If so, i only need to make the bootloader load the kernel at 0x1000000?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C os programming

Post by Solar »

@ SpiglerG:

Did you visit the FAQ? Did you toy with the BabySteps tutorial? Did you actually try any of the other tutorials before setting out to write your own code?

may i suggest you read through Rolling your Own Bootloader ? We've listed a list of common questions/options and caveats for people not wanting to use GRUB :)
Every good solution is obvious once you've found it.
SpiglerG

Re:C os programming

Post by SpiglerG »

I only need to modify the bootloader and kernel a little. Only set the loading address to 0x100000, isn't it?
After that, will the kernel work?
beyondsociety

Re:C os programming

Post by beyondsociety »

So it doesn't work because, when it switches, the protected mode addresses ovelap and delete from ram the kernel at 0x0100? If so, i only need to make the bootloader load the kernel at 0x1000000?
Yes. Like I said, you can load it lower but as AR just said, you would need to use the "Get Conventional Memory Size" function to figure out what areas are free and which are not.

A good place for now would be to load it at 1MB. Later on you may want to load it lower depending on what features you would like to support. Just remember to set the A20 gate, or you will not be able to access memory above 1Mb.

Hope this helps.
SpiglerG

Re:C os programming

Post by SpiglerG »

ok. i'll try to load it at 1mb(0x100000) and then enable protected mode and A20.
i'll post later.

PS: i need to make the bootsector switch to pm and enable a20?
or can i do it later.
SpiglerG

Re:C os programming

Post by SpiglerG »

Otherwise, i can switch to protected mode from start.asm, but load it separately from the kernel after the bootsector in the memory and then load another starter for the c function at 0x100000.

So, what should i do?
beyondsociety

Re:C os programming

Post by beyondsociety »

i need to make the bootsector switch to pm and enable a20? or can i do it later.
Since your bootloader is fat12, there will be no room left for pmode or a20. Use a second file to load into pmode, set a20, and call your c kernel. The a20 gate can be set before or after you enter protected mode.
Otherwise, i can switch to protected mode from start.asm, but load it separately from the kernel after the bootsector in the memory and then load another starter for the c function at 0x100000.

So, what should i do?
See above.

Hope this helps.
SpiglerG

Re:C os programming

Post by SpiglerG »

i'm trying to make it.
i'll tell you when i'll try.
SpiglerG

Re:C os programming

Post by SpiglerG »

To read a sector i use bios functions.
using bios functions i need to be in real mode, but in real mode i can't use the address 0x100000(in the loader to load the kernel at that address).
beyondsociety

Re:C os programming

Post by beyondsociety »

To read a sector i use bios functions.
using bios functions i need to be in real mode, but in real mode i can't use the address 0x100000(in the loader to load the kernel at that address).
This needs to go into your second-stage loader: then set a20, enable protected mode, load kernel.

Code: Select all

;-----------------------------------------------------
reset_Disk:                ; Reset Floppy Disk
xor ax, ax                ; Zero AX register
int 13h         ; Actually Reset
or  ah, ah                    ; Check for error code
jc  reset_Disk      ; IF ERROR => reset again
   
;-----------------------------------------------------------
; Load sectors from the Floppy disk into memory
; Using CHS (Cylinder, Head, Sector) Addressing 
;-----------------------------------------------------------
readin_kernel:
mov ax, 0xFFFF                 ; ES:BX = 0xFFFF:0x0010 (1MB)
mov es, ax
mov bx, 0x0010

mov ah, 0x02      ; Load disk data to ES:BX
mov al, 0x17      ; Load 17 sectors
mov ch, 0x00      ; Track  = 0x00
mov cl, 0x02      ; Sector = 0x02
mov dh, 0x00      ; Head   = 0x00
mov dl, [DriveNumber]   ; Drive number (0x00 = Floppy, 0x80 = Hard Drive)

int 13h         ; Read Disk!
or  ah, ah                     ; Check for error code
jc  readin_kernel              ; If Error => read again
0xFFFF:0x0010 requires a20 gate to be set or it will wrap around to 0x00000000.

Hope this helps.
SpiglerG

Re:C os programming

Post by SpiglerG »

I didn't remember this. Yes.
But i need to insert the code you gave me and then enable a20, or enable a20 and then insert you code.
SpiglerG

Re:C os programming

Post by SpiglerG »

PS: FFFF:0010 is 1mb? isn't it 1000F
beyondsociety

Re:C os programming

Post by beyondsociety »

But i need to insert the code you gave me and then enable a20, or enable a20 and then insert you code.
The reset_disk and read_sectors function needs to be before you enable a20. Then you can either enable a20 before or after you enable protected mode as long as it is set.
PS: FFFF:0010 is 1mb? isn't it 1000F
Yes/no.

When the IBM-AT was introduced, it was able to access up to sixteen megabytes of memory (instead of the 1 MByte of the IBM-XT). But to remain compatible with the IBM-XT, a quirk in the XT architecture (memory wraparound) had to be duplicated in the AT. To achieve this, the 20th line on the address bus (A20) was turned off.

So if the a20 gate isnt enabled (turned off), it will cause 0xFFFF:0x0010 (1MB) to wraparound and you will get 0.

Segment * 16 + offset
0xFFFF * 0x0010 + 0x0010 = 0x100000 (1MB)
SpiglerG

Re:C os programming

Post by SpiglerG »

Ok.
Now i can't, but tomorrow i'll try it. if it won't work i'll post here.

The code you posted doesn't search for a file(for example kernel.bin), my bootloader doeas it.
Can i use my bootloader code for the bootloader AND a copy for the loader? (the loader copy will switch to pm, load gdt, enable a20, but first of all, will load a kernel.bin at FFFF:0010).
Post Reply