]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/nvram/nvram.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / nvram / nvram.c
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/lock.h>
35 #include <sys/proc.h>
36 #include <sys/sx.h>
37 #include <sys/uio.h>
38 #include <sys/module.h>
39
40 #include <machine/clock.h>
41 #include <isa/rtc.h>
42
43 /*
44  * Linux-style /dev/nvram driver
45  *
46  * cmos ram starts at bytes 14 through 128, for a total of 114 bytes.
47  * The driver exposes byte 14 as file offset 0.
48  *
49  * Offsets 2 through 31 are checksummed at offset 32, 33.
50  * In order to avoid the possibility of making the machine unbootable at the
51  * bios level (press F1 to continue!), we refuse to allow writes if we do
52  * not see a pre-existing valid checksum.  If the existing sum is invalid,
53  * then presumably we do not know how to make a sum that the bios will accept.
54  */
55
56 #define NVRAM_FIRST     RTC_DIAG        /* 14 */
57 #define NVRAM_LAST      128
58
59 #define CKSUM_FIRST     2
60 #define CKSUM_LAST      31
61 #define CKSUM_MSB       32
62 #define CKSUM_LSB       33
63
64 static d_open_t         nvram_open;
65 static d_read_t         nvram_read;
66 static d_write_t        nvram_write;
67
68 static struct cdev *nvram_dev;
69 static struct sx nvram_lock;
70
71 static struct cdevsw nvram_cdevsw = {
72         .d_version =    D_VERSION,
73         .d_open =       nvram_open,
74         .d_read =       nvram_read,
75         .d_write =      nvram_write,
76         .d_name =       "nvram",
77 };
78
79 static int
80 nvram_open(struct cdev *dev __unused, int flags, int fmt __unused,
81     struct thread *td)
82 {
83         int error = 0;
84
85         if (flags & FWRITE)
86                 error = securelevel_gt(td->td_ucred, 0);
87
88         return (error);
89 }
90
91 static int
92 nvram_read(struct cdev *dev, struct uio *uio, int flags)
93 {
94         int nv_off;
95         u_char v;
96         int error = 0;
97
98         while (uio->uio_resid > 0 && error == 0) {
99                 nv_off = uio->uio_offset + NVRAM_FIRST;
100                 if (nv_off < NVRAM_FIRST || nv_off >= NVRAM_LAST)
101                         return (0);     /* Signal EOF */
102                 /* Single byte at a time */
103                 v = rtcin(nv_off);
104                 error = uiomove(&v, 1, uio);
105         }
106         return (error);
107
108 }
109
110 static int
111 nvram_write(struct cdev *dev, struct uio *uio, int flags)
112 {
113         int nv_off;
114         u_char v;
115         int error = 0;
116         int i;
117         uint16_t sum;
118
119         sx_xlock(&nvram_lock);
120
121         /* Assert that we understand the existing checksum first!  */
122         sum = rtcin(NVRAM_FIRST + CKSUM_MSB) << 8 |
123               rtcin(NVRAM_FIRST + CKSUM_LSB);
124         for (i = CKSUM_FIRST; i <= CKSUM_LAST; i++)
125                 sum -= rtcin(NVRAM_FIRST + i);
126         if (sum != 0) {
127                 sx_xunlock(&nvram_lock);
128                 return (EIO);
129         }
130         /* Bring in user data and write */
131         while (uio->uio_resid > 0 && error == 0) {
132                 nv_off = uio->uio_offset + NVRAM_FIRST;
133                 if (nv_off < NVRAM_FIRST || nv_off >= NVRAM_LAST) {
134                         sx_xunlock(&nvram_lock);
135                         return (0);     /* Signal EOF */
136                 }
137                 /* Single byte at a time */
138                 error = uiomove(&v, 1, uio);
139                 writertc(nv_off, v);
140         }
141         /* Recalculate checksum afterwards */
142         sum = 0;
143         for (i = CKSUM_FIRST; i <= CKSUM_LAST; i++)
144                 sum += rtcin(NVRAM_FIRST + i);
145         writertc(NVRAM_FIRST + CKSUM_MSB, sum >> 8);
146         writertc(NVRAM_FIRST + CKSUM_LSB, sum);
147         sx_xunlock(&nvram_lock);
148         return (error);
149 }
150
151 static int
152 nvram_modevent(module_t mod __unused, int type, void *data __unused)
153 {
154         switch (type) {
155         case MOD_LOAD:
156                 sx_init(&nvram_lock, "nvram");
157                 nvram_dev = make_dev(&nvram_cdevsw, 0,
158                     UID_ROOT, GID_KMEM, 0640, "nvram");
159                 break;
160         case MOD_UNLOAD:
161         case MOD_SHUTDOWN:
162                 destroy_dev(nvram_dev);
163                 sx_destroy(&nvram_lock);
164                 break;
165         default:
166                 return (EOPNOTSUPP);
167         }
168         return (0);
169 }
170 DEV_MODULE(nvram, nvram_modevent, NULL);