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 | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 17 | #include "blob_handler.hpp" |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 18 | #include "bt.hpp" |
Patrick Venture | 46bdadc | 2019-01-18 09:04:16 -0800 | [diff] [blame] | 19 | #include "io.hpp" |
Patrick Venture | cf2d1b1 | 2018-12-11 18:22:36 -0800 | [diff] [blame] | 20 | #include "ipmi_handler.hpp" |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 21 | #include "lpc.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 | aa107a6 | 2018-12-12 15:16:25 -0800 | [diff] [blame] | 30 | #include <iostream> |
| 31 | #include <iterator> |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 32 | #include <memory> |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 33 | #include <string> |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 34 | #include <vector> |
| 35 | |
| 36 | #define IPMILPC "ipmilpc" |
| 37 | #define IPMIBT "ipmibt" |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | const std::vector<std::string> interfaceList = {IPMIBT, IPMILPC}; |
| 42 | } |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 43 | |
| 44 | void usage(const char* program) |
| 45 | { |
Patrick Venture | 7ae5dde | 2018-12-14 10:03:35 -0800 | [diff] [blame] | 46 | std::fprintf( |
| 47 | stderr, |
| 48 | "Usage: %s --command <command> --interface <interface> --image " |
| 49 | "<image file> --sig <signature file>\n", |
| 50 | program); |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 51 | |
Patrick Venture | 7ae5dde | 2018-12-14 10:03:35 -0800 | [diff] [blame] | 52 | std::fprintf(stderr, "interfaces: "); |
Patrick Venture | aa107a6 | 2018-12-12 15:16:25 -0800 | [diff] [blame] | 53 | std::copy(interfaceList.begin(), interfaceList.end(), |
| 54 | std::ostream_iterator<std::string>(std::cerr, ", ")); |
| 55 | std::fprintf(stderr, "\n"); |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool checkCommand(const std::string& command) |
| 59 | { |
| 60 | return (command == "update"); |
| 61 | } |
| 62 | |
| 63 | bool checkInterface(const std::string& interface) |
| 64 | { |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 65 | auto intf = |
| 66 | std::find(interfaceList.begin(), interfaceList.end(), interface); |
| 67 | return (intf != interfaceList.end()); |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | int main(int argc, char* argv[]) |
| 71 | { |
| 72 | std::string command, interface, imagePath, signaturePath; |
| 73 | |
| 74 | while (1) |
| 75 | { |
| 76 | // clang-format off |
| 77 | static struct option long_options[] = { |
| 78 | {"command", required_argument, 0, 'c'}, |
| 79 | {"interface", required_argument, 0, 'i'}, |
| 80 | {"image", required_argument, 0, 'm'}, |
| 81 | {"sig", required_argument, 0, 's'}, |
| 82 | {0, 0, 0, 0} |
| 83 | }; |
| 84 | // clang-format on |
| 85 | |
| 86 | int option_index = 0; |
| 87 | int c = |
| 88 | getopt_long(argc, argv, "c:i:m:s:", long_options, &option_index); |
| 89 | if (c == -1) |
| 90 | { |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | switch (c) |
| 95 | { |
| 96 | case 'c': |
| 97 | command = std::string{optarg}; |
| 98 | if (!checkCommand(command)) |
| 99 | { |
| 100 | usage(argv[0]); |
| 101 | exit(EXIT_FAILURE); |
| 102 | } |
| 103 | |
| 104 | break; |
| 105 | case 'i': |
| 106 | interface = std::string{optarg}; |
| 107 | if (!checkInterface(interface)) |
| 108 | { |
| 109 | usage(argv[0]); |
| 110 | exit(EXIT_FAILURE); |
| 111 | } |
| 112 | break; |
| 113 | case 'm': |
| 114 | imagePath = std::string{optarg}; |
| 115 | break; |
| 116 | case 's': |
| 117 | signaturePath = std::string{optarg}; |
| 118 | break; |
| 119 | default: |
| 120 | usage(argv[0]); |
| 121 | exit(EXIT_FAILURE); |
| 122 | } |
| 123 | } |
| 124 | |
Patrick Venture | 361bd5a | 2018-12-14 09:49:47 -0800 | [diff] [blame] | 125 | if (command.empty()) |
| 126 | { |
| 127 | usage(argv[0]); |
| 128 | exit(EXIT_FAILURE); |
| 129 | } |
| 130 | |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 131 | /* They want to update the firmware. */ |
| 132 | if (command == "update") |
| 133 | { |
| 134 | if (interface.empty() || imagePath.empty() || signaturePath.empty()) |
| 135 | { |
| 136 | usage(argv[0]); |
| 137 | exit(EXIT_FAILURE); |
| 138 | } |
| 139 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 140 | host_tool::IpmiHandler ipmi; |
| 141 | host_tool::BlobHandler blob(&ipmi); |
Patrick Venture | 46bdadc | 2019-01-18 09:04:16 -0800 | [diff] [blame] | 142 | host_tool::DevMemDevice devmem; |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 143 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 144 | std::unique_ptr<host_tool::DataInterface> handler; |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 145 | |
| 146 | /* Input has already been validated in this case. */ |
| 147 | if (interface == IPMIBT) |
| 148 | { |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 149 | handler = std::make_unique<host_tool::BtDataHandler>(&blob); |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 150 | } |
| 151 | else if (interface == IPMILPC) |
| 152 | { |
Patrick Venture | 46bdadc | 2019-01-18 09:04:16 -0800 | [diff] [blame] | 153 | handler = |
| 154 | std::make_unique<host_tool::LpcDataHandler>(&blob, &devmem); |
Patrick Venture | a658636 | 2018-12-11 18:47:13 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (!handler) |
| 158 | { |
| 159 | /* TODO(venture): use a custom exception. */ |
| 160 | std::fprintf(stderr, "Interface %s is unavailable\n", |
| 161 | interface.c_str()); |
| 162 | exit(EXIT_FAILURE); |
| 163 | } |
| 164 | |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 165 | /* The parameters are all filled out. */ |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 166 | try |
| 167 | { |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 168 | host_tool::updaterMain(&blob, handler.get(), imagePath, |
| 169 | signaturePath); |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 170 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 171 | catch (const host_tool::ToolException& e) |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 172 | { |
| 173 | std::fprintf(stderr, "Exception received: %s\n", e.what()); |
| 174 | return -1; |
| 175 | } |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |