blob: a933e67cf66ccc0feb3a3fbee59d5369f0c7c71d [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 Venturecf9b2192019-06-27 12:09:52 -070022#include "progress.hpp"
Patrick Venture2bc23fe2018-12-13 10:16:36 -080023#include "tool_errors.hpp"
Patrick Venturebf58cd62018-12-11 09:05:46 -080024#include "updater.hpp"
25
26/* Use CLI11 argument parser once in openbmc/meta-oe or whatever. */
27#include <getopt.h>
28
Patrick Venturea6586362018-12-11 18:47:13 -080029#include <algorithm>
Patrick Venturebf58cd62018-12-11 09:05:46 -080030#include <cstdio>
Patrick Venture8104a522019-06-19 19:04:36 -070031#include <cstdlib>
Patrick Venturee564d5b2019-05-14 12:30:06 -070032#include <exception>
Patrick Ventureaa107a62018-12-12 15:16:25 -080033#include <iostream>
Patrick Venture664c5bc2019-03-07 08:09:45 -080034#include <ipmiblob/blob_handler.hpp>
35#include <ipmiblob/ipmi_handler.hpp>
Patrick Ventureaa107a62018-12-12 15:16:25 -080036#include <iterator>
Patrick Venture03db87e2019-06-20 07:56:06 -070037#include <limits>
Patrick Venture00887592018-12-11 10:57:06 -080038#include <memory>
Patrick Venturebf58cd62018-12-11 09:05:46 -080039#include <string>
Patrick Venturea6586362018-12-11 18:47:13 -080040#include <vector>
41
42#define IPMILPC "ipmilpc"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070043#define IPMIPCI "ipmipci"
Patrick Venturea6586362018-12-11 18:47:13 -080044#define IPMIBT "ipmibt"
Patrick Venture9f937c42019-06-21 07:55:54 -070045#define STATIC "static"
46#define UBITAR "ubitar"
Patrick Venturec9792e72019-07-02 07:35:48 -070047#define BIOS "bios"
Patrick Venturea6586362018-12-11 18:47:13 -080048
49namespace
50{
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070051const std::vector<std::string> interfaceList = {IPMIBT, IPMILPC, IPMIPCI};
Patrick Venturec9792e72019-07-02 07:35:48 -070052const std::vector<std::string> typeList = {STATIC, UBITAR, BIOS};
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 "
Patrick Venture9f937c42019-06-21 07:55:54 -070060 "<image file> --sig <signature file> --type <layout>\n",
Patrick Venture7ae5dde2018-12-14 10:03:35 -080061 program);
Patrick Venturea6586362018-12-11 18:47:13 -080062
Patrick Venture7ae5dde2018-12-14 10:03:35 -080063 std::fprintf(stderr, "interfaces: ");
Patrick Ventureaa107a62018-12-12 15:16:25 -080064 std::copy(interfaceList.begin(), interfaceList.end(),
65 std::ostream_iterator<std::string>(std::cerr, ", "));
66 std::fprintf(stderr, "\n");
Patrick Venture9f937c42019-06-21 07:55:54 -070067
68 std::fprintf(stderr, "layouts: ");
69 std::copy(typeList.begin(), typeList.end(),
70 std::ostream_iterator<std::string>(std::cerr, ", "));
71 std::fprintf(stderr, "\n");
72}
73
74bool checkType(const std::string& type)
75{
76 auto tFound = std::find(typeList.begin(), typeList.end(), type);
77 return (tFound != typeList.end());
Patrick Venturebf58cd62018-12-11 09:05:46 -080078}
79
80bool checkCommand(const std::string& command)
81{
82 return (command == "update");
83}
84
85bool checkInterface(const std::string& interface)
86{
Patrick Venturea6586362018-12-11 18:47:13 -080087 auto intf =
88 std::find(interfaceList.begin(), interfaceList.end(), interface);
89 return (intf != interfaceList.end());
Patrick Venturebf58cd62018-12-11 09:05:46 -080090}
91
92int main(int argc, char* argv[])
93{
Patrick Venture9f937c42019-06-21 07:55:54 -070094 std::string command, interface, imagePath, signaturePath, type;
Patrick Venture8104a522019-06-19 19:04:36 -070095 char* valueEnd = nullptr;
96 long address = 0;
97 long length = 0;
Patrick Venture03db87e2019-06-20 07:56:06 -070098 std::uint32_t hostAddress = 0;
99 std::uint32_t hostLength = 0;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800100
101 while (1)
102 {
103 // clang-format off
104 static struct option long_options[] = {
105 {"command", required_argument, 0, 'c'},
106 {"interface", required_argument, 0, 'i'},
107 {"image", required_argument, 0, 'm'},
108 {"sig", required_argument, 0, 's'},
Patrick Venture8104a522019-06-19 19:04:36 -0700109 {"address", required_argument, 0, 'a'},
110 {"length", required_argument, 0, 'l'},
Patrick Venture9f937c42019-06-21 07:55:54 -0700111 {"type", required_argument, 0, 't'},
Patrick Venturebf58cd62018-12-11 09:05:46 -0800112 {0, 0, 0, 0}
113 };
114 // clang-format on
115
116 int option_index = 0;
Patrick Venture9f937c42019-06-21 07:55:54 -0700117 int c = getopt_long(argc, argv, "c:i:m:s:a:l:t:", long_options,
118 &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};
181 if (!checkType(type))
182 {
183 usage(argv[0]);
184 exit(EXIT_FAILURE);
185 }
186 break;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800187 default:
188 usage(argv[0]);
189 exit(EXIT_FAILURE);
190 }
191 }
192
Patrick Venture361bd5a2018-12-14 09:49:47 -0800193 if (command.empty())
194 {
195 usage(argv[0]);
196 exit(EXIT_FAILURE);
197 }
198
Patrick Venturebf58cd62018-12-11 09:05:46 -0800199 /* They want to update the firmware. */
200 if (command == "update")
201 {
202 if (interface.empty() || imagePath.empty() || signaturePath.empty())
203 {
204 usage(argv[0]);
205 exit(EXIT_FAILURE);
206 }
207
Patrick Venture866d4482019-05-13 09:26:52 -0700208 auto ipmi = ipmiblob::IpmiHandler::CreateIpmiHandler();
209 ipmiblob::BlobHandler blob(std::move(ipmi));
Patrick Venture46bdadc2019-01-18 09:04:16 -0800210 host_tool::DevMemDevice devmem;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700211 host_tool::PciUtilImpl pci;
Patrick Venturecf9b2192019-06-27 12:09:52 -0700212 host_tool::ProgressStdoutIndicator progress;
Patrick Venture00887592018-12-11 10:57:06 -0800213
Patrick Venture9b534f02018-12-13 16:10:02 -0800214 std::unique_ptr<host_tool::DataInterface> handler;
Patrick Venturea6586362018-12-11 18:47:13 -0800215
216 /* Input has already been validated in this case. */
217 if (interface == IPMIBT)
218 {
Patrick Venturecf9b2192019-06-27 12:09:52 -0700219 handler =
220 std::make_unique<host_tool::BtDataHandler>(&blob, &progress);
Patrick Venturea6586362018-12-11 18:47:13 -0800221 }
222 else if (interface == IPMILPC)
223 {
Patrick Venture03db87e2019-06-20 07:56:06 -0700224 if (hostAddress == 0 || hostLength == 0)
Patrick Venture8104a522019-06-19 19:04:36 -0700225 {
226 std::fprintf(stderr, "Address or Length were 0\n");
227 exit(EXIT_FAILURE);
228 }
229 handler = std::make_unique<host_tool::LpcDataHandler>(
Patrick Venturecf9b2192019-06-27 12:09:52 -0700230 &blob, &devmem, hostAddress, hostLength, &progress);
Patrick Venturea6586362018-12-11 18:47:13 -0800231 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700232 else if (interface == IPMIPCI)
233 {
234 handler = std::make_unique<host_tool::P2aDataHandler>(
Patrick Venturecf9b2192019-06-27 12:09:52 -0700235 &blob, &devmem, &pci, &progress);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700236 }
Patrick Venturea6586362018-12-11 18:47:13 -0800237
238 if (!handler)
239 {
240 /* TODO(venture): use a custom exception. */
241 std::fprintf(stderr, "Interface %s is unavailable\n",
242 interface.c_str());
243 exit(EXIT_FAILURE);
244 }
245
Patrick Venturebf58cd62018-12-11 09:05:46 -0800246 /* The parameters are all filled out. */
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800247 try
248 {
Patrick Venture55646de2019-05-16 10:06:26 -0700249 host_tool::UpdateHandler updater(&blob, handler.get());
Patrick Venture9f937c42019-06-21 07:55:54 -0700250 host_tool::updaterMain(&updater, imagePath, signaturePath, type);
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800251 }
Patrick Venture9b534f02018-12-13 16:10:02 -0800252 catch (const host_tool::ToolException& e)
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800253 {
254 std::fprintf(stderr, "Exception received: %s\n", e.what());
255 return -1;
256 }
Patrick Venturee564d5b2019-05-14 12:30:06 -0700257 catch (const std::exception& e)
258 {
259 std::fprintf(stderr, "Unexpected exception received: %s\n",
260 e.what());
261 return -1;
262 }
Patrick Venturebf58cd62018-12-11 09:05:46 -0800263 }
264
265 return 0;
266}