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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include <stdint.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <gpxe/settings.h>
  22. #include <gpxe/init.h>
  23. #include <gpxe/uuid.h>
  24. #include <gpxe/smbios.h>
  25. /** SMBIOS settings tag magic number */
  26. #define SMBIOS_TAG_MAGIC 0x5B /* "SmBios" */
  27. /**
  28. * Construct SMBIOS empty tag
  29. *
  30. * @ret tag SMBIOS setting tag
  31. */
  32. #define SMBIOS_EMPTY_TAG ( SMBIOS_TAG_MAGIC << 24 )
  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. ( ( SMBIOS_TAG_MAGIC << 24 ) | \
  43. ( (_type) << 16 ) | \
  44. ( offsetof ( _structure, _field ) << 8 ) | \
  45. ( sizeof ( ( ( _structure * ) 0 )->_field ) ) )
  46. /**
  47. * Construct SMBIOS string tag
  48. *
  49. * @v _type SMBIOS structure type number
  50. * @v _structure SMBIOS structure data type
  51. * @v _field Field within SMBIOS structure data type
  52. * @ret tag SMBIOS setting tag
  53. */
  54. #define SMBIOS_STRING_TAG( _type, _structure, _field ) \
  55. ( ( SMBIOS_TAG_MAGIC << 24 ) | \
  56. ( (_type) << 16 ) | \
  57. ( offsetof ( _structure, _field ) << 8 ) )
  58. /**
  59. * Store value of SMBIOS setting
  60. *
  61. * @v settings Settings block
  62. * @v setting Setting to store
  63. * @v data Setting data, or NULL to clear setting
  64. * @v len Length of setting data
  65. * @ret rc Return status code
  66. */
  67. static int smbios_store ( struct settings *settings __unused,
  68. struct setting *setting __unused,
  69. const void *data __unused, size_t len __unused ) {
  70. /* Cannot write data into SMBIOS */
  71. return -ENOTSUP;
  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. if ( tag_magic != SMBIOS_TAG_MAGIC )
  97. return -ENOENT;
  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. return read_smbios_string ( &structure,
  110. buf[tag_offset],
  111. data, len );
  112. } else {
  113. /* Raw data */
  114. if ( len > tag_len )
  115. len = tag_len;
  116. memcpy ( data, &buf[tag_offset], len );
  117. return tag_len;
  118. }
  119. }
  120. }
  121. /** SMBIOS settings operations */
  122. static struct settings_operations smbios_settings_operations = {
  123. .store = smbios_store,
  124. .fetch = smbios_fetch,
  125. };
  126. /** SMBIOS settings */
  127. static struct settings smbios_settings = {
  128. .refcnt = NULL,
  129. .name = "smbios",
  130. .tag_magic = SMBIOS_EMPTY_TAG,
  131. .siblings = LIST_HEAD_INIT ( smbios_settings.siblings ),
  132. .children = LIST_HEAD_INIT ( smbios_settings.children ),
  133. .op = &smbios_settings_operations,
  134. };
  135. /** Initialise SMBIOS settings */
  136. static void smbios_init ( void ) {
  137. int rc;
  138. if ( ( rc = register_settings ( &smbios_settings, NULL ) ) != 0 ) {
  139. DBG ( "SMBIOS could not register settings: %s\n",
  140. strerror ( rc ) );
  141. return;
  142. }
  143. }
  144. /** SMBIOS settings initialiser */
  145. struct init_fn smbios_init_fn __init_fn ( INIT_NORMAL ) = {
  146. .initialise = smbios_init,
  147. };
  148. /** UUID setting obtained via SMBIOS */
  149. struct setting uuid_setting __setting = {
  150. .name = "uuid",
  151. .description = "UUID",
  152. .tag = SMBIOS_RAW_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  153. struct smbios_system_information, uuid ),
  154. .type = &setting_type_uuid,
  155. };
  156. /** Other SMBIOS named settings */
  157. struct setting smbios_named_settings[] __setting = {
  158. {
  159. .name = "manufacturer",
  160. .description = "Manufacturer",
  161. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  162. struct smbios_system_information,
  163. manufacturer ),
  164. .type = &setting_type_string,
  165. },
  166. {
  167. .name = "product",
  168. .description = "Product name",
  169. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  170. struct smbios_system_information,
  171. product ),
  172. .type = &setting_type_string,
  173. },
  174. {
  175. .name = "serial",
  176. .description = "Serial number",
  177. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  178. struct smbios_system_information,
  179. serial ),
  180. .type = &setting_type_string,
  181. },
  182. {
  183. .name = "asset",
  184. .description = "Asset tag",
  185. .tag = SMBIOS_STRING_TAG ( SMBIOS_TYPE_ENCLOSURE_INFORMATION,
  186. struct smbios_enclosure_information,
  187. asset_tag ),
  188. .type = &setting_type_string,
  189. },
  190. };