memconsole-coreboot.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * memconsole-coreboot.c
  3. *
  4. * Memory based BIOS console accessed through coreboot table.
  5. *
  6. * Copyright 2017 Google Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License v2.0 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include "memconsole.h"
  21. #include "coreboot_table.h"
  22. #define CB_TAG_CBMEM_CONSOLE 0x17
  23. /* CBMEM firmware console log descriptor. */
  24. struct cbmem_cons {
  25. u32 size_dont_access_after_boot;
  26. u32 cursor;
  27. u8 body[0];
  28. } __packed;
  29. #define CURSOR_MASK ((1 << 28) - 1)
  30. #define OVERFLOW (1 << 31)
  31. static struct cbmem_cons __iomem *cbmem_console;
  32. static u32 cbmem_console_size;
  33. /*
  34. * The cbmem_console structure is read again on every access because it may
  35. * change at any time if runtime firmware logs new messages. This may rarely
  36. * lead to race conditions where the firmware overwrites the beginning of the
  37. * ring buffer with more lines after we have already read |cursor|. It should be
  38. * rare and harmless enough that we don't spend extra effort working around it.
  39. */
  40. static ssize_t memconsole_coreboot_read(char *buf, loff_t pos, size_t count)
  41. {
  42. u32 cursor = cbmem_console->cursor & CURSOR_MASK;
  43. u32 flags = cbmem_console->cursor & ~CURSOR_MASK;
  44. u32 size = cbmem_console_size;
  45. struct seg { /* describes ring buffer segments in logical order */
  46. u32 phys; /* physical offset from start of mem buffer */
  47. u32 len; /* length of segment */
  48. } seg[2] = { {0}, {0} };
  49. size_t done = 0;
  50. int i;
  51. if (flags & OVERFLOW) {
  52. if (cursor > size) /* Shouldn't really happen, but... */
  53. cursor = 0;
  54. seg[0] = (struct seg){.phys = cursor, .len = size - cursor};
  55. seg[1] = (struct seg){.phys = 0, .len = cursor};
  56. } else {
  57. seg[0] = (struct seg){.phys = 0, .len = min(cursor, size)};
  58. }
  59. for (i = 0; i < ARRAY_SIZE(seg) && count > done; i++) {
  60. done += memory_read_from_buffer(buf + done, count - done, &pos,
  61. cbmem_console->body + seg[i].phys, seg[i].len);
  62. pos -= seg[i].len;
  63. }
  64. return done;
  65. }
  66. static int memconsole_coreboot_init(phys_addr_t physaddr)
  67. {
  68. struct cbmem_cons __iomem *tmp_cbmc;
  69. tmp_cbmc = memremap(physaddr, sizeof(*tmp_cbmc), MEMREMAP_WB);
  70. if (!tmp_cbmc)
  71. return -ENOMEM;
  72. /* Read size only once to prevent overrun attack through /dev/mem. */
  73. cbmem_console_size = tmp_cbmc->size_dont_access_after_boot;
  74. cbmem_console = memremap(physaddr,
  75. cbmem_console_size + sizeof(*cbmem_console),
  76. MEMREMAP_WB);
  77. memunmap(tmp_cbmc);
  78. if (!cbmem_console)
  79. return -ENOMEM;
  80. memconsole_setup(memconsole_coreboot_read);
  81. return 0;
  82. }
  83. static int memconsole_probe(struct platform_device *pdev)
  84. {
  85. int ret;
  86. struct lb_cbmem_ref entry;
  87. ret = coreboot_table_find(CB_TAG_CBMEM_CONSOLE, &entry, sizeof(entry));
  88. if (ret)
  89. return ret;
  90. ret = memconsole_coreboot_init(entry.cbmem_addr);
  91. if (ret)
  92. return ret;
  93. return memconsole_sysfs_init();
  94. }
  95. static int memconsole_remove(struct platform_device *pdev)
  96. {
  97. memconsole_exit();
  98. if (cbmem_console)
  99. memunmap(cbmem_console);
  100. return 0;
  101. }
  102. static struct platform_driver memconsole_driver = {
  103. .probe = memconsole_probe,
  104. .remove = memconsole_remove,
  105. .driver = {
  106. .name = "memconsole",
  107. },
  108. };
  109. static int __init platform_memconsole_init(void)
  110. {
  111. struct platform_device *pdev;
  112. pdev = platform_device_register_simple("memconsole", -1, NULL, 0);
  113. if (IS_ERR(pdev))
  114. return PTR_ERR(pdev);
  115. platform_driver_register(&memconsole_driver);
  116. return 0;
  117. }
  118. module_init(platform_memconsole_init);
  119. MODULE_AUTHOR("Google, Inc.");
  120. MODULE_LICENSE("GPL");