blob: 7bbf380309c7475c42e5f90929a208df5af97193 [file] [log] [blame]
Patrick Venturebf58cd62018-12-11 09:05:46 -08001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "updater.hpp"
18
19/* Use CLI11 argument parser once in openbmc/meta-oe or whatever. */
20#include <getopt.h>
21
22#include <cstdio>
23#include <string>
24
25void usage(const char* program)
26{
27 std::fprintf(stderr,
28 "Usage: %s -command <command> -interface <interface> -image "
29 "<image file> -sig <signature file>",
30 program);
31}
32
33bool checkCommand(const std::string& command)
34{
35 return (command == "update");
36}
37
38bool checkInterface(const std::string& interface)
39{
40 return (interface == "ipmibt" || interface == "ipmilpc");
41}
42
43int main(int argc, char* argv[])
44{
45 std::string command, interface, imagePath, signaturePath;
46
47 while (1)
48 {
49 // clang-format off
50 static struct option long_options[] = {
51 {"command", required_argument, 0, 'c'},
52 {"interface", required_argument, 0, 'i'},
53 {"image", required_argument, 0, 'm'},
54 {"sig", required_argument, 0, 's'},
55 {0, 0, 0, 0}
56 };
57 // clang-format on
58
59 int option_index = 0;
60 int c =
61 getopt_long(argc, argv, "c:i:m:s:", long_options, &option_index);
62 if (c == -1)
63 {
64 break;
65 }
66
67 switch (c)
68 {
69 case 'c':
70 command = std::string{optarg};
71 if (!checkCommand(command))
72 {
73 usage(argv[0]);
74 exit(EXIT_FAILURE);
75 }
76
77 break;
78 case 'i':
79 interface = std::string{optarg};
80 if (!checkInterface(interface))
81 {
82 usage(argv[0]);
83 exit(EXIT_FAILURE);
84 }
85 break;
86 case 'm':
87 imagePath = std::string{optarg};
88 break;
89 case 's':
90 signaturePath = std::string{optarg};
91 break;
92 default:
93 usage(argv[0]);
94 exit(EXIT_FAILURE);
95 }
96 }
97
98 /* They want to update the firmware. */
99 if (command == "update")
100 {
101 if (interface.empty() || imagePath.empty() || signaturePath.empty())
102 {
103 usage(argv[0]);
104 exit(EXIT_FAILURE);
105 }
106
107 /* The parameters are all filled out. */
108 return updaterMain(interface, imagePath, signaturePath);
109 }
110
111 return 0;
112}