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 | * Functions for generating pseudo-random CPER generic processor sections. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 3 | * |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdlib.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 8 | #include <libcper/BaseTypes.h> |
| 9 | #include <libcper/generator/gen-utils.h> |
| 10 | #include <libcper/generator/sections/gen-section.h> |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 11 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 12 | //Generates a single pseudo-random generic processor section, saving the resulting address to the given |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 13 | //location. Returns the size of the newly created section. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 14 | size_t generate_section_generic(void **location, |
| 15 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 16 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 17 | //Create random bytes. |
| 18 | size_t size = generate_random_section(location, 192); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 19 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 20 | //Set reserved locations to zero. |
| 21 | UINT8 *start_byte = (UINT8 *)*location; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 22 | UINT64 *validation = (UINT64 *)*location; |
| 23 | *validation &= 0x1FFF; |
| 24 | if (validBitsType == ALL_VALID) { |
| 25 | *validation = 0x1FFF; |
| 26 | } else if (validBitsType == SOME_VALID) { |
| 27 | *validation = 0x1555; |
| 28 | } |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 29 | *(start_byte + 12) &= 0x7; |
| 30 | *((UINT16 *)(start_byte + 14)) = 0x0; |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 31 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 32 | //Ensure CPU brand string does not terminate early. |
| 33 | for (int i = 0; i < 128; i++) { |
| 34 | UINT8 *byte = start_byte + 24 + i; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 35 | //Ensure only ascii is used |
Aushim Nagarkatti | a6f070b | 2025-02-04 11:06:43 -0800 | [diff] [blame] | 36 | *byte = rand() % 127 + 1; |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 37 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 38 | //Null terminate last byte. |
| 39 | if (i == 127) { |
| 40 | *byte = 0x0; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return size; |
| 45 | } |