srm_env.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * srm_env.c - Access to SRM environment
  3. * variables through linux' procfs
  4. *
  5. * (C) 2001,2002,2006 by 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. #include <linux/kernel.h>
  32. #include <linux/gfp.h>
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/seq_file.h>
  37. #include <asm/console.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/machvec.h>
  40. #define BASE_DIR "srm_environment" /* Subdir in /proc/ */
  41. #define NAMED_DIR "named_variables" /* Subdir for known variables */
  42. #define NUMBERED_DIR "numbered_variables" /* Subdir for all variables */
  43. #define VERSION "0.0.6" /* Module version */
  44. #define NAME "srm_env" /* Module name */
  45. MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
  46. MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
  47. MODULE_LICENSE("GPL");
  48. typedef struct _srm_env {
  49. char *name;
  50. unsigned long id;
  51. struct proc_dir_entry *proc_entry;
  52. } srm_env_t;
  53. static struct proc_dir_entry *base_dir;
  54. static struct proc_dir_entry *named_dir;
  55. static struct proc_dir_entry *numbered_dir;
  56. static char number[256][4];
  57. static srm_env_t srm_named_entries[] = {
  58. { "auto_action", ENV_AUTO_ACTION },
  59. { "boot_dev", ENV_BOOT_DEV },
  60. { "bootdef_dev", ENV_BOOTDEF_DEV },
  61. { "booted_dev", ENV_BOOTED_DEV },
  62. { "boot_file", ENV_BOOT_FILE },
  63. { "booted_file", ENV_BOOTED_FILE },
  64. { "boot_osflags", ENV_BOOT_OSFLAGS },
  65. { "booted_osflags", ENV_BOOTED_OSFLAGS },
  66. { "boot_reset", ENV_BOOT_RESET },
  67. { "dump_dev", ENV_DUMP_DEV },
  68. { "enable_audit", ENV_ENABLE_AUDIT },
  69. { "license", ENV_LICENSE },
  70. { "char_set", ENV_CHAR_SET },
  71. { "language", ENV_LANGUAGE },
  72. { "tty_dev", ENV_TTY_DEV },
  73. { NULL, 0 },
  74. };
  75. static srm_env_t srm_numbered_entries[256];
  76. static int srm_env_proc_show(struct seq_file *m, void *v)
  77. {
  78. unsigned long ret;
  79. srm_env_t *entry;
  80. char *page;
  81. entry = m->private;
  82. page = (char *)__get_free_page(GFP_USER);
  83. if (!page)
  84. return -ENOMEM;
  85. ret = callback_getenv(entry->id, page, PAGE_SIZE);
  86. if ((ret >> 61) == 0) {
  87. seq_write(m, page, ret);
  88. ret = 0;
  89. } else
  90. ret = -EFAULT;
  91. free_page((unsigned long)page);
  92. return ret;
  93. }
  94. static int srm_env_proc_open(struct inode *inode, struct file *file)
  95. {
  96. return single_open(file, srm_env_proc_show, PDE(inode)->data);
  97. }
  98. static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer,
  99. size_t count, loff_t *pos)
  100. {
  101. int res;
  102. srm_env_t *entry = PDE(file->f_path.dentry->d_inode)->data;
  103. char *buf = (char *) __get_free_page(GFP_USER);
  104. unsigned long ret1, ret2;
  105. if (!buf)
  106. return -ENOMEM;
  107. res = -EINVAL;
  108. if (count >= PAGE_SIZE)
  109. goto out;
  110. res = -EFAULT;
  111. if (copy_from_user(buf, buffer, count))
  112. goto out;
  113. buf[count] = '\0';
  114. ret1 = callback_setenv(entry->id, buf, count);
  115. if ((ret1 >> 61) == 0) {
  116. do
  117. ret2 = callback_save_env();
  118. while((ret2 >> 61) == 1);
  119. res = (int) ret1;
  120. }
  121. out:
  122. free_page((unsigned long)buf);
  123. return res;
  124. }
  125. static const struct file_operations srm_env_proc_fops = {
  126. .owner = THIS_MODULE,
  127. .open = srm_env_proc_open,
  128. .read = seq_read,
  129. .llseek = seq_lseek,
  130. .release = single_release,
  131. .write = srm_env_proc_write,
  132. };
  133. static void
  134. srm_env_cleanup(void)
  135. {
  136. srm_env_t *entry;
  137. unsigned long var_num;
  138. if (base_dir) {
  139. /*
  140. * Remove named entries
  141. */
  142. if (named_dir) {
  143. entry = srm_named_entries;
  144. while (entry->name != NULL && entry->id != 0) {
  145. if (entry->proc_entry) {
  146. remove_proc_entry(entry->name,
  147. named_dir);
  148. entry->proc_entry = NULL;
  149. }
  150. entry++;
  151. }
  152. remove_proc_entry(NAMED_DIR, base_dir);
  153. }
  154. /*
  155. * Remove numbered entries
  156. */
  157. if (numbered_dir) {
  158. for (var_num = 0; var_num <= 255; var_num++) {
  159. entry = &srm_numbered_entries[var_num];
  160. if (entry->proc_entry) {
  161. remove_proc_entry(entry->name,
  162. numbered_dir);
  163. entry->proc_entry = NULL;
  164. entry->name = NULL;
  165. }
  166. }
  167. remove_proc_entry(NUMBERED_DIR, base_dir);
  168. }
  169. remove_proc_entry(BASE_DIR, NULL);
  170. }
  171. return;
  172. }
  173. static int __init
  174. srm_env_init(void)
  175. {
  176. srm_env_t *entry;
  177. unsigned long var_num;
  178. /*
  179. * Check system
  180. */
  181. if (!alpha_using_srm) {
  182. printk(KERN_INFO "%s: This Alpha system doesn't "
  183. "know about SRM (or you've booted "
  184. "SRM->MILO->Linux, which gets "
  185. "misdetected)...\n", __func__);
  186. return -ENODEV;
  187. }
  188. /*
  189. * Init numbers
  190. */
  191. for (var_num = 0; var_num <= 255; var_num++)
  192. sprintf(number[var_num], "%ld", var_num);
  193. /*
  194. * Create base directory
  195. */
  196. base_dir = proc_mkdir(BASE_DIR, NULL);
  197. if (!base_dir) {
  198. printk(KERN_ERR "Couldn't create base dir /proc/%s\n",
  199. BASE_DIR);
  200. goto cleanup;
  201. }
  202. /*
  203. * Create per-name subdirectory
  204. */
  205. named_dir = proc_mkdir(NAMED_DIR, base_dir);
  206. if (!named_dir) {
  207. printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
  208. BASE_DIR, NAMED_DIR);
  209. goto cleanup;
  210. }
  211. /*
  212. * Create per-number subdirectory
  213. */
  214. numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
  215. if (!numbered_dir) {
  216. printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
  217. BASE_DIR, NUMBERED_DIR);
  218. goto cleanup;
  219. }
  220. /*
  221. * Create all named nodes
  222. */
  223. entry = srm_named_entries;
  224. while (entry->name && entry->id) {
  225. entry->proc_entry = proc_create_data(entry->name, 0644, named_dir,
  226. &srm_env_proc_fops, entry);
  227. if (!entry->proc_entry)
  228. goto cleanup;
  229. entry++;
  230. }
  231. /*
  232. * Create all numbered nodes
  233. */
  234. for (var_num = 0; var_num <= 255; var_num++) {
  235. entry = &srm_numbered_entries[var_num];
  236. entry->name = number[var_num];
  237. entry->proc_entry = proc_create_data(entry->name, 0644, numbered_dir,
  238. &srm_env_proc_fops, entry);
  239. if (!entry->proc_entry)
  240. goto cleanup;
  241. entry->id = var_num;
  242. }
  243. printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
  244. VERSION);
  245. return 0;
  246. cleanup:
  247. srm_env_cleanup();
  248. return -ENOMEM;
  249. }
  250. static void __exit
  251. srm_env_exit(void)
  252. {
  253. srm_env_cleanup();
  254. printk(KERN_INFO "%s: unloaded successfully\n", NAME);
  255. return;
  256. }
  257. module_init(srm_env_init);
  258. module_exit(srm_env_exit);