4

There are a lot of question on stackoverflow with the similar title. I read all of them, but none of them answers my problem. This is why I opened this question.

I am creating an operating system in assembler and C. I found that I must compile C code to binary format, extract text section and save it as a file, then convert it to ISO, then mount it to virtual optical dive of diskete and then load my OS in VirtualBox. So, that is a lot of work I want to avoid. I don't want to convert my binary file to ISO every time.

So, I decided to put the binary machine code of my OS to virtual hard drive (VDI file) and then set it to the top of boot order and load it instead of loading from virtual optical drive ISO.

I was researching how VDI works and I found that it is usually dinamically allocated and that only the beginning of data is stored. So, the start of VDI represents a header and the the rest is actual data stored on virtual drive. So, I found that data starts at some address (in my case it is 0x00200000 from the start of the VDI file).

Then, I basically filled from that address to the end of VDI file with pattern 55 AA. So, I suppose it now means that the disk is bootable (because at the end of first sector is still signature 55 AA).

I started virtual machine and it says:

No bootable medium found! System halted

Is there any way to solve this? Why is my virtual disk still not bootable?

Edit

Here is actual VDI file: 1.vdi

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
patak daca
  • 49
  • 3
  • The error says the medium is not _bootable_; it does not speak about mountability. What did you do to make your medium bootable? – Lightness Races in Orbit Apr 23 '17 at 01:43
  • @BoundaryImposition. Fixed typo. – patak daca Apr 23 '17 at 01:44
  • @BoundaryImposition. *"What did you do to make your medium bootable?"* - I filled the whole virtual drive with pattern `55 AA`. – patak daca Apr 23 '17 at 01:45
  • @Olaf. Thank you for edit. – patak daca Apr 23 '17 at 01:56
  • And what is the virtual computer supposed to do with a whole drive filled with `55 AA`? – Lightness Races in Orbit Apr 23 '17 at 13:30
  • @BoundaryImposition : I suspect at a minimum that if he filled the drive with 0xaa55 he was expecting that the drive would be deemed bootable and the error wouldn't show up (that wouldn't make for a useful bootloader though). He may have not known exactly where to put the boot signature so he filled the drive as an experiment. At least that is the feeling I got when I read it. – Michael Petch Apr 23 '17 at 13:51
  • 1
    @MichaelPetch: I think you're right - the expectation comes from a misconception of what "bootable" means or, at the very least, an assumption that a different error message would be displayed next. – Lightness Races in Orbit Apr 23 '17 at 14:15

1 Answers1

7

You don't provide a minimal complete verifiable example showing a bootloader and how you get it into a VDI. But at a minimum you'll need to place 0xAA55 in the last 2 bytes of the master boot record. The example below creates a simple bootloader; creates a 2MiB raw image; places the bootloader in the raw image; and converts the raw image to a VDI.

boot.asm:

BITS 16
ORG 0x7C00

    xor ax, ax
    mov ds, ax
    mov ss, ax       ; Stack below bootloader
    mov sp, 0x7c00

    mov ax, 0xb800   ; Video segment b800
    mov es, ax

    ; Print Hello with white on light magenta
    mov word [es:0x0], 0x57 << 8 | 'H'
    mov word [es:0x2], 0x57 << 8 | 'e'
    mov word [es:0x4], 0x57 << 8 | 'l'
    mov word [es:0x6], 0x57 << 8 | 'l'
    mov word [es:0x8], 0x57 << 8 | 'o'

    ; End with infinite loop
    cli
endloop:
    hlt
    jmp endloop

; Fill out to 510 bytes and add boot signature
times 510 - ($ - $$) db 0
dw 0xAA55            ; add boot signature at the end of bootloader

I then use this command to create the bootloader file boot.bin:

nasm -f bin boot.asm -o boot.bin

Create a 2MiB disk image file 1.raw:

dd if=/dev/zero of=1.raw bs=1024 count=2048

Place the bootloader boot.bin at beginning of file 1.raw without truncating the rest of file:

dd if=boot.bin of=1.raw conv=notrunc

Create a VDI image called 1.vdi from 1.raw:

rm -f 1.vdi
VBoxManage convertfromraw 1.raw 1.vdi --format VDI

When added to a virtual machine under VirtualBox I get this on the display:

enter image description here


Your VDI File

In your supplied image file 1.vdi I noticed this when I did a hexdump:

00200000  aa 55 aa 55 aa 55 aa 55  aa 55 aa 55 aa 55 aa 55
*
002004c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*
00300000

This output suggests to me that you reversed the bytes of the boot signature in your file. It should be 0x55 followed by 0xaa. 0xaa55 as a WORD is stored with the bytes reversed.

Valid boot medium may be more than just getting the boot signature correct. Some BIOSes may search for certain instructions in the first few bytes that are typically found in bootloaders. Failure to find such instructions (examples often include things like JMP, XOR, CLI, MOV) may cause it to think that it isn't valid boot medium.

One way to test whether 0xAA55 at the end is enough by itself I used hexedit and modified your 1.vdi file to look like this:

00200000  aa 55 aa 55 aa 55 aa 55  aa 55 aa 55 aa 55 aa 55
00200010  aa 55 aa 55 aa 55 aa 55  aa 55 aa 55 aa 55 aa 55
*
002001f0  aa 55 aa 55 aa 55 aa 55  aa 55 aa 55 aa 55 55 aa <-- Corrected signature
                                                               at 1fe & 1ff
00200200  aa 55 aa 55 aa 55 aa 55  aa 55 aa 55 aa 55 aa 55
*
002004c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*
00300000

Running with that change alone didn't work. I then used hexedit and placed a CLI opcode (0xFA) as the first byte of the sector. The resulting file now looked like:

           v-- CLI instruction
00200000  fa 55 aa 55 aa 55 aa 55  aa 55 aa 55 aa 55 aa 55
00200010  aa 55 aa 55 aa 55 aa 55  aa 55 aa 55 aa 55 aa 55
*
002001f0  aa 55 aa 55 aa 55 aa 55  aa 55 aa 55 aa 55 55 aa
00200200  aa 55 aa 55 aa 55 aa 55  aa 55 aa 55 aa 55 aa 55
*
002004c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
*
00300000

I have placed fa as the first byte int the bootloader. Now when I use your image the error No bootable medium found! System halted no longer appears. This suggests that VirtualBox is looking for more than a boot signature, and is doing some kind of sanity check to determine if the start of the bootloader appears to be executable instructions. This is not uncommon for BIOSes. Some may do such a check, some may not. At this time I haven't looked over the VirtualBox source code to determine the exact checks it performs to make its determination.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    Do you ever get the feeling that you are writing the same answer all the time? :-) – Cody Gray - on strike Apr 23 '17 at 09:51
  • 1
    @CodyGray : there is a resemblance from answer to answer yep. In this case I wasn't aware of an existing answer that dealt with Virtual Box and and VDI format so figured I'd toss up an answer lol. – Michael Petch Apr 23 '17 at 13:13
  • Seriously, given how popular this programming assignment seems to be, you might want to just migrate this answer into Documentation and then link to it in perpetuity. As long as they're forcing Documentation down our throats, we might as well figure out how to make it worth something. Or, maybe it would be worth it to create a canonical Q&A about getting started creating a bootloader, along with your troubleshooting tips from another answer, then we can close questions as dupes of that one. There's a lot of great introductory material in your answers, but it's hard to find. – Cody Gray - on strike Apr 23 '17 at 13:15
  • @CodyGray A couple of us have proposed documentation for [bootloader](http://stackoverflow.com/documentation/bootloader/commit) . I agree with you ;-) – Michael Petch Apr 23 '17 at 13:17
  • @CodyGray : I have considered putting a pile of links into the bootloader tag to Q&A in SO but I find doing that may seem as a self serving way of generating interest in one's own answers. – Michael Petch Apr 23 '17 at 13:19
  • 1
    @CodyGray: A canonical Q&A (why not just tag this one?) would be of far more value than trying to shoehorn usefulness into Docs. – Lightness Races in Orbit Apr 23 '17 at 13:33
  • _"a self serving way of generating interest in one's own answers"_ Shrug. I don't think that's possible. You wrote your answer to give information to others, and spreading that information further is just more giving. Sure, you potentially expand your upvoting audience, but upvotes are for the information not for the person ... and you potentially expand your downvoting audience also. I wouldn't worry about it. – Lightness Races in Orbit Apr 23 '17 at 13:34
  • @BoundaryImposition : I think Cody is suggesting a canonical question and answers that may be broader in scope than this one and an answer partially based on the contents of [another answer I reference a lot](http://stackoverflow.com/a/32705076/3857942) or any answers that may be about general trouble shooting of bootloaders. – Michael Petch Apr 23 '17 at 14:29
  • @MichaelPetch: I think this Q&A has the perfect size & scope for a SE question; anything broader would be off-topic, canonical or no. – Lightness Races in Orbit Apr 23 '17 at 14:33