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