1 /* Helpers for initial module or kernel cmdline parsing
2 Copyright (C) 2001 Rusty Russell.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <linux/moduleparam.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/ctype.h>
28 #define DEBUGP(fmt, a...)
30 /* Protects all parameters, and incidentally kmalloced_param list. */
31 static DEFINE_MUTEX(param_lock
);
33 /* This just allows us to keep track of which parameters are kmalloced. */
34 struct kmalloced_param
{
35 struct list_head list
;
38 static LIST_HEAD(kmalloced_params
);
40 static void *kmalloc_parameter(unsigned int size
)
42 struct kmalloced_param
*p
;
44 p
= kmalloc(sizeof(*p
) + size
, GFP_KERNEL
);
48 list_add(&p
->list
, &kmalloced_params
);
52 /* Does nothing if parameter wasn't kmalloced above. */
53 static void maybe_kfree_parameter(void *param
)
55 struct kmalloced_param
*p
;
57 list_for_each_entry(p
, &kmalloced_params
, list
) {
58 if (p
->val
== param
) {
66 static inline char dash2underscore(char c
)
73 static inline int parameq(const char *input
, const char *paramname
)
76 for (i
= 0; dash2underscore(input
[i
]) == paramname
[i
]; i
++)
82 static int parse_one(char *param
,
84 const struct kernel_param
*params
,
86 int (*handle_unknown
)(char *param
, char *val
))
92 for (i
= 0; i
< num_params
; i
++) {
93 if (parameq(param
, params
[i
].name
)) {
94 /* Noone handled NULL, so do it here. */
95 if (!val
&& params
[i
].ops
->set
!= param_set_bool
)
97 DEBUGP("They are equal! Calling %p\n",
99 mutex_lock(¶m_lock
);
100 err
= params
[i
].ops
->set(val
, ¶ms
[i
]);
101 mutex_unlock(¶m_lock
);
106 if (handle_unknown
) {
107 DEBUGP("Unknown argument: calling %p\n", handle_unknown
);
108 return handle_unknown(param
, val
);
111 DEBUGP("Unknown argument `%s'\n", param
);
115 /* You can use " around spaces, but can't escape ". */
116 /* Hyphens and underscores equivalent in parameter names. */
117 static char *next_arg(char *args
, char **param
, char **val
)
119 unsigned int i
, equals
= 0;
120 int in_quote
= 0, quoted
= 0;
129 for (i
= 0; args
[i
]; i
++) {
130 if (isspace(args
[i
]) && !in_quote
)
137 in_quote
= !in_quote
;
145 *val
= args
+ equals
+ 1;
147 /* Don't include quotes in value. */
150 if (args
[i
-1] == '"')
153 if (quoted
&& args
[i
-1] == '"')
163 /* Chew up trailing spaces. */
164 return skip_spaces(next
);
167 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
168 int parse_args(const char *name
,
170 const struct kernel_param
*params
,
172 int (*unknown
)(char *param
, char *val
))
176 DEBUGP("Parsing ARGS: %s\n", args
);
178 /* Chew leading spaces */
179 args
= skip_spaces(args
);
183 int irq_was_disabled
;
185 args
= next_arg(args
, ¶m
, &val
);
186 irq_was_disabled
= irqs_disabled();
187 ret
= parse_one(param
, val
, params
, num
, unknown
);
188 if (irq_was_disabled
&& !irqs_disabled()) {
189 printk(KERN_WARNING
"parse_args(): option '%s' enabled "
194 printk(KERN_ERR
"%s: Unknown parameter `%s'\n",
199 "%s: `%s' too large for parameter `%s'\n",
200 name
, val
?: "", param
);
206 "%s: `%s' invalid for parameter `%s'\n",
207 name
, val
?: "", param
);
216 /* Lazy bastard, eh? */
217 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
218 int param_set_##name(const char *val, const struct kernel_param *kp) \
223 ret = strtolfn(val, 0, &l); \
224 if (ret == -EINVAL || ((type)l != l)) \
226 *((type *)kp->arg) = l; \
229 int param_get_##name(char *buffer, const struct kernel_param *kp) \
231 return sprintf(buffer, format, *((type *)kp->arg)); \
233 struct kernel_param_ops param_ops_##name = { \
234 .set = param_set_##name, \
235 .get = param_get_##name, \
237 EXPORT_SYMBOL(param_set_##name); \
238 EXPORT_SYMBOL(param_get_##name); \
239 EXPORT_SYMBOL(param_ops_##name)
242 STANDARD_PARAM_DEF(byte
, unsigned char, "%c", unsigned long, strict_strtoul
);
243 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol
);
244 STANDARD_PARAM_DEF(ushort
, unsigned short, "%hu", unsigned long, strict_strtoul
);
245 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol
);
246 STANDARD_PARAM_DEF(uint
, unsigned int, "%u", unsigned long, strict_strtoul
);
247 STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol
);
248 STANDARD_PARAM_DEF(ulong
, unsigned long, "%lu", unsigned long, strict_strtoul
);
250 int param_set_charp(const char *val
, const struct kernel_param
*kp
)
252 if (strlen(val
) > 1024) {
253 printk(KERN_ERR
"%s: string parameter too long\n",
258 maybe_kfree_parameter(*(char **)kp
->arg
);
260 /* This is a hack. We can't kmalloc in early boot, and we
261 * don't need to; this mangled commandline is preserved. */
262 if (slab_is_available()) {
263 *(char **)kp
->arg
= kmalloc_parameter(strlen(val
)+1);
264 if (!*(char **)kp
->arg
)
266 strcpy(*(char **)kp
->arg
, val
);
268 *(const char **)kp
->arg
= val
;
272 EXPORT_SYMBOL(param_set_charp
);
274 int param_get_charp(char *buffer
, const struct kernel_param
*kp
)
276 return sprintf(buffer
, "%s", *((char **)kp
->arg
));
278 EXPORT_SYMBOL(param_get_charp
);
280 static void param_free_charp(void *arg
)
282 maybe_kfree_parameter(*((char **)arg
));
285 struct kernel_param_ops param_ops_charp
= {
286 .set
= param_set_charp
,
287 .get
= param_get_charp
,
288 .free
= param_free_charp
,
290 EXPORT_SYMBOL(param_ops_charp
);
292 /* Actually could be a bool or an int, for historical reasons. */
293 int param_set_bool(const char *val
, const struct kernel_param
*kp
)
297 /* No equals means "set"... */
300 /* One of =[yYnN01] */
302 case 'y': case 'Y': case '1':
305 case 'n': case 'N': case '0':
312 if (kp
->flags
& KPARAM_ISBOOL
)
313 *(bool *)kp
->arg
= v
;
318 EXPORT_SYMBOL(param_set_bool
);
320 int param_get_bool(char *buffer
, const struct kernel_param
*kp
)
323 if (kp
->flags
& KPARAM_ISBOOL
)
324 val
= *(bool *)kp
->arg
;
326 val
= *(int *)kp
->arg
;
328 /* Y and N chosen as being relatively non-coder friendly */
329 return sprintf(buffer
, "%c", val
? 'Y' : 'N');
331 EXPORT_SYMBOL(param_get_bool
);
333 struct kernel_param_ops param_ops_bool
= {
334 .set
= param_set_bool
,
335 .get
= param_get_bool
,
337 EXPORT_SYMBOL(param_ops_bool
);
339 /* This one must be bool. */
340 int param_set_invbool(const char *val
, const struct kernel_param
*kp
)
344 struct kernel_param dummy
;
346 dummy
.arg
= &boolval
;
347 dummy
.flags
= KPARAM_ISBOOL
;
348 ret
= param_set_bool(val
, &dummy
);
350 *(bool *)kp
->arg
= !boolval
;
353 EXPORT_SYMBOL(param_set_invbool
);
355 int param_get_invbool(char *buffer
, const struct kernel_param
*kp
)
357 return sprintf(buffer
, "%c", (*(bool *)kp
->arg
) ? 'N' : 'Y');
359 EXPORT_SYMBOL(param_get_invbool
);
361 struct kernel_param_ops param_ops_invbool
= {
362 .set
= param_set_invbool
,
363 .get
= param_get_invbool
,
365 EXPORT_SYMBOL(param_ops_invbool
);
367 /* We break the rule and mangle the string. */
368 static int param_array(const char *name
,
370 unsigned int min
, unsigned int max
,
371 void *elem
, int elemsize
,
372 int (*set
)(const char *, const struct kernel_param
*kp
),
377 struct kernel_param kp
;
380 /* Get the name right for errors. */
386 /* We expect a comma-separated list of values. */
391 printk(KERN_ERR
"%s: can only take %i arguments\n",
395 len
= strcspn(val
, ",");
397 /* nul-terminate and parse */
399 ((char *)val
)[len
] = '\0';
400 BUG_ON(!mutex_is_locked(¶m_lock
));
408 } while (save
== ',');
411 printk(KERN_ERR
"%s: needs at least %i arguments\n",
418 static int param_array_set(const char *val
, const struct kernel_param
*kp
)
420 const struct kparam_array
*arr
= kp
->arr
;
421 unsigned int temp_num
;
423 return param_array(kp
->name
, val
, 1, arr
->max
, arr
->elem
,
424 arr
->elemsize
, arr
->ops
->set
, kp
->flags
,
425 arr
->num
?: &temp_num
);
428 static int param_array_get(char *buffer
, const struct kernel_param
*kp
)
431 const struct kparam_array
*arr
= kp
->arr
;
432 struct kernel_param p
;
435 for (i
= off
= 0; i
< (arr
->num
? *arr
->num
: arr
->max
); i
++) {
438 p
.arg
= arr
->elem
+ arr
->elemsize
* i
;
439 BUG_ON(!mutex_is_locked(¶m_lock
));
440 ret
= arr
->ops
->get(buffer
+ off
, &p
);
449 static void param_array_free(void *arg
)
452 const struct kparam_array
*arr
= arg
;
455 for (i
= 0; i
< (arr
->num
? *arr
->num
: arr
->max
); i
++)
456 arr
->ops
->free(arr
->elem
+ arr
->elemsize
* i
);
459 struct kernel_param_ops param_array_ops
= {
460 .set
= param_array_set
,
461 .get
= param_array_get
,
462 .free
= param_array_free
,
464 EXPORT_SYMBOL(param_array_ops
);
466 int param_set_copystring(const char *val
, const struct kernel_param
*kp
)
468 const struct kparam_string
*kps
= kp
->str
;
470 if (strlen(val
)+1 > kps
->maxlen
) {
471 printk(KERN_ERR
"%s: string doesn't fit in %u chars.\n",
472 kp
->name
, kps
->maxlen
-1);
475 strcpy(kps
->string
, val
);
478 EXPORT_SYMBOL(param_set_copystring
);
480 int param_get_string(char *buffer
, const struct kernel_param
*kp
)
482 const struct kparam_string
*kps
= kp
->str
;
483 return strlcpy(buffer
, kps
->string
, kps
->maxlen
);
485 EXPORT_SYMBOL(param_get_string
);
487 struct kernel_param_ops param_ops_string
= {
488 .set
= param_set_copystring
,
489 .get
= param_get_string
,
491 EXPORT_SYMBOL(param_ops_string
);
493 /* sysfs output in /sys/modules/XYZ/parameters/ */
494 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
495 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
497 extern struct kernel_param __start___param
[], __stop___param
[];
499 struct param_attribute
501 struct module_attribute mattr
;
502 const struct kernel_param
*param
;
505 struct module_param_attrs
508 struct attribute_group grp
;
509 struct param_attribute attrs
[0];
513 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
515 static ssize_t
param_attr_show(struct module_attribute
*mattr
,
516 struct module
*mod
, char *buf
)
519 struct param_attribute
*attribute
= to_param_attr(mattr
);
521 if (!attribute
->param
->ops
->get
)
524 mutex_lock(¶m_lock
);
525 count
= attribute
->param
->ops
->get(buf
, attribute
->param
);
526 mutex_unlock(¶m_lock
);
534 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
535 static ssize_t
param_attr_store(struct module_attribute
*mattr
,
536 struct module
*owner
,
537 const char *buf
, size_t len
)
540 struct param_attribute
*attribute
= to_param_attr(mattr
);
542 if (!attribute
->param
->ops
->set
)
545 mutex_lock(¶m_lock
);
546 err
= attribute
->param
->ops
->set(buf
, attribute
->param
);
547 mutex_unlock(¶m_lock
);
554 #ifdef CONFIG_MODULES
557 #define __modinit __init
561 void __kernel_param_lock(void)
563 mutex_lock(¶m_lock
);
565 EXPORT_SYMBOL(__kernel_param_lock
);
567 void __kernel_param_unlock(void)
569 mutex_unlock(¶m_lock
);
571 EXPORT_SYMBOL(__kernel_param_unlock
);
574 * add_sysfs_param - add a parameter to sysfs
575 * @mk: struct module_kobject
576 * @kparam: the actual parameter definition to add to sysfs
577 * @name: name of parameter
579 * Create a kobject if for a (per-module) parameter if mp NULL, and
580 * create file in sysfs. Returns an error on out of memory. Always cleans up
581 * if there's an error.
583 static __modinit
int add_sysfs_param(struct module_kobject
*mk
,
584 const struct kernel_param
*kp
,
587 struct module_param_attrs
*new;
588 struct attribute
**attrs
;
591 /* We don't bother calling this with invisible parameters. */
599 attrs
= mk
->mp
->grp
.attrs
;
603 new = krealloc(mk
->mp
,
604 sizeof(*mk
->mp
) + sizeof(mk
->mp
->attrs
[0]) * (num
+1),
611 attrs
= krealloc(attrs
, sizeof(new->grp
.attrs
[0])*(num
+2), GFP_KERNEL
);
617 /* Sysfs wants everything zeroed. */
618 memset(new, 0, sizeof(*new));
619 memset(&new->attrs
[num
], 0, sizeof(new->attrs
[num
]));
620 memset(&attrs
[num
], 0, sizeof(attrs
[num
]));
621 new->grp
.name
= "parameters";
622 new->grp
.attrs
= attrs
;
624 /* Tack new one on the end. */
625 sysfs_attr_init(&new->attrs
[num
].mattr
.attr
);
626 new->attrs
[num
].param
= kp
;
627 new->attrs
[num
].mattr
.show
= param_attr_show
;
628 new->attrs
[num
].mattr
.store
= param_attr_store
;
629 new->attrs
[num
].mattr
.attr
.name
= (char *)name
;
630 new->attrs
[num
].mattr
.attr
.mode
= kp
->perm
;
633 /* Fix up all the pointers, since krealloc can move us */
634 for (num
= 0; num
< new->num
; num
++)
635 new->grp
.attrs
[num
] = &new->attrs
[num
].mattr
.attr
;
636 new->grp
.attrs
[num
] = NULL
;
648 #ifdef CONFIG_MODULES
649 static void free_module_param_attrs(struct module_kobject
*mk
)
651 kfree(mk
->mp
->grp
.attrs
);
657 * module_param_sysfs_setup - setup sysfs support for one module
659 * @kparam: module parameters (array)
660 * @num_params: number of module parameters
662 * Adds sysfs entries for module parameters under
663 * /sys/module/[mod->name]/parameters/
665 int module_param_sysfs_setup(struct module
*mod
,
666 const struct kernel_param
*kparam
,
667 unsigned int num_params
)
672 for (i
= 0; i
< num_params
; i
++) {
673 if (kparam
[i
].perm
== 0)
675 err
= add_sysfs_param(&mod
->mkobj
, &kparam
[i
], kparam
[i
].name
);
684 /* Create the param group. */
685 err
= sysfs_create_group(&mod
->mkobj
.kobj
, &mod
->mkobj
.mp
->grp
);
687 free_module_param_attrs(&mod
->mkobj
);
692 * module_param_sysfs_remove - remove sysfs support for one module
695 * Remove sysfs entries for module parameters and the corresponding
698 void module_param_sysfs_remove(struct module
*mod
)
701 sysfs_remove_group(&mod
->mkobj
.kobj
, &mod
->mkobj
.mp
->grp
);
702 /* We are positive that no one is using any param
703 * attrs at this point. Deallocate immediately. */
704 free_module_param_attrs(&mod
->mkobj
);
709 void destroy_params(const struct kernel_param
*params
, unsigned num
)
713 for (i
= 0; i
< num
; i
++)
714 if (params
[i
].ops
->free
)
715 params
[i
].ops
->free(params
[i
].arg
);
718 static void __init
kernel_add_sysfs_param(const char *name
,
719 struct kernel_param
*kparam
,
720 unsigned int name_skip
)
722 struct module_kobject
*mk
;
723 struct kobject
*kobj
;
726 kobj
= kset_find_obj(module_kset
, name
);
728 /* We already have one. Remove params so we can add more. */
729 mk
= to_module_kobject(kobj
);
730 /* We need to remove it before adding parameters. */
731 sysfs_remove_group(&mk
->kobj
, &mk
->mp
->grp
);
733 mk
= kzalloc(sizeof(struct module_kobject
), GFP_KERNEL
);
736 mk
->mod
= THIS_MODULE
;
737 mk
->kobj
.kset
= module_kset
;
738 err
= kobject_init_and_add(&mk
->kobj
, &module_ktype
, NULL
,
741 kobject_put(&mk
->kobj
);
742 printk(KERN_ERR
"Module '%s' failed add to sysfs, "
743 "error number %d\n", name
, err
);
744 printk(KERN_ERR
"The system will be unstable now.\n");
747 /* So that exit path is even. */
748 kobject_get(&mk
->kobj
);
751 /* These should not fail at boot. */
752 err
= add_sysfs_param(mk
, kparam
, kparam
->name
+ name_skip
);
754 err
= sysfs_create_group(&mk
->kobj
, &mk
->mp
->grp
);
756 kobject_uevent(&mk
->kobj
, KOBJ_ADD
);
757 kobject_put(&mk
->kobj
);
761 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
763 * Add module_parameters to sysfs for "modules" built into the kernel.
765 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
766 * "parameter" name is stored behind a dot in kernel_param->name. So,
767 * extract the "module" name for all built-in kernel_param-eters,
768 * and for all who have the same, call kernel_add_sysfs_param.
770 static void __init
param_sysfs_builtin(void)
772 struct kernel_param
*kp
;
773 unsigned int name_len
;
774 char modname
[MODULE_NAME_LEN
];
776 for (kp
= __start___param
; kp
< __stop___param
; kp
++) {
782 dot
= strchr(kp
->name
, '.');
784 /* This happens for core_param() */
785 strcpy(modname
, "kernel");
788 name_len
= dot
- kp
->name
+ 1;
789 strlcpy(modname
, kp
->name
, name_len
);
791 kernel_add_sysfs_param(modname
, kp
, name_len
);
796 /* module-related sysfs stuff */
798 static ssize_t
module_attr_show(struct kobject
*kobj
,
799 struct attribute
*attr
,
802 struct module_attribute
*attribute
;
803 struct module_kobject
*mk
;
806 attribute
= to_module_attr(attr
);
807 mk
= to_module_kobject(kobj
);
809 if (!attribute
->show
)
812 ret
= attribute
->show(attribute
, mk
->mod
, buf
);
817 static ssize_t
module_attr_store(struct kobject
*kobj
,
818 struct attribute
*attr
,
819 const char *buf
, size_t len
)
821 struct module_attribute
*attribute
;
822 struct module_kobject
*mk
;
825 attribute
= to_module_attr(attr
);
826 mk
= to_module_kobject(kobj
);
828 if (!attribute
->store
)
831 ret
= attribute
->store(attribute
, mk
->mod
, buf
, len
);
836 static const struct sysfs_ops module_sysfs_ops
= {
837 .show
= module_attr_show
,
838 .store
= module_attr_store
,
841 static int uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
843 struct kobj_type
*ktype
= get_ktype(kobj
);
845 if (ktype
== &module_ktype
)
850 static const struct kset_uevent_ops module_uevent_ops
= {
851 .filter
= uevent_filter
,
854 struct kset
*module_kset
;
855 int module_sysfs_initialized
;
857 struct kobj_type module_ktype
= {
858 .sysfs_ops
= &module_sysfs_ops
,
862 * param_sysfs_init - wrapper for built-in params support
864 static int __init
param_sysfs_init(void)
866 module_kset
= kset_create_and_add("module", &module_uevent_ops
, NULL
);
868 printk(KERN_WARNING
"%s (%d): error creating kset\n",
872 module_sysfs_initialized
= 1;
874 param_sysfs_builtin();
878 subsys_initcall(param_sysfs_init
);
880 #endif /* CONFIG_SYSFS */