Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

smbios.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <realmode.h>
  23. #include <pnpbios.h>
  24. #include <smbios.h>
  25. /** @file
  26. *
  27. * System Management BIOS
  28. *
  29. */
  30. /** Signature for an SMBIOS structure */
  31. #define SMBIOS_SIGNATURE \
  32. ( ( '_' << 0 ) + ( 'S' << 8 ) + ( 'M' << 16 ) + ( '_' << 24 ) )
  33. /** SMBIOS entry point */
  34. struct smbios_entry {
  35. /** Signature
  36. *
  37. * Must be equal to SMBIOS_SIGNATURE
  38. */
  39. uint32_t signature;
  40. /** Checksum */
  41. uint8_t checksum;
  42. /** Length */
  43. uint8_t length;
  44. /** Major version */
  45. uint8_t major;
  46. /** Minor version */
  47. uint8_t minor;
  48. /** Maximum structure size */
  49. uint16_t max;
  50. /** Entry point revision */
  51. uint8_t revision;
  52. /** Formatted area */
  53. uint8_t formatted[5];
  54. /** DMI Signature */
  55. uint8_t dmi_signature[5];
  56. /** DMI checksum */
  57. uint8_t dmi_checksum;
  58. /** Structure table length */
  59. uint16_t smbios_length;
  60. /** Structure table address */
  61. physaddr_t smbios_address;
  62. /** Number of SMBIOS structures */
  63. uint16_t smbios_count;
  64. /** BCD revision */
  65. uint8_t bcd_revision;
  66. } __attribute__ (( packed ));
  67. /** An SMBIOS structure */
  68. struct smbios {
  69. /** Type */
  70. uint8_t type;
  71. /** Length */
  72. uint8_t length;
  73. /** Handle */
  74. uint16_t handle;
  75. } __attribute__ (( packed ));
  76. struct smbios_system_information {
  77. struct smbios header;
  78. uint8_t manufacturer;
  79. uint8_t product;
  80. uint8_t version;
  81. uint8_t serial;
  82. } __attribute__ (( packed ));
  83. /**
  84. * Find SMBIOS
  85. *
  86. * @v emtry SMBIOS entry point to fill in
  87. * @ret rc Return status code
  88. */
  89. static int find_smbios_entry ( struct smbios_entry *entry ) {
  90. union {
  91. struct smbios_entry entry;
  92. uint8_t bytes[256]; /* 256 is maximum length possible */
  93. } u;
  94. unsigned int offset;
  95. size_t len;
  96. unsigned int i;
  97. uint8_t sum = 0;
  98. /* Try to find SMBIOS */
  99. for ( offset = 0 ; offset < 0x10000 ; offset += 0x10 ) {
  100. /* Read start of header and verify signature */
  101. copy_from_real ( &u.entry, BIOS_SEG, offset,
  102. sizeof ( u.entry ));
  103. if ( u.entry.signature != SMBIOS_SIGNATURE )
  104. continue;
  105. /* Read whole header and verify checksum */
  106. len = u.entry.length;
  107. copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
  108. for ( i = 0 ; i < len ; i++ ) {
  109. sum += u.bytes[i];
  110. }
  111. if ( sum != 0 ) {
  112. DBG ( "SMBIOS at %04x:%04x has bad checksum %02x\n",
  113. BIOS_SEG, offset, sum );
  114. continue;
  115. }
  116. /* Fill result structure */
  117. DBG ( "Found SMBIOS entry point at %04x:%04x\n",
  118. BIOS_SEG, offset );
  119. memcpy ( entry, &u.entry, sizeof ( *entry ) );
  120. return 0;
  121. }
  122. DBG ( "No SMBIOS found\n" );
  123. return -ENOENT;
  124. }
  125. /**
  126. * Find specific structure type within SMBIOS
  127. *
  128. * @v entry SMBIOS entry point
  129. * @v type Structure type
  130. * @v data SMBIOS structure buffer to fill in
  131. * @ret rc Return status code
  132. *
  133. * The buffer must be at least @c entry->max bytes in size.
  134. */
  135. static int find_smbios ( struct smbios_entry *entry, unsigned int type,
  136. void *data ) {
  137. struct smbios *smbios = data;
  138. userptr_t smbios_address = phys_to_user ( entry->smbios_address );
  139. unsigned int count = 0;
  140. size_t offset = 0;
  141. size_t frag_len;
  142. void *end;
  143. while ( ( offset < entry->smbios_length ) &&
  144. ( count < entry->smbios_count ) ) {
  145. /* Read next SMBIOS structure */
  146. frag_len = ( entry->smbios_length - offset );
  147. if ( frag_len > entry->max )
  148. frag_len = entry->max;
  149. copy_from_user ( data, smbios_address, offset, frag_len );
  150. /* Sanity protection; ensure the last two bytes of the
  151. * buffer are 0x00,0x00, just so that a terminator
  152. * exists somewhere. Also ensure that this lies
  153. * outside the formatted area.
  154. */
  155. *( ( uint16_t * ) ( data + entry->max - 2 ) ) = 0;
  156. if ( smbios->length > ( entry->max - 2 ) ) {
  157. DBG ( "Invalid SMBIOS structure length %zd\n",
  158. smbios->length );
  159. return -ENOENT;
  160. }
  161. DBG ( "Found SMBIOS structure type %d at offset %zx\n",
  162. smbios->type, offset );
  163. /* If this is the structure we want, return */
  164. if ( smbios->type == type )
  165. return 0;
  166. /* Find end of record. This will always exist, thanks
  167. * to our sanity check above.
  168. */
  169. for ( end = ( data + smbios->length ) ;
  170. end < ( data + entry->max ) ; end++ ) {
  171. if ( *( ( uint16_t * ) end ) == 0 ) {
  172. end += 2;
  173. break;
  174. }
  175. }
  176. offset += ( end - data );
  177. count++;
  178. }
  179. DBG ( "SMBIOS structure type %d not found\n", type );
  180. return -ENOENT;
  181. }
  182. /**
  183. * Find indexed string within SMBIOS structure
  184. *
  185. * @v data SMBIOS structure
  186. * @v index String index
  187. * @ret string String, or NULL
  188. */
  189. static const char * find_smbios_string ( void *data, unsigned int index ) {
  190. struct smbios *smbios = data;
  191. const char *string;
  192. size_t len;
  193. if ( ! index )
  194. return NULL;
  195. string = ( data + smbios->length );
  196. while ( --index ) {
  197. /* Move to next string */
  198. len = strlen ( string );
  199. if ( len == 0 ) {
  200. /* Reached premature end of string table */
  201. DBG ( "SMBIOS string index %d not found\n", index );
  202. return NULL;
  203. }
  204. string += ( len + 1 );
  205. }
  206. return string;
  207. }
  208. /**
  209. * Find SMBIOS serial number
  210. *
  211. * @v data Buffer to fill
  212. * @v len Length of buffer
  213. */
  214. int find_smbios_serial ( void *data, size_t len ) {
  215. struct smbios_entry entry;
  216. const char *string;
  217. int rc;
  218. if ( ( rc = find_smbios_entry ( &entry ) ) != 0 )
  219. return rc;
  220. char buffer[entry.max];
  221. if ( ( rc = find_smbios ( &entry, 1, buffer ) ) != 0 )
  222. return rc;
  223. struct smbios_system_information *sysinfo = ( void * ) buffer;
  224. string = find_smbios_string ( buffer, sysinfo->serial );
  225. if ( ! string )
  226. return -ENOENT;
  227. DBG ( "Found serial number \"%s\"\n", string );
  228. snprintf ( data, len, "%s", string );
  229. return 0;
  230. }