Live data from Hacker News

Writing a Tiny x86 Bootloader

joebergeron.io

31–40 of 55 posts

Re: Writing a Tiny x86 Bootloader

#31

It's sad that most of these "bootloader" guides don't take you into actualy boot-loading something. No searching a file system. No loading another binary. No setting up 32 mode. Nothing, just printing text. That's what most of these are on.

He uses bios calls , thats what you need to learn to be able to write a bootloader, the rest is just reading up some documentation about what you are going to load. You can't fit a filesystem parser into 1 sector and have anything other sensible in there... from bootloader you load sectors from disk, using bios generally (on x86 atleast). and you can understand how to do it from this tutorial.

> the rest is just reading up some documentation about what you are going to load

That documentation is usually.... lacking

> You can't fit a filesystem parser into 1 sector and have anything other sensible in there

You don't use a single sector. You use a 2 stage bootloader that uses a reserved FAT sector. In FAT12 (at least) you can specifiy how many reserved secotrs on formatting. A reserved sector comes directly after the first sector. You can do a full FAT12 implementation in assembly in two sectors.

> using bios generally (on x86 atleast). and you can understand how to do it from this tutorial.

You're going to need to do a lot more reading. Even just looking up the interrupts you need is a task. I've found nothign that does a good job explaining the 1, 2, and 3s of how to get all the way to booting from a formated disk.

Re: Writing a Tiny x86 Bootloader

#32

If you read this article, you'll understand just how easy it is to compromise so many computers. What I have seen infecting my own systems works on computers built in 2004 as well as new machines, so its exploiting the design and implementation of various international standards. The way it works, is the bios loads the malware irrespective of the boot drive order specified in the bios, and then it seems to rewrite th…

>bios loads the malware irrespective of the boot drive order specified in the bios

>rewrite the make and model of the hard drives

ummm, no

Re: Writing a Tiny x86 Bootloader

#33

Earlier quoted context omitted.

He uses bios calls , thats what you need to learn to be able to write a bootloader, the rest is just reading up some documentation about what you are going to load. You can't fit a filesystem parser into 1 sector and have anything other sensible in there... from bootloader you load sectors from disk, using bios generally (on x86 atleast). and you can understand how to do it from this tutorial.

> the rest is just reading up some documentation about what you are going to load That documentation is usually.... lacking > You can't fit a filesystem parser into 1 sector and have anything other sensible in there You don't use a single sector. You use a 2 stage bootloader that uses a reserved FAT sector. In FAT12 (at least) you can specifiy how many reserved secotrs on formatting. A reserved sector comes directly…

You sound pretty well versed in this area, would you be interested in writing something up? Don't take this as a dig, I know the value of free time and how much that free time is lacking for most of us, I just thought I'd toss it out there.

Re: Writing a Tiny x86 Bootloader

#34
post #12

Why he is using 32-bit esp/ebp registers in 16-bit environment? There are 16-bit sp/bp registers. And there is 0xFFFF limit on segment descriptors in real mode anyway.

I've since updated the article and code on Github to reflect this discssion. Thanks HN as always for teaching me something new.

Re: Writing a Tiny x86 Bootloader

#35
post #9

Earlier quoted context omitted.

What would be the equivalent of this in UEFI? I mean is there a one page tutorial on booting with UEFI by writing a few lines of assembly?

I found this article previously on HN: https://news.ycombinator.com/item?id=12238498 You may find it interesting. Basically you won't bother writing a bootloader with UEFI, since it already provides that feature. Instead you'll get right at working on your OS.

[deleted]

Re: Writing a Tiny x86 Bootloader

#36

It's sad that most of these "bootloader" guides don't take you into actualy boot-loading something. No searching a file system. No loading another binary. No setting up 32 mode. Nothing, just printing text. That's what most of these are on.

[deleted]

Re: Writing a Tiny x86 Bootloader

#37
It's been forever since I've thought about the segmented memory model. Can someone explain this math to me:

The OP states:

"Since our code resides at 0x7C00, the data segment may begin at 0x7C0"

0x7C00 in decimal is 31744 and 0x7C0 in is decimal 1984 and a segment is 16 bits. I understand that the CPU is hardwired to do a JMP to 0x7C00. But how did they arrive at choosing 0x7C0 for the data segment?

Re: Writing a Tiny x86 Bootloader

#38
Random note, but it seems one of the resources linked (http://www.ctyme.com/intr/rb-0097.htm) can contain NSFW ads, didn't get me in trouble but still gets me nervous on a work machine (probably shouldn't be reading NH either for that matter).

That link is in the text "See the spec here." in the word "here".

I usually use adblock but I used the disable-all-extensions extension to debug something and forgot to turn them back on.

Re: Writing a Tiny x86 Bootloader

#39

Earlier quoted context omitted.

He uses bios calls , thats what you need to learn to be able to write a bootloader, the rest is just reading up some documentation about what you are going to load. You can't fit a filesystem parser into 1 sector and have anything other sensible in there... from bootloader you load sectors from disk, using bios generally (on x86 atleast). and you can understand how to do it from this tutorial.

> the rest is just reading up some documentation about what you are going to load That documentation is usually.... lacking > You can't fit a filesystem parser into 1 sector and have anything other sensible in there You don't use a single sector. You use a 2 stage bootloader that uses a reserved FAT sector. In FAT12 (at least) you can specifiy how many reserved secotrs on formatting. A reserved sector comes directly…

You're going to need to do a lot more reading. Even just looking up the interrupts you need is a task. I've found nothign that does a good job explaining the 1, 2, and 3s of how to get all the way to booting from a formated disk.

There is unlikely to be a single thorough body of documentation on this sort of thing. IMHO, the best resource would be the numerous open source bootloaders that are available.

Re: Writing a Tiny x86 Bootloader

#40
I wrote a more elaborate bootloader for a job about 15 years ago - it was a fun challenge to squeeze a lot of functionality into such a small, limited package.

At the time, bootloaders could add additional functionality by staying resident and hooking various interrupts to provide services or work around bugs in the BIOS. For instance, my bootloader would translate sector mappings that would allow WinPE to boot from USB drives that would not otherwise work due to (idiotic) limitations in some BIOSes.

This is a fun example but doesn't really go very far. Handling memory and reading additional code from disk are other topics that are required for a full bootloader, but this was a nice trip down memory lane.

Post reply on HN