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> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 8 | #include <libcper/BaseTypes.h> |
| 9 | #include <libcper/generator/gen-utils.h> |
| 10 | #include <libcper/generator/sections/gen-section.h> |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 11 | |
Patrick Williams | 30cd66a | 2024-12-18 11:23:15 -0500 | [diff] [blame] | 12 | #define PCIE_PORT_TYPES (int[]){ 0, 1, 4, 5, 6, 7, 8, 9, 10 } |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 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. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 16 | size_t generate_section_pcie(void **location, |
| 17 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 18 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 19 | //Create random bytes. |
| 20 | int size = 208; |
| 21 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 22 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 23 | //Set reserved areas to zero. |
| 24 | UINT64 *validation = (UINT64 *)bytes; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame] | 25 | *validation &= 0xFF; //Validation 8-63 |
| 26 | if (validBitsType == ALL_VALID) { |
| 27 | *validation = 0xFF; |
| 28 | } else if (validBitsType == SOME_VALID) { |
| 29 | *validation = 0x55; |
| 30 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 31 | UINT32 *version = (UINT32 *)(bytes + 12); |
| 32 | *version &= 0xFFFF; //Version bytes 2-3 |
| 33 | UINT32 *reserved = (UINT32 *)(bytes + 20); |
| 34 | *reserved = 0; //Reserved bytes 20-24 |
| 35 | *(bytes + 37) &= ~0x7; //Device ID byte 13 bits 0-3 |
| 36 | *(bytes + 39) = 0; //Device ID byte 15 |
Lawrence Tang | 3ab351f | 2022-07-20 16:09:34 +0100 | [diff] [blame] | 37 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 38 | //Set expected values. |
Ed Tanous | 2d4d3b6 | 2025-03-11 10:34:29 -0700 | [diff] [blame] | 39 | int minor = cper_rand() % 128; |
| 40 | int major = cper_rand() % 128; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 41 | *version = int_to_bcd(minor); |
| 42 | *version |= int_to_bcd(major) << 8; |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 43 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 44 | //Fix values that could be above range. |
| 45 | UINT32 *port_type = (UINT32 *)(bytes + 8); |
Ed Tanous | 2d4d3b6 | 2025-03-11 10:34:29 -0700 | [diff] [blame] | 46 | *port_type = PCIE_PORT_TYPES[cper_rand() % |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 47 | (sizeof(PCIE_PORT_TYPES) / sizeof(int))]; |
| 48 | |
| 49 | //Set return values, exit. |
| 50 | *location = bytes; |
| 51 | return size; |
| 52 | } |