about the 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.
adeelmahmood1

about the kernel

Post by adeelmahmood1 »

hi
ok i have made a boot loader which can load and print some messages .. now i want my loader to load another c program which can be for example my kernel ..
now i have some questions about this ...
if i m trying to read from

al=5      
ch=0   
cl=2   
dh=0
dl=0

then how could i place my c kernel at this place .. coz according to this when loader will look at this place (as described above by reg's) it will find my c kernel there and then it will load it .. how does this works ????
coz i didnt put it at that place or even at anyplace...
also when i tried to compile my load.asm with "-f bin" parameter it worked fine and when i used "-f coff" coz i think i need a load.o file to link with kernel.o it gave me an error
"unrecognized directive [org]"
so plz someone help me
thanx
Friend

Re:about the kernel

Post by Friend »

Hi,

the [org] directive is only valid in flat binary files, in other formats you specify the base address ([org]) at link time, if you use LD you add the -Ttext=0x100000 for a kernel loaded to 1MB, you can put any address in that arg, as long as you load the code to that address.

By the looks of those regs you will need to put your kernel starting at sector 2 on the disk, and this will then load sectors 2-6. You can use any program that writes directly to disk tp put it there, in windows I use bootcopy, but there are others like partcopy, debug, etc, if you are using linux I cannot help you, sorry.

I hope this helps
Tom

Re:about the kernel

Post by Tom »

Yea, "Friend" is right about the org for binarys only. If your trying to link a C kernel to a bootsector DON'T DO IT ;) That's what I first tryied to do - it doesn't work.

Now, are you using win32 or linux to compile and link your C kernel?

Also, loading at 0x100000 will make NASM say the the variable is too big...that's because if your using the BIOS to load your C or ASM kernel, ax, bx, or cx & dx are too small ( 16-bit ) to do that. You need to do this:

Asm bootsector->2nd stage kernel linked with C kernel->Need more room?Create a driver to load stuff if in PMode.
adeelmahmood1

Re:about the kernel

Post by adeelmahmood1 »

well u know what
i m a little confused now and i m not sure what to do next so would u plz help me and tell me...
i m using nasm as assembler ... in windows 2000
partcopy for writing to floppy
and gcc and ld ..etc ... thats what i m using
so u said that i should not link the c kernel with asm loader .. then what else should i do ...
right now i have just made a bootloader and u can see i m a beginner so what is the next step for me ...
thanx for ur help
if u dont use windows its fine .. atleast tell me by concept and rest i will figure out some how ...
adeelmahmood1

Re:about the kernel

Post by adeelmahmood1 »

Need more room?Create a driver to load stuff if in PMode.
plz can u explain that in detail !!!
Friend

Re:about the kernel

Post by Friend »

Hi again,

What Tom means is that you cannot link your boot sector code with your kernel, this is normal, although I have never tried but a few ideas do come to mind on how to make it work, but I have never tried so it may very well be impossible. What you need to do is build your boot sector and write it to sector 1 of the disk using partcopy. Once you have done this you need to compile your c source(s) using the -c switch for gcc and then link your file(s) using LD and then write them to sector 2 of the disk, even if you only have 1 c source file you still need to use LD and link it. Remember that gcc outputs elf by default so if you want to link other asm files with your c code then you must compile them with the -f switch and specify elf, but dont use the org directive in these asm files as LD will handle that with it -Ttext switch.

So here is an example of what to do:

Compile your boot code

nasmw boot.asm -o boot.bin
partcopy -f0 0 200 boot.bin

Compile your kernel

nasmw -f elf ports.asm
gcc -c kernel.c
ld -Ttext=0x10000 kernel.o ports.o -o kernel.exe
objcopy -O kernel.exe kernel.bin
partcopy -f0 200 2000 kernel.bin

the prots.asm is just an example of what to do to link asm with c, you then should have a working boot disk, remeber that you will need to load your kernel code to 1000:0000 for this to work as is. Remember that for this kernel to work properly it will need to be the first file in the list of .o files and the the first function in your kernel.c file will be the entry point and it cannot contain any string constants like "A String." or there can be problems. By doing this you will not need an intermediate asm call to jump to your kernel entry.

A note about the args to these program -O in objcopy is the uppercase character O not zero. And I dont know how partcopy will react if your kernel file is not as big as I have specified (8KB).

With the driver that Tom spoke about you will need to setup a fair bit of stuff to do this so I would just load to below 1MB for now until you get it up and running.

If you want I will give you a small example to use and you can see it in action, but I am assuming you know that you must be in 32bit pmode before you can use any gcc compiled c code.

hope this helps.
Friend

Re:about the kernel

Post by Friend »

Sorry,

Above in the commands

objcopy -O kernel.exe kernel.bin

should be

objcopy -O binary kernel.exe kernel.bin

Sorry again.
adeelmahmood1

Re:about the kernel

Post by adeelmahmood1 »

ih
thanx for ur help .. ok i m now working on ur idea and lets see wat happens .. any way thanx alot for ur help .. only one confusion
first u said
With the driver that Tom spoke about you will need to setup a fair bit of stuff to do this so I would just load to below 1MB for now until you get it up and running
and then u said
but I am assuming you know that you must be in 32bit pmode before you can use any gcc compiled c code.
now i dont understand wat u mean by this .. u ask me to work in the real mode and get something running and then go for protected mode and then u r telling me that c compiled code will not work in real mode...
so how my c kernel will work in real mode even if i do all the stuff u have told me ...
plz clearify this
thanx
dronkit

Re:about the kernel

Post by dronkit »

gcc can't compile 16 bits code (actually, gas can't).

And 32 bits code will not work in real mode, mainly because of the different
segmentation schemes between the two.

So you write the bootstrapper (the boot sector or mbr) in 16 bits code using real mode segmentation and then setup your idt and gdt, switch
to pmode and jump to the kernel (which gets compiled by gcc in 32 bits mode, using flat addresses).
adeelmahmood1

Re:about the kernel

Post by adeelmahmood1 »

alright
can u send me a link to a good tutorial or a site which has some info about the IDT n GDT tables
thanx
Tom

Re:about the kernel

Post by Tom »

I have a pre-made GDT someone else made for me...(because, I didn't even know what the was then...)
You can use that one, but it's not a tutorial.

It's at my site in Pk0.6 in boot.asm
Soon in pk0.7 there will be a IDT.
adeelmahmood1

Re:about the kernel

Post by adeelmahmood1 »

hi .. yeah i have checked out ur boot.asm but can u do me a favor can u explain all this to me in a bit more detail keeping in mind that im an absolute beginner ...
; Get ready to set PMode ( Protected Mode)
setpmode:
cli ; Stop BIOS interrups -
; because PMode can't use them
; in this manner

; Load the gdtr
   lgdt[gdtr] ; you didn't have a gdt

; Set Protected Mode ( PMode )
mov eax, cr0
or eax, 1
mov cr0, eax

jmp dword codesel:pmode

; We are in protected mode here:
[bits 32]
pmode:
; load them all with 32bit sel
mov eax,datasel
mov ss,eax
mov ds,eax
mov es,eax
mov fs,eax
mov gs,eax

mov ax,0x10
mov ss,ax
mov esp,0xFFFF ; maxmimum

mov dl,[drive]

   ; stop the floppy motor from spinning
   mov edx,0x3f2
   mov al,0x0c
   out dx,al
   
jmp dword codesel:0x1000   ; Jump to the C Kernel


; Global descriptor table
; this tells the computer where
; all the segments (now selectors) are
gdtr
dw gdt_end-1
dd gdt
gdt
nullsel equ $-gdt
gdt0
dd 0
dd 0
codesel equ $-gdt
dw 0ffffh
dw 0
db 0
db 09ah
db 0cfh
db 0h
datasel equ $-gdt
dw 0ffffh
dw 0h
db 0h
db 092h
db 0cfh
db 0

gdt_end
thanx for ur help
Tom

Re:about the kernel

Post by Tom »

ok...guess I better have changed all my comments in the next release( pk0.7 will have a bootsector, 2nd stage loader, 3rd stage loader linked with the C Kernel ).

That code does this in this order:

Sets PMode (setpmode) - Sets Protected Mode, a processor mode, for running a 32-bit C Kernel, mine

the lgdt loads the Global Descriptor Table, a thing that tells PMode where the data goes.

the jump dword codesel:pmode jumps to set PMode. You need this.

The bits 32 tells NASM that your in PMode.

The load them all with 32-bit sel is for setting register values, i.e. make the registers correctly set - need this.

The stop floppy motor from spinning stops that little green light and the motor, we are in PMode so the BIOS can't do it - so we do that.

the jump dword codesel:0x1000 is where the FritzOS C kernel is loaded - go the the C Kernel.

Hope that helps,
adeelmahmood1

Re:about the kernel

Post by adeelmahmood1 »

hi
well thanx for ur help
yeah i can understand some of the steps now ..
- set up the pmde
-load gdt
-jump to pmode
-load the reg's with some new stuff
but u didnt got my point .. actually i dont understand all this code u have written for ur gdt .. what is this .. i think for that ill have to read some tutorials about GDT's and this stuff and then ill get back to ur code..
by the way thanx for ur help
adeelmahmood1

Re:about the kernel

Post by adeelmahmood1 »

and a couple more Q' s
i dont know i m right but can u tell me how many bytes can one sector of a floppy can hold or store in ...
and can u also tell me any website to get info about these GDT's ... coz i couldnt find a good one ... may be the place from where u learned it ...
thanx alot
Post Reply