xref: /openbmc/libcper/cli-app/cper-convert.c (revision e407b4c8)
102c801a5SLawrence Tang /**
202c801a5SLawrence Tang  * A user-space application linking to the CPER-JSON conversion library which allows for easy
302c801a5SLawrence Tang  * conversion between CPER and CPER-JSON formats.
402c801a5SLawrence Tang  *
502c801a5SLawrence Tang  * Author: Lawrence.Tang@arm.com
602c801a5SLawrence Tang  **/
702c801a5SLawrence Tang 
802c801a5SLawrence Tang #include <stdio.h>
902c801a5SLawrence Tang #include <string.h>
1002c801a5SLawrence Tang #include <libgen.h>
1102c801a5SLawrence Tang #include <limits.h>
1202c801a5SLawrence Tang #include "json.h"
1302c801a5SLawrence Tang #include "../cper-parse.h"
1402c801a5SLawrence Tang #include "../json-schema.h"
1502c801a5SLawrence Tang 
1602c801a5SLawrence Tang void cper_to_json(int argc, char *argv[]);
1702c801a5SLawrence Tang void json_to_cper(int argc, char *argv[]);
1802c801a5SLawrence Tang void print_help(void);
1902c801a5SLawrence Tang 
2002c801a5SLawrence Tang int main(int argc, char *argv[])
2102c801a5SLawrence Tang {
2202c801a5SLawrence Tang 	//Ensure at least one argument is present.
23*e407b4c8SLawrence Tang 	if (argc < 2) {
2402c801a5SLawrence Tang 		printf("Invalid number of arguments. See 'cper-convert --help' for command information.\n");
2502c801a5SLawrence Tang 		return -1;
2602c801a5SLawrence Tang 	}
2702c801a5SLawrence Tang 
2802c801a5SLawrence Tang 	//Run the requested command.
2902c801a5SLawrence Tang 	if (strcmp(argv[1], "to-json") == 0)
3002c801a5SLawrence Tang 		cper_to_json(argc, argv);
3102c801a5SLawrence Tang 	else if (strcmp(argv[1], "to-cper") == 0)
3202c801a5SLawrence Tang 		json_to_cper(argc, argv);
3302c801a5SLawrence Tang 	else if (strcmp(argv[1], "--help") == 0)
3402c801a5SLawrence Tang 		print_help();
35*e407b4c8SLawrence Tang 	else {
36*e407b4c8SLawrence Tang 		printf("Unrecognised argument '%s'. See 'cper-convert --help' for command information.\n",
37*e407b4c8SLawrence Tang 		       argv[1]);
3802c801a5SLawrence Tang 		return -1;
3902c801a5SLawrence Tang 	}
4002c801a5SLawrence Tang 
4102c801a5SLawrence Tang 	return 0;
4202c801a5SLawrence Tang }
4302c801a5SLawrence Tang 
4402c801a5SLawrence Tang //Command for converting a provided CPER log file into JSON.
4502c801a5SLawrence Tang void cper_to_json(int argc, char *argv[])
4602c801a5SLawrence Tang {
47*e407b4c8SLawrence Tang 	if (argc < 3) {
4802c801a5SLawrence Tang 		printf("Insufficient number of arguments for 'to-json'. See 'cper-convert --help' for command information.\n");
4902c801a5SLawrence Tang 		return;
5002c801a5SLawrence Tang 	}
5102c801a5SLawrence Tang 
5202c801a5SLawrence Tang 	//Get a handle for the log file.
5302c801a5SLawrence Tang 	FILE *cper_file = fopen(argv[2], "r");
54*e407b4c8SLawrence Tang 	if (cper_file == NULL) {
55*e407b4c8SLawrence Tang 		printf("Could not open provided CPER file '%s', file handle returned null.\n",
56*e407b4c8SLawrence Tang 		       argv[2]);
5702c801a5SLawrence Tang 		return;
5802c801a5SLawrence Tang 	}
5902c801a5SLawrence Tang 
6002c801a5SLawrence Tang 	//Convert.
6102c801a5SLawrence Tang 	json_object *ir = cper_to_ir(cper_file);
6202c801a5SLawrence Tang 	fclose(cper_file);
63*e407b4c8SLawrence Tang 	const char *json_output =
64*e407b4c8SLawrence Tang 		json_object_to_json_string_ext(ir, JSON_C_TO_STRING_PRETTY);
6502c801a5SLawrence Tang 
6602c801a5SLawrence Tang 	//Check whether there is a "--out" argument, if there is, then output to file instead.
6702c801a5SLawrence Tang 	//Otherwise, just send to console.
68*e407b4c8SLawrence Tang 	if (argc != 5) {
6902c801a5SLawrence Tang 		printf("%s\n", json_output);
7002c801a5SLawrence Tang 		return;
7102c801a5SLawrence Tang 	}
7202c801a5SLawrence Tang 
7302c801a5SLawrence Tang 	//File out argument exists. Argument valid?
74*e407b4c8SLawrence Tang 	if (strcmp(argv[3], "--out") != 0) {
75*e407b4c8SLawrence Tang 		printf("Invalid argument '%s' for command 'to-json'. See 'cper-convert --help' for command information.\n",
76*e407b4c8SLawrence Tang 		       argv[3]);
7702c801a5SLawrence Tang 		return;
7802c801a5SLawrence Tang 	}
7902c801a5SLawrence Tang 
8002c801a5SLawrence Tang 	//Try to open a file handle to the desired output file.
8102c801a5SLawrence Tang 	FILE *json_file = fopen(argv[4], "w");
82*e407b4c8SLawrence Tang 	if (json_file == NULL) {
83*e407b4c8SLawrence Tang 		printf("Could not get a handle for output file '%s', file handle returned null.\n",
84*e407b4c8SLawrence Tang 		       argv[4]);
8502c801a5SLawrence Tang 		return;
8602c801a5SLawrence Tang 	}
8702c801a5SLawrence Tang 
8802c801a5SLawrence Tang 	//Write out to file.
8902c801a5SLawrence Tang 	fwrite(json_output, strlen(json_output), 1, json_file);
9002c801a5SLawrence Tang 	fclose(json_file);
9102c801a5SLawrence Tang }
9202c801a5SLawrence Tang 
9302c801a5SLawrence Tang //Command for converting a provided CPER-JSON JSON file to CPER binary.
9402c801a5SLawrence Tang void json_to_cper(int argc, char *argv[])
9502c801a5SLawrence Tang {
96*e407b4c8SLawrence Tang 	if (argc < 5) {
9702c801a5SLawrence Tang 		printf("Insufficient number of arguments for 'to-cper'. See 'cper-convert --help' for command information.\n");
9802c801a5SLawrence Tang 		return;
9902c801a5SLawrence Tang 	}
10002c801a5SLawrence Tang 
10102c801a5SLawrence Tang 	//Read JSON IR from file.
10202c801a5SLawrence Tang 	json_object *ir = json_object_from_file(argv[2]);
103*e407b4c8SLawrence Tang 	if (ir == NULL) {
104*e407b4c8SLawrence Tang 		printf("Could not read JSON from file '%s', import returned null.\n",
105*e407b4c8SLawrence Tang 		       argv[2]);
10602c801a5SLawrence Tang 		return;
10702c801a5SLawrence Tang 	}
10802c801a5SLawrence Tang 
10902c801a5SLawrence Tang 	//Are we skipping validation?
11002c801a5SLawrence Tang 	int do_validate = 1;
111*e407b4c8SLawrence Tang 	if (argc >= 6 && argc < 8) {
112*e407b4c8SLawrence Tang 		if (strcmp(argv[5], "--no-validate") == 0) {
11302c801a5SLawrence Tang 			do_validate = 0;
11402c801a5SLawrence Tang 		}
11502c801a5SLawrence Tang 	}
11602c801a5SLawrence Tang 
11702c801a5SLawrence Tang 	//Validate the JSON against specification, unless otherwise specified.
118*e407b4c8SLawrence Tang 	if (do_validate) {
11902c801a5SLawrence Tang 		char *specification_path = NULL;
12002c801a5SLawrence Tang 
121d34f2b11SLawrence Tang 		//Is there a specification file path?
122*e407b4c8SLawrence Tang 		if (argc >= 7 &&
123*e407b4c8SLawrence Tang 		    strcmp(argv[argc - 2], "--specification") == 0) {
124d34f2b11SLawrence Tang 			specification_path = argv[argc - 1];
125*e407b4c8SLawrence Tang 		} else {
12602c801a5SLawrence Tang 			//Make the specification path the assumed default (application directory + specification/cper-json.json).
12702c801a5SLawrence Tang 			specification_path = malloc(PATH_MAX);
12802c801a5SLawrence Tang 			char *dir = dirname(argv[0]);
12902c801a5SLawrence Tang 			strcpy(specification_path, dir);
130*e407b4c8SLawrence Tang 			strcat(specification_path,
131*e407b4c8SLawrence Tang 			       "/specification/cper-json.json");
13202c801a5SLawrence Tang 		}
133d34f2b11SLawrence Tang 
134d34f2b11SLawrence Tang 		//Enable debug mode if indicated.
135*e407b4c8SLawrence Tang 		for (int i = 5; i < argc; i++) {
136*e407b4c8SLawrence Tang 			if (strcmp(argv[i], "--debug") == 0) {
137d34f2b11SLawrence Tang 				validate_schema_debug_enable();
138d34f2b11SLawrence Tang 				printf("debug enabled.\n");
139d34f2b11SLawrence Tang 				break;
140d34f2b11SLawrence Tang 			}
14102c801a5SLawrence Tang 		}
14202c801a5SLawrence Tang 
14302c801a5SLawrence Tang 		//Attempt to verify with the the specification.
14402c801a5SLawrence Tang 		char *error_message = malloc(JSON_ERROR_MSG_MAX_LEN);
145*e407b4c8SLawrence Tang 		int success = validate_schema_from_file(specification_path, ir,
146*e407b4c8SLawrence Tang 							error_message);
14702c801a5SLawrence Tang 
14802c801a5SLawrence Tang 		//Free specification path (if necessary).
14902c801a5SLawrence Tang 		if (argc == 5)
15002c801a5SLawrence Tang 			free(specification_path);
15102c801a5SLawrence Tang 
15202c801a5SLawrence Tang 		//If failed, early exit before conversion.
153*e407b4c8SLawrence Tang 		if (!success) {
154*e407b4c8SLawrence Tang 			printf("JSON format validation failed: %s\n",
155*e407b4c8SLawrence Tang 			       error_message);
15602c801a5SLawrence Tang 			free(error_message);
15702c801a5SLawrence Tang 			return;
15802c801a5SLawrence Tang 		}
15902c801a5SLawrence Tang 		free(error_message);
16002c801a5SLawrence Tang 	}
16102c801a5SLawrence Tang 
16202c801a5SLawrence Tang 	//Attempt a conversion.
16302c801a5SLawrence Tang 	//Open a read for the output file.
16402c801a5SLawrence Tang 	FILE *cper_file = fopen(argv[4], "w");
165*e407b4c8SLawrence Tang 	if (cper_file == NULL) {
166*e407b4c8SLawrence Tang 		printf("Could not open output file '%s', file handle returned null.\n",
167*e407b4c8SLawrence Tang 		       argv[4]);
16802c801a5SLawrence Tang 		return;
16902c801a5SLawrence Tang 	}
17002c801a5SLawrence Tang 
17102c801a5SLawrence Tang 	//Run the converter.
17202c801a5SLawrence Tang 	ir_to_cper(ir, cper_file);
17302c801a5SLawrence Tang 	fclose(cper_file);
17402c801a5SLawrence Tang }
17502c801a5SLawrence Tang 
17602c801a5SLawrence Tang //Command for printing help information.
17702c801a5SLawrence Tang void print_help(void)
17802c801a5SLawrence Tang {
17902c801a5SLawrence Tang 	printf(":: to-json cper.file [--out file.name]\n");
18002c801a5SLawrence Tang 	printf("\tConverts the provided CPER log file into JSON, by default outputting to console. If '--out' is specified,\n");
18102c801a5SLawrence Tang 	printf("\tThe outputted JSON will be written to the provided file name instead.\n");
182d34f2b11SLawrence Tang 	printf("\n:: to-cper cper.json --out file.name [--no-validate] [--debug] [--specification some/spec/path.json]\n");
18302c801a5SLawrence Tang 	printf("\tConverts the provided CPER-JSON JSON file into CPER binary. An output file must be specified with '--out'.\n");
18402c801a5SLawrence Tang 	printf("\tBy default, the provided JSON will try to be validated against a specification. If no specification file path\n");
18502c801a5SLawrence Tang 	printf("\tis provided with '--specification', then it will default to 'argv[0] + /specification/cper-json.json'.\n");
18602c801a5SLawrence Tang 	printf("\tIf the '--no-validate' argument is set, then the provided JSON will not be validated. Be warned, this may cause\n");
18702c801a5SLawrence Tang 	printf("\tpremature exit/unexpected behaviour in CPER output.\n");
18802c801a5SLawrence Tang 	printf("\n:: --help\n");
18902c801a5SLawrence Tang 	printf("\tDisplays help information to the console.\n");
19002c801a5SLawrence Tang }