1 /* 2 * Copyright (C) 2019 The Android Open Source Project 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 17 #include <getopt.h> 18 #include <sysexits.h> 19 20 #include <libvbmeta/libvbmeta.h> 21 22 using android::fs_mgr::WriteToSuperVBMetaFile; 23 24 /* Prints program usage to |where|. */ 25 static int usage(int /* argc */, char* argv[]) { 26 fprintf(stderr, 27 "%s - command-line tool for creating Super VBMeta Image.\n" 28 "\n" 29 "Usage:\n" 30 " %s [options]\n" 31 "\n" 32 "Required options:\n" 33 " -o,--output=FILE Output file.\n" 34 "\n" 35 "Optional:\n" 36 " -i,--image=VBMETA_NAME=FILE include the given vbmeta file as\n" 37 " initial data for the super vbmeta.\n", 38 argv[0], argv[0]); 39 return EX_USAGE; 40 } 41 42 int main(int argc, char* argv[]) { 43 struct option options[] = { 44 { "help", no_argument, nullptr, 'h' }, 45 { "image", required_argument, nullptr, 'i' }, 46 { "output", required_argument, nullptr, 'o' }, 47 { nullptr, 0, nullptr, 0 }, 48 }; 49 50 std::string output_path; 51 std::map<std::string, std::string> images; 52 53 int rv; 54 while ((rv = getopt_long_only(argc, argv, "i:o:", options, NULL)) != -1) { 55 switch (rv) { 56 case 'h': 57 return usage(argc, argv); 58 case 'i': 59 { 60 char* separator = strchr(optarg, '='); 61 if (!separator || separator == optarg || !strlen(separator + 1)) { 62 fprintf(stderr, "Expected VBMETA_NAME=FILE.\n"); 63 return EX_USAGE; 64 } 65 *separator = '\0'; 66 67 std::string vbmeta_name(optarg); 68 std::string file(separator + 1); 69 images[vbmeta_name] = file; 70 break; 71 } 72 case 'o': 73 output_path = optarg; 74 break; 75 default: 76 break; 77 } 78 } 79 80 // Check for empty arguments so we can print a more helpful message rather 81 // than error on each individual missing argument. 82 if (optind == 1) { 83 return usage(argc, argv); 84 } 85 86 if (output_path.empty()) { 87 fprintf(stderr, "--output must specify a valid path.\n"); 88 return EX_USAGE; 89 } 90 91 if (!WriteToSuperVBMetaFile(output_path.c_str(), images)) { 92 return EX_CANTCREAT; 93 } 94 95 return EX_OK; 96 } 97