Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / firmware / pcbios / pnpbios.c
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <realmode.h>
26 #include <pnpbios.h>
27
28 /** @file
29  *
30  * PnP BIOS
31  *
32  */
33
34 /** PnP BIOS structure */
35 struct pnp_bios {
36         /** Signature
37          *
38          * Must be equal to @c PNP_BIOS_SIGNATURE
39          */
40         uint32_t signature;
41         /** Version as BCD (e.g. 1.0 is 0x10) */
42         uint8_t version;
43         /** Length of this structure */
44         uint8_t length;
45         /** System capabilities */
46         uint16_t control;
47         /** Checksum */
48         uint8_t checksum;
49 } __attribute__ (( packed ));
50
51 /** Signature for a PnP BIOS structure */
52 #define PNP_BIOS_SIGNATURE \
53         ( ( '$' << 0 ) + ( 'P' << 8 ) + ( 'n' << 16 ) + ( 'P' << 24 ) )
54
55 /**
56  * Test address for PnP BIOS structure
57  *
58  * @v offset            Offset within BIOS segment to test
59  * @ret rc              Return status code
60  */
61 static int is_pnp_bios ( unsigned int offset ) {
62         union {
63                 struct pnp_bios pnp_bios;
64                 uint8_t bytes[256]; /* 256 is maximum length possible */
65         } u;
66         size_t len;
67         unsigned int i;
68         uint8_t sum = 0;
69
70         /* Read start of header and verify signature */
71         copy_from_real ( &u.pnp_bios, BIOS_SEG, offset, sizeof ( u.pnp_bios ));
72         if ( u.pnp_bios.signature != PNP_BIOS_SIGNATURE )
73                 return -EINVAL;
74
75         /* Read whole header and verify checksum */
76         len = u.pnp_bios.length;
77         copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
78         for ( i = 0 ; i < len ; i++ ) {
79                 sum += u.bytes[i];
80         }
81         if ( sum != 0 )
82                 return -EINVAL;
83
84         DBG ( "Found PnP BIOS at %04x:%04x\n", BIOS_SEG, offset );
85
86         return 0;
87 }
88
89 /**
90  * Locate Plug-and-Play BIOS
91  *
92  * @ret pnp_offset      Offset of PnP BIOS structure within BIOS segment
93  *
94  * The PnP BIOS structure will be at BIOS_SEG:pnp_offset.  If no PnP
95  * BIOS is found, -1 is returned.
96  */
97 int find_pnp_bios ( void ) {
98         static int pnp_offset = 0;
99
100         if ( pnp_offset )
101                 return pnp_offset;
102
103         for ( pnp_offset = 0 ; pnp_offset < 0x10000 ; pnp_offset += 0x10 ) {
104                 if ( is_pnp_bios ( pnp_offset ) == 0 )
105                         return pnp_offset;
106         }
107
108         pnp_offset = -1;
109         return pnp_offset;
110 }