]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[linux] Provide access to SMBIOS via /dev/mem
authorMichael Brown <mcb30@ipxe.org>
Thu, 5 Dec 2013 03:15:53 +0000 (03:15 +0000)
committerMichael Brown <mcb30@ipxe.org>
Thu, 5 Dec 2013 03:16:27 +0000 (03:16 +0000)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/arch/i386/interface/pcbios/bios_smbios.c
src/include/ipxe/smbios.h
src/interface/linux/linux_smbios.c
src/interface/smbios/smbios.c

index 76a32e93db69081b8962d3068e4fe1be9e28aab9..dd7897e291dd62c0ffef3e3918bd9b48ec160ee2 100644 (file)
@@ -41,49 +41,21 @@ FILE_LICENCE ( GPL2_OR_LATER );
  * @ret rc             Return status code
  */
 static int bios_find_smbios ( struct smbios *smbios ) {
-       union {
-               struct smbios_entry entry;
-               uint8_t bytes[256]; /* 256 is maximum length possible */
-       } u;
-       static unsigned int offset = 0;
-       size_t len;
-       unsigned int i;
-       uint8_t sum;
+       struct smbios_entry entry;
+       int rc;
 
-       /* Try to find SMBIOS */
-       for ( ; offset < 0x10000 ; offset += 0x10 ) {
+       /* Scan through BIOS segment to find SMBIOS entry point */
+       if ( ( rc = find_smbios_entry ( real_to_user ( BIOS_SEG, 0 ), 0x10000,
+                                       &entry ) ) != 0 )
+               return rc;
 
-               /* Read start of header and verify signature */
-               copy_from_real ( &u.entry, BIOS_SEG, offset,
-                                sizeof ( u.entry ));
-               if ( u.entry.signature != SMBIOS_SIGNATURE )
-                       continue;
+       /* Fill in entry point descriptor structure */
+       smbios->address = phys_to_user ( entry.smbios_address );
+       smbios->len = entry.smbios_len;
+       smbios->count = entry.smbios_count;
+       smbios->version = SMBIOS_VERSION ( entry.major, entry.minor );
 
-               /* Read whole header and verify checksum */
-               len = u.entry.len;
-               copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
-               for ( i = 0 , sum = 0 ; i < len ; i++ ) {
-                       sum += u.bytes[i];
-               }
-               if ( sum != 0 ) {
-                       DBG ( "SMBIOS at %04x:%04x has bad checksum %02x\n",
-                             BIOS_SEG, offset, sum );
-                       continue;
-               }
-
-               /* Fill result structure */
-               DBG ( "Found SMBIOS v%d.%d entry point at %04x:%04x\n",
-                     u.entry.major, u.entry.minor, BIOS_SEG, offset );
-               smbios->address = phys_to_user ( u.entry.smbios_address );
-               smbios->len = u.entry.smbios_len;
-               smbios->count = u.entry.smbios_count;
-               smbios->version =
-                       SMBIOS_VERSION ( u.entry.major, u.entry.minor );
-               return 0;
-       }
-
-       DBG ( "No SMBIOS found\n" );
-       return -ENODEV;
+       return 0;
 }
 
 PROVIDE_SMBIOS ( pcbios, find_smbios, bios_find_smbios );
index aaba6cb12ac77b902374e2daefe5f04e0a1d3256..1a34ac04c63d5a7ee9d3e15c9ffd6d15fccf2366 100644 (file)
@@ -162,6 +162,8 @@ struct smbios {
 #define SMBIOS_VERSION( major, minor ) ( ( (major) << 8 ) | (minor) )
 
 extern int find_smbios ( struct smbios *smbios );
+extern int find_smbios_entry ( userptr_t start, size_t len,
+                              struct smbios_entry *entry );
 extern int find_smbios_structure ( unsigned int type, unsigned int instance,
                                   struct smbios_structure *structure );
 extern int read_smbios_structure ( struct smbios_structure *structure,
index f99120bf6388c17fe074ec1fbcfeb5906a9b4e89..6e5174d23c2cc0e7ed8d3b8ba08872c30e18a46f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Piotr JaroszyƄski <p.jaroszynski@gmail.com>
+ * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
  */
 
-FILE_LICENCE(GPL2_OR_LATER);
+FILE_LICENCE ( GPL2_OR_LATER );
 
 #include <errno.h>
+#include <linux_api.h>
+#include <ipxe/linux.h>
 #include <ipxe/smbios.h>
 
+/** SMBIOS filename */
+static const char smbios_filename[] = "/dev/mem";
+
+/** SMBIOS entry point scan region start address */
+#define SMBIOS_ENTRY_START 0xf0000
+
+/** SMBIOS entry point scan region length */
+#define SMBIOS_ENTRY_LEN 0x10000
+
+/** SMBIOS mapping alignment */
+#define SMBIOS_ALIGN 0x1000
+
 /**
  * Find SMBIOS
  *
- * Not implemented currently.
- *
  * @v smbios           SMBIOS entry point descriptor structure to fill in
  * @ret rc             Return status code
  */
-static int linux_find_smbios(struct smbios *smbios __unused)
-{
-       return -ENODEV;
+static int linux_find_smbios ( struct smbios *smbios ) {
+       struct smbios_entry entry;
+       void *entry_mem;
+       void *smbios_mem;
+       size_t smbios_offset;
+       size_t smbios_indent;
+       size_t smbios_len;
+       int fd;
+       int rc;
+
+       /* Open SMBIOS file */
+       fd = linux_open ( smbios_filename, O_RDONLY );
+       if ( fd < 0 ) {
+               rc = -ELINUX ( linux_errno );
+               DBGC ( smbios, "SMBIOS could not open %s: %s\n",
+                      smbios_filename, linux_strerror ( linux_errno ) );
+               goto err_open;
+       }
+
+       /* Map the region potentially containing the SMBIOS entry point */
+       entry_mem = linux_mmap ( NULL, SMBIOS_ENTRY_LEN, PROT_READ, MAP_SHARED,
+                                fd, SMBIOS_ENTRY_START );
+       if ( entry_mem == MAP_FAILED ) {
+               rc = -ELINUX ( linux_errno );
+               DBGC ( smbios, "SMBIOS could not mmap %s (%#x+%#x): %s\n",
+                      smbios_filename, SMBIOS_ENTRY_START, SMBIOS_ENTRY_LEN,
+                      linux_strerror ( linux_errno ) );
+               goto err_mmap_entry;
+       }
+
+       /* Scan for the SMBIOS entry point */
+       if ( ( rc = find_smbios_entry ( virt_to_user ( entry_mem ),
+                                       SMBIOS_ENTRY_LEN, &entry ) ) != 0 )
+               goto err_find_entry;
+
+       /* Map the region containing the SMBIOS structures */
+       smbios_indent = ( entry.smbios_address & ( SMBIOS_ALIGN - 1 ) );
+       smbios_offset = ( entry.smbios_address - smbios_indent );
+       smbios_len = ( entry.smbios_len + smbios_indent );
+       smbios_mem = linux_mmap ( NULL, smbios_len, PROT_READ, MAP_SHARED,
+                                 fd, smbios_offset );
+       if ( smbios_mem == MAP_FAILED ) {
+               rc = -ELINUX ( linux_errno );
+               DBGC ( smbios, "SMBIOS could not mmap %s (%#zx+%#zx): %s\n",
+                      smbios_filename, smbios_offset, smbios_len,
+                      linux_strerror ( linux_errno ) );
+               goto err_mmap_smbios;
+       }
+
+       /* Fill in entry point descriptor structure */
+       smbios->address = virt_to_user ( smbios_mem + smbios_indent );
+       smbios->len = entry.smbios_len;
+       smbios->count = entry.smbios_count;
+       smbios->version = SMBIOS_VERSION ( entry.major, entry.minor );
+
+       /* Unmap the entry point region (no longer required) */
+       linux_munmap ( entry_mem, SMBIOS_ENTRY_LEN );
+
+       return 0;
+
+       linux_munmap ( smbios_mem, smbios_len );
+ err_mmap_smbios:
+ err_find_entry:
+       linux_munmap ( entry_mem, SMBIOS_ENTRY_LEN );
+ err_mmap_entry:
+       linux_close ( fd );
+ err_open:
+       return rc;
 }
 
-PROVIDE_SMBIOS(linux, find_smbios, linux_find_smbios);
+PROVIDE_SMBIOS ( linux, find_smbios, linux_find_smbios );
index 90907e18e45eddd98012437dd92bb81a670b8781..856943428eff70370dc64845008049346ed63458 100644 (file)
@@ -37,6 +37,54 @@ static struct smbios smbios = {
        .address = UNULL,
 };
 
+/**
+ * Scan for SMBIOS entry point structure
+ *
+ * @v start            Start address of region to scan
+ * @v len              Length of region to scan
+ * @v entry            SMBIOS entry point structure to fill in
+ * @ret rc             Return status code
+ */
+int find_smbios_entry ( userptr_t start, size_t len,
+                       struct smbios_entry *entry ) {
+       uint8_t buf[256]; /* 256 is maximum length possible */
+       static size_t offset = 0; /* Avoid repeated attempts to locate SMBIOS */
+       size_t entry_len;
+       unsigned int i;
+       uint8_t sum;
+
+       /* Try to find SMBIOS */
+       for ( ; offset < len ; offset += 0x10 ) {
+
+               /* Read start of header and verify signature */
+               copy_from_user ( entry, start, offset, sizeof ( *entry ) );
+               if ( entry->signature != SMBIOS_SIGNATURE )
+                       continue;
+
+               /* Read whole header and verify checksum */
+               entry_len = entry->len;
+               assert ( entry_len <= sizeof ( buf ) );
+               copy_from_user ( buf, start, offset, entry_len );
+               for ( i = 0, sum = 0 ; i < entry_len ; i++ ) {
+                       sum += buf[i];
+               }
+               if ( sum != 0 ) {
+                       DBG ( "SMBIOS at %08lx has bad checksum %02x\n",
+                             user_to_phys ( start, offset ), sum );
+                       continue;
+               }
+
+               /* Fill result structure */
+               DBG ( "Found SMBIOS v%d.%d entry point at %08lx\n",
+                     entry->major, entry->minor,
+                     user_to_phys ( start, offset ) );
+               return 0;
+       }
+
+       DBG ( "No SMBIOS found\n" );
+       return -ENODEV;
+}
+
 /**
  * Find SMBIOS strings terminator
  *