aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/arch/i386/firmware/pcbios/pnpbios.c
blob: c572914f2cb10c7fdf7b7561a300247647391fe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2007 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
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <realmode.h>
#include <pnpbios.h>

/** @file
 *
 * PnP BIOS
 *
 */

/** PnP BIOS structure */
struct pnp_bios {
	/** Signature
	 *
	 * Must be equal to @c PNP_BIOS_SIGNATURE
	 */
	uint32_t signature;
	/** Version as BCD (e.g. 1.0 is 0x10) */
	uint8_t version;
	/** Length of this structure */
	uint8_t length;
	/** System capabilities */
	uint16_t control;
	/** Checksum */
	uint8_t checksum;
} __attribute__ (( packed ));

/** Signature for a PnP BIOS structure */
#define PNP_BIOS_SIGNATURE \
	( ( '$' << 0 ) + ( 'P' << 8 ) + ( 'n' << 16 ) + ( 'P' << 24 ) )

/**
 * Test address for PnP BIOS structure
 *
 * @v offset		Offset within BIOS segment to test
 * @ret rc		Return status code
 */
static int is_pnp_bios ( unsigned int offset ) {
	union {
		struct pnp_bios pnp_bios;
		uint8_t bytes[256]; /* 256 is maximum length possible */
	} u;
	size_t len;
	unsigned int i;
	uint8_t sum = 0;

	/* Read start of header and verify signature */
	copy_from_real ( &u.pnp_bios, BIOS_SEG, offset, sizeof ( u.pnp_bios ));
	if ( u.pnp_bios.signature != PNP_BIOS_SIGNATURE )
		return -EINVAL;

	/* Read whole header and verify checksum */
	len = u.pnp_bios.length;
	copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
	for ( i = 0 ; i < len ; i++ ) {
		sum += u.bytes[i];
	}
	if ( sum != 0 )
		return -EINVAL;

	DBG ( "Found PnP BIOS at %04x:%04x\n", BIOS_SEG, offset );

	return 0;
}

/**
 * Locate Plug-and-Play BIOS
 *
 * @ret pnp_offset	Offset of PnP BIOS structure within BIOS segment
 *
 * The PnP BIOS structure will be at BIOS_SEG:pnp_offset.  If no PnP
 * BIOS is found, -1 is returned.
 */
int find_pnp_bios ( void ) {
	static int pnp_offset = 0;

	if ( pnp_offset )
		return pnp_offset;

	for ( pnp_offset = 0 ; pnp_offset < 0x10000 ; pnp_offset += 0x10 ) {
		if ( is_pnp_bios ( pnp_offset ) == 0 )
			return pnp_offset;
	}

	pnp_offset = -1;
	return pnp_offset;
}