How could I make a blank raw disk bootable ?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
gamingjam60
Member
Member
Posts: 70
Joined: Sat Aug 24, 2024 10:06 pm
Libera.chat IRC: gamingjam60
Location: India
GitHub: https://github.com/baponkar
Contact:

How could I make a blank raw disk bootable ?

Post by gamingjam60 »

I am making a iso image by using following commands

Code: Select all

$(BUILD_DIR)/$(OS_NAME)-$(OS_VERSION)-image.iso: $(BUILD_DIR)/kernel.bin #$(DEBUG_DIR)/objdump.txt

	# Creating build directory which will be used to create ISO image, kernel.bin and object files
	mkdir -p build
	
	# Creating ISO directory which will be used to create ISO image 
	mkdir -p $(ISO_DIR)/boot

	# Copying files to ISO directory and creating directories 
	cp image/boot_loader_wallpaper.bmp  $(ISO_DIR)/boot/boot_loader_wallpaper.bmp

	cp -v $(BUILD_DIR)/kernel.bin $(ISO_DIR)/boot/

	cp -v $(LIMINE_SRC_DIR)/limine.conf $(ISO_DIR)/boot/

	mkdir -p $(ISO_DIR)/boot/limine
	cp -v $(LIMINE_SRC_DIR)/limine-bios.sys $(LIMINE_SRC_DIR)/limine-bios-cd.bin $(LIMINE_SRC_DIR)/limine-uefi-cd.bin $(ISO_DIR)/boot/limine/
	
	mkdir -p $(ISO_DIR)/EFI/BOOT
	cp -v $(LIMINE_SRC_DIR)/BOOTX64.EFI $(ISO_DIR)/EFI/BOOT/
	cp -v $(LIMINE_SRC_DIR)/BOOTIA32.EFI $(ISO_DIR)/EFI/BOOT/

	# Copy user_programe.elf file into boot 
	cp -v $(USER_MODULE_DIR)/build/$(USER_PROGRAM_FILE).elf $(ISO_DIR)/boot/$(USER_PROGRAM_FILE).elf

	# Creating KeblaOS-0.11-image.iso file by using xorriso.
	xorriso \
		-as mkisofs \
		-b boot/limine/limine-bios-cd.bin \
		-no-emul-boot -boot-load-size 4 -boot-info-table \
		--efi-boot boot/limine/limine-uefi-cd.bin \
		-efi-boot-part --efi-boot-image \
		--protective-msdos-label $(ISO_DIR) \
		-o $(BUILD_DIR)/$(OS_NAME)-$(OS_VERSION)-image.iso
		
	# install the Limine bootloader into an ISO file, specifically for BIOS-based booting.
	$(LIMINE_SRC_DIR)/limine bios-install $(BUILD_DIR)/$(OS_NAME)-$(OS_VERSION)-image.iso
Now I run it and want to make a detected raw disk bootable.
I write a MBR at sector zero which have following boot code:

Code: Select all

static uint8_t limine_bootstrap_code[512] = {
    // Bootstrap code
    0xFA,                               // cli
    0x31, 0xC0,                         // xor ax, ax
    0x8E, 0xD8,                         // mov ds, ax
    0x8E, 0xC0,                         // mov es, ax
    0x8E, 0xD0,                         // mov ss, ax
    0xBC, 0x00, 0x7C,                   // mov sp, 0x7C00
    0xFB,                               // sti

    // Print "Limine..."
    0xB4, 0x0E,                         // mov ah, 0x0E
    0xB0, 'L', 0xCD, 0x10,              // mov al, 'L'; int 0x10
    0xB0, 'i', 0xCD, 0x10,              // mov al, 'i'; int 0x10
    0xB0, 'm', 0xCD, 0x10,              // mov al, 'm'; int 0x10
    0xB0, 'i', 0xCD, 0x10,              // mov al, 'i'; int 0x10
    0xB0, 'n', 0xCD, 0x10,              // mov al, 'n'; int 0x10
    0xB0, 'e', 0xCD, 0x10,              // mov al, 'e'; int 0x10
    0xB0, '.', 0xCD, 0x10,              // mov al, '.'; int 0x10
    0xB0, '.', 0xCD, 0x10,              // mov al, '.'; int 0x10
    0xB0, '.', 0xCD, 0x10,              // mov al, '.'; int 0x10

    // Prepare DAP (Disk Address Packet)
    0xBE, 0x50, 0x7C,                   // mov si, 0x7C50
    0x31, 0xC0,                         // xor ax, ax
    0x8E, 0xC0,                         // mov es, ax
    0x8A, 0x16, 0x80, 0x7C,             // mov dl, [0x7C80] (boot drive)
    0xB4, 0x42,                         // mov ah, 0x42
    0xCD, 0x13,                         // int 0x13
    0x73, 0x02,                         // jnc load_ok
    0xF4,                               // hlt (on error)
    // load_ok:
    0xEA, 0x00, 0x7E, 0x00, 0x00,       // jmp 0x0000:0x7E00

    // Disk Address Packet (DAP) at 0x7C50
    0x10, 0x00,                         // size=16, reserved=0
    0x01, 0x00,                         // read 1 sector
    0x00, 0x7E,                         // offset=0x7E00
    0x00, 0x00,                         // segment=0x0000
    0x00, 0x08, 0x00, 0x00,             // LBA low: 2048
    0x00, 0x00, 0x00, 0x00,             // LBA high: 0

    // Padding to make total 510 bytes of code + data
    // Fill remaining space with zeros
};
Which show load at LBA sector 2048 where sector 2048 have fat32 partition I have make directory structure like iso9660 bootable disk and copied corresponding file. Do I need to find the sector location of limine-bios.sys to load next stage or anything else??
User avatar
BenLunt
Member
Member
Posts: 1039
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: How could I make a blank raw disk bootable ?

Post by BenLunt »

May I ask why you are using a CD-ROM image instead of a Hard Drive (or USB thumb drive) image?

The ISO9660 has a mechanism already attached that (through the firmware) will load a number of sectors to an arbitrary location and then jump to that location.

Also, since you are wanting to place a FAT32 partition on the image, please note that many FAT drivers assume a 512-byte sector size, not a 2048-byte sector size, "assume" being the big word here.

On a CD-ROM, the first 16 sectors are usually cleared to zero, however, I have seen (and used) CD-ROM images that are called hybrid images that allow an ISO boot or a Hard-drive boot. However, please note that the Hard-drive boot may assume 512-byte sectors, and if you are not using the firmware, your "ReadSector()" routine may need to account for that by calculating the offset, reading a 2048-byte sector, then extracting the 512-byte portion.

I noticed you also are adding a /EFI/BOOT/ directory and at least a BOOTX64.EFI file, however, you are including a Legacy Firmware boot strap. The two are not interchangeable. EFI does not use the legacy boot, and legacy boot does not use BOOTX64.EFI. You have to have one or the other.

If you are just getting started, I would recommend that you steer clear of the CD-ROM ISO9660 image and use a Hard-Drive image formatted with a GPT partitioning scheme, with a moderate first partition to hold the EFI boot file(s), kernel file, and system files, and then a much larger partition for the remainder of the user space partitioning scheme.

An advantage to doing this is that if you ever want to run your code on real hardware, it is much easier to modify a Thumb Drive, whereas using CDs, you will have to either use RW-CDs or keep discarding CDs and write new ones each time, each CD type taking a considerable amount of time to write, whereas the USB Thumb Drive is usually a single file that needs to be modified, and is quite quick to modify.

Ben
- https://www.fysnet.net/osdesign_book_series.htm
Octocontrabass
Member
Member
Posts: 6275
Joined: Mon Mar 25, 2013 7:01 pm

Re: How could I make a blank raw disk bootable ?

Post by Octocontrabass »

gamingjam60 wrote: ↑Tue Oct 14, 2025 12:46 amI write a MBR at sector zero which have following boot code:
Your code doesn't work because you're loading the VBR at 0x7E00 instead of 0x7C00.
gamingjam60 wrote: ↑Tue Oct 14, 2025 12:46 amDo I need to find the sector location of limine-bios.sys to load next stage or anything else??
No. Any standard MBR will work.
Post Reply