blob: 3b9760c920f38a0db89d3c8c81fb0b06d42bc5e2 [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
Patrick Venturea6586362018-12-11 18:47:13 -080017#include "bt.hpp"
Patrick Venture46bdadc2019-01-18 09:04:16 -080018#include "io.hpp"
Patrick Venturea6586362018-12-11 18:47:13 -080019#include "lpc.hpp"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070020#include "p2a.hpp"
21#include "pci.hpp"
Patrick Venture2bc23fe2018-12-13 10:16:36 -080022#include "tool_errors.hpp"
Patrick Venturebf58cd62018-12-11 09:05:46 -080023#include "updater.hpp"
24
25/* Use CLI11 argument parser once in openbmc/meta-oe or whatever. */
26#include <getopt.h>
27
Patrick Venturea6586362018-12-11 18:47:13 -080028#include <algorithm>
Patrick Venturebf58cd62018-12-11 09:05:46 -080029#include <cstdio>
Patrick Ventureaa107a62018-12-12 15:16:25 -080030#include <iostream>
Patrick Venture664c5bc2019-03-07 08:09:45 -080031#include <ipmiblob/blob_handler.hpp>
32#include <ipmiblob/ipmi_handler.hpp>
Patrick Ventureaa107a62018-12-12 15:16:25 -080033#include <iterator>
Patrick Venture00887592018-12-11 10:57:06 -080034#include <memory>
Patrick Venturebf58cd62018-12-11 09:05:46 -080035#include <string>
Patrick Venturea6586362018-12-11 18:47:13 -080036#include <vector>
37
38#define IPMILPC "ipmilpc"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070039#define IPMIPCI "ipmipci"
Patrick Venturea6586362018-12-11 18:47:13 -080040#define IPMIBT "ipmibt"
41
42namespace
43{
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070044const std::vector<std::string> interfaceList = {IPMIBT, IPMILPC, IPMIPCI};
Patrick Venturea6586362018-12-11 18:47:13 -080045}
Patrick Venturebf58cd62018-12-11 09:05:46 -080046
47void usage(const char* program)
48{
Patrick Venture7ae5dde2018-12-14 10:03:35 -080049 std::fprintf(
50 stderr,
51 "Usage: %s --command <command> --interface <interface> --image "
52 "<image file> --sig <signature file>\n",
53 program);
Patrick Venturea6586362018-12-11 18:47:13 -080054
Patrick Venture7ae5dde2018-12-14 10:03:35 -080055 std::fprintf(stderr, "interfaces: ");
Patrick Ventureaa107a62018-12-12 15:16:25 -080056 std::copy(interfaceList.begin(), interfaceList.end(),
57 std::ostream_iterator<std::string>(std::cerr, ", "));
58 std::fprintf(stderr, "\n");
Patrick Venturebf58cd62018-12-11 09:05:46 -080059}
60
61bool checkCommand(const std::string& command)
62{
63 return (command == "update");
64}
65
66bool checkInterface(const std::string& interface)
67{
Patrick Venturea6586362018-12-11 18:47:13 -080068 auto intf =
69 std::find(interfaceList.begin(), interfaceList.end(), interface);
70 return (intf != interfaceList.end());
Patrick Venturebf58cd62018-12-11 09:05:46 -080071}
72
73int main(int argc, char* argv[])
74{
75 std::string command, interface, imagePath, signaturePath;
76
77 while (1)
78 {
79 // clang-format off
80 static struct option long_options[] = {
81 {"command", required_argument, 0, 'c'},
82 {"interface", required_argument, 0, 'i'},
83 {"image", required_argument, 0, 'm'},
84 {"sig", required_argument, 0, 's'},
85 {0, 0, 0, 0}
86 };
87 // clang-format on
88
89 int option_index = 0;
90 int c =
91 getopt_long(argc, argv, "c:i:m:s:", long_options, &option_index);
92 if (c == -1)
93 {
94 break;
95 }
96
97 switch (c)
98 {
99 case 'c':
100 command = std::string{optarg};
101 if (!checkCommand(command))
102 {
103 usage(argv[0]);
104 exit(EXIT_FAILURE);
105 }
106
107 break;
108 case 'i':
109 interface = std::string{optarg};
110 if (!checkInterface(interface))
111 {
112 usage(argv[0]);
113 exit(EXIT_FAILURE);
114 }
115 break;
116 case 'm':
117 imagePath = std::string{optarg};
118 break;
119 case 's':
120 signaturePath = std::string{optarg};
121 break;
122 default:
123 usage(argv[0]);
124 exit(EXIT_FAILURE);
125 }
126 }
127
Patrick Venture361bd5a2018-12-14 09:49:47 -0800128 if (command.empty())
129 {
130 usage(argv[0]);
131 exit(EXIT_FAILURE);
132 }
133
Patrick Venturebf58cd62018-12-11 09:05:46 -0800134 /* They want to update the firmware. */
135 if (command == "update")
136 {
137 if (interface.empty() || imagePath.empty() || signaturePath.empty())
138 {
139 usage(argv[0]);
140 exit(EXIT_FAILURE);
141 }
142
Patrick Venture664c5bc2019-03-07 08:09:45 -0800143 ipmiblob::IpmiHandler ipmi;
144 ipmiblob::BlobHandler blob(&ipmi);
Patrick Venture46bdadc2019-01-18 09:04:16 -0800145 host_tool::DevMemDevice devmem;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700146 host_tool::PciUtilImpl pci;
Patrick Venture00887592018-12-11 10:57:06 -0800147
Patrick Venture9b534f02018-12-13 16:10:02 -0800148 std::unique_ptr<host_tool::DataInterface> handler;
Patrick Venturea6586362018-12-11 18:47:13 -0800149
150 /* Input has already been validated in this case. */
151 if (interface == IPMIBT)
152 {
Patrick Venture9b534f02018-12-13 16:10:02 -0800153 handler = std::make_unique<host_tool::BtDataHandler>(&blob);
Patrick Venturea6586362018-12-11 18:47:13 -0800154 }
155 else if (interface == IPMILPC)
156 {
Patrick Venture46bdadc2019-01-18 09:04:16 -0800157 handler =
158 std::make_unique<host_tool::LpcDataHandler>(&blob, &devmem);
Patrick Venturea6586362018-12-11 18:47:13 -0800159 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700160 else if (interface == IPMIPCI)
161 {
162 handler = std::make_unique<host_tool::P2aDataHandler>(
163 &blob, &devmem, &pci);
164 }
Patrick Venturea6586362018-12-11 18:47:13 -0800165
166 if (!handler)
167 {
168 /* TODO(venture): use a custom exception. */
169 std::fprintf(stderr, "Interface %s is unavailable\n",
170 interface.c_str());
171 exit(EXIT_FAILURE);
172 }
173
Patrick Venturebf58cd62018-12-11 09:05:46 -0800174 /* The parameters are all filled out. */
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800175 try
176 {
Patrick Venture9b534f02018-12-13 16:10:02 -0800177 host_tool::updaterMain(&blob, handler.get(), imagePath,
178 signaturePath);
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800179 }
Patrick Venture9b534f02018-12-13 16:10:02 -0800180 catch (const host_tool::ToolException& e)
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800181 {
182 std::fprintf(stderr, "Exception received: %s\n", e.what());
183 return -1;
184 }
Patrick Venturebf58cd62018-12-11 09:05:46 -0800185 }
186
187 return 0;
188}