Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 1 | /** |
| 2 | * A user-space application linking to the CPER-JSON conversion library which allows for easy |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 3 | * conversion between CPER and CPER-JSON formats. |
| 4 | * |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <libgen.h> |
Andrew Adriance | 4482c48 | 2024-07-02 14:55:11 -0700 | [diff] [blame] | 11 | #include <limits.h> |
Lawrence Tang | 5202bbb | 2022-08-12 14:54:36 +0100 | [diff] [blame] | 12 | #include <json.h> |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 13 | #include <libcper/log.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 14 | #include <libcper/cper-parse.h> |
| 15 | #include <libcper/json-schema.h> |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 16 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 17 | void cper_to_json(char *in_file, char *out_file, int is_single_section); |
Ed Tanous | 4bdb226 | 2025-03-10 21:13:46 -0700 | [diff] [blame] | 18 | void json_to_cper(char *in_file, char *out_file); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 19 | void print_help(void); |
| 20 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 21 | int main(int argc, char *argv[]) |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 22 | { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 23 | cper_set_log_stdio(); |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 24 | //Print help if requested. |
| 25 | if (argc == 2 && strcmp(argv[1], "--help") == 0) { |
| 26 | print_help(); |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | //Ensure at least two arguments are present. |
| 31 | if (argc < 3) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 32 | printf("Invalid number of arguments. See 'cper-convert --help' for command information.\n"); |
| 33 | return -1; |
| 34 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 35 | |
Lawrence Tang | 71c70a3 | 2022-08-08 09:16:06 +0100 | [diff] [blame] | 36 | //Parse the command line arguments. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 37 | char *input_file = argv[2]; |
| 38 | char *output_file = NULL; |
| 39 | char *specification_file = NULL; |
| 40 | int no_validate = 0; |
| 41 | int debug = 0; |
| 42 | for (int i = 3; i < argc; i++) { |
| 43 | if (strcmp(argv[i], "--out") == 0 && i < argc - 1) { |
| 44 | //Output file. |
| 45 | output_file = argv[i + 1]; |
| 46 | i++; |
| 47 | } else if (strcmp(argv[i], "--specification") == 0 && |
| 48 | i < argc - 1) { |
| 49 | //Specification file. |
| 50 | specification_file = argv[i + 1]; |
| 51 | i++; |
| 52 | } else if (strcmp(argv[i], "--no-validate") == 0) { |
| 53 | //No validation to be used. |
| 54 | //Invalidates specification file. |
| 55 | specification_file = NULL; |
| 56 | no_validate = 1; |
| 57 | } else if (strcmp(argv[i], "--debug") == 0) { |
| 58 | //Debug output on. |
| 59 | debug = 1; |
| 60 | } else { |
| 61 | printf("Unrecognised argument '%s'. See 'cper-convert --help' for command information.\n", |
| 62 | argv[i]); |
| 63 | } |
| 64 | } |
Lawrence Tang | 71c70a3 | 2022-08-08 09:16:06 +0100 | [diff] [blame] | 65 | |
Ed Tanous | 4bdb226 | 2025-03-10 21:13:46 -0700 | [diff] [blame] | 66 | // Debug is not used at the moment. Leave for compatibility. |
| 67 | (void)debug; |
| 68 | (void)no_validate; |
| 69 | (void)specification_file; |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 70 | //Run the requested command. |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 71 | if (strcmp(argv[1], "to-json") == 0) { |
| 72 | cper_to_json(input_file, output_file, 0); |
| 73 | } else if (strcmp(argv[1], "to-json-section") == 0) { |
| 74 | cper_to_json(input_file, output_file, 1); |
| 75 | } else if (strcmp(argv[1], "to-cper") == 0) { |
Ed Tanous | 4bdb226 | 2025-03-10 21:13:46 -0700 | [diff] [blame] | 76 | json_to_cper(input_file, output_file); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 77 | } else { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 78 | printf("Unrecognised argument '%s'. See 'cper-convert --help' for command information.\n", |
| 79 | argv[1]); |
| 80 | return -1; |
| 81 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 82 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 83 | return 0; |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 86 | //Command for converting a provided CPER log file or CPER single section file into JSON. |
| 87 | void cper_to_json(char *in_file, char *out_file, int is_single_section) |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 88 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 89 | //Get a handle for the log file. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 90 | FILE *cper_file = fopen(in_file, "r"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 91 | if (cper_file == NULL) { |
| 92 | printf("Could not open provided CPER file '%s', file handle returned null.\n", |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 93 | in_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 94 | return; |
| 95 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 96 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 97 | //Convert. |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 98 | json_object *ir; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 99 | if (is_single_section) { |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 100 | ir = cper_single_section_to_ir(cper_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 101 | } else { |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 102 | ir = cper_to_ir(cper_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 103 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 104 | fclose(cper_file); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 105 | |
| 106 | //Output to string. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 107 | const char *json_output = |
| 108 | json_object_to_json_string_ext(ir, JSON_C_TO_STRING_PRETTY); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 109 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 110 | //Check whether there is a "--out" argument, if there is, then output to file instead. |
| 111 | //Otherwise, just send to console. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 112 | if (out_file == NULL) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 113 | printf("%s\n", json_output); |
| 114 | return; |
| 115 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 116 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 117 | //Try to open a file handle to the desired output file. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 118 | FILE *json_file = fopen(out_file, "w"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 119 | if (json_file == NULL) { |
| 120 | printf("Could not get a handle for output file '%s', file handle returned null.\n", |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 121 | out_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 122 | return; |
| 123 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 124 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 125 | //Write out to file. |
| 126 | fwrite(json_output, strlen(json_output), 1, json_file); |
| 127 | fclose(json_file); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | //Command for converting a provided CPER-JSON JSON file to CPER binary. |
Ed Tanous | 4bdb226 | 2025-03-10 21:13:46 -0700 | [diff] [blame] | 131 | void json_to_cper(char *in_file, char *out_file) |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 132 | { |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 133 | //Verify output file exists. |
| 134 | if (out_file == NULL) { |
| 135 | printf("No output file provided for 'to-cper'. See 'cper-convert --help' for command information.\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 136 | return; |
| 137 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 138 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 139 | //Read JSON IR from file. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 140 | json_object *ir = json_object_from_file(in_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 141 | if (ir == NULL) { |
| 142 | printf("Could not read JSON from file '%s', import returned null.\n", |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 143 | in_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 144 | return; |
| 145 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 146 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 147 | //Open a read for the output file. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 148 | FILE *cper_file = fopen(out_file, "w"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 149 | if (cper_file == NULL) { |
| 150 | printf("Could not open output file '%s', file handle returned null.\n", |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 151 | out_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 152 | return; |
| 153 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 154 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 155 | //Detect the type of CPER (full log, single section) from the IR given. |
| 156 | //Run the converter accordingly. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 157 | if (json_object_object_get(ir, "header") != NULL) { |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 158 | ir_to_cper(ir, cper_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 159 | } else { |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 160 | ir_single_section_to_cper(ir, cper_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 161 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 162 | fclose(cper_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 163 | json_object_put(ir); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | //Command for printing help information. |
| 167 | void print_help(void) |
| 168 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 169 | printf(":: to-json cper.file [--out file.name]\n"); |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 170 | printf("\tConverts the provided CPER log file into JSON, by default writing to stdout. If '--out' is specified,\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 171 | printf("\tThe outputted JSON will be written to the provided file name instead.\n"); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 172 | printf("\n:: to-json-section cper.section.file [--out file.name]\n"); |
| 173 | printf("\tConverts the provided single CPER section descriptor & section file into JSON, by default writing to stdout.\n"); |
| 174 | printf("\tOtherwise behaves the same as 'to-json'.\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 175 | printf("\n:: to-cper cper.json --out file.name [--no-validate] [--debug] [--specification some/spec/path.json]\n"); |
| 176 | printf("\tConverts the provided CPER-JSON JSON file into CPER binary. An output file must be specified with '--out'.\n"); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 177 | printf("\tWill automatically detect whether the JSON passed is a single section, or a whole file,\n"); |
| 178 | printf("\tand output binary accordingly.\n\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 179 | printf("\tBy default, the provided JSON will try to be validated against a specification. If no specification file path\n"); |
| 180 | printf("\tis provided with '--specification', then it will default to 'argv[0] + /specification/cper-json.json'.\n"); |
| 181 | printf("\tIf the '--no-validate' argument is set, then the provided JSON will not be validated. Be warned, this may cause\n"); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 182 | printf("\tpremature exit/unexpected behaviour in CPER output.\n\n"); |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 183 | printf("\tIf '--debug' is set, then debug output for JSON specification parsing will be printed to stdout.\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 184 | printf("\n:: --help\n"); |
| 185 | printf("\tDisplays help information to the console.\n"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 186 | } |