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/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/device.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/ctype.h>
28 /* Protects all built-in parameters, modules use their own param_lock */
29 static DEFINE_MUTEX(param_lock);
31 /* Use the module's mutex, or if built-in use the built-in mutex */
32 #define KPARAM_MUTEX(mod) ((mod) ? &(mod)->param_lock : ¶m_lock)
33 #define KPARAM_IS_LOCKED(mod) mutex_is_locked(KPARAM_MUTEX(mod))
35 /* This just allows us to keep track of which parameters are kmalloced. */
36 struct kmalloced_param {
37 struct list_head list;
40 static LIST_HEAD(kmalloced_params);
41 static DEFINE_SPINLOCK(kmalloced_params_lock);
43 static void *kmalloc_parameter(unsigned int size)
45 struct kmalloced_param *p;
47 p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
51 spin_lock(&kmalloced_params_lock);
52 list_add(&p->list, &kmalloced_params);
53 spin_unlock(&kmalloced_params_lock);
58 /* Does nothing if parameter wasn't kmalloced above. */
59 static void maybe_kfree_parameter(void *param)
61 struct kmalloced_param *p;
63 spin_lock(&kmalloced_params_lock);
64 list_for_each_entry(p, &kmalloced_params, list) {
65 if (p->val == param) {
71 spin_unlock(&kmalloced_params_lock);
74 static char dash2underscore(char c)
81 bool parameqn(const char *a, const char *b, size_t n)
85 for (i = 0; i < n; i++) {
86 if (dash2underscore(a[i]) != dash2underscore(b[i]))
92 bool parameq(const char *a, const char *b)
94 return parameqn(a, b, strlen(a)+1);
97 static void param_check_unsafe(const struct kernel_param *kp)
99 if (kp->flags & KERNEL_PARAM_FL_UNSAFE) {
100 pr_warn("Setting dangerous option %s - tainting kernel\n",
102 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
106 static int parse_one(char *param,
109 const struct kernel_param *params,
113 int (*handle_unknown)(char *param, char *val,
120 for (i = 0; i < num_params; i++) {
121 if (parameq(param, params[i].name)) {
122 if (params[i].level < min_level
123 || params[i].level > max_level)
125 /* No one handled NULL, so do it here. */
127 !(params[i].ops->flags & KERNEL_PARAM_OPS_FL_NOARG))
129 pr_debug("handling %s with %p\n", param,
131 kernel_param_lock(params[i].mod);
132 param_check_unsafe(¶ms[i]);
133 err = params[i].ops->set(val, ¶ms[i]);
134 kernel_param_unlock(params[i].mod);
139 if (handle_unknown) {
140 pr_debug("doing %s: %s='%s'\n", doing, param, val);
141 return handle_unknown(param, val, doing);
144 pr_debug("Unknown argument '%s'\n", param);
148 /* You can use " around spaces, but can't escape ". */
149 /* Hyphens and underscores equivalent in parameter names. */
150 static char *next_arg(char *args, char **param, char **val)
152 unsigned int i, equals = 0;
153 int in_quote = 0, quoted = 0;
162 for (i = 0; args[i]; i++) {
163 if (isspace(args[i]) && !in_quote)
170 in_quote = !in_quote;
178 *val = args + equals + 1;
180 /* Don't include quotes in value. */
183 if (args[i-1] == '"')
187 if (quoted && args[i-1] == '"')
196 /* Chew up trailing spaces. */
197 return skip_spaces(next);
200 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
201 char *parse_args(const char *doing,
203 const struct kernel_param *params,
207 int (*unknown)(char *param, char *val, const char *doing))
211 /* Chew leading spaces */
212 args = skip_spaces(args);
215 pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args);
219 int irq_was_disabled;
221 args = next_arg(args, ¶m, &val);
223 if (!val && strcmp(param, "--") == 0)
225 irq_was_disabled = irqs_disabled();
226 ret = parse_one(param, val, doing, params, num,
227 min_level, max_level, unknown);
228 if (irq_was_disabled && !irqs_disabled())
229 pr_warn("%s: option '%s' enabled irq's!\n",
234 pr_err("%s: Unknown parameter `%s'\n", doing, param);
237 pr_err("%s: `%s' too large for parameter `%s'\n",
238 doing, val ?: "", param);
243 pr_err("%s: `%s' invalid for parameter `%s'\n",
244 doing, val ?: "", param);
253 /* Lazy bastard, eh? */
254 #define STANDARD_PARAM_DEF(name, type, format, strtolfn) \
255 int param_set_##name(const char *val, const struct kernel_param *kp) \
257 return strtolfn(val, 0, (type *)kp->arg); \
259 int param_get_##name(char *buffer, const struct kernel_param *kp) \
261 return scnprintf(buffer, PAGE_SIZE, format, \
262 *((type *)kp->arg)); \
264 const struct kernel_param_ops param_ops_##name = { \
265 .set = param_set_##name, \
266 .get = param_get_##name, \
268 EXPORT_SYMBOL(param_set_##name); \
269 EXPORT_SYMBOL(param_get_##name); \
270 EXPORT_SYMBOL(param_ops_##name)
273 STANDARD_PARAM_DEF(byte, unsigned char, "%hhu", kstrtou8);
274 STANDARD_PARAM_DEF(short, short, "%hi", kstrtos16);
275 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", kstrtou16);
276 STANDARD_PARAM_DEF(int, int, "%i", kstrtoint);
277 STANDARD_PARAM_DEF(uint, unsigned int, "%u", kstrtouint);
278 STANDARD_PARAM_DEF(long, long, "%li", kstrtol);
279 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", kstrtoul);
280 STANDARD_PARAM_DEF(ullong, unsigned long long, "%llu", kstrtoull);
282 int param_set_charp(const char *val, const struct kernel_param *kp)
284 if (strlen(val) > 1024) {
285 pr_err("%s: string parameter too long\n", kp->name);
289 maybe_kfree_parameter(*(char **)kp->arg);
291 /* This is a hack. We can't kmalloc in early boot, and we
292 * don't need to; this mangled commandline is preserved. */
293 if (slab_is_available()) {
294 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
295 if (!*(char **)kp->arg)
297 strcpy(*(char **)kp->arg, val);
299 *(const char **)kp->arg = val;
303 EXPORT_SYMBOL(param_set_charp);
305 int param_get_charp(char *buffer, const struct kernel_param *kp)
307 return scnprintf(buffer, PAGE_SIZE, "%s", *((char **)kp->arg));
309 EXPORT_SYMBOL(param_get_charp);
311 static void param_free_charp(void *arg)
313 maybe_kfree_parameter(*((char **)arg));
316 const struct kernel_param_ops param_ops_charp = {
317 .set = param_set_charp,
318 .get = param_get_charp,
319 .free = param_free_charp,
321 EXPORT_SYMBOL(param_ops_charp);
323 /* Actually could be a bool or an int, for historical reasons. */
324 int param_set_bool(const char *val, const struct kernel_param *kp)
326 /* No equals means "set"... */
329 /* One of =[yYnN01] */
330 return strtobool(val, kp->arg);
332 EXPORT_SYMBOL(param_set_bool);
334 int param_get_bool(char *buffer, const struct kernel_param *kp)
336 /* Y and N chosen as being relatively non-coder friendly */
337 return sprintf(buffer, "%c", *(bool *)kp->arg ? 'Y' : 'N');
339 EXPORT_SYMBOL(param_get_bool);
341 const struct kernel_param_ops param_ops_bool = {
342 .flags = KERNEL_PARAM_OPS_FL_NOARG,
343 .set = param_set_bool,
344 .get = param_get_bool,
346 EXPORT_SYMBOL(param_ops_bool);
348 int param_set_bool_enable_only(const char *val, const struct kernel_param *kp)
352 bool orig_value = *(bool *)kp->arg;
353 struct kernel_param dummy_kp = *kp;
355 dummy_kp.arg = &new_value;
357 err = param_set_bool(val, &dummy_kp);
361 /* Don't let them unset it once it's set! */
362 if (!new_value && orig_value)
366 err = param_set_bool(val, kp);
370 EXPORT_SYMBOL_GPL(param_set_bool_enable_only);
372 const struct kernel_param_ops param_ops_bool_enable_only = {
373 .flags = KERNEL_PARAM_OPS_FL_NOARG,
374 .set = param_set_bool_enable_only,
375 .get = param_get_bool,
377 EXPORT_SYMBOL_GPL(param_ops_bool_enable_only);
379 /* This one must be bool. */
380 int param_set_invbool(const char *val, const struct kernel_param *kp)
384 struct kernel_param dummy;
386 dummy.arg = &boolval;
387 ret = param_set_bool(val, &dummy);
389 *(bool *)kp->arg = !boolval;
392 EXPORT_SYMBOL(param_set_invbool);
394 int param_get_invbool(char *buffer, const struct kernel_param *kp)
396 return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
398 EXPORT_SYMBOL(param_get_invbool);
400 const struct kernel_param_ops param_ops_invbool = {
401 .set = param_set_invbool,
402 .get = param_get_invbool,
404 EXPORT_SYMBOL(param_ops_invbool);
406 int param_set_bint(const char *val, const struct kernel_param *kp)
408 /* Match bool exactly, by re-using it. */
409 struct kernel_param boolkp = *kp;
415 ret = param_set_bool(val, &boolkp);
420 EXPORT_SYMBOL(param_set_bint);
422 const struct kernel_param_ops param_ops_bint = {
423 .flags = KERNEL_PARAM_OPS_FL_NOARG,
424 .set = param_set_bint,
425 .get = param_get_int,
427 EXPORT_SYMBOL(param_ops_bint);
429 /* We break the rule and mangle the string. */
430 static int param_array(struct module *mod,
433 unsigned int min, unsigned int max,
434 void *elem, int elemsize,
435 int (*set)(const char *, const struct kernel_param *kp),
440 struct kernel_param kp;
443 /* Get the name right for errors. */
449 /* We expect a comma-separated list of values. */
454 pr_err("%s: can only take %i arguments\n", name, max);
457 len = strcspn(val, ",");
459 /* nul-terminate and parse */
461 ((char *)val)[len] = '\0';
462 BUG_ON(!KPARAM_IS_LOCKED(mod));
470 } while (save == ',');
473 pr_err("%s: needs at least %i arguments\n", name, min);
479 static int param_array_set(const char *val, const struct kernel_param *kp)
481 const struct kparam_array *arr = kp->arr;
482 unsigned int temp_num;
484 return param_array(kp->mod, kp->name, val, 1, arr->max, arr->elem,
485 arr->elemsize, arr->ops->set, kp->level,
486 arr->num ?: &temp_num);
489 static int param_array_get(char *buffer, const struct kernel_param *kp)
492 const struct kparam_array *arr = kp->arr;
493 struct kernel_param p = *kp;
495 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
498 p.arg = arr->elem + arr->elemsize * i;
499 BUG_ON(!KPARAM_IS_LOCKED(p.mod));
500 ret = arr->ops->get(buffer + off, &p);
509 static void param_array_free(void *arg)
512 const struct kparam_array *arr = arg;
515 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
516 arr->ops->free(arr->elem + arr->elemsize * i);
519 const struct kernel_param_ops param_array_ops = {
520 .set = param_array_set,
521 .get = param_array_get,
522 .free = param_array_free,
524 EXPORT_SYMBOL(param_array_ops);
526 int param_set_copystring(const char *val, const struct kernel_param *kp)
528 const struct kparam_string *kps = kp->str;
530 if (strlen(val)+1 > kps->maxlen) {
531 pr_err("%s: string doesn't fit in %u chars.\n",
532 kp->name, kps->maxlen-1);
535 strcpy(kps->string, val);
538 EXPORT_SYMBOL(param_set_copystring);
540 int param_get_string(char *buffer, const struct kernel_param *kp)
542 const struct kparam_string *kps = kp->str;
543 return strlcpy(buffer, kps->string, kps->maxlen);
545 EXPORT_SYMBOL(param_get_string);
547 const struct kernel_param_ops param_ops_string = {
548 .set = param_set_copystring,
549 .get = param_get_string,
551 EXPORT_SYMBOL(param_ops_string);
553 /* sysfs output in /sys/modules/XYZ/parameters/ */
554 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
555 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
557 struct param_attribute
559 struct module_attribute mattr;
560 const struct kernel_param *param;
563 struct module_param_attrs
566 struct attribute_group grp;
567 struct param_attribute attrs[0];
571 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
573 static ssize_t param_attr_show(struct module_attribute *mattr,
574 struct module_kobject *mk, char *buf)
577 struct param_attribute *attribute = to_param_attr(mattr);
579 if (!attribute->param->ops->get)
582 kernel_param_lock(mk->mod);
583 count = attribute->param->ops->get(buf, attribute->param);
584 kernel_param_unlock(mk->mod);
592 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
593 static ssize_t param_attr_store(struct module_attribute *mattr,
594 struct module_kobject *mk,
595 const char *buf, size_t len)
598 struct param_attribute *attribute = to_param_attr(mattr);
600 if (!attribute->param->ops->set)
603 kernel_param_lock(mk->mod);
604 param_check_unsafe(attribute->param);
605 err = attribute->param->ops->set(buf, attribute->param);
606 kernel_param_unlock(mk->mod);
613 #ifdef CONFIG_MODULES
616 #define __modinit __init
619 void kernel_param_lock(struct module *mod)
621 mutex_lock(KPARAM_MUTEX(mod));
624 void kernel_param_unlock(struct module *mod)
626 mutex_unlock(KPARAM_MUTEX(mod));
630 EXPORT_SYMBOL(kernel_param_lock);
631 EXPORT_SYMBOL(kernel_param_unlock);
634 * add_sysfs_param - add a parameter to sysfs
635 * @mk: struct module_kobject
636 * @kparam: the actual parameter definition to add to sysfs
637 * @name: name of parameter
639 * Create a kobject if for a (per-module) parameter if mp NULL, and
640 * create file in sysfs. Returns an error on out of memory. Always cleans up
641 * if there's an error.
643 static __modinit int add_sysfs_param(struct module_kobject *mk,
644 const struct kernel_param *kp,
647 struct module_param_attrs *new_mp;
648 struct attribute **new_attrs;
651 /* We don't bother calling this with invisible parameters. */
655 /* First allocation. */
656 mk->mp = kzalloc(sizeof(*mk->mp), GFP_KERNEL);
659 mk->mp->grp.name = "parameters";
660 /* NULL-terminated attribute array. */
661 mk->mp->grp.attrs = kzalloc(sizeof(mk->mp->grp.attrs[0]),
663 /* Caller will cleanup via free_module_param_attrs */
664 if (!mk->mp->grp.attrs)
668 /* Enlarge allocations. */
669 new_mp = krealloc(mk->mp,
671 sizeof(mk->mp->attrs[0]) * (mk->mp->num + 1),
677 /* Extra pointer for NULL terminator */
678 new_attrs = krealloc(mk->mp->grp.attrs,
679 sizeof(mk->mp->grp.attrs[0]) * (mk->mp->num + 2),
683 mk->mp->grp.attrs = new_attrs;
685 /* Tack new one on the end. */
686 memset(&mk->mp->attrs[mk->mp->num], 0, sizeof(mk->mp->attrs[0]));
687 sysfs_attr_init(&mk->mp->attrs[mk->mp->num].mattr.attr);
688 mk->mp->attrs[mk->mp->num].param = kp;
689 mk->mp->attrs[mk->mp->num].mattr.show = param_attr_show;
690 /* Do not allow runtime DAC changes to make param writable. */
691 if ((kp->perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
692 mk->mp->attrs[mk->mp->num].mattr.store = param_attr_store;
694 mk->mp->attrs[mk->mp->num].mattr.store = NULL;
695 mk->mp->attrs[mk->mp->num].mattr.attr.name = (char *)name;
696 mk->mp->attrs[mk->mp->num].mattr.attr.mode = kp->perm;
699 /* Fix up all the pointers, since krealloc can move us */
700 for (i = 0; i < mk->mp->num; i++)
701 mk->mp->grp.attrs[i] = &mk->mp->attrs[i].mattr.attr;
702 mk->mp->grp.attrs[mk->mp->num] = NULL;
706 #ifdef CONFIG_MODULES
707 static void free_module_param_attrs(struct module_kobject *mk)
710 kfree(mk->mp->grp.attrs);
716 * module_param_sysfs_setup - setup sysfs support for one module
718 * @kparam: module parameters (array)
719 * @num_params: number of module parameters
721 * Adds sysfs entries for module parameters under
722 * /sys/module/[mod->name]/parameters/
724 int module_param_sysfs_setup(struct module *mod,
725 const struct kernel_param *kparam,
726 unsigned int num_params)
731 for (i = 0; i < num_params; i++) {
732 if (kparam[i].perm == 0)
734 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
736 free_module_param_attrs(&mod->mkobj);
745 /* Create the param group. */
746 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
748 free_module_param_attrs(&mod->mkobj);
753 * module_param_sysfs_remove - remove sysfs support for one module
756 * Remove sysfs entries for module parameters and the corresponding
759 void module_param_sysfs_remove(struct module *mod)
762 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
763 /* We are positive that no one is using any param
764 * attrs at this point. Deallocate immediately. */
765 free_module_param_attrs(&mod->mkobj);
770 void destroy_params(const struct kernel_param *params, unsigned num)
774 for (i = 0; i < num; i++)
775 if (params[i].ops->free)
776 params[i].ops->free(params[i].arg);
779 static struct module_kobject * __init locate_module_kobject(const char *name)
781 struct module_kobject *mk;
782 struct kobject *kobj;
785 kobj = kset_find_obj(module_kset, name);
787 mk = to_module_kobject(kobj);
789 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
792 mk->mod = THIS_MODULE;
793 mk->kobj.kset = module_kset;
794 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
796 #ifdef CONFIG_MODULES
798 err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
801 kobject_put(&mk->kobj);
802 pr_crit("Adding module '%s' to sysfs failed (%d), the system may be unstable.\n",
807 /* So that we hold reference in both cases. */
808 kobject_get(&mk->kobj);
814 static void __init kernel_add_sysfs_param(const char *name,
815 const struct kernel_param *kparam,
816 unsigned int name_skip)
818 struct module_kobject *mk;
821 mk = locate_module_kobject(name);
825 /* We need to remove old parameters before adding more. */
827 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
829 /* These should not fail at boot. */
830 err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
832 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
834 kobject_uevent(&mk->kobj, KOBJ_ADD);
835 kobject_put(&mk->kobj);
839 * param_sysfs_builtin - add sysfs parameters for built-in modules
841 * Add module_parameters to sysfs for "modules" built into the kernel.
843 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
844 * "parameter" name is stored behind a dot in kernel_param->name. So,
845 * extract the "module" name for all built-in kernel_param-eters,
846 * and for all who have the same, call kernel_add_sysfs_param.
848 static void __init param_sysfs_builtin(void)
850 const struct kernel_param *kp;
851 unsigned int name_len;
852 char modname[MODULE_NAME_LEN];
854 for (kp = __start___param; kp < __stop___param; kp++) {
860 dot = strchr(kp->name, '.');
862 /* This happens for core_param() */
863 strcpy(modname, "kernel");
866 name_len = dot - kp->name + 1;
867 strlcpy(modname, kp->name, name_len);
869 kernel_add_sysfs_param(modname, kp, name_len);
873 ssize_t __modver_version_show(struct module_attribute *mattr,
874 struct module_kobject *mk, char *buf)
876 struct module_version_attribute *vattr =
877 container_of(mattr, struct module_version_attribute, mattr);
879 return scnprintf(buf, PAGE_SIZE, "%s\n", vattr->version);
882 extern const struct module_version_attribute *__start___modver[];
883 extern const struct module_version_attribute *__stop___modver[];
885 static void __init version_sysfs_builtin(void)
887 const struct module_version_attribute **p;
888 struct module_kobject *mk;
891 for (p = __start___modver; p < __stop___modver; p++) {
892 const struct module_version_attribute *vattr = *p;
894 mk = locate_module_kobject(vattr->module_name);
896 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
898 kobject_uevent(&mk->kobj, KOBJ_ADD);
899 kobject_put(&mk->kobj);
904 /* module-related sysfs stuff */
906 static ssize_t module_attr_show(struct kobject *kobj,
907 struct attribute *attr,
910 struct module_attribute *attribute;
911 struct module_kobject *mk;
914 attribute = to_module_attr(attr);
915 mk = to_module_kobject(kobj);
917 if (!attribute->show)
920 ret = attribute->show(attribute, mk, buf);
925 static ssize_t module_attr_store(struct kobject *kobj,
926 struct attribute *attr,
927 const char *buf, size_t len)
929 struct module_attribute *attribute;
930 struct module_kobject *mk;
933 attribute = to_module_attr(attr);
934 mk = to_module_kobject(kobj);
936 if (!attribute->store)
939 ret = attribute->store(attribute, mk, buf, len);
944 static const struct sysfs_ops module_sysfs_ops = {
945 .show = module_attr_show,
946 .store = module_attr_store,
949 static int uevent_filter(struct kset *kset, struct kobject *kobj)
951 struct kobj_type *ktype = get_ktype(kobj);
953 if (ktype == &module_ktype)
958 static const struct kset_uevent_ops module_uevent_ops = {
959 .filter = uevent_filter,
962 struct kset *module_kset;
963 int module_sysfs_initialized;
965 static void module_kobj_release(struct kobject *kobj)
967 struct module_kobject *mk = to_module_kobject(kobj);
968 complete(mk->kobj_completion);
971 struct kobj_type module_ktype = {
972 .release = module_kobj_release,
973 .sysfs_ops = &module_sysfs_ops,
977 * param_sysfs_init - wrapper for built-in params support
979 static int __init param_sysfs_init(void)
981 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
983 printk(KERN_WARNING "%s (%d): error creating kset\n",
987 module_sysfs_initialized = 1;
989 version_sysfs_builtin();
990 param_sysfs_builtin();
994 subsys_initcall(param_sysfs_init);
996 #endif /* CONFIG_SYSFS */