smbios_build.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SMBIOS Support
  3. *
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. * Copyright (C) 2013 Red Hat, Inc.
  6. * Copyright (c) 2015,2016 Corey Minyard, MontaVista Software, LLC
  7. *
  8. * Authors:
  9. * Alex Williamson <alex.williamson@hp.com>
  10. * Markus Armbruster <armbru@redhat.com>
  11. *
  12. * This work is licensed under the terms of the GNU GPL, version 2. See
  13. * the COPYING file in the top-level directory.
  14. *
  15. * Contributions after 2012-01-13 are licensed under the terms of the
  16. * GNU GPL, version 2 or (at your option) any later version.
  17. */
  18. #ifndef QEMU_SMBIOS_BUILD_H
  19. #define QEMU_SMBIOS_BUILD_H
  20. bool smbios_skip_table(uint8_t type, bool required_table);
  21. extern uint8_t *smbios_tables;
  22. extern size_t smbios_tables_len;
  23. extern unsigned smbios_table_max;
  24. extern unsigned smbios_table_cnt;
  25. #define SMBIOS_BUILD_TABLE_PRE(tbl_type, tbl_handle, tbl_required) \
  26. SMBIOS_BUILD_TABLE_PRE_SIZE(tbl_type, tbl_handle, tbl_required, \
  27. sizeof(struct smbios_type_##tbl_type))\
  28. #define SMBIOS_BUILD_TABLE_PRE_SIZE(tbl_type, tbl_handle, \
  29. tbl_required, tbl_len) \
  30. struct smbios_type_##tbl_type *t; \
  31. size_t t_off; /* table offset into smbios_tables */ \
  32. int str_index = 0; \
  33. do { \
  34. /* should we skip building this table ? */ \
  35. if (smbios_skip_table(tbl_type, tbl_required)) { \
  36. return; \
  37. } \
  38. \
  39. /* use offset of table t within smbios_tables */ \
  40. /* (pointer must be updated after each realloc) */ \
  41. t_off = smbios_tables_len; \
  42. smbios_tables_len += tbl_len; \
  43. smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
  44. t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
  45. \
  46. t->header.type = tbl_type; \
  47. t->header.length = tbl_len; \
  48. t->header.handle = cpu_to_le16(tbl_handle); \
  49. } while (0)
  50. #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
  51. do { \
  52. int len = (value != NULL) ? strlen(value) + 1 : 0; \
  53. if (len > 1) { \
  54. smbios_tables = g_realloc(smbios_tables, \
  55. smbios_tables_len + len); \
  56. memcpy(smbios_tables + smbios_tables_len, value, len); \
  57. smbios_tables_len += len; \
  58. /* update pointer post-realloc */ \
  59. t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
  60. t->field = ++str_index; \
  61. } else { \
  62. t->field = 0; \
  63. } \
  64. } while (0)
  65. #define SMBIOS_TABLE_SET_STR_LIST(tbl_type, value) \
  66. do { \
  67. int len = (value != NULL) ? strlen(value) + 1 : 0; \
  68. if (len > 1) { \
  69. smbios_tables = g_realloc(smbios_tables, \
  70. smbios_tables_len + len); \
  71. memcpy(smbios_tables + smbios_tables_len, value, len); \
  72. smbios_tables_len += len; \
  73. ++str_index; \
  74. } \
  75. } while (0)
  76. #define SMBIOS_BUILD_TABLE_POST \
  77. do { \
  78. size_t term_cnt, t_size; \
  79. \
  80. /* add '\0' terminator (add two if no strings defined) */ \
  81. term_cnt = (str_index == 0) ? 2 : 1; \
  82. smbios_tables = g_realloc(smbios_tables, \
  83. smbios_tables_len + term_cnt); \
  84. memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
  85. smbios_tables_len += term_cnt; \
  86. \
  87. /* update smbios max. element size */ \
  88. t_size = smbios_tables_len - t_off; \
  89. if (t_size > smbios_table_max) { \
  90. smbios_table_max = t_size; \
  91. } \
  92. \
  93. /* update smbios element count */ \
  94. smbios_table_cnt++; \
  95. } while (0)
  96. /* IPMI SMBIOS firmware handling */
  97. void smbios_build_type_38_table(void);
  98. #endif /* QEMU_SMBIOS_BUILD_H */