blob: c986b09bd63a9dbf98c5396f65da4bb09404605f [file] [log] [blame]
Lawrence Tang02c801a2022-07-18 14:43:52 +01001/**
Lawrence Tangefe17e22022-08-08 09:16:23 +01002 * Utility functions to assist in generating pseudo-random CPER sections.
Lawrence Tang02c801a2022-07-18 14:43:52 +01003 *
4 * Author: Lawrence.Tang@arm.com
5 **/
6#include <stdlib.h>
7#include <time.h>
8#include "../edk/BaseTypes.h"
9#include "gen-utils.h"
10
11//Generates a random section of the given byte size, saving the result to the given location.
12//Returns the length of the section as passed in.
Lawrence Tange407b4c2022-07-21 13:54:01 +010013size_t generate_random_section(void **location, size_t size)
Lawrence Tang02c801a2022-07-18 14:43:52 +010014{
Lawrence Tange407b4c2022-07-21 13:54:01 +010015 *location = generate_random_bytes(size);
16 return size;
Lawrence Tang02c801a2022-07-18 14:43:52 +010017}
18
19//Generates a random byte allocation of the given size.
Lawrence Tange407b4c2022-07-21 13:54:01 +010020UINT8 *generate_random_bytes(size_t size)
Lawrence Tang02c801a2022-07-18 14:43:52 +010021{
Lawrence Tange407b4c2022-07-21 13:54:01 +010022 UINT8 *bytes = malloc(size);
23 for (size_t i = 0; i < size; i++)
24 bytes[i] = rand();
25 return bytes;
Lawrence Tang02c801a2022-07-18 14:43:52 +010026}
27
Lawrence Tangde9707f2022-07-19 10:54:31 +010028//Creates a valid common CPER error section, given the start of the error section.
29//Clears reserved bits.
Lawrence Tange407b4c2022-07-21 13:54:01 +010030void create_valid_error_section(UINT8 *start)
Lawrence Tangde9707f2022-07-19 10:54:31 +010031{
Lawrence Tange407b4c2022-07-21 13:54:01 +010032 //Fix reserved bits.
33 UINT64 *error_section = (UINT64 *)start;
34 *error_section &= ~0xFF; //Reserved bits 0-7.
35 *error_section &= 0x7FFFFF; //Reserved bits 23-63
Lawrence Tangde9707f2022-07-19 10:54:31 +010036
Lawrence Tange407b4c2022-07-21 13:54:01 +010037 //Ensure error type has a valid value.
38 *(start + 1) =
39 CPER_ERROR_TYPES_KEYS[rand() % (sizeof(CPER_ERROR_TYPES_KEYS) /
40 sizeof(int))];
Lawrence Tangde9707f2022-07-19 10:54:31 +010041}
42
Lawrence Tang02c801a2022-07-18 14:43:52 +010043//Initializes the random seed for rand() using the current time.
44void init_random()
45{
Lawrence Tange407b4c2022-07-21 13:54:01 +010046 srand((unsigned int)time(NULL));
Lawrence Tang02c801a2022-07-18 14:43:52 +010047}