blob: 0f0399b5db42e3d5168b7257c4e2f3203773dd7b [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"
12
13void print_help();
14
Lawrence Tange407b4c2022-07-21 13:54:01 +010015int main(int argc, char *argv[])
Lawrence Tangd34f2b12022-07-19 15:36:31 +010016{
Lawrence Tange407b4c2022-07-21 13:54:01 +010017 //If help requested, print help.
18 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
19 print_help();
20 return 0;
21 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010022
Lawrence Tang5f388a32022-08-08 10:49:02 +010023 //Parse the command line arguments.
24 char *out_file = NULL;
25 char **sections = NULL;
26 UINT16 num_sections = 0;
27 for (int i = 1; i < argc; i++) {
28 if (strcmp(argv[i], "--out") == 0 && i < argc - 1) {
29 out_file = argv[i + 1];
30 i++;
31 } else if (strcmp(argv[i], "--sections") == 0 && i < argc - 1) {
32 //All arguments after this must be section names.
33 num_sections = argc - i - 1;
34 sections = malloc(sizeof(char *) * num_sections);
35 i++;
36
37 for (int j = i; j < argc; j++)
38 sections[j - i] = argv[j];
39 break;
40 } else {
41 printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n",
42 argv[i]);
43 return -1;
44 }
45 }
46
47 //If no output file passed as argument, exit.
48 if (out_file == NULL) {
49 printf("No output file provided. For command information, refer to 'cper-generate --help'.\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +010050 return -1;
51 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010052
Lawrence Tange407b4c2022-07-21 13:54:01 +010053 //Open a file handle to write output.
Lawrence Tang5f388a32022-08-08 10:49:02 +010054 FILE *cper_file = fopen(out_file, "w");
Lawrence Tange407b4c2022-07-21 13:54:01 +010055 if (cper_file == NULL) {
56 printf("Could not get a handle for output file '%s', file handle returned null.\n",
Lawrence Tang5f388a32022-08-08 10:49:02 +010057 out_file);
Lawrence Tange407b4c2022-07-21 13:54:01 +010058 return -1;
59 }
Lawrence Tangd34f2b12022-07-19 15:36:31 +010060
Lawrence Tange407b4c2022-07-21 13:54:01 +010061 //Generate the record. Type names start from argv[4].
Lawrence Tang5f388a32022-08-08 10:49:02 +010062 generate_cper_record(sections, num_sections, cper_file);
63
64 //Close & free remaining resources.
Lawrence Tange407b4c2022-07-21 13:54:01 +010065 fclose(cper_file);
Lawrence Tang5f388a32022-08-08 10:49:02 +010066 if (sections != NULL)
67 free(sections);
Lawrence Tangd34f2b12022-07-19 15:36:31 +010068}
69
Lawrence Tangd34f2b12022-07-19 15:36:31 +010070//Prints command help for this CPER generator.
71void print_help()
72{
Lawrence Tange407b4c2022-07-21 13:54:01 +010073 printf(":: --out cper.file --sections section1 [section2 section3 ...]\n");
Lawrence Tangefe17e22022-08-08 09:16:23 +010074 printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n");
Lawrence Tange407b4c2022-07-21 13:54:01 +010075 printf("\tValid section type names are the following:\n");
76 printf("\t\t- generic\n");
77 printf("\t\t- ia32x64\n");
78 printf("\t\t- ipf\n");
79 printf("\t\t- arm\n");
80 printf("\t\t- memory\n");
81 printf("\t\t- memory2\n");
82 printf("\t\t- pcie\n");
83 printf("\t\t- firmware\n");
84 printf("\t\t- pcibus\n");
85 printf("\t\t- pcidev\n");
86 printf("\t\t- dmargeneric\n");
87 printf("\t\t- dmarvtd\n");
88 printf("\t\t- dmariommu\n");
89 printf("\t\t- ccixper\n");
90 printf("\t\t- cxlprotocol\n");
91 printf("\t\t- cxlcomponent\n");
92 printf("\t\t- unknown\n");
93 printf("\n:: --help\n");
94 printf("\tDisplays help information to the console.\n");
Lawrence Tangd34f2b12022-07-19 15:36:31 +010095}