[SOLVED]C Kernel can't be loaded using GRUB (Error 13)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

[SOLVED]C Kernel can't be loaded using GRUB (Error 13)

Post by mike20123 »

Hi OS Devolopers and thank you "osdev.org" for providing great infos and tutorials.
I have learned so many things from this web site
I am new here and i need some help :( .
Some infos for what i learned from this great web site:-
I learned to make a simple OS (512 byte).
I learned how to use BIOS interrupts such as printing, reading sectors and load them in memory (simple kernel).
I learned how to setup GDT and entering the protected mode (Pmode).
I learned how to print characters in Pmode using assembly.
And i figured out how to install mouse (using assembly) in Pmode (not BIOS).
I was planning to program my kernel in C instead of assembly
Because most tutorials out there are mostly in C and C++,
And another thing is that i have used c++ for game programming (DirectX) kinda low level programming huh!.
So i think i am kinda good in c and c++.

----------------------------------------
Well...here is my problem
I have followed this tutorial in this link
http://wiki.osdev.org/Bare_bones

Everything went just fine (compiling and linking)
But when i boot my kernel in "bochs" and typing "kernel 200+18" in GRUB Command Line it says:
"Error 13: Invalid or unsupported executable format"
I checked the files and the tutorial over and over again and i have searched this forum and FAQs
And i found nothing O_O



Please guys i need help i am stuck in this for a week
Thanks In Advance
Last edited by mike20123 on Sun Aug 14, 2011 9:00 am, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by Combuster »

If you did a forum search, you might have seen the standard replies for diagnosing the problem. One of these is even mentioned on the wiki page itself:

1) your complete set of build instructions
2) the output of mbcheck on your kernel binary
3) the output of i586-elf-objdump -x of your kernel binary
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by mike20123 »

well.
Thank you very much for the fast reply
as i said before i have followed the tutorial from the OS dev wiki
http://wiki.osdev.org/Bare_bones
the code for loader.s:-

Code: Select all

global loader           ; making entry point visible to linker
extern _kmain            ; kmain is defined elsewhere
 
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0                   ; align loaded modules on page boundaries
MEMINFO     equ  1<<1                   ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002           ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)        ; checksum required
 
section .text
align 4
MultiBootHeader:
   dd MAGIC
   dd FLAGS
   dd CHECKSUM
 
; reserve initial kernel stack space
STACKSIZE equ 0x4000                  ; that's 16k.
 
loader:
   mov esp, stack+STACKSIZE           ; set up the stack
   push eax                           ; pass Multiboot magic number
   push ebx                           ; pass Multiboot info structure
 
   call  _kmain                       ; call kernel proper
 
   cli
hang:
   hlt                                ; halt machine should kernel return
   jmp   hang
 
section .bss
align 4
stack:
   resb STACKSIZE                     ; reserve 16k stack on a doubleword boundary
code for kernel.c:-

Code: Select all

void kmain( void* mbd, unsigned int magic )
{
   if ( magic != 0x2BADB002 )
   {
      /* Something went not according to specs. Print an error */
      /* message and halt, but do *not* rely on the multiboot */
      /* data structure. */
   }
 
   /* You could either use multiboot.h */
   /* (http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#multiboot_002eh) */
   /* or do your offsets yourself. The following is merely an example. */ 
   char * boot_loader_name =(char*) ((long*)mbd)[16];
 
   /* Print a letter to screen to see everything is working: */
   unsigned char *videoram = (unsigned char *) 0xb8000;
   videoram[0] = 65; /* character 'A' */
   videoram[1] = 0x07; /* forground, background color. */
 
   /* Write your kernel here. */
}
script of linker.ld:-

Code: Select all

ENTRY (loader)

SECTIONS{
    . = 0x00100000;

    .text :{
        *(.text)
    }

    .rodata ALIGN (0x1000) : {
        *(.rodata)
    }

    .data ALIGN (0x1000) : {
        *(.data)
    }

    .bss : {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
the code for "Create_Kernel.bat":-

Code: Select all

nasm -f elf -o loader.o loader.s
gcc -o kernel.o -c kernel.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs
ld -T linker.ld -o kernel.bin loader.o kernel.o
rem Windows, DOS
copy /b stage1+stage2+pad+kernel.bin   floppy.img
pause
and i have my project files in
C_Kernel.rar
(54.6 KiB) Downloaded 65 times
and
again thank you "Combuster"
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by xenos »

Have you checked the sizes of stage1, stage2, pad and kernel.bin? Did you make sure that your kernel really starts at block 200 and that it is 18 blocks large? (Where each block is 512 bytes.) Did you build a cross compiler to generate ELF output or are you using native Windows tools (which won't work because they generate PE files, which GRUB cannot load without additional information).
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by egos »

mike20123, you have PE format.

Code: Select all

Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000  4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00  MZђ.........яя..
00000010  B8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00  ё.......@.......
00000020  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000030  00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 00  ............Ђ...
00000040  0E 1F BA 0E 00 B4 09 CD 21 B8 01 4C CD 21 54 68  ..є..ґ.Н!ё.LН!Th
00000050  69 73 20 70 72 6F 67 72 61 6D 20 63 61 6E 6E 6F  is program canno
00000060  74 20 62 65 20 72 75 6E 20 69 6E 20 44 4F 53 20  t be run in DOS 
00000070  6D 6F 64 65 2E 0D 0D 0A 24 00 00 00 00 00 00 00  mode....$.......
00000080  50 45 00 00 4C 01 02 00 56 81 45 4E 00 04 00 00  PE..L...VЃEN....
00000090  1D 00 00 00 E0 00 07 03 0B 01 02 15 00 02 00 00  ....а...........
000000A0  00 00 00 00 00 40 00 00 0C 00 D0 FF 00 00 D0 FF  .....@....Ря..Ря
000000B0  00 00 00 00 00 00 40 00 00 10 00 00 00 02 00 00  ......@.........
000000C0  04 00 00 00 01 00 00 00 04 00 00 00 00 00 00 00  ................
000000D0  44 40 D0 FF 00 02 00 00 BB EA 00 00 03 00 00 00  D@Ря....»к......
000000E0  00 00 20 00 00 10 00 00 00 00 10 00 00 10 00 00  .. .............
000000F0  00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00  ................
00000100  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000110  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000120  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000130  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000140  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000150  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000160  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000170  00 00 00 00 00 00 00 00 2E 74 65 78 74 00 00 00  .........text...
00000180  44 00 00 00 00 00 D0 FF 00 02 00 00 00 02 00 00  D.....Ря........
00000190  00 00 00 00 00 00 00 00 00 00 00 00 20 00 50 60  ............ .P`
000001A0  2E 62 73 73 00 00 00 00 00 40 00 00 44 00 D0 FF  [email protected].Ря
000001B0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000001C0  00 00 00 00 80 00 30 C0 00 00 00 00 00 00 00 00  ....Ђ.0А........
000001D0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000001E0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
000001F0  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000200  02 B0 AD 1B 03 00 00 00 FB 4F 52 E4 .. .. .. ..
Try to use flag 0x10000 and five additional fields:

Code: Select all

; --------------------
; Kernel stub for GRUB
; --------------------

MBH_MAGIC equ 0x1BADB002
MBH_FLAGS equ 0x10000

  use32

  org 0x100000
load_addr:
header_addr:
  dd MBH_MAGIC
  dd MBH_FLAGS
  dd -MBH_MAGIC-MBH_FLAGS
  dd header_addr
  dd load_addr
  dd load_end_addr
  dd bss_end_addr
  dd entry_addr

entry_addr:
  jmp $

load_end_addr:

bss_end_addr:
Or use ELF format.

Also you can try to use FAT instead "Raw FS":
[Solved] kernel.bin->floppy img with GRUB
[SOLVED]: Building floppy.img
If you have seen bad English in my words, tell me what's wrong, please.
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by mike20123 »

thank you egos and XenOS for your reply. :P
egos:
can you please tell me how to make gcc create an ELF file
because i want to know if i am wrong or the tutorial in the wiki,
and if the tutorial has some mistakes we should fix them quickly
for other OS devolopers.
because i didn't create those files by my self
loader.s
kernel.c
linker.ld
i just copy the code from the wiki tutorial.
forXenOS:-
as i said before the files creation has been done by tutorial .
i just followed them carefuly,
and if you check my project files in the attachments
you will see i am using GCC ,LD and NASM.
and guys i don't have that knowldge yet.
i didn't do anything additional to the files i just followed the tutorial in the wiki.
it would be better to fix the code not only for me for other people around the world.
i think if we fix this problem i am going to change the topic of this to "[SOLVED]C Kernel can't be loaded using GRUB (Error 13)",
and other people will not be stuck like me for weeks and this problem is in tutorial implementation.
i hope the wiki tutorial doesn't have mistakes.
and thank you guys.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by egos »

If you have seen bad English in my words, tell me what's wrong, please.
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by mike20123 »

i have modified my linker.ld to:-

Code: Select all

ENTRY (_kmain)
OUTPUT_FORMAT("elf32-i386")
STARTUP(loader.o)
SECTIONS{
    . = 0x00100000;

    .text :{
        *(.text)
    }

    .rodata ALIGN (0x1000) : {
        *(.rodata)
    }

    .data ALIGN (0x1000) : {
        *(.data)
    }

    .bss : {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
but every time i add something i got
ld: cannot perform PE operations on non PE output file 'kernel.bin'.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by xenos »

The reason is that ld now is supposed to output an ELF file, but the input files generated by gcc and nasm are still in PE format. The best thing to do is using a GCC Cross-Compiler.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by mike20123 »

hi
i am using "MinGW" Windows platform
is it good enough :| ?
if not,ca you please give me a download link
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by xenos »

MinGW's compilers will not give you any ELF output. So you either follow egos's advice and include an "aout kludge" in your multiboot header (then it will work with PE files) or you get yourself a cross compiler. There is no download - you need to build one yourself. The instructions are in the wiki:

GCC Cross-Compiler
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by mike20123 »

i can't get the GCC binary files and i have to build it by my self. :shock:
is it illegal or something? :?: :roll:
and please XenOS
if you could please modify my code files with the right code and post it here so me and others can find it. =P~
and please don't say "do it your self" because i am still new in this field#-o :cry: :oops:
i have seen so much c kernels in the web are mostly in ELF format
if i my kernel was PE is it okay to run the C code that was compiled and linked to ELF format?
-----------------------------------------------------------------
and thank you "XenOS" for being active in my topic i really appreciate it :mrgreen:
i know i have been asking kinda extremely annoying questions.
Haroogan
Member
Member
Posts: 28
Joined: Thu Aug 04, 2011 1:10 pm

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by Haroogan »

Hey mike, I'm currently building Cross Compiler under Windows with MinGW, you better start reading similar posts carefully before asking this. Look at my post. Read all my questions and XenOS's answers there to understand the internals and how things fit together, then you will understand why you need Cross Compiler. If you have troubles building Cross Compiler under Windows with MinGW - ask me there.

Edit:
if you could please modify my code files with the right code and post it here so me and others can find it. =P~
and please don't say "do it your self" because i am still new in this field#-o :cry: :oops:
lol, mate nobody is gonna do it for you... [-X You have to do everything by yourself (perhaps with some tips from others), otherwise you will never realize how it works. Sorry, but your request to XenOS looks ridiculous, bold and childish :!:
mike20123
Posts: 15
Joined: Sat Aug 13, 2011 3:18 pm

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by mike20123 »

thank you "Haroogan" for your reply
yes i know asking someone to do almost everything for me is kinda bad :(
but let me tell you something i am not kinda guy who just copy and paste others code with out understanding for what is happening :roll: :|
i just need some help somthing like "kick start" :lol:

what do you mean by build gcc cross compiler your self is download the source files and build it using compiler or IDE(visual studio)? :?:

i just want to know one thing
i want to know where does the problem come from ?
is it the code ,linker script or compiler?
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: C Kernel can't be loaded using GRUB (Error 13)

Post by xenos »

mike20123 wrote:if you could please modify my code files with the right code and post it here so me and others can find it. =P~
No, you really need to learn how to do this yourself. That's the first step towards OS development.

BTW, it's already in this post.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply