iPXE
pnpbios.c
Go to the documentation of this file.
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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <realmode.h>
30 #include <pnpbios.h>
31 
32 /** @file
33  *
34  * PnP BIOS
35  *
36  */
37 
38 /** PnP BIOS structure */
39 struct pnp_bios {
40  /** Signature
41  *
42  * Must be equal to @c PNP_BIOS_SIGNATURE
43  */
45  /** Version as BCD (e.g. 1.0 is 0x10) */
47  /** Length of this structure */
49  /** System capabilities */
51  /** Checksum */
53 } __attribute__ (( packed ));
54 
55 /** Signature for a PnP BIOS structure */
56 #define PNP_BIOS_SIGNATURE \
57  ( ( '$' << 0 ) + ( 'P' << 8 ) + ( 'n' << 16 ) + ( 'P' << 24 ) )
58 
59 /**
60  * Test address for PnP BIOS structure
61  *
62  * @v offset Offset within BIOS segment to test
63  * @ret rc Return status code
64  */
65 static int is_pnp_bios ( unsigned int offset ) {
66  union {
67  struct pnp_bios pnp_bios;
68  uint8_t bytes[256]; /* 256 is maximum length possible */
69  } u;
70  size_t len;
71  unsigned int i;
72  uint8_t sum = 0;
73 
74  /* Read start of header and verify signature */
75  copy_from_real ( &u.pnp_bios, BIOS_SEG, offset, sizeof ( u.pnp_bios ));
76  if ( u.pnp_bios.signature != PNP_BIOS_SIGNATURE )
77  return -EINVAL;
78 
79  /* Read whole header and verify checksum */
80  len = u.pnp_bios.length;
81  copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
82  for ( i = 0 ; i < len ; i++ ) {
83  sum += u.bytes[i];
84  }
85  if ( sum != 0 )
86  return -EINVAL;
87 
88  DBG ( "Found PnP BIOS at %04x:%04x\n", BIOS_SEG, offset );
89 
90  return 0;
91 }
92 
93 /**
94  * Locate Plug-and-Play BIOS
95  *
96  * @ret pnp_offset Offset of PnP BIOS structure within BIOS segment
97  *
98  * The PnP BIOS structure will be at BIOS_SEG:pnp_offset. If no PnP
99  * BIOS is found, -1 is returned.
100  */
101 int find_pnp_bios ( void ) {
102  static int pnp_offset = 0;
103 
104  if ( pnp_offset )
105  return pnp_offset;
106 
107  for ( pnp_offset = 0 ; pnp_offset < 0x10000 ; pnp_offset += 0x10 ) {
108  if ( is_pnp_bios ( pnp_offset ) == 0 )
109  return pnp_offset;
110  }
111 
112  pnp_offset = -1;
113  return pnp_offset;
114 }
PnP BIOS structure.
Definition: pnpbios.c:39
static int is_pnp_bios(unsigned int offset)
Test address for PnP BIOS structure.
Definition: pnpbios.c:65
#define EINVAL
Invalid argument.
Definition: errno.h:428
unsigned short uint16_t
Definition: stdint.h:11
Error codes.
uint16_t control
System capabilities.
Definition: pnpbios.c:50
uint32_t signature
Signature.
Definition: pnpbios.c:44
uint8_t length
Length of this structure.
Definition: pnpbios.c:48
struct pnp_bios __attribute__((packed))
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned char uint8_t
Definition: stdint.h:10
unsigned int uint32_t
Definition: stdint.h:12
PnP BIOS.
uint8_t checksum
Checksum.
Definition: pnpbios.c:52
#define copy_from_real
Definition: libkir.h:79
uint8_t version
Version as BCD (e.g.
Definition: pnpbios.c:46
uint32_t len
Length.
Definition: ena.h:14
#define PNP_BIOS_SIGNATURE
Signature for a PnP BIOS structure.
Definition: pnpbios.c:56
int find_pnp_bios(void)
Locate Plug-and-Play BIOS.
Definition: pnpbios.c:101
union @16 u
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
uint8_t bytes[64]
Definition: ib_mad.h:16
#define BIOS_SEG
Definition: pnpbios.h:13
String functions.