memconsole-coreboot.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/device.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.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_probe(struct coreboot_device *dev)
  67. {
  68. struct cbmem_cons __iomem *tmp_cbmc;
  69. tmp_cbmc = memremap(dev->cbmem_ref.cbmem_addr,
  70. sizeof(*tmp_cbmc), MEMREMAP_WB);
  71. if (!tmp_cbmc)
  72. return -ENOMEM;
  73. /* Read size only once to prevent overrun attack through /dev/mem. */
  74. cbmem_console_size = tmp_cbmc->size_dont_access_after_boot;
  75. cbmem_console = memremap(dev->cbmem_ref.cbmem_addr,
  76. cbmem_console_size + sizeof(*cbmem_console),
  77. MEMREMAP_WB);
  78. memunmap(tmp_cbmc);
  79. if (!cbmem_console)
  80. return -ENOMEM;
  81. memconsole_setup(memconsole_coreboot_read);
  82. return memconsole_sysfs_init();
  83. }
  84. static int memconsole_remove(struct coreboot_device *dev)
  85. {
  86. memconsole_exit();
  87. if (cbmem_console)
  88. memunmap(cbmem_console);
  89. return 0;
  90. }
  91. static struct coreboot_driver memconsole_driver = {
  92. .probe = memconsole_probe,
  93. .remove = memconsole_remove,
  94. .drv = {
  95. .name = "memconsole",
  96. },
  97. .tag = CB_TAG_CBMEM_CONSOLE,
  98. };
  99. static void coreboot_memconsole_exit(void)
  100. {
  101. coreboot_driver_unregister(&memconsole_driver);
  102. }
  103. static int __init coreboot_memconsole_init(void)
  104. {
  105. return coreboot_driver_register(&memconsole_driver);
  106. }
  107. module_exit(coreboot_memconsole_exit);
  108. module_init(coreboot_memconsole_init);
  109. MODULE_AUTHOR("Google, Inc.");
  110. MODULE_LICENSE("GPL");