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/config.h>
19 #include <linux/moduleparam.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/err.h>
30 #define DEBUGP(fmt, a...)
33 static inline int dash2underscore(char c)
40 static inline int parameq(const char *input, const char *paramname)
43 for (i = 0; dash2underscore(input[i]) == paramname[i]; i++)
49 static int parse_one(char *param,
51 struct kernel_param *params,
53 int (*handle_unknown)(char *param, char *val))
58 for (i = 0; i < num_params; i++) {
59 if (parameq(param, params[i].name)) {
60 DEBUGP("They are equal! Calling %p\n",
62 return params[i].set(val, ¶ms[i]);
67 DEBUGP("Unknown argument: calling %p\n", handle_unknown);
68 return handle_unknown(param, val);
71 DEBUGP("Unknown argument `%s'\n", param);
75 /* You can use " around spaces, but can't escape ". */
76 /* Hyphens and underscores equivalent in parameter names. */
77 static char *next_arg(char *args, char **param, char **val)
79 unsigned int i, equals = 0;
82 /* Chew any extra spaces */
83 while (*args == ' ') args++;
85 for (i = 0; args[i]; i++) {
86 if (args[i] == ' ' && !in_quote)
101 *val = args + equals + 1;
103 /* Don't include quotes in value. */
106 if (args[i-1] == '"')
118 /* Args looks like "foo=bar,bar2 baz=fuz wiz". */
119 int parse_args(const char *name,
121 struct kernel_param *params,
123 int (*unknown)(char *param, char *val))
127 DEBUGP("Parsing ARGS: %s\n", args);
132 args = next_arg(args, ¶m, &val);
133 ret = parse_one(param, val, params, num, unknown);
136 printk(KERN_ERR "%s: Unknown parameter `%s'\n",
141 "%s: `%s' too large for parameter `%s'\n",
142 name, val ?: "", param);
148 "%s: `%s' invalid for parameter `%s'\n",
149 name, val ?: "", param);
158 /* Lazy bastard, eh? */
159 #define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \
160 int param_set_##name(const char *val, struct kernel_param *kp) \
165 if (!val) return -EINVAL; \
166 l = strtolfn(val, &endp, 0); \
167 if (endp == val || ((type)l != l)) \
169 *((type *)kp->arg) = l; \
172 int param_get_##name(char *buffer, struct kernel_param *kp) \
174 return sprintf(buffer, format, *((type *)kp->arg)); \
177 STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, simple_strtoul);
178 STANDARD_PARAM_DEF(short, short, "%hi", long, simple_strtol);
179 STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, simple_strtoul);
180 STANDARD_PARAM_DEF(int, int, "%i", long, simple_strtol);
181 STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, simple_strtoul);
182 STANDARD_PARAM_DEF(long, long, "%li", long, simple_strtol);
183 STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, simple_strtoul);
185 int param_set_charp(const char *val, struct kernel_param *kp)
188 printk(KERN_ERR "%s: string parameter expected\n",
193 if (strlen(val) > 1024) {
194 printk(KERN_ERR "%s: string parameter too long\n",
199 *(char **)kp->arg = (char *)val;
203 int param_get_charp(char *buffer, struct kernel_param *kp)
205 return sprintf(buffer, "%s", *((char **)kp->arg));
208 int param_set_bool(const char *val, struct kernel_param *kp)
210 /* No equals means "set"... */
213 /* One of =[yYnN01] */
215 case 'y': case 'Y': case '1':
218 case 'n': case 'N': case '0':
225 int param_get_bool(char *buffer, struct kernel_param *kp)
227 /* Y and N chosen as being relatively non-coder friendly */
228 return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'Y' : 'N');
231 int param_set_invbool(const char *val, struct kernel_param *kp)
234 struct kernel_param dummy = { .arg = &boolval };
236 ret = param_set_bool(val, &dummy);
238 *(int *)kp->arg = !boolval;
242 int param_get_invbool(char *buffer, struct kernel_param *kp)
245 struct kernel_param dummy = { .arg = &val };
247 val = !*(int *)kp->arg;
248 return param_get_bool(buffer, &dummy);
251 /* We cheat here and temporarily mangle the string. */
252 int param_array(const char *name,
254 unsigned int min, unsigned int max,
255 void *elem, int elemsize,
256 int (*set)(const char *, struct kernel_param *kp),
260 struct kernel_param kp;
263 /* Get the name right for errors. */
267 /* No equals sign? */
269 printk(KERN_ERR "%s: expects arguments\n", name);
274 /* We expect a comma-separated list of values. */
279 printk(KERN_ERR "%s: can only take %i arguments\n",
283 len = strcspn(val, ",");
285 /* nul-terminate and parse */
287 ((char *)val)[len] = '\0';
295 } while (save == ',');
298 printk(KERN_ERR "%s: needs at least %i arguments\n",
305 int param_array_set(const char *val, struct kernel_param *kp)
307 struct kparam_array *arr = kp->arg;
309 return param_array(kp->name, val, 1, arr->max, arr->elem,
310 arr->elemsize, arr->set, arr->num ?: &arr->max);
313 int param_array_get(char *buffer, struct kernel_param *kp)
316 struct kparam_array *arr = kp->arg;
317 struct kernel_param p;
320 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) {
323 p.arg = arr->elem + arr->elemsize * i;
324 ret = arr->get(buffer + off, &p);
333 int param_set_copystring(const char *val, struct kernel_param *kp)
335 struct kparam_string *kps = kp->arg;
337 if (strlen(val)+1 > kps->maxlen) {
338 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n",
339 kp->name, kps->maxlen-1);
342 strcpy(kps->string, val);
346 int param_get_string(char *buffer, struct kernel_param *kp)
348 struct kparam_string *kps = kp->arg;
349 return strlcpy(buffer, kps->string, kps->maxlen);
352 /* sysfs output in /sys/modules/XYZ/parameters/ */
354 extern struct kernel_param __start___param[], __stop___param[];
356 #define MAX_KBUILD_MODNAME KOBJ_NAME_LEN
358 struct param_attribute
360 struct attribute attr;
361 struct kernel_param *param;
368 unsigned int num_attributes;
369 struct param_attribute attr[0];
372 #define to_param_attr(n) container_of(n, struct param_attribute, attr);
374 static ssize_t param_attr_show(struct kobject *kobj,
375 struct attribute *attr,
379 struct param_attribute *attribute = to_param_attr(attr);
381 if (!attribute->param->get)
384 count = attribute->param->get(buf, attribute->param);
392 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
393 static ssize_t param_attr_store(struct kobject *kobj,
394 struct attribute *attr,
395 const char *buf, size_t len)
398 struct param_attribute *attribute = to_param_attr(attr);
400 if (!attribute->param->set)
403 err = attribute->param->set(buf, attribute->param);
410 static struct sysfs_ops param_sysfs_ops = {
411 .show = param_attr_show,
412 .store = param_attr_store,
415 static void param_kobj_release(struct kobject *kobj)
417 kfree(container_of(kobj, struct param_kobject, kobj));
420 static struct kobj_type param_ktype = {
421 .sysfs_ops = ¶m_sysfs_ops,
422 .release = ¶m_kobj_release,
425 static struct kset param_kset = {
426 .subsys = &module_subsys,
427 .ktype = ¶m_ktype,
430 #ifdef CONFIG_MODULES
433 #define __modinit __init
437 * param_add_attribute - actually adds an parameter to sysfs
438 * @mod: owner of parameter
439 * @pk: param_kobject the attribute shall be assigned to.
440 * One per module, one per KBUILD_MODNAME.
441 * @kp: kernel_param to be added
442 * @skip: offset where the parameter name start in kp->name.
443 * Needed for built-in modules
445 * Fill in data into appropriate &pk->attr[], and create sysfs file.
447 static __modinit int param_add_attribute(struct module *mod,
448 struct param_kobject *pk,
449 struct kernel_param *kp,
452 struct param_attribute *a;
455 a = &pk->attr[pk->num_attributes];
456 a->attr.name = (char *) &kp->name[skip];
458 a->attr.mode = kp->perm;
460 err = sysfs_create_file(&pk->kobj, &a->attr);
462 pk->num_attributes++;
467 * param_sysfs_remove - remove sysfs support for one module or KBUILD_MODNAME
468 * @pk: struct param_kobject which is to be removed
470 * Called when an error in registration occurs or a module is removed
473 static __modinit void param_sysfs_remove(struct param_kobject *pk)
476 for (i = 0; i < pk->num_attributes; i++)
477 sysfs_remove_file(&pk->kobj,&pk->attr[i].attr);
479 /* Calls param_kobj_release */
480 kobject_unregister(&pk->kobj);
485 * param_sysfs_setup - setup sysfs support for one module or KBUILD_MODNAME
486 * @mk: struct module_kobject (contains parent kobject)
487 * @kparam: array of struct kernel_param, the actual parameter definitions
488 * @num_params: number of entries in array
489 * @name_skip: offset where the parameter name start in kparam[].name. Needed for built-in "modules"
491 * Create a kobject for a (per-module) group of parameters, and create files
492 * in sysfs. A pointer to the param_kobject is returned on success,
493 * NULL if there's no parameter to export, or other ERR_PTR(err).
495 static __modinit struct param_kobject *
496 param_sysfs_setup(struct module_kobject *mk,
497 struct kernel_param *kparam,
498 unsigned int num_params,
499 unsigned int name_skip)
501 struct param_kobject *pk;
502 unsigned int valid_attrs = 0;
506 for (i=0; i<num_params; i++) {
514 pk = kmalloc(sizeof(struct param_kobject)
515 + sizeof(struct param_attribute) * valid_attrs,
518 return ERR_PTR(-ENOMEM);
519 memset(pk, 0, sizeof(struct param_kobject)
520 + sizeof(struct param_attribute) * valid_attrs);
522 err = kobject_set_name(&pk->kobj, "parameters");
526 pk->kobj.kset = ¶m_kset;
527 pk->kobj.parent = &mk->kobj;
528 err = kobject_register(&pk->kobj);
532 for (i = 0; i < num_params; i++) {
533 if (kparam[i].perm) {
534 err = param_add_attribute(mk->mod, pk,
535 &kparam[i], name_skip);
544 param_sysfs_remove(pk);
553 #ifdef CONFIG_MODULES
556 * module_param_sysfs_setup - setup sysfs support for one module
558 * @kparam: module parameters (array)
559 * @num_params: number of module parameters
561 * Adds sysfs entries for module parameters, and creates a link from
562 * /sys/module/[mod->name]/parameters to /sys/parameters/[mod->name]/
564 int module_param_sysfs_setup(struct module *mod,
565 struct kernel_param *kparam,
566 unsigned int num_params)
568 struct param_kobject *pk;
570 pk = param_sysfs_setup(mod->mkobj, kparam, num_params, 0);
574 mod->params_kobject = pk;
579 * module_param_sysfs_remove - remove sysfs support for one module
582 * Remove sysfs entries for module parameters and the corresponding
585 void module_param_sysfs_remove(struct module *mod)
587 if (mod->params_kobject) {
588 param_sysfs_remove(mod->params_kobject);
589 mod->params_kobject = NULL;
595 * kernel_param_sysfs_setup - wrapper for built-in params support
597 static void __init kernel_param_sysfs_setup(const char *name,
598 struct kernel_param *kparam,
599 unsigned int num_params,
600 unsigned int name_skip)
602 struct module_kobject *mk;
604 mk = kmalloc(sizeof(struct module_kobject), GFP_KERNEL);
605 memset(mk, 0, sizeof(struct module_kobject));
607 mk->mod = THIS_MODULE;
608 kobj_set_kset_s(mk, module_subsys);
609 kobject_set_name(&mk->kobj, name);
610 kobject_register(&mk->kobj);
612 /* no need to keep the kobject if no parameter is exported */
613 if (!param_sysfs_setup(mk, kparam, num_params, name_skip))
614 kobject_unregister(&mk->kobj);
618 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules
620 * Add module_parameters to sysfs for "modules" built into the kernel.
622 * The "module" name (KBUILD_MODNAME) is stored before a dot, the
623 * "parameter" name is stored behind a dot in kernel_param->name. So,
624 * extract the "module" name for all built-in kernel_param-eters,
625 * and for all who have the same, call kernel_param_sysfs_setup.
627 static void __init param_sysfs_builtin(void)
629 struct kernel_param *kp, *kp_begin = NULL;
630 unsigned int i, name_len, count = 0;
631 char modname[MAX_KBUILD_MODNAME + 1] = "";
633 for (i=0; i < __stop___param - __start___param; i++) {
636 kp = &__start___param[i];
638 /* We do not handle args without periods. */
639 dot = memchr(kp->name, '.', MAX_KBUILD_MODNAME);
641 DEBUGP("couldn't find period in %s\n", kp->name);
644 name_len = dot - kp->name;
646 /* new kbuild_modname? */
647 if (strlen(modname) != name_len
648 || strncmp(modname, kp->name, name_len) != 0) {
649 /* add a new kobject for previous kernel_params. */
651 kernel_param_sysfs_setup(modname,
656 strncpy(modname, kp->name, name_len);
657 modname[name_len] = '\0';
664 /* last kernel_params need to be registered as well */
666 kernel_param_sysfs_setup(modname, kp_begin, count,
671 /* module-related sysfs stuff */
672 #ifdef CONFIG_MODULES
674 #define to_module_attr(n) container_of(n, struct module_attribute, attr);
675 #define to_module_kobject(n) container_of(n, struct module_kobject, kobj);
677 static ssize_t module_attr_show(struct kobject *kobj,
678 struct attribute *attr,
681 struct module_attribute *attribute;
682 struct module_kobject *mk;
685 attribute = to_module_attr(attr);
686 mk = to_module_kobject(kobj);
688 if (!attribute->show)
691 if (!try_module_get(mk->mod))
694 ret = attribute->show(mk->mod, buf);
701 static struct sysfs_ops module_sysfs_ops = {
702 .show = module_attr_show,
707 static struct sysfs_ops module_sysfs_ops = {
713 static void module_kobj_release(struct kobject *kobj)
715 kfree(container_of(kobj, struct module_kobject, kobj));
718 static struct kobj_type module_ktype = {
719 .sysfs_ops = &module_sysfs_ops,
720 .release = &module_kobj_release,
723 decl_subsys(module, &module_ktype, NULL);
726 * param_sysfs_init - wrapper for built-in params support
728 static int __init param_sysfs_init(void)
730 subsystem_register(&module_subsys);
731 kobject_set_name(¶m_kset.kobj, "parameters");
732 kset_init(¶m_kset);
734 param_sysfs_builtin();
738 __initcall(param_sysfs_init);
740 EXPORT_SYMBOL(param_set_byte);
741 EXPORT_SYMBOL(param_get_byte);
742 EXPORT_SYMBOL(param_set_short);
743 EXPORT_SYMBOL(param_get_short);
744 EXPORT_SYMBOL(param_set_ushort);
745 EXPORT_SYMBOL(param_get_ushort);
746 EXPORT_SYMBOL(param_set_int);
747 EXPORT_SYMBOL(param_get_int);
748 EXPORT_SYMBOL(param_set_uint);
749 EXPORT_SYMBOL(param_get_uint);
750 EXPORT_SYMBOL(param_set_long);
751 EXPORT_SYMBOL(param_get_long);
752 EXPORT_SYMBOL(param_set_ulong);
753 EXPORT_SYMBOL(param_get_ulong);
754 EXPORT_SYMBOL(param_set_charp);
755 EXPORT_SYMBOL(param_get_charp);
756 EXPORT_SYMBOL(param_set_bool);
757 EXPORT_SYMBOL(param_get_bool);
758 EXPORT_SYMBOL(param_set_invbool);
759 EXPORT_SYMBOL(param_get_invbool);
760 EXPORT_SYMBOL(param_array_set);
761 EXPORT_SYMBOL(param_array_get);
762 EXPORT_SYMBOL(param_set_copystring);
763 EXPORT_SYMBOL(param_get_string);