coreboot_table.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * coreboot_table.c
  3. *
  4. * Module providing coreboot table access.
  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/err.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include "coreboot_table.h"
  23. struct coreboot_table_entry {
  24. u32 tag;
  25. u32 size;
  26. };
  27. static struct coreboot_table_header __iomem *ptr_header;
  28. /*
  29. * This function parses the coreboot table for an entry that contains the base
  30. * address of the given entry tag. The coreboot table consists of a header
  31. * directly followed by a number of small, variable-sized entries, which each
  32. * contain an identifying tag and their length as the first two fields.
  33. */
  34. int coreboot_table_find(int tag, void *data, size_t data_size)
  35. {
  36. struct coreboot_table_header header;
  37. struct coreboot_table_entry entry;
  38. void *ptr_entry;
  39. int i;
  40. if (!ptr_header)
  41. return -EPROBE_DEFER;
  42. memcpy_fromio(&header, ptr_header, sizeof(header));
  43. if (strncmp(header.signature, "LBIO", sizeof(header.signature))) {
  44. pr_warn("coreboot_table: coreboot table missing or corrupt!\n");
  45. return -ENODEV;
  46. }
  47. ptr_entry = (void *)ptr_header + header.header_bytes;
  48. for (i = 0; i < header.table_entries; i++) {
  49. memcpy_fromio(&entry, ptr_entry, sizeof(entry));
  50. if (entry.tag == tag) {
  51. if (data_size < entry.size)
  52. return -EINVAL;
  53. memcpy_fromio(data, ptr_entry, entry.size);
  54. return 0;
  55. }
  56. ptr_entry += entry.size;
  57. }
  58. return -ENOENT;
  59. }
  60. EXPORT_SYMBOL(coreboot_table_find);
  61. int coreboot_table_init(void __iomem *ptr)
  62. {
  63. ptr_header = ptr;
  64. return 0;
  65. }
  66. EXPORT_SYMBOL(coreboot_table_init);
  67. int coreboot_table_exit(void)
  68. {
  69. if (ptr_header)
  70. iounmap(ptr_header);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(coreboot_table_exit);
  74. MODULE_AUTHOR("Google, Inc.");
  75. MODULE_LICENSE("GPL");