blob: c944899d78396fc24a246d8bd35061b6e9c76ef5 [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"
44
45namespace
46{
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -070047const std::vector<std::string> interfaceList = {IPMIBT, IPMILPC, IPMIPCI};
Patrick Venturea6586362018-12-11 18:47:13 -080048}
Patrick Venturebf58cd62018-12-11 09:05:46 -080049
50void usage(const char* program)
51{
Patrick Venture7ae5dde2018-12-14 10:03:35 -080052 std::fprintf(
53 stderr,
54 "Usage: %s --command <command> --interface <interface> --image "
55 "<image file> --sig <signature file>\n",
56 program);
Patrick Venturea6586362018-12-11 18:47:13 -080057
Patrick Venture7ae5dde2018-12-14 10:03:35 -080058 std::fprintf(stderr, "interfaces: ");
Patrick Ventureaa107a62018-12-12 15:16:25 -080059 std::copy(interfaceList.begin(), interfaceList.end(),
60 std::ostream_iterator<std::string>(std::cerr, ", "));
61 std::fprintf(stderr, "\n");
Patrick Venturebf58cd62018-12-11 09:05:46 -080062}
63
64bool checkCommand(const std::string& command)
65{
66 return (command == "update");
67}
68
69bool checkInterface(const std::string& interface)
70{
Patrick Venturea6586362018-12-11 18:47:13 -080071 auto intf =
72 std::find(interfaceList.begin(), interfaceList.end(), interface);
73 return (intf != interfaceList.end());
Patrick Venturebf58cd62018-12-11 09:05:46 -080074}
75
76int main(int argc, char* argv[])
77{
78 std::string command, interface, imagePath, signaturePath;
Patrick Venture8104a522019-06-19 19:04:36 -070079 char* valueEnd = nullptr;
80 long address = 0;
81 long length = 0;
Patrick Venture03db87e2019-06-20 07:56:06 -070082 std::uint32_t hostAddress = 0;
83 std::uint32_t hostLength = 0;
Patrick Venturebf58cd62018-12-11 09:05:46 -080084
85 while (1)
86 {
87 // clang-format off
88 static struct option long_options[] = {
89 {"command", required_argument, 0, 'c'},
90 {"interface", required_argument, 0, 'i'},
91 {"image", required_argument, 0, 'm'},
92 {"sig", required_argument, 0, 's'},
Patrick Venture8104a522019-06-19 19:04:36 -070093 {"address", required_argument, 0, 'a'},
94 {"length", required_argument, 0, 'l'},
Patrick Venturebf58cd62018-12-11 09:05:46 -080095 {0, 0, 0, 0}
96 };
97 // clang-format on
98
99 int option_index = 0;
100 int c =
Patrick Venture8104a522019-06-19 19:04:36 -0700101 getopt_long(argc, argv, "c:i:m:s:a:l", long_options, &option_index);
Patrick Venturebf58cd62018-12-11 09:05:46 -0800102 if (c == -1)
103 {
104 break;
105 }
106
107 switch (c)
108 {
109 case 'c':
110 command = std::string{optarg};
111 if (!checkCommand(command))
112 {
113 usage(argv[0]);
114 exit(EXIT_FAILURE);
115 }
116
117 break;
118 case 'i':
119 interface = std::string{optarg};
120 if (!checkInterface(interface))
121 {
122 usage(argv[0]);
123 exit(EXIT_FAILURE);
124 }
125 break;
126 case 'm':
127 imagePath = std::string{optarg};
128 break;
129 case 's':
130 signaturePath = std::string{optarg};
131 break;
Patrick Venture8104a522019-06-19 19:04:36 -0700132 case 'a':
133 address = std::strtol(&optarg[0], &valueEnd, 0);
134 if (valueEnd == nullptr)
135 {
136 usage(argv[0]);
137 exit(EXIT_FAILURE);
138 }
Patrick Venture03db87e2019-06-20 07:56:06 -0700139 if (address > std::numeric_limits<std::uint32_t>::max())
140 {
141 std::fprintf(stderr, "Address beyond 32-bit limit.\n");
142 usage(argv[0]);
143 exit(EXIT_FAILURE);
144 }
145 hostAddress = static_cast<std::uint32_t>(address);
Patrick Venture8104a522019-06-19 19:04:36 -0700146 break;
147 case 'l':
148 length = 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 (length > std::numeric_limits<std::uint32_t>::max())
155 {
156 std::fprintf(stderr, "Length beyond 32-bit limit.\n");
157 usage(argv[0]);
158 exit(EXIT_FAILURE);
159 }
160 hostLength = static_cast<std::uint32_t>(length);
Patrick Venture8104a522019-06-19 19:04:36 -0700161 break;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800162 default:
163 usage(argv[0]);
164 exit(EXIT_FAILURE);
165 }
166 }
167
Patrick Venture361bd5a2018-12-14 09:49:47 -0800168 if (command.empty())
169 {
170 usage(argv[0]);
171 exit(EXIT_FAILURE);
172 }
173
Patrick Venturebf58cd62018-12-11 09:05:46 -0800174 /* They want to update the firmware. */
175 if (command == "update")
176 {
177 if (interface.empty() || imagePath.empty() || signaturePath.empty())
178 {
179 usage(argv[0]);
180 exit(EXIT_FAILURE);
181 }
182
Patrick Venture866d4482019-05-13 09:26:52 -0700183 auto ipmi = ipmiblob::IpmiHandler::CreateIpmiHandler();
184 ipmiblob::BlobHandler blob(std::move(ipmi));
Patrick Venture46bdadc2019-01-18 09:04:16 -0800185 host_tool::DevMemDevice devmem;
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700186 host_tool::PciUtilImpl pci;
Patrick Venture00887592018-12-11 10:57:06 -0800187
Patrick Venture9b534f02018-12-13 16:10:02 -0800188 std::unique_ptr<host_tool::DataInterface> handler;
Patrick Venturea6586362018-12-11 18:47:13 -0800189
190 /* Input has already been validated in this case. */
191 if (interface == IPMIBT)
192 {
Patrick Venture9b534f02018-12-13 16:10:02 -0800193 handler = std::make_unique<host_tool::BtDataHandler>(&blob);
Patrick Venturea6586362018-12-11 18:47:13 -0800194 }
195 else if (interface == IPMILPC)
196 {
Patrick Venture03db87e2019-06-20 07:56:06 -0700197 if (hostAddress == 0 || hostLength == 0)
Patrick Venture8104a522019-06-19 19:04:36 -0700198 {
199 std::fprintf(stderr, "Address or Length were 0\n");
200 exit(EXIT_FAILURE);
201 }
202 handler = std::make_unique<host_tool::LpcDataHandler>(
Patrick Venture03db87e2019-06-20 07:56:06 -0700203 &blob, &devmem, hostAddress, hostLength);
Patrick Venturea6586362018-12-11 18:47:13 -0800204 }
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -0700205 else if (interface == IPMIPCI)
206 {
207 handler = std::make_unique<host_tool::P2aDataHandler>(
208 &blob, &devmem, &pci);
209 }
Patrick Venturea6586362018-12-11 18:47:13 -0800210
211 if (!handler)
212 {
213 /* TODO(venture): use a custom exception. */
214 std::fprintf(stderr, "Interface %s is unavailable\n",
215 interface.c_str());
216 exit(EXIT_FAILURE);
217 }
218
Patrick Venturebf58cd62018-12-11 09:05:46 -0800219 /* The parameters are all filled out. */
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800220 try
221 {
Patrick Venture55646de2019-05-16 10:06:26 -0700222 host_tool::UpdateHandler updater(&blob, handler.get());
223 host_tool::updaterMain(&updater, imagePath, signaturePath);
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800224 }
Patrick Venture9b534f02018-12-13 16:10:02 -0800225 catch (const host_tool::ToolException& e)
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800226 {
227 std::fprintf(stderr, "Exception received: %s\n", e.what());
228 return -1;
229 }
Patrick Venturee564d5b2019-05-14 12:30:06 -0700230 catch (const std::exception& e)
231 {
232 std::fprintf(stderr, "Unexpected exception received: %s\n",
233 e.what());
234 return -1;
235 }
Patrick Venturebf58cd62018-12-11 09:05:46 -0800236 }
237
238 return 0;
239}