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.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2007 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 <stdio.h>
  21. #include <errno.h>
  22. #include <gpxe/uaccess.h>
  23. #include <gpxe/uuid.h>
  24. #include <realmode.h>
  25. #include <pnpbios.h>
  26. #include <smbios.h>
  27. /** @file
  28. *
  29. * System Management BIOS
  30. *
  31. */
  32. /** Signature for SMBIOS entry point */
  33. #define SMBIOS_SIGNATURE \
  34. ( ( '_' << 0 ) + ( 'S' << 8 ) + ( 'M' << 16 ) + ( '_' << 24 ) )
  35. /**
  36. * SMBIOS entry point
  37. *
  38. * This is the single table which describes the list of SMBIOS
  39. * structures. It is located by scanning through the BIOS segment.
  40. */
  41. struct smbios_entry {
  42. /** Signature
  43. *
  44. * Must be equal to SMBIOS_SIGNATURE
  45. */
  46. uint32_t signature;
  47. /** Checksum */
  48. uint8_t checksum;
  49. /** Length */
  50. uint8_t length;
  51. /** Major version */
  52. uint8_t major;
  53. /** Minor version */
  54. uint8_t minor;
  55. /** Maximum structure size */
  56. uint16_t max;
  57. /** Entry point revision */
  58. uint8_t revision;
  59. /** Formatted area */
  60. uint8_t formatted[5];
  61. /** DMI Signature */
  62. uint8_t dmi_signature[5];
  63. /** DMI checksum */
  64. uint8_t dmi_checksum;
  65. /** Structure table length */
  66. uint16_t smbios_length;
  67. /** Structure table address */
  68. physaddr_t smbios_address;
  69. /** Number of SMBIOS structures */
  70. uint16_t smbios_count;
  71. /** BCD revision */
  72. uint8_t bcd_revision;
  73. } __attribute__ (( packed ));
  74. /**
  75. * SMBIOS entry point descriptor
  76. *
  77. * This contains the information from the SMBIOS entry point that we
  78. * care about.
  79. */
  80. struct smbios {
  81. /** Start of SMBIOS structures */
  82. userptr_t address;
  83. /** Length of SMBIOS structures */
  84. size_t length;
  85. /** Number of SMBIOS structures */
  86. unsigned int count;
  87. };
  88. /**
  89. * SMBIOS strings descriptor
  90. *
  91. * This is returned as part of the search for an SMBIOS structure, and
  92. * contains the information needed for extracting the strings within
  93. * the "unformatted" portion of the structure.
  94. */
  95. struct smbios_strings {
  96. /** Start of strings data */
  97. userptr_t data;
  98. /** Length of strings data */
  99. size_t length;
  100. };
  101. /**
  102. * Find SMBIOS
  103. *
  104. * @ret smbios SMBIOS entry point descriptor, or NULL if not found
  105. */
  106. static struct smbios * find_smbios ( void ) {
  107. static struct smbios smbios = {
  108. .address = UNULL,
  109. };
  110. union {
  111. struct smbios_entry entry;
  112. uint8_t bytes[256]; /* 256 is maximum length possible */
  113. } u;
  114. unsigned int offset;
  115. size_t len;
  116. unsigned int i;
  117. uint8_t sum;
  118. /* Return cached result if available */
  119. if ( smbios.address != UNULL )
  120. return &smbios;
  121. /* Try to find SMBIOS */
  122. for ( offset = 0 ; offset < 0x10000 ; offset += 0x10 ) {
  123. /* Read start of header and verify signature */
  124. copy_from_real ( &u.entry, BIOS_SEG, offset,
  125. sizeof ( u.entry ));
  126. if ( u.entry.signature != SMBIOS_SIGNATURE )
  127. continue;
  128. /* Read whole header and verify checksum */
  129. len = u.entry.length;
  130. copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
  131. for ( i = 0 , sum = 0 ; i < len ; i++ ) {
  132. sum += u.bytes[i];
  133. }
  134. if ( sum != 0 ) {
  135. DBG ( "SMBIOS at %04x:%04x has bad checksum %02x\n",
  136. BIOS_SEG, offset, sum );
  137. continue;
  138. }
  139. /* Fill result structure */
  140. DBG ( "Found SMBIOS entry point at %04x:%04x\n",
  141. BIOS_SEG, offset );
  142. smbios.address = phys_to_user ( u.entry.smbios_address );
  143. smbios.length = u.entry.smbios_length;
  144. smbios.count = u.entry.smbios_count;
  145. return &smbios;
  146. }
  147. DBG ( "No SMBIOS found\n" );
  148. return NULL;
  149. }
  150. /**
  151. * Find SMBIOS strings terminator
  152. *
  153. * @v smbios SMBIOS entry point descriptor
  154. * @v offset Offset to start of strings
  155. * @ret offset Offset to strings terminator, or 0 if not found
  156. */
  157. static size_t find_strings_terminator ( struct smbios *smbios,
  158. size_t offset ) {
  159. size_t max_offset = ( smbios->length - 2 );
  160. uint16_t nulnul;
  161. for ( ; offset <= max_offset ; offset++ ) {
  162. copy_from_user ( &nulnul, smbios->address, offset, 2 );
  163. if ( nulnul == 0 )
  164. return ( offset + 1 );
  165. }
  166. return 0;
  167. }
  168. /**
  169. * Find specific structure type within SMBIOS
  170. *
  171. * @v type Structure type to search for
  172. * @v structure Buffer to fill in with structure
  173. * @v length Length of buffer
  174. * @v strings Strings descriptor to fill in, or NULL
  175. * @ret rc Return status code
  176. */
  177. int find_smbios_structure ( unsigned int type, void *structure,
  178. size_t length, struct smbios_strings *strings ) {
  179. struct smbios *smbios;
  180. struct smbios_header header;
  181. struct smbios_strings temp_strings;
  182. unsigned int count = 0;
  183. size_t offset = 0;
  184. size_t strings_offset;
  185. size_t terminator_offset;
  186. /* Locate SMBIOS entry point */
  187. if ( ! ( smbios = find_smbios() ) )
  188. return -ENOENT;
  189. /* Ensure that we have a usable strings descriptor buffer */
  190. if ( ! strings )
  191. strings = &temp_strings;
  192. /* Scan through list of structures */
  193. while ( ( ( offset + sizeof ( header ) ) < smbios->length ) &&
  194. ( count < smbios->count ) ) {
  195. /* Read next SMBIOS structure header */
  196. copy_from_user ( &header, smbios->address, offset,
  197. sizeof ( header ) );
  198. /* Determine start and extent of strings block */
  199. strings_offset = ( offset + header.length );
  200. if ( strings_offset > smbios->length ) {
  201. DBG ( "SMBIOS structure at offset %zx with length "
  202. "%x extends beyond SMBIOS\n", offset,
  203. header.length );
  204. return -ENOENT;
  205. }
  206. terminator_offset =
  207. find_strings_terminator ( smbios, strings_offset );
  208. if ( ! terminator_offset ) {
  209. DBG ( "SMBIOS structure at offset %zx has "
  210. "unterminated strings section\n", offset );
  211. return -ENOENT;
  212. }
  213. strings->data = userptr_add ( smbios->address,
  214. strings_offset );
  215. strings->length = ( terminator_offset - strings_offset );
  216. DBG ( "SMBIOS structure at offset %zx has type %d, "
  217. "length %x, strings length %zx\n",
  218. offset, header.type, header.length, strings->length );
  219. /* If this is the structure we want, return */
  220. if ( header.type == type ) {
  221. if ( length > header.length )
  222. length = header.length;
  223. copy_from_user ( structure, smbios->address,
  224. offset, length );
  225. return 0;
  226. }
  227. /* Move to next SMBIOS structure */
  228. offset = ( terminator_offset + 1 );
  229. count++;
  230. }
  231. DBG ( "SMBIOS structure type %d not found\n", type );
  232. return -ENOENT;
  233. }
  234. /**
  235. * Find indexed string within SMBIOS structure
  236. *
  237. * @v strings SMBIOS strings descriptor
  238. * @v index String index
  239. * @v buffer Buffer for string
  240. * @v length Length of string buffer
  241. * @ret rc Return status code
  242. */
  243. int find_smbios_string ( struct smbios_strings *strings, unsigned int index,
  244. char *buffer, size_t length ) {
  245. size_t offset = 0;
  246. size_t string_len;
  247. /* Zero buffer. This ensures that a valid NUL terminator is
  248. * always present (unless length==0).
  249. */
  250. memset ( buffer, 0, length );
  251. /* String numbers start at 1 (0 is used to indicate "no string") */
  252. if ( ! index )
  253. return 0;
  254. while ( offset < strings->length ) {
  255. /* Get string length. This is known safe, since the
  256. * smbios_strings struct is constructed so as to
  257. * always end on a string boundary.
  258. */
  259. string_len = strlen_user ( strings->data, offset );
  260. if ( --index == 0 ) {
  261. /* Copy string, truncating as necessary. */
  262. if ( string_len >= length )
  263. string_len = ( length - 1 );
  264. copy_from_user ( buffer, strings->data,
  265. offset, string_len );
  266. return 0;
  267. }
  268. offset += ( string_len + 1 );
  269. }
  270. DBG ( "SMBIOS string index %d not found\n", index );
  271. return -ENOENT;
  272. }
  273. /**
  274. * Get UUID from SMBIOS
  275. *
  276. * @v uuid UUID to fill in
  277. * @ret rc Return status code
  278. */
  279. int smbios_get_uuid ( union uuid *uuid ) {
  280. struct smbios_system_information sysinfo;
  281. int rc;
  282. if ( ( rc = find_smbios_structure ( SMBIOS_TYPE_SYSTEM_INFORMATION,
  283. &sysinfo, sizeof ( sysinfo ),
  284. NULL ) ) != 0 )
  285. return rc;
  286. memcpy ( uuid, sysinfo.uuid, sizeof ( *uuid ) );
  287. DBG ( "SMBIOS found UUID %s\n", uuid_ntoa ( uuid ) );
  288. return 0;
  289. }