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