Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

smbios_settings.c 6.8KB

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