CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    37

    How to write to bootsector in C for custom bootloader...

    ...I am testing a custom boot loader I wrote and need to know how I can either:

    1) Rewrite the bootloader on the main windows drive OR
    2) Write a new bootloader on a new drive that doesn't already have one.

    To do this I must write the 512 byte bootloader I wrote to sector 0 of the harddisk. I have tried the Debug w command with the correct args but the problem is Windows denies access to doing this (right now trying XP but need to be able to do this in XP and 7). Does anyone konw of any win32 api calls that will allow me to write directly to the disk in sector 0? Thanks.

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: How to write to bootsector in C for custom bootloader...

    You would quite likely end up nuking your hard drive if you did that. Download a program like VirtualBox. Then download or create a floppy disk image. Open both the image and your bootsector in a hex editor and copy the boot sector over the first 512 bytes of the floppy disk image. Then mount the image in VirtualBox.

  3. #3
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664

    Re: How to write to bootsector in C for custom bootloader...

    Use CreateFile(\\.\PhysicalDrive0...)... See KB: http://support.microsoft.com/kb/100027

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: How to write to bootsector in C for custom bootloader...

    If you really were hell bent on testing this on your own hardware (bad idea) then it would be easier to download a live-cd with your favorite (or any) linux distribution.

    From the terminal, you can copy the bootsector into place using:

    Code:
    dd if=mybootsector.bin of=/dev/sda bs=512 count=1
    Assuming sda was the appropriate drive. Like I said, this is a bad idea. Unless you take special care to preserve the partition table, you will nuke your hard drive, and restoring your partitions may be impossible.

  5. #5
    Join Date
    Feb 2009
    Posts
    37

    Re: How to write to bootsector in C for custom bootloader...

    Chris, thanks for the input. I am indeed testing this on a dummy virtualbox vdi drive.

    Jonas, thanks I will check out the link.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured