Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 1 | /** |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 2 | * A user-space application for generating pseudo-random specification compliant CPER records. |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 3 | * |
| 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> |
| 10 | #include "../edk/Cper.h" |
| 11 | #include "cper-generate.h" |
| 12 | |
| 13 | void print_help(); |
| 14 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 15 | int main(int argc, char *argv[]) |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 16 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 17 | //If help requested, print help. |
| 18 | if (argc == 2 && strcmp(argv[1], "--help") == 0) { |
| 19 | print_help(); |
| 20 | return 0; |
| 21 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 22 | |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 23 | //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 Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 50 | return -1; |
| 51 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 52 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 53 | //Open a file handle to write output. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 54 | FILE *cper_file = fopen(out_file, "w"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 55 | if (cper_file == NULL) { |
| 56 | 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] | 57 | out_file); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 58 | return -1; |
| 59 | } |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 60 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 61 | //Generate the record. Type names start from argv[4]. |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 62 | generate_cper_record(sections, num_sections, cper_file); |
| 63 | |
| 64 | //Close & free remaining resources. |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 65 | fclose(cper_file); |
Lawrence Tang | 5f388a3 | 2022-08-08 10:49:02 +0100 | [diff] [blame] | 66 | if (sections != NULL) |
| 67 | free(sections); |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Lawrence Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 70 | //Prints command help for this CPER generator. |
| 71 | void print_help() |
| 72 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 73 | printf(":: --out cper.file --sections section1 [section2 section3 ...]\n"); |
Lawrence Tang | efe17e2 | 2022-08-08 09:16:23 +0100 | [diff] [blame] | 74 | printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n"); |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 75 | 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 Tang | d34f2b1 | 2022-07-19 15:36:31 +0100 | [diff] [blame] | 95 | } |