srm_env.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 a modified version of Erik Mouw's example proc
  8. * interface, so: thank you, Erik! He can be reached via email at
  9. * <J.A.K.Mouw@its.tudelft.nl>. It is based on an idea
  10. * provided by DEC^WCompaq^WIntel's "Jumpstart" CD. They
  11. * included a patch like this as well. Thanks for idea!
  12. *
  13. * This program is free software; you can redistribute
  14. * it and/or modify it under the terms of the GNU General
  15. * Public License version 2 as published by the Free Software
  16. * Foundation.
  17. *
  18. * This program is distributed in the hope that it will be
  19. * useful, but WITHOUT ANY WARRANTY; without even the implied
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  21. * PURPOSE. See the GNU General Public License for more
  22. * details.
  23. *
  24. * You should have received a copy of the GNU General Public
  25. * License along with this program; if not, write to the
  26. * Free Software Foundation, Inc., 59 Temple Place,
  27. * Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/gfp.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/seq_file.h>
  36. #include <asm/console.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/machvec.h>
  39. #define BASE_DIR "srm_environment" /* Subdir in /proc/ */
  40. #define NAMED_DIR "named_variables" /* Subdir for known variables */
  41. #define NUMBERED_DIR "numbered_variables" /* Subdir for all variables */
  42. #define VERSION "0.0.6" /* Module version */
  43. #define NAME "srm_env" /* Module name */
  44. MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
  45. MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
  46. MODULE_LICENSE("GPL");
  47. typedef struct _srm_env {
  48. char *name;
  49. unsigned long id;
  50. } srm_env_t;
  51. static struct proc_dir_entry *base_dir;
  52. static struct proc_dir_entry *named_dir;
  53. static struct proc_dir_entry *numbered_dir;
  54. static srm_env_t srm_named_entries[] = {
  55. { "auto_action", ENV_AUTO_ACTION },
  56. { "boot_dev", ENV_BOOT_DEV },
  57. { "bootdef_dev", ENV_BOOTDEF_DEV },
  58. { "booted_dev", ENV_BOOTED_DEV },
  59. { "boot_file", ENV_BOOT_FILE },
  60. { "booted_file", ENV_BOOTED_FILE },
  61. { "boot_osflags", ENV_BOOT_OSFLAGS },
  62. { "booted_osflags", ENV_BOOTED_OSFLAGS },
  63. { "boot_reset", ENV_BOOT_RESET },
  64. { "dump_dev", ENV_DUMP_DEV },
  65. { "enable_audit", ENV_ENABLE_AUDIT },
  66. { "license", ENV_LICENSE },
  67. { "char_set", ENV_CHAR_SET },
  68. { "language", ENV_LANGUAGE },
  69. { "tty_dev", ENV_TTY_DEV },
  70. { NULL, 0 },
  71. };
  72. static int srm_env_proc_show(struct seq_file *m, void *v)
  73. {
  74. unsigned long ret;
  75. unsigned long id = (unsigned long)m->private;
  76. char *page;
  77. page = (char *)__get_free_page(GFP_USER);
  78. if (!page)
  79. return -ENOMEM;
  80. ret = callback_getenv(id, page, PAGE_SIZE);
  81. if ((ret >> 61) == 0) {
  82. seq_write(m, page, ret);
  83. ret = 0;
  84. } else
  85. ret = -EFAULT;
  86. free_page((unsigned long)page);
  87. return ret;
  88. }
  89. static int srm_env_proc_open(struct inode *inode, struct file *file)
  90. {
  91. return single_open(file, srm_env_proc_show, PDE_DATA(inode));
  92. }
  93. static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer,
  94. size_t count, loff_t *pos)
  95. {
  96. int res;
  97. unsigned long id = (unsigned long)PDE_DATA(file_inode(file));
  98. char *buf = (char *) __get_free_page(GFP_USER);
  99. unsigned long ret1, ret2;
  100. if (!buf)
  101. return -ENOMEM;
  102. res = -EINVAL;
  103. if (count >= PAGE_SIZE)
  104. goto out;
  105. res = -EFAULT;
  106. if (copy_from_user(buf, buffer, count))
  107. goto out;
  108. buf[count] = '\0';
  109. ret1 = callback_setenv(id, buf, count);
  110. if ((ret1 >> 61) == 0) {
  111. do
  112. ret2 = callback_save_env();
  113. while((ret2 >> 61) == 1);
  114. res = (int) ret1;
  115. }
  116. out:
  117. free_page((unsigned long)buf);
  118. return res;
  119. }
  120. static const struct file_operations srm_env_proc_fops = {
  121. .owner = THIS_MODULE,
  122. .open = srm_env_proc_open,
  123. .read = seq_read,
  124. .llseek = seq_lseek,
  125. .release = single_release,
  126. .write = srm_env_proc_write,
  127. };
  128. static int __init
  129. srm_env_init(void)
  130. {
  131. srm_env_t *entry;
  132. unsigned long var_num;
  133. /*
  134. * Check system
  135. */
  136. if (!alpha_using_srm) {
  137. printk(KERN_INFO "%s: This Alpha system doesn't "
  138. "know about SRM (or you've booted "
  139. "SRM->MILO->Linux, which gets "
  140. "misdetected)...\n", __func__);
  141. return -ENODEV;
  142. }
  143. /*
  144. * Create base directory
  145. */
  146. base_dir = proc_mkdir(BASE_DIR, NULL);
  147. if (!base_dir) {
  148. printk(KERN_ERR "Couldn't create base dir /proc/%s\n",
  149. BASE_DIR);
  150. return -ENOMEM;
  151. }
  152. /*
  153. * Create per-name subdirectory
  154. */
  155. named_dir = proc_mkdir(NAMED_DIR, base_dir);
  156. if (!named_dir) {
  157. printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
  158. BASE_DIR, NAMED_DIR);
  159. goto cleanup;
  160. }
  161. /*
  162. * Create per-number subdirectory
  163. */
  164. numbered_dir = proc_mkdir(NUMBERED_DIR, base_dir);
  165. if (!numbered_dir) {
  166. printk(KERN_ERR "Couldn't create dir /proc/%s/%s\n",
  167. BASE_DIR, NUMBERED_DIR);
  168. goto cleanup;
  169. }
  170. /*
  171. * Create all named nodes
  172. */
  173. entry = srm_named_entries;
  174. while (entry->name && entry->id) {
  175. if (!proc_create_data(entry->name, 0644, named_dir,
  176. &srm_env_proc_fops, (void *)entry->id))
  177. goto cleanup;
  178. entry++;
  179. }
  180. /*
  181. * Create all numbered nodes
  182. */
  183. for (var_num = 0; var_num <= 255; var_num++) {
  184. char name[4];
  185. sprintf(name, "%ld", var_num);
  186. if (!proc_create_data(name, 0644, numbered_dir,
  187. &srm_env_proc_fops, (void *)var_num))
  188. goto cleanup;
  189. }
  190. printk(KERN_INFO "%s: version %s loaded successfully\n", NAME,
  191. VERSION);
  192. return 0;
  193. cleanup:
  194. remove_proc_subtree(BASE_DIR, NULL);
  195. return -ENOMEM;
  196. }
  197. static void __exit
  198. srm_env_exit(void)
  199. {
  200. remove_proc_subtree(BASE_DIR, NULL);
  201. printk(KERN_INFO "%s: unloaded successfully\n", NAME);
  202. }
  203. module_init(srm_env_init);
  204. module_exit(srm_env_exit);