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> |
Ed Tanous | 3e72806 | 2025-03-17 20:55:20 -0700 | [diff] [blame] | 16 | #include <libcper/Cper.h> |
| 17 | #include <libcper/base64.h> |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 18 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 19 | void cper_to_json(char *in_file, char *out_file, int is_single_section); |
Ed Tanous | edee0a3 | 2025-03-16 17:40:04 -0700 | [diff] [blame] | 20 | void json_to_cper(const char *in_file, const char *out_file); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 21 | void print_help(void); |
| 22 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 23 | int main(int argc, char *argv[]) |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 24 | { |
Ed Tanous | 50b966f | 2025-03-11 09:06:19 -0700 | [diff] [blame] | 25 | cper_set_log_stdio(); |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 26 | //Print help if requested. |
| 27 | if (argc == 2 && strcmp(argv[1], "--help") == 0) { |
| 28 | print_help(); |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | //Ensure at least two arguments are present. |
| 33 | if (argc < 3) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 34 | printf("Invalid number of arguments. See 'cper-convert --help' for command information.\n"); |
| 35 | return -1; |
| 36 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 37 | |
Lawrence Tang | 71c70a3 | 2022-08-08 09:16:06 +0100 | [diff] [blame] | 38 | //Parse the command line arguments. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 39 | char *input_file = argv[2]; |
| 40 | char *output_file = NULL; |
| 41 | char *specification_file = NULL; |
| 42 | int no_validate = 0; |
| 43 | int debug = 0; |
| 44 | for (int i = 3; i < argc; i++) { |
| 45 | if (strcmp(argv[i], "--out") == 0 && i < argc - 1) { |
| 46 | //Output file. |
| 47 | output_file = argv[i + 1]; |
| 48 | i++; |
| 49 | } else if (strcmp(argv[i], "--specification") == 0 && |
| 50 | i < argc - 1) { |
| 51 | //Specification file. |
| 52 | specification_file = argv[i + 1]; |
| 53 | i++; |
| 54 | } else if (strcmp(argv[i], "--no-validate") == 0) { |
| 55 | //No validation to be used. |
| 56 | //Invalidates specification file. |
| 57 | specification_file = NULL; |
| 58 | no_validate = 1; |
| 59 | } else if (strcmp(argv[i], "--debug") == 0) { |
| 60 | //Debug output on. |
| 61 | debug = 1; |
| 62 | } else { |
| 63 | printf("Unrecognised argument '%s'. See 'cper-convert --help' for command information.\n", |
| 64 | argv[i]); |
| 65 | } |
| 66 | } |
Lawrence Tang | 71c70a3 | 2022-08-08 09:16:06 +0100 | [diff] [blame] | 67 | |
Ed Tanous | 4bdb226 | 2025-03-10 21:13:46 -0700 | [diff] [blame] | 68 | // Debug is not used at the moment. Leave for compatibility. |
| 69 | (void)debug; |
| 70 | (void)no_validate; |
| 71 | (void)specification_file; |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 72 | //Run the requested command. |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 73 | if (strcmp(argv[1], "to-json") == 0) { |
| 74 | cper_to_json(input_file, output_file, 0); |
| 75 | } else if (strcmp(argv[1], "to-json-section") == 0) { |
| 76 | cper_to_json(input_file, output_file, 1); |
| 77 | } else if (strcmp(argv[1], "to-cper") == 0) { |
Ed Tanous | 4bdb226 | 2025-03-10 21:13:46 -0700 | [diff] [blame] | 78 | json_to_cper(input_file, output_file); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 79 | } else { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 80 | printf("Unrecognised argument '%s'. See 'cper-convert --help' for command information.\n", |
| 81 | argv[1]); |
| 82 | return -1; |
| 83 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 84 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 85 | return 0; |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 88 | //Command for converting a provided CPER log file or CPER single section file into JSON. |
| 89 | 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] | 90 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 91 | //Get a handle for the log file. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 92 | FILE *cper_file = fopen(in_file, "r"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 93 | if (cper_file == NULL) { |
| 94 | 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] | 95 | in_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 96 | return; |
| 97 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 98 | |
Ed Tanous | 3e72806 | 2025-03-17 20:55:20 -0700 | [diff] [blame] | 99 | fseek(cper_file, 0, SEEK_END); |
| 100 | long fsize = ftell(cper_file); |
| 101 | fseek(cper_file, 0, SEEK_SET); |
| 102 | |
| 103 | char *fbuff = malloc(fsize); |
| 104 | size_t readsize = fread(fbuff, 1, (long)fsize, cper_file); |
| 105 | if (readsize != (size_t)fsize) { |
Aushim Nagarkatti | 4f887d8 | 2025-05-16 14:06:30 -0700 | [diff] [blame^] | 106 | printf("Could not read CPER file '%s', read returned %zu bytes.\n", |
Ed Tanous | 3e72806 | 2025-03-17 20:55:20 -0700 | [diff] [blame] | 107 | in_file, readsize); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | if (!header_valid(fbuff, readsize)) { |
| 112 | // Check if it's base64 encoded |
| 113 | int32_t decoded_len = 0; |
| 114 | UINT8 *decoded = base64_decode(fbuff, readsize, &decoded_len); |
| 115 | if (decoded == NULL) { |
| 116 | printf("base64 decode failed for CPER file '%s'.\n", |
| 117 | in_file); |
| 118 | free(fbuff); |
| 119 | free(decoded); |
| 120 | return; |
| 121 | } |
| 122 | if (!header_valid((const char *)decoded, decoded_len)) { |
| 123 | printf("Invalid CPER file '%s'.\n", in_file); |
| 124 | free(fbuff); |
| 125 | free(decoded); |
| 126 | return; |
| 127 | } |
| 128 | // Swap the buffer to the base64 decoded buffer. |
| 129 | free(fbuff); |
| 130 | fbuff = (char *)decoded; |
| 131 | |
| 132 | fsize = decoded_len; |
| 133 | decoded = NULL; |
| 134 | } |
| 135 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 136 | //Convert. |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 137 | json_object *ir; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 138 | if (is_single_section) { |
Ed Tanous | 3e72806 | 2025-03-17 20:55:20 -0700 | [diff] [blame] | 139 | ir = cper_buf_single_section_to_ir((UINT8 *)fbuff, readsize); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 140 | } else { |
Ed Tanous | 3e72806 | 2025-03-17 20:55:20 -0700 | [diff] [blame] | 141 | ir = cper_buf_to_ir((UINT8 *)fbuff, fsize); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 142 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 143 | fclose(cper_file); |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 144 | |
| 145 | //Output to string. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 146 | const char *json_output = |
| 147 | json_object_to_json_string_ext(ir, JSON_C_TO_STRING_PRETTY); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 148 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 149 | //Check whether there is a "--out" argument, if there is, then output to file instead. |
| 150 | //Otherwise, just send to console. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 151 | if (out_file == NULL) { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 152 | printf("%s\n", json_output); |
| 153 | return; |
| 154 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 155 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 156 | //Try to open a file handle to the desired output file. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 157 | FILE *json_file = fopen(out_file, "w"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 158 | if (json_file == NULL) { |
| 159 | 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] | 160 | out_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 161 | return; |
| 162 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 163 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 164 | //Write out to file. |
| 165 | fwrite(json_output, strlen(json_output), 1, json_file); |
| 166 | fclose(json_file); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | //Command for converting a provided CPER-JSON JSON file to CPER binary. |
Ed Tanous | edee0a3 | 2025-03-16 17:40:04 -0700 | [diff] [blame] | 170 | void json_to_cper(const char *in_file, const char *out_file) |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 171 | { |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 172 | //Verify output file exists. |
| 173 | if (out_file == NULL) { |
| 174 | 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] | 175 | return; |
| 176 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 177 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 178 | //Read JSON IR from file. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 179 | json_object *ir = json_object_from_file(in_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 180 | if (ir == NULL) { |
| 181 | printf("Could not read JSON from file '%s', import returned null.\n", |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 182 | in_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 183 | return; |
| 184 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 185 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 186 | //Open a read for the output file. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 187 | FILE *cper_file = fopen(out_file, "w"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 188 | if (cper_file == NULL) { |
| 189 | printf("Could not open output file '%s', file handle returned null.\n", |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 190 | out_file); |
Ed Tanous | c6aaee0 | 2025-04-11 12:16:21 -0700 | [diff] [blame] | 191 | json_object_put(ir); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 192 | return; |
| 193 | } |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 194 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 195 | //Detect the type of CPER (full log, single section) from the IR given. |
| 196 | //Run the converter accordingly. |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 197 | if (json_object_object_get(ir, "header") != NULL) { |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 198 | ir_to_cper(ir, cper_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 199 | } else { |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 200 | ir_single_section_to_cper(ir, cper_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 201 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 202 | fclose(cper_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 203 | json_object_put(ir); |
Lawrence Tang | b8fa2f7 | 2022-07-18 10:50:19 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | //Command for printing help information. |
| 207 | void print_help(void) |
| 208 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 209 | printf(":: to-json cper.file [--out file.name]\n"); |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 210 | 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] | 211 | 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] | 212 | printf("\n:: to-json-section cper.section.file [--out file.name]\n"); |
| 213 | printf("\tConverts the provided single CPER section descriptor & section file into JSON, by default writing to stdout.\n"); |
| 214 | printf("\tOtherwise behaves the same as 'to-json'.\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 215 | printf("\n:: to-cper cper.json --out file.name [--no-validate] [--debug] [--specification some/spec/path.json]\n"); |
| 216 | 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] | 217 | printf("\tWill automatically detect whether the JSON passed is a single section, or a whole file,\n"); |
| 218 | printf("\tand output binary accordingly.\n\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 219 | printf("\tBy default, the provided JSON will try to be validated against a specification. If no specification file path\n"); |
| 220 | printf("\tis provided with '--specification', then it will default to 'argv[0] + /specification/cper-json.json'.\n"); |
| 221 | 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] | 222 | printf("\tpremature exit/unexpected behaviour in CPER output.\n\n"); |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 223 | 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] | 224 | printf("\n:: --help\n"); |
| 225 | printf("\tDisplays help information to the console.\n"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 226 | } |