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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 scope */
  28. static const struct settings_scope smbios_settings_scope;
  29. /**
  30. * Construct SMBIOS raw-data tag
  31. *
  32. * @v _type SMBIOS structure type number
  33. * @v _structure SMBIOS structure data type
  34. * @v _field Field within SMBIOS structure data type
  35. * @ret tag SMBIOS setting tag
  36. */
  37. #define SMBIOS_RAW_TAG( _type, _structure, _field ) \
  38. ( ( (_type) << 16 ) | \
  39. ( offsetof ( _structure, _field ) << 8 ) | \
  40. ( sizeof ( ( ( _structure * ) 0 )->_field ) ) )
  41. /**
  42. * Construct SMBIOS string tag
  43. *
  44. * @v _type SMBIOS structure type number
  45. * @v _structure SMBIOS structure data type
  46. * @v _field Field within SMBIOS structure data type
  47. * @ret tag SMBIOS setting tag
  48. */
  49. #define SMBIOS_STRING_TAG( _type, _structure, _field ) \
  50. ( ( (_type) << 16 ) | \
  51. ( offsetof ( _structure, _field ) << 8 ) )
  52. /**
  53. * Check applicability of SMBIOS setting
  54. *
  55. * @v settings Settings block
  56. * @v setting Setting
  57. * @ret applies Setting applies within this settings block
  58. */
  59. static int smbios_applies ( struct settings *settings __unused,
  60. const struct setting *setting ) {
  61. return ( setting->scope == &smbios_settings_scope );
  62. }
  63. /**
  64. * Fetch value of SMBIOS setting
  65. *
  66. * @v settings Settings block, or NULL to search all blocks
  67. * @v setting Setting to fetch
  68. * @v data Buffer to fill with setting data
  69. * @v len Length of buffer
  70. * @ret len Length of setting data, or negative error
  71. */
  72. static int smbios_fetch ( struct settings *settings __unused,
  73. struct setting *setting,
  74. void *data, size_t len ) {
  75. struct smbios_structure structure;
  76. unsigned int tag_instance;
  77. unsigned int tag_type;
  78. unsigned int tag_offset;
  79. unsigned int tag_len;
  80. int rc;
  81. /* Split tag into instance, type, offset and length */
  82. tag_instance = ( ( setting->tag >> 24 ) & 0xff );
  83. tag_type = ( ( setting->tag >> 16 ) & 0xff );
  84. tag_offset = ( ( setting->tag >> 8 ) & 0xff );
  85. tag_len = ( setting->tag & 0xff );
  86. /* Find SMBIOS structure */
  87. if ( ( rc = find_smbios_structure ( tag_type, tag_instance,
  88. &structure ) ) != 0 )
  89. return rc;
  90. {
  91. uint8_t buf[structure.header.len];
  92. const void *raw;
  93. union uuid uuid;
  94. unsigned int index;
  95. /* Read SMBIOS structure */
  96. if ( ( rc = read_smbios_structure ( &structure, buf,
  97. sizeof ( buf ) ) ) != 0 )
  98. return rc;
  99. /* A <length> of zero indicates that the byte at
  100. * <offset> contains a string index. An <offset> of
  101. * zero indicates that the <length> contains a literal
  102. * string index.
  103. */
  104. if ( ( tag_len == 0 ) || ( tag_offset == 0 ) ) {
  105. index = ( ( tag_offset == 0 ) ?
  106. tag_len : buf[tag_offset] );
  107. if ( ( rc = read_smbios_string ( &structure, index,
  108. data, len ) ) < 0 ) {
  109. return rc;
  110. }
  111. if ( ! setting->type )
  112. setting->type = &setting_type_string;
  113. return rc;
  114. }
  115. /* Mangle UUIDs if necessary. iPXE treats UUIDs as
  116. * being in network byte order (big-endian). SMBIOS
  117. * specification version 2.6 states that UUIDs are
  118. * stored with little-endian values in the first three
  119. * fields; earlier versions did not specify an
  120. * endianness. dmidecode assumes that the byte order
  121. * is little-endian if and only if the SMBIOS version
  122. * is 2.6 or higher; we match this behaviour.
  123. */
  124. raw = &buf[tag_offset];
  125. if ( ( setting->type == &setting_type_uuid ) &&
  126. ( tag_len == sizeof ( uuid ) ) &&
  127. ( smbios_version() >= SMBIOS_VERSION ( 2, 6 ) ) ) {
  128. DBG ( "SMBIOS detected mangled UUID\n" );
  129. memcpy ( &uuid, &buf[tag_offset], sizeof ( uuid ) );
  130. uuid_mangle ( &uuid );
  131. raw = &uuid;
  132. }
  133. /* Return data */
  134. if ( len > tag_len )
  135. len = tag_len;
  136. memcpy ( data, raw, len );
  137. if ( ! setting->type )
  138. setting->type = &setting_type_hex;
  139. return tag_len;
  140. }
  141. }
  142. /** SMBIOS settings operations */
  143. static struct settings_operations smbios_settings_operations = {
  144. .applies = smbios_applies,
  145. .fetch = smbios_fetch,
  146. };
  147. /** SMBIOS settings */
  148. static struct settings smbios_settings = {
  149. .refcnt = NULL,
  150. .siblings = LIST_HEAD_INIT ( smbios_settings.siblings ),
  151. .children = LIST_HEAD_INIT ( smbios_settings.children ),
  152. .op = &smbios_settings_operations,
  153. .default_scope = &smbios_settings_scope,
  154. };
  155. /** Initialise SMBIOS settings */
  156. static void smbios_init ( void ) {
  157. int rc;
  158. if ( ( rc = register_settings ( &smbios_settings, NULL,
  159. "smbios" ) ) != 0 ) {
  160. DBG ( "SMBIOS could not register settings: %s\n",
  161. strerror ( rc ) );
  162. return;
  163. }
  164. }
  165. /** SMBIOS settings initialiser */
  166. struct init_fn smbios_init_fn __init_fn ( INIT_NORMAL ) = {
  167. .initialise = smbios_init,
  168. };
  169. /** UUID setting obtained via SMBIOS */
  170. const struct setting uuid_setting __setting ( SETTING_HOST, uuid ) = {
  171. .name = "uuid",
  172. .description = "UUID",
  173. .tag = SMBIOS_RAW_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  174. struct smbios_system_information, uuid ),
  175. .type = &setting_type_uuid,
  176. .scope = &smbios_settings_scope,
  177. };
  178. /** Manufacturer name setting */
  179. const struct setting manufacturer_setting __setting ( SETTING_HOST_EXTRA,
  180. manufacturer ) = {
  181. .name = "manufacturer",
  182. .description = "Manufacturer",
  183. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  184. struct smbios_system_information,
  185. manufacturer ),
  186. .type = &setting_type_string,
  187. .scope = &smbios_settings_scope,
  188. };
  189. /** Product name setting */
  190. const struct setting product_setting __setting ( SETTING_HOST_EXTRA, product )={
  191. .name = "product",
  192. .description = "Product name",
  193. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  194. struct smbios_system_information,
  195. product ),
  196. .type = &setting_type_string,
  197. .scope = &smbios_settings_scope,
  198. };
  199. /** Serial number setting */
  200. const struct setting serial_setting __setting ( SETTING_HOST_EXTRA, serial ) = {
  201. .name = "serial",
  202. .description = "Serial number",
  203. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  204. struct smbios_system_information,
  205. serial ),
  206. .type = &setting_type_string,
  207. .scope = &smbios_settings_scope,
  208. };
  209. /** Asset tag setting */
  210. const struct setting asset_setting __setting ( SETTING_HOST_EXTRA, asset ) = {
  211. .name = "asset",
  212. .description = "Asset tag",
  213. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_ENCLOSURE_INFORMATION,
  214. struct smbios_enclosure_information,
  215. asset_tag ),
  216. .type = &setting_type_string,
  217. .scope = &smbios_settings_scope,
  218. };