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/module.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 /* Protects all parameters, and incidentally kmalloced_param list. */
29 static DEFINE_MUTEX(param_lock
);
31 /* This just allows us to keep track of which parameters are kmalloced. */
32 struct kmalloced_param
{
33 struct list_head list
;
36 static LIST_HEAD(kmalloced_params
);
38 static void *kmalloc_parameter(unsigned int size
)
40 struct kmalloced_param
*p
;
42 p
= kmalloc(sizeof(*p
) + size
, GFP_KERNEL
);
46 list_add(&p
->list
, &kmalloced_params
);
50 /* Does nothing if parameter wasn't kmalloced above. */
51 static void maybe_kfree_parameter(void *param
)
53 struct kmalloced_param
*p
;
55 list_for_each_entry(p
, &kmalloced_params
, list
) {
56 if (p
->val
== param
) {
64 static char dash2underscore(char c
)
71 bool parameqn(const char *a
, const char *b
, size_t n
)
75 for (i
= 0; i
< n
; i
++) {
76 if (dash2underscore(a
[i
]) != dash2underscore(b
[i
]))
82 bool parameq(const char *a
, const char *b
)
84 return parameqn(a
, b
, strlen(a
)+1);
87 static int parse_one(char *param
,
89 const struct kernel_param
*params
,
91 int (*handle_unknown
)(char *param
, char *val
))
97 for (i
= 0; i
< num_params
; i
++) {
98 if (parameq(param
, params
[i
].name
)) {
99 /* No one handled NULL, so do it here. */
100 if (!val
&& params
[i
].ops
->set
!= param_set_bool
101 && params
[i
].ops
->set
!= param_set_bint
)
103 pr_debug("They are equal! Calling %p\n",
105 mutex_lock(¶m_lock
);
106 err
= params
[i
].ops
->set(val
, ¶ms
[i
]);
107 mutex_unlock(¶m_lock
);
112 if (handle_unknown
) {
113 pr_debug("Unknown argument: calling %p\n", handle_unknown
);
114 return handle_unknown(param
, val
);
117 pr_debug("Unknown argument `%s'\n", param
);
121 /* You can use " around spaces, but can't escape ". */
122 /* Hyphens and underscores equivalent in parameter names. */
123 static char *next_arg(char *args
, char **param
, char **val
)
125 unsigned int i
, equals
= 0;
126 int in_quote
= 0, quoted
= 0;
135 for (i
= 0; args
[i
]; i
++) {
136 if (isspace(args
[i
]) && !in_quote
)
143 in_quote
= !in_quote
;
151 *val
= args
+ equals
+ 1;
153 /* Don't include quotes in value. */
156 if (args
[i
-1] == '"')
159 if (quoted
&& args
[i
-1] == '"')
169 /* Chew up trailing spaces. */
170 return skip_spaces(next
);
173 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
174 int parse_args(const char *name
,
176 const struct kernel_param
*params
,
178 int (*unknown
)(char *param
, char *val
))
182 pr_debug("Parsing ARGS: %s\n", args
);
184 /* Chew leading spaces */
185 args
= skip_spaces(args
);
189 int irq_was_disabled
;
191 args
= next_arg(args
, ¶m
, &val
);
192 irq_was_disabled
= irqs_disabled();
193 ret
= parse_one(param
, val
, params
, num
, unknown
);
194 if (irq_was_disabled
&& !irqs_disabled()) {
195 printk(KERN_WARNING
"parse_args(): option '%s' enabled "
200 printk(KERN_ERR
"%s: Unknown parameter `%s'\n",
205 "%s: `%s' too large for parameter `%s'\n",
206 name
, val
?: "", param
);
212 "%s: `%s' invalid for parameter `%s'\n",
213 name
, val
?: "", param
);
222 /* Lazy bastard, eh? */
223 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
224 int param_set_##name(const char *val, const struct kernel_param *kp) \
229 ret = strtolfn(val, 0, &l); \
230 if (ret < 0 || ((type)l != l)) \
231 return ret < 0 ? ret : -EINVAL; \
232 *((type *)kp->arg) = l; \
235 int param_get_##name(char *buffer, const struct kernel_param *kp) \
237 return sprintf(buffer, format, *((type *)kp->arg)); \
239 struct kernel_param_ops param_ops_##name = { \
240 .set = param_set_##name, \
241 .get = param_get_##name, \
243 EXPORT_SYMBOL(param_set_##name); \
244 EXPORT_SYMBOL(param_get_##name); \
245 EXPORT_SYMBOL(param_ops_##name)
248 STANDARD_PARAM_DEF(byte
, unsigned char, "%c", unsigned long, strict_strtoul
);
249 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol
);
250 STANDARD_PARAM_DEF(ushort
, unsigned short, "%hu", unsigned long, strict_strtoul
);
251 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol
);
252 STANDARD_PARAM_DEF(uint
, unsigned int, "%u", unsigned long, strict_strtoul
);
253 STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol
);
254 STANDARD_PARAM_DEF(ulong
, unsigned long, "%lu", unsigned long, strict_strtoul
);
256 int param_set_charp(const char *val
, const struct kernel_param
*kp
)
258 if (strlen(val
) > 1024) {
259 printk(KERN_ERR
"%s: string parameter too long\n",
264 maybe_kfree_parameter(*(char **)kp
->arg
);
266 /* This is a hack. We can't kmalloc in early boot, and we
267 * don't need to; this mangled commandline is preserved. */
268 if (slab_is_available()) {
269 *(char **)kp
->arg
= kmalloc_parameter(strlen(val
)+1);
270 if (!*(char **)kp
->arg
)
272 strcpy(*(char **)kp
->arg
, val
);
274 *(const char **)kp
->arg
= val
;
278 EXPORT_SYMBOL(param_set_charp
);
280 int param_get_charp(char *buffer
, const struct kernel_param
*kp
)
282 return sprintf(buffer
, "%s", *((char **)kp
->arg
));
284 EXPORT_SYMBOL(param_get_charp
);
286 static void param_free_charp(void *arg
)
288 maybe_kfree_parameter(*((char **)arg
));
291 struct kernel_param_ops param_ops_charp
= {
292 .set
= param_set_charp
,
293 .get
= param_get_charp
,
294 .free
= param_free_charp
,
296 EXPORT_SYMBOL(param_ops_charp
);
298 /* Actually could be a bool or an int, for historical reasons. */
299 int param_set_bool(const char *val
, const struct kernel_param
*kp
)
304 /* No equals means "set"... */
307 /* One of =[yYnN01] */
308 ret
= strtobool(val
, &v
);
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 int param_set_bint(const char *val
, const struct kernel_param
*kp
)
369 struct kernel_param boolkp
;
373 /* Match bool exactly, by re-using it. */
376 boolkp
.flags
|= KPARAM_ISBOOL
;
378 ret
= param_set_bool(val
, &boolkp
);
383 EXPORT_SYMBOL(param_set_bint
);
385 struct kernel_param_ops param_ops_bint
= {
386 .set
= param_set_bint
,
387 .get
= param_get_int
,
389 EXPORT_SYMBOL(param_ops_bint
);
391 /* We break the rule and mangle the string. */
392 static int param_array(const char *name
,
394 unsigned int min
, unsigned int max
,
395 void *elem
, int elemsize
,
396 int (*set
)(const char *, const struct kernel_param
*kp
),
401 struct kernel_param kp
;
404 /* Get the name right for errors. */
410 /* We expect a comma-separated list of values. */
415 printk(KERN_ERR
"%s: can only take %i arguments\n",
419 len
= strcspn(val
, ",");
421 /* nul-terminate and parse */
423 ((char *)val
)[len
] = '\0';
424 BUG_ON(!mutex_is_locked(¶m_lock
));
432 } while (save
== ',');
435 printk(KERN_ERR
"%s: needs at least %i arguments\n",
442 static int param_array_set(const char *val
, const struct kernel_param
*kp
)
444 const struct kparam_array
*arr
= kp
->arr
;
445 unsigned int temp_num
;
447 return param_array(kp
->name
, val
, 1, arr
->max
, arr
->elem
,
448 arr
->elemsize
, arr
->ops
->set
, kp
->flags
,
449 arr
->num
?: &temp_num
);
452 static int param_array_get(char *buffer
, const struct kernel_param
*kp
)
455 const struct kparam_array
*arr
= kp
->arr
;
456 struct kernel_param p
;
459 for (i
= off
= 0; i
< (arr
->num
? *arr
->num
: arr
->max
); i
++) {
462 p
.arg
= arr
->elem
+ arr
->elemsize
* i
;
463 BUG_ON(!mutex_is_locked(¶m_lock
));
464 ret
= arr
->ops
->get(buffer
+ off
, &p
);
473 static void param_array_free(void *arg
)
476 const struct kparam_array
*arr
= arg
;
479 for (i
= 0; i
< (arr
->num
? *arr
->num
: arr
->max
); i
++)
480 arr
->ops
->free(arr
->elem
+ arr
->elemsize
* i
);
483 struct kernel_param_ops param_array_ops
= {
484 .set
= param_array_set
,
485 .get
= param_array_get
,
486 .free
= param_array_free
,
488 EXPORT_SYMBOL(param_array_ops
);
490 int param_set_copystring(const char *val
, const struct kernel_param
*kp
)
492 const struct kparam_string
*kps
= kp
->str
;
494 if (strlen(val
)+1 > kps
->maxlen
) {
495 printk(KERN_ERR
"%s: string doesn't fit in %u chars.\n",
496 kp
->name
, kps
->maxlen
-1);
499 strcpy(kps
->string
, val
);
502 EXPORT_SYMBOL(param_set_copystring
);
504 int param_get_string(char *buffer
, const struct kernel_param
*kp
)
506 const struct kparam_string
*kps
= kp
->str
;
507 return strlcpy(buffer
, kps
->string
, kps
->maxlen
);
509 EXPORT_SYMBOL(param_get_string
);
511 struct kernel_param_ops param_ops_string
= {
512 .set
= param_set_copystring
,
513 .get
= param_get_string
,
515 EXPORT_SYMBOL(param_ops_string
);
517 /* sysfs output in /sys/modules/XYZ/parameters/ */
518 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
519 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
521 extern struct kernel_param __start___param
[], __stop___param
[];
523 struct param_attribute
525 struct module_attribute mattr
;
526 const struct kernel_param
*param
;
529 struct module_param_attrs
532 struct attribute_group grp
;
533 struct param_attribute attrs
[0];
537 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
539 static ssize_t
param_attr_show(struct module_attribute
*mattr
,
540 struct module_kobject
*mk
, char *buf
)
543 struct param_attribute
*attribute
= to_param_attr(mattr
);
545 if (!attribute
->param
->ops
->get
)
548 mutex_lock(¶m_lock
);
549 count
= attribute
->param
->ops
->get(buf
, attribute
->param
);
550 mutex_unlock(¶m_lock
);
558 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
559 static ssize_t
param_attr_store(struct module_attribute
*mattr
,
560 struct module_kobject
*km
,
561 const char *buf
, size_t len
)
564 struct param_attribute
*attribute
= to_param_attr(mattr
);
566 if (!attribute
->param
->ops
->set
)
569 mutex_lock(¶m_lock
);
570 err
= attribute
->param
->ops
->set(buf
, attribute
->param
);
571 mutex_unlock(¶m_lock
);
578 #ifdef CONFIG_MODULES
581 #define __modinit __init
585 void __kernel_param_lock(void)
587 mutex_lock(¶m_lock
);
589 EXPORT_SYMBOL(__kernel_param_lock
);
591 void __kernel_param_unlock(void)
593 mutex_unlock(¶m_lock
);
595 EXPORT_SYMBOL(__kernel_param_unlock
);
598 * add_sysfs_param - add a parameter to sysfs
599 * @mk: struct module_kobject
600 * @kparam: the actual parameter definition to add to sysfs
601 * @name: name of parameter
603 * Create a kobject if for a (per-module) parameter if mp NULL, and
604 * create file in sysfs. Returns an error on out of memory. Always cleans up
605 * if there's an error.
607 static __modinit
int add_sysfs_param(struct module_kobject
*mk
,
608 const struct kernel_param
*kp
,
611 struct module_param_attrs
*new;
612 struct attribute
**attrs
;
615 /* We don't bother calling this with invisible parameters. */
623 attrs
= mk
->mp
->grp
.attrs
;
627 new = krealloc(mk
->mp
,
628 sizeof(*mk
->mp
) + sizeof(mk
->mp
->attrs
[0]) * (num
+1),
635 attrs
= krealloc(attrs
, sizeof(new->grp
.attrs
[0])*(num
+2), GFP_KERNEL
);
641 /* Sysfs wants everything zeroed. */
642 memset(new, 0, sizeof(*new));
643 memset(&new->attrs
[num
], 0, sizeof(new->attrs
[num
]));
644 memset(&attrs
[num
], 0, sizeof(attrs
[num
]));
645 new->grp
.name
= "parameters";
646 new->grp
.attrs
= attrs
;
648 /* Tack new one on the end. */
649 sysfs_attr_init(&new->attrs
[num
].mattr
.attr
);
650 new->attrs
[num
].param
= kp
;
651 new->attrs
[num
].mattr
.show
= param_attr_show
;
652 new->attrs
[num
].mattr
.store
= param_attr_store
;
653 new->attrs
[num
].mattr
.attr
.name
= (char *)name
;
654 new->attrs
[num
].mattr
.attr
.mode
= kp
->perm
;
657 /* Fix up all the pointers, since krealloc can move us */
658 for (num
= 0; num
< new->num
; num
++)
659 new->grp
.attrs
[num
] = &new->attrs
[num
].mattr
.attr
;
660 new->grp
.attrs
[num
] = NULL
;
672 #ifdef CONFIG_MODULES
673 static void free_module_param_attrs(struct module_kobject
*mk
)
675 kfree(mk
->mp
->grp
.attrs
);
681 * module_param_sysfs_setup - setup sysfs support for one module
683 * @kparam: module parameters (array)
684 * @num_params: number of module parameters
686 * Adds sysfs entries for module parameters under
687 * /sys/module/[mod->name]/parameters/
689 int module_param_sysfs_setup(struct module
*mod
,
690 const struct kernel_param
*kparam
,
691 unsigned int num_params
)
696 for (i
= 0; i
< num_params
; i
++) {
697 if (kparam
[i
].perm
== 0)
699 err
= add_sysfs_param(&mod
->mkobj
, &kparam
[i
], kparam
[i
].name
);
708 /* Create the param group. */
709 err
= sysfs_create_group(&mod
->mkobj
.kobj
, &mod
->mkobj
.mp
->grp
);
711 free_module_param_attrs(&mod
->mkobj
);
716 * module_param_sysfs_remove - remove sysfs support for one module
719 * Remove sysfs entries for module parameters and the corresponding
722 void module_param_sysfs_remove(struct module
*mod
)
725 sysfs_remove_group(&mod
->mkobj
.kobj
, &mod
->mkobj
.mp
->grp
);
726 /* We are positive that no one is using any param
727 * attrs at this point. Deallocate immediately. */
728 free_module_param_attrs(&mod
->mkobj
);
733 void destroy_params(const struct kernel_param
*params
, unsigned num
)
737 for (i
= 0; i
< num
; i
++)
738 if (params
[i
].ops
->free
)
739 params
[i
].ops
->free(params
[i
].arg
);
742 static struct module_kobject
* __init
locate_module_kobject(const char *name
)
744 struct module_kobject
*mk
;
745 struct kobject
*kobj
;
748 kobj
= kset_find_obj(module_kset
, name
);
750 mk
= to_module_kobject(kobj
);
752 mk
= kzalloc(sizeof(struct module_kobject
), GFP_KERNEL
);
755 mk
->mod
= THIS_MODULE
;
756 mk
->kobj
.kset
= module_kset
;
757 err
= kobject_init_and_add(&mk
->kobj
, &module_ktype
, NULL
,
759 #ifdef CONFIG_MODULES
761 err
= sysfs_create_file(&mk
->kobj
, &module_uevent
.attr
);
764 kobject_put(&mk
->kobj
);
766 "Module '%s' failed add to sysfs, error number %d\n",
769 "The system will be unstable now.\n");
773 /* So that we hold reference in both cases. */
774 kobject_get(&mk
->kobj
);
780 static void __init
kernel_add_sysfs_param(const char *name
,
781 struct kernel_param
*kparam
,
782 unsigned int name_skip
)
784 struct module_kobject
*mk
;
787 mk
= locate_module_kobject(name
);
791 /* We need to remove old parameters before adding more. */
793 sysfs_remove_group(&mk
->kobj
, &mk
->mp
->grp
);
795 /* These should not fail at boot. */
796 err
= add_sysfs_param(mk
, kparam
, kparam
->name
+ name_skip
);
798 err
= sysfs_create_group(&mk
->kobj
, &mk
->mp
->grp
);
800 kobject_uevent(&mk
->kobj
, KOBJ_ADD
);
801 kobject_put(&mk
->kobj
);
805 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
807 * Add module_parameters to sysfs for "modules" built into the kernel.
809 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
810 * "parameter" name is stored behind a dot in kernel_param->name. So,
811 * extract the "module" name for all built-in kernel_param-eters,
812 * and for all who have the same, call kernel_add_sysfs_param.
814 static void __init
param_sysfs_builtin(void)
816 struct kernel_param
*kp
;
817 unsigned int name_len
;
818 char modname
[MODULE_NAME_LEN
];
820 for (kp
= __start___param
; kp
< __stop___param
; kp
++) {
826 dot
= strchr(kp
->name
, '.');
828 /* This happens for core_param() */
829 strcpy(modname
, "kernel");
832 name_len
= dot
- kp
->name
+ 1;
833 strlcpy(modname
, kp
->name
, name_len
);
835 kernel_add_sysfs_param(modname
, kp
, name_len
);
839 ssize_t
__modver_version_show(struct module_attribute
*mattr
,
840 struct module_kobject
*mk
, char *buf
)
842 struct module_version_attribute
*vattr
=
843 container_of(mattr
, struct module_version_attribute
, mattr
);
845 return sprintf(buf
, "%s\n", vattr
->version
);
848 extern const struct module_version_attribute
*__start___modver
[];
849 extern const struct module_version_attribute
*__stop___modver
[];
851 static void __init
version_sysfs_builtin(void)
853 const struct module_version_attribute
**p
;
854 struct module_kobject
*mk
;
857 for (p
= __start___modver
; p
< __stop___modver
; p
++) {
858 const struct module_version_attribute
*vattr
= *p
;
860 mk
= locate_module_kobject(vattr
->module_name
);
862 err
= sysfs_create_file(&mk
->kobj
, &vattr
->mattr
.attr
);
863 kobject_uevent(&mk
->kobj
, KOBJ_ADD
);
864 kobject_put(&mk
->kobj
);
869 /* module-related sysfs stuff */
871 static ssize_t
module_attr_show(struct kobject
*kobj
,
872 struct attribute
*attr
,
875 struct module_attribute
*attribute
;
876 struct module_kobject
*mk
;
879 attribute
= to_module_attr(attr
);
880 mk
= to_module_kobject(kobj
);
882 if (!attribute
->show
)
885 ret
= attribute
->show(attribute
, mk
, buf
);
890 static ssize_t
module_attr_store(struct kobject
*kobj
,
891 struct attribute
*attr
,
892 const char *buf
, size_t len
)
894 struct module_attribute
*attribute
;
895 struct module_kobject
*mk
;
898 attribute
= to_module_attr(attr
);
899 mk
= to_module_kobject(kobj
);
901 if (!attribute
->store
)
904 ret
= attribute
->store(attribute
, mk
, buf
, len
);
909 static const struct sysfs_ops module_sysfs_ops
= {
910 .show
= module_attr_show
,
911 .store
= module_attr_store
,
914 static int uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
916 struct kobj_type
*ktype
= get_ktype(kobj
);
918 if (ktype
== &module_ktype
)
923 static const struct kset_uevent_ops module_uevent_ops
= {
924 .filter
= uevent_filter
,
927 struct kset
*module_kset
;
928 int module_sysfs_initialized
;
930 struct kobj_type module_ktype
= {
931 .sysfs_ops
= &module_sysfs_ops
,
935 * param_sysfs_init - wrapper for built-in params support
937 static int __init
param_sysfs_init(void)
939 module_kset
= kset_create_and_add("module", &module_uevent_ops
, NULL
);
941 printk(KERN_WARNING
"%s (%d): error creating kset\n",
945 module_sysfs_initialized
= 1;
947 version_sysfs_builtin();
948 param_sysfs_builtin();
952 subsys_initcall(param_sysfs_init
);
954 #endif /* CONFIG_SYSFS */