Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 1 | /** |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 2 | * A user-space application for generating pseudo-random specification compliant CPER records. |
| 3 | * |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 4 | * Author: Lawrence.Tang@arm.com |
| 5 | **/ |
| 6 | |
| 7 | #include <stdio.h> |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 8 | #include <stdlib.h> |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 9 | #include <string.h> |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 10 | #include <libcper/Cper.h> |
| 11 | #include <libcper/generator/cper-generate.h> |
| 12 | #include <libcper/generator/sections/gen-section.h> |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 13 | |
| 14 | void print_help(); |
| 15 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 16 | int main(int argc, char *argv[]) |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 17 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 18 | //If help requested, print help. |
| 19 | if (argc == 2 && strcmp(argv[1], "--help") == 0) { |
| 20 | print_help(); |
| 21 | return 0; |
| 22 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 23 | |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 24 | //Parse the command line arguments. |
| 25 | char *out_file = NULL; |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 26 | char *single_section = NULL; |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 27 | char **sections = NULL; |
| 28 | UINT16 num_sections = 0; |
| 29 | for (int i = 1; i < argc; i++) { |
| 30 | if (strcmp(argv[i], "--out") == 0 && i < argc - 1) { |
| 31 | out_file = argv[i + 1]; |
| 32 | i++; |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 33 | } else if (strcmp(argv[i], "--single-section") == 0 && |
| 34 | i < argc - 1) { |
| 35 | single_section = argv[i + 1]; |
| 36 | i++; |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 37 | } else if (strcmp(argv[i], "--sections") == 0 && i < argc - 1) { |
| 38 | //All arguments after this must be section names. |
| 39 | num_sections = argc - i - 1; |
| 40 | sections = malloc(sizeof(char *) * num_sections); |
| 41 | i++; |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 42 | |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 43 | for (int j = i; j < argc; j++) { |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 44 | sections[j - i] = argv[j]; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 45 | } |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 46 | break; |
| 47 | } else { |
| 48 | printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n", |
| 49 | argv[i]); |
| 50 | return -1; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | //If no output file passed as argument, exit. |
| 55 | if (out_file == NULL) { |
| 56 | printf("No output file provided. For command information, refer to 'cper-generate --help'.\n"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 57 | if (sections) { |
| 58 | free(sections); |
| 59 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 60 | return -1; |
| 61 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 62 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 63 | //Open a file handle to write output. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 64 | FILE *cper_file = fopen(out_file, "w"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 65 | if (cper_file == NULL) { |
| 66 | 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] | 67 | out_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 68 | if (sections) { |
| 69 | free(sections); |
| 70 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 71 | return -1; |
| 72 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 73 | |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 74 | //Which type are we generating? |
| 75 | if (single_section != NULL && sections == NULL) { |
| 76 | generate_single_section_record(single_section, cper_file); |
| 77 | } else if (sections != NULL && single_section == NULL) { |
| 78 | generate_cper_record(sections, num_sections, cper_file); |
| 79 | } else { |
| 80 | //Invalid arguments. |
| 81 | printf("Invalid argument. Either both '--sections' and '--single-section' were set, or neither. For command information, refer to 'cper-generate --help'.\n"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 82 | if (sections) { |
| 83 | free(sections); |
| 84 | } |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 85 | return -1; |
| 86 | } |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 87 | |
| 88 | //Close & free remaining resources. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 89 | fclose(cper_file); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 90 | if (sections != NULL) { |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 91 | free(sections); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 92 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 95 | //Prints command help for this CPER generator. |
| 96 | void print_help() |
| 97 | { |
Lawrence Tang | 617949e | 2022-08-08 14:21:42 +0100 | [diff] [blame] | 98 | printf(":: --out cper.file [--sections section1 ...] [--single-section sectiontype]\n"); |
| 99 | printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n\n"); |
| 100 | printf("\tWhen the '--sections' flag is set, all following arguments are section names, and a full CPER log is generated\n"); |
| 101 | printf("\tcontaining the given sections.\n"); |
| 102 | printf("\tWhen the '--single-section' flag is set, the next argument is the single section that should be generated, and\n"); |
| 103 | printf("\ta single section (no header, only a section descriptor & section) CPER file is generated.\n\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 104 | printf("\tValid section type names are the following:\n"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 105 | for (size_t i = 0; i < generator_definitions_len; i++) { |
Lawrence Tang | ef9a325 | 2022-08-24 16:03:18 +0100 | [diff] [blame] | 106 | printf("\t\t- %s\n", generator_definitions[i].ShortName); |
| 107 | } |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 108 | printf("\t\t- unknown\n"); |
| 109 | printf("\n:: --help\n"); |
| 110 | printf("\tDisplays help information to the console.\n"); |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 111 | } |