Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

smbios_settings.c 5.2KB

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