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. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 3 | * |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 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 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 12 | #define PCIE_PORT_TYPES \ |
| 13 | (int[]) \ |
| 14 | { \ |
| 15 | 0, 1, 4, 5, 6, 7, 8, 9, 10 \ |
| 16 | } |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 17 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 18 | //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] | 19 | //location. Returns the size of the newly created section. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 20 | size_t generate_section_pcie(void **location) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 21 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 22 | //Create random bytes. |
| 23 | int size = 208; |
| 24 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 25 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 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 |
Lawrence Tang | 3ab351f | 2022-07-20 16:09:34 +0100 | [diff] [blame] | 35 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 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; |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 41 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 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 | } |