2005-04-01 10:13:09 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* diodemo.c */
|
|
|
|
/* */
|
|
|
|
/* Direct Disk I/O Demo Program */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* (C) Copyright 2005, Oliver Schmidt, <ol.sc@web.de> */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-03-31 07:28:14 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <conio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dio.h>
|
|
|
|
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
#define MAX_CHUNKS 10 /* Maximum acceptable number of chunks */
|
2005-03-31 07:28:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
static unsigned char ScreenX;
|
|
|
|
static unsigned char ScreenY;
|
|
|
|
|
|
|
|
|
|
|
|
static void ClearLine (void)
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Clear the screen line the cursor is on */
|
2005-03-31 07:28:14 +00:00
|
|
|
{
|
|
|
|
cputc ('\r');
|
|
|
|
cclear (ScreenX);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-11 18:22:49 +00:00
|
|
|
static unsigned char AskForDrive (const char* Name)
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Ask for a drive id and return it */
|
2005-03-31 07:28:14 +00:00
|
|
|
{
|
2012-10-11 18:22:49 +00:00
|
|
|
unsigned char Drive = 0;
|
|
|
|
char Char;
|
2005-03-31 07:28:14 +00:00
|
|
|
|
|
|
|
cprintf ("\r\n%s Drive ID ?", Name);
|
|
|
|
|
|
|
|
do {
|
|
|
|
Char = cgetc ();
|
|
|
|
if (isdigit (Char)) {
|
|
|
|
cputc (Char);
|
2005-09-02 19:47:46 +00:00
|
|
|
Drive = Drive * 10 + Char - '0';
|
2005-03-31 07:28:14 +00:00
|
|
|
}
|
|
|
|
} while (Char != CH_ENTER);
|
|
|
|
|
|
|
|
return Drive;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-11 18:22:49 +00:00
|
|
|
static void AskForDisk (const char* Name, unsigned char Drive)
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Ask the user to insert a specific disk */
|
2005-03-31 07:28:14 +00:00
|
|
|
{
|
|
|
|
ClearLine ();
|
|
|
|
cprintf ("\rInsert %s Disk into Drive %d !", Name, Drive);
|
|
|
|
|
|
|
|
cgetc ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-11 18:22:49 +00:00
|
|
|
static char* AllocBuffer (unsigned int SectSize, unsigned int SectCount, unsigned int* ChunkCount)
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Allocate a copy buffer on the heap and return a pointer to it */
|
2005-03-31 07:28:14 +00:00
|
|
|
{
|
2005-09-02 19:47:46 +00:00
|
|
|
char* Buffer = NULL;
|
2005-03-31 07:28:14 +00:00
|
|
|
unsigned long BufferSize;
|
|
|
|
unsigned int Chunks = 1;
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Increase number of chunks resp. decrease size */
|
|
|
|
/* of one chunk until buffer allocation succeeds */
|
2005-03-31 07:28:14 +00:00
|
|
|
do {
|
2012-10-11 18:22:49 +00:00
|
|
|
*ChunkCount = (unsigned int) ((SectCount + Chunks - 1) / Chunks);
|
2005-03-31 07:28:14 +00:00
|
|
|
BufferSize = *ChunkCount * (unsigned long) SectSize;
|
|
|
|
if (BufferSize < UINT_MAX) {
|
|
|
|
Buffer = malloc ((size_t) BufferSize);
|
|
|
|
}
|
|
|
|
} while (Buffer == NULL && ++Chunks <= MAX_CHUNKS);
|
|
|
|
|
2005-09-02 19:47:46 +00:00
|
|
|
return Buffer;
|
2005-03-31 07:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-02 19:47:46 +00:00
|
|
|
int main (int argc, const char* argv[])
|
2005-03-31 07:28:14 +00:00
|
|
|
{
|
2012-10-11 18:22:49 +00:00
|
|
|
unsigned char SourceId;
|
|
|
|
unsigned char TargetId;
|
|
|
|
dhandle_t Source = NULL;
|
|
|
|
dhandle_t Target = NULL;
|
|
|
|
unsigned int SectSize;
|
|
|
|
unsigned int SectCount;
|
|
|
|
char* Buffer;
|
|
|
|
unsigned int Sector;
|
|
|
|
unsigned int ChunkCount;
|
|
|
|
unsigned int ChunkOffset = 0;
|
2005-03-31 07:28:14 +00:00
|
|
|
|
|
|
|
clrscr ();
|
|
|
|
screensize (&ScreenX, &ScreenY);
|
|
|
|
|
|
|
|
cputs ("Floppy Disk Copy\r\n");
|
|
|
|
chline (16);
|
|
|
|
cputs ("\r\n");
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Get source and target drive id (which may very well be identical) */
|
2005-09-02 19:47:46 +00:00
|
|
|
switch (argc) {
|
|
|
|
case 1:
|
|
|
|
SourceId = AskForDrive ("Source");
|
|
|
|
TargetId = AskForDrive ("Target");
|
|
|
|
cputs ("\r\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
SourceId = TargetId = atoi (argv[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
SourceId = atoi (argv[1]);
|
|
|
|
TargetId = atoi (argv[2]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
cprintf ("\r\nToo many arguments\r\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cputs ("\r\n");
|
2005-03-31 07:28:14 +00:00
|
|
|
|
|
|
|
do {
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Check for single drive copy or inital iteration */
|
2005-03-31 07:28:14 +00:00
|
|
|
if (SourceId == TargetId || Source == NULL) {
|
|
|
|
AskForDisk ("Source", SourceId);
|
|
|
|
}
|
|
|
|
|
2005-09-02 19:47:46 +00:00
|
|
|
/* Check for initial iteration */
|
2005-03-31 07:28:14 +00:00
|
|
|
if (Source == NULL) {
|
2005-09-02 19:47:46 +00:00
|
|
|
|
|
|
|
/* Open source drive */
|
2005-03-31 07:28:14 +00:00
|
|
|
Source = dio_open (SourceId);
|
|
|
|
if (Source == NULL) {
|
2005-04-01 10:13:09 +00:00
|
|
|
cprintf ("\r\n\nError %d on opening Drive %d\r\n", (int) _oserror, SourceId);
|
2005-03-31 07:28:14 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SectSize = dio_query_sectsize (Source);
|
|
|
|
SectCount = dio_query_sectcount (Source);
|
|
|
|
|
2005-09-02 19:47:46 +00:00
|
|
|
/* Allocate buffer */
|
2005-03-31 07:28:14 +00:00
|
|
|
Buffer = AllocBuffer (SectSize, SectCount, &ChunkCount);
|
|
|
|
if (Buffer == NULL) {
|
2005-04-01 10:13:09 +00:00
|
|
|
cputs ("\r\n\nError on allocating Buffer\r\n");
|
2005-03-31 07:28:14 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearLine ();
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Read one chunk of sectors into buffer */
|
2005-03-31 07:28:14 +00:00
|
|
|
for (Sector = ChunkOffset; Sector < SectCount && (Sector - ChunkOffset) < ChunkCount; ++Sector) {
|
|
|
|
cprintf ("\rReading Sector %d of %d", Sector + 1, SectCount);
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Read one sector */
|
2005-03-31 07:28:14 +00:00
|
|
|
if (dio_read (Source, Sector, Buffer + (Sector - ChunkOffset) * SectSize) != 0) {
|
2005-09-02 19:47:46 +00:00
|
|
|
cprintf ("\r\n\nError %d on reading from Drive %d\r\n", (int) _oserror, SourceId);
|
2005-03-31 07:28:14 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Check for single drive copy or inital iteration */
|
2005-03-31 07:28:14 +00:00
|
|
|
if (TargetId == SourceId || Target == NULL) {
|
|
|
|
AskForDisk ("Target", TargetId);
|
|
|
|
}
|
|
|
|
|
2005-09-02 19:47:46 +00:00
|
|
|
/* Open target drive on initial iteration */
|
2005-03-31 07:28:14 +00:00
|
|
|
if (Target == NULL) {
|
|
|
|
Target = dio_open (TargetId);
|
|
|
|
if (Target == NULL) {
|
2005-04-01 10:13:09 +00:00
|
|
|
cprintf ("\r\n\nError %d on opening Drive %d\r\n", (int) _oserror, TargetId);
|
2005-03-31 07:28:14 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Check for compatible drives */
|
2005-03-31 07:28:14 +00:00
|
|
|
if (dio_query_sectsize (Target) != SectSize ||
|
|
|
|
dio_query_sectcount (Target) != SectCount) {
|
2005-04-01 10:13:09 +00:00
|
|
|
cputs ("\r\n\nFormat mismatch between Drives\r\n");
|
2005-03-31 07:28:14 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearLine ();
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Write one chunk of sectors from buffer */
|
2005-03-31 07:28:14 +00:00
|
|
|
for (Sector = ChunkOffset; Sector < SectCount && (Sector - ChunkOffset) < ChunkCount; ++Sector) {
|
|
|
|
cprintf ("\rWriting Sector %d of %d", Sector + 1, SectCount);
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Write one sector */
|
2005-03-31 07:28:14 +00:00
|
|
|
if (dio_write (Target, Sector, Buffer + (Sector - ChunkOffset) * SectSize) != 0) {
|
2005-09-02 19:47:46 +00:00
|
|
|
cprintf ("\r\n\nError %d on writing to Drive %d\r\n", (int) _oserror, TargetId);
|
2005-03-31 07:28:14 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-01 10:13:09 +00:00
|
|
|
/* Advance to next chunk */
|
2005-03-31 07:28:14 +00:00
|
|
|
ChunkOffset += ChunkCount;
|
|
|
|
|
|
|
|
} while (Sector < SectCount);
|
|
|
|
|
|
|
|
ClearLine ();
|
|
|
|
cprintf ("\rSuccessfully copied %d Sectors\r\n", SectCount);
|
|
|
|
|
|
|
|
free (Buffer);
|
|
|
|
dio_close (Source);
|
|
|
|
dio_close (Target);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|