xref: /linux/arch/alpha/kernel/srm_env.c (revision 359745d78351c6f5442435f81549f0207ece28aa)
145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * srm_env.c - Access to SRM environment
41da177e4SLinus Torvalds  *             variables through linux' procfs
51da177e4SLinus Torvalds  *
616b7f4dcSJan-Benedict Glaw  * (C) 2001,2002,2006 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
71da177e4SLinus Torvalds  *
8395cf969SPaul Bolle  * This driver is a modified version of Erik Mouw's example proc
9395cf969SPaul Bolle  * interface, so: thank you, Erik! He can be reached via email at
101da177e4SLinus Torvalds  * <J.A.K.Mouw@its.tudelft.nl>. It is based on an idea
111da177e4SLinus Torvalds  * provided by DEC^WCompaq^WIntel's "Jumpstart" CD. They
121da177e4SLinus Torvalds  * included a patch like this as well. Thanks for idea!
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <linux/kernel.h>
165a0e3ad6STejun Heo #include <linux/gfp.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/proc_fs.h>
200ead0f84SAlexey Dobriyan #include <linux/seq_file.h>
211da177e4SLinus Torvalds #include <asm/console.h>
227c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
231da177e4SLinus Torvalds #include <asm/machvec.h>
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #define BASE_DIR	"srm_environment"	/* Subdir in /proc/		*/
261da177e4SLinus Torvalds #define NAMED_DIR	"named_variables"	/* Subdir for known variables	*/
271da177e4SLinus Torvalds #define NUMBERED_DIR	"numbered_variables"	/* Subdir for all variables	*/
2816b7f4dcSJan-Benedict Glaw #define VERSION		"0.0.6"			/* Module version		*/
291da177e4SLinus Torvalds #define NAME		"srm_env"		/* Module name			*/
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
321da177e4SLinus Torvalds MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
331da177e4SLinus Torvalds MODULE_LICENSE("GPL");
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds typedef struct _srm_env {
361da177e4SLinus Torvalds 	char			*name;
371da177e4SLinus Torvalds 	unsigned long		id;
381da177e4SLinus Torvalds } srm_env_t;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds static struct proc_dir_entry	*base_dir;
411da177e4SLinus Torvalds static struct proc_dir_entry	*named_dir;
421da177e4SLinus Torvalds static struct proc_dir_entry	*numbered_dir;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static srm_env_t	srm_named_entries[] = {
451da177e4SLinus Torvalds 	{ "auto_action",	ENV_AUTO_ACTION		},
461da177e4SLinus Torvalds 	{ "boot_dev",		ENV_BOOT_DEV		},
471da177e4SLinus Torvalds 	{ "bootdef_dev",	ENV_BOOTDEF_DEV		},
481da177e4SLinus Torvalds 	{ "booted_dev",		ENV_BOOTED_DEV		},
491da177e4SLinus Torvalds 	{ "boot_file",		ENV_BOOT_FILE		},
501da177e4SLinus Torvalds 	{ "booted_file",	ENV_BOOTED_FILE		},
511da177e4SLinus Torvalds 	{ "boot_osflags",	ENV_BOOT_OSFLAGS	},
521da177e4SLinus Torvalds 	{ "booted_osflags",	ENV_BOOTED_OSFLAGS	},
531da177e4SLinus Torvalds 	{ "boot_reset",		ENV_BOOT_RESET		},
541da177e4SLinus Torvalds 	{ "dump_dev",		ENV_DUMP_DEV		},
551da177e4SLinus Torvalds 	{ "enable_audit",	ENV_ENABLE_AUDIT	},
561da177e4SLinus Torvalds 	{ "license",		ENV_LICENSE		},
571da177e4SLinus Torvalds 	{ "char_set",		ENV_CHAR_SET		},
581da177e4SLinus Torvalds 	{ "language",		ENV_LANGUAGE		},
591da177e4SLinus Torvalds 	{ "tty_dev",		ENV_TTY_DEV		},
601da177e4SLinus Torvalds 	{ NULL,			0			},
611da177e4SLinus Torvalds };
621da177e4SLinus Torvalds 
630ead0f84SAlexey Dobriyan static int srm_env_proc_show(struct seq_file *m, void *v)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds 	unsigned long	ret;
661c1ec6c6SAl Viro 	unsigned long	id = (unsigned long)m->private;
670ead0f84SAlexey Dobriyan 	char		*page;
681da177e4SLinus Torvalds 
690ead0f84SAlexey Dobriyan 	page = (char *)__get_free_page(GFP_USER);
700ead0f84SAlexey Dobriyan 	if (!page)
710ead0f84SAlexey Dobriyan 		return -ENOMEM;
721da177e4SLinus Torvalds 
731c1ec6c6SAl Viro 	ret = callback_getenv(id, page, PAGE_SIZE);
741da177e4SLinus Torvalds 
7516b7f4dcSJan-Benedict Glaw 	if ((ret >> 61) == 0) {
760ead0f84SAlexey Dobriyan 		seq_write(m, page, ret);
770ead0f84SAlexey Dobriyan 		ret = 0;
7816b7f4dcSJan-Benedict Glaw 	} else
790ead0f84SAlexey Dobriyan 		ret = -EFAULT;
800ead0f84SAlexey Dobriyan 	free_page((unsigned long)page);
810ead0f84SAlexey Dobriyan 	return ret;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
840ead0f84SAlexey Dobriyan static int srm_env_proc_open(struct inode *inode, struct file *file)
850ead0f84SAlexey Dobriyan {
86*359745d7SMuchun Song 	return single_open(file, srm_env_proc_show, pde_data(inode));
870ead0f84SAlexey Dobriyan }
880ead0f84SAlexey Dobriyan 
890ead0f84SAlexey Dobriyan static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer,
900ead0f84SAlexey Dobriyan 				  size_t count, loff_t *pos)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	int res;
93*359745d7SMuchun Song 	unsigned long	id = (unsigned long)pde_data(file_inode(file));
941da177e4SLinus Torvalds 	char		*buf = (char *) __get_free_page(GFP_USER);
951da177e4SLinus Torvalds 	unsigned long	ret1, ret2;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	if (!buf)
981da177e4SLinus Torvalds 		return -ENOMEM;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	res = -EINVAL;
1011da177e4SLinus Torvalds 	if (count >= PAGE_SIZE)
1021da177e4SLinus Torvalds 		goto out;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	res = -EFAULT;
1051da177e4SLinus Torvalds 	if (copy_from_user(buf, buffer, count))
1061da177e4SLinus Torvalds 		goto out;
1071da177e4SLinus Torvalds 	buf[count] = '\0';
1081da177e4SLinus Torvalds 
1091c1ec6c6SAl Viro 	ret1 = callback_setenv(id, buf, count);
1101da177e4SLinus Torvalds 	if ((ret1 >> 61) == 0) {
1111da177e4SLinus Torvalds 		do
1121da177e4SLinus Torvalds 			ret2 = callback_save_env();
1131da177e4SLinus Torvalds 		while((ret2 >> 61) == 1);
1141da177e4SLinus Torvalds 		res = (int) ret1;
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds  out:
1181da177e4SLinus Torvalds 	free_page((unsigned long)buf);
1191da177e4SLinus Torvalds 	return res;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
12297a32539SAlexey Dobriyan static const struct proc_ops srm_env_proc_ops = {
12397a32539SAlexey Dobriyan 	.proc_open	= srm_env_proc_open,
12497a32539SAlexey Dobriyan 	.proc_read	= seq_read,
12597a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
12697a32539SAlexey Dobriyan 	.proc_release	= single_release,
12797a32539SAlexey Dobriyan 	.proc_write	= srm_env_proc_write,
1280ead0f84SAlexey Dobriyan };
1290ead0f84SAlexey Dobriyan 
1301da177e4SLinus Torvalds static int __init
1311da177e4SLinus Torvalds srm_env_init(void)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	srm_env_t	*entry;
1341da177e4SLinus Torvalds 	unsigned long	var_num;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/*
1371da177e4SLinus Torvalds 	 * Check system
1381da177e4SLinus Torvalds 	 */
1391da177e4SLinus Torvalds 	if (!alpha_using_srm) {
1401da177e4SLinus Torvalds 		printk(KERN_INFO "%s: This Alpha system doesn't "
1411da177e4SLinus Torvalds 				"know about SRM (or you've booted "
1421da177e4SLinus Torvalds 				"SRM->MILO->Linux, which gets "
143bbb8d343SHarvey Harrison 				"misdetected)...\n", __func__);
1441da177e4SLinus Torvalds 		return -ENODEV;
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	/*
1481da177e4SLinus Torvalds 	 * Create base directory
1491da177e4SLinus Torvalds 	 */
1501da177e4SLinus Torvalds 	base_dir = proc_mkdir(BASE_DIR, NULL);
15116b7f4dcSJan-Benedict Glaw 	if (!base_dir) {
1521da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create base dir /proc/%s\n",
1531da177e4SLinus Torvalds 				BASE_DIR);
154c35f2e49SAl Viro 		return -ENOMEM;
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	/*
1581da177e4SLinus Torvalds 	 * Create per-name subdirectory
1591da177e4SLinus Torvalds 	 */
1601da177e4SLinus Torvalds 	named_dir = proc_mkdir(NAMED_DIR, base_dir);
16116b7f4dcSJan-Benedict Glaw 	if (!named_dir) {
1621da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
1631da177e4SLinus Torvalds 				BASE_DIR, NAMED_DIR);
1641da177e4SLinus Torvalds 		goto cleanup;
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	/*
1681da177e4SLinus Torvalds 	 * Create per-number subdirectory
1691da177e4SLinus Torvalds 	 */
1701da177e4SLinus Torvalds 	numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
17116b7f4dcSJan-Benedict Glaw 	if (!numbered_dir) {
1721da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
1731da177e4SLinus Torvalds 				BASE_DIR, NUMBERED_DIR);
1741da177e4SLinus Torvalds 		goto cleanup;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	/*
1791da177e4SLinus Torvalds 	 * Create all named nodes
1801da177e4SLinus Torvalds 	 */
1811da177e4SLinus Torvalds 	entry = srm_named_entries;
18216b7f4dcSJan-Benedict Glaw 	while (entry->name && entry->id) {
183c35f2e49SAl Viro 		if (!proc_create_data(entry->name, 0644, named_dir,
18497a32539SAlexey Dobriyan 			     &srm_env_proc_ops, (void *)entry->id))
1851da177e4SLinus Torvalds 			goto cleanup;
1861da177e4SLinus Torvalds 		entry++;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	/*
1901da177e4SLinus Torvalds 	 * Create all numbered nodes
1911da177e4SLinus Torvalds 	 */
1921da177e4SLinus Torvalds 	for (var_num = 0; var_num <= 255; var_num++) {
1931c1ec6c6SAl Viro 		char name[4];
1941c1ec6c6SAl Viro 		sprintf(name, "%ld", var_num);
1951c1ec6c6SAl Viro 		if (!proc_create_data(name, 0644, numbered_dir,
19697a32539SAlexey Dobriyan 			     &srm_env_proc_ops, (void *)var_num))
1971da177e4SLinus Torvalds 			goto cleanup;
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
2011da177e4SLinus Torvalds 			VERSION);
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	return 0;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds cleanup:
206c35f2e49SAl Viro 	remove_proc_subtree(BASE_DIR, NULL);
2071da177e4SLinus Torvalds 	return -ENOMEM;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds static void __exit
2111da177e4SLinus Torvalds srm_env_exit(void)
2121da177e4SLinus Torvalds {
213c35f2e49SAl Viro 	remove_proc_subtree(BASE_DIR, NULL);
2141da177e4SLinus Torvalds 	printk(KERN_INFO "%s: unloaded successfully\n", NAME);
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds module_init(srm_env_init);
2181da177e4SLinus Torvalds module_exit(srm_env_exit);
219