grub-mkrescue无法创建可引导的CD映像

时间:2016-07-19 23:02:33

标签: c++ x86 virtualbox osdev bootable

我有这个iso图像,显然VirtualBox无法读取。我目前正在使用GRUB并遵循this教程和OS-VM安装的一集。我在屏幕上记录了错误,因为我完成了以下步骤:

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

目前我的文件是:

生成文件:

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 /引导/平头/的grub.cfg:

set timeout=0
set default=0

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

以上代码由于某些原因通过VirtualBox运行(上面的视频显示我是如何做到的),得到错误'错误:无法从可启动媒体读取'。帮助将不胜感激

修改

虽然这里的问题是由于某种原因,当我这样做时,它适用于qemu:

qemu-system-i386 -kernel BoneOS.bin 

但没有显示菜单选择

使用Qemu

当我运行命令时:

qemu-system-i386 -cdrom BoneOS.iso

我没有像Virtual Box那样得到可启动设备,这里有一个说明它的视频:

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

0 个答案:

没有答案