| David Anderson | 4124123 | 2018-06-13 16:50:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <inttypes.h> |
| 19 | #include <stdio.h> |
| 20 | #include <sysexits.h> |
| 21 | |
| 22 | #include <memory> |
| 23 | |
| 24 | #include <android-base/parseint.h> |
| 25 | #include <android-base/strings.h> |
| 26 | #include <liblp/builder.h> |
| 27 | #include <liblp/writer.h> |
| 28 | |
| 29 | using namespace android; |
| 30 | using namespace android::fs_mgr; |
| 31 | |
| 32 | /* Prints program usage to |where|. */ |
| 33 | static int usage(int /* argc */, char* argv[]) { |
| 34 | fprintf(stderr, |
| 35 | "%s - command-line tool for creating Android Logical Partition images.\n" |
| 36 | "\n" |
| 37 | "Usage:\n" |
| 38 | " %s [options]\n" |
| 39 | "\n" |
| 40 | "Required options:\n" |
| 41 | " -d,--device-size=SIZE Size of the block device for logical partitions.\n" |
| 42 | " -m,--metadata-size=SIZE Maximum size to reserve for partition metadata.\n" |
| 43 | " -s,--metadata-slots=COUNT Number of slots to store metadata copies.\n" |
| 44 | " -p,--partition=DATA Add a partition given the data, see below.\n" |
| 45 | " -o,--output=FILE Output file.\n" |
| 46 | "\n" |
| 47 | "Partition format:\n" |
| 48 | " <name>:<guid>:<attributes>:<size>\n" |
| 49 | " Attrs must be 'none' or 'readonly'.\n", |
| 50 | argv[0], argv[0]); |
| 51 | return EX_USAGE; |
| 52 | } |
| 53 | |
| 54 | int main(int argc, char* argv[]) { |
| 55 | struct option options[] = { |
| 56 | { "device-size", required_argument, nullptr, 'd' }, |
| 57 | { "metadata-size", required_argument, nullptr, 'm' }, |
| 58 | { "metadata-slots", required_argument, nullptr, 's' }, |
| 59 | { "partition", required_argument, nullptr, 'p' }, |
| 60 | { "output", required_argument, nullptr, 'o' }, |
| 61 | { "help", no_argument, nullptr, 'h' }, |
| 62 | { nullptr, 0, nullptr, 0 }, |
| 63 | }; |
| 64 | |
| 65 | uint64_t blockdevice_size = 0; |
| 66 | uint32_t metadata_size = 0; |
| 67 | uint32_t metadata_slots = 0; |
| 68 | std::string output_path; |
| 69 | std::vector<std::string> partitions; |
| 70 | |
| 71 | int rv; |
| 72 | int index; |
| 73 | while ((rv = getopt_long_only(argc, argv, "d:m:s:p:o:h", options, &index)) != -1) { |
| 74 | switch (rv) { |
| 75 | case 'h': |
| 76 | return usage(argc, argv); |
| 77 | case 'd': |
| 78 | if (!android::base::ParseUint(optarg, &blockdevice_size)) { |
| 79 | fprintf(stderr, "Invalid argument to --device-size.\n"); |
| 80 | return EX_USAGE; |
| 81 | } |
| 82 | break; |
| 83 | case 'm': |
| 84 | if (!android::base::ParseUint(optarg, &metadata_size)) { |
| 85 | fprintf(stderr, "Invalid argument to --metadata-size.\n"); |
| 86 | return EX_USAGE; |
| 87 | } |
| 88 | break; |
| 89 | case 's': |
| 90 | if (!android::base::ParseUint(optarg, &metadata_slots)) { |
| 91 | fprintf(stderr, "Invalid argument to --metadata-slots.\n"); |
| 92 | return EX_USAGE; |
| 93 | } |
| 94 | break; |
| 95 | case 'p': |
| 96 | partitions.push_back(optarg); |
| 97 | break; |
| 98 | case 'o': |
| 99 | output_path = optarg; |
| 100 | break; |
| 101 | default: |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Check for empty arguments so we can print a more helpful message rather |
| 107 | // than error on each individual missing argument. |
| 108 | if (optind == 1) { |
| 109 | return usage(argc, argv); |
| 110 | } |
| 111 | |
| 112 | if (!blockdevice_size) { |
| 113 | fprintf(stderr, "--device-size needs more than 0 bytes of disk space.\n"); |
| 114 | return EX_USAGE; |
| 115 | } |
| 116 | if (!metadata_size) { |
| 117 | fprintf(stderr, "--metadata-size must be more than 0 bytes.\n"); |
| 118 | return EX_USAGE; |
| 119 | } |
| 120 | if (!metadata_slots) { |
| 121 | fprintf(stderr, "--metadata-slots must be more than 0.\n"); |
| 122 | return EX_USAGE; |
| 123 | } |
| 124 | if (output_path.empty()) { |
| 125 | fprintf(stderr, "--output must specify a valid path.\n"); |
| 126 | return EX_USAGE; |
| 127 | } |
| 128 | if (partitions.empty()) { |
| 129 | fprintf(stderr, "Partition table must have at least one entry.\n"); |
| 130 | return EX_USAGE; |
| 131 | } |
| 132 | |
| 133 | std::unique_ptr<MetadataBuilder> builder = |
| 134 | MetadataBuilder::New(blockdevice_size, metadata_size, metadata_slots); |
| 135 | |
| 136 | for (const auto& partition_info : partitions) { |
| 137 | std::vector<std::string> parts = android::base::Split(partition_info, ":"); |
| 138 | if (parts.size() != 4) { |
| 139 | fprintf(stderr, "Partition info has invalid formatting.\n"); |
| 140 | return EX_USAGE; |
| 141 | } |
| 142 | |
| 143 | std::string name = parts[0]; |
| 144 | if (!name.length()) { |
| 145 | fprintf(stderr, "Partition must have a valid name.\n"); |
| 146 | return EX_USAGE; |
| 147 | } |
| 148 | |
| 149 | uint64_t size; |
| 150 | if (!android::base::ParseUint(parts[3].c_str(), &size)) { |
| 151 | fprintf(stderr, "Partition must have a valid size.\n"); |
| 152 | return EX_USAGE; |
| 153 | } |
| 154 | |
| 155 | uint32_t attribute_flags = 0; |
| 156 | std::string attributes = parts[2]; |
| 157 | if (attributes == "readonly") { |
| 158 | attribute_flags |= LP_PARTITION_ATTR_READONLY; |
| 159 | } else if (attributes != "none") { |
| 160 | fprintf(stderr, "Attribute not recognized: %s\n", attributes.c_str()); |
| 161 | return EX_USAGE; |
| 162 | } |
| 163 | |
| 164 | Partition* partition = builder->AddPartition(name, parts[1], attribute_flags); |
| 165 | if (!builder->GrowPartition(partition, size)) { |
| 166 | fprintf(stderr, "Not enough space on device for partition %s with size %" PRIu64 "\n", |
| 167 | name.c_str(), size); |
| 168 | return EX_SOFTWARE; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | std::unique_ptr<LpMetadata> metadata = builder->Export(); |
| 173 | if (!WriteToImageFile(output_path.c_str(), *metadata.get())) { |
| 174 | return EX_CANTCREAT; |
| 175 | } |
| 176 | |
| 177 | return EX_OK; |
| 178 | } |