Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 1 | /* |
| 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 Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 17 | #include "bt.hpp" |
Patrick Venture | 46bdadc | 2019-01-18 09:04:16 -0800 | [diff] [blame] | 18 | #include "io.hpp" |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 19 | #include "lpc.hpp" |
Benjamin Fair | 30d09a3 | 2019-10-11 16:57:47 -0700 | [diff] [blame^] | 20 | #include "net.hpp" |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 21 | #include "p2a.hpp" |
| 22 | #include "pci.hpp" |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 23 | #include "progress.hpp" |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 24 | #include "tool_errors.hpp" |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 25 | #include "updater.hpp" |
| 26 | |
| 27 | /* Use CLI11 argument parser once in openbmc/meta-oe or whatever. */ |
| 28 | #include <getopt.h> |
| 29 | |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 30 | #include <algorithm> |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 31 | #include <cstdio> |
Patrick Venture | 8104a52 | 2019-06-19 19:04:36 -0700 | [diff] [blame] | 32 | #include <cstdlib> |
Patrick Venture | e564d5b | 2019-05-14 12:30:06 -0700 | [diff] [blame] | 33 | #include <exception> |
Patrick Venture | aa107a6 | 2018-12-12 15:16:25 -0800 | [diff] [blame] | 34 | #include <iostream> |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 35 | #include <ipmiblob/blob_handler.hpp> |
| 36 | #include <ipmiblob/ipmi_handler.hpp> |
Patrick Venture | aa107a6 | 2018-12-12 15:16:25 -0800 | [diff] [blame] | 37 | #include <iterator> |
Patrick Venture | 03db87e | 2019-06-20 07:56:06 -0700 | [diff] [blame] | 38 | #include <limits> |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 39 | #include <memory> |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 40 | #include <string> |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 41 | #include <vector> |
| 42 | |
| 43 | #define IPMILPC "ipmilpc" |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 44 | #define IPMIPCI "ipmipci" |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 45 | #define IPMIBT "ipmibt" |
Benjamin Fair | 30d09a3 | 2019-10-11 16:57:47 -0700 | [diff] [blame^] | 46 | #define IPMINET "ipminet" |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 47 | |
| 48 | namespace |
| 49 | { |
Benjamin Fair | 30d09a3 | 2019-10-11 16:57:47 -0700 | [diff] [blame^] | 50 | const std::vector<std::string> interfaceList = {IPMINET, IPMIBT, IPMILPC, |
| 51 | IPMIPCI}; |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 52 | } // namespace |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 53 | |
| 54 | void usage(const char* program) |
| 55 | { |
Patrick Venture | 7ae5dde | 2018-12-14 10:03:35 -0800 | [diff] [blame] | 56 | std::fprintf( |
| 57 | stderr, |
| 58 | "Usage: %s --command <command> --interface <interface> --image " |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 59 | "<image file> --sig <signature file> --type <layout> " |
| 60 | "[--ignore-update]\n", |
Patrick Venture | 7ae5dde | 2018-12-14 10:03:35 -0800 | [diff] [blame] | 61 | program); |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 62 | |
Patrick Venture | 7ae5dde | 2018-12-14 10:03:35 -0800 | [diff] [blame] | 63 | std::fprintf(stderr, "interfaces: "); |
Patrick Venture | aa107a6 | 2018-12-12 15:16:25 -0800 | [diff] [blame] | 64 | std::copy(interfaceList.begin(), interfaceList.end(), |
| 65 | std::ostream_iterator<std::string>(std::cerr, ", ")); |
| 66 | std::fprintf(stderr, "\n"); |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 67 | |
Patrick Venture | c498caa | 2019-08-15 10:12:50 -0700 | [diff] [blame] | 68 | std::fprintf(stderr, "layouts examples: image, bios\n"); |
| 69 | std::fprintf(stderr, |
| 70 | "the type field specifies '/flash/{layout}' for a handler\n"); |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | bool checkCommand(const std::string& command) |
| 74 | { |
| 75 | return (command == "update"); |
| 76 | } |
| 77 | |
| 78 | bool checkInterface(const std::string& interface) |
| 79 | { |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 80 | auto intf = |
| 81 | std::find(interfaceList.begin(), interfaceList.end(), interface); |
| 82 | return (intf != interfaceList.end()); |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int main(int argc, char* argv[]) |
| 86 | { |
Benjamin Fair | 30d09a3 | 2019-10-11 16:57:47 -0700 | [diff] [blame^] | 87 | std::string command, interface, imagePath, signaturePath, type, host; |
| 88 | std::string port = "623"; |
Patrick Venture | 8104a52 | 2019-06-19 19:04:36 -0700 | [diff] [blame] | 89 | char* valueEnd = nullptr; |
| 90 | long address = 0; |
| 91 | long length = 0; |
Patrick Venture | 03db87e | 2019-06-20 07:56:06 -0700 | [diff] [blame] | 92 | std::uint32_t hostAddress = 0; |
| 93 | std::uint32_t hostLength = 0; |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 94 | bool ignoreUpdate = false; |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 95 | |
| 96 | while (1) |
| 97 | { |
| 98 | // clang-format off |
| 99 | static struct option long_options[] = { |
| 100 | {"command", required_argument, 0, 'c'}, |
| 101 | {"interface", required_argument, 0, 'i'}, |
| 102 | {"image", required_argument, 0, 'm'}, |
| 103 | {"sig", required_argument, 0, 's'}, |
Patrick Venture | 8104a52 | 2019-06-19 19:04:36 -0700 | [diff] [blame] | 104 | {"address", required_argument, 0, 'a'}, |
| 105 | {"length", required_argument, 0, 'l'}, |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 106 | {"type", required_argument, 0, 't'}, |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 107 | {"ignore-update", no_argument, 0, 'u'}, |
Benjamin Fair | 30d09a3 | 2019-10-11 16:57:47 -0700 | [diff] [blame^] | 108 | {"host", required_argument, 0, 'H'}, |
| 109 | {"port", optional_argument, 0, 'p'}, |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 110 | {0, 0, 0, 0} |
| 111 | }; |
| 112 | // clang-format on |
| 113 | |
| 114 | int option_index = 0; |
Benjamin Fair | 30d09a3 | 2019-10-11 16:57:47 -0700 | [diff] [blame^] | 115 | int c = getopt_long(argc, argv, "c:i:m:s:a:l:t:uH:p:", long_options, |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 116 | &option_index); |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 117 | 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 Venture | 8104a52 | 2019-06-19 19:04:36 -0700 | [diff] [blame] | 147 | 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 Venture | 03db87e | 2019-06-20 07:56:06 -0700 | [diff] [blame] | 154 | 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 Venture | 8104a52 | 2019-06-19 19:04:36 -0700 | [diff] [blame] | 161 | 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 Venture | 03db87e | 2019-06-20 07:56:06 -0700 | [diff] [blame] | 169 | 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 Venture | 8104a52 | 2019-06-19 19:04:36 -0700 | [diff] [blame] | 176 | break; |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 177 | case 't': |
| 178 | type = std::string{optarg}; |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 179 | break; |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 180 | case 'u': |
| 181 | ignoreUpdate = true; |
| 182 | break; |
Benjamin Fair | 30d09a3 | 2019-10-11 16:57:47 -0700 | [diff] [blame^] | 183 | case 'H': |
| 184 | host = std::string{optarg}; |
| 185 | break; |
| 186 | case 'p': |
| 187 | port = std::string{optarg}; |
| 188 | break; |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 189 | default: |
| 190 | usage(argv[0]); |
| 191 | exit(EXIT_FAILURE); |
| 192 | } |
| 193 | } |
| 194 | |
Patrick Venture | 361bd5a | 2018-12-14 09:49:47 -0800 | [diff] [blame] | 195 | if (command.empty()) |
| 196 | { |
| 197 | usage(argv[0]); |
| 198 | exit(EXIT_FAILURE); |
| 199 | } |
| 200 | |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 201 | /* They want to update the firmware. */ |
| 202 | if (command == "update") |
| 203 | { |
Patrick Venture | c498caa | 2019-08-15 10:12:50 -0700 | [diff] [blame] | 204 | if (interface.empty() || imagePath.empty() || signaturePath.empty() || |
| 205 | type.empty()) |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 206 | { |
| 207 | usage(argv[0]); |
| 208 | exit(EXIT_FAILURE); |
| 209 | } |
| 210 | |
Patrick Venture | 866d448 | 2019-05-13 09:26:52 -0700 | [diff] [blame] | 211 | auto ipmi = ipmiblob::IpmiHandler::CreateIpmiHandler(); |
| 212 | ipmiblob::BlobHandler blob(std::move(ipmi)); |
Patrick Venture | 46bdadc | 2019-01-18 09:04:16 -0800 | [diff] [blame] | 213 | host_tool::DevMemDevice devmem; |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 214 | host_tool::PciUtilImpl pci; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 215 | host_tool::ProgressStdoutIndicator progress; |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 216 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 217 | std::unique_ptr<host_tool::DataInterface> handler; |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 218 | |
| 219 | /* Input has already been validated in this case. */ |
| 220 | if (interface == IPMIBT) |
| 221 | { |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 222 | handler = |
| 223 | std::make_unique<host_tool::BtDataHandler>(&blob, &progress); |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 224 | } |
Benjamin Fair | 30d09a3 | 2019-10-11 16:57:47 -0700 | [diff] [blame^] | 225 | else if (interface == IPMINET) |
| 226 | { |
| 227 | if (host.empty()) |
| 228 | { |
| 229 | std::fprintf(stderr, "Host not specified\n"); |
| 230 | exit(EXIT_FAILURE); |
| 231 | } |
| 232 | handler = std::make_unique<host_tool::NetDataHandler>( |
| 233 | &blob, &progress, host, port); |
| 234 | } |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 235 | else if (interface == IPMILPC) |
| 236 | { |
Patrick Venture | 03db87e | 2019-06-20 07:56:06 -0700 | [diff] [blame] | 237 | if (hostAddress == 0 || hostLength == 0) |
Patrick Venture | 8104a52 | 2019-06-19 19:04:36 -0700 | [diff] [blame] | 238 | { |
| 239 | std::fprintf(stderr, "Address or Length were 0\n"); |
| 240 | exit(EXIT_FAILURE); |
| 241 | } |
| 242 | handler = std::make_unique<host_tool::LpcDataHandler>( |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 243 | &blob, &devmem, hostAddress, hostLength, &progress); |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 244 | } |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 245 | else if (interface == IPMIPCI) |
| 246 | { |
| 247 | handler = std::make_unique<host_tool::P2aDataHandler>( |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 248 | &blob, &devmem, &pci, &progress); |
Patrick Venture | b5bf0fc | 2019-05-03 14:33:49 -0700 | [diff] [blame] | 249 | } |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 250 | |
| 251 | if (!handler) |
| 252 | { |
| 253 | /* TODO(venture): use a custom exception. */ |
| 254 | std::fprintf(stderr, "Interface %s is unavailable\n", |
| 255 | interface.c_str()); |
| 256 | exit(EXIT_FAILURE); |
| 257 | } |
| 258 | |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 259 | /* The parameters are all filled out. */ |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 260 | try |
| 261 | { |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 262 | host_tool::UpdateHandler updater(&blob, handler.get()); |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 263 | host_tool::updaterMain(&updater, imagePath, signaturePath, type, |
| 264 | ignoreUpdate); |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 265 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 266 | catch (const host_tool::ToolException& e) |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 267 | { |
| 268 | std::fprintf(stderr, "Exception received: %s\n", e.what()); |
| 269 | return -1; |
| 270 | } |
Patrick Venture | e564d5b | 2019-05-14 12:30:06 -0700 | [diff] [blame] | 271 | catch (const std::exception& e) |
| 272 | { |
| 273 | std::fprintf(stderr, "Unexpected exception received: %s\n", |
| 274 | e.what()); |
| 275 | return -1; |
| 276 | } |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |