You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smbios_settings.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <ipxe/settings.h>
  23. #include <ipxe/init.h>
  24. #include <ipxe/uuid.h>
  25. #include <ipxe/smbios.h>
  26. /** SMBIOS settings tag magic number */
  27. #define SMBIOS_TAG_MAGIC 0x5B /* "SmBios" */
  28. /**
  29. * Construct SMBIOS empty tag
  30. *
  31. * @ret tag SMBIOS setting tag
  32. */
  33. #define SMBIOS_EMPTY_TAG ( SMBIOS_TAG_MAGIC << 24 )
  34. /**
  35. * Construct SMBIOS raw-data tag
  36. *
  37. * @v _type SMBIOS structure type number
  38. * @v _structure SMBIOS structure data type
  39. * @v _field Field within SMBIOS structure data type
  40. * @ret tag SMBIOS setting tag
  41. */
  42. #define SMBIOS_RAW_TAG( _type, _structure, _field ) \
  43. ( ( SMBIOS_TAG_MAGIC << 24 ) | \
  44. ( (_type) << 16 ) | \
  45. ( offsetof ( _structure, _field ) << 8 ) | \
  46. ( sizeof ( ( ( _structure * ) 0 )->_field ) ) )
  47. /**
  48. * Construct SMBIOS string tag
  49. *
  50. * @v _type SMBIOS structure type number
  51. * @v _structure SMBIOS structure data type
  52. * @v _field Field within SMBIOS structure data type
  53. * @ret tag SMBIOS setting tag
  54. */
  55. #define SMBIOS_STRING_TAG( _type, _structure, _field ) \
  56. ( ( SMBIOS_TAG_MAGIC << 24 ) | \
  57. ( (_type) << 16 ) | \
  58. ( offsetof ( _structure, _field ) << 8 ) )
  59. /**
  60. * Check applicability of SMBIOS setting
  61. *
  62. * @v settings Settings block
  63. * @v setting Setting
  64. * @ret applies Setting applies within this settings block
  65. */
  66. static int smbios_applies ( struct settings *settings __unused,
  67. struct setting *setting ) {
  68. unsigned int tag_magic;
  69. /* Check tag magic */
  70. tag_magic = ( setting->tag >> 24 );
  71. return ( tag_magic == SMBIOS_TAG_MAGIC );
  72. }
  73. /**
  74. * Fetch value of SMBIOS setting
  75. *
  76. * @v settings Settings block, or NULL to search all blocks
  77. * @v setting Setting to fetch
  78. * @v data Buffer to fill with setting data
  79. * @v len Length of buffer
  80. * @ret len Length of setting data, or negative error
  81. */
  82. static int smbios_fetch ( struct settings *settings __unused,
  83. struct setting *setting,
  84. void *data, size_t len ) {
  85. struct smbios_structure structure;
  86. unsigned int tag_magic;
  87. unsigned int tag_type;
  88. unsigned int tag_offset;
  89. unsigned int tag_len;
  90. int rc;
  91. /* Split tag into type, offset and length */
  92. tag_magic = ( setting->tag >> 24 );
  93. tag_type = ( ( setting->tag >> 16 ) & 0xff );
  94. tag_offset = ( ( setting->tag >> 8 ) & 0xff );
  95. tag_len = ( setting->tag & 0xff );
  96. assert ( tag_magic == SMBIOS_TAG_MAGIC );
  97. /* Find SMBIOS structure */
  98. if ( ( rc = find_smbios_structure ( tag_type, &structure ) ) != 0 )
  99. return rc;
  100. {
  101. uint8_t buf[structure.header.len];
  102. /* Read SMBIOS structure */
  103. if ( ( rc = read_smbios_structure ( &structure, buf,
  104. sizeof ( buf ) ) ) != 0 )
  105. return rc;
  106. if ( tag_len == 0 ) {
  107. /* String */
  108. return read_smbios_string ( &structure,
  109. buf[tag_offset],
  110. data, len );
  111. } else {
  112. /* Raw data */
  113. if ( len > tag_len )
  114. len = tag_len;
  115. memcpy ( data, &buf[tag_offset], len );
  116. return tag_len;
  117. }
  118. }
  119. }
  120. /** SMBIOS settings operations */
  121. static struct settings_operations smbios_settings_operations = {
  122. .applies = smbios_applies,
  123. .fetch = smbios_fetch,
  124. };
  125. /** SMBIOS settings */
  126. static struct settings smbios_settings = {
  127. .refcnt = NULL,
  128. .tag_magic = SMBIOS_EMPTY_TAG,
  129. .siblings = LIST_HEAD_INIT ( smbios_settings.siblings ),
  130. .children = LIST_HEAD_INIT ( smbios_settings.children ),
  131. .op = &smbios_settings_operations,
  132. };
  133. /** Initialise SMBIOS settings */
  134. static void smbios_init ( void ) {
  135. int rc;
  136. if ( ( rc = register_settings ( &smbios_settings, NULL,
  137. "smbios" ) ) != 0 ) {
  138. DBG ( "SMBIOS could not register settings: %s\n",
  139. strerror ( rc ) );
  140. return;
  141. }
  142. }
  143. /** SMBIOS settings initialiser */
  144. struct init_fn smbios_init_fn __init_fn ( INIT_NORMAL ) = {
  145. .initialise = smbios_init,
  146. };
  147. /** UUID setting obtained via SMBIOS */
  148. struct setting uuid_setting __setting ( SETTING_HOST ) = {
  149. .name = "uuid",
  150. .description = "UUID",
  151. .tag = SMBIOS_RAW_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  152. struct smbios_system_information, uuid ),
  153. .type = &setting_type_uuid,
  154. };
  155. /** Other SMBIOS named settings */
  156. struct setting smbios_named_settings[] __setting ( SETTING_HOST_EXTRA ) = {
  157. {
  158. .name = "manufacturer",
  159. .description = "Manufacturer",
  160. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  161. struct smbios_system_information,
  162. manufacturer ),
  163. .type = &setting_type_string,
  164. },
  165. {
  166. .name = "product",
  167. .description = "Product name",
  168. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  169. struct smbios_system_information,
  170. product ),
  171. .type = &setting_type_string,
  172. },
  173. {
  174. .name = "serial",
  175. .description = "Serial number",
  176. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  177. struct smbios_system_information,
  178. serial ),
  179. .type = &setting_type_string,
  180. },
  181. {
  182. .name = "asset",
  183. .description = "Asset tag",
  184. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_ENCLOSURE_INFORMATION,
  185. struct smbios_enclosure_information,
  186. asset_tag ),
  187. .type = &setting_type_string,
  188. },
  189. };