Jump to content
  • Advertisement
Sign in to follow this  
godsenddeath

Creating a floppy disk image

This topic is 2996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

At one point a year ago I was playing around with the OS dev tutorials at Broken Thorn, which are really, really good, but now I'm running Windows 7, and Virtual Floppy Drive won't work properly due to driver signing (and because I'm running 64 bit). My solution is to write a program that will create a floppy disk image, and copy my bootloader data into the first part of it, but I can't find any information on the layout of a floppy disk image (.IMG). I thought it would just be a flat 1.44 MB binary file, but that doesn't seem to work. Does anyone have any experience or bookmarked resources they can share? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I dunno wich program u are using to access ur floppy image, but im currently doing some similar stuff, and written a program to create my floppy image, to test with a virtual machine, and it work just fine. There's no header, just 2880 sectors of 512 bytes to write into a file, and ur done, also remember to write the boot sector in the first sector (first 512 bytes). Also, doing this will render the floppy image unreadable by windows, this is normal since the 1st sector just been altered.

Here's my code to create my image from a .exe containing the boot sector data:


#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

void main(int argc,char *argv[])
{
// Make sure we have at least 2 arguments
if(argc < 2){
printf("Usage :.. <sourcefilename> <outputfilename>");
exit(0);
}

// The File Handles
FILE *srcFile, *dstFile;

// Open the source file for reading
printf("Opening exec file %s",argv[1]);
if((srcFile = fopen(argv[1],"rb"))==NULL){
printf("\nfatal-error: file (%s) not found. exiting..",argv[1]);
exit(1);
}

// Usefull values
const int NumSectorPerFloppy = 2880;
const int SectorSize = 512;
const int BufferSize = SectorSize * 2;

// Create a 1k buffer to read our .exe data into and fill it with 0s
char* ExecBuffer = new char[BufferSize];
for(int Cpt = 0; Cpt < BufferSize; Cpt++)
ExecBuffer[Cpt] = 0;

// Read the source file data(should be 1k)
int NumBytesRead = 0;
NumBytesRead = fread(ExecBuffer,1,BufferSize,srcFile);

// Make sure we've read 1024 bytes
if(NumBytesRead < BufferSize){
printf("\nfatal-error: .Exe file too small (must be 1k)",argv[2]);
delete [] ExecBuffer;
fclose(srcFile);
exit(1);
}

// Create the output image file
if((dstFile = fopen(argv[2],"wb+"))==NULL){
printf("\nfatal-error: output file (%s) could not be created. exiting..",argv[2]);
delete [] ExecBuffer;
fclose(srcFile);
exit(1);
}
printf("\nCreating destination file %s",argv[2]);

// Create a buffer of 512 bytes and fill it with 0s
char *EmptyBuffer = new char [SectorSize];
for(Cpt = 0; Cpt < SectorSize; Cpt++)
EmptyBuffer[Cpt] = 0;

// Write the image data
for(Cpt = 0; Cpt < NumSectorPerFloppy; Cpt++){
switch(Cpt)
{
case 0: fwrite(&ExecBuffer[SectorSize], 1, SectorSize, dstFile); break;
default: fwrite(&EmptyBuffer[0], 1, SectorSize, dstFile); break;
}
}

// Delete allocated memory
delete [] EmptyBuffer;
delete [] ExecBuffer;

// Close the files
fclose(dstFile);
fclose(srcFile);

printf("\nDone.\n");
exit(0);
}










[Edited by - Vortez on April 12, 2010 7:26:27 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!