I'm writing a C#.Net app to run on windows that needs to take an image of a removable disk and chuck it onto a Linux Live USB. The Live USB is the inserted into the target machine and boots, on start up it runs a script which uses the dd command like so to flash it onto another drive:

dd if=/path/to/file/from/csharp/program of=/dev/sdX

The problem I am having is creating the image on the windows side. I have tried my Live Linux out with files I have created on a Linux system using dd and that works fine, but I need to be able to create these files from within a C#.Net application on Windows. I'd rather not have to rely on cygwin or some other dependency so tried to use the Win32 CreateFile function to open the physical device.

CreateFile is called with the first arg set to "\.\F:" (if F: is the drive I want to image), like so:

But when the output file is dd'd back onto a disk using the Live Linux USB the result is not as expected (the disk isn't bootable etc, but from examining the output file in a hex editor, it looks like there is an MBR at the beginning etc).

Is this a problem with endianess or should I using something other than a FileStream to copy the data into the file.

Alternatively is there an example of dd for Windows source code (C# or C++, i've looked at the Delphi for http://www.chrysocome.net/dd and don't totally understand it or have a decent Delphi IDE to pick the code apart) so I can see how that works?

UPDATE/EDIT:

Here is a hex string of the first 512 Bytes that the dd output contains:

This was taken from exactly the same CF card without any editing/writing etc happening, so i'm confused as to why they are so different, but both end with the correct 55 AA bytes too. Does Windows mangle the MBR's on cards when they're accessed this way or is some other weird under the hood stuff happening that I'm not aware of?

I'm assuming using calling "dd" from cygwin from your C# code is out of the question?
–
GregAug 17 '11 at 12:23

It would be a last resort, the problem I think have is converting the //./F: path into something that dd under cygwin understands. So the problem (for me) then becomes how to within the c# application convert my //./F: path into /dev/sdX in cygwin.
–
rb_Aug 17 '11 at 12:38

1

@Kragen I think what is happening here is that the windows version is getting the Volume Boot Record not the Master Boot Record. This is probably because I'm passing to it //./F: and F: is just the first partition, not the Physical Disk. Would be nice if someone else could confirm if I'm correct or not before I get into the next question about how to access the Physical Disk (instead of partition/volume).
–
rb_Aug 17 '11 at 17:02

@rb_ That sounds spot on to me - looking at the two samples the first (produced by dd) looks like a MBR (the first 512 bytes of the disk), wheras the second looks like a boot record (the first 512 bytes of a partition). Both look like valid and completely different boot sectors - there is no way you could accidentally mangle one into the other from a programming mistake.
–
JustinAug 18 '11 at 8:35

2 Answers
2

I think what you have should work - I've tried this myself using a bootable floppy disk image (mounted as a virtual drive using ImDisk) and the resulting file is binary identical to the original image.

The problem is with whatever is using the disk image that you've just written.

There is some subtle differences in dealing with the specific device you are accessing (although I can't think what)

The most likely culprit is step 2. What exactly is it that you are doing with the resulting disk image?

Update: This is written in the comments, but for completeness I thought I'd add it to my answer - it looks like whats happening is that the contents of the first partition of the disk is being written, when instead what is wanted is the contents of the entire disk.

When you take a look at the second hex string (the one produced by sample code) in something like HxD we see this:

And we see something that looks to me a lot like a master boot record. Why? Because in the MBR all of the first 440 bytes is boot code, unlike a FAT boot sector which contains the distinctive bios parameter block (it looks like garbage above, but if you put that through a disassembler you get something that looks like valid 16 bit code).

Also, both of those look like valid and completely different boot sectors (complete with error messages). There is no way that a programming error could have "mangled" one to look like the other - it must just be that the wrong thing is being read.

In order to get CreateFile to return the disk instead of the partition it looks like you just need to pass it a different string, for example @"\\.\PhysicalDrive0" opens the first physical disk.

Some background info, that will explain what I'm doing in more detail. Our old systems used to boot from a FAT formatted CF card, so to update the software on the system, you'd just reflash the CF card using a windows app. The new system doesn't have any support for booting from CF cards, but it does have a hard drive and a USB port. So the plan was, like before use the windows app to flash the CF card, then take a an image of that CF card (in windows with my C# app) copy it onto the Live USB that would then flash the hard drive, from the Live Linux environment using dd, with the CF card image
–
rb_Aug 17 '11 at 14:08

I don't think it is step 2 as if I put a image that i have created from the CF using a Linux VM and dd onto the Live USB it does work.
–
rb_Aug 17 '11 at 14:10

@rb_ 7 Ah I see - so the image is eventually written to a hard disk. In that case I'm not sure why its not working - as long as the first sector (512 bytes) ends with the "valid bootsector" signature bytes (0x55, 0xAA) the BIOS should just go ahead and boot using that boot sector. My guess would be that the BIOS is in fact using that boot sector, but hardware differences mean that the boot loader (that previously worked on your old system) is failing on the new system.
–
JustinAug 17 '11 at 14:35

+1 for complete working code w/namespaces everything so no hunting around necessary. ♡
–
shelleybutterflyAug 17 '11 at 18:01

@Kragen Cool, thanks for re-assuring me about that, I guess I need to ask another question (after a bit o googling) about how to use WMI (or something similar) to get mappings from F:\ to \\.\PhysicalDriveX.
–
rb_Aug 18 '11 at 9:14

This is what i've written to do get the \.\PhysicalDriveX path for a given drive letter. If Pass the drive letter into this and take the return value and pass into CreateFile as the first Param I should now get something similar to dd under Linux.