This source file includes following definitions.
- nvramattach
- nvramopen
- nvramclose
- nvramread
- nvram_get_byte
- nvram_csum_valid
    1 
    2 
    3 
    4 
    5 
    6 
    7 
    8 
    9 
   10 
   11 
   12 
   13 
   14 
   15 
   16 
   17 
   18 
   19 
   20 
   21 
   22 
   23 
   24 
   25 
   26 
   27 
   28 #include <sys/param.h>
   29 #include <sys/systm.h>
   30 #include <sys/uio.h>
   31 #include <sys/fcntl.h>
   32 #include <sys/conf.h>
   33 
   34 #include <dev/ic/mc146818reg.h>
   35 #include <i386/isa/nvram.h>
   36 
   37 
   38 #define NVRAM_CSUM_START        (MC_NVRAM_START + 2)
   39 #define NVRAM_CSUM_END          (MC_NVRAM_START + 31)
   40 #define NVRAM_CSUM_LOC          (MC_NVRAM_START + 32)
   41 
   42 #define NVRAM_SIZE              (128 - MC_NVRAM_START)
   43 
   44 
   45 
   46 void nvramattach(int);
   47 
   48 int nvramopen(dev_t dev, int flag, int mode, struct proc *p);
   49 int nvramclose(dev_t dev, int flag, int mode, struct proc *p);
   50 int nvramread(dev_t dev, struct uio *uio, int flags);
   51 
   52 int nvram_csum_valid(void);
   53 int nvram_get_byte(int byteno);
   54 
   55 static int nvram_initialized;
   56 
   57 void
   58 nvramattach(int num)
   59 {
   60         if (num > 1)
   61                 return;
   62 
   63         if (nvram_initialized || nvram_csum_valid()) {
   64 #ifdef NVRAM_DEBUG
   65                 printf("nvram: initialized\n");
   66 #endif
   67                 nvram_initialized = 1;
   68         } else
   69                 printf("nvram: invalid checksum\n");
   70 }
   71 
   72 int
   73 nvramopen(dev_t dev, int flag, int mode, struct proc *p)
   74 {
   75         
   76 
   77         if ((minor(dev) != 0) || (!nvram_initialized))
   78                 return (ENXIO);
   79 
   80         if ((flag & FWRITE))
   81                 return (EPERM);
   82 
   83         return (0);
   84 }
   85 
   86 int
   87 nvramclose(dev_t dev, int flag, int mode, struct proc *p)
   88 {
   89         return (0);
   90 }
   91 
   92 int
   93 nvramread(dev_t dev, struct uio *uio, int flags)
   94 {
   95         u_char buf[NVRAM_SIZE];
   96         u_int pos = uio->uio_offset;
   97         u_char *tmp;
   98         int count = min(sizeof(buf), uio->uio_resid);
   99         int ret;
  100 
  101         if (!nvram_initialized)
  102                 return (ENXIO);
  103 
  104         if (uio->uio_resid == 0)
  105                 return (0);
  106 
  107 #ifdef NVRAM_DEBUG
  108         printf("attempting to read %d bytes at offset %d\n", count, pos);
  109 #endif
  110 
  111         for (tmp = buf; count-- > 0 && pos < NVRAM_SIZE; ++pos, ++tmp)
  112                 *tmp = nvram_get_byte(pos);
  113 
  114 #ifdef NVRAM_DEBUG
  115         printf("nvramread read %d bytes (%s)\n", (tmp - buf), tmp);
  116 #endif
  117 
  118         ret = uiomove((caddr_t)buf, (tmp - buf), uio);
  119 
  120         uio->uio_offset += uio->uio_resid;
  121 
  122         return (ret);
  123 }
  124 
  125 int
  126 nvram_get_byte(int byteno)
  127 {
  128         if (!nvram_initialized)
  129                 return (ENXIO);
  130 
  131         return (mc146818_read(NULL, byteno + MC_NVRAM_START) & 0xff);
  132 }
  133 
  134 int
  135 nvram_csum_valid()
  136 {
  137         u_short csum = 0;
  138         u_short csumexpect;
  139         int nreg;
  140 
  141         for (nreg = NVRAM_CSUM_START; nreg <= NVRAM_CSUM_END; nreg++)
  142                 csum += mc146818_read(NULL, nreg);
  143 
  144         csumexpect = mc146818_read(NULL, NVRAM_CSUM_LOC) << 8 |
  145             mc146818_read(NULL, NVRAM_CSUM_LOC + 1);
  146 
  147 #ifdef NVRAM_DEBUG
  148         printf("nvram: checksum is %x, expecting %x\n", (csum & 0xffff),
  149                 csumexpect);
  150 #endif
  151 
  152         return ((csum & 0xffff) == csumexpect);
  153 }