Page 2 of 3

Re: linux versus multiboot in GRUB2

Posted: Thu Dec 17, 2020 1:56 pm
by jamesread
Ok so now I made a cross compiler and am trying to cross compile. Here is what I got:

Code: Select all

 ~/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

Re: linux versus multiboot in GRUB2

Posted: Thu Dec 17, 2020 2:00 pm
by nexos
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 :) .

Re: linux versus multiboot in GRUB2

Posted: Thu Dec 17, 2020 2:15 pm
by jamesread
OK, I got the kernel to compile with:

Code: Select all

 ~/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: Select all

error: unsupported tag: 0x8.
Is there some kind of problem with boot.S

Re: linux versus multiboot in GRUB2

Posted: Thu Dec 17, 2020 2:40 pm
by nexos
jamesread wrote:OK, I got the kernel to compile with:

Code: Select all

 ~/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: Select all

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

Re: linux versus multiboot in GRUB2

Posted: Thu Dec 17, 2020 3:00 pm
by jamesread
OK. I did that. The error persists.

Re: linux versus multiboot in GRUB2

Posted: Thu Dec 17, 2020 6:30 pm
by Octocontrabass
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.)

Re: linux versus multiboot in GRUB2

Posted: Thu Dec 17, 2020 8:47 pm
by jamesread
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: Select all

/*  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

Re: linux versus multiboot in GRUB2

Posted: Thu Dec 17, 2020 8:56 pm
by jamesread
I updated my kernel.c file to print a 'Hello World' message. I get a blank screen when booting with:

Code: Select all

qemu-system-x86_64 -bios OVMF-pure-efi.fd -cdrom myos.iso
but when booting in legacy mode I get the error:

Code: Select all

error: out of memory

Re: linux versus multiboot in GRUB2

Posted: Fri Dec 18, 2020 3:22 pm
by Octocontrabass
"Out of memory" usually means your kernel is linked at an address that's too high. What does your linker script look like?

Re: linux versus multiboot in GRUB2

Posted: Fri Dec 18, 2020 5:31 pm
by nexos
Here is how you should compile it:
First, run this command

Code: Select all

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: Select all

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: Select all

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: Select all

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

Re: linux versus multiboot in GRUB2

Posted: Sat Dec 19, 2020 11:25 am
by jamesread

Code: Select all

~/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

Re: linux versus multiboot in GRUB2

Posted: Sat Dec 19, 2020 1:58 pm
by nullplan
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.

Re: linux versus multiboot in GRUB2

Posted: Sat Dec 19, 2020 3:33 pm
by jamesread
OK, I fixed the semicolon. Thanks for that. Running with

Code: Select all

qemu-system-x86_64 -bios OVMF-pure-efi.fd -cdrom myos.iso
now gives the error:

Code: Select all

error: no suitable video mode found

Re: linux versus multiboot in GRUB2

Posted: Sat Dec 19, 2020 8:35 pm
by nexos
Trying booting in BIOS mode

Re: linux versus multiboot in GRUB2

Posted: Sat Dec 19, 2020 9:29 pm
by jamesread
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.