From Floppy to HDD

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
AndyB
Posts: 15
Joined: Fri Dec 17, 2010 5:43 pm

From Floppy to HDD

Post by AndyB »

I have been experimenting and trying to put a simple bootstrap program on a FAT32 formatted hard disk image.

I followed the steps outlined on the Loopback_Device wiki entry to create and format the image file. The fdisk print-out of the partition table:
Disk hdd.img: 51 MB, 51609600 bytes
16 heads, 63 sectors/track, 100 cylinders, total 100800 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x308a691d

Device Boot Start End Blocks Id System
hdd.img1 * 2048 100799 49376 c W95 FAT32 (LBA)
The bootstrap program is just simply prints hello world and hangs, the important part is the BPB, which comes straight from QUASI, is as follows:
OEM_ID db "AABBCCDD"
BytesPerSector dw 0x0200
SectorsPerCluster db 0x08
ReservedSectors dw 0x0020
TotalFATs db 0x02
MaxRootEntries dw 0x0000
NumberOfSectors dw 0x0000
MediaDescriptor db 0xF8
SectorsPerFAT dw 0x0000
SectorsPerTrack dw 0x003D
SectorsPerHead dw 0x0002
HiddenSectors dd 0x00000000
TotalSectors dd 0x00FE3B1F
BigSectorsPerFAT dd 0x00000778
Flags dw 0x0000
FSVersion dw 0x0000
RootDirectoryStart dd 0x00000002
FSInfoSector dw 0x0001
BackupBootSector dw 0x0006

TIMES 13 DB 0 ;jumping to next offset

DriveNumber db 0x00
Signature db 0x29
VolumeID dd 0xFFFFFFFF
VolumeLabel db "AABBCCDDEEF"
SystemID db "FAT32 "
According to the wiki MBR, when fdisk partitions a drive it will install an MBR which does the following:

The MBR that FDISK uses is coded to:
  • relocate itself to 0x0000:0x0600
  • examine the byte at offset 0x1be, 0x1ce, 0x1de, and 0x1ee to determine the active partition
  • load only the first sector of the active partition (which is expected to contain a DOS bootsector) to 0x0000:0x7c00 (hence the previous relocation)
  • set SI
  • jump to 0x7c00 -- transferring control to the DOS bootsector.
As in the above table, the FAT32 partition starts at block 2048, so I would expect that I could write my bootstrap program to the first sector of the partition, boot the disk with qemu and the MBR would load my program from the first sector of the active partition.

I have attempted this with:
dd if=boot of=hdd.img seek=2048 conv=notrunc
and also with a seek value of 2047 in case that dd would seek past 2047 and then start writing at 2048. But unfortunately neither of these attempts work.
After copying the new sector across the image can still be mounted and written to, but if I boot it there is no "Hello World".

Either one of my steps is wrong or I am missing a step.
I was hoping that someone here could shine some light on the subject for me?

Thanks for your time in reading.
Andy
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: From Floppy to HDD

Post by DavidCooper »

Someone else might be able to solve that just by reading your post, but I'd need to see the MBR and VBR sectors. Have you looked at them with a hex editor to check what's actually been written in the disk image? I'd look at the partition table directly to find out where it actually says the active partition starts, and then I'd look to see if the VBR is where it's supposed to be. If that's all in order, I'd put in a new VBR temporarily just to check whether it's being loaded and jumped to by the MBR, and I'd use the simplest possible code in it to print something to the screen and enter into an infinite loop. If it doesn't print anything, move this temporary VBR into the MBR position and see if it prints to the screen from there (while making sure you can restore the MBR afterwards). By doing this, you can narrow down the place where the problem lies. If you're still stuck with it after trying that, you can post the hex of the MBR and the program code for your VBR here (or a link to the image file for downloading) and someone will doubtless find the problem for you before I get a chance to do so.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
kammce
Posts: 18
Joined: Sun Apr 15, 2012 9:17 pm

Re: From Floppy to HDD

Post by kammce »

I am still having problems with this same issue. Did you ever find a fix for your issue? If so, what was it? I attempted the same thing except I actually rewrote my MBR with the grub stage1 file... which proved to fail me in the long run.
AndyB
Posts: 15
Joined: Fri Dec 17, 2010 5:43 pm

Re: From Floppy to HDD

Post by AndyB »

I have not had much time lately but fortunately I just sat down and got it working without much hassle.

As above I was writing my MBR to the first sector and then afterwards partitioning with fdisk. On DavidCoopers advice I checked the image out in a hex editor and unfortunately fdisk does not only modify the patition table it also zeros out the first 446 bytes that is the MBR code.

Ive just been playing around and got it working completely though.

Firstly follow the Hard Disk Image steps on the wikis Loopback_device page to create an empty hard disk image.
Again, following the steps in the article I partitioned the image with a single primary FAt32 partition marked as active.

Now, while the image is setup on a loopback device but NOT mounted, copy your VBR to the patition.

Code: Select all

dd if=boot of=hdd.img bs=512 count=1 conv=notrunc
At this stage you have an partitioned image and your stage 1 bootloader in the first sector of the FAT32 partition.

Now use nasm to compile the attached MBR source file and copy that to the very first sector of the overall image, not the FAT32 partition.

Code: Select all

dd if=mbr of=hdd.img bs=446 count=1 conv=notrunc
Note the output size of 446, we only want to write the mbr code, not overwrite the partition table that has been set up.

Now you should be able to boot, the MBR will detect your active partition and load your bootsector for you.
Well worth the work to have FAT32.

Ive attached some MBR and VBR source files. I take no credit for any of the code. The MBR came from a website that I cant find anymore but I believe it said it was a disassembly of an FDISK MBR. As for the boot.asm is from BrokenThorn Entertainments OSDev series with a FAT32 BPB from quasi boot.
Attachments

[The extension s has been deactivated and can no longer be displayed.]

mbr.asm
(10.35 KiB) Downloaded 85 times
Post Reply