blob: ed974f5747c86a07a9bef0422de45893d24868d3 [file] [log] [blame]
Lawrence Tangd34f2b12022-07-19 15:36:31 +01001/**
Lawrence Tangefe17e22022-08-08 09:16:23 +01002 * A user-space application for generating pseudo-random specification compliant CPER records.
Lawrence Tangd34f2b12022-07-19 15:36:31 +01003 *
4 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include <stdio.h>
Lawrence Tang5f388a32022-08-08 10:49:02 +01008#include <stdlib.h>
Lawrence Tangd34f2b12022-07-19 15:36:31 +01009#include <string.h>
10#include "../edk/Cper.h"
11#include "cper-generate.h"
12
13void print_help();
14
Lawrence Tange407b4c2022-07-21 13:54:01 +010015int main(int argc, char *argv[])
Lawrence Tangd34f2b12022-07-19 15:36:31 +010016{
Lawrence Tange407b4c2022-07-21 13:54:01 +010017 //If help requested, print help.
18 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
19 print_help();
20 return 0;
21 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010022
Lawrence Tang5f388a32022-08-08 10:49:02 +010023 //Parse the command line arguments.
24 char *out_file = NULL;
Lawrence Tang617949e2022-08-08 14:21:42 +010025 char *single_section = NULL;
Lawrence Tang5f388a32022-08-08 10:49:02 +010026 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 Tang617949e2022-08-08 14:21:42 +010032 } else if (strcmp(argv[i], "--single-section") == 0 &&
33 i < argc - 1) {
34 single_section = argv[i + 1];
35 i++;
Lawrence Tang5f388a32022-08-08 10:49:02 +010036 } 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 Tang617949e2022-08-08 14:21:42 +010041
Lawrence Tang5f388a32022-08-08 10:49:02 +010042 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 Tange407b4c2022-07-21 13:54:01 +010055 return -1;
56 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010057
Lawrence Tange407b4c2022-07-21 13:54:01 +010058 //Open a file handle to write output.
Lawrence Tang5f388a32022-08-08 10:49:02 +010059 FILE *cper_file = fopen(out_file, "w");
Lawrence Tange407b4c2022-07-21 13:54:01 +010060 if (cper_file == NULL) {
61 printf("Could not get a handle for output file '%s', file handle returned null.\n",
Lawrence Tang5f388a32022-08-08 10:49:02 +010062 out_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +010063 return -1;
64 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010065
Lawrence Tang617949e2022-08-08 14:21:42 +010066 //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 Tang5f388a32022-08-08 10:49:02 +010076
77 //Close & free remaining resources.
Lawrence Tange407b4c2022-07-21 13:54:01 +010078 fclose(cper_file);
Lawrence Tang5f388a32022-08-08 10:49:02 +010079 if (sections != NULL)
80 free(sections);
Lawrence Tangd34f2b12022-07-19 15:36:31 +010081}
82
Lawrence Tangd34f2b12022-07-19 15:36:31 +010083//Prints command help for this CPER generator.
84void print_help()
85{
Lawrence Tang617949e2022-08-08 14:21:42 +010086 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 Tange407b4c2022-07-21 13:54:01 +010092 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 Tangd34f2b12022-07-19 15:36:31 +0100112}