Page 1 of 1
[SOLVED] Virtual Disk (Bochs Disk File) and MBR
Posted: Wed Jun 28, 2006 3:40 pm
by yonami
Hi.
I am newbie in OS programming.
I must boot my kernel from hard disk - not floppy. So I have create virtual disk, and here is a little problem. I do not know how to write MBR section for my Bootloader.
Simple bootloader in 512 bytes, should call extended bootloader over 512 - 'cose file system driver is needed to boot kernel from disk. So for testing I must write my own file system but first write bootloader to virtual hard disk.
I know how to copy files to virtual disk but I really do not know how to write bootsector... It is passible?
I really need my own file system. So I really need my own bootloader at mbr...
I am using Linux - any idea?
Thanks a lot.
Posted: Wed Jun 28, 2006 6:50 pm
by AndrewAPrice
Disk images in Bochs are basically raw disk images. Find out how big the sectors are for the harddrive (usualy 512 bytes), and write a binary bootloader that is that size, and replace the beginning of the disk image with your bootloader.
The master boot record also needs to store a list of partitions in a partition table, and later you'll probably need and extended MBR to help store the partition table in if 512 bytes isn't enough (Windows XP reserves 8MB for the extended MBR!)
If partitioning is too hard you could write a file system header to the first sector of the disk and use the whole disk as a single partition. You can do anything you like, it's you're operating system!
Just remember that only the first sector of the hard drive is in memory when the system is powered on.
EDIT: You can find plenty of examples of MBRs through a simple Google search!
Posted: Thu Jun 29, 2006 7:43 am
by JAAman
I really need my own file system. So I really need my own bootloader at mbr...
no you dont... the MBR doesnt know anything about the filesystem -- only the partitions, your boot sector should be at the first sector of the partition, which the MBR will load just like BIOS would
I am using Linux - any idea?
not really familier with linux (as i usually use windows), but iirc linux comes with tools to create disk images (and even format them -- this should take care of your MBR) and insert sectors (for adding your bootsector)
The master boot record also needs to store a list of partitions in a partition table, and later you'll probably need and extended MBR to help store the partition table in if 512 bytes isn't enough (Windows XP reserves 8MB for the extended MBR!)
this isnt quite accurate -- boot menu programs (not really part of the MBR), commonly use sectors between the MBR and the first partition (edit: also some partition managers use this space to create backups of the MBR/partition table) -- traditionally, partitions always start on the first sector of a cylinder, therefore an entire cylinder is unused between the MBR and the first partition (this wasnt intended for an extended MBR, but rather to simplify sector adjustment)
modern systems have no concept of 'cylinder' as they use LBA (for obvious reasons), however, for compatability, winXP reserves some space:
512 (bytes/sector) * 63 (max sector/track) * 254 (max heads) = 8,193,024 -- almost 8MB -- noone could possibly expect more than that
Posted: Thu Jun 29, 2006 9:56 am
by yonami
OK. But I know that
I am asking how to write my 512 bytes on disk file. I can't copy bootloader as a single file. It must be in MBR section.
I think I must write simple code, and run with Bochs as pseudo-kernel. Than than kernel should write on virtual disk byte by byte my Bootloader ...
Good I think?
Posted: Thu Jun 29, 2006 10:08 am
by chase
yonami wrote:OK. But I know that
I am asking how to write my 512 bytes on disk file. I can't copy bootloader as a single file. It must be in MBR section.
I think I must write simple code, and run with Bochs as pseudo-kernel. Than than kernel should write on virtual disk byte by byte my Bootloader ...
Good I think?
Are you just asking what utility to use to copy your compiled code into the correct section of your virtual disk image?
If so I'm assuming that the disk image is a file that is a raw image(no special compressed virtual machine format). In Linux you'd use "dd" like this:
Code: Select all
dd if=/path/mymbr.bin of=/path/image.dsk bs=512 count=1
That assumes you're trying to update the first sector in a raw disk image. If you want to update a 512 byte boot sector that exists at the second sector of the virtual disk you'd do this:
Code: Select all
dd if=/path/mysector.bin of=/path/image.dsk bs=512 count=1 seek=1
EDIT: You could probably just have your compiled MBR be your virtual disk image if you wanted, just add zeros to the end of it to get the right disk size.
Posted: Thu Jun 29, 2006 10:24 am
by yonami
EDIT:
that is what I want to know
it is not a question
Thanks

Posted: Fri Jun 30, 2006 7:55 pm
by AndrewAPrice
yonami wrote:Is that what I want to know

If that is a question, then the answer is yes!
Posted: Sat Jul 01, 2006 10:28 pm
by yonami
OK. Now I can't write Extended Bootloader
Should I use seek=512 to write Extended Bootloader at 512 (200h)?:
Code: Select all
dd if=boot-ext.out bs=308 of=/dev/loop/0 seek=512
Posted: Sun Jul 02, 2006 11:55 am
by rexlunae
yonami wrote:OK. Now I can't write Extended Bootloader
Should I use seek=512 to write Extended Bootloader at 512 (200h)?:
Code: Select all
dd if=boot-ext.out bs=308 of=/dev/loop/0 seek=512
Not quite. Seek is in units of bs, so to write the second sector of a drive, use bs=512 and seek=1.