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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <gpxe/uaccess.h>
  24. #include <gpxe/smbios.h>
  25. /** @file
  26. *
  27. * System Management BIOS
  28. *
  29. */
  30. /** SMBIOS entry point descriptor */
  31. static struct smbios smbios = {
  32. .address = UNULL,
  33. };
  34. /**
  35. * Find SMBIOS strings terminator
  36. *
  37. * @v offset Offset to start of strings
  38. * @ret offset Offset to strings terminator, or 0 if not found
  39. */
  40. static size_t find_strings_terminator ( size_t offset ) {
  41. size_t max_offset = ( smbios.len - 2 );
  42. uint16_t nulnul;
  43. for ( ; offset <= max_offset ; offset++ ) {
  44. copy_from_user ( &nulnul, smbios.address, offset, 2 );
  45. if ( nulnul == 0 )
  46. return ( offset + 1 );
  47. }
  48. return 0;
  49. }
  50. /**
  51. * Find specific structure type within SMBIOS
  52. *
  53. * @v type Structure type to search for
  54. * @v structure SMBIOS structure descriptor to fill in
  55. * @ret rc Return status code
  56. */
  57. int find_smbios_structure ( unsigned int type,
  58. struct smbios_structure *structure ) {
  59. unsigned int count = 0;
  60. size_t offset = 0;
  61. size_t strings_offset;
  62. size_t terminator_offset;
  63. int rc;
  64. /* Find SMBIOS */
  65. if ( ( smbios.address == UNULL ) &&
  66. ( ( rc = find_smbios ( &smbios ) ) != 0 ) )
  67. return rc;
  68. assert ( smbios.address != UNULL );
  69. /* Scan through list of structures */
  70. while ( ( ( offset + sizeof ( structure->header ) ) < smbios.len )
  71. && ( count < smbios.count ) ) {
  72. /* Read next SMBIOS structure header */
  73. copy_from_user ( &structure->header, smbios.address, offset,
  74. sizeof ( structure->header ) );
  75. /* Determine start and extent of strings block */
  76. strings_offset = ( offset + structure->header.len );
  77. if ( strings_offset > smbios.len ) {
  78. DBG ( "SMBIOS structure at offset %zx with length "
  79. "%x extends beyond SMBIOS\n", offset,
  80. structure->header.len );
  81. return -ENOENT;
  82. }
  83. terminator_offset = find_strings_terminator ( strings_offset );
  84. if ( ! terminator_offset ) {
  85. DBG ( "SMBIOS structure at offset %zx has "
  86. "unterminated strings section\n", offset );
  87. return -ENOENT;
  88. }
  89. structure->strings_len = ( terminator_offset - strings_offset);
  90. DBG ( "SMBIOS structure at offset %zx has type %d, length %x, "
  91. "strings length %zx\n", offset, structure->header.type,
  92. structure->header.len, structure->strings_len );
  93. /* If this is the structure we want, return */
  94. if ( structure->header.type == type ) {
  95. structure->offset = offset;
  96. return 0;
  97. }
  98. /* Move to next SMBIOS structure */
  99. offset = ( terminator_offset + 1 );
  100. count++;
  101. }
  102. DBG ( "SMBIOS structure type %d not found\n", type );
  103. return -ENOENT;
  104. }
  105. /**
  106. * Copy SMBIOS structure
  107. *
  108. * @v structure SMBIOS structure descriptor
  109. * @v data Buffer to hold SMBIOS structure
  110. * @v len Length of buffer
  111. * @ret rc Return status code
  112. */
  113. int read_smbios_structure ( struct smbios_structure *structure,
  114. void *data, size_t len ) {
  115. assert ( smbios.address != UNULL );
  116. if ( len > structure->header.len )
  117. len = structure->header.len;
  118. copy_from_user ( data, smbios.address, structure->offset, len );
  119. return 0;
  120. }
  121. /**
  122. * Find indexed string within SMBIOS structure
  123. *
  124. * @v structure SMBIOS structure descriptor
  125. * @v index String index
  126. * @v data Buffer for string
  127. * @v len Length of string buffer
  128. * @ret rc Length of string, or negative error
  129. */
  130. int read_smbios_string ( struct smbios_structure *structure,
  131. unsigned int index, void *data, size_t len ) {
  132. size_t strings_start = ( structure->offset + structure->header.len );
  133. size_t strings_end = ( strings_start + structure->strings_len );
  134. size_t offset;
  135. size_t string_len;
  136. assert ( smbios.address != UNULL );
  137. /* String numbers start at 1 (0 is used to indicate "no string") */
  138. if ( ! index )
  139. return -ENOENT;
  140. for ( offset = strings_start ; offset < strings_end ;
  141. offset += ( string_len + 1 ) ) {
  142. /* Get string length. This is known safe, since the
  143. * smbios_strings struct is constructed so as to
  144. * always end on a string boundary.
  145. */
  146. string_len = strlen_user ( smbios.address, offset );
  147. if ( --index == 0 ) {
  148. /* Copy string, truncating as necessary. */
  149. if ( len > string_len )
  150. len = string_len;
  151. copy_from_user ( data, smbios.address, offset, len );
  152. return string_len;
  153. }
  154. }
  155. DBG ( "SMBIOS string index %d not found\n", index );
  156. return -ENOENT;
  157. }