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 7.5KB

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