Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 1 | /** |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 2 | * Functions for generating pseudo-random CPER platform memory error sections. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 3 | * |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +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 | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 11 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 12 | //Generates a single pseudo-random platform memory error section, saving the resulting address to the given |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +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_memory(void **location, |
| 15 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 16 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 17 | //Create random bytes. |
| 18 | int size = 80; |
| 19 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 20 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 21 | //Set reserved areas to zero. |
| 22 | UINT64 *validation = (UINT64 *)bytes; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 23 | //Validation 22-63 reserved. 19/20=0 for bank |
| 24 | *validation &= 0x27FFFF; |
| 25 | if (validBitsType == ALL_VALID) { |
| 26 | *validation = 0x27FFFF; |
| 27 | } else if (validBitsType == SOME_VALID) { |
| 28 | *validation = 0x275555; |
| 29 | } |
| 30 | *(bytes + 73) &= ~0x1C; //Extended bits 2-4 |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 31 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 32 | //Fix values that could be above range. |
| 33 | *(bytes + 72) = rand() % 16; //Memory error type |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 34 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 35 | //Fix error status. |
| 36 | create_valid_error_section(bytes + 8); |
| 37 | |
| 38 | //Set return values, exit. |
| 39 | *location = bytes; |
| 40 | return size; |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 43 | //Generates a single pseudo-random memory 2 error section, saving the resulting address to the given |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 44 | //location. Returns the size of the newly created section. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 45 | size_t generate_section_memory2(void **location, |
| 46 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 47 | { |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 48 | //Create random bytes. |
| 49 | int size = 96; |
| 50 | UINT8 *bytes = generate_random_bytes(size); |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 51 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 52 | //Set reserved areas to zero. |
| 53 | UINT64 *validation = (UINT64 *)bytes; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 54 | //Validation 22-63, 20/21 is 0 since 6 is valid |
| 55 | *validation &= 0xFFFFF; |
| 56 | if (validBitsType == ALL_VALID) { |
| 57 | *validation = 0xFFFFF; |
| 58 | } else if (validBitsType == SOME_VALID) { |
| 59 | *validation = 0x55555; |
| 60 | } |
| 61 | *(bytes + 63) = 0; //Reserved byte 63 |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 62 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 63 | //Fix values that could be above range. |
| 64 | *(bytes + 61) = rand() % 16; //Memory error type |
| 65 | *(bytes + 62) = rand() % 2; //Status |
Lawrence Tang | de9707f | 2022-07-19 10:54:31 +0100 | [diff] [blame] | 66 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 67 | //Fix error status. |
| 68 | create_valid_error_section(bytes + 8); |
| 69 | |
| 70 | //Set return values, exit. |
| 71 | *location = bytes; |
| 72 | return size; |
| 73 | } |