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 **sections = NULL;
26 	UINT16 num_sections = 0;
27 	for (int i = 1; i < argc; i++) {
28 		if (strcmp(argv[i], "--out") == 0 && i < argc - 1) {
29 			out_file = argv[i + 1];
30 			i++;
31 		} else if (strcmp(argv[i], "--sections") == 0 && i < argc - 1) {
32 			//All arguments after this must be section names.
33 			num_sections = argc - i - 1;
34 			sections = malloc(sizeof(char *) * num_sections);
35 			i++;
36 
37 			for (int j = i; j < argc; j++)
38 				sections[j - i] = argv[j];
39 			break;
40 		} else {
41 			printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n",
42 			       argv[i]);
43 			return -1;
44 		}
45 	}
46 
47 	//If no output file passed as argument, exit.
48 	if (out_file == NULL) {
49 		printf("No output file provided. For command information, refer to 'cper-generate --help'.\n");
50 		return -1;
51 	}
52 
53 	//Open a file handle to write output.
54 	FILE *cper_file = fopen(out_file, "w");
55 	if (cper_file == NULL) {
56 		printf("Could not get a handle for output file '%s', file handle returned null.\n",
57 		       out_file);
58 		return -1;
59 	}
60 
61 	//Generate the record. Type names start from argv[4].
62 	generate_cper_record(sections, num_sections, cper_file);
63 
64 	//Close & free remaining resources.
65 	fclose(cper_file);
66 	if (sections != NULL)
67 		free(sections);
68 }
69 
70 //Prints command help for this CPER generator.
71 void print_help()
72 {
73 	printf(":: --out cper.file --sections section1 [section2 section3 ...]\n");
74 	printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n");
75 	printf("\tValid section type names are the following:\n");
76 	printf("\t\t- generic\n");
77 	printf("\t\t- ia32x64\n");
78 	printf("\t\t- ipf\n");
79 	printf("\t\t- arm\n");
80 	printf("\t\t- memory\n");
81 	printf("\t\t- memory2\n");
82 	printf("\t\t- pcie\n");
83 	printf("\t\t- firmware\n");
84 	printf("\t\t- pcibus\n");
85 	printf("\t\t- pcidev\n");
86 	printf("\t\t- dmargeneric\n");
87 	printf("\t\t- dmarvtd\n");
88 	printf("\t\t- dmariommu\n");
89 	printf("\t\t- ccixper\n");
90 	printf("\t\t- cxlprotocol\n");
91 	printf("\t\t- cxlcomponent\n");
92 	printf("\t\t- unknown\n");
93 	printf("\n:: --help\n");
94 	printf("\tDisplays help information to the console.\n");
95 }