iPXE
smbios.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 #include <assert.h>
00030 #include <ipxe/uaccess.h>
00031 #include <ipxe/smbios.h>
00032 
00033 /** @file
00034  *
00035  * System Management BIOS
00036  *
00037  */
00038 
00039 /** SMBIOS entry point descriptor */
00040 static struct smbios smbios = {
00041         .address = UNULL,
00042 };
00043 
00044 /**
00045  * Scan for SMBIOS entry point structure
00046  *
00047  * @v start             Start address of region to scan
00048  * @v len               Length of region to scan
00049  * @v entry             SMBIOS entry point structure to fill in
00050  * @ret rc              Return status code
00051  */
00052 int find_smbios_entry ( userptr_t start, size_t len,
00053                         struct smbios_entry *entry ) {
00054         uint8_t buf[256]; /* 256 is maximum length possible */
00055         static size_t offset = 0; /* Avoid repeated attempts to locate SMBIOS */
00056         size_t entry_len;
00057         unsigned int i;
00058         uint8_t sum;
00059 
00060         /* Try to find SMBIOS */
00061         for ( ; offset < len ; offset += 0x10 ) {
00062 
00063                 /* Read start of header and verify signature */
00064                 copy_from_user ( entry, start, offset, sizeof ( *entry ) );
00065                 if ( entry->signature != SMBIOS_SIGNATURE )
00066                         continue;
00067 
00068                 /* Read whole header and verify checksum */
00069                 entry_len = entry->len;
00070                 assert ( entry_len <= sizeof ( buf ) );
00071                 copy_from_user ( buf, start, offset, entry_len );
00072                 for ( i = 0, sum = 0 ; i < entry_len ; i++ ) {
00073                         sum += buf[i];
00074                 }
00075                 if ( sum != 0 ) {
00076                         DBG ( "SMBIOS at %08lx has bad checksum %02x\n",
00077                               user_to_phys ( start, offset ), sum );
00078                         continue;
00079                 }
00080 
00081                 /* Fill result structure */
00082                 DBG ( "Found SMBIOS v%d.%d entry point at %08lx\n",
00083                       entry->major, entry->minor,
00084                       user_to_phys ( start, offset ) );
00085                 return 0;
00086         }
00087 
00088         DBG ( "No SMBIOS found\n" );
00089         return -ENODEV;
00090 }
00091 
00092 /**
00093  * Find SMBIOS strings terminator
00094  *
00095  * @v offset            Offset to start of strings
00096  * @ret offset          Offset to strings terminator, or 0 if not found
00097  */
00098 static size_t find_strings_terminator ( size_t offset ) {
00099         size_t max_offset = ( smbios.len - 2 );
00100         uint16_t nulnul;
00101 
00102         for ( ; offset <= max_offset ; offset++ ) {
00103                 copy_from_user ( &nulnul, smbios.address, offset, 2 );
00104                 if ( nulnul == 0 )
00105                         return ( offset + 1 );
00106         }
00107         return 0;
00108 }
00109 
00110 /**
00111  * Find specific structure type within SMBIOS
00112  *
00113  * @v type              Structure type to search for
00114  * @v instance          Instance of this type of structure
00115  * @v structure         SMBIOS structure descriptor to fill in
00116  * @ret rc              Return status code
00117  */
00118 int find_smbios_structure ( unsigned int type, unsigned int instance,
00119                             struct smbios_structure *structure ) {
00120         unsigned int count = 0;
00121         size_t offset = 0;
00122         size_t strings_offset;
00123         size_t terminator_offset;
00124         int rc;
00125 
00126         /* Find SMBIOS */
00127         if ( ( smbios.address == UNULL ) &&
00128              ( ( rc = find_smbios ( &smbios ) ) != 0 ) )
00129                 return rc;
00130         assert ( smbios.address != UNULL );
00131 
00132         /* Scan through list of structures */
00133         while ( ( ( offset + sizeof ( structure->header ) ) < smbios.len )
00134                 && ( count < smbios.count ) ) {
00135 
00136                 /* Read next SMBIOS structure header */
00137                 copy_from_user ( &structure->header, smbios.address, offset,
00138                                  sizeof ( structure->header ) );
00139 
00140                 /* Determine start and extent of strings block */
00141                 strings_offset = ( offset + structure->header.len );
00142                 if ( strings_offset > smbios.len ) {
00143                         DBG ( "SMBIOS structure at offset %zx with length "
00144                               "%x extends beyond SMBIOS\n", offset,
00145                               structure->header.len );
00146                         return -ENOENT;
00147                 }
00148                 terminator_offset = find_strings_terminator ( strings_offset );
00149                 if ( ! terminator_offset ) {
00150                         DBG ( "SMBIOS structure at offset %zx has "
00151                               "unterminated strings section\n", offset );
00152                         return -ENOENT;
00153                 }
00154                 structure->strings_len = ( terminator_offset - strings_offset);
00155 
00156                 DBG ( "SMBIOS structure at offset %zx has type %d, length %x, "
00157                       "strings length %zx\n", offset, structure->header.type,
00158                       structure->header.len, structure->strings_len );
00159 
00160                 /* If this is the structure we want, return */
00161                 if ( ( structure->header.type == type ) &&
00162                      ( instance-- == 0 ) ) {
00163                         structure->offset = offset;
00164                         return 0;
00165                 }
00166 
00167                 /* Move to next SMBIOS structure */
00168                 offset = ( terminator_offset + 1 );
00169                 count++;
00170         }
00171 
00172         DBG ( "SMBIOS structure type %d not found\n", type );
00173         return -ENOENT;
00174 }
00175 
00176 /**
00177  * Copy SMBIOS structure
00178  *
00179  * @v structure         SMBIOS structure descriptor
00180  * @v data              Buffer to hold SMBIOS structure
00181  * @v len               Length of buffer
00182  * @ret rc              Return status code
00183  */
00184 int read_smbios_structure ( struct smbios_structure *structure,
00185                             void *data, size_t len ) {
00186 
00187         assert ( smbios.address != UNULL );
00188 
00189         if ( len > structure->header.len )
00190                 len = structure->header.len;
00191         copy_from_user ( data, smbios.address, structure->offset, len );
00192         return 0;
00193 }
00194 
00195 /**
00196  * Find indexed string within SMBIOS structure
00197  *
00198  * @v structure         SMBIOS structure descriptor
00199  * @v index             String index
00200  * @v data              Buffer for string
00201  * @v len               Length of string buffer
00202  * @ret rc              Length of string, or negative error
00203  */
00204 int read_smbios_string ( struct smbios_structure *structure,
00205                          unsigned int index, void *data, size_t len ) {
00206         size_t strings_start = ( structure->offset + structure->header.len );
00207         size_t strings_end = ( strings_start + structure->strings_len );
00208         size_t offset;
00209         size_t string_len;
00210 
00211         assert ( smbios.address != UNULL );
00212 
00213         /* String numbers start at 1 (0 is used to indicate "no string") */
00214         if ( ! index )
00215                 return -ENOENT;
00216 
00217         for ( offset = strings_start ; offset < strings_end ;
00218               offset += ( string_len + 1 ) ) {
00219                 /* Get string length.  This is known safe, since the
00220                  * smbios_strings struct is constructed so as to
00221                  * always end on a string boundary.
00222                  */
00223                 string_len = strlen_user ( smbios.address, offset );
00224                 if ( --index == 0 ) {
00225                         /* Copy string, truncating as necessary. */
00226                         if ( len > string_len )
00227                                 len = string_len;
00228                         copy_from_user ( data, smbios.address, offset, len );
00229                         return string_len;
00230                 }
00231         }
00232 
00233         DBG ( "SMBIOS string index %d not found\n", index );
00234         return -ENOENT;
00235 }
00236 
00237 /**
00238  * Get SMBIOS version
00239  *
00240  * @ret version         Version, or negative error
00241  */
00242 int smbios_version ( void ) {
00243         int rc;
00244 
00245         /* Find SMBIOS */
00246         if ( ( smbios.address == UNULL ) &&
00247              ( ( rc = find_smbios ( &smbios ) ) != 0 ) )
00248                 return rc;
00249         assert ( smbios.address != UNULL );
00250 
00251         return smbios.version;
00252 }