1d34f2b11SLawrence Tang /** 2efe17e2cSLawrence Tang * A user-space application for generating pseudo-random specification compliant CPER records. 3d34f2b11SLawrence Tang * 4d34f2b11SLawrence Tang * Author: Lawrence.Tang@arm.com 5d34f2b11SLawrence Tang **/ 6d34f2b11SLawrence Tang 7d34f2b11SLawrence Tang #include <stdio.h> 85f388a3fSLawrence Tang #include <stdlib.h> 9d34f2b11SLawrence Tang #include <string.h> 10d34f2b11SLawrence Tang #include "../edk/Cper.h" 11d34f2b11SLawrence Tang #include "cper-generate.h" 12*ef9a325fSLawrence Tang #include "sections/gen-section.h" 13d34f2b11SLawrence Tang 14d34f2b11SLawrence Tang void print_help(); 15d34f2b11SLawrence Tang 16d34f2b11SLawrence Tang int main(int argc, char *argv[]) 17d34f2b11SLawrence Tang { 18d34f2b11SLawrence Tang //If help requested, print help. 19e407b4c8SLawrence Tang if (argc == 2 && strcmp(argv[1], "--help") == 0) { 20d34f2b11SLawrence Tang print_help(); 21d34f2b11SLawrence Tang return 0; 22d34f2b11SLawrence Tang } 23d34f2b11SLawrence Tang 245f388a3fSLawrence Tang //Parse the command line arguments. 255f388a3fSLawrence Tang char *out_file = NULL; 26617949e4SLawrence Tang char *single_section = NULL; 275f388a3fSLawrence Tang char **sections = NULL; 285f388a3fSLawrence Tang UINT16 num_sections = 0; 295f388a3fSLawrence Tang for (int i = 1; i < argc; i++) { 305f388a3fSLawrence Tang if (strcmp(argv[i], "--out") == 0 && i < argc - 1) { 315f388a3fSLawrence Tang out_file = argv[i + 1]; 325f388a3fSLawrence Tang i++; 33617949e4SLawrence Tang } else if (strcmp(argv[i], "--single-section") == 0 && 34617949e4SLawrence Tang i < argc - 1) { 35617949e4SLawrence Tang single_section = argv[i + 1]; 36617949e4SLawrence Tang i++; 375f388a3fSLawrence Tang } else if (strcmp(argv[i], "--sections") == 0 && i < argc - 1) { 385f388a3fSLawrence Tang //All arguments after this must be section names. 395f388a3fSLawrence Tang num_sections = argc - i - 1; 405f388a3fSLawrence Tang sections = malloc(sizeof(char *) * num_sections); 415f388a3fSLawrence Tang i++; 425f388a3fSLawrence Tang 435f388a3fSLawrence Tang for (int j = i; j < argc; j++) 445f388a3fSLawrence Tang sections[j - i] = argv[j]; 455f388a3fSLawrence Tang break; 465f388a3fSLawrence Tang } else { 475f388a3fSLawrence Tang printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n", 485f388a3fSLawrence Tang argv[i]); 495f388a3fSLawrence Tang return -1; 505f388a3fSLawrence Tang } 515f388a3fSLawrence Tang } 525f388a3fSLawrence Tang 535f388a3fSLawrence Tang //If no output file passed as argument, exit. 545f388a3fSLawrence Tang if (out_file == NULL) { 555f388a3fSLawrence Tang printf("No output file provided. For command information, refer to 'cper-generate --help'.\n"); 56d34f2b11SLawrence Tang return -1; 57d34f2b11SLawrence Tang } 58d34f2b11SLawrence Tang 59d34f2b11SLawrence Tang //Open a file handle to write output. 605f388a3fSLawrence Tang FILE *cper_file = fopen(out_file, "w"); 61e407b4c8SLawrence Tang if (cper_file == NULL) { 62e407b4c8SLawrence Tang printf("Could not get a handle for output file '%s', file handle returned null.\n", 635f388a3fSLawrence Tang out_file); 64d34f2b11SLawrence Tang return -1; 65d34f2b11SLawrence Tang } 66d34f2b11SLawrence Tang 67617949e4SLawrence Tang //Which type are we generating? 68617949e4SLawrence Tang if (single_section != NULL && sections == NULL) { 69617949e4SLawrence Tang generate_single_section_record(single_section, cper_file); 70617949e4SLawrence Tang } else if (sections != NULL && single_section == NULL) { 715f388a3fSLawrence Tang generate_cper_record(sections, num_sections, cper_file); 72617949e4SLawrence Tang } else { 73617949e4SLawrence Tang //Invalid arguments. 74617949e4SLawrence Tang printf("Invalid argument. Either both '--sections' and '--single-section' were set, or neither. For command information, refer to 'cper-generate --help'.\n"); 75617949e4SLawrence Tang return -1; 76617949e4SLawrence Tang } 775f388a3fSLawrence Tang 785f388a3fSLawrence Tang //Close & free remaining resources. 79d34f2b11SLawrence Tang fclose(cper_file); 805f388a3fSLawrence Tang if (sections != NULL) 815f388a3fSLawrence Tang free(sections); 82d34f2b11SLawrence Tang } 83d34f2b11SLawrence Tang 84d34f2b11SLawrence Tang //Prints command help for this CPER generator. 85d34f2b11SLawrence Tang void print_help() 86d34f2b11SLawrence Tang { 87617949e4SLawrence Tang printf(":: --out cper.file [--sections section1 ...] [--single-section sectiontype]\n"); 88617949e4SLawrence Tang printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n\n"); 89617949e4SLawrence Tang printf("\tWhen the '--sections' flag is set, all following arguments are section names, and a full CPER log is generated\n"); 90617949e4SLawrence Tang printf("\tcontaining the given sections.\n"); 91617949e4SLawrence Tang printf("\tWhen the '--single-section' flag is set, the next argument is the single section that should be generated, and\n"); 92617949e4SLawrence Tang printf("\ta single section (no header, only a section descriptor & section) CPER file is generated.\n\n"); 93d34f2b11SLawrence Tang printf("\tValid section type names are the following:\n"); 94*ef9a325fSLawrence Tang for (int i=0; i<generator_definitions_len; i++) { 95*ef9a325fSLawrence Tang printf("\t\t- %s\n", generator_definitions[i].ShortName); 96*ef9a325fSLawrence Tang } 97d34f2b11SLawrence Tang printf("\t\t- unknown\n"); 98d34f2b11SLawrence Tang printf("\n:: --help\n"); 99d34f2b11SLawrence Tang printf("\tDisplays help information to the console.\n"); 100d34f2b11SLawrence Tang }