xref: /openbmc/libcper/generator/gen-utils.c (revision fedd457d)
1 /**
2  * Utility functions to assist in generating pseudo-random CPER sections.
3  *
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.
generate_random_section(void ** location,size_t size)13 size_t generate_random_section(void **location, size_t size)
14 {
15 	*location = generate_random_bytes(size);
16 	return size;
17 }
18 
19 //Generates a random byte allocation of the given size.
generate_random_bytes(size_t size)20 UINT8 *generate_random_bytes(size_t size)
21 {
22 	UINT8 *bytes = malloc(size);
23 	for (size_t i = 0; i < size; i++) {
24 		bytes[i] = rand();
25 	}
26 	return bytes;
27 }
28 
29 //Creates a valid common CPER error section, given the start of the error section.
30 //Clears reserved bits.
create_valid_error_section(UINT8 * start)31 void create_valid_error_section(UINT8 *start)
32 {
33 	//Fix reserved bits.
34 	UINT64 *error_section = (UINT64 *)start;
35 	*error_section &= ~0xFF;    //Reserved bits 0-7.
36 	*error_section &= 0x7FFFFF; //Reserved bits 23-63
37 
38 	//Ensure error type has a valid value.
39 	*(start + 1) =
40 		CPER_ERROR_TYPES_KEYS[rand() % (sizeof(CPER_ERROR_TYPES_KEYS) /
41 						sizeof(int))];
42 }
43 
44 //Initializes the random seed for rand() using the current time.
init_random()45 void init_random()
46 {
47 	srand((unsigned int)time(NULL));
48 }
49