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>
31 #define DEBUGP(fmt, a...)
34 /* This just allows us to keep track of which parameters are kmalloced. */
35 struct kmalloced_param
{
36 struct list_head list
;
39 static DEFINE_MUTEX(param_lock
);
40 static LIST_HEAD(kmalloced_params
);
42 static void *kmalloc_parameter(unsigned int size
)
44 struct kmalloced_param
*p
;
46 p
= kmalloc(sizeof(*p
) + size
, GFP_KERNEL
);
50 mutex_lock(¶m_lock
);
51 list_add(&p
->list
, &kmalloced_params
);
52 mutex_unlock(¶m_lock
);
57 /* Does nothing if parameter wasn't kmalloced above. */
58 static void maybe_kfree_parameter(void *param
)
60 struct kmalloced_param
*p
;
62 mutex_lock(¶m_lock
);
63 list_for_each_entry(p
, &kmalloced_params
, list
) {
64 if (p
->val
== param
) {
70 mutex_unlock(¶m_lock
);
73 static inline char dash2underscore(char c
)
80 static inline int parameq(const char *input
, const char *paramname
)
83 for (i
= 0; dash2underscore(input
[i
]) == paramname
[i
]; i
++)
89 static int parse_one(char *param
,
91 struct kernel_param
*params
,
93 int (*handle_unknown
)(char *param
, char *val
))
98 for (i
= 0; i
< num_params
; i
++) {
99 if (parameq(param
, params
[i
].name
)) {
100 /* Noone handled NULL, so do it here. */
101 if (!val
&& params
[i
].ops
->set
!= param_set_bool
)
103 DEBUGP("They are equal! Calling %p\n",
105 return params
[i
].ops
->set(val
, ¶ms
[i
]);
109 if (handle_unknown
) {
110 DEBUGP("Unknown argument: calling %p\n", handle_unknown
);
111 return handle_unknown(param
, val
);
114 DEBUGP("Unknown argument `%s'\n", param
);
118 /* You can use " around spaces, but can't escape ". */
119 /* Hyphens and underscores equivalent in parameter names. */
120 static char *next_arg(char *args
, char **param
, char **val
)
122 unsigned int i
, equals
= 0;
123 int in_quote
= 0, quoted
= 0;
132 for (i
= 0; args
[i
]; i
++) {
133 if (isspace(args
[i
]) && !in_quote
)
140 in_quote
= !in_quote
;
148 *val
= args
+ equals
+ 1;
150 /* Don't include quotes in value. */
153 if (args
[i
-1] == '"')
156 if (quoted
&& args
[i
-1] == '"')
166 /* Chew up trailing spaces. */
167 return skip_spaces(next
);
170 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
171 int parse_args(const char *name
,
173 struct kernel_param
*params
,
175 int (*unknown
)(char *param
, char *val
))
179 DEBUGP("Parsing ARGS: %s\n", args
);
181 /* Chew leading spaces */
182 args
= skip_spaces(args
);
186 int irq_was_disabled
;
188 args
= next_arg(args
, ¶m
, &val
);
189 irq_was_disabled
= irqs_disabled();
190 ret
= parse_one(param
, val
, params
, num
, unknown
);
191 if (irq_was_disabled
&& !irqs_disabled()) {
192 printk(KERN_WARNING
"parse_args(): option '%s' enabled "
197 printk(KERN_ERR
"%s: Unknown parameter `%s'\n",
202 "%s: `%s' too large for parameter `%s'\n",
203 name
, val
?: "", param
);
209 "%s: `%s' invalid for parameter `%s'\n",
210 name
, val
?: "", param
);
219 /* Lazy bastard, eh? */
220 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
221 int param_set_##name(const char *val, const struct kernel_param *kp) \
226 ret = strtolfn(val, 0, &l); \
227 if (ret == -EINVAL || ((type)l != l)) \
229 *((type *)kp->arg) = l; \
232 int param_get_##name(char *buffer, const struct kernel_param *kp) \
234 return sprintf(buffer, format, *((type *)kp->arg)); \
236 struct kernel_param_ops param_ops_##name = { \
237 .set = param_set_##name, \
238 .get = param_get_##name, \
240 EXPORT_SYMBOL(param_set_##name); \
241 EXPORT_SYMBOL(param_get_##name); \
242 EXPORT_SYMBOL(param_ops_##name)
245 STANDARD_PARAM_DEF(byte
, unsigned char, "%c", unsigned long, strict_strtoul
);
246 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol
);
247 STANDARD_PARAM_DEF(ushort
, unsigned short, "%hu", unsigned long, strict_strtoul
);
248 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol
);
249 STANDARD_PARAM_DEF(uint
, unsigned int, "%u", unsigned long, strict_strtoul
);
250 STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol
);
251 STANDARD_PARAM_DEF(ulong
, unsigned long, "%lu", unsigned long, strict_strtoul
);
253 int param_set_charp(const char *val
, const struct kernel_param
*kp
)
255 if (strlen(val
) > 1024) {
256 printk(KERN_ERR
"%s: string parameter too long\n",
261 maybe_kfree_parameter(*(char **)kp
->arg
);
263 /* This is a hack. We can't kmalloc in early boot, and we
264 * don't need to; this mangled commandline is preserved. */
265 if (slab_is_available()) {
266 *(char **)kp
->arg
= kmalloc_parameter(strlen(val
)+1);
267 if (!*(char **)kp
->arg
)
269 strcpy(*(char **)kp
->arg
, val
);
271 *(const char **)kp
->arg
= val
;
275 EXPORT_SYMBOL(param_set_charp
);
277 int param_get_charp(char *buffer
, const struct kernel_param
*kp
)
279 return sprintf(buffer
, "%s", *((char **)kp
->arg
));
281 EXPORT_SYMBOL(param_get_charp
);
283 static void param_free_charp(void *arg
)
285 maybe_kfree_parameter(*((char **)arg
));
288 struct kernel_param_ops param_ops_charp
= {
289 .set
= param_set_charp
,
290 .get
= param_get_charp
,
291 .free
= param_free_charp
,
293 EXPORT_SYMBOL(param_ops_charp
);
295 /* Actually could be a bool or an int, for historical reasons. */
296 int param_set_bool(const char *val
, const struct kernel_param
*kp
)
300 /* No equals means "set"... */
303 /* One of =[yYnN01] */
305 case 'y': case 'Y': case '1':
308 case 'n': case 'N': case '0':
315 if (kp
->flags
& KPARAM_ISBOOL
)
316 *(bool *)kp
->arg
= v
;
321 EXPORT_SYMBOL(param_set_bool
);
323 int param_get_bool(char *buffer
, const struct kernel_param
*kp
)
326 if (kp
->flags
& KPARAM_ISBOOL
)
327 val
= *(bool *)kp
->arg
;
329 val
= *(int *)kp
->arg
;
331 /* Y and N chosen as being relatively non-coder friendly */
332 return sprintf(buffer
, "%c", val
? 'Y' : 'N');
334 EXPORT_SYMBOL(param_get_bool
);
336 struct kernel_param_ops param_ops_bool
= {
337 .set
= param_set_bool
,
338 .get
= param_get_bool
,
340 EXPORT_SYMBOL(param_ops_bool
);
342 /* This one must be bool. */
343 int param_set_invbool(const char *val
, const struct kernel_param
*kp
)
347 struct kernel_param dummy
;
349 dummy
.arg
= &boolval
;
350 dummy
.flags
= KPARAM_ISBOOL
;
351 ret
= param_set_bool(val
, &dummy
);
353 *(bool *)kp
->arg
= !boolval
;
356 EXPORT_SYMBOL(param_set_invbool
);
358 int param_get_invbool(char *buffer
, const struct kernel_param
*kp
)
360 return sprintf(buffer
, "%c", (*(bool *)kp
->arg
) ? 'N' : 'Y');
362 EXPORT_SYMBOL(param_get_invbool
);
364 struct kernel_param_ops param_ops_invbool
= {
365 .set
= param_set_invbool
,
366 .get
= param_get_invbool
,
368 EXPORT_SYMBOL(param_ops_invbool
);
370 /* We break the rule and mangle the string. */
371 static int param_array(const char *name
,
373 unsigned int min
, unsigned int max
,
374 void *elem
, int elemsize
,
375 int (*set
)(const char *, const struct kernel_param
*kp
),
380 struct kernel_param kp
;
383 /* Get the name right for errors. */
389 /* We expect a comma-separated list of values. */
394 printk(KERN_ERR
"%s: can only take %i arguments\n",
398 len
= strcspn(val
, ",");
400 /* nul-terminate and parse */
402 ((char *)val
)[len
] = '\0';
410 } while (save
== ',');
413 printk(KERN_ERR
"%s: needs at least %i arguments\n",
420 static int param_array_set(const char *val
, const struct kernel_param
*kp
)
422 const struct kparam_array
*arr
= kp
->arr
;
423 unsigned int temp_num
;
425 return param_array(kp
->name
, val
, 1, arr
->max
, arr
->elem
,
426 arr
->elemsize
, arr
->ops
->set
, kp
->flags
,
427 arr
->num
?: &temp_num
);
430 static int param_array_get(char *buffer
, const struct kernel_param
*kp
)
433 const struct kparam_array
*arr
= kp
->arr
;
434 struct kernel_param p
;
437 for (i
= off
= 0; i
< (arr
->num
? *arr
->num
: arr
->max
); i
++) {
440 p
.arg
= arr
->elem
+ arr
->elemsize
* i
;
441 ret
= arr
->ops
->get(buffer
+ off
, &p
);
450 static void param_array_free(void *arg
)
453 const struct kparam_array
*arr
= arg
;
456 for (i
= 0; i
< (arr
->num
? *arr
->num
: arr
->max
); i
++)
457 arr
->ops
->free(arr
->elem
+ arr
->elemsize
* i
);
460 struct kernel_param_ops param_array_ops
= {
461 .set
= param_array_set
,
462 .get
= param_array_get
,
463 .free
= param_array_free
,
465 EXPORT_SYMBOL(param_array_ops
);
467 int param_set_copystring(const char *val
, const struct kernel_param
*kp
)
469 const struct kparam_string
*kps
= kp
->str
;
471 if (strlen(val
)+1 > kps
->maxlen
) {
472 printk(KERN_ERR
"%s: string doesn't fit in %u chars.\n",
473 kp
->name
, kps
->maxlen
-1);
476 strcpy(kps
->string
, val
);
479 EXPORT_SYMBOL(param_set_copystring
);
481 int param_get_string(char *buffer
, const struct kernel_param
*kp
)
483 const struct kparam_string
*kps
= kp
->str
;
484 return strlcpy(buffer
, kps
->string
, kps
->maxlen
);
486 EXPORT_SYMBOL(param_get_string
);
488 struct kernel_param_ops param_ops_string
= {
489 .set
= param_set_copystring
,
490 .get
= param_get_string
,
492 EXPORT_SYMBOL(param_ops_string
);
494 /* sysfs output in /sys/modules/XYZ/parameters/ */
495 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
496 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
498 extern struct kernel_param __start___param
[], __stop___param
[];
500 struct param_attribute
502 struct module_attribute mattr
;
503 const struct kernel_param
*param
;
506 struct module_param_attrs
509 struct attribute_group grp
;
510 struct param_attribute attrs
[0];
514 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
516 static ssize_t
param_attr_show(struct module_attribute
*mattr
,
517 struct module
*mod
, char *buf
)
520 struct param_attribute
*attribute
= to_param_attr(mattr
);
522 if (!attribute
->param
->ops
->get
)
525 count
= attribute
->param
->ops
->get(buf
, attribute
->param
);
533 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
534 static ssize_t
param_attr_store(struct module_attribute
*mattr
,
535 struct module
*owner
,
536 const char *buf
, size_t len
)
539 struct param_attribute
*attribute
= to_param_attr(mattr
);
541 if (!attribute
->param
->ops
->set
)
544 err
= attribute
->param
->ops
->set(buf
, attribute
->param
);
551 #ifdef CONFIG_MODULES
554 #define __modinit __init
559 * add_sysfs_param - add a parameter to sysfs
560 * @mk: struct module_kobject
561 * @kparam: the actual parameter definition to add to sysfs
562 * @name: name of parameter
564 * Create a kobject if for a (per-module) parameter if mp NULL, and
565 * create file in sysfs. Returns an error on out of memory. Always cleans up
566 * if there's an error.
568 static __modinit
int add_sysfs_param(struct module_kobject
*mk
,
569 const struct kernel_param
*kp
,
572 struct module_param_attrs
*new;
573 struct attribute
**attrs
;
576 /* We don't bother calling this with invisible parameters. */
584 attrs
= mk
->mp
->grp
.attrs
;
588 new = krealloc(mk
->mp
,
589 sizeof(*mk
->mp
) + sizeof(mk
->mp
->attrs
[0]) * (num
+1),
596 attrs
= krealloc(attrs
, sizeof(new->grp
.attrs
[0])*(num
+2), GFP_KERNEL
);
602 /* Sysfs wants everything zeroed. */
603 memset(new, 0, sizeof(*new));
604 memset(&new->attrs
[num
], 0, sizeof(new->attrs
[num
]));
605 memset(&attrs
[num
], 0, sizeof(attrs
[num
]));
606 new->grp
.name
= "parameters";
607 new->grp
.attrs
= attrs
;
609 /* Tack new one on the end. */
610 sysfs_attr_init(&new->attrs
[num
].mattr
.attr
);
611 new->attrs
[num
].param
= kp
;
612 new->attrs
[num
].mattr
.show
= param_attr_show
;
613 new->attrs
[num
].mattr
.store
= param_attr_store
;
614 new->attrs
[num
].mattr
.attr
.name
= (char *)name
;
615 new->attrs
[num
].mattr
.attr
.mode
= kp
->perm
;
618 /* Fix up all the pointers, since krealloc can move us */
619 for (num
= 0; num
< new->num
; num
++)
620 new->grp
.attrs
[num
] = &new->attrs
[num
].mattr
.attr
;
621 new->grp
.attrs
[num
] = NULL
;
633 #ifdef CONFIG_MODULES
634 static void free_module_param_attrs(struct module_kobject
*mk
)
636 kfree(mk
->mp
->grp
.attrs
);
642 * module_param_sysfs_setup - setup sysfs support for one module
644 * @kparam: module parameters (array)
645 * @num_params: number of module parameters
647 * Adds sysfs entries for module parameters under
648 * /sys/module/[mod->name]/parameters/
650 int module_param_sysfs_setup(struct module
*mod
,
651 const struct kernel_param
*kparam
,
652 unsigned int num_params
)
657 for (i
= 0; i
< num_params
; i
++) {
658 if (kparam
[i
].perm
== 0)
660 err
= add_sysfs_param(&mod
->mkobj
, &kparam
[i
], kparam
[i
].name
);
669 /* Create the param group. */
670 err
= sysfs_create_group(&mod
->mkobj
.kobj
, &mod
->mkobj
.mp
->grp
);
672 free_module_param_attrs(&mod
->mkobj
);
677 * module_param_sysfs_remove - remove sysfs support for one module
680 * Remove sysfs entries for module parameters and the corresponding
683 void module_param_sysfs_remove(struct module
*mod
)
686 sysfs_remove_group(&mod
->mkobj
.kobj
, &mod
->mkobj
.mp
->grp
);
687 /* We are positive that no one is using any param
688 * attrs at this point. Deallocate immediately. */
689 free_module_param_attrs(&mod
->mkobj
);
694 void destroy_params(const struct kernel_param
*params
, unsigned num
)
698 for (i
= 0; i
< num
; i
++)
699 if (params
[i
].ops
->free
)
700 params
[i
].ops
->free(params
[i
].arg
);
703 static void __init
kernel_add_sysfs_param(const char *name
,
704 struct kernel_param
*kparam
,
705 unsigned int name_skip
)
707 struct module_kobject
*mk
;
708 struct kobject
*kobj
;
711 kobj
= kset_find_obj(module_kset
, name
);
713 /* We already have one. Remove params so we can add more. */
714 mk
= to_module_kobject(kobj
);
715 /* We need to remove it before adding parameters. */
716 sysfs_remove_group(&mk
->kobj
, &mk
->mp
->grp
);
718 mk
= kzalloc(sizeof(struct module_kobject
), GFP_KERNEL
);
721 mk
->mod
= THIS_MODULE
;
722 mk
->kobj
.kset
= module_kset
;
723 err
= kobject_init_and_add(&mk
->kobj
, &module_ktype
, NULL
,
726 kobject_put(&mk
->kobj
);
727 printk(KERN_ERR
"Module '%s' failed add to sysfs, "
728 "error number %d\n", name
, err
);
729 printk(KERN_ERR
"The system will be unstable now.\n");
732 /* So that exit path is even. */
733 kobject_get(&mk
->kobj
);
736 /* These should not fail at boot. */
737 err
= add_sysfs_param(mk
, kparam
, kparam
->name
+ name_skip
);
739 err
= sysfs_create_group(&mk
->kobj
, &mk
->mp
->grp
);
741 kobject_uevent(&mk
->kobj
, KOBJ_ADD
);
742 kobject_put(&mk
->kobj
);
746 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
748 * Add module_parameters to sysfs for "modules" built into the kernel.
750 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
751 * "parameter" name is stored behind a dot in kernel_param->name. So,
752 * extract the "module" name for all built-in kernel_param-eters,
753 * and for all who have the same, call kernel_add_sysfs_param.
755 static void __init
param_sysfs_builtin(void)
757 struct kernel_param
*kp
;
758 unsigned int name_len
;
759 char modname
[MODULE_NAME_LEN
];
761 for (kp
= __start___param
; kp
< __stop___param
; kp
++) {
767 dot
= strchr(kp
->name
, '.');
769 /* This happens for core_param() */
770 strcpy(modname
, "kernel");
773 name_len
= dot
- kp
->name
+ 1;
774 strlcpy(modname
, kp
->name
, name_len
);
776 kernel_add_sysfs_param(modname
, kp
, name_len
);
781 /* module-related sysfs stuff */
783 static ssize_t
module_attr_show(struct kobject
*kobj
,
784 struct attribute
*attr
,
787 struct module_attribute
*attribute
;
788 struct module_kobject
*mk
;
791 attribute
= to_module_attr(attr
);
792 mk
= to_module_kobject(kobj
);
794 if (!attribute
->show
)
797 ret
= attribute
->show(attribute
, mk
->mod
, buf
);
802 static ssize_t
module_attr_store(struct kobject
*kobj
,
803 struct attribute
*attr
,
804 const char *buf
, size_t len
)
806 struct module_attribute
*attribute
;
807 struct module_kobject
*mk
;
810 attribute
= to_module_attr(attr
);
811 mk
= to_module_kobject(kobj
);
813 if (!attribute
->store
)
816 ret
= attribute
->store(attribute
, mk
->mod
, buf
, len
);
821 static const struct sysfs_ops module_sysfs_ops
= {
822 .show
= module_attr_show
,
823 .store
= module_attr_store
,
826 static int uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
828 struct kobj_type
*ktype
= get_ktype(kobj
);
830 if (ktype
== &module_ktype
)
835 static const struct kset_uevent_ops module_uevent_ops
= {
836 .filter
= uevent_filter
,
839 struct kset
*module_kset
;
840 int module_sysfs_initialized
;
842 struct kobj_type module_ktype
= {
843 .sysfs_ops
= &module_sysfs_ops
,
847 * param_sysfs_init - wrapper for built-in params support
849 static int __init
param_sysfs_init(void)
851 module_kset
= kset_create_and_add("module", &module_uevent_ops
, NULL
);
853 printk(KERN_WARNING
"%s (%d): error creating kset\n",
857 module_sysfs_initialized
= 1;
859 param_sysfs_builtin();
863 subsys_initcall(param_sysfs_init
);
865 #endif /* CONFIG_SYSFS */