srm_env.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * 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/module.h>
  59. #include <linux/init.h>
  60. #include <linux/proc_fs.h>
  61. #include <asm/console.h>
  62. #include <asm/uaccess.h>
  63. #include <asm/machvec.h>
  64. #define BASE_DIR "srm_environment" /* Subdir in /proc/ */
  65. #define NAMED_DIR "named_variables" /* Subdir for known variables */
  66. #define NUMBERED_DIR "numbered_variables" /* Subdir for all variables */
  67. #define VERSION "0.0.5" /* Module version */
  68. #define NAME "srm_env" /* Module name */
  69. MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
  70. MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
  71. MODULE_LICENSE("GPL");
  72. typedef struct _srm_env {
  73. char *name;
  74. unsigned long id;
  75. struct proc_dir_entry *proc_entry;
  76. } srm_env_t;
  77. static struct proc_dir_entry *base_dir;
  78. static struct proc_dir_entry *named_dir;
  79. static struct proc_dir_entry *numbered_dir;
  80. static char number[256][4];
  81. static srm_env_t srm_named_entries[] = {
  82. { "auto_action", ENV_AUTO_ACTION },
  83. { "boot_dev", ENV_BOOT_DEV },
  84. { "bootdef_dev", ENV_BOOTDEF_DEV },
  85. { "booted_dev", ENV_BOOTED_DEV },
  86. { "boot_file", ENV_BOOT_FILE },
  87. { "booted_file", ENV_BOOTED_FILE },
  88. { "boot_osflags", ENV_BOOT_OSFLAGS },
  89. { "booted_osflags", ENV_BOOTED_OSFLAGS },
  90. { "boot_reset", ENV_BOOT_RESET },
  91. { "dump_dev", ENV_DUMP_DEV },
  92. { "enable_audit", ENV_ENABLE_AUDIT },
  93. { "license", ENV_LICENSE },
  94. { "char_set", ENV_CHAR_SET },
  95. { "language", ENV_LANGUAGE },
  96. { "tty_dev", ENV_TTY_DEV },
  97. { NULL, 0 },
  98. };
  99. static srm_env_t srm_numbered_entries[256];
  100. static int
  101. srm_env_read(char *page, char **start, off_t off, int count, int *eof,
  102. void *data)
  103. {
  104. int nbytes;
  105. unsigned long ret;
  106. srm_env_t *entry;
  107. if(off != 0)
  108. return -EFAULT;
  109. entry = (srm_env_t *) data;
  110. ret = callback_getenv(entry->id, page, count);
  111. if((ret >> 61) == 0)
  112. nbytes = (int) ret;
  113. else
  114. nbytes = -EFAULT;
  115. return nbytes;
  116. }
  117. static int
  118. srm_env_write(struct file *file, const char __user *buffer, unsigned long count,
  119. void *data)
  120. {
  121. int res;
  122. srm_env_t *entry;
  123. char *buf = (char *) __get_free_page(GFP_USER);
  124. unsigned long ret1, ret2;
  125. entry = (srm_env_t *) data;
  126. if (!buf)
  127. return -ENOMEM;
  128. res = -EINVAL;
  129. if (count >= PAGE_SIZE)
  130. goto out;
  131. res = -EFAULT;
  132. if (copy_from_user(buf, buffer, count))
  133. goto out;
  134. buf[count] = '\0';
  135. ret1 = callback_setenv(entry->id, buf, count);
  136. if ((ret1 >> 61) == 0) {
  137. do
  138. ret2 = callback_save_env();
  139. while((ret2 >> 61) == 1);
  140. res = (int) ret1;
  141. }
  142. out:
  143. free_page((unsigned long)buf);
  144. return res;
  145. }
  146. static void
  147. srm_env_cleanup(void)
  148. {
  149. srm_env_t *entry;
  150. unsigned long var_num;
  151. if(base_dir) {
  152. /*
  153. * Remove named entries
  154. */
  155. if(named_dir) {
  156. entry = srm_named_entries;
  157. while(entry->name != NULL && entry->id != 0) {
  158. if(entry->proc_entry) {
  159. remove_proc_entry(entry->name,
  160. named_dir);
  161. entry->proc_entry = NULL;
  162. }
  163. entry++;
  164. }
  165. remove_proc_entry(NAMED_DIR, base_dir);
  166. }
  167. /*
  168. * Remove numbered entries
  169. */
  170. if(numbered_dir) {
  171. for(var_num = 0; var_num <= 255; var_num++) {
  172. entry = &srm_numbered_entries[var_num];
  173. if(entry->proc_entry) {
  174. remove_proc_entry(entry->name,
  175. numbered_dir);
  176. entry->proc_entry = NULL;
  177. entry->name = NULL;
  178. }
  179. }
  180. remove_proc_entry(NUMBERED_DIR, base_dir);
  181. }
  182. remove_proc_entry(BASE_DIR, NULL);
  183. }
  184. return;
  185. }
  186. static int __init
  187. srm_env_init(void)
  188. {
  189. srm_env_t *entry;
  190. unsigned long var_num;
  191. /*
  192. * Check system
  193. */
  194. if(!alpha_using_srm) {
  195. printk(KERN_INFO "%s: This Alpha system doesn't "
  196. "know about SRM (or you've booted "
  197. "SRM->MILO->Linux, which gets "
  198. "misdetected)...\n", __FUNCTION__);
  199. return -ENODEV;
  200. }
  201. /*
  202. * Init numbers
  203. */
  204. for(var_num = 0; var_num <= 255; var_num++)
  205. sprintf(number[var_num], "%ld", var_num);
  206. /*
  207. * Create base directory
  208. */
  209. base_dir = proc_mkdir(BASE_DIR, NULL);
  210. if(base_dir == NULL) {
  211. printk(KERN_ERR "Couldn't create base dir /proc/%s\n",
  212. BASE_DIR);
  213. goto cleanup;
  214. }
  215. base_dir->owner = THIS_MODULE;
  216. /*
  217. * Create per-name subdirectory
  218. */
  219. named_dir = proc_mkdir(NAMED_DIR, base_dir);
  220. if(named_dir == NULL) {
  221. printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
  222. BASE_DIR, NAMED_DIR);
  223. goto cleanup;
  224. }
  225. named_dir->owner = THIS_MODULE;
  226. /*
  227. * Create per-number subdirectory
  228. */
  229. numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
  230. if(numbered_dir == NULL) {
  231. printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
  232. BASE_DIR, NUMBERED_DIR);
  233. goto cleanup;
  234. }
  235. numbered_dir->owner = THIS_MODULE;
  236. /*
  237. * Create all named nodes
  238. */
  239. entry = srm_named_entries;
  240. while(entry->name != NULL && entry->id != 0) {
  241. entry->proc_entry = create_proc_entry(entry->name,
  242. 0644, named_dir);
  243. if(entry->proc_entry == NULL)
  244. goto cleanup;
  245. entry->proc_entry->data = (void *) entry;
  246. entry->proc_entry->owner = THIS_MODULE;
  247. entry->proc_entry->read_proc = srm_env_read;
  248. entry->proc_entry->write_proc = srm_env_write;
  249. entry++;
  250. }
  251. /*
  252. * Create all numbered nodes
  253. */
  254. for(var_num = 0; var_num <= 255; var_num++) {
  255. entry = &srm_numbered_entries[var_num];
  256. entry->name = number[var_num];
  257. entry->proc_entry = create_proc_entry(entry->name,
  258. 0644, numbered_dir);
  259. if(entry->proc_entry == NULL)
  260. goto cleanup;
  261. entry->id = var_num;
  262. entry->proc_entry->data = (void *) entry;
  263. entry->proc_entry->owner = THIS_MODULE;
  264. entry->proc_entry->read_proc = srm_env_read;
  265. entry->proc_entry->write_proc = srm_env_write;
  266. }
  267. printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
  268. VERSION);
  269. return 0;
  270. cleanup:
  271. srm_env_cleanup();
  272. return -ENOMEM;
  273. }
  274. static void __exit
  275. srm_env_exit(void)
  276. {
  277. srm_env_cleanup();
  278. printk(KERN_INFO "%s: unloaded successfully\n", NAME);
  279. return;
  280. }
  281. module_init(srm_env_init);
  282. module_exit(srm_env_exit);