/* MakeMarkDiskData
 * reads bootblock, rootblock and bitmap of a disk and generates
 * a datafile (e.g. MarkDiskData.c) for the use with MarkDisk.c
 * the datafile is to compile and to link to MarkDisk
 */

#include <exec/types.h>
#include <exec/memory.h>
#include <devices/trackdisk.h>
#include <libraries/dosextens.h>
#include <proto/all.h>
#include <stdio.h>
#include <fcntl.h>

#define TRACK_SIZE			((LONG)(NUMSECS * TD_SECTOR))

SHORT Read_Block(struct IOExtTD *disk,UBYTE *buffer,SHORT block)
{
disk->iotd_Req.io_Length=TD_SECTOR;
disk->iotd_Req.io_Data=(APTR)buffer;
disk->iotd_Req.io_Command=CMD_READ;
disk->iotd_Req.io_Offset=(ULONG)(TD_SECTOR * block);
DoIO((struct IORequest *)disk);
if (disk->iotd_Req.io_Error) return (FALSE);
return (TRUE);
}

void Motor_Off(struct IOExtTD *disk)
{
disk->iotd_Req.io_Length=0;
disk->iotd_Req.io_Command=TD_MOTOR;
DoIO((struct IORequest *)disk);
}

void main(int ac,char **av)
{
struct IOExtTD *diskreq;
struct MsgPort *diskPort;
ULONG *block;
int i;
if (ac==2)
	{
	FILE *file;
	if (file=fopen(av[1],"w"))
		{
		fprintf (file,"/* boot-, root- and mapblock data for MarkDisk.c\n * generated by MaC!\n*/\n\n#include<exec/types.h>\n");
		printf("Please insert a freshly formatted disk into df0: and press return!");
		while(getchar()!='\n');
		if (diskPort=CreatePort(NULL,NULL))
			{
			if (diskreq=(struct IOExtTD *)CreateExtIO(diskPort,sizeof(struct IOExtTD)))
				{
				if (!OpenDevice(TD_NAME,0L,(struct IORequest *)diskreq,0L))
					{
					if (block=AllocMem(TD_SECTOR,MEMF_CHIP|MEMF_PUBLIC))
						{
						if (Read_Block(diskreq,(UBYTE*)block,0))
							{
							fprintf (file,"\nULONG bbdata[] = {\n");
							for (i=0;i<TD_SECTOR/4;i++) fprintf (file,"0x%08x%s",block[i],((i==(TD_SECTOR/4-1))?("\n};\n"):((i%8==7)?(",\n"):(","))));
							if (Read_Block(diskreq,(UBYTE*)block,880))
								{
								fprintf (file,"\nULONG rbdata[] = {\n");
								for (i=0;i<TD_SECTOR/4;i++) fprintf (file,"0x%08x%s",block[i],((i==(TD_SECTOR/4-1))?("\n};\n"):((i%8==7)?(",\n"):(","))));
								if (Read_Block(diskreq,(UBYTE*)block,881))
									{
									fprintf (file,"\nULONG mbdata[] = {\n");
									for (i=0;i<TD_SECTOR/4;i++)	fprintf (file,"0x%08x%s",block[i],((i==(TD_SECTOR/4-1))?("\n};\n"):((i%8==7)?(",\n"):(","))));
									} else printf("Couldn't read Mapblock!\n");
								} else printf("Couldn't read Rootblock!\n");
							} else printf("Couldn't read Bootblock!\n");
						FreeMem(block,TD_SECTOR);
						Motor_Off(diskreq);
						} else printf("Out of Chio-Chip-Mem!\n");
					CloseDevice((struct IORequest *)diskreq);
					} else printf("Could not acces disk!\n");
				DeleteExtIO((struct IORequest *)diskreq);
				} else printf("Out of memory\n");
			DeletePort(diskPort);
			} else printf("Couldn't create diskPort\n");
		fclose(file);
		} else printf ("Couldn't open outputfile '%s'!\n",av[1]);
	} else printf ("Usage: %s filename\n",av[0]);
}
