1/* Helpers for initial module or kernel cmdline parsing 2 Copyright (C) 2001 Rusty Russell. 3 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. 8 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. 13 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 17*/ 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 27#define DEBUGP(fmt, a...) 28 29static inline char dash2underscore(char c) 30{ 31 if (c == '-') 32 return '_'; 33 return c; 34} 35 36static inline int parameq(const char *input, const char *paramname) 37{ 38 unsigned int i; 39 for (i = 0; dash2underscore(input[i]) == paramname[i]; i++) 40 if (input[i] == '\0') 41 return 1; 42 return 0; 43} 44 45static int parse_one(char *param, 46 char *val, 47 struct kernel_param *params, 48 unsigned num_params, 49 int (*handle_unknown)(char *param, char *val)) 50{ 51 unsigned int i; 52 53 /* Find parameter */ 54 for (i = 0; i < num_params; i++) { 55 if (parameq(param, params[i].name)) { 56 DEBUGP("They are equal! Calling %p\n", 57 params[i].set); 58 return params[i].set(val, ¶ms[i]); 59 } 60 } 61 62 if (handle_unknown) { 63 DEBUGP("Unknown argument: calling %p\n", handle_unknown); 64 return handle_unknown(param, val); 65 } 66 67 DEBUGP("Unknown argument `%s'\n", param); 68 return -ENOENT; 69} 70 71/* You can use " around spaces, but can't escape ". */ 72/* Hyphens and underscores equivalent in parameter names. */ 73static char *next_arg(char *args, char **param, char **val) 74{ 75 unsigned int i, equals = 0; 76 int in_quote = 0, quoted = 0; 77 char *next; 78 79 if (*args == '"') { 80 args++; 81 in_quote = 1; 82 quoted = 1; 83 } 84 85 for (i = 0; args[i]; i++) { 86 if (args[i] == ' ' && !in_quote) 87 break; 88 if (equals == 0) { 89 if (args[i] == '=') 90 equals = i; 91 } 92 if (args[i] == '"') 93 in_quote = !in_quote; 94 } 95 96 *param = args; 97 if (!equals) 98 *val = NULL; 99 else { 100 args[equals] = '\0'; 101 *val = args + equals + 1; 102 103 /* Don't include quotes in value. */ 104 if (**val == '"') { 105 (*val)++; 106 if (args[i-1] == '"') 107 args[i-1] = '\0'; 108 } 109 if (quoted && args[i-1] == '"') 110 args[i-1] = '\0'; 111 } 112 113 if (args[i]) { 114 args[i] = '\0'; 115 next = args + i + 1; 116 } else 117 next = args + i; 118 119 /* Chew up trailing spaces. */ 120 while (*next == ' ') 121 next++; 122 return next; 123} 124 125/* Args looks like "foo=bar,bar2 baz=fuz wiz". */ 126int parse_args(const char *name, 127 char *args, 128 struct kernel_param *params, 129 unsigned num, 130 int (*unknown)(char *param, char *val)) 131{ 132 char *param, *val; 133 134 DEBUGP("Parsing ARGS: %s\n", args); 135 136 /* Chew leading spaces */ 137 while (*args == ' ') 138 args++; 139 140 while (*args) { 141 int ret; 142 int irq_was_disabled; 143 144 args = next_arg(args, ¶m, &val); 145 irq_was_disabled = irqs_disabled(); 146 ret = parse_one(param, val, params, num, unknown); 147 if (irq_was_disabled && !irqs_disabled()) { 148 printk(KERN_WARNING "parse_args(): option '%s' enabled " 149 "irq's!\n", param); 150 } 151 switch (ret) { 152 case -ENOENT: 153 printk(KERN_ERR "%s: Unknown parameter `%s'\n", 154 name, param); 155 return ret; 156 case -ENOSPC: 157 printk(KERN_ERR 158 "%s: `%s' too large for parameter `%s'\n", 159 name, val ?: "", param); 160 return ret; 161 case 0: 162 break; 163 default: 164 printk(KERN_ERR 165 "%s: `%s' invalid for parameter `%s'\n", 166 name, val ?: "", param); 167 return ret; 168 } 169 } 170 171 /* All parsed OK. */ 172 return 0; 173} 174 175/* Lazy bastard, eh? */ 176#define STANDARD_PARAM_DEF(name, type, format, tmptype, strtolfn) \ 177 int param_set_##name(const char *val, struct kernel_param *kp) \ 178 { \ 179 char *endp; \ 180 tmptype l; \ 181 \ 182 if (!val) return -EINVAL; \ 183 l = strtolfn(val, &endp, 0); \ 184 if (endp == val || ((type)l != l)) \ 185 return -EINVAL; \ 186 *((type *)kp->arg) = l; \ 187 return 0; \ 188 } \ 189 int param_get_##name(char *buffer, struct kernel_param *kp) \ 190 { \ 191 return sprintf(buffer, format, *((type *)kp->arg)); \ 192 } 193 194STANDARD_PARAM_DEF(byte, unsigned char, "%c", unsigned long, simple_strtoul); 195STANDARD_PARAM_DEF(short, short, "%hi", long, simple_strtol); 196STANDARD_PARAM_DEF(ushort, unsigned short, "%hu", unsigned long, simple_strtoul); 197STANDARD_PARAM_DEF(int, int, "%i", long, simple_strtol); 198STANDARD_PARAM_DEF(uint, unsigned int, "%u", unsigned long, simple_strtoul); 199STANDARD_PARAM_DEF(long, long, "%li", long, simple_strtol); 200STANDARD_PARAM_DEF(ulong, unsigned long, "%lu", unsigned long, simple_strtoul); 201 202int param_set_charp(const char *val, struct kernel_param *kp) 203{ 204 if (!val) { 205 printk(KERN_ERR "%s: string parameter expected\n", 206 kp->name); 207 return -EINVAL; 208 } 209 210 if (strlen(val) > 1024) { 211 printk(KERN_ERR "%s: string parameter too long\n", 212 kp->name); 213 return -ENOSPC; 214 } 215 216 *(char **)kp->arg = (char *)val; 217 return 0; 218} 219 220int param_get_charp(char *buffer, struct kernel_param *kp) 221{ 222 return sprintf(buffer, "%s", *((char **)kp->arg)); 223} 224 225int param_set_bool(const char *val, struct kernel_param *kp) 226{ 227 /* No equals means "set"... */ 228 if (!val) val = "1"; 229 230 /* One of =[yYnN01] */ 231 switch (val[0]) { 232 case 'y': case 'Y': case '1': 233 *(int *)kp->arg = 1; 234 return 0; 235 case 'n': case 'N': case '0': 236 *(int *)kp->arg = 0; 237 return 0; 238 } 239 return -EINVAL; 240} 241 242int param_get_bool(char *buffer, struct kernel_param *kp) 243{ 244 /* Y and N chosen as being relatively non-coder friendly */ 245 return sprintf(buffer, "%c", (*(int *)kp->arg) ? 'Y' : 'N'); 246} 247 248int param_set_invbool(const char *val, struct kernel_param *kp) 249{ 250 int boolval, ret; 251 struct kernel_param dummy = { .arg = &boolval }; 252 253 ret = param_set_bool(val, &dummy); 254 if (ret == 0) 255 *(int *)kp->arg = !boolval; 256 return ret; 257} 258 259int param_get_invbool(char *buffer, struct kernel_param *kp) 260{ 261 int val; 262 struct kernel_param dummy = { .arg = &val }; 263 264 val = !*(int *)kp->arg; 265 return param_get_bool(buffer, &dummy); 266} 267 268/* We break the rule and mangle the string. */ 269static int param_array(const char *name, 270 const char *val, 271 unsigned int min, unsigned int max, 272 void *elem, int elemsize, 273 int (*set)(const char *, struct kernel_param *kp), 274 int *num) 275{ 276 int ret; 277 struct kernel_param kp; 278 char save; 279 280 /* Get the name right for errors. */ 281 kp.name = name; 282 kp.arg = elem; 283 284 /* No equals sign? */ 285 if (!val) { 286 printk(KERN_ERR "%s: expects arguments\n", name); 287 return -EINVAL; 288 } 289 290 *num = 0; 291 /* We expect a comma-separated list of values. */ 292 do { 293 int len; 294 295 if (*num == max) { 296 printk(KERN_ERR "%s: can only take %i arguments\n", 297 name, max); 298 return -EINVAL; 299 } 300 len = strcspn(val, ","); 301 302 /* nul-terminate and parse */ 303 save = val[len]; 304 ((char *)val)[len] = '\0'; 305 ret = set(val, &kp); 306 307 if (ret != 0) 308 return ret; 309 kp.arg += elemsize; 310 val += len+1; 311 (*num)++; 312 } while (save == ','); 313 314 if (*num < min) { 315 printk(KERN_ERR "%s: needs at least %i arguments\n", 316 name, min); 317 return -EINVAL; 318 } 319 return 0; 320} 321 322int param_array_set(const char *val, struct kernel_param *kp) 323{ 324 struct kparam_array *arr = kp->arg; 325 unsigned int temp_num; 326 327 return param_array(kp->name, val, 1, arr->max, arr->elem, 328 arr->elemsize, arr->set, arr->num ?: &temp_num); 329} 330 331int param_array_get(char *buffer, struct kernel_param *kp) 332{ 333 int i, off, ret; 334 struct kparam_array *arr = kp->arg; 335 struct kernel_param p; 336 337 p = *kp; 338 for (i = off = 0; i < (arr->num ? *arr->num : arr->max); i++) { 339 if (i) 340 buffer[off++] = ','; 341 p.arg = arr->elem + arr->elemsize * i; 342 ret = arr->get(buffer + off, &p); 343 if (ret < 0) 344 return ret; 345 off += ret; 346 } 347 buffer[off] = '\0'; 348 return off; 349} 350 351int param_set_copystring(const char *val, struct kernel_param *kp) 352{ 353 struct kparam_string *kps = kp->arg; 354 355 if (!val) { 356 printk(KERN_ERR "%s: missing param set value\n", kp->name); 357 return -EINVAL; 358 } 359 if (strlen(val)+1 > kps->maxlen) { 360 printk(KERN_ERR "%s: string doesn't fit in %u chars.\n", 361 kp->name, kps->maxlen-1); 362 return -ENOSPC; 363 } 364 strcpy(kps->string, val); 365 return 0; 366} 367 368int param_get_string(char *buffer, struct kernel_param *kp) 369{ 370 struct kparam_string *kps = kp->arg; 371 return strlcpy(buffer, kps->string, kps->maxlen); 372} 373 374/* sysfs output in /sys/modules/XYZ/parameters/ */ 375 376extern struct kernel_param __start___param[], __stop___param[]; 377 378#define MAX_KBUILD_MODNAME KOBJ_NAME_LEN 379 380struct param_attribute 381{ 382 struct module_attribute mattr; 383 struct kernel_param *param; 384}; 385 386struct module_param_attrs 387{ 388 struct attribute_group grp; 389 struct param_attribute attrs[0]; 390}; 391 392#ifdef CONFIG_SYSFS 393#define to_param_attr(n) container_of(n, struct param_attribute, mattr); 394 395static ssize_t param_attr_show(struct module_attribute *mattr, 396 struct module *mod, char *buf) 397{ 398 int count; 399 struct param_attribute *attribute = to_param_attr(mattr); 400 401 if (!attribute->param->get) 402 return -EPERM; 403 404 count = attribute->param->get(buf, attribute->param); 405 if (count > 0) { 406 strcat(buf, "\n"); 407 ++count; 408 } 409 return count; 410} 411 412/* sysfs always hands a nul-terminated string in buf. We rely on that. */ 413static ssize_t param_attr_store(struct module_attribute *mattr, 414 struct module *owner, 415 const char *buf, size_t len) 416{ 417 int err; 418 struct param_attribute *attribute = to_param_attr(mattr); 419 420 if (!attribute->param->set) 421 return -EPERM; 422 423 err = attribute->param->set(buf, attribute->param); 424 if (!err) 425 return len; 426 return err; 427} 428#endif 429 430#ifdef CONFIG_MODULES 431#define __modinit 432#else 433#define __modinit __init 434#endif 435 436#ifdef CONFIG_SYSFS 437/* 438 * param_sysfs_setup - setup sysfs support for one module or KBUILD_MODNAME 439 * @mk: struct module_kobject (contains parent kobject) 440 * @kparam: array of struct kernel_param, the actual parameter definitions 441 * @num_params: number of entries in array 442 * @name_skip: offset where the parameter name start in kparam[].name. Needed for built-in "modules" 443 * 444 * Create a kobject for a (per-module) group of parameters, and create files 445 * in sysfs. A pointer to the param_kobject is returned on success, 446 * NULL if there's no parameter to export, or other ERR_PTR(err). 447 */ 448static __modinit struct module_param_attrs * 449param_sysfs_setup(struct module_kobject *mk, 450 struct kernel_param *kparam, 451 unsigned int num_params, 452 unsigned int name_skip) 453{ 454 struct module_param_attrs *mp; 455 unsigned int valid_attrs = 0; 456 unsigned int i, size[2]; 457 struct param_attribute *pattr; 458 struct attribute **gattr; 459 int err; 460 461 for (i=0; i<num_params; i++) { 462 if (kparam[i].perm) 463 valid_attrs++; 464 } 465 466 if (!valid_attrs) 467 return NULL; 468 469 size[0] = ALIGN(sizeof(*mp) + 470 valid_attrs * sizeof(mp->attrs[0]), 471 sizeof(mp->grp.attrs[0])); 472 size[1] = (valid_attrs + 1) * sizeof(mp->grp.attrs[0]); 473 474 mp = kmalloc(size[0] + size[1], GFP_KERNEL); 475 if (!mp) 476 return ERR_PTR(-ENOMEM); 477 478 mp->grp.name = "parameters"; 479 mp->grp.attrs = (void *)mp + size[0]; 480 481 pattr = &mp->attrs[0]; 482 gattr = &mp->grp.attrs[0]; 483 for (i = 0; i < num_params; i++) { 484 struct kernel_param *kp = &kparam[i]; 485 if (kp->perm) { 486 pattr->param = kp; 487 pattr->mattr.show = param_attr_show; 488 pattr->mattr.store = param_attr_store; 489 pattr->mattr.attr.name = (char *)&kp->name[name_skip]; 490 pattr->mattr.attr.owner = mk->mod; 491 pattr->mattr.attr.mode = kp->perm; 492 *(gattr++) = &(pattr++)->mattr.attr; 493 } 494 } 495 *gattr = NULL; 496 497 if ((err = sysfs_create_group(&mk->kobj, &mp->grp))) { 498 kfree(mp); 499 return ERR_PTR(err); 500 } 501 return mp; 502} 503 504#ifdef CONFIG_MODULES 505/* 506 * module_param_sysfs_setup - setup sysfs support for one module 507 * @mod: module 508 * @kparam: module parameters (array) 509 * @num_params: number of module parameters 510 * 511 * Adds sysfs entries for module parameters, and creates a link from 512 * /sys/module/[mod->name]/parameters to /sys/parameters/[mod->name]/ 513 */ 514int module_param_sysfs_setup(struct module *mod, 515 struct kernel_param *kparam, 516 unsigned int num_params) 517{ 518 struct module_param_attrs *mp; 519 520 mp = param_sysfs_setup(&mod->mkobj, kparam, num_params, 0); 521 if (IS_ERR(mp)) 522 return PTR_ERR(mp); 523 524 mod->param_attrs = mp; 525 return 0; 526} 527 528/* 529 * module_param_sysfs_remove - remove sysfs support for one module 530 * @mod: module 531 * 532 * Remove sysfs entries for module parameters and the corresponding 533 * kobject. 534 */ 535void module_param_sysfs_remove(struct module *mod) 536{ 537 if (mod->param_attrs) { 538 sysfs_remove_group(&mod->mkobj.kobj, 539 &mod->param_attrs->grp); 540 /* We are positive that no one is using any param 541 * attrs at this point. Deallocate immediately. */ 542 kfree(mod->param_attrs); 543 mod->param_attrs = NULL; 544 } 545} 546#endif 547 548/* 549 * kernel_param_sysfs_setup - wrapper for built-in params support 550 */ 551static void __init kernel_param_sysfs_setup(const char *name, 552 struct kernel_param *kparam, 553 unsigned int num_params, 554 unsigned int name_skip) 555{ 556 struct module_kobject *mk; 557 int ret; 558 559 mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL); 560 BUG_ON(!mk); 561 562 mk->mod = THIS_MODULE; 563 kobj_set_kset_s(mk, module_subsys); 564 kobject_set_name(&mk->kobj, name); 565 kobject_init(&mk->kobj); 566 ret = kobject_add(&mk->kobj); 567 BUG_ON(ret < 0); 568 param_sysfs_setup(mk, kparam, num_params, name_skip); 569 kobject_uevent(&mk->kobj, KOBJ_ADD); 570} 571 572/* 573 * param_sysfs_builtin - add contents in /sys/parameters for built-in modules 574 * 575 * Add module_parameters to sysfs for "modules" built into the kernel. 576 * 577 * The "module" name (KBUILD_MODNAME) is stored before a dot, the 578 * "parameter" name is stored behind a dot in kernel_param->name. So, 579 * extract the "module" name for all built-in kernel_param-eters, 580 * and for all who have the same, call kernel_param_sysfs_setup. 581 */ 582static void __init param_sysfs_builtin(void) 583{ 584 struct kernel_param *kp, *kp_begin = NULL; 585 unsigned int i, name_len, count = 0; 586 char modname[MAX_KBUILD_MODNAME + 1] = ""; 587 588 for (i=0; i < __stop___param - __start___param; i++) { 589 char *dot; 590 591 kp = &__start___param[i]; 592 593 /* We do not handle args without periods. */ 594 dot = memchr(kp->name, '.', MAX_KBUILD_MODNAME); 595 if (!dot) { 596 DEBUGP("couldn't find period in %s\n", kp->name); 597 continue; 598 } 599 name_len = dot - kp->name; 600 601 /* new kbuild_modname? */ 602 if (strlen(modname) != name_len 603 || strncmp(modname, kp->name, name_len) != 0) { 604 /* add a new kobject for previous kernel_params. */ 605 if (count) 606 kernel_param_sysfs_setup(modname, 607 kp_begin, 608 count, 609 strlen(modname)+1); 610 611 strncpy(modname, kp->name, name_len); 612 modname[name_len] = '\0'; 613 count = 0; 614 kp_begin = kp; 615 } 616 count++; 617 } 618 619 /* last kernel_params need to be registered as well */ 620 if (count) 621 kernel_param_sysfs_setup(modname, kp_begin, count, 622 strlen(modname)+1); 623} 624 625 626/* module-related sysfs stuff */ 627 628#define to_module_attr(n) container_of(n, struct module_attribute, attr); 629#define to_module_kobject(n) container_of(n, struct module_kobject, kobj); 630 631static ssize_t module_attr_show(struct kobject *kobj, 632 struct attribute *attr, 633 char *buf) 634{ 635 struct module_attribute *attribute; 636 struct module_kobject *mk; 637 int ret; 638 639 attribute = to_module_attr(attr); 640 mk = to_module_kobject(kobj); 641 642 if (!attribute->show) 643 return -EIO; 644 645 ret = attribute->show(attribute, mk->mod, buf); 646 647 return ret; 648} 649 650static ssize_t module_attr_store(struct kobject *kobj, 651 struct attribute *attr, 652 const char *buf, size_t len) 653{ 654 struct module_attribute *attribute; 655 struct module_kobject *mk; 656 int ret; 657 658 attribute = to_module_attr(attr); 659 mk = to_module_kobject(kobj); 660 661 if (!attribute->store) 662 return -EIO; 663 664 ret = attribute->store(attribute, mk->mod, buf, len); 665 666 return ret; 667} 668 669static struct sysfs_ops module_sysfs_ops = { 670 .show = module_attr_show, 671 .store = module_attr_store, 672}; 673 674static struct kobj_type module_ktype; 675 676static int uevent_filter(struct kset *kset, struct kobject *kobj) 677{ 678 struct kobj_type *ktype = get_ktype(kobj); 679 680 if (ktype == &module_ktype) 681 return 1; 682 return 0; 683} 684 685static struct kset_uevent_ops module_uevent_ops = { 686 .filter = uevent_filter, 687}; 688 689decl_subsys(module, &module_ktype, &module_uevent_ops); 690int module_sysfs_initialized; 691 692static struct kobj_type module_ktype = { 693 .sysfs_ops = &module_sysfs_ops, 694}; 695 696/* 697 * param_sysfs_init - wrapper for built-in params support 698 */ 699static int __init param_sysfs_init(void) 700{ 701 int ret; 702 703 ret = subsystem_register(&module_subsys); 704 if (ret < 0) { 705 printk(KERN_WARNING "%s (%d): subsystem_register error: %d\n", 706 __FILE__, __LINE__, ret); 707 return ret; 708 } 709 module_sysfs_initialized = 1; 710 711 param_sysfs_builtin(); 712 713 return 0; 714} 715subsys_initcall(param_sysfs_init); 716 717#else 718#endif 719 720EXPORT_SYMBOL(param_set_byte); 721EXPORT_SYMBOL(param_get_byte); 722EXPORT_SYMBOL(param_set_short); 723EXPORT_SYMBOL(param_get_short); 724EXPORT_SYMBOL(param_set_ushort); 725EXPORT_SYMBOL(param_get_ushort); 726EXPORT_SYMBOL(param_set_int); 727EXPORT_SYMBOL(param_get_int); 728EXPORT_SYMBOL(param_set_uint); 729EXPORT_SYMBOL(param_get_uint); 730EXPORT_SYMBOL(param_set_long); 731EXPORT_SYMBOL(param_get_long); 732EXPORT_SYMBOL(param_set_ulong); 733EXPORT_SYMBOL(param_get_ulong); 734EXPORT_SYMBOL(param_set_charp); 735EXPORT_SYMBOL(param_get_charp); 736EXPORT_SYMBOL(param_set_bool); 737EXPORT_SYMBOL(param_get_bool); 738EXPORT_SYMBOL(param_set_invbool); 739EXPORT_SYMBOL(param_get_invbool); 740EXPORT_SYMBOL(param_array_set); 741EXPORT_SYMBOL(param_array_get); 742EXPORT_SYMBOL(param_set_copystring); 743EXPORT_SYMBOL(param_get_string); 744