blob: 3876844491d517fd82c5567af43bc16c9773ea46 [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"
Benjamin Fair30d09a32019-10-11 16:57:47 -070020#include "net.hpp"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070021#include "p2a.hpp"
22#include "pci.hpp"
Benjamin Faire5aafa52020-06-05 21:04:24 -070023#include "pciaccess.hpp"
Patrick Venturecf9b2192019-06-27 12:09:52 -070024#include "progress.hpp"
Patrick Venture2bc23fe2018-12-13 10:16:36 -080025#include "tool_errors.hpp"
Patrick Venturebf58cd62018-12-11 09:05:46 -080026#include "updater.hpp"
27
28/* Use CLI11 argument parser once in openbmc/meta-oe or whatever. */
29#include <getopt.h>
30
Patrick Venture9b37b092020-05-28 20:58:57 -070031#include <ipmiblob/blob_handler.hpp>
32#include <ipmiblob/ipmi_handler.hpp>
33
Patrick Venturea6586362018-12-11 18:47:13 -080034#include <algorithm>
Patrick Venturebf58cd62018-12-11 09:05:46 -080035#include <cstdio>
Patrick Venture8104a522019-06-19 19:04:36 -070036#include <cstdlib>
Patrick Venturee564d5b2019-05-14 12:30:06 -070037#include <exception>
Patrick Ventureaa107a62018-12-12 15:16:25 -080038#include <iostream>
39#include <iterator>
Patrick Venture03db87e2019-06-20 07:56:06 -070040#include <limits>
Patrick Venture00887592018-12-11 10:57:06 -080041#include <memory>
Patrick Venturebf58cd62018-12-11 09:05:46 -080042#include <string>
Patrick Venturea6586362018-12-11 18:47:13 -080043#include <vector>
44
45#define IPMILPC "ipmilpc"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070046#define IPMIPCI "ipmipci"
Patrick Venturea6586362018-12-11 18:47:13 -080047#define IPMIBT "ipmibt"
Benjamin Fair30d09a32019-10-11 16:57:47 -070048#define IPMINET "ipminet"
Patrick Venturea6586362018-12-11 18:47:13 -080049
50namespace
51{
Benjamin Fair30d09a32019-10-11 16:57:47 -070052const std::vector<std::string> interfaceList = {IPMINET, IPMIBT, IPMILPC,
53 IPMIPCI};
Patrick Venture9f937c42019-06-21 07:55:54 -070054} // namespace
Patrick Venturebf58cd62018-12-11 09:05:46 -080055
56void usage(const char* program)
57{
Patrick Venture7ae5dde2018-12-14 10:03:35 -080058 std::fprintf(
59 stderr,
60 "Usage: %s --command <command> --interface <interface> --image "
Brandon Kim6749ba12019-09-19 13:31:37 -070061 "<image file> --sig <signature file> --type <layout> "
62 "[--ignore-update]\n",
Patrick Venture7ae5dde2018-12-14 10:03:35 -080063 program);
Patrick Venturea6586362018-12-11 18:47:13 -080064
Patrick Venture7ae5dde2018-12-14 10:03:35 -080065 std::fprintf(stderr, "interfaces: ");
Patrick Ventureaa107a62018-12-12 15:16:25 -080066 std::copy(interfaceList.begin(), interfaceList.end(),
67 std::ostream_iterator<std::string>(std::cerr, ", "));
68 std::fprintf(stderr, "\n");
Patrick Venture9f937c42019-06-21 07:55:54 -070069
Patrick Venturec498caa2019-08-15 10:12:50 -070070 std::fprintf(stderr, "layouts examples: image, bios\n");
71 std::fprintf(stderr,
72 "the type field specifies '/flash/{layout}' for a handler\n");
Patrick Venturebf58cd62018-12-11 09:05:46 -080073}
74
75bool checkCommand(const std::string& command)
76{
77 return (command == "update");
78}
79
80bool checkInterface(const std::string& interface)
81{
Patrick Venturea6586362018-12-11 18:47:13 -080082 auto intf =
83 std::find(interfaceList.begin(), interfaceList.end(), interface);
84 return (intf != interfaceList.end());
Patrick Venturebf58cd62018-12-11 09:05:46 -080085}
86
87int main(int argc, char* argv[])
88{
Benjamin Fair30d09a32019-10-11 16:57:47 -070089 std::string command, interface, imagePath, signaturePath, type, host;
90 std::string port = "623";
Patrick Venture8104a522019-06-19 19:04:36 -070091 char* valueEnd = nullptr;
92 long address = 0;
93 long length = 0;
Patrick Venture03db87e2019-06-20 07:56:06 -070094 std::uint32_t hostAddress = 0;
95 std::uint32_t hostLength = 0;
Brandon Kim6749ba12019-09-19 13:31:37 -070096 bool ignoreUpdate = false;
Patrick Venturebf58cd62018-12-11 09:05:46 -080097
98 while (1)
99 {
100 // clang-format off
101 static struct option long_options[] = {
102 {"command", required_argument, 0, 'c'},
103 {"interface", required_argument, 0, 'i'},
104 {"image", required_argument, 0, 'm'},
105 {"sig", required_argument, 0, 's'},
Patrick Venture8104a522019-06-19 19:04:36 -0700106 {"address", required_argument, 0, 'a'},
107 {"length", required_argument, 0, 'l'},
Patrick Venture9f937c42019-06-21 07:55:54 -0700108 {"type", required_argument, 0, 't'},
Brandon Kim6749ba12019-09-19 13:31:37 -0700109 {"ignore-update", no_argument, 0, 'u'},
Benjamin Fair30d09a32019-10-11 16:57:47 -0700110 {"host", required_argument, 0, 'H'},
111 {"port", optional_argument, 0, 'p'},
Patrick Venturebf58cd62018-12-11 09:05:46 -0800112 {0, 0, 0, 0}
113 };
114 // clang-format on
115
116 int option_index = 0;
Benjamin Fair30d09a32019-10-11 16:57:47 -0700117 int c = getopt_long(argc, argv, "c:i:m:s:a:l:t:uH:p:", long_options,
Patrick Venture9f937c42019-06-21 07:55:54 -0700118 &option_index);
Patrick Venturebf58cd62018-12-11 09:05:46 -0800119 if (c == -1)
120 {
121 break;
122 }
123
124 switch (c)
125 {
126 case 'c':
127 command = std::string{optarg};
128 if (!checkCommand(command))
129 {
130 usage(argv[0]);
131 exit(EXIT_FAILURE);
132 }
133
134 break;
135 case 'i':
136 interface = std::string{optarg};
137 if (!checkInterface(interface))
138 {
139 usage(argv[0]);
140 exit(EXIT_FAILURE);
141 }
142 break;
143 case 'm':
144 imagePath = std::string{optarg};
145 break;
146 case 's':
147 signaturePath = std::string{optarg};
148 break;
Patrick Venture8104a522019-06-19 19:04:36 -0700149 case 'a':
150 address = std::strtol(&optarg[0], &valueEnd, 0);
151 if (valueEnd == nullptr)
152 {
153 usage(argv[0]);
154 exit(EXIT_FAILURE);
155 }
Patrick Venture03db87e2019-06-20 07:56:06 -0700156 if (address > std::numeric_limits<std::uint32_t>::max())
157 {
158 std::fprintf(stderr, "Address beyond 32-bit limit.\n");
159 usage(argv[0]);
160 exit(EXIT_FAILURE);
161 }
162 hostAddress = static_cast<std::uint32_t>(address);
Patrick Venture8104a522019-06-19 19:04:36 -0700163 break;
164 case 'l':
165 length = std::strtol(&optarg[0], &valueEnd, 0);
166 if (valueEnd == nullptr)
167 {
168 usage(argv[0]);
169 exit(EXIT_FAILURE);
170 }
Patrick Venture03db87e2019-06-20 07:56:06 -0700171 if (length > std::numeric_limits<std::uint32_t>::max())
172 {
173 std::fprintf(stderr, "Length beyond 32-bit limit.\n");
174 usage(argv[0]);
175 exit(EXIT_FAILURE);
176 }
177 hostLength = static_cast<std::uint32_t>(length);
Patrick Venture8104a522019-06-19 19:04:36 -0700178 break;
Patrick Venture9f937c42019-06-21 07:55:54 -0700179 case 't':
180 type = std::string{optarg};
Patrick Venture9f937c42019-06-21 07:55:54 -0700181 break;
Brandon Kim6749ba12019-09-19 13:31:37 -0700182 case 'u':
183 ignoreUpdate = true;
184 break;
Benjamin Fair30d09a32019-10-11 16:57:47 -0700185 case 'H':
186 host = std::string{optarg};
187 break;
188 case 'p':
189 port = std::string{optarg};
190 break;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800191 default:
192 usage(argv[0]);
193 exit(EXIT_FAILURE);
194 }
195 }
196
Patrick Venture361bd5a2018-12-14 09:49:47 -0800197 if (command.empty())
198 {
199 usage(argv[0]);
200 exit(EXIT_FAILURE);
201 }
202
Patrick Venturebf58cd62018-12-11 09:05:46 -0800203 /* They want to update the firmware. */
204 if (command == "update")
205 {
Patrick Venturec498caa2019-08-15 10:12:50 -0700206 if (interface.empty() || imagePath.empty() || signaturePath.empty() ||
207 type.empty())
Patrick Venturebf58cd62018-12-11 09:05:46 -0800208 {
209 usage(argv[0]);
210 exit(EXIT_FAILURE);
211 }
212
Patrick Venture866d4482019-05-13 09:26:52 -0700213 auto ipmi = ipmiblob::IpmiHandler::CreateIpmiHandler();
214 ipmiblob::BlobHandler blob(std::move(ipmi));
Rui Zhangd8515a62020-03-24 16:46:44 -0700215#ifdef ENABLE_PPC
216 const std::string ppcMemPath = "/sys/kernel/debug/powerpc/lpc/fw";
217 host_tool::PpcMemDevice devmem(ppcMemPath);
218#else
Patrick Venture46bdadc2019-01-18 09:04:16 -0800219 host_tool::DevMemDevice devmem;
Rui Zhangd8515a62020-03-24 16:46:44 -0700220#endif
Patrick Venturecf9b2192019-06-27 12:09:52 -0700221 host_tool::ProgressStdoutIndicator progress;
Patrick Venture00887592018-12-11 10:57:06 -0800222
Patrick Venture9b534f02018-12-13 16:10:02 -0800223 std::unique_ptr<host_tool::DataInterface> handler;
Patrick Venturea6586362018-12-11 18:47:13 -0800224
225 /* Input has already been validated in this case. */
226 if (interface == IPMIBT)
227 {
Patrick Venturecf9b2192019-06-27 12:09:52 -0700228 handler =
229 std::make_unique<host_tool::BtDataHandler>(&blob, &progress);
Patrick Venturea6586362018-12-11 18:47:13 -0800230 }
Benjamin Fair30d09a32019-10-11 16:57:47 -0700231 else if (interface == IPMINET)
232 {
233 if (host.empty())
234 {
235 std::fprintf(stderr, "Host not specified\n");
236 exit(EXIT_FAILURE);
237 }
238 handler = std::make_unique<host_tool::NetDataHandler>(
239 &blob, &progress, host, port);
240 }
Patrick Venturea6586362018-12-11 18:47:13 -0800241 else if (interface == IPMILPC)
242 {
Patrick Venture03db87e2019-06-20 07:56:06 -0700243 if (hostAddress == 0 || hostLength == 0)
Patrick Venture8104a522019-06-19 19:04:36 -0700244 {
245 std::fprintf(stderr, "Address or Length were 0\n");
246 exit(EXIT_FAILURE);
247 }
248 handler = std::make_unique<host_tool::LpcDataHandler>(
Patrick Venturecf9b2192019-06-27 12:09:52 -0700249 &blob, &devmem, hostAddress, hostLength, &progress);
Patrick Venturea6586362018-12-11 18:47:13 -0800250 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700251 else if (interface == IPMIPCI)
252 {
Benjamin Faire5aafa52020-06-05 21:04:24 -0700253 auto& pci = host_tool::PciAccessImpl::getInstance();
254 handler = std::make_unique<host_tool::P2aDataHandler>(&blob, &pci,
255 &progress);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700256 }
Patrick Venturea6586362018-12-11 18:47:13 -0800257
258 if (!handler)
259 {
260 /* TODO(venture): use a custom exception. */
261 std::fprintf(stderr, "Interface %s is unavailable\n",
262 interface.c_str());
263 exit(EXIT_FAILURE);
264 }
265
Patrick Venturebf58cd62018-12-11 09:05:46 -0800266 /* The parameters are all filled out. */
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800267 try
268 {
Patrick Venture55646de2019-05-16 10:06:26 -0700269 host_tool::UpdateHandler updater(&blob, handler.get());
Brandon Kim6749ba12019-09-19 13:31:37 -0700270 host_tool::updaterMain(&updater, imagePath, signaturePath, type,
271 ignoreUpdate);
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800272 }
Patrick Venture9b534f02018-12-13 16:10:02 -0800273 catch (const host_tool::ToolException& e)
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800274 {
275 std::fprintf(stderr, "Exception received: %s\n", e.what());
276 return -1;
277 }
Patrick Venturee564d5b2019-05-14 12:30:06 -0700278 catch (const std::exception& e)
279 {
280 std::fprintf(stderr, "Unexpected exception received: %s\n",
281 e.what());
282 return -1;
283 }
Patrick Venturebf58cd62018-12-11 09:05:46 -0800284 }
285
286 return 0;
287}