I'm using this:kernel (fd0)/boot/grub/kernel.bin
Error 13: Invalid or unsupported executable format
press any key to continue...
to compile it.nasm -f elf -o start.o start.asm
gcc -fno-stack-protector -fno-builtin -nostdinc -O -g -Wall -I. -c -o main.o main.c
gcc -fno-stack-protector -fno-builtin -nostdinc -O -g -Wall -I. -c -o scrn.o scrn.c
ld -T link.ld -o kernel.bin start.o main.o scrn.o
I've searched on google, but I'm still not sure what's wrong.
Could someone please help me.
This is my code:
Code: Select all
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; start.asm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; 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.
[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_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO
MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
; This is the GRUB Multiboot header. A boot signature
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd MULTIBOOT_CHECKSUM
; A call to main (the C kernel) followed by an infinite loop (jmp $)
stublet:
EXTERN cmain ; start of our kernel
call cmain
jmp $
; 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:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Code: Select all
// File: main.c
#include <system.h>
/* some convenient functions - as we don't ahve libc, we must do
everything ourselves */
unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)
{
int i;
for (i=0; i<count;i++) dest[i]=src[i];
return dest;
}
unsigned char *memset(unsigned char *dest, unsigned char val, int count)
{
int i;
for (i=0; i<count;i++) dest[i]=val;
return dest;
}
unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)
{
int i;
for (i=0; i<count;i++) dest[i]=val;
return dest;
}
int strlen(const char *str)
{
int i;
for (i=0;;i++) if (str[i] == '\0') return i;
}
/* We can use this 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. [XXX I still
* have to add devices to the tutorial] */
unsigned char inportb (unsigned short _port)
{
unsigned char rv;
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}
/* We can use this to write to I/O ports to send bytes to
* devices. 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 print stuff
* and then sit in an infinite loop. This will be like our 'idle'
* loop */
void
cmain (unsigned long magic, unsigned long addr)
{
init_video();
puts ((unsigned char*)"hello world!");
/* ...and leave this loop in. Note: there is an endless loop in
* 'start.asm' also, if you accidentally delete this next line */
for (;;);
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
Code: Select all
// File: scrn.c - putting characters on the screen (see comments below)
#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;
#define COLS 80
#define ROWS 24
/* 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 >= ROWS/*25*/)
{
/* Move the current text chunk that makes up the screen
* back in the buffer by a line */
temp = csr_y - ROWS/*25*/ + 1;
memcpy ((unsigned char *)textmemptr,
(const unsigned char *)textmemptr + temp * COLS,
(ROWS/*25*/ - temp) * COLS * 2);
/* Finally, we set the chunk of memory that occupies
* the last line of text to our 'blank' character */
memsetw (textmemptr + (ROWS/*25*/ - temp) * COLS, blank, COLS);
csr_y = ROWS/*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 * COLS + 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 < ROWS/*25*/; i++)
memsetw (textmemptr + i * COLS, blank, COLS);
/* 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 * COLS + 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 >= COLS)
{
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((const char*)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();
}
//////////////////////////////////////////////////////////////////////
Code: Select all
// File: system.h
#ifndef __SYSTEM_H
#define __SYSTEM_H
/* MAIN.C */
extern unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *memset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int 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