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