blob: 8c570abeff0550eab9c337ac567c225ce037d07e [file] [log] [blame]
Lawrence Tangd34f2b12022-07-19 15:36:31 +01001/**
Ed Tanousfedd4572024-07-12 13:56:00 -07002 * A user-space application for generating pseudo-random specification compliant CPER records.
3 *
Lawrence Tangd34f2b12022-07-19 15:36:31 +01004 * Author: Lawrence.Tang@arm.com
5 **/
6
7#include <stdio.h>
Lawrence Tang5f388a32022-08-08 10:49:02 +01008#include <stdlib.h>
Lawrence Tangd34f2b12022-07-19 15:36:31 +01009#include <string.h>
Thu Nguyene42fb482024-10-15 14:43:11 +000010#include <libcper/Cper.h>
11#include <libcper/generator/cper-generate.h>
12#include <libcper/generator/sections/gen-section.h>
Lawrence Tangd34f2b12022-07-19 15:36:31 +010013
14void print_help();
15
Lawrence Tange407b4c2022-07-21 13:54:01 +010016int main(int argc, char *argv[])
Lawrence Tangd34f2b12022-07-19 15:36:31 +010017{
Lawrence Tange407b4c2022-07-21 13:54:01 +010018 //If help requested, print help.
19 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
20 print_help();
21 return 0;
22 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010023
Lawrence Tang5f388a32022-08-08 10:49:02 +010024 //Parse the command line arguments.
25 char *out_file = NULL;
Lawrence Tang617949e2022-08-08 14:21:42 +010026 char *single_section = NULL;
Lawrence Tang5f388a32022-08-08 10:49:02 +010027 char **sections = NULL;
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080028 const GEN_VALID_BITS_TEST_TYPE randomValidbitsSet = RANDOM_VALID;
Lawrence Tang5f388a32022-08-08 10:49:02 +010029 UINT16 num_sections = 0;
30 for (int i = 1; i < argc; i++) {
31 if (strcmp(argv[i], "--out") == 0 && i < argc - 1) {
32 out_file = argv[i + 1];
33 i++;
Lawrence Tang617949e2022-08-08 14:21:42 +010034 } else if (strcmp(argv[i], "--single-section") == 0 &&
35 i < argc - 1) {
36 single_section = argv[i + 1];
37 i++;
Lawrence Tang5f388a32022-08-08 10:49:02 +010038 } else if (strcmp(argv[i], "--sections") == 0 && i < argc - 1) {
39 //All arguments after this must be section names.
40 num_sections = argc - i - 1;
41 sections = malloc(sizeof(char *) * num_sections);
42 i++;
Lawrence Tang617949e2022-08-08 14:21:42 +010043
John Chungf8fc7052024-05-03 20:05:29 +080044 for (int j = i; j < argc; j++) {
Lawrence Tang5f388a32022-08-08 10:49:02 +010045 sections[j - i] = argv[j];
John Chungf8fc7052024-05-03 20:05:29 +080046 }
Lawrence Tang5f388a32022-08-08 10:49:02 +010047 break;
48 } else {
49 printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n",
50 argv[i]);
51 return -1;
52 }
53 }
54
55 //If no output file passed as argument, exit.
56 if (out_file == NULL) {
57 printf("No output file provided. For command information, refer to 'cper-generate --help'.\n");
John Chungf8fc7052024-05-03 20:05:29 +080058 if (sections) {
59 free(sections);
60 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010061 return -1;
62 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010063
Lawrence Tange407b4c2022-07-21 13:54:01 +010064 //Open a file handle to write output.
Lawrence Tang5f388a32022-08-08 10:49:02 +010065 FILE *cper_file = fopen(out_file, "w");
Lawrence Tange407b4c2022-07-21 13:54:01 +010066 if (cper_file == NULL) {
67 printf("Could not get a handle for output file '%s', file handle returned null.\n",
Lawrence Tang5f388a32022-08-08 10:49:02 +010068 out_file);
John Chungf8fc7052024-05-03 20:05:29 +080069 if (sections) {
70 free(sections);
71 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010072 return -1;
73 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010074
Lawrence Tang617949e2022-08-08 14:21:42 +010075 //Which type are we generating?
76 if (single_section != NULL && sections == NULL) {
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080077 generate_single_section_record(single_section, cper_file,
78 randomValidbitsSet);
Lawrence Tang617949e2022-08-08 14:21:42 +010079 } else if (sections != NULL && single_section == NULL) {
Aushim Nagarkattiae8f6d92025-01-29 17:34:44 -080080 generate_cper_record(sections, num_sections, cper_file,
81 randomValidbitsSet);
Lawrence Tang617949e2022-08-08 14:21:42 +010082 } else {
83 //Invalid arguments.
84 printf("Invalid argument. Either both '--sections' and '--single-section' were set, or neither. For command information, refer to 'cper-generate --help'.\n");
John Chungf8fc7052024-05-03 20:05:29 +080085 if (sections) {
86 free(sections);
87 }
Lawrence Tang617949e2022-08-08 14:21:42 +010088 return -1;
89 }
Lawrence Tang5f388a32022-08-08 10:49:02 +010090
91 //Close & free remaining resources.
Lawrence Tange407b4c2022-07-21 13:54:01 +010092 fclose(cper_file);
John Chungf8fc7052024-05-03 20:05:29 +080093 if (sections != NULL) {
Lawrence Tang5f388a32022-08-08 10:49:02 +010094 free(sections);
John Chungf8fc7052024-05-03 20:05:29 +080095 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010096}
97
Lawrence Tangd34f2b12022-07-19 15:36:31 +010098//Prints command help for this CPER generator.
99void print_help()
100{
Lawrence Tang617949e2022-08-08 14:21:42 +0100101 printf(":: --out cper.file [--sections section1 ...] [--single-section sectiontype]\n");
102 printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n\n");
103 printf("\tWhen the '--sections' flag is set, all following arguments are section names, and a full CPER log is generated\n");
104 printf("\tcontaining the given sections.\n");
105 printf("\tWhen the '--single-section' flag is set, the next argument is the single section that should be generated, and\n");
106 printf("\ta single section (no header, only a section descriptor & section) CPER file is generated.\n\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +0100107 printf("\tValid section type names are the following:\n");
John Chungf8fc7052024-05-03 20:05:29 +0800108 for (size_t i = 0; i < generator_definitions_len; i++) {
Lawrence Tangef9a3252022-08-24 16:03:18 +0100109 printf("\t\t- %s\n", generator_definitions[i].ShortName);
110 }
Lawrence Tange407b4c2022-07-21 13:54:01 +0100111 printf("\t\t- unknown\n");
112 printf("\n:: --help\n");
113 printf("\tDisplays help information to the console.\n");
John Chungf8fc7052024-05-03 20:05:29 +0800114}