⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmostool.c

📁 在dos 与Linux下通过命令来改变BIOS项设置。
💻 C
📖 第 1 页 / 共 2 页
字号:
{
  
        unsigned int crcvalue;

        crcvalue=coreUpdateCRC((unsigned char)tokenValue);
#ifdef DEBUG1
        printf("start=%0x   --value=%0x  crcvalue=%0x\n", Tokenitem.pos,(unsigned char)tokenValue,crcvalue);
#endif

        {

                crcvalue=coreUpdateCRC((unsigned char)(tokenValue>>8));
#ifdef DEBUG1
                printf("start=%0x   --value=%0x  crcvalue=%0x\n",Tokenitem.pos, tokenValue>>8,crcvalue);
#endif
        }          
        return crcvalue;
};


unsigned short GetCRC(struct CMOSStruct cmomstruct,unsigned char* DefaultCmos )
/*
 * RetVal: return CRC value
 */
{
        unsigned short retCRC;
        int i=0;
        struct CMOSStruct Tokentable=cmosstruct;
        unsigned media=0;

                for(i=0;i<Tokentable.tokenItemCount;i++)
                {
                                
                        if(Tokentable.cmostokenstruct[i].tokensum)
                        {
                                if(Tokentable.cmostokenstruct[i].bytestring)
                                {
                                         retCRC = UpdateCorefromStrToken(Tokentable.cmostokenstruct[i], DefaultCmos);
                                }
                                else
                                {                               //Compute Stdtoken CRC
                                         retCRC = UpdateCorefromStdToken(Tokentable.cmostokenstruct[i],Tokentable.tokenValue[i]);
                                }
                        }
               }        
        return retCRC;
};

/*
 * End CRC
 */


/*
 * Begin CMOS READ/WRITE
 */

#define low_cmos_addr_port 0x70
#define low_cmos_data_port 0x71
#define high_cmos_addr_port 0x72
#define high_cmos_data_port 0x73
#define extend_cmos_addr_port 0x74
#define extend_cmos_data_port 0x75

typedef unsigned char   BYTE;
typedef unsigned short  WORD;

BYTE Read_CMOS(BYTE addr, WORD offset)
{
        BYTE value = 0;
        BYTE data = addr + 1;
        outp(addr,offset);
        value = (unsigned char)inp(data);
        return value;
};

BYTE Write_CMOS(BYTE addr, WORD offset, BYTE value)
{

        BYTE data = addr + 1;

        outp(addr,offset);
        //delay(3);
        outp(data,value);
       // delay(3);
        return 0;
};

#pragma pack(1)
struct data_time
{
     unsigned char  cur_sec;
     unsigned char  ala_sec;
     unsigned char  cur_min;
     unsigned char  ala_sec1;
     unsigned char  cur_hour;
     unsigned char  ala_hour1;
     unsigned char  cur_day_week;
     unsigned char   cur_day;
     unsigned char   cur_mon;
     unsigned char   cur_year;
 
};

#pragma pack()
short  ReadDate(char * date)
{

      struct data_time datetime;
      unsigned  char showdatetime[20];
      unsigned char  cmosdata[10];
      unsigned short  i;

      memset(showdatetime,'\0',20);
      for(i=0;i<10;i++)
      {
            cmosdata[i]= Read_CMOS(0x70, i);
       }
      
      //strncpy(((unsigned char*)&datetime),cmosdata,10);
    sprintf(showdatetime,"%x-%x-%d\n",cmosdata[8],cmosdata[7],cmosdata[9]+2000);                          
    //  sprintf(showdatetime,"%d-%d-%d  \0",datetime.cur_mon,
//                                                                                      datetime.cur_day,
        //                                                                              datetime.cur_year+2000);
       strncpy(date,showdatetime,strlen(showdatetime));         
    
       
       return 0;       
}


short  ReadTime(char * time)
{

      struct data_time datetime;
      unsigned char showdatetime[20];
      unsigned char  cmosdata[10];
      unsigned short i;

      memset(showdatetime,'\0',20);
      for(i=0;i<10;i++)
      {
            cmosdata[i]= Read_CMOS(0x70, i);
            
      }
      
      //strncpy(((unsigned char*)&datetim/),cmosdata,10);
      
      sprintf(showdatetime,"%x:%x:%x\n",cmosdata[4],cmosdata[2],cmosdata[0]);                   

      /*datetime.cur_hour,
                                                                                        datetime.cur_min,
                                                                                        datetime.cur_sec);*/
       strncpy(time,showdatetime,strlen(showdatetime));         
       
       return 0;       
}



 short  WriteDate(unsigned char* date)
{
      char *cmdline="date=";
      int retVal=0;

      strcat(cmdline,date);
         //printf("%s\n",cmdline);
      retVal=system(cmdline);
      return retVal;
};

 short  WriteTime(unsigned char* time)
{
      char *cmdline="time=";
      int retVal=0;

      strcat(cmdline,time);
      //printf("%s\n",cmdline);
      retVal=system(cmdline);
      return retVal;
};     


void readCMOS(unsigned char* CMOS)
{
      unsigned short i=0;
      for(i=0;i<512;i++)
      {
         BYTE port;
         unsigned index=0;
         port = i<0x80?low_cmos_addr_port:high_cmos_addr_port;
         index= i<0x80?i:i;
                
         CMOS[i] = Read_CMOS(port, index);  
#ifdef DEBUG1
        if(i%16==0)
                printf("%02x: ",i);

        printf(" %02x",CMOS[i]);


        if((i+1)%16==0)
                printf("\n",i);
#endif

      }         
}


void writeCMOS(unsigned char* CMOS)
{
      unsigned short i=0;
//#ifdef DEBUG
//            printf("Function writeCMOS\n");
//#endif

      for(i=14;i<512;i++)
      {
        
         BYTE port;
         unsigned index=0;
         port = i<0x80?low_cmos_addr_port:high_cmos_addr_port;
         index= i<0x80?i:i;
                
         Write_CMOS(port, index, CMOS[i]);  
//#ifdef DEBUG1
  /*      if(i%16==0)
                printf("%02x: ",i);

        printf(" %02x",CMOS[i]);


        if((i+1)%16==0)
                printf("\n",i);
//#endif
*/
      }      
}



unsigned short getChecksum(unsigned char* cmosValue)
{
#define CMOS_CHKSUM_START       0x10    
#define CMOS_CHKSUM_END         0x02D           
#define CMOS_CHKSUM_LO          0x02E
#define CMOS_CHKSUM_HI          0x02F

unsigned short i=0;
unsigned short computeChecksum=0;
unsigned short readchecksum=0;
for(i=CMOS_CHKSUM_START; i<CMOS_CHKSUM_END+1;i++)
{
        computeChecksum += cmosValue[i];
}

readchecksum=cmosValue[0x2f];
readchecksum+=((unsigned short)cmosValue[0x2e])<<8;
//printf("Compute CHECK_SUM  =%x\n Read CHECK_SUM=%x\n", computeChecksum,readchecksum);

return computeChecksum;
}

unsigned short GetEarlyCheckSum(struct CMOSStruct cmomstruct,unsigned char* DefaultCmos )
/*
 * RetVal: return early check value
 */
{
        unsigned short checkSum=0xa55a;
        int i=0;
        struct CMOSStruct Tokentable=cmosstruct;
        unsigned early_pos=0;

                early_pos = Tokentable.earlysum_position;
                for(i=0;i<Tokentable.tokenItemCount;i++)
                {

                        if(i==early_pos)
                        {
                                i++;
                                continue;
                        }

                        if( Tokentable.cmostokenstruct[i].early )
                        {

                                 if(Tokentable.cmostokenstruct[i].bytestring)
                                 {
                                        unsigned  short Startbybyte = (Tokentable.cmostokenstruct[i].pos)/ 8;
                                        unsigned  short len  = Tokentable.cmostokenstruct[i].len;
                                        unsigned  short index;
                                        for(index=0;index<len;index++)
                                        {
                                                 unsigned char buf = DefaultCmos[Startbybyte+index];
                                                 checkSum+=buf;
                                         }

                                        continue;
                                 }


                                checkSum+=Tokentable.tokenValue[i];
                        }
               }

#ifdef DEBUG
                printf("Early CheckSum Read=%x %x  Compute=%x \n",(unsigned short)Tokentable.tokenValue[early_pos],Tokentable.tokenValue[early_pos+1],checkSum);
#endif

        return checkSum;
};

unsigned char cmos[512]; 

void __initCMOS()
{
            fill_flash( );   
            fill_bcpNV_NVT_Stuct();
            fill_TokenTable();      
            fillcmosTokenStruct();
            readCMOS(cmos);
            fillcmosTokenValue(&cmosstruct,cmos);
            crc=bcpnvstruc.cmosCRCSignature;
        //    printf("end inicmos\n");
}   

void __exitCMOS()
{
            unsigned char earlyCheckSum;
            unsigned short  checkSum;
            unsigned short   crcValue;
            unsigned short   earlysum_pos,checksum_pos_h,checksum_pos_l, crc_pos;

/*
 *   compute earlysum and fill cmos
 */
            earlysum_pos = cmosstruct.earlysum_position;
            earlyCheckSum =(unsigned char)GetEarlyCheckSum(cmosstruct,cmos) ;
         //   printf("Exit EARLYksum=%x\n",earlyCheckSum);
            cmosstruct.tokenValue[earlysum_pos] = earlyCheckSum;
            setSTDtokenValue(cmosstruct.cmostokenstruct[earlysum_pos],cmos,earlyCheckSum);

/*
 *   compute checksum and fill cmos
 */
            checksum_pos_h = cmosstruct.checksum_position;
            checksum_pos_l = cmosstruct.checksum_position+1;
            checkSum  =  getChecksum(cmos);
            //printf("Exit checksum=%x\n",checkSum);
            cmosstruct.tokenValue[checksum_pos_h] = checkSum>>8;
            cmosstruct.tokenValue[checksum_pos_l] = checkSum&0x00ff;
            setSTDtokenValue(cmosstruct.cmostokenstruct[checksum_pos_h],cmos,checkSum>>8);
            setSTDtokenValue(cmosstruct.cmostokenstruct[checksum_pos_l],cmos,checkSum);

/*
 *   compute CRC  and fill cmos
 */
            crc=bcpnvstruc.cmosCRCSignature;
            crc_pos = cmosstruct.crc_position;
 //          checkSum  =  getChecksum(cmos);
            crcValue =  GetCRC(cmosstruct,cmos);
            //printf("crc value: %x\n", crcValue);
            setSTDtokenValue(cmosstruct.cmostokenstruct[crc_pos],cmos,crcValue);
/*
 *  updateCMOS
 */

            writeCMOS(cmos);
//printf("end writecmos\n");
}

unsigned short  getTokenItemIndex(unsigned short  start_pos,unsigned short  media)
{
          unsigned short i; 
          unsigned retVal=0xFFFF;
          for(i=0; i<tokentable.itemCount;i++)
          {

                    if(tokentable.tokenItem[i].token.start_pos == start_pos && (tokentable.tokenItem[i].token.flag&0x7) == media)
                                        retVal = i;
          }

            return retVal;
}
                    
unsigned short  getTokenItemValue(unsigned short  index)
{
          unsigned short i; 
          unsigned retVal=0xFFFF;

           if ( index > tokentable.itemCount )
           {
                    printf("index %u is not the range\n",index);
                    return retVal;
           }    
           
           return  cmosstruct.tokenValue[index];
}

unsigned short  setTokenItemValue(unsigned short  index, unsigned short  tokenValue)
{
             cmosstruct.tokenValue[index]=tokenValue;
#ifdef DEBUG
		printf("index=%d tokenValue=%x\n",cmosstruct.tokenValue[index]);
#endif
             setSTDtokenValue(cmosstruct.cmostokenstruct[index],cmos,tokenValue);
              if(cmosstruct.cmostokenstruct[index].pos==0x84)
             {
                  cmosstruct.tokenValue[index-2]=getSTDtokenValue(cmosstruct.cmostokenstruct[index-2],cmos);
             }
}    

#ifdef  MAIN
int main(int argc, char** argv)
{
        unsigned short CRCValue;
        struct  nvtokenIndex nvTokenindex[TokenItemMax];

        int i=0;
       

        fill_flash();   
        fill_bcpNV_NVT_Stuct();
        fill_TokenTable();      
        fillcmosTokenStruct();
        readCMOS(cmos);

        fillcmosTokenValue(&cmosstruct,cmos);
#ifdef DEBUG
        printf("Read CRC value=%x\n",cmosstruct.tokenValue[cmosstruct.crc_position]);
#endif
        getChecksum(cmos);

        GetEarlyCheckSum(cmosstruct,cmos);    
/*
 *  Intialize the crc value
 */
        crc=bcpnvstruc.cmosCRCSignature;
        //printf("CRCSignature is %x\n",crc);
        CRCValue = GetCRC(cmosstruct,cmos);
      /*  for(i=0;i<8;i++)
        {
                printf("ipl%d  value=%x\n",i,cmosstruct.tokenValue[cmosstruct.ipl_position+i]);
        }*/    

#ifdef DEBUG
        printf("CRCValue=%04x\n",CRCValue);
#endif
        return 0;
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -