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