1 /**
2  * A user-space application for generating pseudo-random specification compliant CPER records.
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 "cper-generate.h"
12 
13 void print_help();
14 
15 int main(int argc, char *argv[])
16 {
17 	//If help requested, print help.
18 	if (argc == 2 && strcmp(argv[1], "--help") == 0) {
19 		print_help();
20 		return 0;
21 	}
22 
23 	//Parse the command line arguments.
24 	char *out_file = NULL;
25 	char *single_section = NULL;
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++;
32 		} else if (strcmp(argv[i], "--single-section") == 0 &&
33 			   i < argc - 1) {
34 			single_section = argv[i + 1];
35 			i++;
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++;
41 
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");
55 		return -1;
56 	}
57 
58 	//Open a file handle to write output.
59 	FILE *cper_file = fopen(out_file, "w");
60 	if (cper_file == NULL) {
61 		printf("Could not get a handle for output file '%s', file handle returned null.\n",
62 		       out_file);
63 		return -1;
64 	}
65 
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 	}
76 
77 	//Close & free remaining resources.
78 	fclose(cper_file);
79 	if (sections != NULL)
80 		free(sections);
81 }
82 
83 //Prints command help for this CPER generator.
84 void print_help()
85 {
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");
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");
112 }