Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 1 | /** |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 2 | * A user-space application for generating pseudo-random specification compliant CPER records. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 3 | * |
| 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdio.h> |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 8 | #include <stdlib.h> |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 9 | #include <string.h> |
| 10 | #include "../edk/Cper.h" |
| 11 | #include "cper-generate.h" |
| 12 | |
| 13 | void print_help(); |
| 14 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 15 | int main(int argc, char *argv[]) |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 16 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 17 | //If help requested, print help. |
| 18 | if (argc == 2 && strcmp(argv[1], "--help") == 0) { |
| 19 | print_help(); |
| 20 | return 0; |
| 21 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 22 | |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 23 | //Parse the command line arguments. |
| 24 | char *out_file = NULL; |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame^] | 25 | char *single_section = NULL; |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 26 | char **sections = NULL; |
| 27 | UINT16 num_sections = 0; |
| 28 | for (int i = 1; i < argc; i++) { |
| 29 | if (strcmp(argv[i], "--out") == 0 && i < argc - 1) { |
| 30 | out_file = argv[i + 1]; |
| 31 | i++; |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame^] | 32 | } else if (strcmp(argv[i], "--single-section") == 0 && |
| 33 | i < argc - 1) { |
| 34 | single_section = argv[i + 1]; |
| 35 | i++; |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 36 | } else if (strcmp(argv[i], "--sections") == 0 && i < argc - 1) { |
| 37 | //All arguments after this must be section names. |
| 38 | num_sections = argc - i - 1; |
| 39 | sections = malloc(sizeof(char *) * num_sections); |
| 40 | i++; |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame^] | 41 | |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 42 | for (int j = i; j < argc; j++) |
| 43 | sections[j - i] = argv[j]; |
| 44 | break; |
| 45 | } else { |
| 46 | printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n", |
| 47 | argv[i]); |
| 48 | return -1; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | //If no output file passed as argument, exit. |
| 53 | if (out_file == NULL) { |
| 54 | printf("No output file provided. For command information, refer to 'cper-generate --help'.\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 55 | return -1; |
| 56 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 57 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 58 | //Open a file handle to write output. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 59 | FILE *cper_file = fopen(out_file, "w"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 60 | if (cper_file == NULL) { |
| 61 | printf("Could not get a handle for output file '%s', file handle returned null.\n", |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 62 | out_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 63 | return -1; |
| 64 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 65 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame^] | 66 | //Which type are we generating? |
| 67 | if (single_section != NULL && sections == NULL) { |
| 68 | generate_single_section_record(single_section, cper_file); |
| 69 | } else if (sections != NULL && single_section == NULL) { |
| 70 | generate_cper_record(sections, num_sections, cper_file); |
| 71 | } else { |
| 72 | //Invalid arguments. |
| 73 | printf("Invalid argument. Either both '--sections' and '--single-section' were set, or neither. For command information, refer to 'cper-generate --help'.\n"); |
| 74 | return -1; |
| 75 | } |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 76 | |
| 77 | //Close & free remaining resources. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 78 | fclose(cper_file); |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 79 | if (sections != NULL) |
| 80 | free(sections); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 83 | //Prints command help for this CPER generator. |
| 84 | void print_help() |
| 85 | { |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame^] | 86 | printf(":: --out cper.file [--sections section1 ...] [--single-section sectiontype]\n"); |
| 87 | printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n\n"); |
| 88 | printf("\tWhen the '--sections' flag is set, all following arguments are section names, and a full CPER log is generated\n"); |
| 89 | printf("\tcontaining the given sections.\n"); |
| 90 | printf("\tWhen the '--single-section' flag is set, the next argument is the single section that should be generated, and\n"); |
| 91 | printf("\ta single section (no header, only a section descriptor & section) CPER file is generated.\n\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 92 | printf("\tValid section type names are the following:\n"); |
| 93 | printf("\t\t- generic\n"); |
| 94 | printf("\t\t- ia32x64\n"); |
| 95 | printf("\t\t- ipf\n"); |
| 96 | printf("\t\t- arm\n"); |
| 97 | printf("\t\t- memory\n"); |
| 98 | printf("\t\t- memory2\n"); |
| 99 | printf("\t\t- pcie\n"); |
| 100 | printf("\t\t- firmware\n"); |
| 101 | printf("\t\t- pcibus\n"); |
| 102 | printf("\t\t- pcidev\n"); |
| 103 | printf("\t\t- dmargeneric\n"); |
| 104 | printf("\t\t- dmarvtd\n"); |
| 105 | printf("\t\t- dmariommu\n"); |
| 106 | printf("\t\t- ccixper\n"); |
| 107 | printf("\t\t- cxlprotocol\n"); |
| 108 | printf("\t\t- cxlcomponent\n"); |
| 109 | printf("\t\t- unknown\n"); |
| 110 | printf("\n:: --help\n"); |
| 111 | printf("\tDisplays help information to the console.\n"); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 112 | } |