Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 1 | /** |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 2 | * Describes functions for generating pseudo-random specification compliant CPER records. |
| 3 | * |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 10 | #include <libcper/Cper.h> |
| 11 | #include <libcper/generator/gen-utils.h> |
| 12 | #include <libcper/generator/sections/gen-section.h> |
| 13 | #include <libcper/generator/cper-generate.h> |
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 | EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type, |
Ed Tanous | b35d957 | 2024-06-18 13:17:22 -0700 | [diff] [blame] | 16 | const size_t *lengths, |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 17 | int index, |
| 18 | int num_sections); |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 19 | size_t generate_section(void **location, char *type, |
| 20 | GEN_VALID_BITS_TEST_TYPE validBitsType); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 21 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 22 | //Generates a CPER record with the given section types, outputting to the given stream. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 23 | void generate_cper_record(char **types, UINT16 num_sections, FILE *out, |
| 24 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 25 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 26 | //Initialise randomiser. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 27 | init_random(0); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 28 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 29 | //Generate the sections. |
| 30 | void *sections[num_sections]; |
| 31 | size_t section_lengths[num_sections]; |
| 32 | for (int i = 0; i < num_sections; i++) { |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 33 | section_lengths[i] = |
| 34 | generate_section(sections + i, types[i], validBitsType); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 35 | if (section_lengths[i] == 0) { |
| 36 | //Error encountered, exit. |
| 37 | printf("Error encountered generating section %d of type '%s', length returned zero.\n", |
| 38 | i + 1, types[i]); |
| 39 | return; |
| 40 | } |
| 41 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 42 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 43 | //Generate the header given the number of sections. |
| 44 | EFI_COMMON_ERROR_RECORD_HEADER *header = |
| 45 | (EFI_COMMON_ERROR_RECORD_HEADER *)calloc( |
| 46 | 1, sizeof(EFI_COMMON_ERROR_RECORD_HEADER)); |
| 47 | header->SignatureStart = 0x52455043; //CPER |
| 48 | header->SectionCount = num_sections; |
| 49 | header->SignatureEnd = 0xFFFFFFFF; |
| 50 | header->Flags = 4; //HW_ERROR_FLAGS_SIMULATED |
| 51 | header->RecordID = (UINT64)rand(); |
| 52 | header->ErrorSeverity = rand() % 4; |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 53 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 54 | //Generate a valid timestamp. |
| 55 | header->TimeStamp.Century = int_to_bcd(rand() % 100); |
| 56 | header->TimeStamp.Year = int_to_bcd(rand() % 100); |
| 57 | header->TimeStamp.Month = int_to_bcd(rand() % 12 + 1); |
| 58 | header->TimeStamp.Day = int_to_bcd(rand() % 31 + 1); |
| 59 | header->TimeStamp.Hours = int_to_bcd(rand() % 24 + 1); |
| 60 | header->TimeStamp.Seconds = int_to_bcd(rand() % 60); |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 61 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 62 | //Turn all validation bits on. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 63 | header->ValidationBits = 0x3; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 64 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 65 | //Generate the section descriptors given the number of sections. |
| 66 | EFI_ERROR_SECTION_DESCRIPTOR *section_descriptors[num_sections]; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 67 | for (int i = 0; i < num_sections; i++) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 68 | section_descriptors[i] = generate_section_descriptor( |
| 69 | types[i], section_lengths, i, num_sections); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 70 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 71 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 72 | //Calculate total length of structure, set in header. |
| 73 | size_t total_len = sizeof(EFI_COMMON_ERROR_RECORD_HEADER); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 74 | for (int i = 0; i < num_sections; i++) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 75 | total_len += section_lengths[i]; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 76 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 77 | total_len += num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR); |
| 78 | header->RecordLength = (UINT32)total_len; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 79 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 80 | //Write to stream in order, free all resources. |
| 81 | fwrite(header, sizeof(EFI_COMMON_ERROR_RECORD_HEADER), 1, out); |
| 82 | fflush(out); |
| 83 | free(header); |
| 84 | for (int i = 0; i < num_sections; i++) { |
| 85 | fwrite(section_descriptors[i], |
| 86 | sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, out); |
| 87 | fflush(out); |
| 88 | free(section_descriptors[i]); |
| 89 | } |
| 90 | for (int i = 0; i < num_sections; i++) { |
| 91 | fwrite(sections[i], section_lengths[i], 1, out); |
| 92 | fflush(out); |
| 93 | free(sections[i]); |
| 94 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 97 | //Generates a single section record for the given section, and outputs to file. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 98 | void generate_single_section_record(char *type, FILE *out, |
| 99 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 100 | { |
| 101 | //Generate a section. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 102 | void *section = NULL; |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 103 | size_t section_len = generate_section(§ion, type, validBitsType); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 104 | |
| 105 | //Generate a descriptor, correct the offset. |
| 106 | EFI_ERROR_SECTION_DESCRIPTOR *section_descriptor = |
| 107 | generate_section_descriptor(type, §ion_len, 0, 1); |
| 108 | section_descriptor->SectionOffset = |
| 109 | sizeof(EFI_ERROR_SECTION_DESCRIPTOR); |
| 110 | |
| 111 | //Write all to file. |
| 112 | fwrite(section_descriptor, sizeof(EFI_ERROR_SECTION_DESCRIPTOR), 1, |
| 113 | out); |
| 114 | fwrite(section, section_len, 1, out); |
| 115 | fflush(out); |
| 116 | |
| 117 | //Free remaining resources. |
| 118 | free(section_descriptor); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 119 | free(section); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 122 | //Generates a single section descriptor for a section with the given properties. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 123 | EFI_ERROR_SECTION_DESCRIPTOR *generate_section_descriptor(char *type, |
Ed Tanous | b35d957 | 2024-06-18 13:17:22 -0700 | [diff] [blame] | 124 | const size_t *lengths, |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 125 | int index, |
| 126 | int num_sections) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 127 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 128 | EFI_ERROR_SECTION_DESCRIPTOR *descriptor = |
| 129 | (EFI_ERROR_SECTION_DESCRIPTOR *)generate_random_bytes( |
| 130 | sizeof(EFI_ERROR_SECTION_DESCRIPTOR)); |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 131 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 132 | //Set reserved bits to zero. |
| 133 | descriptor->Resv1 = 0; |
| 134 | descriptor->SectionFlags &= 0xFF; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 135 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 136 | //Validation bits all set to 'on'. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 137 | descriptor->SecValidMask = 0x3; |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 138 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 139 | //Set severity. |
| 140 | descriptor->Severity = rand() % 4; |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 141 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 142 | //Set length, offset from base record. |
| 143 | descriptor->SectionLength = (UINT32)lengths[index]; |
| 144 | descriptor->SectionOffset = |
| 145 | sizeof(EFI_COMMON_ERROR_RECORD_HEADER) + |
| 146 | (num_sections * sizeof(EFI_ERROR_SECTION_DESCRIPTOR)); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 147 | for (int i = 0; i < index; i++) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 148 | descriptor->SectionOffset += lengths[i]; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 149 | } |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 150 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 151 | //Ensure the FRU text is not null terminated early. |
| 152 | for (int i = 0; i < 20; i++) { |
Aushim Nagarkatti | a6f070b | 2025-02-04 11:06:43 -0800 | [diff] [blame] | 153 | // FRU string can only be ASCII |
| 154 | descriptor->FruString[i] = rand() % 127 + 1; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 155 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 156 | //Null terminate last byte. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 157 | if (i == 19) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 158 | descriptor->FruString[i] = 0x0; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 159 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 160 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 161 | |
Lawrence Tang | 8f97745 | 2022-08-24 14:55:07 +0100 | [diff] [blame] | 162 | //If section type is not "unknown", set section type GUID based on type name. |
| 163 | int section_guid_found = 0; |
| 164 | if (strcmp(type, "unknown") == 0) { |
| 165 | section_guid_found = 1; |
| 166 | } else { |
| 167 | //Find the appropriate GUID for this section name. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 168 | for (size_t i = 0; i < generator_definitions_len; i++) { |
Lawrence Tang | 8f97745 | 2022-08-24 14:55:07 +0100 | [diff] [blame] | 169 | if (strcmp(type, generator_definitions[i].ShortName) == |
| 170 | 0) { |
| 171 | memcpy(&descriptor->SectionType, |
| 172 | generator_definitions[i].Guid, |
| 173 | sizeof(EFI_GUID)); |
| 174 | section_guid_found = 1; |
| 175 | break; |
| 176 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 177 | } |
Lawrence Tang | 8f97745 | 2022-08-24 14:55:07 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | //Undefined section, show error. |
| 181 | if (!section_guid_found) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 182 | //Undefined section, show error. |
| 183 | printf("Undefined section type '%s' provided. See 'cper-generate --help' for command information.\n", |
| 184 | type); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | return descriptor; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | //Generates a single CPER section given the string type. |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 192 | size_t generate_section(void **location, char *type, |
| 193 | GEN_VALID_BITS_TEST_TYPE validBitsType) |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 194 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 195 | //The length of the section. |
| 196 | size_t length = 0; |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 197 | |
Lawrence Tang | 8f97745 | 2022-08-24 14:55:07 +0100 | [diff] [blame] | 198 | //If the section name is "unknown", simply generate a random bytes section. |
| 199 | int section_generated = 0; |
| 200 | if (strcmp(type, "unknown") == 0) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 201 | length = generate_random_section(location, rand() % 256); |
Lawrence Tang | 8f97745 | 2022-08-24 14:55:07 +0100 | [diff] [blame] | 202 | section_generated = 1; |
| 203 | } else { |
| 204 | //Function defined section, switch on the type, generate accordingly. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 205 | for (size_t i = 0; i < generator_definitions_len; i++) { |
Lawrence Tang | 8f97745 | 2022-08-24 14:55:07 +0100 | [diff] [blame] | 206 | if (strcmp(type, generator_definitions[i].ShortName) == |
| 207 | 0) { |
| 208 | length = generator_definitions[i].Generate( |
Aushim Nagarkatti | ae8f6d9 | 2025-01-29 17:34:44 -0800 | [diff] [blame^] | 209 | location, validBitsType); |
Lawrence Tang | 8f97745 | 2022-08-24 14:55:07 +0100 | [diff] [blame] | 210 | section_generated = 1; |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | //If we didn't find a section generator for the given name, error out. |
| 217 | if (!section_generated) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 218 | printf("Undefined section type '%s' given to generate. See 'cper-generate --help' for command information.\n", |
| 219 | type); |
| 220 | return 0; |
| 221 | } |
Lawrence Tang | 02c801a | 2022-07-18 14:43:52 +0100 | [diff] [blame] | 222 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 223 | return length; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 224 | } |