blob: 0e29295bba963aa1adf624be812fef94b1395f62 [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"
Patrick Venturecf9b2192019-06-27 12:09:52 -070023#include "progress.hpp"
Patrick Venture2bc23fe2018-12-13 10:16:36 -080024#include "tool_errors.hpp"
Patrick Venturebf58cd62018-12-11 09:05:46 -080025#include "updater.hpp"
26
27/* Use CLI11 argument parser once in openbmc/meta-oe or whatever. */
28#include <getopt.h>
29
Patrick Venture9b37b092020-05-28 20:58:57 -070030#include <ipmiblob/blob_handler.hpp>
31#include <ipmiblob/ipmi_handler.hpp>
32
Patrick Venturea6586362018-12-11 18:47:13 -080033#include <algorithm>
Patrick Venturebf58cd62018-12-11 09:05:46 -080034#include <cstdio>
Patrick Venture8104a522019-06-19 19:04:36 -070035#include <cstdlib>
Patrick Venturee564d5b2019-05-14 12:30:06 -070036#include <exception>
Patrick Ventureaa107a62018-12-12 15:16:25 -080037#include <iostream>
38#include <iterator>
Patrick Venture03db87e2019-06-20 07:56:06 -070039#include <limits>
Patrick Venture00887592018-12-11 10:57:06 -080040#include <memory>
Patrick Venturebf58cd62018-12-11 09:05:46 -080041#include <string>
Patrick Venturea6586362018-12-11 18:47:13 -080042#include <vector>
43
44#define IPMILPC "ipmilpc"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070045#define IPMIPCI "ipmipci"
Patrick Venturea6586362018-12-11 18:47:13 -080046#define IPMIBT "ipmibt"
Benjamin Fair30d09a32019-10-11 16:57:47 -070047#define IPMINET "ipminet"
Patrick Venturea6586362018-12-11 18:47:13 -080048
49namespace
50{
Benjamin Fair30d09a32019-10-11 16:57:47 -070051const std::vector<std::string> interfaceList = {IPMINET, IPMIBT, IPMILPC,
52 IPMIPCI};
Patrick Venture9f937c42019-06-21 07:55:54 -070053} // namespace
Patrick Venturebf58cd62018-12-11 09:05:46 -080054
55void usage(const char* program)
56{
Patrick Venture7ae5dde2018-12-14 10:03:35 -080057 std::fprintf(
58 stderr,
59 "Usage: %s --command <command> --interface <interface> --image "
Brandon Kim6749ba12019-09-19 13:31:37 -070060 "<image file> --sig <signature file> --type <layout> "
61 "[--ignore-update]\n",
Patrick Venture7ae5dde2018-12-14 10:03:35 -080062 program);
Patrick Venturea6586362018-12-11 18:47:13 -080063
Patrick Venture7ae5dde2018-12-14 10:03:35 -080064 std::fprintf(stderr, "interfaces: ");
Patrick Ventureaa107a62018-12-12 15:16:25 -080065 std::copy(interfaceList.begin(), interfaceList.end(),
66 std::ostream_iterator<std::string>(std::cerr, ", "));
67 std::fprintf(stderr, "\n");
Patrick Venture9f937c42019-06-21 07:55:54 -070068
Patrick Venturec498caa2019-08-15 10:12:50 -070069 std::fprintf(stderr, "layouts examples: image, bios\n");
70 std::fprintf(stderr,
71 "the type field specifies '/flash/{layout}' for a handler\n");
Patrick Venturebf58cd62018-12-11 09:05:46 -080072}
73
74bool checkCommand(const std::string& command)
75{
76 return (command == "update");
77}
78
79bool checkInterface(const std::string& interface)
80{
Patrick Venturea6586362018-12-11 18:47:13 -080081 auto intf =
82 std::find(interfaceList.begin(), interfaceList.end(), interface);
83 return (intf != interfaceList.end());
Patrick Venturebf58cd62018-12-11 09:05:46 -080084}
85
86int main(int argc, char* argv[])
87{
Benjamin Fair30d09a32019-10-11 16:57:47 -070088 std::string command, interface, imagePath, signaturePath, type, host;
89 std::string port = "623";
Patrick Venture8104a522019-06-19 19:04:36 -070090 char* valueEnd = nullptr;
91 long address = 0;
92 long length = 0;
Patrick Venture03db87e2019-06-20 07:56:06 -070093 std::uint32_t hostAddress = 0;
94 std::uint32_t hostLength = 0;
Brandon Kim6749ba12019-09-19 13:31:37 -070095 bool ignoreUpdate = false;
Patrick Venturebf58cd62018-12-11 09:05:46 -080096
97 while (1)
98 {
99 // clang-format off
100 static struct option long_options[] = {
101 {"command", required_argument, 0, 'c'},
102 {"interface", required_argument, 0, 'i'},
103 {"image", required_argument, 0, 'm'},
104 {"sig", required_argument, 0, 's'},
Patrick Venture8104a522019-06-19 19:04:36 -0700105 {"address", required_argument, 0, 'a'},
106 {"length", required_argument, 0, 'l'},
Patrick Venture9f937c42019-06-21 07:55:54 -0700107 {"type", required_argument, 0, 't'},
Brandon Kim6749ba12019-09-19 13:31:37 -0700108 {"ignore-update", no_argument, 0, 'u'},
Benjamin Fair30d09a32019-10-11 16:57:47 -0700109 {"host", required_argument, 0, 'H'},
110 {"port", optional_argument, 0, 'p'},
Patrick Venturebf58cd62018-12-11 09:05:46 -0800111 {0, 0, 0, 0}
112 };
113 // clang-format on
114
115 int option_index = 0;
Benjamin Fair30d09a32019-10-11 16:57:47 -0700116 int c = getopt_long(argc, argv, "c:i:m:s:a:l:t:uH:p:", long_options,
Patrick Venture9f937c42019-06-21 07:55:54 -0700117 &option_index);
Patrick Venturebf58cd62018-12-11 09:05:46 -0800118 if (c == -1)
119 {
120 break;
121 }
122
123 switch (c)
124 {
125 case 'c':
126 command = std::string{optarg};
127 if (!checkCommand(command))
128 {
129 usage(argv[0]);
130 exit(EXIT_FAILURE);
131 }
132
133 break;
134 case 'i':
135 interface = std::string{optarg};
136 if (!checkInterface(interface))
137 {
138 usage(argv[0]);
139 exit(EXIT_FAILURE);
140 }
141 break;
142 case 'm':
143 imagePath = std::string{optarg};
144 break;
145 case 's':
146 signaturePath = std::string{optarg};
147 break;
Patrick Venture8104a522019-06-19 19:04:36 -0700148 case 'a':
149 address = std::strtol(&optarg[0], &valueEnd, 0);
150 if (valueEnd == nullptr)
151 {
152 usage(argv[0]);
153 exit(EXIT_FAILURE);
154 }
Patrick Venture03db87e2019-06-20 07:56:06 -0700155 if (address > std::numeric_limits<std::uint32_t>::max())
156 {
157 std::fprintf(stderr, "Address beyond 32-bit limit.\n");
158 usage(argv[0]);
159 exit(EXIT_FAILURE);
160 }
161 hostAddress = static_cast<std::uint32_t>(address);
Patrick Venture8104a522019-06-19 19:04:36 -0700162 break;
163 case 'l':
164 length = std::strtol(&optarg[0], &valueEnd, 0);
165 if (valueEnd == nullptr)
166 {
167 usage(argv[0]);
168 exit(EXIT_FAILURE);
169 }
Patrick Venture03db87e2019-06-20 07:56:06 -0700170 if (length > std::numeric_limits<std::uint32_t>::max())
171 {
172 std::fprintf(stderr, "Length beyond 32-bit limit.\n");
173 usage(argv[0]);
174 exit(EXIT_FAILURE);
175 }
176 hostLength = static_cast<std::uint32_t>(length);
Patrick Venture8104a522019-06-19 19:04:36 -0700177 break;
Patrick Venture9f937c42019-06-21 07:55:54 -0700178 case 't':
179 type = std::string{optarg};
Patrick Venture9f937c42019-06-21 07:55:54 -0700180 break;
Brandon Kim6749ba12019-09-19 13:31:37 -0700181 case 'u':
182 ignoreUpdate = true;
183 break;
Benjamin Fair30d09a32019-10-11 16:57:47 -0700184 case 'H':
185 host = std::string{optarg};
186 break;
187 case 'p':
188 port = std::string{optarg};
189 break;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800190 default:
191 usage(argv[0]);
192 exit(EXIT_FAILURE);
193 }
194 }
195
Patrick Venture361bd5a2018-12-14 09:49:47 -0800196 if (command.empty())
197 {
198 usage(argv[0]);
199 exit(EXIT_FAILURE);
200 }
201
Patrick Venturebf58cd62018-12-11 09:05:46 -0800202 /* They want to update the firmware. */
203 if (command == "update")
204 {
Patrick Venturec498caa2019-08-15 10:12:50 -0700205 if (interface.empty() || imagePath.empty() || signaturePath.empty() ||
206 type.empty())
Patrick Venturebf58cd62018-12-11 09:05:46 -0800207 {
208 usage(argv[0]);
209 exit(EXIT_FAILURE);
210 }
211
Patrick Venture866d4482019-05-13 09:26:52 -0700212 auto ipmi = ipmiblob::IpmiHandler::CreateIpmiHandler();
213 ipmiblob::BlobHandler blob(std::move(ipmi));
Rui Zhangd8515a62020-03-24 16:46:44 -0700214#ifdef ENABLE_PPC
215 const std::string ppcMemPath = "/sys/kernel/debug/powerpc/lpc/fw";
216 host_tool::PpcMemDevice devmem(ppcMemPath);
217#else
Patrick Venture46bdadc2019-01-18 09:04:16 -0800218 host_tool::DevMemDevice devmem;
Rui Zhangd8515a62020-03-24 16:46:44 -0700219#endif
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700220 host_tool::PciUtilImpl pci;
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 {
253 handler = std::make_unique<host_tool::P2aDataHandler>(
Patrick Venturecf9b2192019-06-27 12:09:52 -0700254 &blob, &devmem, &pci, &progress);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700255 }
Patrick Venturea6586362018-12-11 18:47:13 -0800256
257 if (!handler)
258 {
259 /* TODO(venture): use a custom exception. */
260 std::fprintf(stderr, "Interface %s is unavailable\n",
261 interface.c_str());
262 exit(EXIT_FAILURE);
263 }
264
Patrick Venturebf58cd62018-12-11 09:05:46 -0800265 /* The parameters are all filled out. */
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800266 try
267 {
Patrick Venture55646de2019-05-16 10:06:26 -0700268 host_tool::UpdateHandler updater(&blob, handler.get());
Brandon Kim6749ba12019-09-19 13:31:37 -0700269 host_tool::updaterMain(&updater, imagePath, signaturePath, type,
270 ignoreUpdate);
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800271 }
Patrick Venture9b534f02018-12-13 16:10:02 -0800272 catch (const host_tool::ToolException& e)
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800273 {
274 std::fprintf(stderr, "Exception received: %s\n", e.what());
275 return -1;
276 }
Patrick Venturee564d5b2019-05-14 12:30:06 -0700277 catch (const std::exception& e)
278 {
279 std::fprintf(stderr, "Unexpected exception received: %s\n",
280 e.what());
281 return -1;
282 }
Patrick Venturebf58cd62018-12-11 09:05:46 -0800283 }
284
285 return 0;
286}