bootinfo_proc.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Based on arch/arm/kernel/atags_proc.c
  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/slab.h>
  10. #include <linux/string.h>
  11. #include <asm/bootinfo.h>
  12. #include <asm/byteorder.h>
  13. static char bootinfo_tmp[1536] __initdata;
  14. static void *bootinfo_copy;
  15. static size_t bootinfo_size;
  16. static ssize_t bootinfo_read(struct file *file, char __user *buf,
  17. size_t count, loff_t *ppos)
  18. {
  19. return simple_read_from_buffer(buf, count, ppos, bootinfo_copy,
  20. bootinfo_size);
  21. }
  22. static const struct file_operations bootinfo_fops = {
  23. .read = bootinfo_read,
  24. .llseek = default_llseek,
  25. };
  26. void __init save_bootinfo(const struct bi_record *bi)
  27. {
  28. const void *start = bi;
  29. size_t size = sizeof(bi->tag);
  30. while (be16_to_cpu(bi->tag) != BI_LAST) {
  31. uint16_t n = be16_to_cpu(bi->size);
  32. size += n;
  33. bi = (struct bi_record *)((unsigned long)bi + n);
  34. }
  35. if (size > sizeof(bootinfo_tmp)) {
  36. pr_err("Cannot save %zu bytes of bootinfo\n", size);
  37. return;
  38. }
  39. pr_info("Saving %zu bytes of bootinfo\n", size);
  40. memcpy(bootinfo_tmp, start, size);
  41. bootinfo_size = size;
  42. }
  43. static int __init init_bootinfo_procfs(void)
  44. {
  45. /*
  46. * This cannot go into save_bootinfo() because kmalloc and proc don't
  47. * work yet when it is called.
  48. */
  49. struct proc_dir_entry *pde;
  50. if (!bootinfo_size)
  51. return -EINVAL;
  52. bootinfo_copy = kmemdup(bootinfo_tmp, bootinfo_size, GFP_KERNEL);
  53. if (!bootinfo_copy)
  54. return -ENOMEM;
  55. pde = proc_create_data("bootinfo", 0400, NULL, &bootinfo_fops, NULL);
  56. if (!pde) {
  57. kfree(bootinfo_copy);
  58. return -ENOMEM;
  59. }
  60. return 0;
  61. }
  62. arch_initcall(init_bootinfo_procfs);