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>
31 #define DEBUGP(fmt, a...)
34 /* Protects all parameters, and incidentally kmalloced_param list. */
35 static DEFINE_MUTEX(param_lock);
37 /* This just allows us to keep track of which parameters are kmalloced. */
38 struct kmalloced_param {
39 struct list_head list;
42 static LIST_HEAD(kmalloced_params);
44 static void *kmalloc_parameter(unsigned int size)
46 struct kmalloced_param *p;
48 p = kmalloc(sizeof(*p) + size, GFP_KERNEL);
52 list_add(&p->list, &kmalloced_params);
56 /* Does nothing if parameter wasn't kmalloced above. */
57 static void maybe_kfree_parameter(void *param)
59 struct kmalloced_param *p;
61 list_for_each_entry(p, &kmalloced_params, list) {
62 if (p->val == param) {
70 static char dash2underscore(char c)
77 bool parameqn(const char *a, const char *b, size_t n)
81 for (i = 0; i < n; i++) {
82 if (dash2underscore(a[i]) != dash2underscore(b[i]))
88 bool parameq(const char *a, const char *b)
90 return parameqn(a, b, strlen(a)+1);
93 static int parse_one(char *param,
95 const struct kernel_param *params,
97 int (*handle_arg)(char *param, char *val, int known))
103 for (i = 0; i < num_params; i++) {
104 if (parameq(param, params[i].name)) {
105 /* No one handled NULL, so do it here. */
106 if (!val && params[i].ops->set != param_set_bool)
110 DEBUGP("Valid argument: calling %p\n",
112 ret = handle_arg(param, val, 1);
116 DEBUGP("They are equal! Calling %p\n",
118 mutex_lock(¶m_lock);
119 err = params[i].ops->set(val, ¶ms[i]);
120 mutex_unlock(¶m_lock);
126 DEBUGP("Unknown argument: calling %p\n", handle_arg);
127 return handle_arg(param, val, 0);
130 DEBUGP("Unknown argument `%s'\n", param);
134 /* You can use " around spaces, but can't escape ". */
135 /* Hyphens and underscores equivalent in parameter names. */
136 static char *next_arg(char *args, char **param, char **val)
138 unsigned int i, equals = 0;
139 int in_quote = 0, quoted = 0;
148 for (i = 0; args[i]; i++) {
149 if (isspace(args[i]) && !in_quote)
156 in_quote = !in_quote;
164 *val = args + equals + 1;
166 /* Don't include quotes in value. */
169 if (args[i-1] == '"')
172 if (quoted && args[i-1] == '"')
182 /* Chew up trailing spaces. */
183 return skip_spaces(next);
186 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
187 int parse_args(const char *name,
189 const struct kernel_param *params,
191 int (*handle_arg)(char *param, char *val, int arg))
195 DEBUGP("Parsing ARGS: %s\n", args);
197 /* Chew leading spaces */
198 args = skip_spaces(args);
202 int irq_was_disabled;
204 args = next_arg(args, ¶m, &val);
205 irq_was_disabled = irqs_disabled();
206 ret = parse_one(param, val, params, num, handle_arg);
207 if (irq_was_disabled && !irqs_disabled()) {
208 printk(KERN_WARNING "parse_args(): option '%s' enabled "
213 printk(KERN_ERR "%s: Unknown parameter `%s'\n",
218 "%s: `%s' too large for parameter `%s'\n",
219 name, val ?: "", param);
225 "%s: `%s' invalid for parameter `%s'\n",
226 name, val ?: "", param);
235 /* Lazy bastard, eh? */
236 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
237 int param_set_##name(const char *val, const struct kernel_param *kp) \
242 ret = strtolfn(val, 0, &l); \
243 if (ret < 0 || ((type)l != l)) \
244 return ret < 0 ? ret : -EINVAL; \
245 *((type *)kp->arg) = l; \
248 int param_get_##name(char *buffer, const struct kernel_param *kp) \
250 return sprintf(buffer, format, *((type *)kp->arg)); \
252 struct kernel_param_ops param_ops_##name = { \
253 .set = param_set_##name, \
254 .get = param_get_##name, \
256 EXPORT_SYMBOL(param_set_##name); \
257 EXPORT_SYMBOL(param_get_##name); \
258 EXPORT_SYMBOL(param_ops_##name)
261 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
262 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
263 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
264 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
265 STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul);
266 STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
267 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
269 int param_set_charp(const char *val, const struct kernel_param *kp)
271 if (strlen(val) > 1024) {
272 printk(KERN_ERR "%s: string parameter too long\n",
277 maybe_kfree_parameter(*(char **)kp->arg);
279 /* This is a hack. We can't kmalloc in early boot, and we
280 * don't need to; this mangled commandline is preserved. */
281 if (slab_is_available()) {
282 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
283 if (!*(char **)kp->arg)
285 strcpy(*(char **)kp->arg, val);
287 *(const char **)kp->arg = val;
291 EXPORT_SYMBOL(param_set_charp);
293 int param_get_charp(char *buffer, const struct kernel_param *kp)
295 return sprintf(buffer, "%s", *((char **)kp->arg));
297 EXPORT_SYMBOL(param_get_charp);
299 static void param_free_charp(void *arg)
301 maybe_kfree_parameter(*((char **)arg));
304 struct kernel_param_ops param_ops_charp = {
305 .set = param_set_charp,
306 .get = param_get_charp,
307 .free = param_free_charp,
309 EXPORT_SYMBOL(param_ops_charp);
311 /* Actually could be a bool or an int, for historical reasons. */
312 int param_set_bool(const char *val, const struct kernel_param *kp)
317 /* No equals means "set"... */
320 /* One of =[yYnN01] */
321 ret = strtobool(val, &v);
325 if (kp->flags & KPARAM_ISBOOL)
326 *(bool *)kp->arg = v;
331 EXPORT_SYMBOL(param_set_bool);
333 int param_get_bool(char *buffer, const struct kernel_param *kp)
336 if (kp->flags & KPARAM_ISBOOL)
337 val = *(bool *)kp->arg;
339 val = *(int *)kp->arg;
341 /* Y and N chosen as being relatively non-coder friendly */
342 return sprintf(buffer, "%c", val ? 'Y' : 'N');
344 EXPORT_SYMBOL(param_get_bool);
346 struct kernel_param_ops param_ops_bool = {
347 .set = param_set_bool,
348 .get = param_get_bool,
350 EXPORT_SYMBOL(param_ops_bool);
352 /* This one must be bool. */
353 int param_set_invbool(const char *val, const struct kernel_param *kp)
357 struct kernel_param dummy;
359 dummy.arg = &boolval;
360 dummy.flags = KPARAM_ISBOOL;
361 ret = param_set_bool(val, &dummy);
363 *(bool *)kp->arg = !boolval;
366 EXPORT_SYMBOL(param_set_invbool);
368 int param_get_invbool(char *buffer, const struct kernel_param *kp)
370 return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
372 EXPORT_SYMBOL(param_get_invbool);
374 struct kernel_param_ops param_ops_invbool = {
375 .set = param_set_invbool,
376 .get = param_get_invbool,
378 EXPORT_SYMBOL(param_ops_invbool);
380 /* We break the rule and mangle the string. */
381 static int param_array(const char *name,
383 unsigned int min, unsigned int max,
384 void *elem, int elemsize,
385 int (*set)(const char *, const struct kernel_param *kp),
390 struct kernel_param kp;
393 /* Get the name right for errors. */
399 /* We expect a comma-separated list of values. */
404 printk(KERN_ERR "%s: can only take %i arguments\n",
408 len = strcspn(val, ",");
410 /* nul-terminate and parse */
412 ((char *)val)[len] = '\0';
413 BUG_ON(!mutex_is_locked(¶m_lock));
421 } while (save == ',');
424 printk(KERN_ERR "%s: needs at least %i arguments\n",
431 static int param_array_set(const char *val, const struct kernel_param *kp)
433 const struct kparam_array *arr = kp->arr;
434 unsigned int temp_num;
436 return param_array(kp->name, val, 1, arr->max, arr->elem,
437 arr->elemsize, arr->ops->set, kp->flags,
438 arr->num ?: &temp_num);
441 static int param_array_get(char *buffer, const struct kernel_param *kp)
444 const struct kparam_array *arr = kp->arr;
445 struct kernel_param p;
448 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
451 p.arg = arr->elem + arr->elemsize * i;
452 BUG_ON(!mutex_is_locked(¶m_lock));
453 ret = arr->ops->get(buffer + off, &p);
462 static void param_array_free(void *arg)
465 const struct kparam_array *arr = arg;
468 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
469 arr->ops->free(arr->elem + arr->elemsize * i);
472 struct kernel_param_ops param_array_ops = {
473 .set = param_array_set,
474 .get = param_array_get,
475 .free = param_array_free,
477 EXPORT_SYMBOL(param_array_ops);
479 int param_set_copystring(const char *val, const struct kernel_param *kp)
481 const struct kparam_string *kps = kp->str;
483 if (strlen(val)+1 > kps->maxlen) {
484 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
485 kp->name, kps->maxlen-1);
488 strcpy(kps->string, val);
491 EXPORT_SYMBOL(param_set_copystring);
493 int param_get_string(char *buffer, const struct kernel_param *kp)
495 const struct kparam_string *kps = kp->str;
496 return strlcpy(buffer, kps->string, kps->maxlen);
498 EXPORT_SYMBOL(param_get_string);
500 struct kernel_param_ops param_ops_string = {
501 .set = param_set_copystring,
502 .get = param_get_string,
504 EXPORT_SYMBOL(param_ops_string);
506 /* sysfs output in /sys/modules/XYZ/parameters/ */
507 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
508 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
510 extern struct kernel_param __start___param[], __stop___param[];
512 struct param_attribute
514 struct module_attribute mattr;
515 const struct kernel_param *param;
518 struct module_param_attrs
521 struct attribute_group grp;
522 struct param_attribute attrs[0];
526 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
528 static ssize_t param_attr_show(struct module_attribute *mattr,
529 struct module_kobject *mk, char *buf)
532 struct param_attribute *attribute = to_param_attr(mattr);
534 if (!attribute->param->ops->get)
537 mutex_lock(¶m_lock);
538 count = attribute->param->ops->get(buf, attribute->param);
539 mutex_unlock(¶m_lock);
547 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
548 static ssize_t param_attr_store(struct module_attribute *mattr,
549 struct module_kobject *km,
550 const char *buf, size_t len)
553 struct param_attribute *attribute = to_param_attr(mattr);
555 if (!attribute->param->ops->set)
558 mutex_lock(¶m_lock);
559 err = attribute->param->ops->set(buf, attribute->param);
560 mutex_unlock(¶m_lock);
567 #ifdef CONFIG_MODULES
570 #define __modinit __init
574 void __kernel_param_lock(void)
576 mutex_lock(¶m_lock);
578 EXPORT_SYMBOL(__kernel_param_lock);
580 void __kernel_param_unlock(void)
582 mutex_unlock(¶m_lock);
584 EXPORT_SYMBOL(__kernel_param_unlock);
587 * add_sysfs_param - add a parameter to sysfs
588 * @mk: struct module_kobject
589 * @kparam: the actual parameter definition to add to sysfs
590 * @name: name of parameter
592 * Create a kobject if for a (per-module) parameter if mp NULL, and
593 * create file in sysfs. Returns an error on out of memory. Always cleans up
594 * if there's an error.
596 static __modinit int add_sysfs_param(struct module_kobject *mk,
597 const struct kernel_param *kp,
600 struct module_param_attrs *new;
601 struct attribute **attrs;
604 /* We don't bother calling this with invisible parameters. */
612 attrs = mk->mp->grp.attrs;
616 new = krealloc(mk->mp,
617 sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
624 attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
630 /* Sysfs wants everything zeroed. */
631 memset(new, 0, sizeof(*new));
632 memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
633 memset(&attrs[num], 0, sizeof(attrs[num]));
634 new->grp.name = "parameters";
635 new->grp.attrs = attrs;
637 /* Tack new one on the end. */
638 sysfs_attr_init(&new->attrs[num].mattr.attr);
639 new->attrs[num].param = kp;
640 new->attrs[num].mattr.show = param_attr_show;
641 new->attrs[num].mattr.store = param_attr_store;
642 new->attrs[num].mattr.attr.name = (char *)name;
643 new->attrs[num].mattr.attr.mode = kp->perm;
646 /* Fix up all the pointers, since krealloc can move us */
647 for (num = 0; num < new->num; num++)
648 new->grp.attrs[num] = &new->attrs[num].mattr.attr;
649 new->grp.attrs[num] = NULL;
661 #ifdef CONFIG_MODULES
662 static void free_module_param_attrs(struct module_kobject *mk)
664 kfree(mk->mp->grp.attrs);
670 * module_param_sysfs_setup - setup sysfs support for one module
672 * @kparam: module parameters (array)
673 * @num_params: number of module parameters
675 * Adds sysfs entries for module parameters under
676 * /sys/module/[mod->name]/parameters/
678 int module_param_sysfs_setup(struct module *mod,
679 const struct kernel_param *kparam,
680 unsigned int num_params)
685 for (i = 0; i < num_params; i++) {
686 if (kparam[i].perm == 0)
688 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
697 /* Create the param group. */
698 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
700 free_module_param_attrs(&mod->mkobj);
705 * module_param_sysfs_remove - remove sysfs support for one module
708 * Remove sysfs entries for module parameters and the corresponding
711 void module_param_sysfs_remove(struct module *mod)
714 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
715 /* We are positive that no one is using any param
716 * attrs at this point. Deallocate immediately. */
717 free_module_param_attrs(&mod->mkobj);
722 void destroy_params(const struct kernel_param *params, unsigned num)
726 for (i = 0; i < num; i++)
727 if (params[i].ops->free)
728 params[i].ops->free(params[i].arg);
731 static struct module_kobject * __init locate_module_kobject(const char *name)
733 struct module_kobject *mk;
734 struct kobject *kobj;
737 kobj = kset_find_obj(module_kset, name);
739 mk = to_module_kobject(kobj);
741 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
744 mk->mod = THIS_MODULE;
745 mk->kobj.kset = module_kset;
746 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
748 #ifdef CONFIG_MODULES
750 err = sysfs_create_file(&mk->kobj, &module_uevent.attr);
753 kobject_put(&mk->kobj);
755 "Module '%s' failed add to sysfs, error number %d\n",
758 "The system will be unstable now.\n");
762 /* So that we hold reference in both cases. */
763 kobject_get(&mk->kobj);
769 static void __init kernel_add_sysfs_param(const char *name,
770 struct kernel_param *kparam,
771 unsigned int name_skip)
773 struct module_kobject *mk;
776 mk = locate_module_kobject(name);
780 /* We need to remove old parameters before adding more. */
782 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
784 /* These should not fail at boot. */
785 err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
787 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
789 kobject_uevent(&mk->kobj, KOBJ_ADD);
790 kobject_put(&mk->kobj);
794 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
796 * Add module_parameters to sysfs for "modules" built into the kernel.
798 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
799 * "parameter" name is stored behind a dot in kernel_param->name. So,
800 * extract the "module" name for all built-in kernel_param-eters,
801 * and for all who have the same, call kernel_add_sysfs_param.
803 static void __init param_sysfs_builtin(void)
805 struct kernel_param *kp;
806 unsigned int name_len;
807 char modname[MODULE_NAME_LEN];
809 for (kp = __start___param; kp < __stop___param; kp++) {
815 dot = strchr(kp->name, '.');
817 /* This happens for core_param() */
818 strcpy(modname, "kernel");
821 name_len = dot - kp->name + 1;
822 strlcpy(modname, kp->name, name_len);
824 kernel_add_sysfs_param(modname, kp, name_len);
828 ssize_t __modver_version_show(struct module_attribute *mattr,
829 struct module_kobject *mk, char *buf)
831 struct module_version_attribute *vattr =
832 container_of(mattr, struct module_version_attribute, mattr);
834 return sprintf(buf, "%s\n", vattr->version);
837 extern const struct module_version_attribute *__start___modver[];
838 extern const struct module_version_attribute *__stop___modver[];
840 static void __init version_sysfs_builtin(void)
842 const struct module_version_attribute **p;
843 struct module_kobject *mk;
846 for (p = __start___modver; p < __stop___modver; p++) {
847 const struct module_version_attribute *vattr = *p;
849 mk = locate_module_kobject(vattr->module_name);
851 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
852 kobject_uevent(&mk->kobj, KOBJ_ADD);
853 kobject_put(&mk->kobj);
858 /* module-related sysfs stuff */
860 static ssize_t module_attr_show(struct kobject *kobj,
861 struct attribute *attr,
864 struct module_attribute *attribute;
865 struct module_kobject *mk;
868 attribute = to_module_attr(attr);
869 mk = to_module_kobject(kobj);
871 if (!attribute->show)
874 ret = attribute->show(attribute, mk, buf);
879 static ssize_t module_attr_store(struct kobject *kobj,
880 struct attribute *attr,
881 const char *buf, size_t len)
883 struct module_attribute *attribute;
884 struct module_kobject *mk;
887 attribute = to_module_attr(attr);
888 mk = to_module_kobject(kobj);
890 if (!attribute->store)
893 ret = attribute->store(attribute, mk, buf, len);
898 static const struct sysfs_ops module_sysfs_ops = {
899 .show = module_attr_show,
900 .store = module_attr_store,
903 static int uevent_filter(struct kset *kset, struct kobject *kobj)
905 struct kobj_type *ktype = get_ktype(kobj);
907 if (ktype == &module_ktype)
912 static const struct kset_uevent_ops module_uevent_ops = {
913 .filter = uevent_filter,
916 struct kset *module_kset;
917 int module_sysfs_initialized;
919 struct kobj_type module_ktype = {
920 .sysfs_ops = &module_sysfs_ops,
924 * param_sysfs_init - wrapper for built-in params support
926 static int __init param_sysfs_init(void)
928 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
930 printk(KERN_WARNING "%s (%d): error creating kset\n",
934 module_sysfs_initialized = 1;
936 version_sysfs_builtin();
937 param_sysfs_builtin();
941 subsys_initcall(param_sysfs_init);
943 #endif /* CONFIG_SYSFS */