blob: 999739a38a8d878360e71c024f0947d830f48244 [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.
John Chungf8fc7052024-05-03 20:05:29 +080014size_t generate_section_pci_dev(void **location)
Lawrence Tangde9707f2022-07-19 10:54:31 +010015{
John Chungf8fc7052024-05-03 20:05:29 +080016 //Generate how many register pairs will be attached to this section.
17 UINT32 num_memory_pairs = rand() % 4;
18 UINT32 num_io_pairs = rand() % 4;
19 UINT32 num_registers = num_memory_pairs + num_io_pairs;
Lawrence Tangde9707f2022-07-19 10:54:31 +010020
John Chungf8fc7052024-05-03 20:05:29 +080021 //Create random bytes.
22 int size = 40 + (num_registers * 16);
23 UINT8 *bytes = generate_random_bytes(size);
Lawrence Tangde9707f2022-07-19 10:54:31 +010024
John Chungf8fc7052024-05-03 20:05:29 +080025 //Set reserved areas to zero.
26 UINT64 *validation = (UINT64 *)bytes;
27 *validation &= 0x1F; //Validation 5-63
28 for (int i = 0; i < 5; i++) {
29 *(bytes + 27 + i) = 0; //Bytes 11-15 of ID info.
30 }
Lawrence Tangde9707f2022-07-19 10:54:31 +010031
John Chungf8fc7052024-05-03 20:05:29 +080032 //Set expected values.
33 UINT32 *memory_number_field = (UINT32 *)(bytes + 32);
34 UINT32 *io_number_field = (UINT32 *)(bytes + 36);
35 *memory_number_field = num_memory_pairs;
36 *io_number_field = num_io_pairs;
37
38 //Fix error status.
39 create_valid_error_section(bytes + 8);
40
41 //Set return values, exit.
42 *location = bytes;
43 return size;
44}