srm_env.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:8k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * srm_env.c - Access to SRM environment
  3.  *             variables through linux' procfs
  4.  *
  5.  * Copyright (C) 2001-2002 Jan-Benedict Glaw <jbglaw@lug-owl.de>
  6.  *
  7.  * This driver is at all a modified version of Erik Mouw's
  8.  * ./linux/Documentation/DocBook/procfs_example.c, so: thank
  9.  * you, Erik! He can be reached via email at
  10.  * <J.A.K.Mouw@its.tudelft.nl>. It is based on an idea
  11.  * provided by DEC^WCompaq^WIntel's "Jumpstart" CD. They
  12.  * included a patch like this as well. Thanks for idea!
  13.  *
  14.  * This program is free software; you can redistribute
  15.  * it and/or modify it under the terms of the GNU General
  16.  * Public License version 2 as published by the Free Software
  17.  * Foundation.
  18.  *
  19.  * This program is distributed in the hope that it will be
  20.  * useful, but WITHOUT ANY WARRANTY; without even the implied
  21.  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  22.  * PURPOSE.  See the GNU General Public License for more
  23.  * details.
  24.  * 
  25.  * You should have received a copy of the GNU General Public
  26.  * License along with this program; if not, write to the
  27.  * Free Software Foundation, Inc., 59 Temple Place,
  28.  * Suite 330, Boston, MA  02111-1307  USA
  29.  *
  30.  */
  31. /*
  32.  * Changelog
  33.  * ~~~~~~~~~
  34.  *
  35.  * Thu, 22 Aug 2002 15:10:43 +0200
  36.  *  - Update Config.help entry. I got a number of emails asking
  37.  *    me to tell their senders if they could make use of this
  38.  *    piece of code... So: "SRM is something like BIOS for your
  39.  *    Alpha"
  40.  *  - Update code formatting a bit to better conform CodingStyle
  41.  *    rules.
  42.  *  - So this is v0.0.5, with no changes (except formatting)
  43.  * 
  44.  * Wed, 22 May 2002 00:11:21 +0200
  45.  *  - Fix typo on comment (SRC -> SRM)
  46.  *  - Call this "Version 0.0.4"
  47.  *
  48.  * Tue,  9 Apr 2002 18:44:40 +0200
  49.  *  - Implement access by variable name and additionally
  50.  *    by number. This is done by creating two subdirectories
  51.  *    where one holds all names (like the old directory
  52.  *    did) and the other holding 256 files named like "0",
  53.  *    "1" and so on.
  54.  *  - Call this "Version 0.0.3"
  55.  *
  56.  */
  57. #include <linux/kernel.h>
  58. #include <linux/config.h>
  59. #include <linux/module.h>
  60. #include <linux/init.h>
  61. #include <linux/proc_fs.h>
  62. #include <asm/console.h>
  63. #include <asm/uaccess.h>
  64. #include <asm/machvec.h>
  65. #define BASE_DIR "srm_environment" /* Subdir in /proc/ */
  66. #define NAMED_DIR "named_variables" /* Subdir for known variables */
  67. #define NUMBERED_DIR "numbered_variables" /* Subdir for all variables */
  68. #define VERSION "0.0.5" /* Module version */
  69. #define NAME "srm_env" /* Module name */
  70. MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
  71. MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
  72. MODULE_LICENSE("GPL");
  73. typedef struct _srm_env {
  74. char *name;
  75. unsigned long id;
  76. struct proc_dir_entry *proc_entry;
  77. } srm_env_t;
  78. static struct proc_dir_entry *base_dir;
  79. static struct proc_dir_entry *named_dir;
  80. static struct proc_dir_entry *numbered_dir;
  81. static char number[256][4];
  82. static srm_env_t srm_named_entries[] = {
  83. { "auto_action", ENV_AUTO_ACTION },
  84. { "boot_dev", ENV_BOOT_DEV },
  85. { "bootdef_dev", ENV_BOOTDEF_DEV },
  86. { "booted_dev", ENV_BOOTED_DEV },
  87. { "boot_file", ENV_BOOT_FILE },
  88. { "booted_file", ENV_BOOTED_FILE },
  89. { "boot_osflags", ENV_BOOT_OSFLAGS },
  90. { "booted_osflags", ENV_BOOTED_OSFLAGS },
  91. { "boot_reset", ENV_BOOT_RESET },
  92. { "dump_dev", ENV_DUMP_DEV },
  93. { "enable_audit", ENV_ENABLE_AUDIT },
  94. { "license", ENV_LICENSE },
  95. { "char_set", ENV_CHAR_SET },
  96. { "language", ENV_LANGUAGE },
  97. { "tty_dev", ENV_TTY_DEV },
  98. { NULL, 0 },
  99. };
  100. static srm_env_t srm_numbered_entries[256];
  101. static int
  102. srm_env_read(char *page, char **start, off_t off, int count, int *eof,
  103. void *data)
  104. {
  105. int nbytes;
  106. unsigned long ret;
  107. srm_env_t *entry;
  108. MOD_INC_USE_COUNT;
  109. if(off != 0) {
  110. MOD_DEC_USE_COUNT;
  111. return -EFAULT;
  112. }
  113. entry = (srm_env_t *) data;
  114. ret = callback_getenv(entry->id, page, count);
  115. if((ret >> 61) == 0)
  116. nbytes = (int) ret;
  117. else
  118. nbytes = -EFAULT;
  119. MOD_DEC_USE_COUNT;
  120. return nbytes;
  121. }
  122. static int
  123. srm_env_write(struct file *file, const char *buffer, unsigned long count,
  124. void *data)
  125. {
  126. #define BUFLEN 512
  127. int nbytes;
  128. srm_env_t *entry;
  129. char buf[BUFLEN];
  130. unsigned long ret1, ret2;
  131. MOD_INC_USE_COUNT;
  132. entry = (srm_env_t *) data;
  133. nbytes = strlen(buffer) + 1;
  134. if(nbytes > BUFLEN) {
  135. MOD_DEC_USE_COUNT;
  136. return -ENOMEM;
  137. }
  138. /* memcpy(aligned_buffer, buffer, nbytes) */
  139. if(copy_from_user(buf, buffer, count)) {
  140. MOD_DEC_USE_COUNT;
  141. return -EFAULT;
  142. }
  143. buf[count] = 0x00;
  144. ret1 = callback_setenv(entry->id, buf, count);
  145. if((ret1 >> 61) == 0) {
  146. do 
  147. ret2 = callback_save_env();
  148. while((ret2 >> 61) == 1);
  149. nbytes = (int) ret1;
  150. } else
  151. nbytes = -EFAULT;
  152. MOD_DEC_USE_COUNT;
  153. return nbytes;
  154. }
  155. static void
  156. srm_env_cleanup(void)
  157. {
  158. srm_env_t *entry;
  159. unsigned long var_num;
  160. if(base_dir) {
  161. /*
  162.  * Remove named entries
  163.  */
  164. if(named_dir) {
  165. entry = srm_named_entries;
  166. while(entry->name != NULL && entry->id != 0) {
  167. if(entry->proc_entry) {
  168. remove_proc_entry(entry->name,
  169. named_dir);
  170. entry->proc_entry = NULL;
  171. }
  172. entry++;
  173. }
  174. remove_proc_entry(NAMED_DIR, base_dir);
  175. }
  176. /*
  177.  * Remove numbered entries
  178.  */
  179. if(numbered_dir) {
  180. for(var_num = 0; var_num <= 255; var_num++) {
  181. entry = &srm_numbered_entries[var_num];
  182. if(entry->proc_entry) {
  183. remove_proc_entry(entry->name,
  184. numbered_dir);
  185. entry->proc_entry = NULL;
  186. entry->name = NULL;
  187. }
  188. }
  189. remove_proc_entry(NUMBERED_DIR, base_dir);
  190. }
  191. remove_proc_entry(BASE_DIR, NULL);
  192. }
  193. return;
  194. }
  195. static int __init
  196. srm_env_init(void)
  197. {
  198. srm_env_t *entry;
  199. unsigned long var_num;
  200. /*
  201.  * Check system
  202.  */
  203. if(!alpha_using_srm) {
  204. printk(KERN_INFO "%s: This Alpha system doesn't "
  205. "know about SRM (or you've booted "
  206. "SRM->MILO->Linux, which gets "
  207. "misdetected)...n", __FUNCTION__);
  208. return -ENODEV;
  209. }
  210. /*
  211.  * Init numbers
  212.  */
  213. for(var_num = 0; var_num <= 255; var_num++)
  214. sprintf(number[var_num], "%ld", var_num);
  215. /*
  216.  * Create base directory
  217.  */
  218. base_dir = proc_mkdir(BASE_DIR, NULL);
  219. if(base_dir == NULL) {
  220. printk(KERN_ERR "Couldn't create base dir /proc/%sn",
  221. BASE_DIR);
  222. goto cleanup;
  223. }
  224. base_dir->owner = THIS_MODULE;
  225. /*
  226.  * Create per-name subdirectory
  227.  */
  228. named_dir = proc_mkdir(NAMED_DIR, base_dir);
  229. if(named_dir == NULL) {
  230. printk(KERN_ERR "Couldn't create dir /proc/%s/%sn",
  231. BASE_DIR, NAMED_DIR);
  232. goto cleanup;
  233. }
  234. named_dir->owner = THIS_MODULE;
  235. /*
  236.  * Create per-number subdirectory
  237.  */
  238. numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
  239. if(numbered_dir == NULL) {
  240. printk(KERN_ERR "Couldn't create dir /proc/%s/%sn",
  241. BASE_DIR, NUMBERED_DIR);
  242. goto cleanup;
  243. }
  244. numbered_dir->owner = THIS_MODULE;
  245. /*
  246.  * Create all named nodes
  247.  */
  248. entry = srm_named_entries;
  249. while(entry->name != NULL && entry->id != 0) {
  250. entry->proc_entry = create_proc_entry(entry->name,
  251. 0644, named_dir);
  252. if(entry->proc_entry == NULL)
  253. goto cleanup;
  254. entry->proc_entry->data = (void *) entry;
  255. entry->proc_entry->owner = THIS_MODULE;
  256. entry->proc_entry->read_proc = srm_env_read;
  257. entry->proc_entry->write_proc = srm_env_write;
  258. entry++;
  259. }
  260. /*
  261.  * Create all numbered nodes
  262.  */
  263. for(var_num = 0; var_num <= 255; var_num++) {
  264. entry = &srm_numbered_entries[var_num];
  265. entry->name = number[var_num];
  266. entry->proc_entry = create_proc_entry(entry->name,
  267. 0644, numbered_dir);
  268. if(entry->proc_entry == NULL)
  269. goto cleanup;
  270. entry->id = var_num;
  271. entry->proc_entry->data = (void *) entry;
  272. entry->proc_entry->owner = THIS_MODULE;
  273. entry->proc_entry->read_proc = srm_env_read;
  274. entry->proc_entry->write_proc = srm_env_write;
  275. }
  276. printk(KERN_INFO "%s: version %s loaded successfullyn", NAME,
  277. VERSION);
  278. return 0;
  279. cleanup:
  280. srm_env_cleanup();
  281. return -ENOMEM;
  282. }
  283. static void __exit
  284. srm_env_exit(void)
  285. {
  286. srm_env_cleanup();
  287. printk(KERN_INFO "%s: unloaded successfullyn", NAME);
  288. return;
  289. }
  290. module_init(srm_env_init);
  291. module_exit(srm_env_exit);