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> |
| 8 | #include "../../edk/BaseTypes.h" |
| 9 | #include "../gen-utils.h" |
Lawrence Tang | 8f97745 | 2022-08-24 14:55:07 +0100 | [diff] [blame] | 10 | #include "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. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 14 | size_t generate_section_generic(void **location) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 15 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 16 | //Create random bytes. |
| 17 | size_t size = generate_random_section(location, 192); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 18 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 19 | //Set reserved locations to zero. |
| 20 | UINT8 *start_byte = (UINT8 *)*location; |
| 21 | *((UINT64 *)start_byte) &= 0xFFF; |
| 22 | *(start_byte + 12) &= 0x7; |
| 23 | *((UINT16 *)(start_byte + 14)) = 0x0; |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 24 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 25 | //Ensure CPU brand string does not terminate early. |
| 26 | for (int i = 0; i < 128; i++) { |
| 27 | UINT8 *byte = start_byte + 24 + i; |
| 28 | if (*byte == 0x0) { |
| 29 | *byte = rand() % 127 + 1; |
| 30 | } |
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 | //Null terminate last byte. |
| 33 | if (i == 127) { |
| 34 | *byte = 0x0; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return size; |
| 39 | } |