bootconfig.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifdef CONFIG_KSU_SUSFS_SPOOF_CMDLINE_OR_BOOTCONFIG
  14. extern int susfs_spoof_cmdline_or_bootconfig(struct seq_file *m);
  15. #endif
  16. static int boot_config_proc_show(struct seq_file *m, void *v)
  17. {
  18. #ifdef CONFIG_KSU_SUSFS_SPOOF_CMDLINE_OR_BOOTCONFIG
  19. if (saved_boot_config) {
  20. if (!susfs_spoof_cmdline_or_bootconfig(m)) {
  21. return 0;
  22. }
  23. }
  24. #endif
  25. if (saved_boot_config)
  26. seq_puts(m, saved_boot_config);
  27. return 0;
  28. }
  29. /* Rest size of buffer */
  30. #define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
  31. /* Return the needed total length if @size is 0 */
  32. static int __init copy_xbc_key_value_list(char *dst, size_t size)
  33. {
  34. struct xbc_node *leaf, *vnode;
  35. char *key, *end = dst + size;
  36. const char *val;
  37. char q;
  38. int ret = 0;
  39. key = kzalloc(XBC_KEYLEN_MAX, GFP_KERNEL);
  40. if (!key)
  41. return -ENOMEM;
  42. xbc_for_each_key_value(leaf, val) {
  43. ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
  44. if (ret < 0)
  45. break;
  46. ret = snprintf(dst, rest(dst, end), "%s = ", key);
  47. if (ret < 0)
  48. break;
  49. dst += ret;
  50. vnode = xbc_node_get_child(leaf);
  51. if (vnode) {
  52. xbc_array_for_each_value(vnode, val) {
  53. if (strchr(val, '"'))
  54. q = '\'';
  55. else
  56. q = '"';
  57. ret = snprintf(dst, rest(dst, end), "%c%s%c%s",
  58. q, val, q, xbc_node_is_array(vnode) ? ", " : "\n");
  59. if (ret < 0)
  60. goto out;
  61. dst += ret;
  62. }
  63. } else {
  64. ret = snprintf(dst, rest(dst, end), "\"\"\n");
  65. if (ret < 0)
  66. break;
  67. dst += ret;
  68. }
  69. }
  70. out:
  71. kfree(key);
  72. return ret < 0 ? ret : dst - (end - size);
  73. }
  74. static int __init proc_boot_config_init(void)
  75. {
  76. int len;
  77. len = copy_xbc_key_value_list(NULL, 0);
  78. if (len < 0)
  79. return len;
  80. if (len > 0) {
  81. saved_boot_config = kzalloc(len + 1, GFP_KERNEL);
  82. if (!saved_boot_config)
  83. return -ENOMEM;
  84. len = copy_xbc_key_value_list(saved_boot_config, len + 1);
  85. if (len < 0) {
  86. kfree(saved_boot_config);
  87. return len;
  88. }
  89. }
  90. proc_create_single("bootconfig", 0, NULL, boot_config_proc_show);
  91. return 0;
  92. }
  93. fs_initcall(proc_boot_config_init);