srm_env.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:5k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * srm_env.c - Access to SRC environment variables through
  3.  *             the linux procfs
  4.  *
  5.  * (C)2001, 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: thanky
  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's "Jumpstart" CD. They included
  12.  * a patch like this as well. Thanks for idea!
  13.  *
  14.  *
  15.  * This software has been developed while working on the LART
  16.  * computing board (http://www.lart.tudelft.nl/). The
  17.  * development has been sponsored by the Mobile Multi-media
  18.  * Communications (http://www.mmc.tudelft.nl/) and Ubiquitous
  19.  * Communications (http://www.ubicom.tudelft.nl/) projects.
  20.  *
  21.  * This program is free software; you can redistribute
  22.  * it and/or modify it under the terms of the GNU General
  23.  * Public License as published by the Free Software
  24.  * Foundation version 2.
  25.  *
  26.  * This program is distributed in the hope that it will be
  27.  * useful, but WITHOUT ANY WARRANTY; without even the implied
  28.  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  29.  * PURPOSE.  See the GNU General Public License for more
  30.  * details.
  31.  * 
  32.  * You should have received a copy of the GNU General Public
  33.  * License along with this program; if not, write to the
  34.  * Free Software Foundation, Inc., 59 Temple Place,
  35.  * Suite 330, Boston, MA  02111-1307  USA
  36.  *
  37.  */
  38. #include <linux/kernel.h>
  39. #include <linux/config.h>
  40. #include <linux/module.h>
  41. #include <linux/init.h>
  42. #include <linux/proc_fs.h>
  43. #include <asm/console.h>
  44. #include <asm/uaccess.h>
  45. #define DIRNAME "srm_environment" /* Subdir in /proc/ */
  46. #define VERSION "0.0.2" /* Module version */
  47. #define NAME "srm_env" /* Module name */
  48. #define DEBUG
  49. MODULE_AUTHOR("Jan-Benedict Glaw <jbglaw@lug-owl.de>");
  50. MODULE_DESCRIPTION("Accessing Alpha SRM environment through procfs interface");
  51. MODULE_LICENSE("GPL");
  52. EXPORT_NO_SYMBOLS;
  53. typedef struct _srm_env {
  54. char *name;
  55. unsigned long id;
  56. struct proc_dir_entry *proc_entry;
  57. } srm_env_t;
  58. static struct proc_dir_entry *directory;
  59. static srm_env_t srm_entries[] = {
  60. { "auto_action", ENV_AUTO_ACTION },
  61. { "boot_dev", ENV_BOOT_DEV },
  62. { "bootdef_dev", ENV_BOOTDEF_DEV },
  63. { "booted_dev", ENV_BOOTED_DEV },
  64. { "boot_file", ENV_BOOT_FILE },
  65. { "booted_file", ENV_BOOTED_FILE },
  66. { "boot_osflags", ENV_BOOT_OSFLAGS },
  67. { "booted_osflags", ENV_BOOTED_OSFLAGS },
  68. { "boot_reset", ENV_BOOT_RESET },
  69. { "dump_dev", ENV_DUMP_DEV },
  70. { "enable_audit", ENV_ENABLE_AUDIT },
  71. { "license", ENV_LICENSE },
  72. { "char_set", ENV_CHAR_SET },
  73. { "language", ENV_LANGUAGE },
  74. { "tty_dev", ENV_TTY_DEV },
  75. { NULL, 0 },
  76. };
  77. static int srm_env_read(char *page, char **start, off_t off, int count,
  78. int *eof, void *data)
  79. {
  80. int nbytes;
  81. unsigned long ret;
  82. srm_env_t *entry;
  83. MOD_INC_USE_COUNT;
  84. if(off != 0) {
  85. MOD_DEC_USE_COUNT;
  86. return -EFAULT;
  87. }
  88. entry = (srm_env_t *)data;
  89. ret = callback_getenv(entry->id, page, count);
  90. if((ret >> 61) == 0)
  91. nbytes = (int)ret;
  92. else
  93. nbytes = -EFAULT;
  94. MOD_DEC_USE_COUNT;
  95. return nbytes;
  96. }
  97. static int srm_env_write(struct file *file, const char *buffer,
  98. unsigned long count, void *data)
  99. {
  100. #define BUFLEN 512
  101. int nbytes;
  102. srm_env_t *entry;
  103. char buf[BUFLEN];
  104. unsigned long ret1, ret2;
  105. MOD_INC_USE_COUNT;
  106. entry = (srm_env_t *) data;
  107. nbytes = strlen(buffer) + 1;
  108. if(nbytes > BUFLEN) {
  109. MOD_DEC_USE_COUNT;
  110. return -ENOMEM;
  111. }
  112. //memcpy(aligned_buffer, buffer, nbytes)
  113. if(copy_from_user(buf, buffer, count)) {
  114. MOD_DEC_USE_COUNT;
  115. return -EFAULT;
  116. }
  117. buf[count] = 0x00;
  118. ret1 = callback_setenv(entry->id, buf, count);
  119. if((ret1 >> 61) == 0) {
  120. do 
  121. ret2 = callback_save_env();
  122. while((ret2 >> 61) == 1);
  123. nbytes = (int)ret1;
  124. } else
  125. nbytes = -EFAULT;
  126. MOD_DEC_USE_COUNT;
  127. return nbytes;
  128. }
  129. static void srm_env_cleanup(void)
  130. {
  131. srm_env_t *entry;
  132. if(directory) {
  133. entry = srm_entries;
  134. while(entry->name != NULL && entry->id != 0) {
  135. if(entry->proc_entry) {
  136. remove_proc_entry(entry->name, directory);
  137. entry->proc_entry = NULL;
  138. }
  139. entry++;
  140. }
  141. remove_proc_entry(DIRNAME, NULL);
  142. }
  143. return;
  144. }
  145. static int __init srm_env_init(void)
  146. {
  147. srm_env_t *entry;
  148. if(!alpha_using_srm) {
  149. printk(KERN_INFO "%s: This Alpha system doesn't "
  150. "know about SRM...n", __FUNCTION__);
  151. return -ENODEV;
  152. }
  153. directory = proc_mkdir(DIRNAME, NULL);
  154. if(directory == NULL)
  155. return -ENOMEM;
  156. directory->owner = THIS_MODULE;
  157. /* Now create all the nodes... */
  158. entry = srm_entries;
  159. while(entry->name != NULL && entry->id != 0) {
  160. entry->proc_entry = create_proc_entry(entry->name, 0644,
  161. directory);
  162. if(entry->proc_entry == NULL)
  163. goto cleanup;
  164. entry->proc_entry->data = entry;
  165. entry->proc_entry->read_proc = srm_env_read;
  166. entry->proc_entry->write_proc = srm_env_write;
  167. entry->proc_entry->owner = THIS_MODULE;
  168. entry++;
  169. }
  170. printk(KERN_INFO "%s: version %s loaded successfullyn", NAME,
  171. VERSION);
  172. return 0;
  173. cleanup:
  174. srm_env_cleanup();
  175. return -ENOMEM;
  176. }
  177. static void __exit srm_env_exit(void)
  178. {
  179. srm_env_cleanup();
  180. printk(KERN_INFO "%s: unloaded successfullyn", NAME);
  181. return;
  182. }
  183. module_init(srm_env_init);
  184. module_exit(srm_env_exit);