blob: 6deac59e67563fa9b58c0ee4f54215bf2dfff79a [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 PCI component 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>
Thu Nguyene42fb482024-10-15 14:43:11 +00008#include <libcper/BaseTypes.h>
9#include <libcper/generator/gen-utils.h>
10#include <libcper/generator/sections/gen-section.h>
Lawrence Tangde9707f2022-07-19 10:54:31 +010011
Lawrence Tangefe17e22022-08-08 09:16:23 +010012//Generates a single pseudo-random PCI component error section, saving the resulting address to the given
Lawrence Tangde9707f2022-07-19 10:54:31 +010013//location. Returns the size of the newly created section.
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080014size_t generate_section_pci_dev(void **location,
15 GEN_VALID_BITS_TEST_TYPE validBitsType)
Lawrence Tangde9707f2022-07-19 10:54:31 +010016{
John Chungf8fc7052024-05-03 20:05:29 +080017 //Generate how many register pairs will be attached to this section.
18 UINT32 num_memory_pairs = rand() % 4;
19 UINT32 num_io_pairs = rand() % 4;
20 UINT32 num_registers = num_memory_pairs + num_io_pairs;
Lawrence Tangde9707f2022-07-19 10:54:31 +010021
John Chungf8fc7052024-05-03 20:05:29 +080022 //Create random bytes.
23 int size = 40 + (num_registers * 16);
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;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080028 *validation &= 0x1F; //Validation 5-63
29 if (validBitsType == ALL_VALID) {
30 *validation = 0x1F;
31 } else if (validBitsType == SOME_VALID) {
32 *validation = 0x15;
33 }
John Chungf8fc7052024-05-03 20:05:29 +080034 for (int i = 0; i < 5; i++) {
35 *(bytes + 27 + i) = 0; //Bytes 11-15 of ID info.
36 }
Lawrence Tangde9707f2022-07-19 10:54:31 +010037
John Chungf8fc7052024-05-03 20:05:29 +080038 //Set expected values.
39 UINT32 *memory_number_field = (UINT32 *)(bytes + 32);
40 UINT32 *io_number_field = (UINT32 *)(bytes + 36);
41 *memory_number_field = num_memory_pairs;
42 *io_number_field = num_io_pairs;
43
44 //Fix error status.
45 create_valid_error_section(bytes + 8);
46
47 //Set return values, exit.
48 *location = bytes;
49 return size;
50}