Hi, how can I copy my bootloader to the bootsector of a disk image, floppy image or CD-ROM? I just want to test my bootloader. If it's a lot easier to  do in a virtual floppy image, how can I make one in Mac?

Thanks in advanced
Posted on 2007-08-07 23:03:57 by (o_0)
If you are using Mac OS-X, you should have access to DD.
Posted on 2007-08-07 23:07:43 by SpooK
Yes, I do. But I don't know how to use it. I typed man dd but I don't understand. I just want to write to the bootsector of a disk image.
Posted on 2007-08-07 23:10:37 by (o_0)
Ok I typed this:

dd if=bootloader.bin of=bootimage.iso

It said 512 bytes were transfered correctly but the image won't boot in Qemu. Am I missing a step?

I verified the disk image in Disk Utility and it said that its got no file system.
Posted on 2007-08-07 23:24:39 by (o_0)

Ok I typed this:

dd if=bootloader.bin of=bootimage.iso


Well, if "bootloader.bin" is already in a flat binary format, there is no need to do what you just did to get it working in QEMU... you essentially made a copy of "bootloader.bin" and called it "bootimage.iso" which does nothing for solving the problem.

Moreover, the suggestion of DD was to address the need to write RAW sectors to a physical drive (i.e. copy your bootsector to a floppy disk.)


It said 512 bytes were transfered correctly but the image won't boot in Qemu. Am I missing a step?

I verified the disk image in Disk Utility and it said that its got no file system.


Reading no file system would be a good thing for a file the size of one sector with seemingly no file system header.

Time for more basic questions.

Do you have QEMU configured correctly to boot your bin/iso image as a floppy disk? Have you double-checked this failure to boot with another emulator such as Bochs, VirtualPC or VMWare?

How is your bootloader compiled/assembled? Do you have a PowerPC or x86 Mac?

All these questions are pretty important in order to address the issue, so please answer all of them.

If your bootloader source does happen to be in assembly language, go ahead and attach the contents to a reply.
Posted on 2007-08-07 23:36:17 by SpooK
I have an Intel iMac (i386). I configured Q (Qemu) to boot from the disk image directly. I assembled bootloader.asm like this:

nasm -f bin bootloader.asm -o bootloader.bin

What do you mean there's no need to do what I did? Can I boot from a flat binary?
Posted on 2007-08-07 23:41:47 by (o_0)

What do you mean there's no need to do what I did?


You didn't need to issue a DD command. All you did is copied the contents of "bootsector.bin" to "bootsector.iso", DD doesn't perform any internal magic... it takes X bytes from the source in and sends X bytes out to the destination. So, as I said, you essentially made a copy of the file. What DD *would* be useful for is if the destination was your floppy drive that contains a real/physical floppy disk.


Can I boot from a flat binary?


Yes you can, but successful booting/execution is dependent on the proper code.

Clear your mind of higher-level constructs such as file systems and address the basics of the computer system you are programming for.

A flat binary image is nothing but RAW code and data. By RAW, I mean RAW as in the file does not need to be fixed-up at run-time... as opposed common executable formats such as PE, AOUT and ELF. The file is already laid-out the exact way it needs to be executed starting at the first byte.

For starters, I recommend reading THIS to gain a better idea of how the x86 boot process works.

I also highly recommend attaching your bootloader.asm (or whatever it is called) file to a post so I can see the code in question... that will help resolve the problem as I will be able to tell if your bootloader is doing anything useful or just crashing the system.
Posted on 2007-08-07 23:59:37 by SpooK
Lol I've already read that. Here's the code:

bits	16
org 0x7C00
_start:
call _greet
hlt
_greet:
mov si, msg
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x07
.char_loop
lodsb
or al, al
jz .return
int 0x10
jmp .char_loop
.return
ret
msg db "Welcome!", 10, 0
times 510 - ($ - $$) db 0
dw 0xAA55


Posted on 2007-08-08 00:02:14 by (o_0)
Hey! I got it to work! The wierd thing is that it displays "Welcome!" twice, the second is a little indented. Like this:

Welcome!
            Welcome!
                          _

Posted on 2007-08-08 00:04:32 by (o_0)

Hey! I got it to work!


It is common courtesy to explain what you did to fix your work, so others can learn from it as well ;)

To point out a few things anyhow...

1) You have not setup a "certain" stack frame prior to executing any code.

2) None of the segment registers are set to known values.

3) The HLT instruction only halts the CPU until interrupted, so you should really use "jmp $" or even...


.halt
hlt
jmp .halt


HtH.
Posted on 2007-08-08 00:09:52 by SpooK
In Qemu I set 'Floppy' to 'No Floppy', 'CD-ROM' to 'No CD-ROM', 'Hard disk' to my disk image path, and 'Boot from' to 'Hard disk'. So you say I should put 'jmp $' instead of hlt? Ok, but why is the output printed twice?
Posted on 2007-08-08 00:14:29 by (o_0)

In Qemu I set 'Floppy' to 'No Floppy', 'CD-ROM' to 'No CD-ROM', 'Hard disk' to my disk image path, and 'Boot from' to 'Hard disk'. So you say I should put 'jmp $' instead of hlt? Ok, but why is the output printed twice?


Carefully re-read #3 above.

If the CPU is interrupted, execution resumes at the next instruction after HLT... which is the start of your printing function. You are just getting lucky that the extra RET (that jumps back to who knows where at this point due to stack corruption) freezes the system.
Posted on 2007-08-08 00:18:41 by SpooK
Ok I did what you said and it printed out like this:

Welcome!
            _

The code's like this:

bits	16
org 0x7C00
_start:
call _greet
jmp $
_greet:
mov si, msg
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x07
.char_loop
lodsb
or al, al
jz .return
int 0x10
jmp .char_loop
.return
ret
msg db "Welcome!", 10, 0
times 510 - ($ - $$) db 0
dw 0xAA55


Just one thing. Why is that thing '_' there instead of like this?

Welcome!
_


Posted on 2007-08-08 00:21:52 by (o_0)


Just one thing. Why is that thing '_' there instead of like this?

Welcome!
_



Because your Welcome message only produces a NEWLINE (LINE FEED), which increments the line position by one row. To move the cursor back to the beginning of the current line, you also need to issue a CARRIAGE RETURN like so...


"Welcome!", 13,10, 0
Posted on 2007-08-08 00:26:54 by SpooK
Thanks for all the help. Just one more thing lol. You said that none of the segment registers are set to known values, what do you mean? I'm sorry if this is obvious but I'm just starting out.
Posted on 2007-08-08 00:30:22 by (o_0)

Thanks for all the help. Just one more thing lol. You said that none of the segment registers are set to known values, what do you mean? I'm sorry if this is obvious but I'm just starting out.


Well, if you are new to assembly language, I would suggest reading up on the basics first. Randy Hyde's Art of Assembly (AoA) series is known to be quite informative.

To answer your question though, you are attempt to accept control of the system from the BIOS with many unknowns involved. The segment registers are not set to what your program would expect of them. Your stack is not setup to a known state. Basically, without those two factors... you are taking a gamble at system stability.

If you don't know what the segment registers are, or what they do... then I am even more inclined to insist that you start reading AoA... especially the 16-bit "DOS" version if you want to make a bootloader :idea:

You can read and learn from AoA step-by-step and modify your bootloader according to your further understanding of things... a healthy mix of theory and practice :)

Good luck with your programming endeavors ;)
Posted on 2007-08-08 00:41:58 by SpooK
I started reading "Programming from the Ground Up" by Jonathan Barlett but stopped because it uses the AT&T syntax and it's for Linux. And I can't buy any book beacuse I have no credit card. Although I'm starting to read "PC Assembly Language" by Paul A. Carter, is it good?
Posted on 2007-08-08 13:37:05 by (o_0)
2) None of the segment registers are set to known values.


Is this better?

bits	16
org 0x7C00
_start:
xor ax, ax
mov ds, ax
mov es, ax
call _greet
jmp $
_greet:
mov si, msg
mov ah, 0x0E
mov bh, 0x00
mov bl, 0x07
.char_loop
lodsb
or al, al
jz .return
int 0x10
jmp .char_loop
.return
ret
msg db "Welcome!", 13, 10, 0
times 510 - ($ - $$) db 0
dw 0xAA55



Posted on 2007-08-08 14:00:46 by (o_0)

I started reading "Programming from the Ground Up" by Jonathan Barlett but stopped because it uses the AT&T syntax and it's for Linux. And I can't buy any book beacuse I have no credit card. Although I'm starting to read "PC Assembly Language" by Paul A. Carter, is it good?


It's OK. I still suggest getting AoA, the 16-bit DOS edition.


Is this better?


If you cannot answer that question yourself, you need to keep reading ;)

These are basic x86 programming concepts which you will need to learn.
Posted on 2007-08-08 18:44:42 by SpooK
Why the 16-bit DOS? I don't even have Windows, and I want to program in 32 bit Pmode.

Edit: Oh, and I don't have a credit card so I can't buy it...
Posted on 2007-08-08 19:28:53 by (o_0)