Page 1 of 7
Making a bootable image
Posted: Fri Oct 30, 2020 4:45 am
by Bonfra
I'm following this tutorial to create a bootable image:
https://wiki.osdev.org/Bootable_Disk
With the small difference that to copy files inside the image i use mcopy instead of loop because I'm on WSL.
It works fine and in can burn the image to a usb using Rufus until I add the bootloader with:
Code: Select all
dd if=./bin/boot/boot.bin of=diskimage.dd conv=notrunc bs=512 count=1
As it say in the tutorial.
After adding the bootloader the if I burn the image to a USB windows can't read the usb contet just like it is corrupted. By now I'm loading a bootloader designed for a floppy but i removed the part of the disk read and just added a "hello world" print to check if the bootloader works, and it doesn't.
What am I doing wrong?
EDIT:
Also if i open the image with poweriso before adding the bootloader i can navigate the content, then i can't.
I also tried with a floppy img but the problem persist.
QEMU can boot the image evene if is "Broken"
Re: Making a bootable image
Posted: Fri Oct 30, 2020 6:19 am
by PeterX
Without seing your code I have to guess. Maybe you post it on github?
It seems you are overwriting the filesystem or partition table with the "dd" command. That would explain why the ISO gets corrupted. But it doesn't explain why the hello-world-sector doesn't work.
How do you boot the image? On real hardware or as a disk image with an emulator?
On some PCs an USB stick without partition table or FAT FS won't boot.
Greetings
Peter
Re: Making a bootable image
Posted: Fri Oct 30, 2020 9:34 am
by Bonfra
Hi, thanks for the quick answer.
PeterX wrote:Without seing your code I have to guess. Maybe you post it on github?
I don't think that seeing the actual bootloader code could be useful, anyway I'll add it later, now I'm kinda blocked by other features.
PeterX wrote:It seems you are overwriting the filesystem or partition table with the "dd" command. That would explain why the ISO gets corrupted. [...] On real hardware or as a disk image with an emulator?
That could very be the case since in virtual emulators like qemu and bochs the bootloade runs but in a real device it doesn't (I'm using an old asus from ~10 years ago)
Where/How can I load the bootloader without causing any damage?
Re: Making a bootable image
Posted: Fri Oct 30, 2020 10:12 am
by PeterX
In you have an MBR with a partition table you can use:
Code: Select all
dd if=xyz of=blabla bs=1 count=446
dd if=xyz of=blabla bs=1 count=2 skip=510 seek=510
If you have volumn boot sector with FAT you can use:
Code: Select all
dd if=xyz of=blabla bs=1 count=3
dd if=xyz of=blabla bs=1 seek=X skip=X
Find out the value of X here:
https://en.wikipedia.org/wiki/BIOS_parameter_block
An older thread about this:
viewtopic.php?f=1&t=36896
Greetings
Peter
Re: Making a bootable image
Posted: Fri Oct 30, 2020 1:32 pm
by bzt
Bonfra wrote:After adding the bootloader the if I burn the image to a USB windows can't read the usb contet just like it is corrupted. By now I'm loading a bootloader designed for a floppy but i removed the part of the disk read and just added a "hello world" print to check if the bootloader works, and it doesn't.
What am I doing wrong?
That tutorial creates a GPT partitioning table. This means you have to write a boot sector that can understand GPT (in short, forget code designed for floppies). If you don't want to mess with GPT, then you can map the GPT partitions into MBR partitions and use a "classic" MBR code (under Windows, use "FDISK.EXE /MBR").
A simple ANSI C program that locates the
ESP and creates an MBR record for it can be found
here (no dependencies, ca. 60 SLoC). I wrote it for the Raspberry Pi, because its firmware doesn't understand GPT, but it works with all MBR code.
Bonfra wrote:Also if i open the image with poweriso before adding the bootloader i can navigate the content, then i can't.
This means you're not using the GPT. You'll need to map the ESP into the MBR table too.
PeterX wrote:In you have an MBR with a partition table you can use
I've added the example to the wiki page.
Cheers,
bzt
Re: Making a bootable image
Posted: Fri Oct 30, 2020 2:41 pm
by Bonfra
I'm not really sure on what I have to do, is my first time doing a bootloader my own
I've followed the new wiki:
Code: Select all
$ dd if=boot.bin of=diskimage.dd conv=notrunc bs=446 count=1
$ dd if=boot.bin of=diskimage.dd conv=notrunc bs=2 count=1 skip=510 seek=510
But is just like before, qemu and bochs boot the image but i can't explore the image with poweriso and if i burn the image to a usb, the stick is no more recognized.
This is my bootloader simplified:
Code: Select all
org 0
bits 16
jmp 0x7C00 : boot
BiosPrint:
pusha
.loop:
lodsb
or al, al
jz .done
mov ah, 0x0E
int 0x10
jmp .loop
.done:
popa
ret
%macro BiosPrintMacro 1
mov si, word %1
call BiosPrint
%endmacro
boot:
.init:
cli ; Disable interrupts
; All data segments (except es) are initialized to use the code segment.
mov ax, cs
mov ds, ax
mov fs, ax
mov gs, ax
; Set up a temporary stack.
xor ax, ax
mov ss, ax
mov sp, 0x00007C00
; The es segment is 0, which is useful for absolute addressing of the first 64KiB of memory.
mov es, ax
sti ; Re-enable interrupts.
BiosPrintMacro Message
hang:
cli
hlt
jmp hang
Message db "Hello World!", 13, 10, 0
times 510-($-$$) db 0
dw 0xAA55
Re: Making a bootable image
Posted: Fri Oct 30, 2020 3:03 pm
by PeterX
1.)
This is wrong.
It must be 0x7C0:boot or 0:boot. (If you use the latter, use "org 0x7C00")
2.) a) Have you setup a valid partition table on the USB stick?
b) What is the command/tool you use for writing to the USB stick?
3.) According to this:
https://www.computerhope.com/unix/dd.htm
the dd-example with "bs=2 count=1 seek=510 skip=510" is wrong.
I think it must be "bs=1 count=2" (because otherwise it seeks/skips 510x2 bytes.)
Re: Making a bootable image
Posted: Fri Oct 30, 2020 3:34 pm
by Bonfra
PeterX wrote:
It must be 0x7C0:boot or 0:boot. (If you use the latter, use "org 0x7C00")
Yes actually in the full bootloader i have in another file with a full memory layout
And then in the actual bootloader:
So i just simplified it wrongly.
PeterX wrote:
What is the command/tool you use for writing to the USB stick?
I Use
Rufus to write the image to the usb.
PeterX wrote:
the dd-example with "bs=2 count=1 seek=510 skip=510" is wrong.
I think it must be "bs=1 count=2" (because otherwise it seeks/skips 510x2 bytes.)
I've tried both and both gave the same result, i tried to explore the image before running the second command and it still breaks so I think that is this one that overrides:
Code: Select all
dd if=bin/boot/boot.bin of=diskimage.dd conv=notrunc bs=446 count=1
Re: Making a bootable image
Posted: Fri Oct 30, 2020 3:44 pm
by PeterX
So the question remains: Have you setup a proper partition table? Or are you using the whole disk as a FAT partition? Is your disk image CD/DVD, HD or floppy disk?
I forgot this: Maybe you use UEFI? If so, in Legacy mode?
Re: Making a bootable image
Posted: Fri Oct 30, 2020 4:13 pm
by Bonfra
I create only one partiton as the wiki shows:
Code: Select all
$ fdisk diskimage.dd
Welcome to fdisk (util-linux 2.30.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xfa00b86e.
Command (m for help): g
Created a new GPT disklabel (GUID: E6B4945A-8308-448B-9ACA-0E656854CF66).
Command (m for help): n p
Partition number (1-128, default 1): 1
First sector (2048-262110, default 2048): 2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-262110, default 262110): +8M
Created a new partition 1 of type 'Linux filesystem' and of size 8 MiB.
Command (m for help): t 1
Selected partition 1
Partition type (type L to list all types): 1
Changed type of partition 'Linux filesystem' to 'EFI System'.
Command (m for help): w
The partition table has been altered.Syncing disks.
$
then add a Fat file system with:
Code: Select all
mkfs.vfat -F 16 -n "EFI System" diskimage.dd
Re: Making a bootable image
Posted: Fri Oct 30, 2020 4:44 pm
by PeterX
Bonfra wrote:
$ fdisk diskimage.dd
then add a Fat file system with:
Code: Select all
mkfs.vfat -F 16 -n "EFI System" diskimage.dd
I think that's a mistake because you add the FAT FS to the whole "disk" (image) not the partition. Then the MBR (the first sector) has a partition table AND a FAT fs BPB (BIOS Parameter Block).
The command for writing the bootcode to the MBR will indeed overwrite the BPB.
You should decide for either partition table or BPB. In other words for partitioned disk or un-partitioned disk. Floppy disks are traditionally un-partitioned and HDs are traditionally partitioned. USB sticks can normally be bootable like floppy or like HD, but I guess that depends on what the BIOS supports. Or do you want to write a CD/DVD image?
You ignored several times my questions. So again: Do you use Legacy BIOS or UEFI? Do you use Legacy boot mode (CSM) or UEFI mode? Do you use OVMF in QEMU?
Re: Making a bootable image
Posted: Fri Oct 30, 2020 5:01 pm
by Bonfra
I think that a floppy beeing really small will not be enough for a fully developed kernel, so I want to go with HDs.
Being that HDs are partitioned I need to create a file system in the first (and for now only) partition. The wiki uses losetup but I'm using WSL and it does not implement loop module as far as I'm aware so I wrongly tried to do it with mkfs.vfat on the whole disk.
Is there a way to create the filesystem in the partiton without using losetup?
You ignored several times my questions. So again: Do you use Legacy BIOS or UEFI? Do you use Legacy boot mode (CSM) or UEFI mode? Do you use OVMF in QEMU?
I'm sorry but I don't have the slightest idea, how can i check?
Here is the repo:
https://github.com/DefEnge/test-kernel
the bootloader part is in the boot direcotry
In this repo the bootloader is still the one that boot with the floppy kinda wrongly, the bootloader that i've used for testing is kinda the one I've poster before
Re: Making a bootable image
Posted: Fri Oct 30, 2020 5:13 pm
by PeterX
I quote
https://itsfoss.com/check-uefi-or-bios/
On Windows, “System Information” in Start panel and under BIOS Mode, you can find the boot mode. If it says Legacy, your system has BIOS. If it says UEFI, well it’s UEFI.
Tell us the result of the check.
You need the file "OVMF.fd" from the Tianocore/OVMF project to emulate UEFI! Since WSL is Ubuntu AFAIK, you can install it with
And you use the floppy option "-fda" in the run-script. Use the HD option "-hda" instead. [EDIT: -hda isprobably wrong.]
Try this page for mounting a HD image file:
https://www.howtogeek.com/howto/windows ... ows-vista/
Re: Making a bootable image
Posted: Fri Oct 30, 2020 5:25 pm
by Bonfra
Following
https://itsfoss.com/check-uefi-or-bios/ I can say that my physical machine does not support UEFI.
You need the file "OVMF.fd" from the Tianocore/OVMF project to emulate UEFI! Since WSL is Ubuntu AFAIK, you can installe it with
If I undertood correctrly this program is used to enable UEFI support on virtual machines but since my physical machine is no UEFI I don't need it right?
Re: Making a bootable image
Posted: Fri Oct 30, 2020 6:22 pm
by BenLunt
Hi,
I think what Peter is trying to express to you, since your USB is not booting on the Windows machine, he is trying to ask if the Windows machine is Legacy BIOS or UEFI firmware.
- If the Windows machine is Legacy BIOS, to boot the USB, you *must* have a MBR and then at least one partition.
- If the Windows machine is UEFI Firmware (without CSM), you *must* have a GPT and then at least one partition.
Since you have declared that the Windows machine is Legacy BIOS, let's say that you *must* have a MBR and at least one partition.
This MBR must have at least one entry marked BOOTABLE (80h) and point to the first sector of the bootable partition. This MBR must be at LBA 0.
Now let's say that your bootable partition starts at LBA 63 (legacy default, but you can have it at any LBA).
Your Bootable FAT partition, with a valid BPB, must be written to the media starting at LBA 63.
Your FAT BPB must also have the Hidden Sectors member set to 63.
It looks like you are using a *nix platform to do this, since you are using DD and other *nix utilities. I have a utility (sadly for your sake it is Windows only) that can manipulate image files with ease.
http://www.fysnet.net/ultimate/index.htm
Does this help?
Ben