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