2
0

smbios_build.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. struct smbios_type_##tbl_type *t; \
  27. size_t t_off; /* table offset into smbios_tables */ \
  28. int str_index = 0; \
  29. do { \
  30. /* should we skip building this table ? */ \
  31. if (smbios_skip_table(tbl_type, tbl_required)) { \
  32. return; \
  33. } \
  34. \
  35. /* use offset of table t within smbios_tables */ \
  36. /* (pointer must be updated after each realloc) */ \
  37. t_off = smbios_tables_len; \
  38. smbios_tables_len += sizeof(*t); \
  39. smbios_tables = g_realloc(smbios_tables, smbios_tables_len); \
  40. t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
  41. \
  42. t->header.type = tbl_type; \
  43. t->header.length = sizeof(*t); \
  44. t->header.handle = cpu_to_le16(tbl_handle); \
  45. } while (0)
  46. #define SMBIOS_TABLE_SET_STR(tbl_type, field, value) \
  47. do { \
  48. int len = (value != NULL) ? strlen(value) + 1 : 0; \
  49. if (len > 1) { \
  50. smbios_tables = g_realloc(smbios_tables, \
  51. smbios_tables_len + len); \
  52. memcpy(smbios_tables + smbios_tables_len, value, len); \
  53. smbios_tables_len += len; \
  54. /* update pointer post-realloc */ \
  55. t = (struct smbios_type_##tbl_type *)(smbios_tables + t_off); \
  56. t->field = ++str_index; \
  57. } else { \
  58. t->field = 0; \
  59. } \
  60. } while (0)
  61. #define SMBIOS_TABLE_SET_STR_LIST(tbl_type, value) \
  62. do { \
  63. int len = (value != NULL) ? strlen(value) + 1 : 0; \
  64. if (len > 1) { \
  65. smbios_tables = g_realloc(smbios_tables, \
  66. smbios_tables_len + len); \
  67. memcpy(smbios_tables + smbios_tables_len, value, len); \
  68. smbios_tables_len += len; \
  69. ++str_index; \
  70. } \
  71. } while (0)
  72. #define SMBIOS_BUILD_TABLE_POST \
  73. do { \
  74. size_t term_cnt, t_size; \
  75. \
  76. /* add '\0' terminator (add two if no strings defined) */ \
  77. term_cnt = (str_index == 0) ? 2 : 1; \
  78. smbios_tables = g_realloc(smbios_tables, \
  79. smbios_tables_len + term_cnt); \
  80. memset(smbios_tables + smbios_tables_len, 0, term_cnt); \
  81. smbios_tables_len += term_cnt; \
  82. \
  83. /* update smbios max. element size */ \
  84. t_size = smbios_tables_len - t_off; \
  85. if (t_size > smbios_table_max) { \
  86. smbios_table_max = t_size; \
  87. } \
  88. \
  89. /* update smbios element count */ \
  90. smbios_table_cnt++; \
  91. } while (0)
  92. /* IPMI SMBIOS firmware handling */
  93. void smbios_build_type_38_table(void);
  94. #endif /* QEMU_SMBIOS_BUILD_H */