Page 1 of 2

Running a bootloader (of sorts) on bare hardware...

Posted: Sat Sep 03, 2011 12:53 pm
by sdfkjsdfkj
I have a bootloader that prints out a line of text and loads the sector next to it and jumps to where it placed it in memory, I'm trying to figure out how to get
it to run on bare hardware without directly overwriting my bootsector on my hard drive. Since its two sectors (1KB) I'm unable to get mkisofs to make a bootable
iso for me, it does not have support for file systems yet so its just doing a raw read on the disk, do I have to add this before I'm going to be able to test it?

As always if I'm being stupid, point it out please

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Sat Sep 03, 2011 1:05 pm
by sdfkjsdfkj
Also as a side question, is there a BIOS call or some other way that can tell me what device the code was loaded from, so instead of taking a shot in the dark and having the possibility of loading my next sectors from a device that I wasn't loaded from, I can load and run what I want to.

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Sat Sep 03, 2011 2:07 pm
by sdfkjsdfkj
Am I going to have to add file system support or what do I do about the 2 sector bootloader?
(Also creating a blank, no bootsector floppy image isn't very helpful)

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Sat Sep 03, 2011 5:01 pm
by Brendan
Hi,
ajtgarber wrote:I have a bootloader that prints out a line of text and loads the sector next to it and jumps to where it placed it in memory, I'm trying to figure out how to get
it to run on bare hardware without directly overwriting my bootsector on my hard drive.
That tells me you've got a hard drive boot loader, and need to boot from something that isn't a hard drive.
ajtgarber wrote:Since its two sectors (1KB) I'm unable to get mkisofs to make a bootable
iso for me, it does not have support for file systems yet so its just doing a raw read on the disk, do I have to add this before I'm going to be able to test it?
"El Torito" boot CDs support "hard disk emulation". Where you put an image of a hard disk (including your boot loader) onto a CD, and the BIOS uses that image to emulate a hard disk and boot from it. The mkisofs utility should support this.
ajtgarber wrote:Also as a side question, is there a BIOS call or some other way that can tell me what device the code was loaded from, so instead of taking a shot in the dark and having the possibility of loading my next sectors from a device that I wasn't loaded from, I can load and run what I want to.
When the BIOS starts your boot loader it leaves a "device number" in DL that says which device the BIOS got your first sector from. You'd just use the "device number" value you've been given to load the second sector from the same device. Note that the device number may be a virtual device and not a real device (e.g. a pretend hard drive that is actually a hard drive image on a CD), and in general it's difficult/complicated to determine how BIOS "device numbers" relate to physical hardware (e.g. if you need to access the boot device using your own device drivers).

Also note that you need different boot loaders for different device types:
  • floppy - no partitions, no "int 0x13 extensions", 512-byte sectors, only 1 sector loaded by BIOS
  • hard disk - has partitions, "int 0x13 extensions" may be necessary, 512-byte sectors, only 1 sector loaded by BIOS
  • CD ("no emulation") - no partitions, "int 0x13 extensions" may be necessary, 2048-byte sectors, entire file loaded by BIOS
  • network/PCE - no partitions, no file IO, no sectors, TFTP used instead, entire file loaded by BIOS
This is one area where EFI is cleaner (same boot loader regardless of which type of device you're booting from).


Cheers,

Brendan

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Sun Sep 04, 2011 2:04 am
by Owen
(With the small exception that EFI requires you to embed a FAT image as an El-Torito image on the CD-ROM, and probably ship an EFI ISO9660 driver inside of that)

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Mon Sep 05, 2011 3:26 pm
by sdfkjsdfkj
If while using hard-disk emulation there are partitions, then how does it tell when the partition table begins (if I'm putting my second-stage bootloader sectors right after the boot sector)?

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Mon Sep 05, 2011 3:27 pm
by Owen
El-Torito

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Mon Sep 05, 2011 7:25 pm
by sdfkjsdfkj
How does that tell when the partition tables begin? (how does it know where to look for partition information? is it set? or is it variable?)

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Mon Sep 05, 2011 11:41 pm
by Combuster
You know, it might help to actually google El-Torito and Partition Table before proving your laziness to us.


@Owen: Congrats on the promotion

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Fri Sep 09, 2011 6:31 am
by MihailB
ajtgarber wrote:How does that tell when the partition tables begin? (how does it know where to look for partition information? is it set? or is it variable?)
For HDs:

Every hard disk starts with the MBR(MasterBootRecord) which is contained in the
first sector of the disk as a BOOT sector.

boot records starts at offset +3 from postion 0
(like:
char buf[512];
boot_record *br=(boot_record)(&buf+3);)

some important constants:
boot_record_start = 3;
#define part_01_start 0x1be
#define part_02_start 0x1Ce
#define part_03_start 0x1de
#define part_04_start 0x1ee
#define FAT12 0x01
#define FAT16_small32m 0x04
#define EXTENDED_PART 0x05
#define FAT16_LBA 0x0E
#define FAT16_CHS 0x06
#define FAT32_LBA 0x0c
#define FAT32_CHS 0x0b
#define FAT32_LBA_EXTENDED 0x0F
#define LINUX_NATIVE 0x83
#define LINUX_SWAP 0x82
#define LINUX_EXTENDED 0x85
#define NTFS_extended1 0x86
#define NTFS_extended2 0x87
#define SPF_BOOT_MANAGER 0x20
#define XENIX_root 2
#define XENIX_usr 3
#define NTFS_ID 7
#define LBA_HD 0x60
#define CHS_HD 0x00


here's how the simple boot_record look like :
typedef struct boot_record {
char OEM_Identifier[8];
word BytesPerSector ;
char SectorsPerCluster ;
word ReservedSectors ;
char NumberOfFATs ;
word RootEntries ;
word NumberOfSectors ;
word MediaDescriptor ;
word SectorsPerFAT ;
word SectorsPerHead ;
word HeadsPerCylinder ;
dword HiddenSectors ;
dword BigNumberOfSectors ;
dword BigSectorsPerFAT ;
word ExtFlags ;
word FSVersion ;
dword RootDirectoryStart ;
word FSInfoSector ;
word BackupBootSector ;
} boot_record;

Another important thing is that each MBR contains 4 entries for 4 partitions definitions ! (they start at the following indexes inside the boot sector : 0x1be,
0x1Ce, 0x1de, 0x1ee as defined above)

Here's how the PARTITION TABLE looks like:
typedef struct partition_table {
char Boot_indicator;
char Beginning_head_number ;
char Beginning_sector_and_high_cylinder_number ;
char Beginning_low_cylinder_number ;
char System_indicator ;
char Ending_head_number ;
char Ending_sector_and_high_cylinder_number ;
char Ending_low_cylinder_number;
dword Number_of_sectors_preceding_the_partition; // or LBA start
dword Number_of_sectors_in_the_partition; //or LBA length
} partition_table;

A partition table define both CHS location and LBA location.
CHS = CylinderHeadSector (in terms of cylinders, heads, and sectors , old, old,old)
LBA = LargeBlockAddressing (in terms of secotors [good])

Another important thing :
- in the MBR we can have the following :
partition 1 : Number_of_sectors_preceding_the_partition=63
Number_of_sectors_in_the_partition=10000000 (~5GB)
System_indicator=FAT32_LBA
partition 2 : Number_of_sectors_preceding_the_partition=63+1000000+some few additional sectors
Number_of_sectors_in_the_partition=200000000 (~100GB)
System_indicator= EXTENDED_PART
partition 3 : 0 and 0
partition 4 : 0 and 0

Now partition 2 is of type EXTENDED_PART (extended partition) : it means that at sector [63+1000000+some few additional sectors] we will find another MBR like sector with another 4partition defs, with a maximum usage of Number_of_sectors_in_the_partition=200000000 (~100GB)

Traditionaly the space between the MBR and the Number_of_sectors_preceding_the_partition is left for BOOT_EXTENDERS like SPFDISK, GRUB, etc... there you can put your staff ...

FOR CDs/DVDs :

each CD/DVD which is ISO9660 complient and starts with session 1 at
sector position 16.

At sector 16 you will find the ISO_PRIMARY_DESCRIPTOR....
The 16 sectors left behind can be used to emulate a HARD DISK boot record ...
(now I hope you get it ...) as define by "EL_TORITO"

//ISO9660 Date and Time record
typedef struct date_iso9660 {
char year_[4]; //ascii
char month_[2] //ascii
char day_[2];
char hour_[2];
char min_[2];
char sec_[2];
char houndres_[2];
char greenwich15; // SIGNED ! in units of 15 minutes ! (west is negative, east is positive
} date_iso9660;

typedef struct iso_primary_descriptor{
char VDtype;
char VDid[5];
char version; //=1
char unused1; //=0
char system_id[32]; //(1 To 32)
char volume_id[32]; //(1 To 32)
char unused2[8];
dword total_number_of_sectors[2];
char unused3[32]
word volume_set_size[2];
word volume_sequence_number[2];
word sector_size[2];
dword path_table_size[2]
dword path_table_LE1
dword path_table_LE2
dword path_table_BE1
dword path_table_BE2
char root_dir_rec[34] // directory_record root_dir_rec + filename + 1|0
char volume_set_id[128];
char publisher_id[128];
char preparer_id[128];
char application_id[128];
char copyright_file_id[37];
char abstract_file_id[37];
char bibliographic_file_id[37];
date_iso9660 creation_date;
date_iso9660 modification_date;
date_iso9660 expiration_date;
date_iso9660 effective_date;
char file_structure_version; //=1
char unused4; //=0
char application_data[512]; //don't care
char unused5[653]; //=0
} iso_primary_descriptor;

Now I'm sure you want to learn (if don't know already) about FAT32/16,
ISO9660 and at least EXT2 ...
You will find all the docs with google/yahoo/alta_vista/bing/ or what ever ...

Maybe you are also interested in low level ASM read hd sector and ATAPI CD/DVD read sector ... (it's hard to find atapi read sector example ... unless you visit my web site .... http://mbarboi.home.ro/project_fat.html ... or you can look inside my
project's open-sources http://filesystemd.sourceforge.net)

I hope this will solve some of your problems ...

You can search for this people and their docs if you need more docs :

"Thanks to following people for their good docs ( &tutorial sources)!"
" - Tilmann Reh - (IDE HD tutorial)"
" - IDE ATA HardDisk Specification"
" - Philip J. Erdelsky - ISO9660 Simplified"
" - ISO 9660 - Wikipedia, the free encyclopedia"
" - Joilet Spec - Microsoft"
" - An Introduction To Programming With ATA And ATAPI"
" - Ben Cadieux - WDE asm sources"
" - Jack R. Ellis - XCDROM.SYS sources"
" - Information Technology - AT Attachment Packet Interface (ATAPI)"
" - Hale Landis - How it Works -- Partition Tables"
" - Hsu-Ping Feng - SpfDisk : Values for operating system indicator"
" - Robert Vandervelde - How Windows 95 Stores Long Filenames"
" - Inbar Raz - FAT DIRs' document"
" - Galen C. Hunt(&Gordon Chaffee) -NOTES ON THE STRUCTURE OF THE VFAT FILESYSTEM"
" - Jozsef Hidasi - How does Windows 95 stores LONG FILENAMES"
" - Thomas Kjoernes - File Allocation Table : How It Seems To Work, etc..."
" - Alex Verstak - FAT/PARTITION documents"
" - EXT2 Specification"
" - Wikipedia - EXT2"
" - EXT2 Overview"
" - John's spec of the second extended filesystem (EXPLORE2FS)"
" - Intel IA32/64 CPU Manuals"
" - Christopher Giese - protected-mode demo code"
" - Alexei A. Frounze - protected-mode tutorial"
" - Wim Osterholt - PORTLIST.TXT"
" - Ralph Brown - Interrupts Bible ..."
" - FreeDOS development team - Big thanks to all programmers, of all kind !"
" - FreeBASIC compiler team and forum member - Nothing of this would be possible without you !"
" - NASM - NASMDOC.TXT"
" - FASM - FASMDOC.TXT"
" - Glaciar P. Moreno I - Set's Editor (setedit)"
" - FbEdit"
" - NC, SCANDISK, Win9xDEFRAG for inspiration about GUI"
" - HOME.RO for free web page hosting"
" - google for being such a good 'searcher' :))"
" - AlphaX team members (for moral suport)"
" - wikipedia - most needed enciclopedia (as good as MS Encarta)"
" - all HOBIEST programmers all over the world - Thanks !"
" - A. Einstein , ICHB & MIT - good teachers"

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Sun Sep 25, 2011 8:27 pm
by sdfkjsdfkj
I guess I should come back since I've still not found my answer, I've decided to go with floppy emulation but following the wiki
hasn't really helped me (page being: http://wiki.osdev.org/Mkisofs). I run the command yet end up with:
Warning: creating filesystem that does not conform to ISO-9660.
Warning: -follow-links does not always work correctly; be careful.
I: -input-charset not specified, using utf-8 (detected in locale settings)
Size of boot image is 4 sectors -> genisoimage: Error - boot image './test.bin' has not an allowable size.
Which is why I was wondering what I was supposed to do about my bootloader being more than one sector.
When running it with the -boot-load-size argument it gives me the same message.

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Sun Sep 25, 2011 11:06 pm
by Combuster
ajtgarber wrote:floppy emulation
(...)
Size of boot image is 4 sectors -> genisoimage: Error - boot image './test.bin' has not an allowable size.
Do you know how many sectors a real floppy contains?

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Mon Sep 26, 2011 4:49 am
by sdfkjsdfkj
Assuming a 1.44 MB disk, about 2813?

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Mon Sep 26, 2011 5:17 am
by Solar
Gosh yeah, that's the way to go. Never mind that information like this can be looked up, never mind that Combuster might have a reason to ask (i.e., you should realize that the number of sectors in a floppy has some meaning related to your problem), never mind that looking up what an "allowable size" would be in the mkisofs / genisoimage documentation might be a good idea.

"About 2813", that's the ticket.

Re: Running a bootloader (of sorts) on bare hardware...

Posted: Sun Oct 02, 2011 8:32 pm
by sdfkjsdfkj
Wow I'm an idiot *facepalm*, okay I found two ways to do it, one was to use the -no-emul-boot flag and the other to pad my file to the correct size.
I apologize for being so dense and stubborn.