xref: /openbmc/libcper/generator/cper-generate-cli.c (revision 5f388a3ff9393b2f27531cff0617415f48224a00)
1d34f2b11SLawrence Tang /**
2efe17e2cSLawrence Tang  * A user-space application for generating pseudo-random specification compliant CPER records.
3d34f2b11SLawrence Tang  *
4d34f2b11SLawrence Tang  * Author: Lawrence.Tang@arm.com
5d34f2b11SLawrence Tang  **/
6d34f2b11SLawrence Tang 
7d34f2b11SLawrence Tang #include <stdio.h>
8*5f388a3fSLawrence Tang #include <stdlib.h>
9d34f2b11SLawrence Tang #include <string.h>
10d34f2b11SLawrence Tang #include "../edk/Cper.h"
11d34f2b11SLawrence Tang #include "cper-generate.h"
12d34f2b11SLawrence Tang 
13d34f2b11SLawrence Tang void print_help();
14d34f2b11SLawrence Tang 
15d34f2b11SLawrence Tang int main(int argc, char *argv[])
16d34f2b11SLawrence Tang {
17d34f2b11SLawrence Tang 	//If help requested, print help.
18e407b4c8SLawrence Tang 	if (argc == 2 && strcmp(argv[1], "--help") == 0) {
19d34f2b11SLawrence Tang 		print_help();
20d34f2b11SLawrence Tang 		return 0;
21d34f2b11SLawrence Tang 	}
22d34f2b11SLawrence Tang 
23*5f388a3fSLawrence Tang 	//Parse the command line arguments.
24*5f388a3fSLawrence Tang 	char *out_file = NULL;
25*5f388a3fSLawrence Tang 	char **sections = NULL;
26*5f388a3fSLawrence Tang 	UINT16 num_sections = 0;
27*5f388a3fSLawrence Tang 	for (int i = 1; i < argc; i++) {
28*5f388a3fSLawrence Tang 		if (strcmp(argv[i], "--out") == 0 && i < argc - 1) {
29*5f388a3fSLawrence Tang 			out_file = argv[i + 1];
30*5f388a3fSLawrence Tang 			i++;
31*5f388a3fSLawrence Tang 		} else if (strcmp(argv[i], "--sections") == 0 && i < argc - 1) {
32*5f388a3fSLawrence Tang 			//All arguments after this must be section names.
33*5f388a3fSLawrence Tang 			num_sections = argc - i - 1;
34*5f388a3fSLawrence Tang 			sections = malloc(sizeof(char *) * num_sections);
35*5f388a3fSLawrence Tang 			i++;
36*5f388a3fSLawrence Tang 
37*5f388a3fSLawrence Tang 			for (int j = i; j < argc; j++)
38*5f388a3fSLawrence Tang 				sections[j - i] = argv[j];
39*5f388a3fSLawrence Tang 			break;
40*5f388a3fSLawrence Tang 		} else {
41*5f388a3fSLawrence Tang 			printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n",
42*5f388a3fSLawrence Tang 			       argv[i]);
43*5f388a3fSLawrence Tang 			return -1;
44*5f388a3fSLawrence Tang 		}
45*5f388a3fSLawrence Tang 	}
46*5f388a3fSLawrence Tang 
47*5f388a3fSLawrence Tang 	//If no output file passed as argument, exit.
48*5f388a3fSLawrence Tang 	if (out_file == NULL) {
49*5f388a3fSLawrence Tang 		printf("No output file provided. For command information, refer to 'cper-generate --help'.\n");
50d34f2b11SLawrence Tang 		return -1;
51d34f2b11SLawrence Tang 	}
52d34f2b11SLawrence Tang 
53d34f2b11SLawrence Tang 	//Open a file handle to write output.
54*5f388a3fSLawrence Tang 	FILE *cper_file = fopen(out_file, "w");
55e407b4c8SLawrence Tang 	if (cper_file == NULL) {
56e407b4c8SLawrence Tang 		printf("Could not get a handle for output file '%s', file handle returned null.\n",
57*5f388a3fSLawrence Tang 		       out_file);
58d34f2b11SLawrence Tang 		return -1;
59d34f2b11SLawrence Tang 	}
60d34f2b11SLawrence Tang 
61d34f2b11SLawrence Tang 	//Generate the record. Type names start from argv[4].
62*5f388a3fSLawrence Tang 	generate_cper_record(sections, num_sections, cper_file);
63*5f388a3fSLawrence Tang 
64*5f388a3fSLawrence Tang 	//Close & free remaining resources.
65d34f2b11SLawrence Tang 	fclose(cper_file);
66*5f388a3fSLawrence Tang 	if (sections != NULL)
67*5f388a3fSLawrence Tang 		free(sections);
68d34f2b11SLawrence Tang }
69d34f2b11SLawrence Tang 
70d34f2b11SLawrence Tang //Prints command help for this CPER generator.
71d34f2b11SLawrence Tang void print_help()
72d34f2b11SLawrence Tang {
73d34f2b11SLawrence Tang 	printf(":: --out cper.file --sections section1 [section2 section3 ...]\n");
74efe17e2cSLawrence Tang 	printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n");
75d34f2b11SLawrence Tang 	printf("\tValid section type names are the following:\n");
76d34f2b11SLawrence Tang 	printf("\t\t- generic\n");
77d34f2b11SLawrence Tang 	printf("\t\t- ia32x64\n");
78d34f2b11SLawrence Tang 	printf("\t\t- ipf\n");
79d34f2b11SLawrence Tang 	printf("\t\t- arm\n");
80d34f2b11SLawrence Tang 	printf("\t\t- memory\n");
81d34f2b11SLawrence Tang 	printf("\t\t- memory2\n");
82d34f2b11SLawrence Tang 	printf("\t\t- pcie\n");
83d34f2b11SLawrence Tang 	printf("\t\t- firmware\n");
84d34f2b11SLawrence Tang 	printf("\t\t- pcibus\n");
85d34f2b11SLawrence Tang 	printf("\t\t- pcidev\n");
86d34f2b11SLawrence Tang 	printf("\t\t- dmargeneric\n");
87d34f2b11SLawrence Tang 	printf("\t\t- dmarvtd\n");
88d34f2b11SLawrence Tang 	printf("\t\t- dmariommu\n");
89d34f2b11SLawrence Tang 	printf("\t\t- ccixper\n");
90d34f2b11SLawrence Tang 	printf("\t\t- cxlprotocol\n");
91d34f2b11SLawrence Tang 	printf("\t\t- cxlcomponent\n");
92d34f2b11SLawrence Tang 	printf("\t\t- unknown\n");
93d34f2b11SLawrence Tang 	printf("\n:: --help\n");
94d34f2b11SLawrence Tang 	printf("\tDisplays help information to the console.\n");
95d34f2b11SLawrence Tang }