Page 2 of 4
Re: Bug jumping from bootsect to kernel
Posted: Tue Apr 26, 2011 5:04 pm
by DavidCooper
Code: Select all
[quote]/* BIOS will copy kernel to es:bx ... */
pushw $KERNEL_POS_ES # KERNEL_POS_ES:KERNEL_POS_BX...
popw %es
movw $KERNEL_POS_BX, %bx
/* Copy function settings... */
movb $0x2, %ah # Copy function...
movb $KERNEL_SECT, %al # Sectors to read...
movb $0x0, %ch # Cylinder 0...
movb $0x2, %cl # Sector 2...
movb $0x0, %dh # Head 0...
movb $0x0, %dl # Device 0...[/quote]
The way you've done this hides the actual values going into ES, BX and AL. Perhaps you should try putting absolute values in instead to see if that's there's a problem there.
Another issue is whether your kernel's being written to the second sector in the first place. Can you make a disk image available so that I can check it directly?
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 7:34 am
by antoniovazquezblanco
DavidCooper wrote:
.text
.globl _start
.include "kernel/kernel.inc"
.org 0
Why .org 0? Wouldn't that be setting up the kernel code to run at the bottom of memory rather than at 10000h? I don't think that will affect the actual code that follows.
As I'm using gnu as and ld if I specify org at 10000h it would generate a lot of zeros before the start of my code. Instead of telling org in the asm file I specify it at compile time as you can see in the makefile in the source.
Code: Select all
AS = as
MKDIR = mkdir -p
RM = rm -rf
LD = ld
EMULATOR = qemu -no-kvm -boot order=a -fda
DD = dd
all: floppy
clean:
$(RM) bin/*
directories:
$(MKDIR) bin/
bootsector: bootsector/bootsector.s directories
$(AS) -o bin/bootsector.o bootsector/bootsector.s
$(LD) -Ttext 0x7c00 --oformat=binary -N -o bin/bootsector.bin bin/bootsector.o
kernel: kernel/main.s directories
$(AS) -o bin/kernel.o kernel/main.s
$(LD) -Ttext 0x10000 --oformat=binary -N -o bin/kernel.bin bin/kernel.o
floppy: directories bootsector kernel
cat bin/bootsector.bin bin/kernel.bin > bin/floppy.img
run: floppy
$(EMULATOR) bin/floppy.img
DavidCooper wrote:
.set STACK_BOT, 0xa0000 # Stack starts at 640K and grows downside...
That will make your stack trash the EBDA (extended BIOS data area) - you might want to avoid doing this so that you can return to real mode to use the BIOS again, and you will want to do this at some point to collect a whole lot of information from the BIOS about the way the machine has been set up.
I will change this and I will comment it for now. Thanks!
DavidCooper wrote:
About watching the code online just click the folders or files and it will appear. You can also use git as it is a git repo and about opening files, they are just plain text, you can open them with any editor and compile them with make. Check the makefile for seeing the requeriments (gcc, as, ld, cat and for testing qemu). Thanks!
I've found out how it works now - I clicked on things before and just got a blank screen, but it's working now. As for the downloaded version, I tried looking at it with notepad and wordpad and just got gibberish, but I can just explore the code online instead.
If you're using windows you should know that linux and windows don't use the same new line simbols. Notepad++ is a good option for win. Thanks for your help.
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 8:00 am
by antoniovazquezblanco
DavidCooper wrote:Code: Select all
[quote]/* BIOS will copy kernel to es:bx ... */
pushw $KERNEL_POS_ES # KERNEL_POS_ES:KERNEL_POS_BX...
popw %es
movw $KERNEL_POS_BX, %bx
/* Copy function settings... */
movb $0x2, %ah # Copy function...
movb $KERNEL_SECT, %al # Sectors to read...
movb $0x0, %ch # Cylinder 0...
movb $0x2, %cl # Sector 2...
movb $0x0, %dh # Head 0...
movb $0x0, %dl # Device 0...[/quote]
The way you've done this hides the actual values going into ES, BX and AL. Perhaps you should try putting absolute values in instead to see if that's there's a problem there.
About the first thing KERNEL_POS_ES = 0x1000, KERNEL_POS_BX = 0x0, KERNEL_SECT = 0x1 as you can see in kernel.inc
Code: Select all
/********************************************************************************/
/* */
/* FIREBIRD OS */
/* */
/* Here I specify a set of constants (memory addresses...) that my operative */
/* system will use in it's kernel. */
/* */
/********************************************************************************/
.set CODE_SEL, 0x8 # Code segment selector in kernel mode...
.set DATA_SEL, 0x10 # Data segment selector in kernel mode...
.set IDT_ADDR, 0x80000 # TODO: For future use...
.set IDT_SIZE, (256*8) # TODO: For future use...
.set GDT_ADDR, (IDT_ADDR+IDT_SIZE) # GDT starts after IDT...
.set GDT_ENTRIES, 5 # Our GDT has 5 descritors...
.set GDT_SIZE, (8*GDT_ENTRIES) # GDT length...
.set KERNEL_SECT, 0x1 # Kernel size in sectors...
.set KERNEL_POS_ES, 0x1000 # Initial kernel position (ES:BX)...
.set KERNEL_POS_BX, 0x0
.set STACK_BOT, 0xa0000 # Stack starts at 640K and grows downside...
DavidCooper wrote:
Another issue is whether your kernel's being written to the second sector in the first place. Can you make a disk image available so that I can check it directly?
I cant attach it but you can download it from my repo:
https://github.com/downloads/antoniovaz ... floppy.img. Thanks
I'm generating the image with "cat bootsect kernel > floppy" Should I change this?
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 12:15 pm
by DavidCooper
Your second sector does contain your code, but there's a fault right at the start of it. This is a cut-down copy of it using my own OS within Bochs after loading the whole of your directory track into memory:-
Code: Select all
102 161 16 0 0 0 142 216 142 192 142 224 142 232 142 208 102 199 5 0
128 11 0 70 32 102 199 5 2 128 11 0 105 32 102 199 5 4 128 11
0 114 32 102 199 5 6 128 11 0 101 32 102 199 5 8 128 11 0 98
32 102 199 5 10 128 11 0 105 32 102 199 5 12 128 11 0 114 32 102
199 5 14 128 11 0 100 32 102 199 5 16 128 11 0 32 32 102 199 5
18 128 11 0 79 32 102 199 5 20 128 11 0 83 32 244 235 253 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0... etc.
disk.dir
000 001
002 000
Now I'll translate it for you.
102 161 16 0 0 0 - the 102 is a prefix used to make the 161 instruction bring in two bytes from the four-byte address that follows (instead of bringing in four bytes as it would without the prefix), so it brings in two bytes from the BIOS's interrupt vector table at the bottom of memory, just 16 bytes in. That is clearly not what you want.
142 216 - it then transfers those two bytes, whatever they are, into DS.
142 192 - then into ES.
142 224 - then FS.
142 232 - then GS.
142 208 - then SS.
102 199 5 0 128 11 0 - then it tries to send the two bytes on the next line below to the screen, but it fails because the segment register holds an invalid value.
70 32 (that's a black capital F with a green background).
I won't bother with the rest because they're just the same kind of thing again. Anyway, there's a problem with DATA_SEL, and I don't know how to fix it, but you probably do as you know a lot more about how assemblers work than I do. Clearly you need to get it to use the value of DATA_SEL directly rather than treating it as an address to fetch a value from.
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 12:29 pm
by DLBuunk
Dammit, i should have seen that.
This:
should be this:
That would load 0x0010 into ax, rather than where 0x0010 points too.
Apart from that everything looks fine.
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 12:34 pm
by DavidCooper
DLBuunk wrote:Dammit, i should have seen that.
That's always the trouble with bugs - obvious things tend not to stand out. Anyway, thanks for your input in all this. Antonio seems to be going about everything the right way and is well worth helping. I look forward to seeing where his kernel goes.
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 12:58 pm
by antoniovazquezblanco
DavidCooper wrote:DLBuunk wrote:Dammit, i should have seen that.
That's always the trouble with bugs - obvious things tend not to stand out. Anyway, thanks for your input in all this. Antonio seems to be going about everything the right way and is well worth helping. I look forward to seeing where his kernel goes.
hahahah I didn't see this enormous fault!!!! That is ridiculous
. But even more ridiculous is that it stills not loading the kernel... Let me test a bit more and I will tell you!
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 1:07 pm
by antoniovazquezblanco
I confirm it stills not working. The latest source code is:
Code: Select all
/********************************************************************************/
/* */
/* FIREBIRD OS */
/* */
/* This is a simple asm bootloader for Firebird OS */
/* */
/********************************************************************************/
#################################
# Main code... #
#################################
.code16
.text
.globl _start
.include "kernel/kernel.inc"
_start:
/* Initialize segment registers... */
ljmp $0x0000, $0x7C05
xorw %ax, %ax
movw %ax, %ds
movw %ax, %ss
movw $0x7c00, %sp
/* Clear screen... */
call clear_screen
/* BIOS will copy kernel to es:bx ... */
pushw $KERNEL_POS_ES # KERNEL_POS_ES:KERNEL_POS_BX...
popw %es
movw $KERNEL_POS_BX, %bx
/* Copy function settings... */
movb $0x2, %ah # Copy function...
movb $KERNEL_SECT, %al # Sectors to read...
movb $0x0, %ch # Cylinder 0...
movb $0x2, %cl # Sector 2...
movb $0x0, %dh # Head 0...
movb $0x0, %dl # Device 0...
/* Read... */
int $0x13
jnc read_done
/* Error reading... */
call reset_floppy
jnc _start # Read again...
/* Error reseting floppy... */
jmp error # Die...
read_done:
/* Enable A20 gate... */
cli # Clear interrupts...
inb $0x92, %al # Open al to port 0x92...
or $0x2, %al # Send value 0x2 to al...
outb %al, $0x92 # Close al port 0x92...
sti # Re-enable interrupts...
/* Move the GDT and load it... */
movw $GDT_ADDR>>4, %ax
movw %ax, %es
movw $gdt, %si
xorw %di, %di
movw $GDT_SIZE>>2, %cx
rep movsl
lgdt gdtr
/* Go protected mode... */
cli
movl %cr0, %eax
or $1, %eax
movl %eax, %cr0
ljmp $CODE_SEL, $protected_mode
.code32
protected_mode:
/* Jump to kernel... */
ljmp $CODE_SEL, $((KERNEL_POS_ES<<4)+KERNEL_POS_BX)
.code16
/* If we reach here something went wrong... */
error:
movw $error_str, %si
call print_string
/* Halt execution if we reach here... */
halt:
hlt
jmp halt
#################################
# Functions... #
#################################
reset_floppy:
push %ax # We use ax and dx so save the values...
push %dx
movw $0x0, %ax
movb $0x0, %dl # Drive to reset...
stc # Activate cartage flag...
int $0x13
pop %dx # Restore ax and dx...
pop %ax
ret
print_string:
pusha
movb $0xe, %ah # Teletype function...
.repeat:
lodsb # Get char from str...
cmpb $0, %al # End of string?
je .done
int $0x10 # Exec function...
jmp .repeat # Next char...
.done:
popa
ret
clear_screen:
push %ax
push %cx
push %bx
push %dx
movw $0x6, %ax # Clear screen function...
movw $0x0, %cx # Clear from 0, 0...
movw $0x174f, %dx # To 23, 79...
movb $0x0, %bh # Black background...
int $0x10
pop %dx
pop %cx
pop %bx
pop %ax
ret
#################################
# Data... #
#################################
error_str:
.ascii "[!] Error booting Firebird OS...\0"
#################################
# GDT descriptor... #
#################################
gdtr:
gdtsize: .word gdt_end-gdt-1
gdtbase: .long GDT_ADDR
gdt:
/* Null descriptor... */
.quad 0x0000000000000000
/* Code segment with 4GB flat memory model... */
.quad 0x00cf9a000000ffff
/* Data segment with 4GB flat memory model... */
.quad 0x00cf92000000ffff
/* For future use... */
.quad 0x0000000000000000
.quad 0x0000000000000000
gdt_end:
#################################
# Boot signature... #
#################################
.org 0x1fe
.word 0xaa55
Code: Select all
/********************************************************************************/
/* */
/* FIREBIRD OS */
/* */
/* Here I specify a set of constants (memory addresses...) that my operative */
/* system will use in it's kernel. */
/* */
/********************************************************************************/
.set CODE_SEL, 0x8 # Code segment selector in kernel mode...
.set DATA_SEL, 0x10 # Data segment selector in kernel mode...
.set IDT_ADDR, 0x80000 # TODO: For future use...
.set IDT_SIZE, (256*8) # TODO: For future use...
.set GDT_ADDR, (IDT_ADDR+IDT_SIZE) # GDT starts after IDT...
.set GDT_ENTRIES, 5 # Our GDT has 5 descritors...
.set GDT_SIZE, (8*GDT_ENTRIES) # GDT length...
.set KERNEL_SECT, 0x1 # Kernel size in sectors...
.set KERNEL_POS_ES, 0x1000 # Initial kernel position (ES:BX)...
.set KERNEL_POS_BX, 0x0
.set STACK_BOT, 0xa0000 # Stack starts at 640K and grows downside...
Code: Select all
/********************************************************************************/
/* */
/* FIREBIRD OS */
/* */
/* This is a simple asm testing kernel for Firebird OS */
/* */
/********************************************************************************/
.text
.globl _start
.include "kernel/kernel.inc"
.org 0
_start:
movw $DATA_SEL, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
#movl $STACK_BOT, %esp
movw $0x2046, 0xb8000
movw $0x2069, 0xb8002
movw $0x2072, 0xb8004
movw $0x2065, 0xb8006
movw $0x2062, 0xb8008
movw $0x2069, 0xb800a
movw $0x2072, 0xb800c
movw $0x2064, 0xb800e
movw $0x2020, 0xb8010
movw $0x204f, 0xb8012
movw $0x2053, 0xb8014
halt:
hlt
jmp halt
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 1:17 pm
by DavidCooper
Why have you added a clear_screen routine which may add new bugs, and why have you removed the debug code to print an F to the screen? You can't now tell where it's failing.
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 1:38 pm
by antoniovazquezblanco
DavidCooper wrote:Why have you added a clear_screen routine which may add new bugs, and why have you removed the debug code to print an F to the screen? You can't now tell where it's failing.
You're right, the clear screen has a bug but the kernel stills not loading. I will comment the clear screen routine for future check by myself. Thanks!
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 2:26 pm
by DavidCooper
I've just gone through the machine code from the disk image and nothing is showing up. When you use lgdt you're lining it up on the original version in the boot sector rather than the copy, but that won't stop it working. Your clear_screen routine has the pushes or pops of bx and cx the wrong way round, but again that shouldn't cause a problem here. I don't know if I'm looking at an updated disk image or not, but the second sector starts with 102 161 16 0 0 0 (which won't work), so it's probably still the old version. Please put the new version up. Also, put the debug code back in to print to the screen just before the jump to the kernel, and then run it to see if it gets far enough to display the F.
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 2:38 pm
by antoniovazquezblanco
DavidCooper wrote:I've just gone through the machine code from the disk image and nothing is showing up. When you use lgdt you're lining it up on the original version in the boot sector rather than the copy, but that won't stop it working. Your clear_screen routine has the pushes or pops of bx and cx the wrong way round, but again that shouldn't cause a problem here. I don't know if I'm looking at an updated disk image or not, but the second sector starts with 102 161 16 0 0 0 (which won't work), so it's probably still the old version. Please put the new version up. Also, put the debug code back in to print to the screen just before the jump to the kernel, and then run it to see if it gets far enough to display the F.
The clear screen routine was removed as I plan to do it in other part of the code. The second thing is that I've uploaded the new floppy image and the debug chars are now PM indicating it reached protected mode. In my case it loads until PM.
https://github.com/downloads/antoniovaz ... floppy.img
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 2:46 pm
by DavidCooper
Bingo!
I changed the clear_screen call into three nops and changed the start of the kernel from 102 161 16 0 0 0 to 144 184 16 0 0 0 (which is a nop followed by a mov instruction to load eax with the four following bytes.
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 2:58 pm
by DavidCooper
That was from a previous version, but I've just run your latest disk image in Bochs and it works.
Re: Bug jumping from bootsect to kernel
Posted: Wed Apr 27, 2011 3:00 pm
by antoniovazquezblanco
DavidCooper wrote:Bingo!
I changed the clear_screen call into three nops and changed the start of the kernel from 102 161 16 0 0 0 to 144 184 16 0 0 0 (which is a nop followed by a mov instruction to load eax with the four following bytes.
IT DOESN'T WORK FOR ME!