Page 1 of 1

GRUB 13: AGAIN

Posted: Tue Jun 19, 2007 1:42 pm
by theallinone
Hi everybody.
I am a quite experienced C/C++ programmer and I have been developing my own software.
Wtvr, iI decided to have a go at OS development and I started googling. I knew i had to know ASM, i just thought, let me get happy enough to get a 'Hello World' kernel and then learn asm so that I can totally understand OS programming.
Yes, i know, it's kinda stupid to do this but that's me...
Anyway, i have a WinXPPro and i tried to compile these tutorials:

http://www.osdever.net/bkerndev/

Unfortunely I couldn't get it work.
ld kept giving me a variety of errors which i realized involved NASM for windows...yeyeye...

I mounted Ubuntu on VMware and set the floppy to be mine.
It all went well and it was doing great (i guess, 'cause the kernel booted) until i added scrn.c . Most precisely the problem must of had started when I added the puts(); calls in main.c.

I read this article, but I am linking correctly, I guess...

http://www.osdev.org/phpBB2/viewtopic.php?t=7725

ld -T link.ld -o kernel.bin start.o main.o scrn.o
I have the exact code in the following file (but only for printing OnScreen, all the rest I haven't done)

http://www.osdever.net/bkerndev/Sources/main.c

I hope you can help me, plz, I have been trying it all.

OH and for those of you who don't know what GRUB 13 means:

"Error 13: Invalid or unsupported executable format". (in GRUB)

edit: I am using X86 architecture :P

Posted: Tue Jun 19, 2007 2:56 pm
by Combuster
Use a hex editor to see wether the multiboot header is indeed aligned and in the first 8K of the binary. If it isn't, try putting the multiboot header into a separately named section and link that first.

Posted: Tue Jun 19, 2007 3:18 pm
by theallinone
ok...sorry for my extreme noobish but...how am i going to do that?

I have XVI32 and i opened my kernel.bin:

Image

I found that there is obviously more and i don't think i understand it so, here is the file: http://theallinone.ifastnet.com/kernel.bin



I repeat, I am noob. Until here i had been using an IDE and barely used a console compilation. I let my IDE do it...so I did not quite get what you said, sry again...

Posted: Tue Jun 19, 2007 4:17 pm
by Tyler
Perhaps it is just my infinite wisdom that makes this seem obvious to me, whereas to others it may appear something complicated is wrong... but don't you think maybe something is missing from that file, given thats its entire content is the words "Hello World"?

I advise you see exactly what it is you are assembling/compiling if anything at all.

Posted: Tue Jun 19, 2007 4:34 pm
by theallinone
Well, i also think that it is quite obvious, but it is not the only content of the file, which is the reason why i gave you a link to download it...

I found something else in the bottom (there may be more, but here's what i found):

Image

Posted: Tue Jun 19, 2007 4:49 pm
by theallinone
Well, I'll just post the entire process from my Ubuntu:

start.asm:

Code: Select all

; bkerndev - Bran's Kernel Development Tutorial
; By:   Brandon F. ([email protected])
; Desc: Kernel entry point, stack, and Interrupt Service Routines.
;
; Notes: No warranty expressed or implied. Use at own risk.
;
; This is the kernel's entry point. We could either call main here,
; or we can use this to setup the stack or other nice stuff, like
; perhaps setting up the GDT and segments. Please note that interrupts
; are disabled at this point: More on interrupts later!
[BITS 32]
global start
start:
    mov esp, _sys_stack     ; This points the stack to our new stack area
    jmp stublet

; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
ALIGN 4
mboot:
    ; Multiboot macros to make a few lines later more readable
    MULTIBOOT_PAGE_ALIGN	equ 1<<0
    MULTIBOOT_MEMORY_INFO	equ 1<<1
    MULTIBOOT_AOUT_KLUDGE	equ 1<<16
    MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
    MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
    MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
    EXTERN code, bss, end

    ; This is the GRUB Multiboot header. A boot signature
    dd MULTIBOOT_HEADER_MAGIC
    dd MULTIBOOT_HEADER_FLAGS
    dd MULTIBOOT_CHECKSUM
    
    ; AOUT kludge - must be physical addresses. Make a note of these:
    ; The linker script fills in the data for these ones!
    dd mboot
    dd code
    dd bss
    dd end
    dd start

; This is an endless loop here. Make a note of this: Later on, we
; will insert an 'extern _main', followed by 'call _main', right
; before the 'jmp $'.
stublet:
    extern main
    call main
    jmp $


; Shortly we will add code for loading the GDT right here!


; In just a few pages in this tutorial, we will add our Interrupt
; Service Routines (ISRs) right here!



; Here is the definition of our BSS section. Right now, we'll use
; it just to store the stack. Remember that a stack actually grows
; downwards, so we declare the size of the data before declaring
; the identifier '_sys_stack'
SECTION .bss
    resb 8192               ; This reserves 8KBytes of memory here
_sys_stack:
system.h:

Code: Select all

#ifndef __SYSTEM_H
#define __SYSTEM_H
#define size_t int /*Now this is because of the prototypes i got from the website i mentioned...is it correct?*/
/* MAIN.C */
extern void *memcpy(void *dest, const void *src, size_t count);
extern void *memset(void *dest, char val, size_t count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count);
extern size_t strlen(const char *str);
extern unsigned char inportb (unsigned short _port);
extern void outportb (unsigned short _port, unsigned char _data);
/* SCRN.C */
extern void cls();
extern void putch(unsigned char c);
extern void puts(unsigned char *str);
extern void settextcolor(unsigned char forecolor, unsigned char backcolor);
extern void init_video();
#endif
main.c:

Code: Select all

#include <system.h>
/* You will need to code these up yourself!  */

/*void *memcpy(void *dest, const void *src, int count)

{

    const char *sp = (const char *)src;

    char *dp = (char *)dest;

    for(; count != 0; count--) *dp++ = *sp++;

    return dest;

}



void *memset(void *dest, char val, int count)

{

    char *temp = (char *)dest;

    for( ; count != 0; count--) *temp++ = val;

    return dest;

}



unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)

{

    unsigned short *temp = (unsigned short *)dest;

    for( ; count != 0; count--) *temp++ = val;

    return dest;

}



int strlen(const char *str)

{

	const char * q = str;

	while (* q++);

		return (q-str+1);

}*/

void *memcpy(void *dest, const void *src, size_t count)
{
    const char *sp = (const char *)src;
    char *dp = (char *)dest;
    for(; count != 0; count--) *dp++ = *sp++;
    return dest;
}

void *memset(void *dest, char val, size_t count)
{
    char *temp = (char *)dest;
    for( ; count != 0; count--) *temp++ = val;
    return dest;
}

unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count)
{
    unsigned short *temp = (unsigned short *)dest;
    for( ; count != 0; count--) *temp++ = val;
    return dest;
}

size_t strlen(const char *str)
{
    size_t retval;
    for(retval = 0; *str != '\0'; str++) retval++;
    return retval;
}

/* We will use this later on for reading from the I/O ports to get data
*  from devices such as the keyboard. We are using what is called
*  'inline assembly' in these routines to actually do the work */
unsigned char inportb (unsigned short _port)
{
    unsigned char rv;
    __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
    return rv;
}

/* We will use this to write to I/O ports to send bytes to devices. This
*  will be used in the next tutorial for changing the textmode cursor
*  position. Again, we use some inline assembly for the stuff that simply
*  cannot be done in C */
void outportb (unsigned short _port, unsigned char _data)
{
    __asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}

/* This is a very simple main() function. All it does is sit in an
*  infinite loop. This will be like our 'idle' loop */
void main()
{
    /* You would add commands after here */
    init_video();
    puts("Hello World");
    /* ...and leave this loop in. There is an endless loop in
    *  'start.asm' also, if you accidentally delete this next line */
    for (;;);
}
scrn.c:

Code: Select all

/* bkerndev - Bran's Kernel Development Tutorial
*  By:   Brandon F. ([email protected])
*  Desc: Screen output functions for Console I/O
*
*  Notes: No warranty expressed or implied. Use at own risk. */
#include <system.h>

/* These define our textpointer, our background and foreground
*  colors (attributes), and x and y cursor coordinates */
unsigned short *textmemptr;
int attrib = 0x0F;
int csr_x = 0, csr_y = 0;

/* Scrolls the screen */
void scroll(void)
{
    unsigned blank, temp;

    /* A blank is defined as a space... we need to give it
    *  backcolor too */
    blank = 0x20 | (attrib << 8);

    /* Row 25 is the end, this means we need to scroll up */
    if(csr_y >= 25)
    {
        /* Move the current text chunk that makes up the screen
        *  back in the buffer by a line */
        temp = csr_y - 25 + 1;
        memcpy (textmemptr, textmemptr + temp * 80, (25 - temp) * 80 * 2);

        /* Finally, we set the chunk of memory that occupies
        *  the last line of text to our 'blank' character */
        memsetw (textmemptr + (25 - temp) * 80, blank, 80);
        csr_y = 25 - 1;
    }
}

/* Updates the hardware cursor: the little blinking line
*  on the screen under the last character pressed! */
void move_csr(void)
{
    unsigned temp;

    /* The equation for finding the index in a linear
    *  chunk of memory can be represented by:
    *  Index = [(y * width) + x] */
    temp = csr_y * 80 + csr_x;

    /* This sends a command to indicies 14 and 15 in the
    *  CRT Control Register of the VGA controller. These
    *  are the high and low bytes of the index that show
    *  where the hardware cursor is to be 'blinking'. To
    *  learn more, you should look up some VGA specific
    *  programming documents. A great start to graphics:
    *  http://www.brackeen.com/home/vga */
    outportb(0x3D4, 14);
    outportb(0x3D5, temp >> 8);
    outportb(0x3D4, 15);
    outportb(0x3D5, temp);
}

/* Clears the screen */
void cls()
{
    unsigned blank;
    int i;

    /* Again, we need the 'short' that will be used to
    *  represent a space with color */
    blank = 0x20 | (attrib << 8);

    /* Sets the entire screen to spaces in our current
    *  color */
    for(i = 0; i < 25; i++)
        memsetw (textmemptr + i * 80, blank, 80);

    /* Update out virtual cursor, and then move the
    *  hardware cursor */
    csr_x = 0;
    csr_y = 0;
    move_csr();
}

/* Puts a single character on the screen */
void putch(unsigned char c)
{
    unsigned short *where;
    unsigned att = attrib << 8;

    /* Handle a backspace, by moving the cursor back one space */
    if(c == 0x08)
    {
        if(csr_x != 0) csr_x--;
    }
    /* Handles a tab by incrementing the cursor's x, but only
    *  to a point that will make it divisible by 8 */
    else if(c == 0x09)
    {
        csr_x = (csr_x + 8) & ~(8 - 1);
    }
    /* Handles a 'Carriage Return', which simply brings the
    *  cursor back to the margin */
    else if(c == '\r')
    {
        csr_x = 0;
    }
    /* We handle our newlines the way DOS and the BIOS do: we
    *  treat it as if a 'CR' was also there, so we bring the
    *  cursor to the margin and we increment the 'y' value */
    else if(c == '\n')
    {
        csr_x = 0;
        csr_y++;
    }
    /* Any character greater than and including a space, is a
    *  printable character. The equation for finding the index
    *  in a linear chunk of memory can be represented by:
    *  Index = [(y * width) + x] */
    else if(c >= ' ')
    {
        where = textmemptr + (csr_y * 80 + csr_x);
        *where = c | att;	/* Character AND attributes: color */
        csr_x++;
    }

    /* If the cursor has reached the edge of the screen's width, we
    *  insert a new line in there */
    if(csr_x >= 80)
    {
        csr_x = 0;
        csr_y++;
    }

    /* Scroll the screen if needed, and finally move the cursor */
    scroll();
    move_csr();
}

/* Uses the above routine to output a string... */
void puts(unsigned char *text)
{
    int i;

    for (i = 0; i < strlen(text); i++)
    {
        putch(text[i]);
    }
}

/* Sets the forecolor and backcolor that we will use */
void settextcolor(unsigned char forecolor, unsigned char backcolor)
{
    /* Top 4 bytes are the background, bottom 4 bytes
    *  are the foreground color */
    attrib = (backcolor << 4) | (forecolor & 0x0F);
}

/* Sets our text-mode VGA pointer, then clears the screen for us */
void init_video(void)
{
    textmemptr = (unsigned short *)0xB8000;
    cls();
}
link.ld:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
build.sh (linux batch file :P):

Code: Select all

echo Now assembling, compiling, and linking your kernel:
echo NASM is assembling...
echo start.asm...
nasm -f aout -o start.o start.asm
#echo start.asm...done
#Remember this spot here: We will add 'gcc' commands here to compile C sources
#echo GCC is compiling...
echo main.c...
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o main.o main.c
echo main.c...done
echo scrn.c...
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o scrn.o scrn.c
echo scrn.c...done
#This links all your files. Remember that as you add *.o files, you need to
#add them after start.o. If you don't add them at all, they won't be in your kernel!
echo LD is linking...
ld -T link.ld -o kernel.bin start.o scrn.o main.o
echo Now copying kernel.bin to floppy0, plese make sure this device is mounted...
cp kernel.bin /media/floppy0/
echo Done!

Posted: Tue Jun 19, 2007 5:08 pm
by nick8325
I think you might need to change the line *(.rodata) in your linker script to *(.rodata*). If that doesn't work, can you run objdump -h main.o and post the output?

Posted: Tue Jun 19, 2007 5:40 pm
by theallinone
I think 'Hello World' never felt this good!!
Thank you very much it was the linker script. Horray for Hello World :)

EDIT: Plus I ran XVI32 again and:

Image

Now the EXTREMELY HUGE SIZE I had has been worked out. It was supposed to be around 10 k... not 1 MB!!

Once again, ty :)

Posted: Wed Jun 20, 2007 3:47 am
by AndrewAPrice
I had a similar problem once. It seemed as if GCC actually 'executed' my OS. My kernel.bin was an ASCII file the was something along the lines of:

Code: Select all

Kernel... OK
Protected Mode... OK
Setting up interupts... OK
Setting up keyboard... OK
Starting kernel thread... Welcome to kernel thread!
@: _
:? I forgot how I fixed it