xref: /linux/arch/alpha/kernel/srm_env.c (revision 16b7f4dcd340875625714438a812ea06400f9666)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * srm_env.c - Access to SRM environment
31da177e4SLinus Torvalds  *             variables through linux' procfs
41da177e4SLinus Torvalds  *
5*16b7f4dcSJan-Benedict Glaw  * (C) 2001,2002,2006 by Jan-Benedict Glaw <jbglaw@lug-owl.de>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This driver is at all a modified version of Erik Mouw's
81da177e4SLinus Torvalds  * Documentation/DocBook/procfs_example.c, so: thank
91da177e4SLinus Torvalds  * 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  * This program is free software; you can redistribute
151da177e4SLinus Torvalds  * it and/or modify it under the terms of the GNU General
161da177e4SLinus Torvalds  * Public License version 2 as published by the Free Software
171da177e4SLinus Torvalds  * Foundation.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * This program is distributed in the hope that it will be
201da177e4SLinus Torvalds  * useful, but WITHOUT ANY WARRANTY; without even the implied
211da177e4SLinus Torvalds  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
221da177e4SLinus Torvalds  * PURPOSE.  See the GNU General Public License for more
231da177e4SLinus Torvalds  * details.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public
261da177e4SLinus Torvalds  * License along with this program; if not, write to the
271da177e4SLinus Torvalds  * Free Software Foundation, Inc., 59 Temple Place,
281da177e4SLinus Torvalds  * Suite 330, Boston, MA  02111-1307  USA
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <linux/kernel.h>
331da177e4SLinus Torvalds #include <linux/module.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/proc_fs.h>
361da177e4SLinus Torvalds #include <asm/console.h>
371da177e4SLinus Torvalds #include <asm/uaccess.h>
381da177e4SLinus Torvalds #include <asm/machvec.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define BASE_DIR	"srm_environment"	/* Subdir in /proc/		*/
411da177e4SLinus Torvalds #define NAMED_DIR	"named_variables"	/* Subdir for known variables	*/
421da177e4SLinus Torvalds #define NUMBERED_DIR	"numbered_variables"	/* Subdir for all variables	*/
43*16b7f4dcSJan-Benedict Glaw #define VERSION		"0.0.6"			/* Module version		*/
441da177e4SLinus Torvalds #define NAME		"srm_env"		/* Module name			*/
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
471da177e4SLinus Torvalds MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
481da177e4SLinus Torvalds MODULE_LICENSE("GPL");
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds typedef struct _srm_env {
511da177e4SLinus Torvalds 	char			*name;
521da177e4SLinus Torvalds 	unsigned long		id;
531da177e4SLinus Torvalds 	struct proc_dir_entry	*proc_entry;
541da177e4SLinus Torvalds } srm_env_t;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static struct proc_dir_entry	*base_dir;
571da177e4SLinus Torvalds static struct proc_dir_entry	*named_dir;
581da177e4SLinus Torvalds static struct proc_dir_entry	*numbered_dir;
591da177e4SLinus Torvalds static char			number[256][4];
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static srm_env_t	srm_named_entries[] = {
621da177e4SLinus Torvalds 	{ "auto_action",	ENV_AUTO_ACTION		},
631da177e4SLinus Torvalds 	{ "boot_dev",		ENV_BOOT_DEV		},
641da177e4SLinus Torvalds 	{ "bootdef_dev",	ENV_BOOTDEF_DEV		},
651da177e4SLinus Torvalds 	{ "booted_dev",		ENV_BOOTED_DEV		},
661da177e4SLinus Torvalds 	{ "boot_file",		ENV_BOOT_FILE		},
671da177e4SLinus Torvalds 	{ "booted_file",	ENV_BOOTED_FILE		},
681da177e4SLinus Torvalds 	{ "boot_osflags",	ENV_BOOT_OSFLAGS	},
691da177e4SLinus Torvalds 	{ "booted_osflags",	ENV_BOOTED_OSFLAGS	},
701da177e4SLinus Torvalds 	{ "boot_reset",		ENV_BOOT_RESET		},
711da177e4SLinus Torvalds 	{ "dump_dev",		ENV_DUMP_DEV		},
721da177e4SLinus Torvalds 	{ "enable_audit",	ENV_ENABLE_AUDIT	},
731da177e4SLinus Torvalds 	{ "license",		ENV_LICENSE		},
741da177e4SLinus Torvalds 	{ "char_set",		ENV_CHAR_SET		},
751da177e4SLinus Torvalds 	{ "language",		ENV_LANGUAGE		},
761da177e4SLinus Torvalds 	{ "tty_dev",		ENV_TTY_DEV		},
771da177e4SLinus Torvalds 	{ NULL,			0			},
781da177e4SLinus Torvalds };
791da177e4SLinus Torvalds static srm_env_t	srm_numbered_entries[256];
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds static int
831da177e4SLinus Torvalds srm_env_read(char *page, char **start, off_t off, int count, int *eof,
841da177e4SLinus Torvalds 		void *data)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	int		nbytes;
871da177e4SLinus Torvalds 	unsigned long	ret;
881da177e4SLinus Torvalds 	srm_env_t	*entry;
891da177e4SLinus Torvalds 
90*16b7f4dcSJan-Benedict Glaw 	if (off != 0) {
91*16b7f4dcSJan-Benedict Glaw 		*eof = 1;
92*16b7f4dcSJan-Benedict Glaw 		return 0;
93*16b7f4dcSJan-Benedict Glaw 	}
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	entry	= (srm_env_t *) data;
961da177e4SLinus Torvalds 	ret	= callback_getenv(entry->id, page, count);
971da177e4SLinus Torvalds 
98*16b7f4dcSJan-Benedict Glaw 	if ((ret >> 61) == 0) {
991da177e4SLinus Torvalds 		nbytes = (int) ret;
100*16b7f4dcSJan-Benedict Glaw 		*eof = 1;
101*16b7f4dcSJan-Benedict Glaw 	} else
1021da177e4SLinus Torvalds 		nbytes = -EFAULT;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	return nbytes;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static int
1081da177e4SLinus Torvalds srm_env_write(struct file *file, const char __user *buffer, unsigned long count,
1091da177e4SLinus Torvalds 		void *data)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	int res;
1121da177e4SLinus Torvalds 	srm_env_t	*entry;
1131da177e4SLinus Torvalds 	char		*buf = (char *) __get_free_page(GFP_USER);
1141da177e4SLinus Torvalds 	unsigned long	ret1, ret2;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	entry = (srm_env_t *) data;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	if (!buf)
1191da177e4SLinus Torvalds 		return -ENOMEM;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	res = -EINVAL;
1221da177e4SLinus Torvalds 	if (count >= PAGE_SIZE)
1231da177e4SLinus Torvalds 		goto out;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	res = -EFAULT;
1261da177e4SLinus Torvalds 	if (copy_from_user(buf, buffer, count))
1271da177e4SLinus Torvalds 		goto out;
1281da177e4SLinus Torvalds 	buf[count] = '\0';
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	ret1 = callback_setenv(entry->id, buf, count);
1311da177e4SLinus Torvalds 	if ((ret1 >> 61) == 0) {
1321da177e4SLinus Torvalds 		do
1331da177e4SLinus Torvalds 			ret2 = callback_save_env();
1341da177e4SLinus Torvalds 		while((ret2 >> 61) == 1);
1351da177e4SLinus Torvalds 		res = (int) ret1;
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds  out:
1391da177e4SLinus Torvalds 	free_page((unsigned long)buf);
1401da177e4SLinus Torvalds 	return res;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static void
1441da177e4SLinus Torvalds srm_env_cleanup(void)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	srm_env_t	*entry;
1471da177e4SLinus Torvalds 	unsigned long	var_num;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	if (base_dir) {
1501da177e4SLinus Torvalds 		/*
1511da177e4SLinus Torvalds 		 * Remove named entries
1521da177e4SLinus Torvalds 		 */
1531da177e4SLinus Torvalds 		if (named_dir) {
1541da177e4SLinus Torvalds 			entry = srm_named_entries;
1551da177e4SLinus Torvalds 			while (entry->name != NULL && entry->id != 0) {
1561da177e4SLinus Torvalds 				if (entry->proc_entry) {
1571da177e4SLinus Torvalds 					remove_proc_entry(entry->name,
1581da177e4SLinus Torvalds 							named_dir);
1591da177e4SLinus Torvalds 					entry->proc_entry = NULL;
1601da177e4SLinus Torvalds 				}
1611da177e4SLinus Torvalds 				entry++;
1621da177e4SLinus Torvalds 			}
1631da177e4SLinus Torvalds 			remove_proc_entry(NAMED_DIR, base_dir);
1641da177e4SLinus Torvalds 		}
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 		/*
1671da177e4SLinus Torvalds 		 * Remove numbered entries
1681da177e4SLinus Torvalds 		 */
1691da177e4SLinus Torvalds 		if (numbered_dir) {
1701da177e4SLinus Torvalds 			for (var_num = 0; var_num <= 255; var_num++) {
1711da177e4SLinus Torvalds 				entry =	&srm_numbered_entries[var_num];
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 				if (entry->proc_entry) {
1741da177e4SLinus Torvalds 					remove_proc_entry(entry->name,
1751da177e4SLinus Torvalds 							numbered_dir);
1761da177e4SLinus Torvalds 					entry->proc_entry	= NULL;
1771da177e4SLinus Torvalds 					entry->name		= NULL;
1781da177e4SLinus Torvalds 				}
1791da177e4SLinus Torvalds 			}
1801da177e4SLinus Torvalds 			remove_proc_entry(NUMBERED_DIR, base_dir);
1811da177e4SLinus Torvalds 		}
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 		remove_proc_entry(BASE_DIR, NULL);
1841da177e4SLinus Torvalds 	}
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	return;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds static int __init
1901da177e4SLinus Torvalds srm_env_init(void)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	srm_env_t	*entry;
1931da177e4SLinus Torvalds 	unsigned long	var_num;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	/*
1961da177e4SLinus Torvalds 	 * Check system
1971da177e4SLinus Torvalds 	 */
1981da177e4SLinus Torvalds 	if (!alpha_using_srm) {
1991da177e4SLinus Torvalds 		printk(KERN_INFO "%s: This Alpha system doesn't "
2001da177e4SLinus Torvalds 				"know about SRM (or you've booted "
2011da177e4SLinus Torvalds 				"SRM->MILO->Linux, which gets "
2021da177e4SLinus Torvalds 				"misdetected)...\n", __FUNCTION__);
2031da177e4SLinus Torvalds 		return -ENODEV;
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/*
2071da177e4SLinus Torvalds 	 * Init numbers
2081da177e4SLinus Torvalds 	 */
2091da177e4SLinus Torvalds 	for (var_num = 0; var_num <= 255; var_num++)
2101da177e4SLinus Torvalds 		sprintf(number[var_num], "%ld", var_num);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/*
2131da177e4SLinus Torvalds 	 * Create base directory
2141da177e4SLinus Torvalds 	 */
2151da177e4SLinus Torvalds 	base_dir = proc_mkdir(BASE_DIR, NULL);
216*16b7f4dcSJan-Benedict Glaw 	if (!base_dir) {
2171da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create base dir /proc/%s\n",
2181da177e4SLinus Torvalds 				BASE_DIR);
2191da177e4SLinus Torvalds 		goto cleanup;
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 	base_dir->owner = THIS_MODULE;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	/*
2241da177e4SLinus Torvalds 	 * Create per-name subdirectory
2251da177e4SLinus Torvalds 	 */
2261da177e4SLinus Torvalds 	named_dir = proc_mkdir(NAMED_DIR, base_dir);
227*16b7f4dcSJan-Benedict Glaw 	if (!named_dir) {
2281da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
2291da177e4SLinus Torvalds 				BASE_DIR, NAMED_DIR);
2301da177e4SLinus Torvalds 		goto cleanup;
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 	named_dir->owner = THIS_MODULE;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	/*
2351da177e4SLinus Torvalds 	 * Create per-number subdirectory
2361da177e4SLinus Torvalds 	 */
2371da177e4SLinus Torvalds 	numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
238*16b7f4dcSJan-Benedict Glaw 	if (!numbered_dir) {
2391da177e4SLinus Torvalds 		printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
2401da177e4SLinus Torvalds 				BASE_DIR, NUMBERED_DIR);
2411da177e4SLinus Torvalds 		goto cleanup;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds 	numbered_dir->owner = THIS_MODULE;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	/*
2471da177e4SLinus Torvalds 	 * Create all named nodes
2481da177e4SLinus Torvalds 	 */
2491da177e4SLinus Torvalds 	entry = srm_named_entries;
250*16b7f4dcSJan-Benedict Glaw 	while (entry->name && entry->id) {
2511da177e4SLinus Torvalds 		entry->proc_entry = create_proc_entry(entry->name,
2521da177e4SLinus Torvalds 				0644, named_dir);
253*16b7f4dcSJan-Benedict Glaw 		if (!entry->proc_entry)
2541da177e4SLinus Torvalds 			goto cleanup;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 		entry->proc_entry->data		= (void *) entry;
2571da177e4SLinus Torvalds 		entry->proc_entry->owner	= THIS_MODULE;
2581da177e4SLinus Torvalds 		entry->proc_entry->read_proc	= srm_env_read;
2591da177e4SLinus Torvalds 		entry->proc_entry->write_proc	= srm_env_write;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 		entry++;
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	/*
2651da177e4SLinus Torvalds 	 * Create all numbered nodes
2661da177e4SLinus Torvalds 	 */
2671da177e4SLinus Torvalds 	for (var_num = 0; var_num <= 255; var_num++) {
2681da177e4SLinus Torvalds 		entry = &srm_numbered_entries[var_num];
2691da177e4SLinus Torvalds 		entry->name = number[var_num];
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		entry->proc_entry = create_proc_entry(entry->name,
2721da177e4SLinus Torvalds 				0644, numbered_dir);
273*16b7f4dcSJan-Benedict Glaw 		if (!entry->proc_entry)
2741da177e4SLinus Torvalds 			goto cleanup;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		entry->id			= var_num;
2771da177e4SLinus Torvalds 		entry->proc_entry->data		= (void *) entry;
2781da177e4SLinus Torvalds 		entry->proc_entry->owner	= THIS_MODULE;
2791da177e4SLinus Torvalds 		entry->proc_entry->read_proc	= srm_env_read;
2801da177e4SLinus Torvalds 		entry->proc_entry->write_proc	= srm_env_write;
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
2841da177e4SLinus Torvalds 			VERSION);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	return 0;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds cleanup:
2891da177e4SLinus Torvalds 	srm_env_cleanup();
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	return -ENOMEM;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static void __exit
2951da177e4SLinus Torvalds srm_env_exit(void)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	srm_env_cleanup();
2981da177e4SLinus Torvalds 	printk(KERN_INFO "%s: unloaded successfully\n", NAME);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	return;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds module_init(srm_env_init);
3041da177e4SLinus Torvalds module_exit(srm_env_exit);
305