xref: /linux/drivers/char/nvram.c (revision e8c715f3a7dae43fabae261493a26474fec11863)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * CMOS/NV-RAM driver for Linux
4  *
5  * Copyright (C) 1997 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
6  * idea by and with help from Richard Jelinek <rj@suse.de>
7  * Portions copyright (c) 2001,2002 Sun Microsystems (thockin@sun.com)
8  *
9  * This driver allows you to access the contents of the non-volatile memory in
10  * the mc146818rtc.h real-time clock. This chip is built into all PCs and into
11  * many Atari machines. In the former it's called "CMOS-RAM", in the latter
12  * "NVRAM" (NV stands for non-volatile).
13  *
14  * The data are supplied as a (seekable) character device, /dev/nvram. The
15  * size of this file is dependent on the controller.  The usual size is 114,
16  * the number of freely available bytes in the memory (i.e., not used by the
17  * RTC itself).
18  *
19  * Checksums over the NVRAM contents are managed by this driver. In case of a
20  * bad checksum, reads and writes return -EIO. The checksum can be initialized
21  * to a sane state either by ioctl(NVRAM_INIT) (clear whole NVRAM) or
22  * ioctl(NVRAM_SETCKS) (doesn't change contents, just makes checksum valid
23  * again; use with care!)
24  *
25  * 	1.1	Cesar Barros: SMP locking fixes
26  * 		added changelog
27  * 	1.2	Erik Gilling: Cobalt Networks support
28  * 		Tim Hockin: general cleanup, Cobalt support
29  * 	1.3	Wim Van Sebroeck: convert PRINT_PROC to seq_file
30  */
31 
32 #define NVRAM_VERSION	"1.3"
33 
34 #include <linux/module.h>
35 #include <linux/nvram.h>
36 #include <linux/types.h>
37 #include <linux/errno.h>
38 #include <linux/miscdevice.h>
39 #include <linux/ioport.h>
40 #include <linux/fcntl.h>
41 #include <linux/mc146818rtc.h>
42 #include <linux/init.h>
43 #include <linux/proc_fs.h>
44 #include <linux/seq_file.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/io.h>
48 #include <linux/uaccess.h>
49 #include <linux/mutex.h>
50 #include <linux/pagemap.h>
51 
52 #ifdef CONFIG_PPC
53 #include <asm/nvram.h>
54 #endif
55 
56 static DEFINE_SPINLOCK(nvram_state_lock);
57 static int nvram_open_cnt;	/* #times opened */
58 static int nvram_open_mode;	/* special open modes */
59 static ssize_t nvram_size;
60 #define NVRAM_WRITE		1 /* opened for writing (exclusive) */
61 #define NVRAM_EXCL		2 /* opened with O_EXCL */
62 
63 #ifdef CONFIG_X86
64 /*
65  * These functions are provided to be called internally or by other parts of
66  * the kernel. It's up to the caller to ensure correct checksum before reading
67  * or after writing (needs to be done only once).
68  *
69  * It is worth noting that these functions all access bytes of general
70  * purpose memory in the NVRAM - that is to say, they all add the
71  * NVRAM_FIRST_BYTE offset.  Pass them offsets into NVRAM as if you did not
72  * know about the RTC cruft.
73  */
74 
75 #define NVRAM_BYTES		(128 - NVRAM_FIRST_BYTE)
76 
77 /* Note that *all* calls to CMOS_READ and CMOS_WRITE must be done with
78  * rtc_lock held. Due to the index-port/data-port design of the RTC, we
79  * don't want two different things trying to get to it at once. (e.g. the
80  * periodic 11 min sync from kernel/time/ntp.c vs. this driver.)
81  */
82 
83 static unsigned char __nvram_read_byte(int i)
84 {
85 	return CMOS_READ(NVRAM_FIRST_BYTE + i);
86 }
87 
88 static unsigned char pc_nvram_read_byte(int i)
89 {
90 	unsigned long flags;
91 	unsigned char c;
92 
93 	spin_lock_irqsave(&rtc_lock, flags);
94 	c = __nvram_read_byte(i);
95 	spin_unlock_irqrestore(&rtc_lock, flags);
96 	return c;
97 }
98 
99 /* This races nicely with trying to read with checksum checking (nvram_read) */
100 static void __nvram_write_byte(unsigned char c, int i)
101 {
102 	CMOS_WRITE(c, NVRAM_FIRST_BYTE + i);
103 }
104 
105 static void pc_nvram_write_byte(unsigned char c, int i)
106 {
107 	unsigned long flags;
108 
109 	spin_lock_irqsave(&rtc_lock, flags);
110 	__nvram_write_byte(c, i);
111 	spin_unlock_irqrestore(&rtc_lock, flags);
112 }
113 
114 /* On PCs, the checksum is built only over bytes 2..31 */
115 #define PC_CKS_RANGE_START	2
116 #define PC_CKS_RANGE_END	31
117 #define PC_CKS_LOC		32
118 
119 static int __nvram_check_checksum(void)
120 {
121 	int i;
122 	unsigned short sum = 0;
123 	unsigned short expect;
124 
125 	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
126 		sum += __nvram_read_byte(i);
127 	expect = __nvram_read_byte(PC_CKS_LOC)<<8 |
128 	    __nvram_read_byte(PC_CKS_LOC+1);
129 	return (sum & 0xffff) == expect;
130 }
131 
132 static void __nvram_set_checksum(void)
133 {
134 	int i;
135 	unsigned short sum = 0;
136 
137 	for (i = PC_CKS_RANGE_START; i <= PC_CKS_RANGE_END; ++i)
138 		sum += __nvram_read_byte(i);
139 	__nvram_write_byte(sum >> 8, PC_CKS_LOC);
140 	__nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1);
141 }
142 
143 static long pc_nvram_set_checksum(void)
144 {
145 	spin_lock_irq(&rtc_lock);
146 	__nvram_set_checksum();
147 	spin_unlock_irq(&rtc_lock);
148 	return 0;
149 }
150 
151 static long pc_nvram_initialize(void)
152 {
153 	ssize_t i;
154 
155 	spin_lock_irq(&rtc_lock);
156 	for (i = 0; i < NVRAM_BYTES; ++i)
157 		__nvram_write_byte(0, i);
158 	__nvram_set_checksum();
159 	spin_unlock_irq(&rtc_lock);
160 	return 0;
161 }
162 
163 static ssize_t pc_nvram_get_size(void)
164 {
165 	return NVRAM_BYTES;
166 }
167 
168 static ssize_t pc_nvram_read(char *buf, size_t count, loff_t *ppos)
169 {
170 	char *p = buf;
171 	loff_t i;
172 
173 	spin_lock_irq(&rtc_lock);
174 	if (!__nvram_check_checksum()) {
175 		spin_unlock_irq(&rtc_lock);
176 		return -EIO;
177 	}
178 	for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
179 		*p = __nvram_read_byte(i);
180 	spin_unlock_irq(&rtc_lock);
181 
182 	*ppos = i;
183 	return p - buf;
184 }
185 
186 static ssize_t pc_nvram_write(char *buf, size_t count, loff_t *ppos)
187 {
188 	char *p = buf;
189 	loff_t i;
190 
191 	spin_lock_irq(&rtc_lock);
192 	if (!__nvram_check_checksum()) {
193 		spin_unlock_irq(&rtc_lock);
194 		return -EIO;
195 	}
196 	for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
197 		__nvram_write_byte(*p, i);
198 	__nvram_set_checksum();
199 	spin_unlock_irq(&rtc_lock);
200 
201 	*ppos = i;
202 	return p - buf;
203 }
204 
205 const struct nvram_ops arch_nvram_ops = {
206 	.read           = pc_nvram_read,
207 	.write          = pc_nvram_write,
208 	.read_byte      = pc_nvram_read_byte,
209 	.write_byte     = pc_nvram_write_byte,
210 	.get_size       = pc_nvram_get_size,
211 	.set_checksum   = pc_nvram_set_checksum,
212 	.initialize     = pc_nvram_initialize,
213 };
214 EXPORT_SYMBOL(arch_nvram_ops);
215 #endif /* CONFIG_X86 */
216 
217 /*
218  * The are the file operation function for user access to /dev/nvram
219  */
220 
221 static loff_t nvram_misc_llseek(struct file *file, loff_t offset, int origin)
222 {
223 	return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE,
224 					nvram_size);
225 }
226 
227 static ssize_t nvram_misc_read(struct file *file, char __user *buf,
228 			       size_t count, loff_t *ppos)
229 {
230 	char *tmp;
231 	ssize_t ret;
232 
233 
234 	if (*ppos >= nvram_size)
235 		return 0;
236 
237 	count = min_t(size_t, count, nvram_size - *ppos);
238 	count = min_t(size_t, count, PAGE_SIZE);
239 
240 	tmp = kmalloc(count, GFP_KERNEL);
241 	if (!tmp)
242 		return -ENOMEM;
243 
244 	ret = nvram_read(tmp, count, ppos);
245 	if (ret <= 0)
246 		goto out;
247 
248 	if (copy_to_user(buf, tmp, ret)) {
249 		*ppos -= ret;
250 		ret = -EFAULT;
251 	}
252 
253 out:
254 	kfree(tmp);
255 	return ret;
256 }
257 
258 static ssize_t nvram_misc_write(struct file *file, const char __user *buf,
259 				size_t count, loff_t *ppos)
260 {
261 	char *tmp;
262 	ssize_t ret;
263 
264 	if (*ppos >= nvram_size)
265 		return 0;
266 
267 	count = min_t(size_t, count, nvram_size - *ppos);
268 	count = min_t(size_t, count, PAGE_SIZE);
269 
270 	tmp = memdup_user(buf, count);
271 	if (IS_ERR(tmp))
272 		return PTR_ERR(tmp);
273 
274 	ret = nvram_write(tmp, count, ppos);
275 	kfree(tmp);
276 	return ret;
277 }
278 
279 static long nvram_misc_ioctl(struct file *file, unsigned int cmd,
280 			     unsigned long arg)
281 {
282 	long ret = -ENOTTY;
283 
284 	switch (cmd) {
285 #ifdef CONFIG_PPC
286 	case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
287 		pr_warn("nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
288 		fallthrough;
289 	case IOC_NVRAM_GET_OFFSET:
290 		ret = -EINVAL;
291 #ifdef CONFIG_PPC_PMAC
292 		if (machine_is(powermac)) {
293 			int part, offset;
294 
295 			if (copy_from_user(&part, (void __user *)arg,
296 					   sizeof(part)) != 0)
297 				return -EFAULT;
298 			if (part < pmac_nvram_OF || part > pmac_nvram_NR)
299 				return -EINVAL;
300 			offset = pmac_get_partition(part);
301 			if (offset < 0)
302 				return -EINVAL;
303 			if (copy_to_user((void __user *)arg,
304 					 &offset, sizeof(offset)) != 0)
305 				return -EFAULT;
306 			ret = 0;
307 		}
308 #endif
309 		break;
310 #ifdef CONFIG_PPC32
311 	case IOC_NVRAM_SYNC:
312 		if (ppc_md.nvram_sync)
313 			ppc_md.nvram_sync();
314 		ret = 0;
315 		break;
316 #endif
317 #elif defined(CONFIG_X86) || defined(CONFIG_M68K)
318 	case NVRAM_INIT:
319 		/* initialize NVRAM contents and checksum */
320 		if (!capable(CAP_SYS_ADMIN))
321 			return -EACCES;
322 
323 		if (arch_nvram_ops.initialize)
324 			ret = arch_nvram_ops.initialize();
325 		break;
326 	case NVRAM_SETCKS:
327 		/* just set checksum, contents unchanged (maybe useful after
328 		 * checksum garbaged somehow...) */
329 		if (!capable(CAP_SYS_ADMIN))
330 			return -EACCES;
331 
332 		if (arch_nvram_ops.set_checksum)
333 			ret = arch_nvram_ops.set_checksum();
334 		break;
335 #endif /* CONFIG_X86 || CONFIG_M68K */
336 	}
337 	return ret;
338 }
339 
340 static int nvram_misc_open(struct inode *inode, struct file *file)
341 {
342 	spin_lock(&nvram_state_lock);
343 
344 	/* Prevent multiple readers/writers if desired. */
345 	if ((nvram_open_cnt && (file->f_flags & O_EXCL)) ||
346 	    (nvram_open_mode & NVRAM_EXCL)) {
347 		spin_unlock(&nvram_state_lock);
348 		return -EBUSY;
349 	}
350 
351 #if defined(CONFIG_X86) || defined(CONFIG_M68K)
352 	/* Prevent multiple writers if the set_checksum ioctl is implemented. */
353 	if ((arch_nvram_ops.set_checksum != NULL) &&
354 	    (file->f_mode & FMODE_WRITE) && (nvram_open_mode & NVRAM_WRITE)) {
355 		spin_unlock(&nvram_state_lock);
356 		return -EBUSY;
357 	}
358 #endif
359 
360 	if (file->f_flags & O_EXCL)
361 		nvram_open_mode |= NVRAM_EXCL;
362 	if (file->f_mode & FMODE_WRITE)
363 		nvram_open_mode |= NVRAM_WRITE;
364 	nvram_open_cnt++;
365 
366 	spin_unlock(&nvram_state_lock);
367 
368 	return 0;
369 }
370 
371 static int nvram_misc_release(struct inode *inode, struct file *file)
372 {
373 	spin_lock(&nvram_state_lock);
374 
375 	nvram_open_cnt--;
376 
377 	/* if only one instance is open, clear the EXCL bit */
378 	if (nvram_open_mode & NVRAM_EXCL)
379 		nvram_open_mode &= ~NVRAM_EXCL;
380 	if (file->f_mode & FMODE_WRITE)
381 		nvram_open_mode &= ~NVRAM_WRITE;
382 
383 	spin_unlock(&nvram_state_lock);
384 
385 	return 0;
386 }
387 
388 #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
389 static const char * const floppy_types[] = {
390 	"none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M",
391 	"3.5'' 2.88M", "3.5'' 2.88M"
392 };
393 
394 static const char * const gfx_types[] = {
395 	"EGA, VGA, ... (with BIOS)",
396 	"CGA (40 cols)",
397 	"CGA (80 cols)",
398 	"monochrome",
399 };
400 
401 static void pc_nvram_proc_read(unsigned char *nvram, struct seq_file *seq,
402 			       void *offset)
403 {
404 	int checksum;
405 	int type;
406 
407 	spin_lock_irq(&rtc_lock);
408 	checksum = __nvram_check_checksum();
409 	spin_unlock_irq(&rtc_lock);
410 
411 	seq_printf(seq, "Checksum status: %svalid\n", checksum ? "" : "not ");
412 
413 	seq_printf(seq, "# floppies     : %d\n",
414 	    (nvram[6] & 1) ? (nvram[6] >> 6) + 1 : 0);
415 	seq_printf(seq, "Floppy 0 type  : ");
416 	type = nvram[2] >> 4;
417 	if (type < ARRAY_SIZE(floppy_types))
418 		seq_printf(seq, "%s\n", floppy_types[type]);
419 	else
420 		seq_printf(seq, "%d (unknown)\n", type);
421 	seq_printf(seq, "Floppy 1 type  : ");
422 	type = nvram[2] & 0x0f;
423 	if (type < ARRAY_SIZE(floppy_types))
424 		seq_printf(seq, "%s\n", floppy_types[type]);
425 	else
426 		seq_printf(seq, "%d (unknown)\n", type);
427 
428 	seq_printf(seq, "HD 0 type      : ");
429 	type = nvram[4] >> 4;
430 	if (type)
431 		seq_printf(seq, "%02x\n", type == 0x0f ? nvram[11] : type);
432 	else
433 		seq_printf(seq, "none\n");
434 
435 	seq_printf(seq, "HD 1 type      : ");
436 	type = nvram[4] & 0x0f;
437 	if (type)
438 		seq_printf(seq, "%02x\n", type == 0x0f ? nvram[12] : type);
439 	else
440 		seq_printf(seq, "none\n");
441 
442 	seq_printf(seq, "HD type 48 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
443 	    nvram[18] | (nvram[19] << 8),
444 	    nvram[20], nvram[25],
445 	    nvram[21] | (nvram[22] << 8), nvram[23] | (nvram[24] << 8));
446 	seq_printf(seq, "HD type 49 data: %d/%d/%d C/H/S, precomp %d, lz %d\n",
447 	    nvram[39] | (nvram[40] << 8),
448 	    nvram[41], nvram[46],
449 	    nvram[42] | (nvram[43] << 8), nvram[44] | (nvram[45] << 8));
450 
451 	seq_printf(seq, "DOS base memory: %d kB\n", nvram[7] | (nvram[8] << 8));
452 	seq_printf(seq, "Extended memory: %d kB (configured), %d kB (tested)\n",
453 	    nvram[9] | (nvram[10] << 8), nvram[34] | (nvram[35] << 8));
454 
455 	seq_printf(seq, "Gfx adapter    : %s\n",
456 	    gfx_types[(nvram[6] >> 4) & 3]);
457 
458 	seq_printf(seq, "FPU            : %sinstalled\n",
459 	    (nvram[6] & 2) ? "" : "not ");
460 
461 	return;
462 }
463 
464 static int nvram_proc_read(struct seq_file *seq, void *offset)
465 {
466 	unsigned char contents[NVRAM_BYTES];
467 	int i = 0;
468 
469 	spin_lock_irq(&rtc_lock);
470 	for (i = 0; i < NVRAM_BYTES; ++i)
471 		contents[i] = __nvram_read_byte(i);
472 	spin_unlock_irq(&rtc_lock);
473 
474 	pc_nvram_proc_read(contents, seq, offset);
475 
476 	return 0;
477 }
478 #endif /* CONFIG_X86 && CONFIG_PROC_FS */
479 
480 static const struct file_operations nvram_misc_fops = {
481 	.owner		= THIS_MODULE,
482 	.llseek		= nvram_misc_llseek,
483 	.read		= nvram_misc_read,
484 	.write		= nvram_misc_write,
485 	.unlocked_ioctl	= nvram_misc_ioctl,
486 	.open		= nvram_misc_open,
487 	.release	= nvram_misc_release,
488 };
489 
490 static struct miscdevice nvram_misc = {
491 	NVRAM_MINOR,
492 	"nvram",
493 	&nvram_misc_fops,
494 };
495 
496 static int __init nvram_module_init(void)
497 {
498 	int ret;
499 
500 	nvram_size = nvram_get_size();
501 	if (nvram_size < 0)
502 		return nvram_size;
503 
504 	ret = misc_register(&nvram_misc);
505 	if (ret) {
506 		pr_err("nvram: can't misc_register on minor=%d\n", NVRAM_MINOR);
507 		return ret;
508 	}
509 
510 #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
511 	if (!proc_create_single("driver/nvram", 0, NULL, nvram_proc_read)) {
512 		pr_err("nvram: can't create /proc/driver/nvram\n");
513 		misc_deregister(&nvram_misc);
514 		return -ENOMEM;
515 	}
516 #endif
517 
518 	pr_info("Non-volatile memory driver v" NVRAM_VERSION "\n");
519 	return 0;
520 }
521 
522 static void __exit nvram_module_exit(void)
523 {
524 #if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
525 	remove_proc_entry("driver/nvram", NULL);
526 #endif
527 	misc_deregister(&nvram_misc);
528 }
529 
530 module_init(nvram_module_init);
531 module_exit(nvram_module_exit);
532 
533 MODULE_DESCRIPTION("CMOS/NV-RAM driver for Linux");
534 MODULE_LICENSE("GPL");
535 MODULE_ALIAS_MISCDEV(NVRAM_MINOR);
536 MODULE_ALIAS("devname:nvram");
537