xref: /freebsd/sys/dev/nvram/nvram.c (revision 35a04710d7286aa9538917fd7f8e417dbee95b82)
1 /*-
2  * Copyright (c) 2007 Peter Wemm
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/conf.h>
33 #include <sys/fcntl.h>
34 #include <sys/proc.h>
35 #include <sys/uio.h>
36 #include <sys/module.h>
37 
38 #include <machine/clock.h>
39 #include <isa/rtc.h>
40 
41 /*
42  * Linux-style /dev/nvram driver
43  *
44  * cmos ram starts at bytes 14 through 128, for a total of 114 bytes.
45  * The driver exposes byte 14 as file offset 0.
46  *
47  * Offsets 2 through 31 are checksummed at offset 32, 33.
48  * In order to avoid the possibility of making the machine unbootable at the
49  * bios level (press F1 to continue!), we refuse to allow writes if we do
50  * not see a pre-existing valid checksum.  If the existing sum is invalid,
51  * then presumably we do not know how to make a sum that the bios will accept.
52  */
53 
54 #define NVRAM_FIRST	RTC_DIAG	/* 14 */
55 #define NVRAM_LAST	128
56 
57 #define CKSUM_FIRST	2
58 #define CKSUM_LAST	31
59 #define CKSUM_MSB	32
60 #define CKSUM_LSB	33
61 
62 static d_open_t		nvram_open;
63 static d_read_t		nvram_read;
64 static d_write_t	nvram_write;
65 
66 static struct cdev *nvram_dev;
67 
68 static struct cdevsw nvram_cdevsw = {
69 	.d_version =	D_VERSION,
70 	.d_flags =	D_NEEDGIANT,
71 	.d_open =	nvram_open,
72 	.d_read =	nvram_read,
73 	.d_write =	nvram_write,
74 	.d_name =	"nvram",
75 };
76 
77 static int
78 nvram_open(struct cdev *dev __unused, int flags, int fmt __unused,
79     struct thread *td)
80 {
81 	int error = 0;
82 
83 	if (flags & FWRITE)
84 		error = securelevel_gt(td->td_ucred, 0);
85 
86 	return (error);
87 }
88 
89 static int
90 nvram_read(struct cdev *dev, struct uio *uio, int flags)
91 {
92 	int nv_off;
93 	u_char v;
94 	int error = 0;
95 
96 	while (uio->uio_resid > 0 && error == 0) {
97 		nv_off = uio->uio_offset + NVRAM_FIRST;
98 		if (nv_off < NVRAM_FIRST || nv_off >= NVRAM_LAST)
99 			return (0);	/* Signal EOF */
100 		/* Single byte at a time */
101 		v = rtcin(nv_off);
102 		error = uiomove(&v, 1, uio);
103 	}
104 	return (error);
105 
106 }
107 
108 static int
109 nvram_write(struct cdev *dev, struct uio *uio, int flags)
110 {
111 	int nv_off;
112 	u_char v;
113 	int error = 0;
114 	int i;
115 	uint16_t sum;
116 
117 	/* Assert that we understand the existing checksum first!  */
118 	sum = rtcin(NVRAM_FIRST + CKSUM_MSB) << 8 |
119 	      rtcin(NVRAM_FIRST + CKSUM_LSB);
120 	for (i = CKSUM_FIRST; i <= CKSUM_LAST; i++)
121 		sum -= rtcin(NVRAM_FIRST + i);
122 	if (sum != 0)
123 		return (EIO);
124 	/* Bring in user data and write */
125 	while (uio->uio_resid > 0 && error == 0) {
126 		nv_off = uio->uio_offset + NVRAM_FIRST;
127 		if (nv_off < NVRAM_FIRST || nv_off >= NVRAM_LAST)
128 			return (0);	/* Signal EOF */
129 		/* Single byte at a time */
130 		error = uiomove(&v, 1, uio);
131 		writertc(nv_off, v);
132 	}
133 	/* Recalculate checksum afterwards */
134 	sum = 0;
135 	for (i = CKSUM_FIRST; i <= CKSUM_LAST; i++)
136 		sum += rtcin(NVRAM_FIRST + i);
137 	writertc(NVRAM_FIRST + CKSUM_MSB, sum >> 8);
138 	writertc(NVRAM_FIRST + CKSUM_LSB, sum);
139 	return (error);
140 }
141 
142 static int
143 nvram_modevent(module_t mod __unused, int type, void *data __unused)
144 {
145 	switch (type) {
146 	case MOD_LOAD:
147 		nvram_dev = make_dev(&nvram_cdevsw, 0,
148 		    UID_ROOT, GID_KMEM, 0640, "nvram");
149 		break;
150 	case MOD_UNLOAD:
151 	case MOD_SHUTDOWN:
152 		destroy_dev(nvram_dev);
153 		break;
154 	default:
155 		return (EOPNOTSUPP);
156 	}
157 	return (0);
158 }
159 DEV_MODULE(nvram, nvram_modevent, NULL);
160