Annotation of sys/arch/amd64/amd64/nvram.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: nvram.c,v 1.1 2007/08/02 16:40:27 deraadt Exp $ */
2:
3: /*
4: * Copyright (c) 2004 Joshua Stein <jcs@openbsd.org>
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 <amd64/isa/nvram.h>
36:
37: /* checksum is calculated over bytes 2 to 31 and stored in byte 32 */
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: /* #define NVRAM_DEBUG 1 */
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: /* TODO: re-calc checksum on every open? */
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: }
CVSweb