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 /* 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 inline char dash2underscore(char c)
77 static inline int parameq(const char *input, const char *paramname)
80 for (i = 0; dash2underscore(input[i]) == paramname[i]; i++)
86 static int parse_one(char *param,
88 const struct kernel_param *params,
90 int (*handle_unknown)(char *param, char *val))
96 for (i = 0; i < num_params; i++) {
97 if (parameq(param, params[i].name)) {
98 /* No one handled NULL, so do it here. */
99 if (!val && params[i].ops->set != param_set_bool)
101 DEBUGP("They are equal! Calling %p\n",
103 mutex_lock(¶m_lock);
104 err = params[i].ops->set(val, ¶ms[i]);
105 mutex_unlock(¶m_lock);
110 if (handle_unknown) {
111 DEBUGP("Unknown argument: calling %p\n", handle_unknown);
112 return handle_unknown(param, val);
115 DEBUGP("Unknown argument `%s'\n", param);
119 /* You can use " around spaces, but can't escape ". */
120 /* Hyphens and underscores equivalent in parameter names. */
121 static char *next_arg(char *args, char **param, char **val)
123 unsigned int i, equals = 0;
124 int in_quote = 0, quoted = 0;
133 for (i = 0; args[i]; i++) {
134 if (isspace(args[i]) && !in_quote)
141 in_quote = !in_quote;
149 *val = args + equals + 1;
151 /* Don't include quotes in value. */
154 if (args[i-1] == '"')
157 if (quoted && args[i-1] == '"')
167 /* Chew up trailing spaces. */
168 return skip_spaces(next);
171 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
172 int parse_args(const char *name,
174 const struct kernel_param *params,
176 int (*unknown)(char *param, char *val))
180 DEBUGP("Parsing ARGS: %s\n", args);
182 /* Chew leading spaces */
183 args = skip_spaces(args);
187 int irq_was_disabled;
189 args = next_arg(args, ¶m, &val);
190 irq_was_disabled = irqs_disabled();
191 ret = parse_one(param, val, params, num, unknown);
192 if (irq_was_disabled && !irqs_disabled()) {
193 printk(KERN_WARNING "parse_args(): option '%s' enabled "
198 printk(KERN_ERR "%s: Unknown parameter `%s'\n",
203 "%s: `%s' too large for parameter `%s'\n",
204 name, val ?: "", param);
210 "%s: `%s' invalid for parameter `%s'\n",
211 name, val ?: "", param);
220 #ifdef CONFIG_RK_CONFIG
221 int module_parse_kernel_cmdline(const char *name, const struct kernel_param *params, unsigned num)
225 size_t name_len = strlen(name);
226 struct kernel_param new_params[num];
227 char args[strlen(saved_command_line) + 1];
232 strcpy(args, saved_command_line);
233 memcpy(new_params, params, sizeof(struct kernel_param) * num);
235 for (i = 0; i < num; i++)
236 new_params[i].name = NULL;
237 for (i = 0; i < num; i++) {
238 char *new_name = kmalloc(strlen(params[i].name) + name_len + 2, GFP_KERNEL);
243 sprintf(new_name, "%s.%s", name, params[i].name);
244 new_params[i].name = new_name;
247 ret = parse_args(name, args, new_params, num, NULL);
250 for (i = 0; i < num; i++)
251 if (new_params[i].name)
252 kfree(new_params[i].name);
257 /* Lazy bastard, eh? */
258 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
259 int param_set_##name(const char *val, const struct kernel_param *kp) \
264 ret = strtolfn(val, 0, &l); \
265 if (ret == -EINVAL || ((type)l != l)) \
267 *((type *)kp->arg) = l; \
270 int param_get_##name(char *buffer, const struct kernel_param *kp) \
272 return sprintf(buffer, format, *((type *)kp->arg)); \
274 struct kernel_param_ops param_ops_##name = { \
275 .set = param_set_##name, \
276 .get = param_get_##name, \
278 EXPORT_SYMBOL(param_set_##name); \
279 EXPORT_SYMBOL(param_get_##name); \
280 EXPORT_SYMBOL(param_ops_##name)
283 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
284 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
285 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
286 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
287 STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul);
288 STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
289 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
291 int param_set_charp(const char *val, const struct kernel_param *kp)
293 if (strlen(val) > 1024) {
294 printk(KERN_ERR "%s: string parameter too long\n",
299 maybe_kfree_parameter(*(char **)kp->arg);
301 /* This is a hack. We can't kmalloc in early boot, and we
302 * don't need to; this mangled commandline is preserved. */
303 if (slab_is_available()) {
304 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
305 if (!*(char **)kp->arg)
307 strcpy(*(char **)kp->arg, val);
309 *(const char **)kp->arg = val;
313 EXPORT_SYMBOL(param_set_charp);
315 int param_get_charp(char *buffer, const struct kernel_param *kp)
317 return sprintf(buffer, "%s", *((char **)kp->arg));
319 EXPORT_SYMBOL(param_get_charp);
321 static void param_free_charp(void *arg)
323 maybe_kfree_parameter(*((char **)arg));
326 struct kernel_param_ops param_ops_charp = {
327 .set = param_set_charp,
328 .get = param_get_charp,
329 .free = param_free_charp,
331 EXPORT_SYMBOL(param_ops_charp);
333 /* Actually could be a bool or an int, for historical reasons. */
334 int param_set_bool(const char *val, const struct kernel_param *kp)
339 /* No equals means "set"... */
342 /* One of =[yYnN01] */
343 ret = strtobool(val, &v);
347 if (kp->flags & KPARAM_ISBOOL)
348 *(bool *)kp->arg = v;
353 EXPORT_SYMBOL(param_set_bool);
355 int param_get_bool(char *buffer, const struct kernel_param *kp)
358 if (kp->flags & KPARAM_ISBOOL)
359 val = *(bool *)kp->arg;
361 val = *(int *)kp->arg;
363 /* Y and N chosen as being relatively non-coder friendly */
364 return sprintf(buffer, "%c", val ? 'Y' : 'N');
366 EXPORT_SYMBOL(param_get_bool);
368 struct kernel_param_ops param_ops_bool = {
369 .set = param_set_bool,
370 .get = param_get_bool,
372 EXPORT_SYMBOL(param_ops_bool);
374 /* This one must be bool. */
375 int param_set_invbool(const char *val, const struct kernel_param *kp)
379 struct kernel_param dummy;
381 dummy.arg = &boolval;
382 dummy.flags = KPARAM_ISBOOL;
383 ret = param_set_bool(val, &dummy);
385 *(bool *)kp->arg = !boolval;
388 EXPORT_SYMBOL(param_set_invbool);
390 int param_get_invbool(char *buffer, const struct kernel_param *kp)
392 return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
394 EXPORT_SYMBOL(param_get_invbool);
396 struct kernel_param_ops param_ops_invbool = {
397 .set = param_set_invbool,
398 .get = param_get_invbool,
400 EXPORT_SYMBOL(param_ops_invbool);
402 /* We break the rule and mangle the string. */
403 static int param_array(const char *name,
405 unsigned int min, unsigned int max,
406 void *elem, int elemsize,
407 int (*set)(const char *, const struct kernel_param *kp),
412 struct kernel_param kp;
415 /* Get the name right for errors. */
421 /* We expect a comma-separated list of values. */
426 printk(KERN_ERR "%s: can only take %i arguments\n",
430 len = strcspn(val, ",");
432 /* nul-terminate and parse */
434 ((char *)val)[len] = '\0';
435 BUG_ON(!mutex_is_locked(¶m_lock));
443 } while (save == ',');
446 printk(KERN_ERR "%s: needs at least %i arguments\n",
453 static int param_array_set(const char *val, const struct kernel_param *kp)
455 const struct kparam_array *arr = kp->arr;
456 unsigned int temp_num;
458 return param_array(kp->name, val, 1, arr->max, arr->elem,
459 arr->elemsize, arr->ops->set, kp->flags,
460 arr->num ?: &temp_num);
463 static int param_array_get(char *buffer, const struct kernel_param *kp)
466 const struct kparam_array *arr = kp->arr;
467 struct kernel_param p;
470 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
473 p.arg = arr->elem + arr->elemsize * i;
474 BUG_ON(!mutex_is_locked(¶m_lock));
475 ret = arr->ops->get(buffer + off, &p);
484 static void param_array_free(void *arg)
487 const struct kparam_array *arr = arg;
490 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
491 arr->ops->free(arr->elem + arr->elemsize * i);
494 struct kernel_param_ops param_array_ops = {
495 .set = param_array_set,
496 .get = param_array_get,
497 .free = param_array_free,
499 EXPORT_SYMBOL(param_array_ops);
501 int param_set_copystring(const char *val, const struct kernel_param *kp)
503 const struct kparam_string *kps = kp->str;
505 if (strlen(val)+1 > kps->maxlen) {
506 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
507 kp->name, kps->maxlen-1);
510 strcpy(kps->string, val);
513 EXPORT_SYMBOL(param_set_copystring);
515 int param_get_string(char *buffer, const struct kernel_param *kp)
517 const struct kparam_string *kps = kp->str;
518 return strlcpy(buffer, kps->string, kps->maxlen);
520 EXPORT_SYMBOL(param_get_string);
522 struct kernel_param_ops param_ops_string = {
523 .set = param_set_copystring,
524 .get = param_get_string,
526 EXPORT_SYMBOL(param_ops_string);
528 /* sysfs output in /sys/modules/XYZ/parameters/ */
529 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
530 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
532 extern struct kernel_param __start___param[], __stop___param[];
534 struct param_attribute
536 struct module_attribute mattr;
537 const struct kernel_param *param;
540 struct module_param_attrs
543 struct attribute_group grp;
544 struct param_attribute attrs[0];
548 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
550 static ssize_t param_attr_show(struct module_attribute *mattr,
551 struct module *mod, char *buf)
554 struct param_attribute *attribute = to_param_attr(mattr);
556 if (!attribute->param->ops->get)
559 mutex_lock(¶m_lock);
560 count = attribute->param->ops->get(buf, attribute->param);
561 mutex_unlock(¶m_lock);
569 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
570 static ssize_t param_attr_store(struct module_attribute *mattr,
571 struct module *owner,
572 const char *buf, size_t len)
575 struct param_attribute *attribute = to_param_attr(mattr);
577 if (!attribute->param->ops->set)
580 mutex_lock(¶m_lock);
581 err = attribute->param->ops->set(buf, attribute->param);
582 mutex_unlock(¶m_lock);
589 #ifdef CONFIG_MODULES
592 #define __modinit __init
596 void __kernel_param_lock(void)
598 mutex_lock(¶m_lock);
600 EXPORT_SYMBOL(__kernel_param_lock);
602 void __kernel_param_unlock(void)
604 mutex_unlock(¶m_lock);
606 EXPORT_SYMBOL(__kernel_param_unlock);
609 * add_sysfs_param - add a parameter to sysfs
610 * @mk: struct module_kobject
611 * @kparam: the actual parameter definition to add to sysfs
612 * @name: name of parameter
614 * Create a kobject if for a (per-module) parameter if mp NULL, and
615 * create file in sysfs. Returns an error on out of memory. Always cleans up
616 * if there's an error.
618 static __modinit int add_sysfs_param(struct module_kobject *mk,
619 const struct kernel_param *kp,
622 struct module_param_attrs *new;
623 struct attribute **attrs;
626 /* We don't bother calling this with invisible parameters. */
634 attrs = mk->mp->grp.attrs;
638 new = krealloc(mk->mp,
639 sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
646 attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
652 /* Sysfs wants everything zeroed. */
653 memset(new, 0, sizeof(*new));
654 memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
655 memset(&attrs[num], 0, sizeof(attrs[num]));
656 new->grp.name = "parameters";
657 new->grp.attrs = attrs;
659 /* Tack new one on the end. */
660 sysfs_attr_init(&new->attrs[num].mattr.attr);
661 new->attrs[num].param = kp;
662 new->attrs[num].mattr.show = param_attr_show;
663 new->attrs[num].mattr.store = param_attr_store;
664 new->attrs[num].mattr.attr.name = (char *)name;
665 new->attrs[num].mattr.attr.mode = kp->perm;
668 /* Fix up all the pointers, since krealloc can move us */
669 for (num = 0; num < new->num; num++)
670 new->grp.attrs[num] = &new->attrs[num].mattr.attr;
671 new->grp.attrs[num] = NULL;
683 #ifdef CONFIG_MODULES
684 static void free_module_param_attrs(struct module_kobject *mk)
686 kfree(mk->mp->grp.attrs);
692 * module_param_sysfs_setup - setup sysfs support for one module
694 * @kparam: module parameters (array)
695 * @num_params: number of module parameters
697 * Adds sysfs entries for module parameters under
698 * /sys/module/[mod->name]/parameters/
700 int module_param_sysfs_setup(struct module *mod,
701 const struct kernel_param *kparam,
702 unsigned int num_params)
707 for (i = 0; i < num_params; i++) {
708 if (kparam[i].perm == 0)
710 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
719 /* Create the param group. */
720 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
722 free_module_param_attrs(&mod->mkobj);
727 * module_param_sysfs_remove - remove sysfs support for one module
730 * Remove sysfs entries for module parameters and the corresponding
733 void module_param_sysfs_remove(struct module *mod)
736 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
737 /* We are positive that no one is using any param
738 * attrs at this point. Deallocate immediately. */
739 free_module_param_attrs(&mod->mkobj);
744 void destroy_params(const struct kernel_param *params, unsigned num)
748 for (i = 0; i < num; i++)
749 if (params[i].ops->free)
750 params[i].ops->free(params[i].arg);
753 static struct module_kobject * __init locate_module_kobject(const char *name)
755 struct module_kobject *mk;
756 struct kobject *kobj;
759 kobj = kset_find_obj(module_kset, name);
761 mk = to_module_kobject(kobj);
763 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
766 mk->mod = THIS_MODULE;
767 mk->kobj.kset = module_kset;
768 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
771 kobject_put(&mk->kobj);
773 "Module '%s' failed add to sysfs, error number %d\n",
776 "The system will be unstable now.\n");
780 /* So that we hold reference in both cases. */
781 kobject_get(&mk->kobj);
787 static void __init kernel_add_sysfs_param(const char *name,
788 struct kernel_param *kparam,
789 unsigned int name_skip)
791 struct module_kobject *mk;
794 mk = locate_module_kobject(name);
798 /* We need to remove old parameters before adding more. */
800 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
802 /* These should not fail at boot. */
803 err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
805 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
807 kobject_uevent(&mk->kobj, KOBJ_ADD);
808 kobject_put(&mk->kobj);
812 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
814 * Add module_parameters to sysfs for "modules" built into the kernel.
816 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
817 * "parameter" name is stored behind a dot in kernel_param->name. So,
818 * extract the "module" name for all built-in kernel_param-eters,
819 * and for all who have the same, call kernel_add_sysfs_param.
821 static void __init param_sysfs_builtin(void)
823 struct kernel_param *kp;
824 unsigned int name_len;
825 char modname[MODULE_NAME_LEN];
827 for (kp = __start___param; kp < __stop___param; kp++) {
833 dot = strchr(kp->name, '.');
835 /* This happens for core_param() */
836 strcpy(modname, "kernel");
839 name_len = dot - kp->name + 1;
840 strlcpy(modname, kp->name, name_len);
842 kernel_add_sysfs_param(modname, kp, name_len);
846 ssize_t __modver_version_show(struct module_attribute *mattr,
847 struct module *mod, char *buf)
849 struct module_version_attribute *vattr =
850 container_of(mattr, struct module_version_attribute, mattr);
852 return sprintf(buf, "%s\n", vattr->version);
855 extern const struct module_version_attribute *__start___modver[];
856 extern const struct module_version_attribute *__stop___modver[];
858 static void __init version_sysfs_builtin(void)
860 const struct module_version_attribute **p;
861 struct module_kobject *mk;
864 for (p = __start___modver; p < __stop___modver; p++) {
865 const struct module_version_attribute *vattr = *p;
867 mk = locate_module_kobject(vattr->module_name);
869 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
870 kobject_uevent(&mk->kobj, KOBJ_ADD);
871 kobject_put(&mk->kobj);
876 /* module-related sysfs stuff */
878 static ssize_t module_attr_show(struct kobject *kobj,
879 struct attribute *attr,
882 struct module_attribute *attribute;
883 struct module_kobject *mk;
886 attribute = to_module_attr(attr);
887 mk = to_module_kobject(kobj);
889 if (!attribute->show)
892 ret = attribute->show(attribute, mk->mod, buf);
897 static ssize_t module_attr_store(struct kobject *kobj,
898 struct attribute *attr,
899 const char *buf, size_t len)
901 struct module_attribute *attribute;
902 struct module_kobject *mk;
905 attribute = to_module_attr(attr);
906 mk = to_module_kobject(kobj);
908 if (!attribute->store)
911 ret = attribute->store(attribute, mk->mod, buf, len);
916 static const struct sysfs_ops module_sysfs_ops = {
917 .show = module_attr_show,
918 .store = module_attr_store,
921 static int uevent_filter(struct kset *kset, struct kobject *kobj)
923 struct kobj_type *ktype = get_ktype(kobj);
925 if (ktype == &module_ktype)
930 static const struct kset_uevent_ops module_uevent_ops = {
931 .filter = uevent_filter,
934 struct kset *module_kset;
935 int module_sysfs_initialized;
937 struct kobj_type module_ktype = {
938 .sysfs_ops = &module_sysfs_ops,
942 * param_sysfs_init - wrapper for built-in params support
944 static int __init param_sysfs_init(void)
946 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
948 printk(KERN_WARNING "%s (%d): error creating kset\n",
952 module_sysfs_initialized = 1;
954 version_sysfs_builtin();
955 param_sysfs_builtin();
959 subsys_initcall(param_sysfs_init);
961 #endif /* CONFIG_SYSFS */