blob: 86a6d6f7cd3d59aabdf9de0eef3f906b34503125 [file] [log] [blame]
Lawrence Tangb8fa2f72022-07-18 10:50:19 +01001/**
2 * A user-space application linking to the CPER-JSON conversion library which allows for easy
3 * conversion between CPER and CPER-JSON formats.
4 *
5 * Author: Lawrence.Tang@arm.com
6 **/
7
8#include <stdio.h>
9#include <string.h>
10#include <libgen.h>
11#include <limits.h>
Lawrence Tang5202bbb2022-08-12 14:54:36 +010012#include <json.h>
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010013#include "../cper-parse.h"
14#include "../json-schema.h"
15
Lawrence Tang617949e2022-08-08 14:21:42 +010016void cper_to_json(char *in_file, char *out_file, int is_single_section);
Lawrence Tang5f388a32022-08-08 10:49:02 +010017void json_to_cper(char *in_file, char *out_file, char *specification_file,
18 char *program_dir, int no_validate, int debug);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010019void print_help(void);
20
Lawrence Tange407b4c2022-07-21 13:54:01 +010021int main(int argc, char *argv[])
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010022{
Lawrence Tang5f388a32022-08-08 10:49:02 +010023 //Print help if requested.
24 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
25 print_help();
26 return 0;
27 }
28
29 //Ensure at least two arguments are present.
30 if (argc < 3) {
Lawrence Tange407b4c2022-07-21 13:54:01 +010031 printf("Invalid number of arguments. See 'cper-convert --help' for command information.\n");
32 return -1;
33 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010034
Lawrence Tang71c70a32022-08-08 09:16:06 +010035 //Parse the command line arguments.
Lawrence Tang5f388a32022-08-08 10:49:02 +010036 char *input_file = argv[2];
37 char *output_file = NULL;
38 char *specification_file = NULL;
39 int no_validate = 0;
40 int debug = 0;
41 for (int i = 3; i < argc; i++) {
42 if (strcmp(argv[i], "--out") == 0 && i < argc - 1) {
43 //Output file.
44 output_file = argv[i + 1];
45 i++;
46 } else if (strcmp(argv[i], "--specification") == 0 &&
47 i < argc - 1) {
48 //Specification file.
49 specification_file = argv[i + 1];
50 i++;
51 } else if (strcmp(argv[i], "--no-validate") == 0) {
52 //No validation to be used.
53 //Invalidates specification file.
54 specification_file = NULL;
55 no_validate = 1;
56 } else if (strcmp(argv[i], "--debug") == 0) {
57 //Debug output on.
58 debug = 1;
59 } else {
60 printf("Unrecognised argument '%s'. See 'cper-convert --help' for command information.\n",
61 argv[i]);
62 }
63 }
Lawrence Tang71c70a32022-08-08 09:16:06 +010064
Lawrence Tange407b4c2022-07-21 13:54:01 +010065 //Run the requested command.
Lawrence Tang617949e2022-08-08 14:21:42 +010066 if (strcmp(argv[1], "to-json") == 0) {
67 cper_to_json(input_file, output_file, 0);
68 } else if (strcmp(argv[1], "to-json-section") == 0) {
69 cper_to_json(input_file, output_file, 1);
70 } else if (strcmp(argv[1], "to-cper") == 0) {
Lawrence Tang5f388a32022-08-08 10:49:02 +010071 json_to_cper(input_file, output_file, specification_file,
72 argv[0], no_validate, debug);
Lawrence Tang617949e2022-08-08 14:21:42 +010073 } else {
Lawrence Tange407b4c2022-07-21 13:54:01 +010074 printf("Unrecognised argument '%s'. See 'cper-convert --help' for command information.\n",
75 argv[1]);
76 return -1;
77 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010078
Lawrence Tange407b4c2022-07-21 13:54:01 +010079 return 0;
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010080}
81
Lawrence Tang617949e2022-08-08 14:21:42 +010082//Command for converting a provided CPER log file or CPER single section file into JSON.
83void cper_to_json(char *in_file, char *out_file, int is_single_section)
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010084{
Lawrence Tange407b4c2022-07-21 13:54:01 +010085 //Get a handle for the log file.
Lawrence Tang5f388a32022-08-08 10:49:02 +010086 FILE *cper_file = fopen(in_file, "r");
Lawrence Tange407b4c2022-07-21 13:54:01 +010087 if (cper_file == NULL) {
88 printf("Could not open provided CPER file '%s', file handle returned null.\n",
Lawrence Tang5f388a32022-08-08 10:49:02 +010089 in_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +010090 return;
91 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010092
Lawrence Tange407b4c2022-07-21 13:54:01 +010093 //Convert.
Lawrence Tang617949e2022-08-08 14:21:42 +010094 json_object *ir;
95 if (is_single_section)
96 ir = cper_single_section_to_ir(cper_file);
97 else
98 ir = cper_to_ir(cper_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +010099 fclose(cper_file);
Lawrence Tang617949e2022-08-08 14:21:42 +0100100
101 //Output to string.
Lawrence Tange407b4c2022-07-21 13:54:01 +0100102 const char *json_output =
103 json_object_to_json_string_ext(ir, JSON_C_TO_STRING_PRETTY);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100104
Lawrence Tange407b4c2022-07-21 13:54:01 +0100105 //Check whether there is a "--out" argument, if there is, then output to file instead.
106 //Otherwise, just send to console.
Lawrence Tang5f388a32022-08-08 10:49:02 +0100107 if (out_file == NULL) {
Lawrence Tange407b4c2022-07-21 13:54:01 +0100108 printf("%s\n", json_output);
109 return;
110 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100111
Lawrence Tange407b4c2022-07-21 13:54:01 +0100112 //Try to open a file handle to the desired output file.
Lawrence Tang5f388a32022-08-08 10:49:02 +0100113 FILE *json_file = fopen(out_file, "w");
Lawrence Tange407b4c2022-07-21 13:54:01 +0100114 if (json_file == NULL) {
115 printf("Could not get a handle for output file '%s', file handle returned null.\n",
Lawrence Tang5f388a32022-08-08 10:49:02 +0100116 out_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100117 return;
118 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100119
Lawrence Tange407b4c2022-07-21 13:54:01 +0100120 //Write out to file.
121 fwrite(json_output, strlen(json_output), 1, json_file);
122 fclose(json_file);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100123}
124
125//Command for converting a provided CPER-JSON JSON file to CPER binary.
Lawrence Tang5f388a32022-08-08 10:49:02 +0100126void json_to_cper(char *in_file, char *out_file, char *specification_file,
127 char *program_dir, int no_validate, int debug)
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100128{
Lawrence Tang5f388a32022-08-08 10:49:02 +0100129 //Verify output file exists.
130 if (out_file == NULL) {
131 printf("No output file provided for 'to-cper'. See 'cper-convert --help' for command information.\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +0100132 return;
133 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100134
Lawrence Tange407b4c2022-07-21 13:54:01 +0100135 //Read JSON IR from file.
Lawrence Tang5f388a32022-08-08 10:49:02 +0100136 json_object *ir = json_object_from_file(in_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100137 if (ir == NULL) {
138 printf("Could not read JSON from file '%s', import returned null.\n",
Lawrence Tang5f388a32022-08-08 10:49:02 +0100139 in_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100140 return;
141 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100142
Lawrence Tange407b4c2022-07-21 13:54:01 +0100143 //Validate the JSON against specification, unless otherwise specified.
Lawrence Tang5f388a32022-08-08 10:49:02 +0100144 if (!no_validate) {
145 int using_default_spec_path = 0;
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100146
Lawrence Tange407b4c2022-07-21 13:54:01 +0100147 //Is there a specification file path?
Lawrence Tang5f388a32022-08-08 10:49:02 +0100148 if (specification_file == NULL) {
149 using_default_spec_path = 1;
150
Lawrence Tange407b4c2022-07-21 13:54:01 +0100151 //Make the specification path the assumed default (application directory + specification/cper-json.json).
Lawrence Tang5f388a32022-08-08 10:49:02 +0100152 specification_file = malloc(PATH_MAX);
153 char *dir = dirname(program_dir);
154 strcpy(specification_file, dir);
155 strcat(specification_file,
Lawrence Tange407b4c2022-07-21 13:54:01 +0100156 "/specification/cper-json.json");
157 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100158
Lawrence Tange407b4c2022-07-21 13:54:01 +0100159 //Enable debug mode if indicated.
Lawrence Tang5f388a32022-08-08 10:49:02 +0100160 if (debug)
161 validate_schema_debug_enable();
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100162
Lawrence Tange407b4c2022-07-21 13:54:01 +0100163 //Attempt to verify with the the specification.
164 char *error_message = malloc(JSON_ERROR_MSG_MAX_LEN);
Lawrence Tang5f388a32022-08-08 10:49:02 +0100165 int success = validate_schema_from_file(specification_file, ir,
Lawrence Tange407b4c2022-07-21 13:54:01 +0100166 error_message);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100167
Lawrence Tange407b4c2022-07-21 13:54:01 +0100168 //Free specification path (if necessary).
Lawrence Tang5f388a32022-08-08 10:49:02 +0100169 if (using_default_spec_path)
170 free(specification_file);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100171
Lawrence Tange407b4c2022-07-21 13:54:01 +0100172 //If failed, early exit before conversion.
173 if (!success) {
174 printf("JSON format validation failed: %s\n",
175 error_message);
176 free(error_message);
177 return;
178 }
179 free(error_message);
180 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100181
Lawrence Tange407b4c2022-07-21 13:54:01 +0100182 //Open a read for the output file.
Lawrence Tang5f388a32022-08-08 10:49:02 +0100183 FILE *cper_file = fopen(out_file, "w");
Lawrence Tange407b4c2022-07-21 13:54:01 +0100184 if (cper_file == NULL) {
185 printf("Could not open output file '%s', file handle returned null.\n",
Lawrence Tang5f388a32022-08-08 10:49:02 +0100186 out_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100187 return;
188 }
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100189
Lawrence Tang617949e2022-08-08 14:21:42 +0100190 //Detect the type of CPER (full log, single section) from the IR given.
191 //Run the converter accordingly.
192 if (json_object_object_get(ir, "header") != NULL)
193 ir_to_cper(ir, cper_file);
194 else
195 ir_single_section_to_cper(ir, cper_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +0100196 fclose(cper_file);
Lawrence Tangb8fa2f72022-07-18 10:50:19 +0100197}
198
199//Command for printing help information.
200void print_help(void)
201{
Lawrence Tange407b4c2022-07-21 13:54:01 +0100202 printf(":: to-json cper.file [--out file.name]\n");
Lawrence Tang5f388a32022-08-08 10:49:02 +0100203 printf("\tConverts the provided CPER log file into JSON, by default writing to stdout. If '--out' is specified,\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +0100204 printf("\tThe outputted JSON will be written to the provided file name instead.\n");
Lawrence Tang617949e2022-08-08 14:21:42 +0100205 printf("\n:: to-json-section cper.section.file [--out file.name]\n");
206 printf("\tConverts the provided single CPER section descriptor & section file into JSON, by default writing to stdout.\n");
207 printf("\tOtherwise behaves the same as 'to-json'.\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +0100208 printf("\n:: to-cper cper.json --out file.name [--no-validate] [--debug] [--specification some/spec/path.json]\n");
209 printf("\tConverts the provided CPER-JSON JSON file into CPER binary. An output file must be specified with '--out'.\n");
Lawrence Tang617949e2022-08-08 14:21:42 +0100210 printf("\tWill automatically detect whether the JSON passed is a single section, or a whole file,\n");
211 printf("\tand output binary accordingly.\n\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +0100212 printf("\tBy default, the provided JSON will try to be validated against a specification. If no specification file path\n");
213 printf("\tis provided with '--specification', then it will default to 'argv[0] + /specification/cper-json.json'.\n");
214 printf("\tIf the '--no-validate' argument is set, then the provided JSON will not be validated. Be warned, this may cause\n");
Lawrence Tang617949e2022-08-08 14:21:42 +0100215 printf("\tpremature exit/unexpected behaviour in CPER output.\n\n");
Lawrence Tang5f388a32022-08-08 10:49:02 +0100216 printf("\tIf '--debug' is set, then debug output for JSON specification parsing will be printed to stdout.\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +0100217 printf("\n:: --help\n");
218 printf("\tDisplays help information to the console.\n");
Lawrence Tang4ef0a0a2022-07-18 10:50:58 +0100219}