Babystep1: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
mNo edit summary
 
(8 intermediate revisions by 6 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__


{{Tone}}
{{Rating|1}}
{{Rating|1}}


Line 11: Line 12:


==Code==
==Code==
The following code is the smallest possible example of booting code from a floppy.
The following code is the smallest possible example of booting code from a disk.


<source lang="asm">
<syntaxhighlight lang="asm">
; boot.asm
; boot.asm
hang:
hang:
Line 19: Line 20:


times 512-($-$$) db 0
times 512-($-$$) db 0
</syntaxhighlight>
</source>
The CPU starts in real mode and the BIOS loads this code at address 0000:7c00. The "times 512..." stuff is NASM's way of saying fill up 512 bytes with zeros. And partcopy is going to expect that (200 in Hex = 512 in Decimal). Change it and you'll see Partcopy choke.
The CPU starts in real mode and the [[BIOS]] loads this code at address 0000:7c00. <code>times 512-($-$$) db 0</code> is NASM's way of saying fill up 512 bytes with zeroes, and partcopy is going to expect that (200 in Hex = 512 in Decimal). Change it and partcopy will likely fail.


Often, you will see a so-called boot signature (0xAA55) at the end. Older versions of [[BIOS|BIOSes]] looked for this in order to identify a boot sector on a disk. It is evidently unnecessary nowadays. If it's needed, the last line would be replaced with (or some version of it)
There is often a boot signature (0xAA55) at the end. Older versions of some BIOSes looked for this in order to identify a boot sector on a disk. It is evidently unnecessary nowadays, unless you're running the code on a legacy BIOS, or in QEMU. If it's needed, the last line would be replaced with (or some version of it):
<source lang="asm">
<syntaxhighlight lang="asm">
; boot.asm
; boot.asm
hang:
hang:
Line 31: Line 32:
db 0x55
db 0x55
db 0xAA
db 0xAA
</syntaxhighlight>
</source>


But the thing I'd really like to point out is how once you've booted, and the cursor is happily blinking on a blank screen, you might notice two things. One is that the floppy motor will turn off and the other is that you can press Ctrl-Alt-Del to reboot. The point is that interrupts (such as INT 0x09) as still being generated.
Once you've booted, and the cursor is happily blinking on a blank screen, the disk's motor will turn off and you can now press Ctrl-Alt-Del to reboot. This is because [[interrupts]] are still being generated.


For kicks try clearing the interrupts flag:
Try clearing the interrupts flag:
<source lang="asm">
<syntaxhighlight lang="asm">
;boot.asm
;boot.asm
cli
cli
Line 45: Line 46:
db 0x55
db 0x55
db 0xAA
db 0xAA
</syntaxhighlight>
</source>
You may notice that the floppy motor doesn't turn off and you can't reboot with Ctrl-Alt-Del.
You may notice that the motor doesn't turn off and you can't reboot with Ctrl-Alt-Del.


If you try to reduce this even more by removing the loop and merely pad out the sector with zeros, the BIOS will have something to say about it. On my machine, it was "Operating System Not Found". I have yet to try filling the sector with zeros except for adding a boot signature.
Removing the loop and merely padding out the sector with zeroes will usually cause the BIOS to throw an error on boot. On most machines, it will say "Operating System Not Found".

Not exactly something you would show your friends, but I wanted to show just what the bare minimum is before I elaborate. Unless I'm irritating anyone, in which case I'll desist.


==Creating disk image==
==Creating disk image==
The code is assembled in [[NASM]] and copied to floppy using partcopy,dd,or debug. Then you simply boot from the floppy.
The code is assembled in [[NASM]] and copied to floppy (outdated), disk or USB-sticks using partcopy, dd, or debug. Then you simply boot from that disk.

For a more detailed description, see the [[Bootable Disk]] page.


===Windows===
===Windows===
<source lang="bash">
<syntaxhighlight lang="bash">
nasmw boot.asm -f bin -o boot.bin
nasmw boot.asm -f bin -o boot.bin
partcopy boot.bin 0 200 -f0
partcopy boot.bin 0 200 -f0
Line 62: Line 63:
debug boot.bin
debug boot.bin
-W 100 0 0 1
-W 100 0 0 1
-Q</source>
-Q</syntaxhighlight>
===Unix===
===Unix===
<source lang="bash">
<syntaxhighlight lang="bash">
nasm boot.asm -f bin -o boot.bin
nasm boot.asm -f bin -o boot.bin
dd if=boot.bin of=/dev/fd0
dd if=boot.bin of=/dev/fd0
</syntaxhighlight>
</source>
To write to a hard drive or USB-stick, use
<syntaxhighlight lang="bash">
nasm boot.asm -f bin -o boot.bin
dd if=boot.bin of=/dev/sda
</syntaxhighlight>
where replace "sda" with sdb, sdc etc. according to your configuration.

==Running the binary in QEMU==
If you don't have an old machine with floppy drive you can emulate one using QEMU (note "'''f'''da").

<syntaxhighlight lang="bash">
qemu-system-i386 -fda boot.bin
</syntaxhighlight>

But it is advisable to forget about floppies altogether, and focus on USB-sticks instead. Also if you're afraid to test your code on your development machine (that would be wise), you can use QEMU (note "'''h'''da").

<syntaxhighlight lang="bash">
qemu-system-i386 -hda boot.bin
</syntaxhighlight>

Use the QEMU monitor command to send Ctrl-Alt-Del to the VM:
<syntaxhighlight lang="bash">
sendkey ctrl-alt-delete
</syntaxhighlight>

Because of how fast emulation has become, you might need to slow down emulation speed to 1% to notice the reboots.


== References ==
== References ==
Line 76: Line 103:
* Interrupts by number: [http://www.osdever.net/downloads.php]
* Interrupts by number: [http://www.osdever.net/downloads.php]
* Randall Hyde's look into the bowels of the PC: [http://webster.cs.ucr.edu/]
* Randall Hyde's look into the bowels of the PC: [http://webster.cs.ucr.edu/]
* QEMU [https://www.qemu.org]


[[Category:Babystep]]
[[Category:Babystep]]

Latest revision as of 22:55, 15 June 2024


This article's tone or style may not reflect the encyclopedic tone used throughout the wiki.
See Wikipedia's article on tone for suggestions.
Difficulty level

Beginner


Babystep1: Your first boot sector

Tutorial

PreviousNext
Babystep2

Your first boot sector.

Code

The following code is the smallest possible example of booting code from a disk.

; boot.asm
hang:
    jmp hang

    times 512-($-$$) db 0

The CPU starts in real mode and the BIOS loads this code at address 0000:7c00. times 512-($-$$) db 0 is NASM's way of saying fill up 512 bytes with zeroes, and partcopy is going to expect that (200 in Hex = 512 in Decimal). Change it and partcopy will likely fail.

There is often a boot signature (0xAA55) at the end. Older versions of some BIOSes looked for this in order to identify a boot sector on a disk. It is evidently unnecessary nowadays, unless you're running the code on a legacy BIOS, or in QEMU. If it's needed, the last line would be replaced with (or some version of it):

; boot.asm
hang:
    jmp hang

    times 510-($-$$) db 0 ; 2 bytes less now
    db 0x55
    db 0xAA

Once you've booted, and the cursor is happily blinking on a blank screen, the disk's motor will turn off and you can now press Ctrl-Alt-Del to reboot. This is because interrupts are still being generated.

Try clearing the interrupts flag:

;boot.asm
     cli
 hang:
     jmp hang

     times 510-($-$$) db 0
     db 0x55
     db 0xAA

You may notice that the motor doesn't turn off and you can't reboot with Ctrl-Alt-Del.

Removing the loop and merely padding out the sector with zeroes will usually cause the BIOS to throw an error on boot. On most machines, it will say "Operating System Not Found".

Creating disk image

The code is assembled in NASM and copied to floppy (outdated), disk or USB-sticks using partcopy, dd, or debug. Then you simply boot from that disk.

For a more detailed description, see the Bootable Disk page.

Windows

nasmw boot.asm -f bin -o boot.bin
partcopy boot.bin 0 200 -f0 
OR
debug boot.bin
-W 100 0 0 1
-Q

Unix

nasm boot.asm -f bin -o boot.bin
dd if=boot.bin of=/dev/fd0

To write to a hard drive or USB-stick, use

nasm boot.asm -f bin -o boot.bin
dd if=boot.bin of=/dev/sda

where replace "sda" with sdb, sdc etc. according to your configuration.

Running the binary in QEMU

If you don't have an old machine with floppy drive you can emulate one using QEMU (note "fda").

qemu-system-i386 -fda boot.bin

But it is advisable to forget about floppies altogether, and focus on USB-sticks instead. Also if you're afraid to test your code on your development machine (that would be wise), you can use QEMU (note "hda").

qemu-system-i386 -hda boot.bin

Use the QEMU monitor command to send Ctrl-Alt-Del to the VM:

sendkey ctrl-alt-delete

Because of how fast emulation has become, you might need to slow down emulation speed to 1% to notice the reboots.

References

  • Instruction Set from the horse's mouth: [1]
  • Easier to read: [2]
  • NASM asembler - docs incl instruction set: [3]
  • Partcopy - download pcopy02.zip (new link): [4]
  • Interrupts by number: [5]
  • Randall Hyde's look into the bowels of the PC: [6]
  • QEMU [7]