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

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