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 static int ignore_unknown(char *param, char *val)
226 int module_parse_kernel_cmdline(const char *name, const struct kernel_param *params, unsigned num)
230 size_t name_len = strlen(name);
231 struct kernel_param new_params[num];
232 char args[strlen(saved_command_line) + 1];
237 strcpy(args, saved_command_line);
238 memcpy(new_params, params, sizeof(struct kernel_param) * num);
240 for (i = 0; i < num; i++)
241 new_params[i].name = NULL;
242 for (i = 0; i < num; i++) {
243 char *new_name = kmalloc(strlen(params[i].name) + name_len + 2, GFP_KERNEL);
248 sprintf(new_name, "%s.%s", name, params[i].name);
249 new_params[i].name = new_name;
252 ret = parse_args(name, args, new_params, num, ignore_unknown);
255 for (i = 0; i < num; i++)
256 if (new_params[i].name)
257 kfree(new_params[i].name);
262 /* Lazy bastard, eh? */
263 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
264 int param_set_##name(const char *val, const struct kernel_param *kp) \
269 ret = strtolfn(val, 0, &l); \
270 if (ret == -EINVAL || ((type)l != l)) \
272 *((type *)kp->arg) = l; \
275 int param_get_##name(char *buffer, const struct kernel_param *kp) \
277 return sprintf(buffer, format, *((type *)kp->arg)); \
279 struct kernel_param_ops param_ops_##name = { \
280 .set = param_set_##name, \
281 .get = param_get_##name, \
283 EXPORT_SYMBOL(param_set_##name); \
284 EXPORT_SYMBOL(param_get_##name); \
285 EXPORT_SYMBOL(param_ops_##name)
288 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, strict_strtoul);
289 STANDARD_PARAM_DEF(short, short, "%hi", long, strict_strtol);
290 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, strict_strtoul);
291 STANDARD_PARAM_DEF(int, int, "%i", long, strict_strtol);
292 STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, strict_strtoul);
293 STANDARD_PARAM_DEF(long, long, "%li", long, strict_strtol);
294 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, strict_strtoul);
296 int param_set_charp(const char *val, const struct kernel_param *kp)
298 if (strlen(val) > 1024) {
299 printk(KERN_ERR "%s: string parameter too long\n",
304 maybe_kfree_parameter(*(char **)kp->arg);
306 /* This is a hack. We can't kmalloc in early boot, and we
307 * don't need to; this mangled commandline is preserved. */
308 if (slab_is_available()) {
309 *(char **)kp->arg = kmalloc_parameter(strlen(val)+1);
310 if (!*(char **)kp->arg)
312 strcpy(*(char **)kp->arg, val);
314 *(const char **)kp->arg = val;
318 EXPORT_SYMBOL(param_set_charp);
320 int param_get_charp(char *buffer, const struct kernel_param *kp)
322 return sprintf(buffer, "%s", *((char **)kp->arg));
324 EXPORT_SYMBOL(param_get_charp);
326 static void param_free_charp(void *arg)
328 maybe_kfree_parameter(*((char **)arg));
331 struct kernel_param_ops param_ops_charp = {
332 .set = param_set_charp,
333 .get = param_get_charp,
334 .free = param_free_charp,
336 EXPORT_SYMBOL(param_ops_charp);
338 /* Actually could be a bool or an int, for historical reasons. */
339 int param_set_bool(const char *val, const struct kernel_param *kp)
344 /* No equals means "set"... */
347 /* One of =[yYnN01] */
348 ret = strtobool(val, &v);
352 if (kp->flags & KPARAM_ISBOOL)
353 *(bool *)kp->arg = v;
358 EXPORT_SYMBOL(param_set_bool);
360 int param_get_bool(char *buffer, const struct kernel_param *kp)
363 if (kp->flags & KPARAM_ISBOOL)
364 val = *(bool *)kp->arg;
366 val = *(int *)kp->arg;
368 /* Y and N chosen as being relatively non-coder friendly */
369 return sprintf(buffer, "%c", val ? 'Y' : 'N');
371 EXPORT_SYMBOL(param_get_bool);
373 struct kernel_param_ops param_ops_bool = {
374 .set = param_set_bool,
375 .get = param_get_bool,
377 EXPORT_SYMBOL(param_ops_bool);
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 dummy.flags = KPARAM_ISBOOL;
388 ret = param_set_bool(val, &dummy);
390 *(bool *)kp->arg = !boolval;
393 EXPORT_SYMBOL(param_set_invbool);
395 int param_get_invbool(char *buffer, const struct kernel_param *kp)
397 return sprintf(buffer, "%c", (*(bool *)kp->arg) ? 'N' : 'Y');
399 EXPORT_SYMBOL(param_get_invbool);
401 struct kernel_param_ops param_ops_invbool = {
402 .set = param_set_invbool,
403 .get = param_get_invbool,
405 EXPORT_SYMBOL(param_ops_invbool);
407 /* We break the rule and mangle the string. */
408 static int param_array(const char *name,
410 unsigned int min, unsigned int max,
411 void *elem, int elemsize,
412 int (*set)(const char *, const struct kernel_param *kp),
417 struct kernel_param kp;
420 /* Get the name right for errors. */
426 /* We expect a comma-separated list of values. */
431 printk(KERN_ERR "%s: can only take %i arguments\n",
435 len = strcspn(val, ",");
437 /* nul-terminate and parse */
439 ((char *)val)[len] = '\0';
440 BUG_ON(!mutex_is_locked(¶m_lock));
448 } while (save == ',');
451 printk(KERN_ERR "%s: needs at least %i arguments\n",
458 static int param_array_set(const char *val, const struct kernel_param *kp)
460 const struct kparam_array *arr = kp->arr;
461 unsigned int temp_num;
463 return param_array(kp->name, val, 1, arr->max, arr->elem,
464 arr->elemsize, arr->ops->set, kp->flags,
465 arr->num ?: &temp_num);
468 static int param_array_get(char *buffer, const struct kernel_param *kp)
471 const struct kparam_array *arr = kp->arr;
472 struct kernel_param p;
475 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
478 p.arg = arr->elem + arr->elemsize * i;
479 BUG_ON(!mutex_is_locked(¶m_lock));
480 ret = arr->ops->get(buffer + off, &p);
489 static void param_array_free(void *arg)
492 const struct kparam_array *arr = arg;
495 for (i = 0; i < (arr->num ? *arr->num : arr->max); i++)
496 arr->ops->free(arr->elem + arr->elemsize * i);
499 struct kernel_param_ops param_array_ops = {
500 .set = param_array_set,
501 .get = param_array_get,
502 .free = param_array_free,
504 EXPORT_SYMBOL(param_array_ops);
506 int param_set_copystring(const char *val, const struct kernel_param *kp)
508 const struct kparam_string *kps = kp->str;
510 if (strlen(val)+1 > kps->maxlen) {
511 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
512 kp->name, kps->maxlen-1);
515 strcpy(kps->string, val);
518 EXPORT_SYMBOL(param_set_copystring);
520 int param_get_string(char *buffer, const struct kernel_param *kp)
522 const struct kparam_string *kps = kp->str;
523 return strlcpy(buffer, kps->string, kps->maxlen);
525 EXPORT_SYMBOL(param_get_string);
527 struct kernel_param_ops param_ops_string = {
528 .set = param_set_copystring,
529 .get = param_get_string,
531 EXPORT_SYMBOL(param_ops_string);
533 /* sysfs output in /sys/modules/XYZ/parameters/ */
534 #define to_module_attr(n) container_of(n, struct module_attribute, attr)
535 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj)
537 extern struct kernel_param __start___param[], __stop___param[];
539 struct param_attribute
541 struct module_attribute mattr;
542 const struct kernel_param *param;
545 struct module_param_attrs
548 struct attribute_group grp;
549 struct param_attribute attrs[0];
553 #define to_param_attr(n) container_of(n, struct param_attribute, mattr)
555 static ssize_t param_attr_show(struct module_attribute *mattr,
556 struct module *mod, char *buf)
559 struct param_attribute *attribute = to_param_attr(mattr);
561 if (!attribute->param->ops->get)
564 mutex_lock(¶m_lock);
565 count = attribute->param->ops->get(buf, attribute->param);
566 mutex_unlock(¶m_lock);
574 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
575 static ssize_t param_attr_store(struct module_attribute *mattr,
576 struct module *owner,
577 const char *buf, size_t len)
580 struct param_attribute *attribute = to_param_attr(mattr);
582 if (!attribute->param->ops->set)
585 mutex_lock(¶m_lock);
586 err = attribute->param->ops->set(buf, attribute->param);
587 mutex_unlock(¶m_lock);
594 #ifdef CONFIG_MODULES
597 #define __modinit __init
601 void __kernel_param_lock(void)
603 mutex_lock(¶m_lock);
605 EXPORT_SYMBOL(__kernel_param_lock);
607 void __kernel_param_unlock(void)
609 mutex_unlock(¶m_lock);
611 EXPORT_SYMBOL(__kernel_param_unlock);
614 * add_sysfs_param - add a parameter to sysfs
615 * @mk: struct module_kobject
616 * @kparam: the actual parameter definition to add to sysfs
617 * @name: name of parameter
619 * Create a kobject if for a (per-module) parameter if mp NULL, and
620 * create file in sysfs. Returns an error on out of memory. Always cleans up
621 * if there's an error.
623 static __modinit int add_sysfs_param(struct module_kobject *mk,
624 const struct kernel_param *kp,
627 struct module_param_attrs *new;
628 struct attribute **attrs;
631 /* We don't bother calling this with invisible parameters. */
639 attrs = mk->mp->grp.attrs;
643 new = krealloc(mk->mp,
644 sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
651 attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
657 /* Sysfs wants everything zeroed. */
658 memset(new, 0, sizeof(*new));
659 memset(&new->attrs[num], 0, sizeof(new->attrs[num]));
660 memset(&attrs[num], 0, sizeof(attrs[num]));
661 new->grp.name = "parameters";
662 new->grp.attrs = attrs;
664 /* Tack new one on the end. */
665 sysfs_attr_init(&new->attrs[num].mattr.attr);
666 new->attrs[num].param = kp;
667 new->attrs[num].mattr.show = param_attr_show;
668 new->attrs[num].mattr.store = param_attr_store;
669 new->attrs[num].mattr.attr.name = (char *)name;
670 new->attrs[num].mattr.attr.mode = kp->perm;
673 /* Fix up all the pointers, since krealloc can move us */
674 for (num = 0; num < new->num; num++)
675 new->grp.attrs[num] = &new->attrs[num].mattr.attr;
676 new->grp.attrs[num] = NULL;
688 #ifdef CONFIG_MODULES
689 static void free_module_param_attrs(struct module_kobject *mk)
691 kfree(mk->mp->grp.attrs);
697 * module_param_sysfs_setup - setup sysfs support for one module
699 * @kparam: module parameters (array)
700 * @num_params: number of module parameters
702 * Adds sysfs entries for module parameters under
703 * /sys/module/[mod->name]/parameters/
705 int module_param_sysfs_setup(struct module *mod,
706 const struct kernel_param *kparam,
707 unsigned int num_params)
712 for (i = 0; i < num_params; i++) {
713 if (kparam[i].perm == 0)
715 err = add_sysfs_param(&mod->mkobj, &kparam[i], kparam[i].name);
724 /* Create the param group. */
725 err = sysfs_create_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
727 free_module_param_attrs(&mod->mkobj);
732 * module_param_sysfs_remove - remove sysfs support for one module
735 * Remove sysfs entries for module parameters and the corresponding
738 void module_param_sysfs_remove(struct module *mod)
741 sysfs_remove_group(&mod->mkobj.kobj, &mod->mkobj.mp->grp);
742 /* We are positive that no one is using any param
743 * attrs at this point. Deallocate immediately. */
744 free_module_param_attrs(&mod->mkobj);
749 void destroy_params(const struct kernel_param *params, unsigned num)
753 for (i = 0; i < num; i++)
754 if (params[i].ops->free)
755 params[i].ops->free(params[i].arg);
758 static struct module_kobject * __init locate_module_kobject(const char *name)
760 struct module_kobject *mk;
761 struct kobject *kobj;
764 kobj = kset_find_obj(module_kset, name);
766 mk = to_module_kobject(kobj);
768 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
771 mk->mod = THIS_MODULE;
772 mk->kobj.kset = module_kset;
773 err = kobject_init_and_add(&mk->kobj, &module_ktype, NULL,
776 kobject_put(&mk->kobj);
778 "Module '%s' failed add to sysfs, error number %d\n",
781 "The system will be unstable now.\n");
785 /* So that we hold reference in both cases. */
786 kobject_get(&mk->kobj);
792 static void __init kernel_add_sysfs_param(const char *name,
793 struct kernel_param *kparam,
794 unsigned int name_skip)
796 struct module_kobject *mk;
799 mk = locate_module_kobject(name);
803 /* We need to remove old parameters before adding more. */
805 sysfs_remove_group(&mk->kobj, &mk->mp->grp);
807 /* These should not fail at boot. */
808 err = add_sysfs_param(mk, kparam, kparam->name + name_skip);
810 err = sysfs_create_group(&mk->kobj, &mk->mp->grp);
812 kobject_uevent(&mk->kobj, KOBJ_ADD);
813 kobject_put(&mk->kobj);
817 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
819 * Add module_parameters to sysfs for "modules" built into the kernel.
821 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
822 * "parameter" name is stored behind a dot in kernel_param->name. So,
823 * extract the "module" name for all built-in kernel_param-eters,
824 * and for all who have the same, call kernel_add_sysfs_param.
826 static void __init param_sysfs_builtin(void)
828 struct kernel_param *kp;
829 unsigned int name_len;
830 char modname[MODULE_NAME_LEN];
832 for (kp = __start___param; kp < __stop___param; kp++) {
838 dot = strchr(kp->name, '.');
840 /* This happens for core_param() */
841 strcpy(modname, "kernel");
844 name_len = dot - kp->name + 1;
845 strlcpy(modname, kp->name, name_len);
847 kernel_add_sysfs_param(modname, kp, name_len);
851 ssize_t __modver_version_show(struct module_attribute *mattr,
852 struct module *mod, char *buf)
854 struct module_version_attribute *vattr =
855 container_of(mattr, struct module_version_attribute, mattr);
857 return sprintf(buf, "%s\n", vattr->version);
860 extern const struct module_version_attribute *__start___modver[];
861 extern const struct module_version_attribute *__stop___modver[];
863 static void __init version_sysfs_builtin(void)
865 const struct module_version_attribute **p;
866 struct module_kobject *mk;
869 for (p = __start___modver; p < __stop___modver; p++) {
870 const struct module_version_attribute *vattr = *p;
872 mk = locate_module_kobject(vattr->module_name);
874 err = sysfs_create_file(&mk->kobj, &vattr->mattr.attr);
875 kobject_uevent(&mk->kobj, KOBJ_ADD);
876 kobject_put(&mk->kobj);
881 /* module-related sysfs stuff */
883 static ssize_t module_attr_show(struct kobject *kobj,
884 struct attribute *attr,
887 struct module_attribute *attribute;
888 struct module_kobject *mk;
891 attribute = to_module_attr(attr);
892 mk = to_module_kobject(kobj);
894 if (!attribute->show)
897 ret = attribute->show(attribute, mk->mod, buf);
902 static ssize_t module_attr_store(struct kobject *kobj,
903 struct attribute *attr,
904 const char *buf, size_t len)
906 struct module_attribute *attribute;
907 struct module_kobject *mk;
910 attribute = to_module_attr(attr);
911 mk = to_module_kobject(kobj);
913 if (!attribute->store)
916 ret = attribute->store(attribute, mk->mod, buf, len);
921 static const struct sysfs_ops module_sysfs_ops = {
922 .show = module_attr_show,
923 .store = module_attr_store,
926 static int uevent_filter(struct kset *kset, struct kobject *kobj)
928 struct kobj_type *ktype = get_ktype(kobj);
930 if (ktype == &module_ktype)
935 static const struct kset_uevent_ops module_uevent_ops = {
936 .filter = uevent_filter,
939 struct kset *module_kset;
940 int module_sysfs_initialized;
942 struct kobj_type module_ktype = {
943 .sysfs_ops = &module_sysfs_ops,
947 * param_sysfs_init - wrapper for built-in params support
949 static int __init param_sysfs_init(void)
951 module_kset = kset_create_and_add("module", &module_uevent_ops, NULL);
953 printk(KERN_WARNING "%s (%d): error creating kset\n",
957 module_sysfs_initialized = 1;
959 version_sysfs_builtin();
960 param_sysfs_builtin();
964 subsys_initcall(param_sysfs_init);
966 #endif /* CONFIG_SYSFS */