iPXE
pnpbios.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 <realmode.h>
00030 #include <pnpbios.h>
00031 
00032 /** @file
00033  *
00034  * PnP BIOS
00035  *
00036  */
00037 
00038 /** PnP BIOS structure */
00039 struct pnp_bios {
00040         /** Signature
00041          *
00042          * Must be equal to @c PNP_BIOS_SIGNATURE
00043          */
00044         uint32_t signature;
00045         /** Version as BCD (e.g. 1.0 is 0x10) */
00046         uint8_t version;
00047         /** Length of this structure */
00048         uint8_t length;
00049         /** System capabilities */
00050         uint16_t control;
00051         /** Checksum */
00052         uint8_t checksum;
00053 } __attribute__ (( packed ));
00054 
00055 /** Signature for a PnP BIOS structure */
00056 #define PNP_BIOS_SIGNATURE \
00057         ( ( '$' << 0 ) + ( 'P' << 8 ) + ( 'n' << 16 ) + ( 'P' << 24 ) )
00058 
00059 /**
00060  * Test address for PnP BIOS structure
00061  *
00062  * @v offset            Offset within BIOS segment to test
00063  * @ret rc              Return status code
00064  */
00065 static int is_pnp_bios ( unsigned int offset ) {
00066         union {
00067                 struct pnp_bios pnp_bios;
00068                 uint8_t bytes[256]; /* 256 is maximum length possible */
00069         } u;
00070         size_t len;
00071         unsigned int i;
00072         uint8_t sum = 0;
00073 
00074         /* Read start of header and verify signature */
00075         copy_from_real ( &u.pnp_bios, BIOS_SEG, offset, sizeof ( u.pnp_bios ));
00076         if ( u.pnp_bios.signature != PNP_BIOS_SIGNATURE )
00077                 return -EINVAL;
00078 
00079         /* Read whole header and verify checksum */
00080         len = u.pnp_bios.length;
00081         copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
00082         for ( i = 0 ; i < len ; i++ ) {
00083                 sum += u.bytes[i];
00084         }
00085         if ( sum != 0 )
00086                 return -EINVAL;
00087 
00088         DBG ( "Found PnP BIOS at %04x:%04x\n", BIOS_SEG, offset );
00089 
00090         return 0;
00091 }
00092 
00093 /**
00094  * Locate Plug-and-Play BIOS
00095  *
00096  * @ret pnp_offset      Offset of PnP BIOS structure within BIOS segment
00097  *
00098  * The PnP BIOS structure will be at BIOS_SEG:pnp_offset.  If no PnP
00099  * BIOS is found, -1 is returned.
00100  */
00101 int find_pnp_bios ( void ) {
00102         static int pnp_offset = 0;
00103 
00104         if ( pnp_offset )
00105                 return pnp_offset;
00106 
00107         for ( pnp_offset = 0 ; pnp_offset < 0x10000 ; pnp_offset += 0x10 ) {
00108                 if ( is_pnp_bios ( pnp_offset ) == 0 )
00109                         return pnp_offset;
00110         }
00111 
00112         pnp_offset = -1;
00113         return pnp_offset;
00114 }