3

我有这个显然 VirtualBox 无法读取的 iso 图像。我目前正在使用 GRUB 并遵循教程和 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

内核.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);
}

装载机.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:

链接器.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
}

上面的代码由于某种原因通过 VirtualBox 运行时(上面的视频向您展示了我是如何做到的),让我收到错误“错误:无法从可启动媒体读取”。帮助将不胜感激

编辑

不过,这里的问题是由于某种原因,当我这样做时,它可以在 qemu 上运行:

qemu-system-i386 -kernel BoneOS.bin 

但不显示菜单选择

与 Qem

当我运行命令时:

qemu-system-i386 -cdrom BoneOS.iso

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

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

4

0 回答 0