bootconfig.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * /proc/bootconfig - Extra boot configuration
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/init.h>
  7. #include <linux/printk.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/bootconfig.h>
  11. #include <linux/slab.h>
  12. static char *saved_boot_config;
  13. static int boot_config_proc_show(struct seq_file *m, void *v)
  14. {
  15. if (saved_boot_config)
  16. seq_puts(m, saved_boot_config);
  17. return 0;
  18. }
  19. /* Rest size of buffer */
  20. #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
  21. /* Return the needed total length if @size is 0 */
  22. static int __init copy_xbc_key_value_list(char *dst, size_t size)
  23. {
  24. struct xbc_node *leaf, *vnode;
  25. char *key, *end = dst + size;
  26. const char *val;
  27. char q;
  28. int ret = 0;
  29. key = kzalloc(XBC_KEYLEN_MAX, GFP_KERNEL);
  30. if (!key)
  31. return -ENOMEM;
  32. xbc_for_each_key_value(leaf, val) {
  33. ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
  34. if (ret < 0)
  35. break;
  36. ret = snprintf(dst, rest(dst, end), "%s = ", key);
  37. if (ret < 0)
  38. break;
  39. dst += ret;
  40. vnode = xbc_node_get_child(leaf);
  41. if (vnode) {
  42. xbc_array_for_each_value(vnode, val) {
  43. if (strchr(val, '"'))
  44. q = '\'';
  45. else
  46. q = '"';
  47. ret = snprintf(dst, rest(dst, end), "%c%s%c%s",
  48. q, val, q, xbc_node_is_array(vnode) ? ", " : "\n");
  49. if (ret < 0)
  50. goto out;
  51. dst += ret;
  52. }
  53. } else {
  54. ret = snprintf(dst, rest(dst, end), "\"\"\n");
  55. if (ret < 0)
  56. break;
  57. dst += ret;
  58. }
  59. }
  60. out:
  61. kfree(key);
  62. return ret < 0 ? ret : dst - (end - size);
  63. }
  64. static int __init proc_boot_config_init(void)
  65. {
  66. int len;
  67. len = copy_xbc_key_value_list(NULL, 0);
  68. if (len < 0)
  69. return len;
  70. if (len > 0) {
  71. saved_boot_config = kzalloc(len + 1, GFP_KERNEL);
  72. if (!saved_boot_config)
  73. return -ENOMEM;
  74. len = copy_xbc_key_value_list(saved_boot_config, len + 1);
  75. if (len < 0) {
  76. kfree(saved_boot_config);
  77. return len;
  78. }
  79. }
  80. proc_create_single("bootconfig", 0, NULL, boot_config_proc_show);
  81. return 0;
  82. }
  83. fs_initcall(proc_boot_config_init);