Page 1 of 1

Program freezes after loading OS through GRUB on bochs...

Posted: Sat Jun 12, 2010 9:21 pm
by Yargh
Hello, I have recently been attempting to develop some sort of an "OS". At first I had been manually writing characters to the vram, however I have just converted the example Pascal console library to C and added it. It compiled with no errors. The only problem is:

Whenever I try to boot into the OS from Bochs, it freezes after typing "boot" in grub.

In the console for bochs, it is showing these errors:
00096092378i[FDD ] read() on floppy image returns 0
00096137333i[FDD ] read() on floppy image returns 0
00096182288i[FDD ] read() on floppy image returns 0
00096227243i[FDD ] read() on floppy image returns 0
00096272198i[FDD ] read() on floppy image returns 0
00096317153i[FDD ] read() on floppy image returns 0
00096362108i[FDD ] read() on floppy image returns 0

Usually it works, but for some reason after I added this library it won't load.

Here is the source code:

kernel.c:

Code: Select all

#include "console.h"

/*Kernel Main*/
void kmain(void* mbd, unsigned int magic){
	if(magic != 0x2BADB002){ /*Not launched using the magic number, 0x1BADB002. For some reason,
	 even though the kernel is launched with 0x1BADB002, it shows up in the kernel as 0x2BADB002*/
		sWriteStr((unsigned char*)"Error 0");
		xpos = 0;
		ypos = ypos + 1;
		
		//Error 0, kernel not launched correctly
		return;
    }
	
    char * boot_loader_name =(char*) ((long*)mbd)[16];

    /* Print startup message */
	clrscr();
	
	sWriteStr((unsigned char*)"Welcome to lOS!");
	xpos = 0;
	ypos = ypos + 1;
}
console.c:

Code: Select all

#include "console.h"

unsigned char *vram = (unsigned char *) 0xb8000;

void clrscr(){
	for(int i = 0; i < 3999; i = i + 1){
		vram[i] = 0x00;
	}
}

void sWriteChar(unsigned char val){
	int offset;
	if(ypos > 24){
		ypos = 0;
	}
	if(xpos > 79){
		xpos = 0;
	}
	
	offset = (xpos << 1) + (ypos * 160);
	vram[offset] = val;
	offset = offset + 1;
	vram[offset] = 0x07;
	offset = offset + 1;
	
	xpos = (offset % 160);
	ypos = (offset - xpos) / 160;
	xpos = xpos >> 1;
	
}

void sWriteStr(unsigned char* val){
	int offset, i;
	
	if(ypos > 24){
		ypos = 0;
	}
	if(xpos > 79){
		xpos = 0;
	}
	
	offset = (xpos << 1) + (ypos * 160);
	i = 0;
	while(val[i] != (char)0x00){
		vram[offset] = val[i];
		offset = offset + 1;
		vram[offset] = 0x07;
		offset = offset + 1;
		i = i + 1;
	}
	
}
console.h:

Code: Select all

int xpos;
int ypos;

void clrscr();

void sWriteChar(unsigned char val);
void sWriteStr(unsigned char* val);
If it makes any difference, I am compiling it on a machine running Ubuntu Server 10.04, and running it in Bochs on OSX.

Thanks in advance for any help...

Re: Program freezes after loading OS through GRUB on bochs..

Posted: Sat Jun 12, 2010 11:22 pm
by gerryg400
What loader are you using ?

Edit: oops sorry I can see it's grub.

Re: Program freezes after loading OS through GRUB on bochs..

Posted: Sat Jun 12, 2010 11:59 pm
by Yargh
It's the same with Qemu, however it loads far faster. It seems to work if I print out one character at a time, however it crashes when I try to get the character from a char* array. Example:

It crashes with this:

Code: Select all

   char* msg = "Hello!";
	sWriteChar(msg[1]);
But it works with this:

Code: Select all

   sWriteChar('e');

Re: Program freezes after loading OS through GRUB on bochs..

Posted: Sun Jun 13, 2010 12:14 am
by gerryg400
Whenever I try to boot into the OS from Bochs, it freezes after typing "boot" in grub.
Usually it works, but for some reason after I added this library it won't load.
It's the same with Qemu, however it loads far faster
however it crashes when I try to get the character from a char* array.
I'm confused, does it freeze, not load, load slowly or crash ?

Re: Program freezes after loading OS through GRUB on bochs..

Posted: Sun Jun 13, 2010 2:16 am
by Combuster
read() on floppy image returns 0
Your floppy image is broken. Make sure its the correct 1.4MB floppy size or emulators won't read large parts of it.

Re: Program freezes after loading OS through GRUB on bochs..

Posted: Sun Jun 13, 2010 10:04 am
by Yargh
By loading, I mean getting to grub and typing "kernel 200+18" and "boot", and by crashing I mean hanging after typing boot and not going through instructions before the "bad" code segment. Sorry for the confusion.
Combuster wrote:
read() on floppy image returns 0
Your floppy image is broken. Make sure its the correct 1.4MB floppy size or emulators won't read large parts of it.
So to make it get one character from an unsigned char *, I need to make sure the floppy is 1.4MB by adding a dummy file? Couldn't I just dd the floppy.img file to the start of a 15mb or so HDD image?