OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:19 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 31 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 1:56 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
Ok so now I made a cross compiler and am trying to cross compile. Here is what I got:

Code:
~/opt/cross/bin/i686-elf-gcc kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
/home/yaakov/opt/cross/lib/gcc/i686-elf/10.2.0/../../../../i686-elf/bin/ld: cannot find crt0.o: No such file or directory
/home/yaakov/opt/cross/lib/gcc/i686-elf/10.2.0/../../../../i686-elf/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 2:00 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
jamesread wrote:
nexos wrote:
My current OS uses Multiboot 2 at the moment.


Is the code for your OS available?

No, I haven't uploaded that kernel yet, but there are plenty of Mulitboot 2 kernels on Github :) .

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 2:15 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
OK, I got the kernel to compile with:

Code:
~/opt/cross/bin/i686-elf-gcc kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra -nostdlib -nostdinc


However, the new image produces the following error in qemu

Code:
error: unsupported tag: 0x8.


Is there some kind of problem with boot.S


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 2:40 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
jamesread wrote:
OK, I got the kernel to compile with:

Code:
~/opt/cross/bin/i686-elf-gcc kernel.c -o kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra -nostdlib -nostdinc


However, the new image produces the following error in qemu

Code:
error: unsupported tag: 0x8.


Is there some kind of problem with boot.S

I had the same error myself. Before each tag, put the line .align 8

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 3:00 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
OK. I did that. The error persists.


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 6:30 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
That means you either missed one of the tags or you put it somewhere other than the beginning of the tag. (Or you didn't rebuild your OS correctly so you're still testing with the version that isn't fixed.)


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 8:47 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
OK, now I'm getting a blank screen. So I guess that means success. For googlers who arrive at this page the fixed boot.S file looks like this:

Code:
/*  boot.S - bootstrap the kernel */
/*  Copyright (C) 1999, 2001, 2010  Free Software Foundation, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#define ASM_FILE        1
#include <multiboot2.h>

/*  C symbol format. HAVE_ASM_USCORE is defined by configure. */
#ifdef HAVE_ASM_USCORE
# define EXT_C(sym)                     _ ## sym
#else
# define EXT_C(sym)                     sym
#endif

/*  The size of our stack (16KB). */
#define STACK_SIZE                      0x4000

/*  The flags for the Multiboot header. */
#ifdef __ELF__
# define AOUT_KLUDGE 0
#else
# define AOUT_KLUDGE MULTIBOOT_AOUT_KLUDGE
#endif
       
        .text

        .globl  start, _start
   .align   8
start:
   .align   8
_start:
        jmp     multiboot_entry

        /*  Align 64 bits boundary. */
        .align  8
       
        /*  Multiboot header. */
multiboot_header:
        /*  magic */
        .long   MULTIBOOT2_HEADER_MAGIC
        /*  ISA: i386 */
        .long   MULTIBOOT_ARCHITECTURE_I386
        /*  Header length. */
        .long   multiboot_header_end - multiboot_header
        /*  checksum */
        .long   -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_ARCHITECTURE_I386 + (multiboot_header_end - multiboot_header))
#ifndef __ELF__
   .align   8
address_tag_start:     
        .short MULTIBOOT_HEADER_TAG_ADDRESS
        .short MULTIBOOT_HEADER_TAG_OPTIONAL
        .long address_tag_end - address_tag_start
        /*  header_addr */
        .long   multiboot_header
        /*  load_addr */
        .long   _start
        /*  load_end_addr */
        .long   _edata
        /*  bss_end_addr */
        .long   _end
   .align   8
address_tag_end:
   .align   8
entry_address_tag_start:       
        .short MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS
        .short MULTIBOOT_HEADER_TAG_OPTIONAL
        .long entry_address_tag_end - entry_address_tag_start
        /*  entry_addr */
        .long multiboot_entry
   .align   8
entry_address_tag_end:
#endif /*  __ELF__ */
   .align   8
framebuffer_tag_start: 
        .short MULTIBOOT_HEADER_TAG_FRAMEBUFFER
        .short MULTIBOOT_HEADER_TAG_OPTIONAL
        .long framebuffer_tag_end - framebuffer_tag_start
        .long 1024
        .long 768
        .long 32
   .align   8
framebuffer_tag_end:
        .short MULTIBOOT_HEADER_TAG_END
        .short 0
        .long 8
   .align   8
multiboot_header_end:
   .align   8
multiboot_entry:
        /*  Initialize the stack pointer. */
        movl    $(stack + STACK_SIZE), %esp

        /*  Reset EFLAGS. */
        pushl   $0
        popf

        /*  Push the pointer to the Multiboot information structure. */
        pushl   %ebx
        /*  Push the magic value. */
        pushl   %eax

        /*  Now enter the C main function... */
        call    EXT_C(cmain)

        /*  Halt. */
        pushl   $halt_message
        call    EXT_C(printf)
       
   .align   8
loop:   hlt
        jmp     loop

   .align   8
halt_message:
        .asciz  "Halted."

        /*  Our stack area. */
        .comm   stack, STACK_SIZE


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Thu Dec 17, 2020 8:56 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
I updated my kernel.c file to print a 'Hello World' message. I get a blank screen when booting with:

Code:
qemu-system-x86_64 -bios OVMF-pure-efi.fd -cdrom myos.iso


but when booting in legacy mode I get the error:

Code:
error: out of memory


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Fri Dec 18, 2020 3:22 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
"Out of memory" usually means your kernel is linked at an address that's too high. What does your linker script look like?


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Fri Dec 18, 2020 5:31 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
Here is how you should compile it:
First, run this command
Code:
i686-elf-gcc -c boot.S -o boot.o

The -c option tells gcc to compile and assemble it only, not to link. Note that you could in theory directly call i686-elf-as
Then compile your kernel file:
Code:
i686-elf-gcc -c kernel.c -o kernel.o -ffreestanding -fno-stack-protector -O0 -g -Wall -Wextra

-ffreestanding tells gcc that the compiled file does not have any standard includes or standard libraries. -fno-stack-protector tells gcc not to imply calls to the stack smashing protector.
Then link the whole thing with
Code:
i686-elf-gcc -Tlink.ld -nostdlib boot.o kernel.o -lgcc

-nostdlib tells gcc that there is no standard library here. -lgcc tell gcc to link with libgcc, as gcc emits calls to libgcc even with -ffreestanding. link.ld is a linker script. It tells ld how to layout the sections. It should look like this
Code:
ENTRY(_start)
SECTIONS
{
    . = 0x100000

    .text ALIGN(4096) : {
        *(.text)
    }
    .data ALIGN(4096) : {
         *(.data)
     }
     .rodata ALIGN(4096) : {
          *(.rodata*)
      }
      .bss ALIGN(4096) : {
           *(.bss)
       }
       end = .;
}

I can answer question you have about this linker script

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Sat Dec 19, 2020 11:25 am 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
Code:
~/opt/cross/bin/i686-elf-gcc -o kernel-file -T link.ld -nostdlib boot.o kernel.o -lgcc
/home/yaakov/opt/cross/lib/gcc/i686-elf/10.2.0/../../../../i686-elf/bin/ld:link.ld:6: syntax error
collect2: error: ld returned 1 exit status


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Sat Dec 19, 2020 1:58 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
Yeah, you're missing a semicolon. Look, if you come back here for every undotted I and uncrossed T, we're going to be here all day. You do have to invest some of your own time on this venture. I found the documentation of the linker script format within five minutes of using a search engine.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Sat Dec 19, 2020 3:33 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
OK, I fixed the semicolon. Thanks for that. Running with

Code:
qemu-system-x86_64 -bios OVMF-pure-efi.fd -cdrom myos.iso


now gives the error:

Code:
error: no suitable video mode found


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Sat Dec 19, 2020 8:35 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
Trying booting in BIOS mode

_________________
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg


Top
 Profile  
 
 Post subject: Re: linux versus multiboot in GRUB2
PostPosted: Sat Dec 19, 2020 9:29 pm 
Offline
Member
Member

Joined: Wed Dec 09, 2020 11:32 am
Posts: 49
When booting in BIOS mode I get no output to screen and then an automatic reboot. This I have tried on hardware also with the same result.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 31 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 56 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group