CMOS读写

/********************************************************
*程序名称: RWCMOS
*用 途: 读、写CMOS内存信息
*编译方法: 用下列命令编译连接可以得到CMOS.COM:
*tcc -mt cmos
*tlink c:/tc/lib/c0t+cmos,cmos,,c:/tc/lib/cs/lib / 
********************************************************/

#include <stdio.h>
#include <ctype.h>
#include <string.h>

struct hdinfo//硬盘物理参数结构体
{
 unsigned int Cylinder;//硬盘柱体数
 unsigned char Head;//面数
 unsigned int WP;
 unsigned int LZ;
 unsigned char Sector;//扇区数
};

struct TCMOSStruc//CMOS RAM信息结构体,该结构体为网上所查
{
 unsigned char Seconds; //实时钟秒数
 unsigned char SecondAlarm;//秒报警
 unsigned char Minutes; //实时钟分数
 unsigned char MinuteAlarm;//分报警
 unsigned char Hours; //实时钟小时数
 unsigned char HourAlarm;//时报警
 unsigned char DayOfWeek;//周几
 unsigned char DayOfMonth;//号数
 unsigned char Month;//月份
 unsigned char Year;//年份
 unsigned char StatusRegA;//状态寄存器A
 unsigned char StatusRegB;//状态寄存器B
 unsigned char StatusRegC;//状态寄存器C
 unsigned char StatusRegD;//状态寄存器D
 unsigned char DiagStatus;//诊断状态
 unsigned char ShutDownStatus;//关机状态
 struct {
  unsigned FloppyDrive2 : 4;//第二软驱的类型
  unsigned FloppyDrive1 : 4;//第一软驱的类型
 } fd;
 unsigned char Reserved1;//保留
 struct {
  unsigned FixedDrive2 : 4;//第二硬盘的类型
  unsigned FixedDrive1 : 4;//第一硬盘的类型
 } hd;
 unsigned char Reserved2;//保留
 struct {
  unsigned diskdrv : 1;//磁盘驱动器
  unsigned mpu : 1;//数学处理器
  unsigned : 2;//保留
  unsigned videotype : 2;//当前显示类型
  unsigned numdrive : 2;//磁盘驱动器的数目
 }equipment;
 unsigned int RAM;//基本内存KB数
 unsigned int XMS;//扩充内存KB数
 unsigned char FixedDriveType1;//驱动器C扩展字节
 unsigned char FixedDriveType2;//驱动器D扩展字节
 unsigned int Reserved3;//保留
 struct hdinfo hd1;//硬盘C的物理参数
 struct hdinfo hd2;//硬盘D的物理参数
 unsigned char Sys;//系统字节
 unsigned int CheckSum;//校验和
 unsigned int XMS1;//扩展内存KB数
 unsigned char DateCentury;//世纪的BCD值
 unsigned char InfoFlags;//信息标志
 unsigned char Reserved4[4];//保留
 unsigned char password[8];//口令字节
}CMOSRec;

/*****************************************************
*函数名称:GetFileName
*功能描述:获取文件名函数
*参数列表:无
*返回值:返回文件名
******************************************************/
char *GetFileName()
{
 char ch,filename[80];
 printf("/nPlease input the drive name: ");
 while ((ch=getchar())=='/n');
 printf("/n");
 filename[0]=ch;
 filename[1]='/0';
 strcat(filename,"://CMOS.DAT");
 return filename;
}

/*****************************************************
*函数名称:ReadFile
*功能描述:从CMOS.DAT中读出信息
*参数列表:无
*返回值:无
******************************************************/
void ReadFile()
{
 FILE *f1;
 char filename[80];
 strcpy(filename,GetFileName());
 if((f1 = fopen(filename,"rb"))==NULL)
 {
  /* 打开一个二进制文件 */
  printf("File not found !");
 }
 fread(&CMOSRec,sizeof(CMOSRec),1,f1); /* 从文件中读CMOS内存信息 */
 fclose(f1);
}

/*****************************************************
*函数名称:WriteFile
*功能描述:从CMOS内存中读出的信息写入文件
*参数列表:无
*返回值:无
******************************************************/
void WriteFile()
{
 FILE *f1;
 char flnm[80];
 strcpy(flnm,GetFileName());
 if ((f1 = fopen(flnm,"wb"))==NULL)
 {
  /* 建立一个二进制文件 */
  printf("File does not opened  !");
 }
 fwrite(&CMOSRec,sizeof(CMOSRec),1,f1) ; /* 写CMOS内存信息记录到文件 */
 fclose(f1);
}

/*****************************************************
*函数名称:ReadCMOS
*功能描述:从CMOS内存中读信息
*参数列表:无
*返回值:无
******************************************************/
void ReadCMOS()
{
 _asm mov di,offset CMOSRec
 _asm mov cx,0x40
 _asm mov ah,0
 _asm mov bx,0
 _asm mov dx,0x70 /* CMOS口地址 */
 _L1:
 _asm mov al,ah
 _asm out dx,al
 _asm inc dx
 _asm in al,dx
 _asm mov BYTE PTR [di+BX],al
 _asm inc ah
 _asm inc bx
 _asm dec dx
 _asm loop L1
}

/*****************************************************
*函数名称:WriteCMOS
*功能描述:向CMOS内存写入信息
*参数列表:无
*返回值:无
******************************************************/
void WriteCMOS()
{
 _asm mov di,offset CMOSRec
 _asm mov CX,0x40
 _asm mov AH,0
 _asm mov BX,0
 _asm mov DX,0x70
 _L1:
 _asm mov AL,AH
 _asm out DX,AL
 _asm mov AL,BYTE PTR [di+BX]
 _asm inc DX
 _asm out DX,AL
 _asm inc AH
 _asm inc BX
 _asm dec dX
 _asm loop _L1
}

/*****************************************************
*函数名称:DisplayHardDiskInfo
*功能描述:显示硬盘信息
*参数列表:无
*返回值:无
******************************************************/
void DisplayHardDiskInfo(char HDType,hdinfo HDInfoRec,char Order)
{
 printf("/nFixed Drive %d: %d",Order,HDType);
 printf("/n Cylinder : %d",HDInfoRec.Cylinder);
 printf("/n Head : %d",HDInfoRec.Head);
 printf("/n Sector: %d",HDInfoRec.Sector);
 printf("/n LZ: %d",HDInfoRec.LZ);
 printf("/n WP: %d",HDInfoRec.WP);
}

 

/*****************************************************
*函数名称:DisplayCMOS
*功能描述:显示CMOS内存信息
*参数列表:无
*返回值:无
******************************************************/
void DisplayCMOS()
{
 int i;
 printf("/nCMOS RAM information:");
 printf("/nDate(MM-DD-YY): %d%d-%d%d-%d%d",CMOSRec.Month>>4,CMOSRec.Month & 0xf,CMOSRec.DayOfMonth>>4,CMOSRec.DayOfMonth & 0xf,CMOSRec.Year>>4,CMOSRec.Year & 0xf);
 printf("/nTime(HH:MM:SS): %d%d:%d%d:%d%d",CMOSRec.Hours>>4,CMOSRec.Hours & 0xf,
 CMOSRec.Minutes>>4,CMOSRec.Minutes & 0xf,
 CMOSRec.Seconds>>4,CMOSRec.Seconds & 0xf);
 printf("/nConventional Memory: %d KB",CMOSRec.RAM);
 printf("/nExtended Memory: %d KB",CMOSRec.XMS);
 if (CMOSRec.hd.FixedDrive1 != 0)
 {
  DisplayHardDiskInfo(CMOSRec.FixedDriveType1,CMOSRec.hd1,1);
 }
 if (CMOSRec.hd.FixedDrive2 != 0)
 {
  DisplayHardDiskInfo(CMOSRec.FixedDriveType2,CMOSRec.hd2,2);
 }
 printf("/nScan  code of password:");
 for (i=0;i<8;++i)
 {
  printf(" %d",CMOSRec.password);
 }
 printf("/nThe number of disk drives : %d",CMOSRec.equipment.numdrive);
 printf("/nVideo Type : %d",CMOSRec.equipment.videotype);
 printf("/nMath co-processor present :");

 if(CMOSRec.equipment.mpu==1)
 {
  printf("YES");
 }
 else
 {
  printf("NO");
 }
 printf("/nDisk drives : %d",CMOSRec.equipment.diskdrv);
}

/*****************************************************
*函数名称:help
*功能描述:显示帮助信息
*返回值:无
******************************************************/
void help()
{
 printf(" CMOS /R --- read information from CMOS RAM and write it to CMOS.DAT file./n");
 printf(" CMOS /W --- read configuration information from CMOS.DAT and write it to CMOS RAM/n");
}

/*****************************************************
*函数名称:main
*功能描述:主函数
*返回值:无
******************************************************/
void main(char *argc[],int argn)
{
 char ch;
 char *temp;
 if(argn == 2)
 {
  temp = argc[1];
  ch = toupper(temp[1]);
  switch(ch)
  {
  case 'R':
   ReadCMOS();//读CMOS内存信息,并写入文件
   DisplayCMOS();//显示CMOS内存信息
   WriteFile();//写入文件
   break;
  case'W':
   ReadFile();//从文件中读CMOS信息,并写入CMOS内存中
   DisplayCMOS();//显示CMOS内存信息
   WriteCMOS();//写入CMOS
   DisplayCMOS();//显示CMOS内存信息
  default:
   help();
  }
 }
 else
  help();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值