1 /**
2  * Functions for generating pseudo-random CPER PCIe error sections.
3  *
4  * Author: Lawrence.Tang@arm.com
5  **/
6 
7 #include <stdlib.h>
8 #include "../../edk/BaseTypes.h"
9 #include "../gen-utils.h"
10 #include "gen-section.h"
11 
12 #define PCIE_PORT_TYPES                                                        \
13 	(int[])                                                                \
14 	{                                                                      \
15 		0, 1, 4, 5, 6, 7, 8, 9, 10                                     \
16 	}
17 
18 //Generates a single pseudo-random PCIe error section, saving the resulting address to the given
19 //location. Returns the size of the newly created section.
generate_section_pcie(void ** location)20 size_t generate_section_pcie(void **location)
21 {
22 	//Create random bytes.
23 	int size = 208;
24 	UINT8 *bytes = generate_random_bytes(size);
25 
26 	//Set reserved areas to zero.
27 	UINT64 *validation = (UINT64 *)bytes;
28 	*validation &= 0xFF;   //Validation 8-63
29 	UINT32 *version = (UINT32 *)(bytes + 12);
30 	*version &= 0xFFFF;    //Version bytes 2-3
31 	UINT32 *reserved = (UINT32 *)(bytes + 20);
32 	*reserved = 0;	       //Reserved bytes 20-24
33 	*(bytes + 37) &= ~0x7; //Device ID byte 13 bits 0-3
34 	*(bytes + 39) = 0;     //Device ID byte 15
35 
36 	//Set expected values.
37 	int minor = rand() % 128;
38 	int major = rand() % 128;
39 	*version = int_to_bcd(minor);
40 	*version |= int_to_bcd(major) << 8;
41 
42 	//Fix values that could be above range.
43 	UINT32 *port_type = (UINT32 *)(bytes + 8);
44 	*port_type = PCIE_PORT_TYPES[rand() %
45 				     (sizeof(PCIE_PORT_TYPES) / sizeof(int))];
46 
47 	//Set return values, exit.
48 	*location = bytes;
49 	return size;
50 }
51