Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 1 | /** |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 2 | * Utility functions to assist in generating pseudo-random CPER sections. |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 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. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 13 | size_t generate_random_section(void **location, size_t size) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 14 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 15 | *location = generate_random_bytes(size); |
| 16 | return size; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | //Generates a random byte allocation of the given size. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 20 | UINT8 *generate_random_bytes(size_t size) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 21 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 22 | UINT8 *bytes = malloc(size); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame^] | 23 | for (size_t i = 0; i < size; i++) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 24 | bytes[i] = rand(); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame^] | 25 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 26 | return bytes; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 29 | //Creates a valid common CPER error section, given the start of the error section. |
| 30 | //Clears reserved bits. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 31 | void create_valid_error_section(UINT8 *start) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 32 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 33 | //Fix reserved bits. |
| 34 | UINT64 *error_section = (UINT64 *)start; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame^] | 35 | *error_section &= ~0xFF; //Reserved bits 0-7. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 36 | *error_section &= 0x7FFFFF; //Reserved bits 23-63 |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 37 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 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))]; |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 44 | //Initializes the random seed for rand() using the current time. |
| 45 | void init_random() |
| 46 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 47 | srand((unsigned int)time(NULL)); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame^] | 48 | } |