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