2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
23 #include <gpxe/uaccess.h>
30 * System Management BIOS
34 /** Signature for SMBIOS entry point */
35 #define SMBIOS_SIGNATURE \
36 ( ( '_' << 0 ) + ( 'S' << 8 ) + ( 'M' << 16 ) + ( '_' << 24 ) )
41 * This is the single table which describes the list of SMBIOS
42 * structures. It is located by scanning through the BIOS segment.
47 * Must be equal to SMBIOS_SIGNATURE
58 /** Maximum structure size */
60 /** Entry point revision */
65 uint8_t dmi_signature[5];
68 /** Structure table length */
69 uint16_t smbios_length;
70 /** Structure table address */
71 physaddr_t smbios_address;
72 /** Number of SMBIOS structures */
73 uint16_t smbios_count;
76 } __attribute__ (( packed ));
79 * SMBIOS entry point descriptor
81 * This contains the information from the SMBIOS entry point that we
85 /** Start of SMBIOS structures */
87 /** Length of SMBIOS structures */
89 /** Number of SMBIOS structures */
94 * SMBIOS strings descriptor
96 * This is returned as part of the search for an SMBIOS structure, and
97 * contains the information needed for extracting the strings within
98 * the "unformatted" portion of the structure.
100 struct smbios_strings {
101 /** Start of strings data */
103 /** Length of strings data */
110 * @ret smbios SMBIOS entry point descriptor, or NULL if not found
112 static struct smbios * find_smbios ( void ) {
113 static struct smbios smbios = {
117 struct smbios_entry entry;
118 uint8_t bytes[256]; /* 256 is maximum length possible */
125 /* Return cached result if available */
126 if ( smbios.address != UNULL )
129 /* Try to find SMBIOS */
130 for ( offset = 0 ; offset < 0x10000 ; offset += 0x10 ) {
132 /* Read start of header and verify signature */
133 copy_from_real ( &u.entry, BIOS_SEG, offset,
135 if ( u.entry.signature != SMBIOS_SIGNATURE )
138 /* Read whole header and verify checksum */
139 len = u.entry.length;
140 copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
141 for ( i = 0 , sum = 0 ; i < len ; i++ ) {
145 DBG ( "SMBIOS at %04x:%04x has bad checksum %02x\n",
146 BIOS_SEG, offset, sum );
150 /* Fill result structure */
151 DBG ( "Found SMBIOS entry point at %04x:%04x\n",
153 smbios.address = phys_to_user ( u.entry.smbios_address );
154 smbios.length = u.entry.smbios_length;
155 smbios.count = u.entry.smbios_count;
159 DBG ( "No SMBIOS found\n" );
164 * Find SMBIOS strings terminator
166 * @v smbios SMBIOS entry point descriptor
167 * @v offset Offset to start of strings
168 * @ret offset Offset to strings terminator, or 0 if not found
170 static size_t find_strings_terminator ( struct smbios *smbios,
172 size_t max_offset = ( smbios->length - 2 );
175 for ( ; offset <= max_offset ; offset++ ) {
176 copy_from_user ( &nulnul, smbios->address, offset, 2 );
178 return ( offset + 1 );
184 * Find specific structure type within SMBIOS
186 * @v type Structure type to search for
187 * @v structure Buffer to fill in with structure
188 * @v length Length of buffer
189 * @v strings Strings descriptor to fill in, or NULL
190 * @ret rc Return status code
192 int find_smbios_structure ( unsigned int type, void *structure,
193 size_t length, struct smbios_strings *strings ) {
194 struct smbios *smbios;
195 struct smbios_header header;
196 struct smbios_strings temp_strings;
197 unsigned int count = 0;
199 size_t strings_offset;
200 size_t terminator_offset;
202 /* Locate SMBIOS entry point */
203 if ( ! ( smbios = find_smbios() ) )
206 /* Ensure that we have a usable strings descriptor buffer */
208 strings = &temp_strings;
210 /* Scan through list of structures */
211 while ( ( ( offset + sizeof ( header ) ) < smbios->length ) &&
212 ( count < smbios->count ) ) {
214 /* Read next SMBIOS structure header */
215 copy_from_user ( &header, smbios->address, offset,
218 /* Determine start and extent of strings block */
219 strings_offset = ( offset + header.length );
220 if ( strings_offset > smbios->length ) {
221 DBG ( "SMBIOS structure at offset %zx with length "
222 "%zx extends beyond SMBIOS\n", offset,
227 find_strings_terminator ( smbios, strings_offset );
228 if ( ! terminator_offset ) {
229 DBG ( "SMBIOS structure at offset %zx has "
230 "unterminated strings section\n", offset );
233 strings->data = userptr_add ( smbios->address,
235 strings->length = ( terminator_offset - strings_offset );
237 DBG ( "SMBIOS structure at offset %zx has type %d, "
238 "length %zx, strings length %zx\n",
239 offset, header.type, header.length, strings->length );
241 /* If this is the structure we want, return */
242 if ( header.type == type ) {
243 if ( length > header.length )
244 length = header.length;
245 copy_from_user ( structure, smbios->address,
250 /* Move to next SMBIOS structure */
251 offset = ( terminator_offset + 1 );
255 DBG ( "SMBIOS structure type %d not found\n", type );
260 * Find indexed string within SMBIOS structure
262 * @v strings SMBIOS strings descriptor
263 * @v index String index
264 * @v buffer Buffer for string
265 * @v length Length of string buffer
266 * @ret rc Return status code
268 int find_smbios_string ( struct smbios_strings *strings, unsigned int index,
269 char *buffer, size_t length ) {
273 /* Zero buffer. This ensures that a valid NUL terminator is
274 * always present (unless length==0).
276 memset ( buffer, 0, length );
278 /* String numbers start at 1 (0 is used to indicate "no string") */
282 while ( offset < strings->length ) {
283 /* Get string length. This is known safe, since the
284 * smbios_strings struct is constructed so as to
285 * always end on a string boundary.
287 string_len = strlen_user ( strings->data, offset );
288 if ( --index == 0 ) {
289 /* Copy string, truncating as necessary. */
290 if ( string_len >= length )
291 string_len = ( length - 1 );
292 copy_from_user ( buffer, strings->data,
293 offset, string_len );
296 offset += ( string_len + 1 );
299 DBG ( "SMBIOS string index %d not found\n", index );
304 * Find SMBIOS serial number
307 int dump_smbios_info ( void ) {
308 struct smbios_system_information sysinfo;
309 struct smbios_strings strings;
313 if ( ( rc = find_smbios_structure ( SMBIOS_TYPE_SYSTEM_INFORMATION,
314 &sysinfo, sizeof ( sysinfo ),
318 DBG_HD ( &sysinfo, sizeof ( sysinfo ) );
320 if ( ( rc = find_smbios_string ( &strings, sysinfo.manufacturer,
321 buf, sizeof ( buf ) ) ) != 0 )
323 DBG ( "Manufacturer: \"%s\"\n", buf );
325 if ( ( rc = find_smbios_string ( &strings, sysinfo.product,
326 buf, sizeof ( buf ) ) ) != 0 )
328 DBG ( "Product: \"%s\"\n", buf );
331 DBG_HD ( &sysinfo.uuid, sizeof ( sysinfo.uuid ) );