blob: c9e76c0a62ba70777ffd9d7dbc4c63b43d7b1c05 [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 Venture8104a522019-06-19 19:04:36 -070030#include <cstdlib>
Patrick Venturee564d5b2019-05-14 12:30:06 -070031#include <exception>
Patrick Ventureaa107a62018-12-12 15:16:25 -080032#include <iostream>
Patrick Venture664c5bc2019-03-07 08:09:45 -080033#include <ipmiblob/blob_handler.hpp>
34#include <ipmiblob/ipmi_handler.hpp>
Patrick Ventureaa107a62018-12-12 15:16:25 -080035#include <iterator>
Patrick Venture03db87e2019-06-20 07:56:06 -070036#include <limits>
Patrick Venture00887592018-12-11 10:57:06 -080037#include <memory>
Patrick Venturebf58cd62018-12-11 09:05:46 -080038#include <string>
Patrick Venturea6586362018-12-11 18:47:13 -080039#include <vector>
40
41#define IPMILPC "ipmilpc"
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070042#define IPMIPCI "ipmipci"
Patrick Venturea6586362018-12-11 18:47:13 -080043#define IPMIBT "ipmibt"
Patrick Venture9f937c42019-06-21 07:55:54 -070044#define STATIC "static"
45#define UBITAR "ubitar"
Patrick Venturea6586362018-12-11 18:47:13 -080046
47namespace
48{
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070049const std::vector<std::string> interfaceList = {IPMIBT, IPMILPC, IPMIPCI};
Patrick Venture9f937c42019-06-21 07:55:54 -070050const std::vector<std::string> typeList = {STATIC, UBITAR};
51} // namespace
Patrick Venturebf58cd62018-12-11 09:05:46 -080052
53void usage(const char* program)
54{
Patrick Venture7ae5dde2018-12-14 10:03:35 -080055 std::fprintf(
56 stderr,
57 "Usage: %s --command <command> --interface <interface> --image "
Patrick Venture9f937c42019-06-21 07:55:54 -070058 "<image file> --sig <signature file> --type <layout>\n",
Patrick Venture7ae5dde2018-12-14 10:03:35 -080059 program);
Patrick Venturea6586362018-12-11 18:47:13 -080060
Patrick Venture7ae5dde2018-12-14 10:03:35 -080061 std::fprintf(stderr, "interfaces: ");
Patrick Ventureaa107a62018-12-12 15:16:25 -080062 std::copy(interfaceList.begin(), interfaceList.end(),
63 std::ostream_iterator<std::string>(std::cerr, ", "));
64 std::fprintf(stderr, "\n");
Patrick Venture9f937c42019-06-21 07:55:54 -070065
66 std::fprintf(stderr, "layouts: ");
67 std::copy(typeList.begin(), typeList.end(),
68 std::ostream_iterator<std::string>(std::cerr, ", "));
69 std::fprintf(stderr, "\n");
70}
71
72bool checkType(const std::string& type)
73{
74 auto tFound = std::find(typeList.begin(), typeList.end(), type);
75 return (tFound != typeList.end());
Patrick Venturebf58cd62018-12-11 09:05:46 -080076}
77
78bool checkCommand(const std::string& command)
79{
80 return (command == "update");
81}
82
83bool checkInterface(const std::string& interface)
84{
Patrick Venturea6586362018-12-11 18:47:13 -080085 auto intf =
86 std::find(interfaceList.begin(), interfaceList.end(), interface);
87 return (intf != interfaceList.end());
Patrick Venturebf58cd62018-12-11 09:05:46 -080088}
89
90int main(int argc, char* argv[])
91{
Patrick Venture9f937c42019-06-21 07:55:54 -070092 std::string command, interface, imagePath, signaturePath, type;
Patrick Venture8104a522019-06-19 19:04:36 -070093 char* valueEnd = nullptr;
94 long address = 0;
95 long length = 0;
Patrick Venture03db87e2019-06-20 07:56:06 -070096 std::uint32_t hostAddress = 0;
97 std::uint32_t hostLength = 0;
Patrick Venturebf58cd62018-12-11 09:05:46 -080098
99 while (1)
100 {
101 // clang-format off
102 static struct option long_options[] = {
103 {"command", required_argument, 0, 'c'},
104 {"interface", required_argument, 0, 'i'},
105 {"image", required_argument, 0, 'm'},
106 {"sig", required_argument, 0, 's'},
Patrick Venture8104a522019-06-19 19:04:36 -0700107 {"address", required_argument, 0, 'a'},
108 {"length", required_argument, 0, 'l'},
Patrick Venture9f937c42019-06-21 07:55:54 -0700109 {"type", required_argument, 0, 't'},
Patrick Venturebf58cd62018-12-11 09:05:46 -0800110 {0, 0, 0, 0}
111 };
112 // clang-format on
113
114 int option_index = 0;
Patrick Venture9f937c42019-06-21 07:55:54 -0700115 int c = getopt_long(argc, argv, "c:i:m:s:a:l:t:", long_options,
116 &option_index);
Patrick Venturebf58cd62018-12-11 09:05:46 -0800117 if (c == -1)
118 {
119 break;
120 }
121
122 switch (c)
123 {
124 case 'c':
125 command = std::string{optarg};
126 if (!checkCommand(command))
127 {
128 usage(argv[0]);
129 exit(EXIT_FAILURE);
130 }
131
132 break;
133 case 'i':
134 interface = std::string{optarg};
135 if (!checkInterface(interface))
136 {
137 usage(argv[0]);
138 exit(EXIT_FAILURE);
139 }
140 break;
141 case 'm':
142 imagePath = std::string{optarg};
143 break;
144 case 's':
145 signaturePath = std::string{optarg};
146 break;
Patrick Venture8104a522019-06-19 19:04:36 -0700147 case 'a':
148 address = std::strtol(&optarg[0], &valueEnd, 0);
149 if (valueEnd == nullptr)
150 {
151 usage(argv[0]);
152 exit(EXIT_FAILURE);
153 }
Patrick Venture03db87e2019-06-20 07:56:06 -0700154 if (address > std::numeric_limits<std::uint32_t>::max())
155 {
156 std::fprintf(stderr, "Address beyond 32-bit limit.\n");
157 usage(argv[0]);
158 exit(EXIT_FAILURE);
159 }
160 hostAddress = static_cast<std::uint32_t>(address);
Patrick Venture8104a522019-06-19 19:04:36 -0700161 break;
162 case 'l':
163 length = std::strtol(&optarg[0], &valueEnd, 0);
164 if (valueEnd == nullptr)
165 {
166 usage(argv[0]);
167 exit(EXIT_FAILURE);
168 }
Patrick Venture03db87e2019-06-20 07:56:06 -0700169 if (length > std::numeric_limits<std::uint32_t>::max())
170 {
171 std::fprintf(stderr, "Length beyond 32-bit limit.\n");
172 usage(argv[0]);
173 exit(EXIT_FAILURE);
174 }
175 hostLength = static_cast<std::uint32_t>(length);
Patrick Venture8104a522019-06-19 19:04:36 -0700176 break;
Patrick Venture9f937c42019-06-21 07:55:54 -0700177 case 't':
178 type = std::string{optarg};
179 if (!checkType(type))
180 {
181 usage(argv[0]);
182 exit(EXIT_FAILURE);
183 }
184 break;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800185 default:
186 usage(argv[0]);
187 exit(EXIT_FAILURE);
188 }
189 }
190
Patrick Venture361bd5a2018-12-14 09:49:47 -0800191 if (command.empty())
192 {
193 usage(argv[0]);
194 exit(EXIT_FAILURE);
195 }
196
Patrick Venturebf58cd62018-12-11 09:05:46 -0800197 /* They want to update the firmware. */
198 if (command == "update")
199 {
200 if (interface.empty() || imagePath.empty() || signaturePath.empty())
201 {
202 usage(argv[0]);
203 exit(EXIT_FAILURE);
204 }
205
Patrick Venture866d4482019-05-13 09:26:52 -0700206 auto ipmi = ipmiblob::IpmiHandler::CreateIpmiHandler();
207 ipmiblob::BlobHandler blob(std::move(ipmi));
Patrick Venture46bdadc2019-01-18 09:04:16 -0800208 host_tool::DevMemDevice devmem;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700209 host_tool::PciUtilImpl pci;
Patrick Venture00887592018-12-11 10:57:06 -0800210
Patrick Venture9b534f02018-12-13 16:10:02 -0800211 std::unique_ptr<host_tool::DataInterface> handler;
Patrick Venturea6586362018-12-11 18:47:13 -0800212
213 /* Input has already been validated in this case. */
214 if (interface == IPMIBT)
215 {
Patrick Venture9b534f02018-12-13 16:10:02 -0800216 handler = std::make_unique<host_tool::BtDataHandler>(&blob);
Patrick Venturea6586362018-12-11 18:47:13 -0800217 }
218 else if (interface == IPMILPC)
219 {
Patrick Venture03db87e2019-06-20 07:56:06 -0700220 if (hostAddress == 0 || hostLength == 0)
Patrick Venture8104a522019-06-19 19:04:36 -0700221 {
222 std::fprintf(stderr, "Address or Length were 0\n");
223 exit(EXIT_FAILURE);
224 }
225 handler = std::make_unique<host_tool::LpcDataHandler>(
Patrick Venture03db87e2019-06-20 07:56:06 -0700226 &blob, &devmem, hostAddress, hostLength);
Patrick Venturea6586362018-12-11 18:47:13 -0800227 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700228 else if (interface == IPMIPCI)
229 {
230 handler = std::make_unique<host_tool::P2aDataHandler>(
231 &blob, &devmem, &pci);
232 }
Patrick Venturea6586362018-12-11 18:47:13 -0800233
234 if (!handler)
235 {
236 /* TODO(venture): use a custom exception. */
237 std::fprintf(stderr, "Interface %s is unavailable\n",
238 interface.c_str());
239 exit(EXIT_FAILURE);
240 }
241
Patrick Venturebf58cd62018-12-11 09:05:46 -0800242 /* The parameters are all filled out. */
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800243 try
244 {
Patrick Venture55646de2019-05-16 10:06:26 -0700245 host_tool::UpdateHandler updater(&blob, handler.get());
Patrick Venture9f937c42019-06-21 07:55:54 -0700246 host_tool::updaterMain(&updater, imagePath, signaturePath, type);
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800247 }
Patrick Venture9b534f02018-12-13 16:10:02 -0800248 catch (const host_tool::ToolException& e)
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800249 {
250 std::fprintf(stderr, "Exception received: %s\n", e.what());
251 return -1;
252 }
Patrick Venturee564d5b2019-05-14 12:30:06 -0700253 catch (const std::exception& e)
254 {
255 std::fprintf(stderr, "Unexpected exception received: %s\n",
256 e.what());
257 return -1;
258 }
Patrick Venturebf58cd62018-12-11 09:05:46 -0800259 }
260
261 return 0;
262}