home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / HARDWARE / ROM2.ZIP / ROM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-18  |  11.6 KB  |  339 lines

  1. /****************************************************************************/
  2. /* PROGRAM:  ROM                                                            */
  3. /* VERSION:  1.0                                                            */
  4. /*    DATE:  December 18, 1989                                              */
  5. /*  AUTHOR:  Sydney M. Willett                                              */
  6. /* DESCRIPTION:  Will either save to file, or restore from file, CMOS ROM   */
  7. /*               data used on AT class IBM or compatible computers.         */
  8. /* INPUTS:  ...Command Line...                                              */
  9. /*          s - save CMOS ROM data to a file named "CMOSROM.DAT"            */
  10. /*          r - restore CMOS ROM data from a file named "CMOSROM.DAT"       */
  11. /*   hh:mm:ss - hours:minutes:seconds in 24 hour format to reconfigure CMOS */
  12. /* mm/dd/yyyy - month/day/year to reconfigure CMOS                          */
  13. /*          b - cold boot the system after program executes                 */
  14. /* OUTPUTS:  Will create a file named "CMOSROM.DAT" when saving CMOS ROM    */
  15. /*           data.  Will restore CMOS ROM data from the file.               */
  16. /****************************************************************************/
  17.  
  18.  
  19. /*============================================================================
  20. INCLUDES
  21. ----------------------------------------------------------------------------*/
  22. #include <conio.h>
  23. #include <dir.h>
  24. #include <dos.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <time.h>
  29.  
  30.  
  31. /*============================================================================
  32. EQUATES
  33. ----------------------------------------------------------------------------*/
  34. #define COLD 0
  35.  
  36.  
  37. /*============================================================================
  38. DATA DECLARATIONS
  39. ----------------------------------------------------------------------------*/
  40. typedef unsigned char BYTE ;
  41. BYTE action = 0 ;
  42. BYTE dateflag = 0 ;
  43. BYTE timeflag = 0 ;
  44. BYTE cent, hour, mday, min, mon, sec, year ;
  45. int temp ;
  46.  
  47.  
  48. /*============================================================================
  49. FUNCTION DECLARATIONS
  50. ----------------------------------------------------------------------------*/
  51. extern BOOT ( int cold_warm ) ;
  52. unsigned char int2bcd ( int integer ) ;
  53. int parse_nbr ( char *string ) ;
  54. void time_date ( void ) ;
  55.  
  56.  
  57. /*============================================================================
  58. FUNCTION:  main
  59. PURPOSE:  Will either save to file, or restore from file, CMOS ROM data used
  60.           on AT class IBM or compatible computers.
  61. RETURNS:  None
  62. ARGUMENTS:  ...Command Line...
  63.             s - save CMOS ROM data to a file named "CMOSROM.DAT"
  64.             r - restore CMOS ROM data from a file named "CMOSROM.DAT"
  65.      hh:mm:ss - hours:minutes:seconds in 24 hour format to reconfigure CMOS
  66.    mm/dd/yyyy - month/day/year to reconfigure CMOS
  67.             b - cold boot the system after program executes
  68. GLOBAL VARIABLES:  action, cent, dateflag, hour, min, mday, mon, sec, temp,
  69.                    timeflag, year
  70. FUNCTIONS USED:  BOOT, cputs, fclose, fcloseall, fgetc, fopen, fread, fwrite,
  71.                  inportb, int2bcd, int86, localtime, outportb, parse_nbr,
  72.                  remove, strchr, strlwr, remove, time, time_date
  73. ----------------------------------------------------------------------------*/
  74. void main ( int argc, char **argv )
  75. {
  76. FILE *cmosfile ;
  77. struct ffblk Ffblk ;
  78. BYTE cmosdata[50] ;
  79. BYTE bootflag = 0 ;
  80. int arglen, i, j, portbyte ;
  81.  
  82.   for (i=1 ; i<argc && action!=0xFF ; i++) /* command line parser */
  83.   {
  84.     arglen = strlen(argv[i]) ;
  85.     strlwr(argv[i]) ;
  86.     j = 0 + (argv[i][0]=='/' || argv[i][0]=='-') ;
  87.     if ( argv[i][j]=='r' || argv[i][j]=='s' ) /* save or restore */
  88.       action = argv[i][j] ;
  89.     else
  90.     {
  91.       if ( argv[i][j] == 'b' ) /* boot */
  92.         bootflag++ ;
  93.       else
  94.       {
  95.         if ( strchr(&argv[i][j],':') != NULL ) /* time input */
  96.         {
  97.           j += parse_nbr(&argv[i][j]) ;
  98.           hour = temp ;
  99.           if ( hour > 23 )
  100.             action = 0xFF ;
  101.           else
  102.           {
  103.             j += parse_nbr(&argv[i][j]) ;
  104.             min = temp ;
  105.             if ( min > 59 )
  106.               action = 0xFF ;
  107.             else
  108.             {
  109.               j += parse_nbr(&argv[i][j]) ;
  110.               sec = temp ;
  111.               if ( j>arglen+1 || sec>59 )
  112.                 action = 0xFF ;
  113.               else
  114.                 timeflag++ ;
  115.             }
  116.           }
  117.         }
  118.         else /* date input (or maybe an error) */
  119.         {
  120.           j += parse_nbr(&argv[i][j]) ;
  121.           mon = temp ;
  122.           if ( mon > 12 )
  123.             action = 0xFF ;
  124.           else
  125.           {
  126.             j += parse_nbr(&argv[i][j]) ;
  127.             mday = temp ;
  128.             if ( mday > 31 )
  129.               action = 0xFF ;
  130.             else
  131.             {
  132.               j += parse_nbr(&argv[i][j]) ;
  133.               if ( j>arglen+1 || temp<1970 || temp>2099 )
  134.                 action = 0xFF ;
  135.               else
  136.               {
  137.                 cent = temp/100 ;
  138.                 year = temp%100 ;
  139.                 dateflag++ ;
  140.               }
  141.             }
  142.           }
  143.         }
  144.       }
  145.     }
  146.   }
  147.   switch ( action )
  148.   {
  149.     case 'r' : cmosfile = fopen("CMOSROM.DAT","rb") ;
  150.                if ( cmosfile == NULL )
  151.                  cputs("\n\r*** Error Opening File CMOSROM.DAT ***\n\r") ;
  152.                else
  153.                {
  154.                  findfirst("CMOSROM.DAT",&Ffblk,0) ;
  155.                  if ( Ffblk.ff_fsize != 50 )
  156.                    cputs("\n\r*** File CMOSROM.DAT Corrupted ***\n\r") ;
  157.                  else
  158.                  {
  159.                    fread(&cmosdata[0],sizeof(BYTE),50,cmosfile) ; /* read cmos data file */
  160.                    for (portbyte=14 ; portbyte<=63 ; portbyte++) /* restore cmos data */
  161.                    {
  162.                      outportb(0x70,portbyte) ;
  163.                      outportb(0x71,cmosdata[portbyte-14]) ;
  164.                    }
  165.                    time_date() ;
  166.                    if ( bootflag ) /* cold boot system if requested */
  167.                      BOOT(COLD) ;
  168.                  }
  169.                }
  170.                break ;
  171.     case 's' : for (portbyte=14 ; portbyte<=63 ; portbyte++) /* read cmos data */
  172.                {
  173.                  outportb(0x70,portbyte) ;
  174.                  cmosdata[portbyte-14] = inportb(0x71) ;
  175.                }
  176.                cmosfile = fopen("CMOSROM.DAT","wb") ; /* open cmos data file */
  177.                if ( cmosfile == NULL ) /* error opening file */
  178.                  cputs("\n\r*** Error Opening File CMOSROM.DAT ***\n\r") ;
  179.                else
  180.                {
  181.                  fwrite(&cmosdata[0],sizeof(BYTE),50,cmosfile) ;
  182.                  fclose(cmosfile) ; /* close file */
  183.                  cmosfile = fopen("CMOSROM.DAT","rb") ; /* open cmos file to read */
  184.                  if ( cmosfile == NULL )                /* error opening file */
  185.                  {
  186.                    cputs("\n\r*** Error Reading File CMOSROM.DAT ***\n\r") ;
  187.                    remove("CMOSROM.DAT") ;
  188.                  }
  189.                  else
  190.                  {
  191.                    for (i=0 ; i<50 ; i++)
  192.                    {
  193.                      if ( cmosdata[i] != fgetc(cmosfile) )
  194.                      {
  195.                        cputs("\n\r*** Error Comparing File CMOSROM.DAT and CMOS ROM Data ***\n\r") ;
  196.                        remove("CMOSROM.DAT") ;
  197.                        break ;
  198.                      }
  199.                    }
  200.                    time_date() ;
  201.                    if ( bootflag )
  202.                      BOOT(COLD) ;
  203.                  }
  204.                }
  205.                break ;
  206.     default  : if ( dateflag || timeflag || bootflag )
  207.                {
  208.                  time_date() ;
  209.                  if ( bootflag )
  210.                    BOOT(COLD) ;
  211.                }
  212.                else
  213.                {
  214.                  cputs("\n\r*** Invalid Command Line Parameter(s) ***\n\r") ;
  215.                  cputs("\n\rUsage: ROM [action] [time] [date] [boot]\n\r") ;
  216.                  cputs("  action - s           save CMOS ROM data to file\n\r") ;
  217.                  cputs("           r           restore CMOS ROM data from file\n\r") ;
  218.                  cputs("    time - hh:mm:ss    24 hour time\n\r") ;
  219.                  cputs("    date - mm/dd/yyyy  month/day/year\n\r") ;
  220.                  cputs("    boot - b           cold boot the system after executing program\n\r") ;
  221.                }
  222.                break ;
  223.   }
  224.   fcloseall() ;
  225. }
  226.  
  227.  
  228. /*============================================================================
  229. FUNCTION NAME:  int2bcd
  230. PURPOSE:  Converts an integer to a single byte binary coded decimal (BCD).
  231.           Will truncate any digits more than two.
  232. RETURNS:  unsigned char - BCD conversion of the argument
  233. ARGUMENTS:  int integer - integer to be conveted to BCD
  234. GLOBAL VARIABLES:  None
  235. FUNCTIONS USED:  None
  236. ----------------------------------------------------------------------------*/
  237. BYTE int2bcd ( int integer )
  238. {
  239. BYTE bcd ;
  240.  
  241.   bcd = integer/10 ;
  242.   bcd <<= 4 ;
  243.   bcd += integer%10 ;
  244.   return(bcd) ;
  245. }
  246.  
  247.  
  248. /*============================================================================
  249. FUNCTION NAME:  parse_nbr
  250. PURPOSE:  To convert consecutive ascii digits into an integer
  251. RETURNS:  int = Number of characters converted plus one
  252. ARGUMENTS:  char *string = pointer to the first character of string to convert
  253. GLOBAL VARIABLES:  temp
  254. FUNCTIONS USED:  None
  255. ----------------------------------------------------------------------------*/
  256. int parse_nbr ( char *string )
  257. {
  258. int counter ;
  259.  
  260.   temp = 0 ;
  261.   for ( counter=0 ; 1 ; counter++ )
  262.   {
  263.     if ( string[counter]<'0' || string[counter]>'9' )
  264.       break ;
  265.     else
  266.       temp *= 10 ;
  267.       temp += string[counter]-0x30 ;
  268.   }
  269.   return(counter+1) ;
  270. }
  271.  
  272.  
  273. /*============================================================================
  274. FUNCTION NAME:  time_date
  275. PURPOSE:  To update CMOS ROM and system time and date if needed.
  276. RETURNS:  None
  277. ARGUMENTS:  None
  278. GLOBAL VARIABLES:  action, cent, dateflag, hour, min, mday, mon, sec, timeflag,
  279.                    year
  280. FUNCTIONS USED:  int86, intdos
  281. ----------------------------------------------------------------------------*/
  282. void time_date ( void )
  283. {
  284. struct tm *local ;
  285. time_t bintime ;
  286. union REGS Regs ;
  287.  
  288.   time(&bintime) ; /* get system date and time */
  289.   local = localtime(&bintime) ;
  290.   if ( timeflag ) /* set system time if user supplied */
  291.   {
  292.     Regs.h.ch = hour ;
  293.     Regs.h.cl = min ;
  294.     Regs.h.dh = sec ;
  295.     Regs.h.dl = 0 ;
  296.     Regs.h.ah = 0x2D ;
  297.     intdos(&Regs,&Regs) ;
  298.   }
  299.   else /* use system time to update cmos if not user supplied */
  300.   {
  301.     hour = local->tm_hour ;
  302.     min = local->tm_min ;
  303.     sec = local->tm_sec ;
  304.   }
  305.   if ( timeflag || action=='r' ) /* update time */
  306.   {
  307.     Regs.h.ch = int2bcd(hour) ;
  308.     Regs.h.cl = int2bcd(min) ;
  309.     Regs.h.dh = int2bcd(sec) ;
  310.     Regs.h.dl = 0 ;
  311.     Regs.h.ah = 3 ;
  312.     int86(0x1A,&Regs,&Regs) ;
  313.   }
  314.   if ( dateflag ) /* set system date if user supplied */
  315.   {
  316.     Regs.x.cx = cent*100 + year ;
  317.     Regs.h.dh = mon ;
  318.     Regs.h.dl = mday ;
  319.     Regs.h.ah = 0x2B ;
  320.     intdos(&Regs,&Regs) ;
  321.   }
  322.   else /* use system date to update cmos if not user supplied */
  323.   {
  324.     cent = 19 ;
  325.     year = local->tm_year ;
  326.     mon = local->tm_mon + 1 ;
  327.     mday = local->tm_mday ;
  328.   }
  329.   if ( dateflag || action=='r' ) /* update date */
  330.   {
  331.     Regs.h.ch = int2bcd(cent) ;
  332.     Regs.h.cl = int2bcd(year) ;
  333.     Regs.h.dh = int2bcd(mon) ;
  334.     Regs.h.dl = int2bcd(mday) ;
  335.     Regs.h.ah = 5 ;
  336.     int86(0x1A,&Regs,&Regs) ;
  337.   }
  338. }
  339.