blob: 3547f1dad8784548ca6a30b2c55d524bcab1a696 [file] [log] [blame]
Lawrence Tangde9707f2022-07-19 10:54:31 +01001/**
Lawrence Tangefe17e22022-08-08 09:16:23 +01002 * Functions for generating pseudo-random CPER PCIe error sections.
Ed Tanousfedd4572024-07-12 13:56:00 -07003 *
Lawrence Tangde9707f2022-07-19 10:54:31 +01004 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include <stdlib.h>
8#include "../../edk/BaseTypes.h"
9#include "../gen-utils.h"
Lawrence Tang8f977452022-08-24 14:55:07 +010010#include "gen-section.h"
Lawrence Tangde9707f2022-07-19 10:54:31 +010011
John Chungf8fc7052024-05-03 20:05:29 +080012#define PCIE_PORT_TYPES \
13 (int[]) \
14 { \
15 0, 1, 4, 5, 6, 7, 8, 9, 10 \
16 }
Lawrence Tangde9707f2022-07-19 10:54:31 +010017
Lawrence Tangefe17e22022-08-08 09:16:23 +010018//Generates a single pseudo-random PCIe error section, saving the resulting address to the given
Lawrence Tangde9707f2022-07-19 10:54:31 +010019//location. Returns the size of the newly created section.
John Chungf8fc7052024-05-03 20:05:29 +080020size_t generate_section_pcie(void **location)
Lawrence Tangde9707f2022-07-19 10:54:31 +010021{
John Chungf8fc7052024-05-03 20:05:29 +080022 //Create random bytes.
23 int size = 208;
24 UINT8 *bytes = generate_random_bytes(size);
Lawrence Tangde9707f2022-07-19 10:54:31 +010025
John Chungf8fc7052024-05-03 20:05:29 +080026 //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
Lawrence Tang3ab351f2022-07-20 16:09:34 +010035
John Chungf8fc7052024-05-03 20:05:29 +080036 //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;
Lawrence Tangde9707f2022-07-19 10:54:31 +010041
John Chungf8fc7052024-05-03 20:05:29 +080042 //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}