| 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 | cf2d1b1 | 2018-12-11 18:22:36 -0800 | [diff] [blame^] | 18 | #include "ipmi_handler.hpp" | 
| Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 19 | #include "updater.hpp" | 
|  | 20 |  | 
|  | 21 | /* Use CLI11 argument parser once in openbmc/meta-oe or whatever. */ | 
|  | 22 | #include <getopt.h> | 
|  | 23 |  | 
|  | 24 | #include <cstdio> | 
| Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 25 | #include <memory> | 
| Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 26 | #include <string> | 
|  | 27 |  | 
|  | 28 | void usage(const char* program) | 
|  | 29 | { | 
|  | 30 | std::fprintf(stderr, | 
|  | 31 | "Usage: %s -command <command> -interface <interface> -image " | 
|  | 32 | "<image file> -sig <signature file>", | 
|  | 33 | program); | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | bool checkCommand(const std::string& command) | 
|  | 37 | { | 
|  | 38 | return (command == "update"); | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | bool checkInterface(const std::string& interface) | 
|  | 42 | { | 
|  | 43 | return (interface == "ipmibt" || interface == "ipmilpc"); | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | int main(int argc, char* argv[]) | 
|  | 47 | { | 
|  | 48 | std::string command, interface, imagePath, signaturePath; | 
|  | 49 |  | 
|  | 50 | while (1) | 
|  | 51 | { | 
|  | 52 | // clang-format off | 
|  | 53 | static struct option long_options[] = { | 
|  | 54 | {"command", required_argument, 0, 'c'}, | 
|  | 55 | {"interface", required_argument, 0, 'i'}, | 
|  | 56 | {"image", required_argument, 0, 'm'}, | 
|  | 57 | {"sig", required_argument, 0, 's'}, | 
|  | 58 | {0, 0, 0, 0} | 
|  | 59 | }; | 
|  | 60 | // clang-format on | 
|  | 61 |  | 
|  | 62 | int option_index = 0; | 
|  | 63 | int c = | 
|  | 64 | getopt_long(argc, argv, "c:i:m:s:", long_options, &option_index); | 
|  | 65 | if (c == -1) | 
|  | 66 | { | 
|  | 67 | break; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | switch (c) | 
|  | 71 | { | 
|  | 72 | case 'c': | 
|  | 73 | command = std::string{optarg}; | 
|  | 74 | if (!checkCommand(command)) | 
|  | 75 | { | 
|  | 76 | usage(argv[0]); | 
|  | 77 | exit(EXIT_FAILURE); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | break; | 
|  | 81 | case 'i': | 
|  | 82 | interface = std::string{optarg}; | 
|  | 83 | if (!checkInterface(interface)) | 
|  | 84 | { | 
|  | 85 | usage(argv[0]); | 
|  | 86 | exit(EXIT_FAILURE); | 
|  | 87 | } | 
|  | 88 | break; | 
|  | 89 | case 'm': | 
|  | 90 | imagePath = std::string{optarg}; | 
|  | 91 | break; | 
|  | 92 | case 's': | 
|  | 93 | signaturePath = std::string{optarg}; | 
|  | 94 | break; | 
|  | 95 | default: | 
|  | 96 | usage(argv[0]); | 
|  | 97 | exit(EXIT_FAILURE); | 
|  | 98 | } | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | /* They want to update the firmware. */ | 
|  | 102 | if (command == "update") | 
|  | 103 | { | 
|  | 104 | if (interface.empty() || imagePath.empty() || signaturePath.empty()) | 
|  | 105 | { | 
|  | 106 | usage(argv[0]); | 
|  | 107 | exit(EXIT_FAILURE); | 
|  | 108 | } | 
|  | 109 |  | 
| Patrick Venture | cf2d1b1 | 2018-12-11 18:22:36 -0800 | [diff] [blame^] | 110 | IpmiHandler ipmi; | 
|  | 111 | BlobHandler blob(&ipmi); | 
| Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 112 |  | 
| Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 113 | /* The parameters are all filled out. */ | 
| Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 114 | return updaterMain(&blob, interface, imagePath, signaturePath); | 
| Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
|  | 117 | return 0; | 
|  | 118 | } |