3

I have this iso image which apparently VirtualBox can not read. I am currently using GRUB And following this tutorial and in the episode of OS-VM Installation. i have screen-recorded the error as i went through the steps here:

https://www.youtube.com/watch?v=ZGnPo34wOAw&feature=youtu.be

here are my files currently:

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

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

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

clean:
      rm $(objects)
      rm -rf iso

qemu_compile: all BoneOS.bin qemu


qemu:
    qemu-system-i386 -kernel BoneOS.bin 

BoneOS.iso: BoneOS.bin
    mkdir iso
    mkdir iso/boot
    mkdir iso/boot/grub
    cp BoneOS.bin iso/boot/BoneOS.bin
    echo 'set timeout=0'                      > iso/boot/grub/grub.cfg
    echo 'set default=0'                     >> iso/boot/grub/grub.cfg
    echo ''                                  >> iso/boot/grub/grub.cfg
    echo 'menuentry "My Operating System" {' >> iso/boot/grub/grub.cfg
    echo '  multiboot /boot/BoneOS.bin'    >> iso/boot/grub/grub.cfg
    echo '  boot'                            >> iso/boot/grub/grub.cfg
    echo '}'                                 >> iso/boot/grub/grub.cfg
    grub-mkrescue -o BoneOS.iso iso

kernel.cc:

#define SWAP(a, b) do { typeof(a) temp = a; a = b; b = temp; } while (0)


static  unsigned short* ViedoMemory =((unsigned short*)0xb8000);

typedef void (*constructor)();
extern "C" constructor start_ctors;
extern "C" constructor end_ctors;
extern "C" void callConstructors()
{
    for(constructor* i = &start_ctors; i != &end_ctors; i++)
        (*i)();
}



class Foo
{
public:
    Foo(int r)
    {
        x=r;
    }

    int getX()
    {
        return x;
    }
private:
    int x=0;
};

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


char* str_cat(char *dest, const char *src)
{

    while (*dest!= '\0')
        *dest++ ;
    do
    {
        *dest++ = *src++;
    }
    while (*src != '\0') ;

    return dest;
}

/* A utility function to reverse a string  */
void reverse(char str[], int length)
{
    int start = 0;
    int end = length -1;
    while (start < end)
    {
        SWAP(*(str+start), *(str+end));
        start++;
        end--;
    }
}

char* itoa(int num, char* str, int base)
{
    int i = 0;
    unsigned int isNegative = 0;

    /* Handle 0 explicitely, otherwise empty string is printed for 0 */
    if (num == 0)
    {
        str[i++] = '0';
        str[i] = '\0';
        return str;
    }

    /* In standard itoa(), negative numbers are handled only with
       base 10. Otherwise numbers are considered unsigned. */
    if (num < 0 && base == 10)
    {
        isNegative = 1;
        num = -num;
    }

    /* Process individual digits */
    while (num != 0)
    {
        int rem = num % base;
        str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
        num = num/base;
    }

    /* If number is negative, append '-' */
    if (isNegative)
        str[i++] = '-';

    str[i] = '\0'; /* Append string terminator */

    /* Reverse the string */
    reverse(str, i);

    return str;
}

void printf(char *str)
{

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




Foo bar(2);
extern "C" void kernelMain(void* multiboot_structure, unsigned int magicnumber)
{

   int *multiboot_struct_int = (int*)multiboot_structure;
   char *str = "",*ram="RAM";
   char* RAM_AM = itoa(*multiboot_struct_int,str,10);

   printf(itoa(bar.getX(),str,10));
   while(1);
}

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
    .extern callConstructors
    .globl loader

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

        _eof:
             cli
             hlt 
             jmp _eof


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

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) 
  }
}

iso/boot/grub/grub.cfg:

set timeout=0
set default=0

menuentry "My Operating System" {
  multiboot /boot/BoneOS.bin
  boot
}

Above code for some reason when ran via VirtualBox(video above shows you how i did it) , gets me the error 'Error: Could not read from Bootable Medium'. Help would be appreciated

Edit

The catch here though is for some reason it works on qemu when i do:

qemu-system-i386 -kernel BoneOS.bin 

But does not show me the menu selection

With Qemu

When i run the command:

qemu-system-i386 -cdrom BoneOS.iso

I get no Bootable Device just like Virtual Box, here is a viedo illustrating it:

https://www.youtube.com/watch?v=8D6LtcMoztw&feature=youtu.be

6
  • Much better! This time the question makes sense. Commented Jul 19, 2016 at 23:20
  • @MichaelPetch i just type 'virtualbox' . I was kinda speedy on the commands sorry. Commented Jul 19, 2016 at 23:26
  • If you want QEMU to boot from an ISO try this: qemu-system-i386 -cdrom BoneOS.iso Commented Jul 19, 2016 at 23:32
  • Edited question @MichaelPetch . Gives me No Bootable Device Commented Jul 19, 2016 at 23:44
  • Have a feeling that your stack size (2mb) in the BSS segment is possibly causing GRUB to work improperly. Try changing the stack size to something smaller. Try 16kb with .space 16*1024 . Commented Jul 20, 2016 at 1:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.