srm_env.c 6.8 KB

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