1*de9707f9SLawrence Tang /**
2*de9707f9SLawrence Tang  * Functions for generating psuedo-random CCIX PER error sections.
3*de9707f9SLawrence Tang  *
4*de9707f9SLawrence Tang  * Author: Lawrence.Tang@arm.com
5*de9707f9SLawrence Tang  **/
6*de9707f9SLawrence Tang 
7*de9707f9SLawrence Tang #include <stdlib.h>
8*de9707f9SLawrence Tang #include "../../edk/BaseTypes.h"
9*de9707f9SLawrence Tang #include "../gen-utils.h"
10*de9707f9SLawrence Tang #include "gen-sections.h"
11*de9707f9SLawrence Tang 
12*de9707f9SLawrence Tang //Generates a single psuedo-random CCIX PER error section, saving the resulting address to the given
13*de9707f9SLawrence Tang //location. Returns the size of the newly created section.
14*de9707f9SLawrence Tang size_t generate_section_ccix_per(void** location)
15*de9707f9SLawrence Tang {
16*de9707f9SLawrence Tang     //Create a random length for the CCIX PER log.
17*de9707f9SLawrence Tang     //The log attached here does not necessarily conform to the CCIX specification, and is simply random.
18*de9707f9SLawrence Tang     int log_len = (rand() % 5 + 1) * 32;
19*de9707f9SLawrence Tang 
20*de9707f9SLawrence Tang     //Create random bytes.
21*de9707f9SLawrence Tang     int size = 16 + log_len;
22*de9707f9SLawrence Tang     UINT8* bytes = generate_random_bytes(size);
23*de9707f9SLawrence Tang 
24*de9707f9SLawrence Tang     //Set reserved areas to zero.
25*de9707f9SLawrence Tang     UINT64* validation = (UINT64*)(bytes + 4);
26*de9707f9SLawrence Tang     *validation &= 0b111; //Validation bits 3-63.
27*de9707f9SLawrence Tang     *(bytes + 13) &= 0b11111; //CCIX port ID bits 5-7.
28*de9707f9SLawrence Tang     UINT16* reserved = (UINT16*)(bytes + 14);
29*de9707f9SLawrence Tang     *reserved = 0; //Reserved bytes 14-15.
30*de9707f9SLawrence Tang 
31*de9707f9SLawrence Tang     //Set return values, exit.
32*de9707f9SLawrence Tang     *location = bytes;
33*de9707f9SLawrence Tang     return size;
34*de9707f9SLawrence Tang }