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>
85f388a3fSLawrence Tang #include <stdlib.h>
9d34f2b11SLawrence Tang #include <string.h>
10d34f2b11SLawrence Tang #include "../edk/Cper.h"
11d34f2b11SLawrence Tang #include "cper-generate.h"
12ef9a325fSLawrence Tang #include "sections/gen-section.h"
13d34f2b11SLawrence Tang 
14d34f2b11SLawrence Tang void print_help();
15d34f2b11SLawrence Tang 
main(int argc,char * argv[])16d34f2b11SLawrence Tang int main(int argc, char *argv[])
17d34f2b11SLawrence Tang {
18d34f2b11SLawrence Tang 	//If help requested, print help.
19e407b4c8SLawrence Tang 	if (argc == 2 && strcmp(argv[1], "--help") == 0) {
20d34f2b11SLawrence Tang 		print_help();
21d34f2b11SLawrence Tang 		return 0;
22d34f2b11SLawrence Tang 	}
23d34f2b11SLawrence Tang 
245f388a3fSLawrence Tang 	//Parse the command line arguments.
255f388a3fSLawrence Tang 	char *out_file = NULL;
26617949e4SLawrence Tang 	char *single_section = NULL;
275f388a3fSLawrence Tang 	char **sections = NULL;
285f388a3fSLawrence Tang 	UINT16 num_sections = 0;
295f388a3fSLawrence Tang 	for (int i = 1; i < argc; i++) {
305f388a3fSLawrence Tang 		if (strcmp(argv[i], "--out") == 0 && i < argc - 1) {
315f388a3fSLawrence Tang 			out_file = argv[i + 1];
325f388a3fSLawrence Tang 			i++;
33617949e4SLawrence Tang 		} else if (strcmp(argv[i], "--single-section") == 0 &&
34617949e4SLawrence Tang 			   i < argc - 1) {
35617949e4SLawrence Tang 			single_section = argv[i + 1];
36617949e4SLawrence Tang 			i++;
375f388a3fSLawrence Tang 		} else if (strcmp(argv[i], "--sections") == 0 && i < argc - 1) {
385f388a3fSLawrence Tang 			//All arguments after this must be section names.
395f388a3fSLawrence Tang 			num_sections = argc - i - 1;
405f388a3fSLawrence Tang 			sections = malloc(sizeof(char *) * num_sections);
415f388a3fSLawrence Tang 			i++;
425f388a3fSLawrence Tang 
43*f8fc7052SJohn Chung 			for (int j = i; j < argc; j++) {
445f388a3fSLawrence Tang 				sections[j - i] = argv[j];
45*f8fc7052SJohn Chung 			}
465f388a3fSLawrence Tang 			break;
475f388a3fSLawrence Tang 		} else {
485f388a3fSLawrence Tang 			printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n",
495f388a3fSLawrence Tang 			       argv[i]);
505f388a3fSLawrence Tang 			return -1;
515f388a3fSLawrence Tang 		}
525f388a3fSLawrence Tang 	}
535f388a3fSLawrence Tang 
545f388a3fSLawrence Tang 	//If no output file passed as argument, exit.
555f388a3fSLawrence Tang 	if (out_file == NULL) {
565f388a3fSLawrence Tang 		printf("No output file provided. For command information, refer to 'cper-generate --help'.\n");
57*f8fc7052SJohn Chung 		if (sections) {
58*f8fc7052SJohn Chung 			free(sections);
59*f8fc7052SJohn Chung 		}
60d34f2b11SLawrence Tang 		return -1;
61d34f2b11SLawrence Tang 	}
62d34f2b11SLawrence Tang 
63d34f2b11SLawrence Tang 	//Open a file handle to write output.
645f388a3fSLawrence Tang 	FILE *cper_file = fopen(out_file, "w");
65e407b4c8SLawrence Tang 	if (cper_file == NULL) {
66e407b4c8SLawrence Tang 		printf("Could not get a handle for output file '%s', file handle returned null.\n",
675f388a3fSLawrence Tang 		       out_file);
68*f8fc7052SJohn Chung 		if (sections) {
69*f8fc7052SJohn Chung 			free(sections);
70*f8fc7052SJohn Chung 		}
71d34f2b11SLawrence Tang 		return -1;
72d34f2b11SLawrence Tang 	}
73d34f2b11SLawrence Tang 
74617949e4SLawrence Tang 	//Which type are we generating?
75617949e4SLawrence Tang 	if (single_section != NULL && sections == NULL) {
76617949e4SLawrence Tang 		generate_single_section_record(single_section, cper_file);
77617949e4SLawrence Tang 	} else if (sections != NULL && single_section == NULL) {
785f388a3fSLawrence Tang 		generate_cper_record(sections, num_sections, cper_file);
79617949e4SLawrence Tang 	} else {
80617949e4SLawrence Tang 		//Invalid arguments.
81617949e4SLawrence Tang 		printf("Invalid argument. Either both '--sections' and '--single-section' were set, or neither. For command information, refer to 'cper-generate --help'.\n");
82*f8fc7052SJohn Chung 		if (sections) {
83*f8fc7052SJohn Chung 			free(sections);
84*f8fc7052SJohn Chung 		}
85617949e4SLawrence Tang 		return -1;
86617949e4SLawrence Tang 	}
875f388a3fSLawrence Tang 
885f388a3fSLawrence Tang 	//Close & free remaining resources.
89d34f2b11SLawrence Tang 	fclose(cper_file);
90*f8fc7052SJohn Chung 	if (sections != NULL) {
915f388a3fSLawrence Tang 		free(sections);
92d34f2b11SLawrence Tang 	}
93*f8fc7052SJohn Chung }
94d34f2b11SLawrence Tang 
95d34f2b11SLawrence Tang //Prints command help for this CPER generator.
print_help()96d34f2b11SLawrence Tang void print_help()
97d34f2b11SLawrence Tang {
98617949e4SLawrence Tang 	printf(":: --out cper.file [--sections section1 ...] [--single-section sectiontype]\n");
99617949e4SLawrence Tang 	printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n\n");
100617949e4SLawrence Tang 	printf("\tWhen the '--sections' flag is set, all following arguments are section names, and a full CPER log is generated\n");
101617949e4SLawrence Tang 	printf("\tcontaining the given sections.\n");
102617949e4SLawrence Tang 	printf("\tWhen the '--single-section' flag is set, the next argument is the single section that should be generated, and\n");
103617949e4SLawrence Tang 	printf("\ta single section (no header, only a section descriptor & section) CPER file is generated.\n\n");
104d34f2b11SLawrence Tang 	printf("\tValid section type names are the following:\n");
105*f8fc7052SJohn Chung 	for (size_t i = 0; i < generator_definitions_len; i++) {
106ef9a325fSLawrence Tang 		printf("\t\t- %s\n", generator_definitions[i].ShortName);
107ef9a325fSLawrence Tang 	}
108d34f2b11SLawrence Tang 	printf("\t\t- unknown\n");
109d34f2b11SLawrence Tang 	printf("\n:: --help\n");
110d34f2b11SLawrence Tang 	printf("\tDisplays help information to the console.\n");
111d34f2b11SLawrence Tang }
112