/****************************************************************************
 * LOGOLOCK - Software Security System for CLAYCO        - Sean Clay - 1993 *
 *   Program is in TWO parts:      (1) logo() - Draw the CLAYCO logo        *
 *                                 (2) lock() - Re-generate the lock code
 ****************************************************************************/

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <bios.h>

/* --------------------[ Define Functions ]------------------------- */

#define NUL 0
#define MAX_TRACK   512                   // ONLY NEED 512
#define ALL_SECTORS_READABLE 0            // Exit (ERRORLEVEL) values
#define DISK_HAS_BAD_SECTORS 1            // Sensed bad sectors
#define DRIVE_DOOR_OPEN 6                 // Maximum bytes/sector

/*****************************Prototypes******************************/
  void    ResetDrive          ( int );
  unsigned int   readSectors         ( int, unsigned int, unsigned int,
				     unsigned int, unsigned int );
  unsigned char  SelectBoot          ();

void clayco_lock(void);
void logo(void);
void DrawLine(int, int, int);

/**************************  Global Data******************************/
 unsigned int select=0;
 unsigned char oneSector[MAX_TRACK];
 unsigned char far *diskBuffer  = oneSector;

 unsigned int  cylinder ,
	       drive    ,
	       head = 0 ,
	       sector   ,
	       sectorsToRead,
	       t = 0    ,
	       track    ,
	       returnCode = ALL_SECTORS_READABLE;

/* --------------------[ Main Routine ]-------------------- */
main()
{
    logo();		/* Draw logo */
    clayco_lock();	/* Set the lock file */
}

/***************************************************************************
 * clayco_lock() function - Set security code based on disk serial number. *
 ***************************************************************************/
void clayco_lock(void)
{
char sec_key[20] = "XXXXXXXX",
     ser_number[20] = "C200000",
     lock_code[20] = "0000";

  int key1 = 0,			/* security code part1 */
      key2 = 0;			/* security code part2 */

  FILE *clayco_file;			/* ptr to FILE */

/* ---------------[ Read Drive C Boot Sector ]------------------- */
  drive=0x80;	/* drive C */
  select=1;	/* Hard Disk */
  SelectBoot();        /* this does all the work */
  key1=abs(diskBuffer[42]); key2=abs(diskBuffer[41]);
  while (key1>99) key1=key1/2;
  while (key2>99) key2=key2/2;
/* ------------------[ Read Current Information from File ]----------- */
  clayco_file = fopen("clayco.ssn","r");
  if(clayco_file==NULL)  { printf("\nSerial number file is Missing!\n");
      exit(0);
  }
  else  {
    while(!feof(clayco_file)) {
      fread(ser_number,1,7,clayco_file);
      fread(lock_code,1,4,clayco_file);
      fread(sec_key,1,8,clayco_file);
      break;
    }
  }
  fclose(clayco_file);
/* ------------------[ Write Security Code to File ]----------- */
  clayco_file = fopen("clayco.ssn","w");
  if(clayco_file==NULL)  { printf("\nSerial number file is Missing!\n");
      exit(0);
  }
  ser_number[7]='\0';
  lock_code[4]='\0';
  sec_key[8]='\0';
  fprintf(clayco_file,"%s",ser_number);
  fprintf(clayco_file,"%02d%02d",key1,key2); /* security code */
  fprintf(clayco_file,"%s",sec_key);
  fclose(clayco_file);			/* close the file */
} /* End of clayco_lock() */

/************************************************************************/
/* logo.c - Draws the CLAYCO logo in Red, White, and Blue.              */
/* Proprietary Software of CLAYCO - Copyright (c) 1991 - Revision 1.0A  */
/************************************************************************/

void logo(void)
{

int Red[3][16] =
     {  { 8,18,71, 7,13,18,70, 6,17,69, 5,16,22,42,52,63},
	{ 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7, 7},
	{ 7, 3, 1, 2, 1, 2, 3, 2, 2, 5, 2, 2, 4, 4, 4,17}  };

int White[3][22] =
  {  { 4,15,20,25,30,35,40,45,50,55,65, 3,14,19,24,29,34,39,49,54,66,72},
     { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
     { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,13, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5}  };

int Blue[3][37] =
  {  { 2,13,18,23,28,33,38,48,53,65,74, 1, 8,12,17,22,27,32,37,
      42,47,52,64,76, 2,11,18,23,28,38,48,63,78,30,29,28,27     },
     {10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,
      11,11,11,11,11,12,12,12,12,12,12,12,12,12,13,14,15,16     },
     { 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 2, 1, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 3, 3, 6, 3, 4, 2, 5, 4, 4, 2, 2, 2, 2, 2, 2     }  };

  int I;

  window( 1, 1, 80, 25);
  textbackground( BLACK );
  clrscr();
  textcolor( RED );
  for(I=0; I<16; I++) DrawLine(Red[0][I], Red[1][I], Red[2][I]);
  textcolor( WHITE );
  for(I=0; I<22; I++) DrawLine(White[0][I], White[1][I], White[2][I]);
  textcolor( BLUE );
  for(I=0; I<37; I++) DrawLine(Blue[0][I], Blue[1][I], Blue[2][I]);
  textcolor( WHITE );
  gotoxy(15,22);
  cputs("Copyright (c) 1994 - CLAYCO -   All rights reserved");
  sleep(5);
  clrscr();
}

void DrawLine(int X, int Y, int Length)
{
  int I;
  for(I=X; I < X+Length+1; I++) {
    gotoxy(I,Y);
    cputs("\xCD");
  }
} /* end DrawLine */




//***********************Get boot sector***************************
unsigned char SelectBoot()
{
  unsigned int errorCode=0;

  int try       = 0;                   //counter for re-try
  track         = 0;                   //values--Boot is track 0,
  cylinder      = track;               //head = O, sector = 1,
  head          = select;              //0 for floppy, 1 for HD
  sector        = 1;                   //Number of Sectors to Read = 1
  sectorsToRead = 1;                   //master partition boot sector


  for(try=0; try<3; try++)
  {
     ResetDrive(drive);
     errorCode = readSectors(drive, head, cylinder, sector, sectorsToRead);
     if(!errorCode)
	break;
  }
  if(errorCode)
  {
      exit(0);
  }

}

//************************Read-data Data Field *******************************

 unsigned int readSectors(int drive,unsigned int head,unsigned intcylinder,
		      unsigned int sector,unsigned int sectorsToRead )
 {

     union REGS regs;
     struct SREGS sregs;

     unsigned int sectorMask;

 while(1)
 {
   sectorMask = (cylinder>>2);          //get cylinder number high 2 bits
   sectorMask &= ( 128 + 64 );          //need this mask for hard disks only
   sectorMask +=sector;                 //now add sector # into low 6 bits

   regs.h.ah   = 2;                     // AH = 2 - read sectors
   regs.h.al   = sectorsToRead;         // AL = number to read
   regs.h.ch   = cylinder;              // CH = track or cylinder low 8 bits
   regs.h.cl   = sectorMask;            // CL = sector(diskette or sector +
					// cyl(upper 2 bits+sector(hard disk)
   regs.h.dh   = head;                  // DH = head
   regs.h.dl   = drive;                 // DL = drive
   regs.x.bx   = FP_OFF( diskBuffer );  // BX = data buffer offset
   sregs.es    = FP_SEG( diskBuffer );  // ES = data buffer segment

   int86x( 0x13, &regs, &regs, &sregs ); // Call BIOS

   if (regs.x.cflag == 0 )               //Carry false = 0 = no errors
       break;
   if (regs.h.ah !=DRIVE_DOOR_OPEN)
   {
       return (regs.h.ah);               //Return errorCode if hard error
       printf("L469 if error then errorCode=%d",regs.h.ah);
   }
   return(0);
 }
 return(0);                              // Return 0 no error
}

// ********************  Physical Disk I/O Functions  ******************* 
// Reset drive and controller - Do recalibrate & then see if error is real

void ResetDrive( int driveNumber )
{
   union REGS regs;

   regs.h.ah = 0;                   // AH = 0 - reset drive and controller 
   regs.h.dl = driveNumber;         // DL = drive number 
   int86( 0x13, &regs, &regs );     // Call BIOS
}


/* ========================[ END OF PROGRAM ]===========================*/
