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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <ipxe/settings.h>
  24. #include <ipxe/init.h>
  25. #include <ipxe/uuid.h>
  26. #include <ipxe/smbios.h>
  27. /** SMBIOS settings tag magic number */
  28. #define SMBIOS_TAG_MAGIC 0x5B /* "SmBios" */
  29. /**
  30. * Construct SMBIOS empty tag
  31. *
  32. * @ret tag SMBIOS setting tag
  33. */
  34. #define SMBIOS_EMPTY_TAG ( SMBIOS_TAG_MAGIC << 24 )
  35. /**
  36. * Construct SMBIOS raw-data tag
  37. *
  38. * @v _type SMBIOS structure type number
  39. * @v _structure SMBIOS structure data type
  40. * @v _field Field within SMBIOS structure data type
  41. * @ret tag SMBIOS setting tag
  42. */
  43. #define SMBIOS_RAW_TAG( _type, _structure, _field ) \
  44. ( ( SMBIOS_TAG_MAGIC << 24 ) | \
  45. ( (_type) << 16 ) | \
  46. ( offsetof ( _structure, _field ) << 8 ) | \
  47. ( sizeof ( ( ( _structure * ) 0 )->_field ) ) )
  48. /**
  49. * Construct SMBIOS string tag
  50. *
  51. * @v _type SMBIOS structure type number
  52. * @v _structure SMBIOS structure data type
  53. * @v _field Field within SMBIOS structure data type
  54. * @ret tag SMBIOS setting tag
  55. */
  56. #define SMBIOS_STRING_TAG( _type, _structure, _field ) \
  57. ( ( SMBIOS_TAG_MAGIC << 24 ) | \
  58. ( (_type) << 16 ) | \
  59. ( offsetof ( _structure, _field ) << 8 ) )
  60. /**
  61. * Check applicability of SMBIOS setting
  62. *
  63. * @v settings Settings block
  64. * @v setting Setting
  65. * @ret applies Setting applies within this settings block
  66. */
  67. static int smbios_applies ( struct settings *settings __unused,
  68. struct setting *setting ) {
  69. unsigned int tag_magic;
  70. /* Check tag magic */
  71. tag_magic = ( setting->tag >> 24 );
  72. return ( tag_magic == SMBIOS_TAG_MAGIC );
  73. }
  74. /**
  75. * Fetch value of SMBIOS setting
  76. *
  77. * @v settings Settings block, or NULL to search all blocks
  78. * @v setting Setting to fetch
  79. * @v data Buffer to fill with setting data
  80. * @v len Length of buffer
  81. * @ret len Length of setting data, or negative error
  82. */
  83. static int smbios_fetch ( struct settings *settings __unused,
  84. struct setting *setting,
  85. void *data, size_t len ) {
  86. struct smbios_structure structure;
  87. unsigned int tag_magic;
  88. unsigned int tag_type;
  89. unsigned int tag_offset;
  90. unsigned int tag_len;
  91. int rc;
  92. /* Split tag into type, offset and length */
  93. tag_magic = ( setting->tag >> 24 );
  94. tag_type = ( ( setting->tag >> 16 ) & 0xff );
  95. tag_offset = ( ( setting->tag >> 8 ) & 0xff );
  96. tag_len = ( setting->tag & 0xff );
  97. assert ( tag_magic == SMBIOS_TAG_MAGIC );
  98. /* Find SMBIOS structure */
  99. if ( ( rc = find_smbios_structure ( tag_type, &structure ) ) != 0 )
  100. return rc;
  101. {
  102. uint8_t buf[structure.header.len];
  103. /* Read SMBIOS structure */
  104. if ( ( rc = read_smbios_structure ( &structure, buf,
  105. sizeof ( buf ) ) ) != 0 )
  106. return rc;
  107. if ( tag_len == 0 ) {
  108. /* String */
  109. if ( ( rc = read_smbios_string ( &structure,
  110. buf[tag_offset],
  111. data, len ) ) < 0 ) {
  112. return rc;
  113. }
  114. if ( ! setting->type )
  115. setting->type = &setting_type_string;
  116. return rc;
  117. } else {
  118. /* Raw data */
  119. if ( len > tag_len )
  120. len = tag_len;
  121. memcpy ( data, &buf[tag_offset], len );
  122. if ( ! setting->type )
  123. setting->type = &setting_type_hex;
  124. return tag_len;
  125. }
  126. }
  127. }
  128. /** SMBIOS settings operations */
  129. static struct settings_operations smbios_settings_operations = {
  130. .applies = smbios_applies,
  131. .fetch = smbios_fetch,
  132. };
  133. /** SMBIOS settings */
  134. static struct settings smbios_settings = {
  135. .refcnt = NULL,
  136. .tag_magic = SMBIOS_EMPTY_TAG,
  137. .siblings = LIST_HEAD_INIT ( smbios_settings.siblings ),
  138. .children = LIST_HEAD_INIT ( smbios_settings.children ),
  139. .op = &smbios_settings_operations,
  140. };
  141. /** Initialise SMBIOS settings */
  142. static void smbios_init ( void ) {
  143. int rc;
  144. if ( ( rc = register_settings ( &smbios_settings, NULL,
  145. "smbios" ) ) != 0 ) {
  146. DBG ( "SMBIOS could not register settings: %s\n",
  147. strerror ( rc ) );
  148. return;
  149. }
  150. }
  151. /** SMBIOS settings initialiser */
  152. struct init_fn smbios_init_fn __init_fn ( INIT_NORMAL ) = {
  153. .initialise = smbios_init,
  154. };
  155. /** UUID setting obtained via SMBIOS */
  156. struct setting uuid_setting __setting ( SETTING_HOST ) = {
  157. .name = "uuid",
  158. .description = "UUID",
  159. .tag = SMBIOS_RAW_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  160. struct smbios_system_information, uuid ),
  161. .type = &setting_type_uuid,
  162. };
  163. /** Other SMBIOS named settings */
  164. struct setting smbios_named_settings[] __setting ( SETTING_HOST_EXTRA ) = {
  165. {
  166. .name = "manufacturer",
  167. .description = "Manufacturer",
  168. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  169. struct smbios_system_information,
  170. manufacturer ),
  171. .type = &setting_type_string,
  172. },
  173. {
  174. .name = "product",
  175. .description = "Product name",
  176. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  177. struct smbios_system_information,
  178. product ),
  179. .type = &setting_type_string,
  180. },
  181. {
  182. .name = "serial",
  183. .description = "Serial number",
  184. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  185. struct smbios_system_information,
  186. serial ),
  187. .type = &setting_type_string,
  188. },
  189. {
  190. .name = "asset",
  191. .description = "Asset tag",
  192. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_ENCLOSURE_INFORMATION,
  193. struct smbios_enclosure_information,
  194. asset_tag ),
  195. .type = &setting_type_string,
  196. },
  197. };