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