Debian Codegraph @

util/amdtools/ec_usb_pd_fw/main.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <unistd.h>
6 #include <stdbool.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "rom.h"
13
14 static const char *USAGE_FMT = "Usage: %s <rom-file> <command> [arguments]\n"
15 " -h|--help\n"
16 " -f|--fmap_region_name FMAP region\n"
17 " -s|--slot [0-11] Specify slot to use\n"
18 " -n|--next_free Use next free slot\n\n"
19 "Command:\n"
20 " GEN_PDFW_PTR - Writes the USB Type-C PD firmware PTR\n";
21
22 static const char *program_name;
23
24 static void print_program_usage(void)
25 {
26 fprintf(stderr, USAGE_FMT, program_name);
27 exit(EXIT_FAILURE);
28 }
29
30 #define ERROR(...) do { fprintf(stderr, "ERROR: " __VA_ARGS__); print_program_usage(); } while(0)
31
32 static void print_help(void)
33 {
34 printf(USAGE_FMT, program_name);
35 }
36
37 static const struct option longopts[] = {
38 {"help", 0, 0, 'h'},
39 {"fmap_region_name", 1, 0, 'f'},
40 {"next_free", 0, 0, 'n'},
41 {"slot", 1, 0, 's'},
42 {NULL, 0, 0, 0}
43 };
44
45 int main(int argc, char *argv[])
46 {
47 char *fmap_region_name = NULL;
48 const char *rom_file = NULL;
49 const char *command = NULL;
50 int slot = -1;
51 bool next_free = false;
52
53 int opt, idx, ret = 0;
54 program_name = argv[0];
55
56 if (argc < 3)
57 ERROR("Not enough argument given\n\n");
58
59 while (!ret &&
60 (opt = getopt_long(argc, argv, "?hf:s:n",
61 longopts, &idx)) != -1) {
62 switch (opt) {
63 case 'h':
64 case '?':
65 print_help();
66 exit(EXIT_SUCCESS);
67 break;
68 case 'n':
69 next_free = true;
70 break;
71 case 'f':
72 fmap_region_name = strdup(optarg);
73 break;
74 case 's':
75 slot = strtol(optarg, NULL, 0);
76 break;
77 }
78 }
79 if (optind < argc) {
80 rom_file = argv[optind];
81 optind++;
82 }
83 if (optind < argc) {
84 command = argv[optind];
85 optind++;
86 }
87
88 if (optind < argc)
89 ERROR("Unknown argument: %s\n\n", argv[optind]);
90
91 if (!rom_file)
92 ERROR("No ROM specified\n\n");
93
94 if (!command) {
95 ERROR("No command specified\n\n");
96 } else if (!strcmp(command, "GEN_PDFW_PTR")) {
97 if (!fmap_region_name)
98 ERROR("No fmap region specified\n\n");
99 if (next_free && slot >= 0)
100 ERROR("Specified slot and auto allocation\n\n");
101 if (!next_free && slot < 0)
102 ERROR("Didn't specify a slot and not auto allocating\n\n");
103 if (slot > 11)
104 ERROR("Invalid slot specified\n\n");
105 if (!rom_set_pd_fw_ptr(rom_file, fmap_region_name, slot, next_free))
106 ERROR("Failed to write EC PD PTR\n\n");
107 } else
108 ERROR("Unknown command specified: '%s'\n\n", command);
109
110 if (fmap_region_name)
111 free(fmap_region_name);
112
113 return ret;
114 }