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