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

📄 cmos.c

📁 Util-linux 软件包包含许多工具。其中比较重要的是加载、卸载、格式化、分区和管理硬盘驱动器
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * i386 CMOS starts out with 14 bytes clock data * alpha has something similar, but with details * depending on the machine type. * * byte 0: seconds (0-59) * byte 2: minutes (0-59) * byte 4: hours (0-23 in 24hr mode, *                1-12 in 12hr mode, with high bit unset/set if am/pm) * byte 6: weekday (1-7, Sunday=1) * byte 7: day of the month (1-31) * byte 8: month (1-12) * byte 9: year (0-99) * Numbers are stored in BCD/binary if bit 2 of byte 11 is unset/set * The clock is in 12hr/24hr mode if bit 1 of byte 11 is unset/set * The clock is undefined (being updated) if bit 7 of byte 10 is set. * The clock is frozen (to be updated) by setting bit 7 of byte 11 * Bit 7 of byte 14 indicates whether the CMOS clock is reliable: * it is 1 if RTC power has been good since this bit was last read; * it is 0 when the battery is dead and system power has been off. * * Avoid setting the RTC clock within 2 seconds of the day rollover * that starts a new month or enters daylight saving time. * * The century situation is messy: * Usually byte 50 (0x32) gives the century (in BCD, so 19 or 20 hex), * but IBM PS/2 has (part of) a checksum there and uses byte 55 (0x37). * Sometimes byte 127 (0x7f) or Bank 1, byte 0x48 gives the century. * The original RTC will not access any century byte; some modern * versions will. If a modern RTC or BIOS increments the century byte * it may go from 0x19 to 0x20, but in some buggy cases 0x1a is produced. *//* * A struct tm has int fields *   tm_sec (0-59, 60 or 61 only for leap seconds) *   tm_min (0-59) *   tm_hour (0-23) *   tm_mday (1-31) *   tm_mon (0-11) *   tm_year (number of years since 1900) *   tm_wday (0-6, 0=Sunday) *   tm_yday (0-365) *   tm_isdst (>0: yes, 0: no, <0: unknown) */#include <unistd.h>		/* for geteuid() */#include <fcntl.h>		/* for O_RDWR */#include "../defines.h"		/* for HAVE_sys_io_h */#include "nls.h"#if defined(__i386__)#ifdef HAVE_sys_io_h#include <sys/io.h>#else#include <asm/io.h>		/* for inb, outb */#endif#elif defined(__alpha__)/* <asm/io.h> fails to compile, probably because of u8 etc */extern unsigned int     inb(unsigned long port);extern void             outb(unsigned char b,unsigned long port);#elsevoid outb(int a, int b){}int inb(int c){ return 0; }#endif#include "clock.h"#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10)#define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)/* * The epoch. * * Unix uses 1900 as epoch for a struct tm, and 1970 for a time_t. * But what was written to CMOS? * Digital DECstations use 1928 - this is on a mips or alpha * Digital Unix uses 1952, e.g. on AXPpxi33 * Windows NT uses 1980. * The ARC console expects to boot Windows NT and uses 1980. * (But a Ruffian uses 1900, just like SRM.) * It is reported that ALPHA_PRE_V1_2_SRM_CONSOLE uses 1958. */#define TM_EPOCH 1900int cmos_epoch = 1900;/* Martin Ostermann writes: The problem with the Jensen is twofold: First, it has the clock at adifferent address. Secondly, it has a distinction beween "local" andnormal bus addresses. The local ones pertain to the hardware integratedinto the chipset, like serial/parallel ports and of course, the RTC.Those need to be addressed differently. This is handled fine in the kernel,and it's not a problem, since this usually gets totally optimized by thecompile. But the i/o routines of (g)libc lack this support so far.The result of this is, that the old clock program worked only on theJensen when USE_DEV_PORT was defined, but not with the normal inb/outbfunctions. */int use_dev_port = 0;		/* 1 for Jensen */int dev_port_fd;unsigned short clock_ctl_addr = 0x70;	/* 0x170 for Jensen */unsigned short clock_data_addr = 0x71; 	/* 0x171 for Jensen */int century_byte = 0;		/* 0: don't access a century byte				  50 (0x32): usual PC value				  55 (0x37): PS/2 */#ifdef __alpha__int funkyTOY = 0;		/* 1 for PC164/LX164/SX164 type alpha */#endif#ifdef __alphastatic intis_in_cpuinfo(char *fmt, char *str){    FILE *cpuinfo;    char field[256];    char format[256];    int found = 0;    sprintf(format, "%s : %s", fmt, "%255s");    if ((cpuinfo = fopen ("/proc/cpuinfo", "r")) != NULL) {	while (!feof(cpuinfo)) {	    if (fscanf (cpuinfo, format, field) == 1) {		if (strncmp(field, str, strlen(str)) == 0)		    found = 1;		break;	    }	    fgets (field, 256, cpuinfo);	}	fclose(cpuinfo);    }    return found;}/* Set cmos_epoch, either from user options, or by asking the kernel,   or by looking at /proc/cpu_info */voidset_cmos_epoch(int ARCconsole, int SRM) {  unsigned long epoch;  /* Believe the user */  if (epoch_option != -1) {    cmos_epoch = epoch_option;    return;  }  if (ARCconsole)    cmos_epoch = 1980;  if (ARCconsole || SRM)    return;  /* If we can ask the kernel, we don't need guessing from /proc/cpuinfo */  if (get_epoch_rtc(&epoch, 1) == 0) {     cmos_epoch = epoch;     return;  }  /* The kernel source today says: read the year.     If it is in 0-19 then the epoch is 2000.     If it is in 20-47 then the epoch is 1980.     If it is in 48-69 then the epoch is 1952.     If it is in 70-99 then the epoch is 1928.     Otherwise the epoch is 1900.     Clearly, this must be changed before 2019. */  /* See whether we are dealing with SRM or MILO, as they have     different "epoch" ideas. */  if (is_in_cpuinfo("system serial number", "MILO")) {      ARCconsole = 1;      if (debug) printf (_("booted from MILO\n"));  }  /* See whether we are dealing with a RUFFIAN aka Alpha PC-164 UX (or BX),     as they have REALLY different TOY (TimeOfYear) format: BCD, and not     an ARC-style epoch.     BCD is detected dynamically, but we must NOT adjust like ARC. */  if (ARCconsole && is_in_cpuinfo("system type", "Ruffian")) {    ARCconsole = 0;    if (debug) printf (_("Ruffian BCD clock\n"));  }  if (ARCconsole)    cmos_epoch = 1980;}voidset_cmos_access(int Jensen, int funky_toy) {  /* See whether we're dealing with a Jensen---it has a weird I/O     system.  DEC was just learning how to build Alpha PCs.  */  if (Jensen || is_in_cpuinfo("system type", "Jensen")) {    use_dev_port = 1;    clock_ctl_addr = 0x170;    clock_data_addr = 0x171;    if (debug) printf (_("clockport adjusted to 0x%x\n"), clock_ctl_addr);  }  /* see whether we are dealing with PC164/LX164/SX164, as they have a TOY     that must be accessed differently to work correctly. */  /* Nautilus stuff reported by Neoklis Kyriazis */  if (funky_toy ||      is_in_cpuinfo("system variation", "PC164") ||      is_in_cpuinfo("system variation", "LX164") ||      is_in_cpuinfo("system variation", "SX164") ||      is_in_cpuinfo("system type", "Nautilus")) {      funkyTOY = 1;      if (debug) printf (_("funky TOY!\n"));  }}#endif#ifdef __i386__/* * Try to do CMOS access atomically, so that no other processes * can get a time slice while we are reading or setting the clock. * (Also, if the kernel time is synchronized with an external source, *  the kernel itself will fiddle with the RTC every 11 minutes.) */static unsigned longatomic(const char *name, unsigned long (*op)(unsigned long),       unsigned long arg){  unsigned long v;  __asm__ volatile ("cli");  v = (*op)(arg);  __asm__ volatile ("sti");  return v;}#elif __alpha__/* * The Alpha doesn't allow user-level code to disable interrupts (for * good reasons).  Instead, we ensure atomic operation by performing * the operation and checking whether the high 32 bits of the cycle * counter changed.  If they did, a context switch must have occurred * and we redo the operation.  As long as the operation is reasonably * short, it will complete atomically, eventually. */static unsigned longatomic(const char *name, unsigned long (*op)(unsigned long),       unsigned long arg){  unsigned long ts1, ts2, n, v;  for (n = 0; n < 1000; ++n) {    asm volatile ("rpcc %0" : "r="(ts1));    v = (*op)(arg);    asm volatile ("rpcc %0" : "r="(ts2));    if ((ts1 ^ ts2) >> 32 == 0) {      return v;    }  }  fprintf(stderr, _("%s: atomic %s failed for 1000 iterations!"), progname, name);  exit(1);}#else/* * Hmmh, this isn't very atomic.  Maybe we should force an error * instead? */static unsigned longatomic(const char *name, unsigned long (*op)(unsigned long),       unsigned long arg){    return (*op)(arg);}#endifstatic inlineunsigned long cmos_read(unsigned long reg){  if (use_dev_port) {    unsigned char v = reg | 0x80;    lseek(dev_port_fd, clock_ctl_addr, 0);    write(dev_port_fd, &v, 1);    lseek(dev_port_fd, clock_data_addr, 0);    read(dev_port_fd, &v, 1);    return v;  } else {    /* We only want to read CMOS data, but unfortunately       writing to bit 7 disables (1) or enables (0) NMI;       since this bit is read-only we have to guess the old status.       Various docs suggest that one should disable NMI while       reading/writing CMOS data, and enable it again afterwards.       This would yield the sequence	  outb (reg | 0x80, 0x70);	  val = inb(0x71);	  outb (0x0d, 0x70);	// 0x0d: random read-only location       Other docs state that "any write to 0x70 should be followed       by an action to 0x71 or the RTC wil be left in an unknown state".       Most docs say that it doesnt matter at all what one does.     */    /* bit 0x80: disable NMI while reading - should we?       Let us follow the kernel and not disable.       Called only with 0 <= reg < 128 */    outb (reg, clock_ctl_addr);    return inb (clock_data_addr);  }

⌨️ 快捷键说明

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