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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! PCI I/O Protocol
//!
//! Used by code, typically drivers, running in the EFI boot services
//! environment to access memory and I/O on a PCI controller.

pub const PROTOCOL_GUID: crate::base::Guid = crate::base::Guid::from_fields(
    0x4cf5b200,
    0x68b8,
    0x4ca5,
    0x9e,
    0xec,
    &[0xb2, 0x3e, 0x3f, 0x50, 0x02, 0x9a],
);

pub type Width = u32;

pub const WIDTH_UINT8: Width = 0x00000000;
pub const WIDTH_UINT16: Width = 0x00000001;
pub const WIDTH_UINT32: Width = 0x00000002;
pub const WIDTH_UINT64: Width = 0x00000003;
pub const WIDTH_FIFO_UINT8: Width = 0x00000004;
pub const WIDTH_FIFO_UINT16: Width = 0x00000005;
pub const WIDTH_FIFO_UINT32: Width = 0x00000006;
pub const WIDTH_FIFO_UINT64: Width = 0x00000007;
pub const WIDTH_FILL_UINT8: Width = 0x00000008;
pub const WIDTH_FILL_UINT16: Width = 0x00000009;
pub const WIDTH_FILL_UINT32: Width = 0x0000000a;
pub const WIDTH_FILL_UINT64: Width = 0x0000000b;
pub const WIDTH_MAXIMUM: Width = 0x0000000c;

pub type Operation = u32;

pub const OPERATION_BUS_MASTER_READ: Operation = 0x00000000;
pub const OPERATION_BUS_MASTER_WRITE: Operation = 0x00000001;
pub const OPERATION_BUS_MASTER_COMMON_BUFFER: Operation = 0x00000002;
pub const OPERATION_MAXIMUM: Operation = 0x00000003;

pub type Attribute = u64;

pub const ATTRIBUTE_ISA_MOTHERBOARD_IO: Attribute = 0x00000001;
pub const ATTRIBUTE_ISA_IO: Attribute = 0x00000002;
pub const ATTRIBUTE_VGA_PALETTE_IO: Attribute = 0x00000004;
pub const ATTRIBUTE_VGA_MEMORY: Attribute = 0x00000008;
pub const ATTRIBUTE_VGA_IO: Attribute = 0x00000010;
pub const ATTRIBUTE_IDE_PRIMARY_IO: Attribute = 0x00000020;
pub const ATTRIBUTE_IDE_SECONDARY_IO: Attribute = 0x00000040;
pub const ATTRIBUTE_MEMORY_WRITE_COMBINE: Attribute = 0x00000080;
pub const ATTRIBUTE_IO: Attribute = 0x00000100;
pub const ATTRIBUTE_MEMORY: Attribute = 0x00000200;
pub const ATTRIBUTE_BUS_MASTER: Attribute = 0x00000400;
pub const ATTRIBUTE_MEMORY_CACHED: Attribute = 0x00000800;
pub const ATTRIBUTE_MEMORY_DISABLE: Attribute = 0x00001000;
pub const ATTRIBUTE_EMBEDDED_DEVICE: Attribute = 0x00002000;
pub const ATTRIBUTE_EMBEDDED_ROM: Attribute = 0x00004000;
pub const ATTRIBUTE_DUAL_ADDRESS_CYCLE: Attribute = 0x00008000;
pub const ATTRIBUTE_ISA_IO_16: Attribute = 0x00010000;
pub const ATTRIBUTE_VGA_PALETTE_IO_16: Attribute = 0x00020000;
pub const ATTRIBUTE_VGA_IO_16: Attribute = 0x00040000;

pub type AttributeOperation = u32;

pub const ATTRIBUTE_OPERATION_GET: AttributeOperation = 0x00000000;
pub const ATTRIBUTE_OPERATION_SET: AttributeOperation = 0x00000001;
pub const ATTRIBUTE_OPERATION_ENABLE: AttributeOperation = 0x00000002;
pub const ATTRIBUTE_OPERATION_DISABLE: AttributeOperation = 0x00000003;
pub const ATTRIBUTE_OPERATION_SUPPORTED: AttributeOperation = 0x00000004;
pub const ATTRIBUTE_OPERATION_MAXIMUM: AttributeOperation = 0x00000005;

pub const PASS_THROUGH_BAR: u8 = 0xff;

pub type ProtocolPollIoMem = eficall! {fn(
    *mut Protocol,
    Width,
    u8,
    u64,
    u64,
    u64,
    u64,
    *mut u64,
) -> crate::base::Status};

pub type ProtocolIoMem = eficall! {fn(
    *mut Protocol,
    Width,
    u8,
    u64,
    usize,
    *mut core::ffi::c_void,
) -> crate::base::Status};

pub type ProtocolConfig = eficall! {fn(
    *mut Protocol,
    Width,
    u32,
    usize,
    *mut core::ffi::c_void,
) -> crate::base::Status};

pub type ProtocolCopyMem = eficall! {fn(
    *mut Protocol,
    Width,
    u8,
    u64,
    u8,
    u64,
    usize,
) -> crate::base::Status};

pub type ProtocolMap = eficall! {fn(
    *mut Protocol,
    Operation,
    *mut core::ffi::c_void,
    *mut usize,
    *mut crate::base::PhysicalAddress,
    *mut *mut core::ffi::c_void,
) -> crate::base::Status};

pub type ProtocolUnmap = eficall! {fn(
    *mut Protocol,
    *mut core::ffi::c_void,
) -> crate::base::Status};

pub type ProtocolAllocateBuffer = eficall! {fn(
    *mut Protocol,
    crate::system::AllocateType,
    crate::system::MemoryType,
    usize,
    *mut *mut core::ffi::c_void,
    Attribute,
) -> crate::base::Status};

pub type ProtocolFreeBuffer = eficall! {fn(
    *mut Protocol,
    usize,
    *mut core::ffi::c_void,
) -> crate::base::Status};

pub type ProtocolFlush = eficall! {fn(
    *mut Protocol,
) -> crate::base::Status};

pub type ProtocolGetLocation = eficall! {fn(
    *mut Protocol,
    *mut usize,
    *mut usize,
    *mut usize,
    *mut usize,
) -> crate::base::Status};

pub type ProtocolAttributes = eficall! {fn(
    *mut Protocol,
    AttributeOperation,
    Attribute,
    *mut Attribute,
) -> crate::base::Status};

pub type ProtocolGetBarAttributes = eficall! {fn(
    *mut Protocol,
    u8,
    *mut Attribute,
    *mut *mut core::ffi::c_void,
) -> crate::base::Status};

pub type ProtocolSetBarAttributes = eficall! {fn(
    *mut Protocol,
    Attribute,
    u8,
    *mut u64,
    *mut u64,
) -> crate::base::Status};

#[repr(C)]
pub struct Access {
    pub read: ProtocolIoMem,
    pub write: ProtocolIoMem,
}

#[repr(C)]
pub struct ConfigAccess {
    pub read: ProtocolConfig,
    pub write: ProtocolConfig,
}

#[repr(C)]
pub struct Protocol {
    pub poll_mem: ProtocolPollIoMem,
    pub poll_io: ProtocolPollIoMem,
    pub mem: Access,
    pub io: Access,
    pub pci: ConfigAccess,
    pub copy_mem: ProtocolCopyMem,
    pub map: ProtocolMap,
    pub unmap: ProtocolUnmap,
    pub allocate_buffer: ProtocolAllocateBuffer,
    pub free_buffer: ProtocolFreeBuffer,
    pub flush: ProtocolFlush,
    pub get_location: ProtocolGetLocation,
    pub attributes: ProtocolAttributes,
    pub get_bar_attributes: ProtocolGetBarAttributes,
    pub set_bar_attributes: ProtocolSetBarAttributes,
    pub rom_size: u64,
    pub rom_image: *mut core::ffi::c_void,
}