Converting my kernel into plain binary dosn't work

Programming, for all ages and all languages.
REV

Re:Converting my kernel into plain binary dosn't work

Post by REV »

Yeah I looked at the binaries/object files and found some incorrect stuff like the data being there first and some other stuff.

I think I was doing something stupid all along.

I belive I have figured this out but do not know how to fix it.
It is the data segment. I belive it is overlapping the code segment.

Heres my bootloader (well part of it): I THINK THIS IS IT.
[tt]
   ;***Setup the segment***
   mov bx, 0x1000      ;---\/
   mov es, bx      ;0x1000:0000
   mov bx, 0x0000      ;--------/\
   int 13h

   mov ax, 0x1000      ;Setup the Data segment
   mov ds, ax      ;Point to the kernel

   jmp 0x1000:0x0000   ;Go
[/tt]

Just to be sure here are the linker scripts:
[tt]
ALL:
init.obj
; kinit.obj
(Other OBJS are commented out)
MAIN: 100000 0 0
*
[/tt]
REV

Re:Converting my kernel into plain binary dosn't work

Post by REV »

Heres the "cut down" bootloader. (Display messages and stuff are cut out).

[tt]
[bits 16]
[org 0x0000]         ;Foreward to the address 0x0000

jmp start
(String varibles)

start:
mov ax, 0x07C0
mov ds, ax
mov es, ax

ResetDrive:
   mov ah, 00h      ;Function for resetting the disk
   int 13h
   or ah, ah      ;Test to see if it is ok
   jnz ResetDrive      ;If AH is not 0 then we may as well try this again
   jmp FindKernel
;*****************

FindKernel:
   mov ah, 02h      ;BIOS FUNCTION
   mov al, 01h      ;# Of sectors to read
   mov ch, 0h      ;Track
   mov cl, 02h      ;Starting sector
   mov dh, 0h      ;The head to read
   mov dl, 0h      ;Disk

   ;***Setup the segment***
   mov bx, 0x1000      ;---\/
   mov es, bx      ;0x1000:0000
   mov bx, 0x0000      ;--------/\
   int 13h

   mov ax, 0x1000      ;Setup the Data segment
   mov ds, ax      ;Point to the kernel

   jmp 0x1000:0x0000   

   jmp Hang

Hang:
   cli
   hlt
;*************************

;Signature
times 510-($-$$) db 0      ;Get rid of the slack space by filling up the sector
dw 0AA55h
[/tt]
Ryu

Re:Converting my kernel into plain binary dosn't work

Post by Ryu »

You still got one too many zero in the script, use exactly this: MAIN: 10000 0 0

Can you provide me where it crashes?

edit: btw, nothing is wrong with that code. Though it seems only 1 sector is copied to 1000:0000h which I'm not sure that is intentional.
REV

Re:Converting my kernel into plain binary dosn't work

Post by REV »

Well it loads the bootsector code, prints the welcome message then uses the BIOS function to go sector 2 and reads 1 sector into 1000:0000 and then jumps to it. I suspect it jumps there ok but crashes tring to run the Data(?) I can't get BOCHS to run I'm a bad connection right now.


I created a quick assembly file that prints '!' to the screen. The bootloader jumps there ok.
Ryu

Re:Converting my kernel into plain binary dosn't work

Post by Ryu »

Try: MAIN 10000 10000 0

What I'm making off the poor help document of jloc is that the second hex is where your loading the image, the first specifies what segment should jloc compute offsets, jmps, calls, all those assembled time immediates that should be calculated. As the second hex basically will add/subtract (compute the relatively) on top of that so you basically specify where the image is going to be loaded in memory.
REV

Re:Converting my kernel into plain binary dosn't work

Post by REV »

No it dosn't like it. :o
Here have all the code I'm working with. When I looked at the main binary (The kernel) in the hex editor I saw the data first. >:(

Heres the code from the linker:
ALL:
init.obj
; kinit.obj
; system.obj
; ports.obj
; floppy.obj
; graphics.obj
; video.obj
; text.obj
; print.obj
MAIN: 10000 10000 0
*

Heres the code form init.asm
[tt]
;************************************
;* INIT.ASM v0.0.1 By Mike Cody ([email protected])
;************************************
;Compiler: NASM v0.98
;Nasm -o init.obj -f obj hal\i386\init.asm
;--------------------
;Includes:
;/cpuid.asm   --   Push/Pop for CPUID

[bits 16]         ;Set us up in real mode.
;[extern main_]
;**********************************************************

;========================
; Start us up (Code)
;========================
;Because well be in Real Mode I would take this time
;to set the proper video modes and other code
;Even though the bootloader does this some things do **** up

SECTION .text
jmp start
start:

mov ah, 00h
mov al, 03h         ;80x25x16 (Text mode)
int 10h

mov si, msgMadeit
call DisplayMessage

jmp hang

;========================
; CPUID/CPU Detection
;========================

call pCPUID
%include "D:\CodyOS\Hal\i386\CPUID.ASM"
cmp dx, 1
jz NoCPU
jnz PMode

;***NO 386 WAS FOUND****
;Showstopper!
NoCPU:
call ShowStop
mov si, msgNo386
call DisplayMessage
jmp hang
;*****************************

;========================
; Move to Protected mode
;========================

PMode:
;cli
;
;mov eax, cr0
;or ax, 1
;mov cr0, eax
;
;[bits 32]
;We are now in 32-bit protected mode :p
;Time to start CodyOS!

;=======================
; Run CodyOS!
;=======================
;Run the C/C++ portion of CodyOS
;call main_

;---------------------------
;If CodyOS fucks up somewhere and returns back here which should
;never happen (since control is transfered to codyos_core(); in \core\main.c
hang:
jmp hang
;---------------------------

;***********************
; SHOWSTOPPER Function
; Useage:
;
; call ShowStop
;***********************
ShowStop:
   mov si, msgShowStop
   call DisplayMessage
   ret
   ;It be reccommend to hang the machine soon
   ;since Showstopper is never a good word
;***********************
;*********DISPLAYMESSAGE***********
;Prints a message to the screen
;Useage:
;mov si, Message
;call DisplayMessage
;**********************************
DisplayMessage:
   ;This code makes it so the CPU isn't wasted on useless
   ;clock cycles
   mov ah, 0Eh   ;BIOS Display function
   mov bh, 0h   ;Page
   mov bl, 7h   ;Colour even though this never seems to work
GoPrint:      
   lodsb      ;Go to the next character
   or al, al   ;Is AL null?
   jz DisplayDone   ;EOS is hit so we return
   int 10h      ;BIOS Interrupt
   jmp GoPrint   ;Else
DisplayDone:
   ret      ;Return us to the function that called us
;**********************************

SECTION .data
;************************
; Declare varibles (Data)
;************************
   msgShowStop   db   "SHOWSTOPPER: An unrecoverable fault has occured", 0x0D, 0x0A, 0x00
   msgNo386   db   "A compatible 80386 processor was not detected!", 0x0D, 0x0A, 0x00
   msgMadeit   db   "Ok we made it!", 0x00
[/tt]
Ryu

Re:Converting my kernel into plain binary dosn't work

Post by Ryu »

Try an ORG 0 for .text segment. The segments may be switched around in the single object output.

edit: Just incase..

SECTION .text
ORG 0
jmp start
start:
...

..and keep MAIN: 10000 10000 0
REV

Re:Converting my kernel into plain binary dosn't work

Post by REV »

NASM Error: "D:\CodyOS\hal\i386\init.asm:23: parser: instruction expected"
Ryu

Re:Converting my kernel into plain binary dosn't work

Post by Ryu »

Okay I don't know how that caused a assembler error. But I have no clues with NASM on the other hand. Lets just do this a more simple method that will work.

Do not creats a .text segment for the barebone initialization code. Meaning you do something like the following:

[bits 16]
ORG 0
extern _main ; or however you declare an external protocall in NASM

jmp init

init:

.......


call _main
REV

Re:Converting my kernel into plain binary dosn't work

Post by REV »

I had to take out the ORG because thats not allowed in OBJ files according to NASM.

>:( >:( >:( >:( >:( >:( >:( What else could be causing this?
Ryu

Re:Converting my kernel into plain binary dosn't work

Post by Ryu »

Hmm MASM allows it, and I've seen NASM code that uses ORG. What a pain :P

I was trying to avoid complicated jloc scripts which I haven't had the need for them yet. From the documents I suppose you can do this:

ALL:
file1.obj
file2.obj
file3.obj
last.obj
DATA: 10000 TEXT.after TEXT.i_after
*,DGROUP
TEXT: 10000 10000 0
*

I'm unsure though, tell me how it goes in private. I don't think we want lot of post with trial and erros :P

edit: forgot star at the end
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Converting my kernel into plain binary dosn't work

Post by Candy »

Ryu wrote: I'm unsure though, tell me how it goes in private. I don't think we want lot of post with trial and erros :P
For sure, if you're going to solve a problem we're very interested. The point of this forum is to allow you to discuss those things.
REV

Re:Converting my kernel into plain binary dosn't work

Post by REV »

This is a JLOC issue.
The segments are fine in the assembly code.

This is what happens
When JLOC links the files it places the data segment first and then the code segment. What will happen is that the when the computer is goes to execute the kernel after control is passed the bootloader it will execute data which will result in a fault.

What I am tring to figure out is how to make JLOC place the code segment first and then the data segment later.
Ryu

Re:Converting my kernel into plain binary dosn't work

Post by Ryu »

Candy wrote:
Ryu wrote: I'm unsure though, tell me how it goes in private. I don't think we want lot of post with trial and erros :P
For sure, if you're going to solve a problem we're very interested. The point of this forum is to allow you to discuss those things.
I didn't think anyone wanted to see bunch of post of trial and errors. But for sure I was going to post the remedy to this situation when we solved it. But if people is interested then heres my last post to him:

As you have aready mentioned, the problem is that .data section is being placed on the top of the image while .text section is at the bottom. Normally if you planned to use flat binary you wouldn't be making any sections at all or just one section where all code and data would reside in. However creating sections should be valid too for jloc, but the problem is the linker needs to link properly. (In your code .text section should be at offset 0 of the image)

About the section overlapps, please confirm this because I find it very weird for two sections to overlap created by the linker.

The boot loader segments is all right. The only problem is linking with jloc to deal with multiple sections that will be linked as one section (flat binary).

Your Init.asm is fine, the entry point is offset 0, because your using a jmp 1000:0000h == CS=1000h EIP=0.

What you can also do if do this trial and error because what that assembler does is ambigous to me.

You can do the code:

SECTION .text
db "A1A1"
SECTION .data
db "B2B2"

Don't assemble anyother code for the object. If the two overlapps then we got a odd issue. This should output for jloc "A1A1" in the hex editor be first then "B2B2".
If it doesn't try putting .data section on top of .text section in the code. Experiment a little try to figure out why the behaviour and ofcourse resolving the main issue of .text ( that should be at the beginning of the image).

Another problem is I wont be having my compilers/assemblers/linkers/tools to help solve this issue with the behavior of jloc, for at least till this weekend. I'm quite booked at work when I get home its shower and bed. Its pretty much that this problem is expected, I went through a load getting MASM+VC+JLOC to work together. This is where you need good debugging skills, not only at runtime
REV

Re:Converting my kernel into plain binary dosn't work

Post by REV »

They don't appear to be overlapping anymore. What I mean is that when I dissambled them with a hex editor it looked like some data is in the middle of the binary file but now there not overlapping anymore and the data is always at the top of the file. >:( Confirmed: No overlapping anymore

Can I get rid of the sections alltogther and just have a jump ver the data?

jmp Start
db skfdapjfsdjfsdfsdfsdf
Start:

If I do what should I do with the linker script.
Post Reply