blob: ae85c1badd819184cd9615a56c2a0e68f1a811d2 [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"
45
46namespace
47{
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070048const std::vector<std::string> interfaceList = {IPMIBT, IPMILPC, IPMIPCI};
Patrick Venture9f937c42019-06-21 07:55:54 -070049} // namespace
Patrick Venturebf58cd62018-12-11 09:05:46 -080050
51void usage(const char* program)
52{
Patrick Venture7ae5dde2018-12-14 10:03:35 -080053 std::fprintf(
54 stderr,
55 "Usage: %s --command <command> --interface <interface> --image "
Patrick Venture9f937c42019-06-21 07:55:54 -070056 "<image file> --sig <signature file> --type <layout>\n",
Patrick Venture7ae5dde2018-12-14 10:03:35 -080057 program);
Patrick Venturea6586362018-12-11 18:47:13 -080058
Patrick Venture7ae5dde2018-12-14 10:03:35 -080059 std::fprintf(stderr, "interfaces: ");
Patrick Ventureaa107a62018-12-12 15:16:25 -080060 std::copy(interfaceList.begin(), interfaceList.end(),
61 std::ostream_iterator<std::string>(std::cerr, ", "));
62 std::fprintf(stderr, "\n");
Patrick Venture9f937c42019-06-21 07:55:54 -070063
Patrick Venturec498caa2019-08-15 10:12:50 -070064 std::fprintf(stderr, "layouts examples: image, bios\n");
65 std::fprintf(stderr,
66 "the type field specifies '/flash/{layout}' for a handler\n");
Patrick Venturebf58cd62018-12-11 09:05:46 -080067}
68
69bool checkCommand(const std::string& command)
70{
71 return (command == "update");
72}
73
74bool checkInterface(const std::string& interface)
75{
Patrick Venturea6586362018-12-11 18:47:13 -080076 auto intf =
77 std::find(interfaceList.begin(), interfaceList.end(), interface);
78 return (intf != interfaceList.end());
Patrick Venturebf58cd62018-12-11 09:05:46 -080079}
80
81int main(int argc, char* argv[])
82{
Patrick Venture9f937c42019-06-21 07:55:54 -070083 std::string command, interface, imagePath, signaturePath, type;
Patrick Venture8104a522019-06-19 19:04:36 -070084 char* valueEnd = nullptr;
85 long address = 0;
86 long length = 0;
Patrick Venture03db87e2019-06-20 07:56:06 -070087 std::uint32_t hostAddress = 0;
88 std::uint32_t hostLength = 0;
Patrick Venturebf58cd62018-12-11 09:05:46 -080089
90 while (1)
91 {
92 // clang-format off
93 static struct option long_options[] = {
94 {"command", required_argument, 0, 'c'},
95 {"interface", required_argument, 0, 'i'},
96 {"image", required_argument, 0, 'm'},
97 {"sig", required_argument, 0, 's'},
Patrick Venture8104a522019-06-19 19:04:36 -070098 {"address", required_argument, 0, 'a'},
99 {"length", required_argument, 0, 'l'},
Patrick Venture9f937c42019-06-21 07:55:54 -0700100 {"type", required_argument, 0, 't'},
Patrick Venturebf58cd62018-12-11 09:05:46 -0800101 {0, 0, 0, 0}
102 };
103 // clang-format on
104
105 int option_index = 0;
Patrick Venture9f937c42019-06-21 07:55:54 -0700106 int c = getopt_long(argc, argv, "c:i:m:s:a:l:t:", long_options,
107 &option_index);
Patrick Venturebf58cd62018-12-11 09:05:46 -0800108 if (c == -1)
109 {
110 break;
111 }
112
113 switch (c)
114 {
115 case 'c':
116 command = std::string{optarg};
117 if (!checkCommand(command))
118 {
119 usage(argv[0]);
120 exit(EXIT_FAILURE);
121 }
122
123 break;
124 case 'i':
125 interface = std::string{optarg};
126 if (!checkInterface(interface))
127 {
128 usage(argv[0]);
129 exit(EXIT_FAILURE);
130 }
131 break;
132 case 'm':
133 imagePath = std::string{optarg};
134 break;
135 case 's':
136 signaturePath = std::string{optarg};
137 break;
Patrick Venture8104a522019-06-19 19:04:36 -0700138 case 'a':
139 address = std::strtol(&optarg[0], &valueEnd, 0);
140 if (valueEnd == nullptr)
141 {
142 usage(argv[0]);
143 exit(EXIT_FAILURE);
144 }
Patrick Venture03db87e2019-06-20 07:56:06 -0700145 if (address > std::numeric_limits<std::uint32_t>::max())
146 {
147 std::fprintf(stderr, "Address beyond 32-bit limit.\n");
148 usage(argv[0]);
149 exit(EXIT_FAILURE);
150 }
151 hostAddress = static_cast<std::uint32_t>(address);
Patrick Venture8104a522019-06-19 19:04:36 -0700152 break;
153 case 'l':
154 length = std::strtol(&optarg[0], &valueEnd, 0);
155 if (valueEnd == nullptr)
156 {
157 usage(argv[0]);
158 exit(EXIT_FAILURE);
159 }
Patrick Venture03db87e2019-06-20 07:56:06 -0700160 if (length > std::numeric_limits<std::uint32_t>::max())
161 {
162 std::fprintf(stderr, "Length beyond 32-bit limit.\n");
163 usage(argv[0]);
164 exit(EXIT_FAILURE);
165 }
166 hostLength = static_cast<std::uint32_t>(length);
Patrick Venture8104a522019-06-19 19:04:36 -0700167 break;
Patrick Venture9f937c42019-06-21 07:55:54 -0700168 case 't':
169 type = std::string{optarg};
Patrick Venture9f937c42019-06-21 07:55:54 -0700170 break;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800171 default:
172 usage(argv[0]);
173 exit(EXIT_FAILURE);
174 }
175 }
176
Patrick Venture361bd5a2018-12-14 09:49:47 -0800177 if (command.empty())
178 {
179 usage(argv[0]);
180 exit(EXIT_FAILURE);
181 }
182
Patrick Venturebf58cd62018-12-11 09:05:46 -0800183 /* They want to update the firmware. */
184 if (command == "update")
185 {
Patrick Venturec498caa2019-08-15 10:12:50 -0700186 if (interface.empty() || imagePath.empty() || signaturePath.empty() ||
187 type.empty())
Patrick Venturebf58cd62018-12-11 09:05:46 -0800188 {
189 usage(argv[0]);
190 exit(EXIT_FAILURE);
191 }
192
Patrick Venture866d4482019-05-13 09:26:52 -0700193 auto ipmi = ipmiblob::IpmiHandler::CreateIpmiHandler();
194 ipmiblob::BlobHandler blob(std::move(ipmi));
Patrick Venture46bdadc2019-01-18 09:04:16 -0800195 host_tool::DevMemDevice devmem;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700196 host_tool::PciUtilImpl pci;
Patrick Venturecf9b2192019-06-27 12:09:52 -0700197 host_tool::ProgressStdoutIndicator progress;
Patrick Venture00887592018-12-11 10:57:06 -0800198
Patrick Venture9b534f02018-12-13 16:10:02 -0800199 std::unique_ptr<host_tool::DataInterface> handler;
Patrick Venturea6586362018-12-11 18:47:13 -0800200
201 /* Input has already been validated in this case. */
202 if (interface == IPMIBT)
203 {
Patrick Venturecf9b2192019-06-27 12:09:52 -0700204 handler =
205 std::make_unique<host_tool::BtDataHandler>(&blob, &progress);
Patrick Venturea6586362018-12-11 18:47:13 -0800206 }
207 else if (interface == IPMILPC)
208 {
Patrick Venture03db87e2019-06-20 07:56:06 -0700209 if (hostAddress == 0 || hostLength == 0)
Patrick Venture8104a522019-06-19 19:04:36 -0700210 {
211 std::fprintf(stderr, "Address or Length were 0\n");
212 exit(EXIT_FAILURE);
213 }
214 handler = std::make_unique<host_tool::LpcDataHandler>(
Patrick Venturecf9b2192019-06-27 12:09:52 -0700215 &blob, &devmem, hostAddress, hostLength, &progress);
Patrick Venturea6586362018-12-11 18:47:13 -0800216 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700217 else if (interface == IPMIPCI)
218 {
219 handler = std::make_unique<host_tool::P2aDataHandler>(
Patrick Venturecf9b2192019-06-27 12:09:52 -0700220 &blob, &devmem, &pci, &progress);
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700221 }
Patrick Venturea6586362018-12-11 18:47:13 -0800222
223 if (!handler)
224 {
225 /* TODO(venture): use a custom exception. */
226 std::fprintf(stderr, "Interface %s is unavailable\n",
227 interface.c_str());
228 exit(EXIT_FAILURE);
229 }
230
Patrick Venturebf58cd62018-12-11 09:05:46 -0800231 /* The parameters are all filled out. */
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800232 try
233 {
Patrick Venture55646de2019-05-16 10:06:26 -0700234 host_tool::UpdateHandler updater(&blob, handler.get());
Patrick Venture9f937c42019-06-21 07:55:54 -0700235 host_tool::updaterMain(&updater, imagePath, signaturePath, type);
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800236 }
Patrick Venture9b534f02018-12-13 16:10:02 -0800237 catch (const host_tool::ToolException& e)
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800238 {
239 std::fprintf(stderr, "Exception received: %s\n", e.what());
240 return -1;
241 }
Patrick Venturee564d5b2019-05-14 12:30:06 -0700242 catch (const std::exception& e)
243 {
244 std::fprintf(stderr, "Unexpected exception received: %s\n",
245 e.what());
246 return -1;
247 }
Patrick Venturebf58cd62018-12-11 09:05:46 -0800248 }
249
250 return 0;
251}