blob: f5b0c8560846faa6ecf84a383a3cf103bb74966a [file] [log] [blame]
Lawrence Tangd34f2b12022-07-19 15:36:31 +01001/**
Lawrence Tangefe17e22022-08-08 09:16:23 +01002 * A user-space application for generating pseudo-random specification compliant CPER records.
Lawrence Tangd34f2b12022-07-19 15:36:31 +01003 *
4 * 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>
10#include "../edk/Cper.h"
11#include "cper-generate.h"
Lawrence Tangef9a3252022-08-24 16:03:18 +010012#include "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;
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 Tang617949e2022-08-08 14:21:42 +010033 } else if (strcmp(argv[i], "--single-section") == 0 &&
34 i < argc - 1) {
35 single_section = argv[i + 1];
36 i++;
Lawrence Tang5f388a32022-08-08 10:49:02 +010037 } 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 Tang617949e2022-08-08 14:21:42 +010042
Lawrence Tang5f388a32022-08-08 10:49:02 +010043 for (int j = i; j < argc; j++)
44 sections[j - i] = argv[j];
45 break;
46 } else {
47 printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n",
48 argv[i]);
49 return -1;
50 }
51 }
52
53 //If no output file passed as argument, exit.
54 if (out_file == NULL) {
55 printf("No output file provided. For command information, refer to 'cper-generate --help'.\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +010056 return -1;
57 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010058
Lawrence Tange407b4c2022-07-21 13:54:01 +010059 //Open a file handle to write output.
Lawrence Tang5f388a32022-08-08 10:49:02 +010060 FILE *cper_file = fopen(out_file, "w");
Lawrence Tange407b4c2022-07-21 13:54:01 +010061 if (cper_file == NULL) {
62 printf("Could not get a handle for output file '%s', file handle returned null.\n",
Lawrence Tang5f388a32022-08-08 10:49:02 +010063 out_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +010064 return -1;
65 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010066
Lawrence Tang617949e2022-08-08 14:21:42 +010067 //Which type are we generating?
68 if (single_section != NULL && sections == NULL) {
69 generate_single_section_record(single_section, cper_file);
70 } else if (sections != NULL && single_section == NULL) {
71 generate_cper_record(sections, num_sections, cper_file);
72 } else {
73 //Invalid arguments.
74 printf("Invalid argument. Either both '--sections' and '--single-section' were set, or neither. For command information, refer to 'cper-generate --help'.\n");
75 return -1;
76 }
Lawrence Tang5f388a32022-08-08 10:49:02 +010077
78 //Close & free remaining resources.
Lawrence Tange407b4c2022-07-21 13:54:01 +010079 fclose(cper_file);
Lawrence Tang5f388a32022-08-08 10:49:02 +010080 if (sections != NULL)
81 free(sections);
Lawrence Tangd34f2b12022-07-19 15:36:31 +010082}
83
Lawrence Tangd34f2b12022-07-19 15:36:31 +010084//Prints command help for this CPER generator.
85void print_help()
86{
Lawrence Tang617949e2022-08-08 14:21:42 +010087 printf(":: --out cper.file [--sections section1 ...] [--single-section sectiontype]\n");
88 printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n\n");
89 printf("\tWhen the '--sections' flag is set, all following arguments are section names, and a full CPER log is generated\n");
90 printf("\tcontaining the given sections.\n");
91 printf("\tWhen the '--single-section' flag is set, the next argument is the single section that should be generated, and\n");
92 printf("\ta single section (no header, only a section descriptor & section) CPER file is generated.\n\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +010093 printf("\tValid section type names are the following:\n");
Lawrence Tangef9a3252022-08-24 16:03:18 +010094 for (int i=0; i<generator_definitions_len; i++) {
95 printf("\t\t- %s\n", generator_definitions[i].ShortName);
96 }
Lawrence Tange407b4c2022-07-21 13:54:01 +010097 printf("\t\t- unknown\n");
98 printf("\n:: --help\n");
99 printf("\tDisplays help information to the console.\n");
Lawrence Tangd34f2b12022-07-19 15:36:31 +0100100}