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