Search code examples
c++x86gnu-assemblerosdevgrub

You need to load your kernel first Error


Right now when I select Custom OS , and when i execute my OS from the menu in GRUB I get a purple background:

error: secure boot forbids loading module from (hdo, gpt7)/boot/grub/x86_64-efi/multiboot.mod
error: You need to load your kernel first    
Press any key to continue . . .

.. I don't necessarily understand why this is happening. let me show you my files:

loader.S:

#Global MultiBoot Kernel Recongnzation
.set MAGIC, 0x1BADB002
.set FLAGS , (1<<0 | 1<<1)
.set CHECKSUM, -(MAGIC + FLAGS)

#Putting in object file
.section .multiboot
    .long MAGIC
    .long FLAGS
    .long CHECKSUM


.section .text

    .extern kernelMain
    .globl loader

        loader:
                mov $kernel_stack , %esp
                push %eax
                push %ebx
                call kernelMain

        _eof:
             cli
             hlt 
             jmp _eof


.section .bss
.space 2*1024*1024 #2 MiB
kernel_stack:

Makefile:

GPPARAMS =  -m32 -Iinclude -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -Wno-write-strings
ASPARAMS =  --32
LDPARAMS =  -melf_i386
objects = kernel.o loader.o 

all:
    g++ -m32 -Iinclude -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -Wno-write-strings  -o kernel.o -c kernel.cc
    as $(ASPARAMS) -o loader.o loader.S

mykernel.bin : linker.ld $(objects)
    ld $(LDPARAMS) -T $< -o $@ $(objects)

install: mykernel.bin
    sudo cp $< /boot/mykernel.bin

clean:
      rm $(objects)

kernel.cc:

int strlen(char* str)
{
        int l=0;
        while(str[l]!='\0')l++;
        return l;
}

void printf(char *str)
{
        unsigned short* ViedoMemory = (unsigned short*)0xb8000;

        for(int i=0; str[i]!='\0'; ++i)
                ViedoMemory[i]= (ViedoMemory[i] & 0xFF00)|str[i];
}



extern "C" void kernelMain(void* multiboot_structure, unsigned int magicnumber)
{
    printf("Hello World");
    while(1);
}

linker.ld:

ENTRY(loader)
OUTPUT_FORMAT(elf32-i386)
OUTPUT_ARCH(i386:i386)

SECTIONS
{
  . = 0x0100000;

  .text :
  {
    *(.multiboot)
    *(.text*)
    *(.rodata)
  }

  .data  :
  {
    start_ctors = .;
    KEEP(*( .init_array ));
    KEEP(*(SORT_BY_INIT_PRIORITY( .init_array.* )));
    end_ctors = .;

    *(.data)
  }

  .bss  :
  {
    *(.bss)
  }

  /DISCARD/ : 
  { 
    *(.fini_array*) 
    *(.comment) 
  }
}

Right now how I load it is first do it by makefile:

make
make mykernel.bin
make install

and then ofcourse in the /boot/grub/grub.cfg I added this:

### BEGIN MYKERNEL
menuentry 'Operating System Tut'{
  multiboot /boot/mykernel.bin
  boot
}
### END MYKERNEL ###

Then when I do sudo reboot, and select Operating System Tut from the drop-down list it gives me the error I described before:

error: secure boot forbids loading module from (hdo, gpt7)/boot/grub/x86_64-efi/multiboot.mod

error: You need to load your kernel first

Press any key to continue . . .

Again, I don't understand why the kernel isn't loading first... Help would be appreciated.


Solution

  • Try to turn off the Secure Boot option in your BIOS and see if that gives you a different result. When this option is enabled, the firmware checks if your boot loader is signed and prevents its execution if it isn't, or if its signature doesn't correspond to a key stored in NVRAM, or is blacklisted in the NVRAM.

    See Managing EFI Boot Loaders for Linux: Dealing with Secure Boot.