| David Anderson | 12211d1 | 2018-07-24 15:21:20 -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 | #include "flashing.h" | 
 | 17 |  | 
 | 18 | #include <fcntl.h> | 
 | 19 | #include <sys/stat.h> | 
 | 20 | #include <unistd.h> | 
 | 21 |  | 
 | 22 | #include <algorithm> | 
 | 23 | #include <memory> | 
 | 24 |  | 
 | 25 | #include <android-base/logging.h> | 
 | 26 | #include <android-base/strings.h> | 
 | 27 | #include <ext4_utils/ext4_utils.h> | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 28 | #include <liblp/builder.h> | 
 | 29 | #include <liblp/liblp.h> | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 30 | #include <sparse/sparse.h> | 
 | 31 |  | 
 | 32 | #include "fastboot_device.h" | 
 | 33 | #include "utility.h" | 
 | 34 |  | 
 | 35 | namespace { | 
 | 36 |  | 
 | 37 | constexpr uint32_t SPARSE_HEADER_MAGIC = 0xed26ff3a; | 
 | 38 |  | 
 | 39 | }  // namespace | 
 | 40 |  | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 41 | using namespace android::fs_mgr; | 
 | 42 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 43 | int FlashRawDataChunk(int fd, const char* data, size_t len) { | 
 | 44 |     size_t ret = 0; | 
 | 45 |     while (ret < len) { | 
 | 46 |         int this_len = std::min(static_cast<size_t>(1048576UL * 8), len - ret); | 
 | 47 |         int this_ret = write(fd, data, this_len); | 
 | 48 |         if (this_ret < 0) { | 
 | 49 |             PLOG(ERROR) << "Failed to flash data of len " << len; | 
 | 50 |             return -1; | 
 | 51 |         } | 
 | 52 |         data += this_ret; | 
 | 53 |         ret += this_ret; | 
 | 54 |     } | 
 | 55 |     return 0; | 
 | 56 | } | 
 | 57 |  | 
 | 58 | int FlashRawData(int fd, const std::vector<char>& downloaded_data) { | 
 | 59 |     int ret = FlashRawDataChunk(fd, downloaded_data.data(), downloaded_data.size()); | 
 | 60 |     if (ret < 0) { | 
 | 61 |         return -errno; | 
 | 62 |     } | 
 | 63 |     return ret; | 
 | 64 | } | 
 | 65 |  | 
 | 66 | int WriteCallback(void* priv, const void* data, size_t len) { | 
 | 67 |     int fd = reinterpret_cast<long long>(priv); | 
 | 68 |     if (!data) { | 
 | 69 |         return lseek64(fd, len, SEEK_CUR) >= 0 ? 0 : -errno; | 
 | 70 |     } | 
 | 71 |     return FlashRawDataChunk(fd, reinterpret_cast<const char*>(data), len); | 
 | 72 | } | 
 | 73 |  | 
 | 74 | int FlashSparseData(int fd, std::vector<char>& downloaded_data) { | 
| Hridya Valsaraju | aec0de5 | 2018-10-10 13:09:41 -0700 | [diff] [blame] | 75 |     struct sparse_file* file = sparse_file_import_buf(downloaded_data.data(), true, false); | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 76 |     if (!file) { | 
 | 77 |         return -ENOENT; | 
 | 78 |     } | 
 | 79 |     return sparse_file_callback(file, false, false, WriteCallback, reinterpret_cast<void*>(fd)); | 
 | 80 | } | 
 | 81 |  | 
 | 82 | int FlashBlockDevice(int fd, std::vector<char>& downloaded_data) { | 
 | 83 |     lseek64(fd, 0, SEEK_SET); | 
 | 84 |     if (downloaded_data.size() >= sizeof(SPARSE_HEADER_MAGIC) && | 
 | 85 |         *reinterpret_cast<uint32_t*>(downloaded_data.data()) == SPARSE_HEADER_MAGIC) { | 
 | 86 |         return FlashSparseData(fd, downloaded_data); | 
 | 87 |     } else { | 
 | 88 |         return FlashRawData(fd, downloaded_data); | 
 | 89 |     } | 
 | 90 | } | 
 | 91 |  | 
 | 92 | int Flash(FastbootDevice* device, const std::string& partition_name) { | 
 | 93 |     PartitionHandle handle; | 
 | 94 |     if (!OpenPartition(device, partition_name, &handle)) { | 
 | 95 |         return -ENOENT; | 
 | 96 |     } | 
 | 97 |  | 
 | 98 |     std::vector<char> data = std::move(device->download_data()); | 
 | 99 |     if (data.size() == 0) { | 
 | 100 |         return -EINVAL; | 
 | 101 |     } else if (data.size() > get_block_device_size(handle.fd())) { | 
 | 102 |         return -EOVERFLOW; | 
 | 103 |     } | 
 | 104 |     return FlashBlockDevice(handle.fd(), data); | 
 | 105 | } | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 106 |  | 
 | 107 | bool UpdateSuper(FastbootDevice* device, const std::string& partition_name, bool wipe) { | 
 | 108 |     std::optional<std::string> super = FindPhysicalPartition(partition_name); | 
 | 109 |     if (!super) { | 
 | 110 |         return device->WriteFail("Could not find partition: " + partition_name); | 
 | 111 |     } | 
 | 112 |  | 
 | 113 |     std::vector<char> data = std::move(device->download_data()); | 
 | 114 |     if (data.empty()) { | 
 | 115 |         return device->WriteFail("No data available"); | 
 | 116 |     } | 
 | 117 |  | 
 | 118 |     std::unique_ptr<LpMetadata> new_metadata = ReadFromImageBlob(data.data(), data.size()); | 
 | 119 |     if (!new_metadata) { | 
 | 120 |         return device->WriteFail("Data is not a valid logical partition metadata image"); | 
 | 121 |     } | 
 | 122 |  | 
 | 123 |     // If we are unable to read the existing metadata, then the super partition | 
 | 124 |     // is corrupt. In this case we reflash the whole thing using the provided | 
 | 125 |     // image. | 
 | 126 |     std::string slot_suffix = device->GetCurrentSlot(); | 
 | 127 |     uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix); | 
 | 128 |     std::unique_ptr<LpMetadata> metadata = ReadMetadata(super->c_str(), slot_number); | 
 | 129 |     if (!metadata || wipe) { | 
 | 130 |         if (!FlashPartitionTable(super.value(), *new_metadata.get())) { | 
 | 131 |             return device->WriteFail("Unable to flash new partition table"); | 
 | 132 |         } | 
 | 133 |         return device->WriteOkay("Successfully flashed partition table"); | 
 | 134 |     } | 
 | 135 |  | 
 | 136 |     // There's a working super partition, and we don't want to wipe it - it may | 
 | 137 |     // may contain partitions created for the user. Instead, we create a zero- | 
 | 138 |     // sized partition for each entry in the new partition table. It is then | 
 | 139 |     // the host's responsibility to size it correctly via resize-logical-partition. | 
 | 140 |     std::unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(*metadata.get()); | 
 | 141 |     if (!builder) { | 
 | 142 |         return device->WriteFail("Unable to create a metadata builder"); | 
 | 143 |     } | 
 | 144 |     for (const auto& partition : new_metadata->partitions) { | 
 | 145 |         std::string name = GetPartitionName(partition); | 
 | 146 |         if (builder->FindPartition(name)) { | 
 | 147 |             continue; | 
 | 148 |         } | 
| David Anderson | e5f2f06 | 2018-10-03 13:49:23 -0700 | [diff] [blame] | 149 |         if (!builder->AddPartition(name, partition.attributes)) { | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 150 |             return device->WriteFail("Unable to add partition: " + name); | 
 | 151 |         } | 
 | 152 |     } | 
 | 153 |  | 
 | 154 |     // The scratch partition may exist as temporary storage, created for | 
 | 155 |     // use by adb remount for overlayfs. If we're performing a flashall | 
 | 156 |     // operation then we want to start over with a clean slate, so we | 
 | 157 |     // remove the scratch partition until it is requested again. | 
 | 158 |     builder->RemovePartition("scratch"); | 
 | 159 |  | 
 | 160 |     new_metadata = builder->Export(); | 
 | 161 |     if (!new_metadata) { | 
 | 162 |         return device->WriteFail("Unable to export new partition table"); | 
 | 163 |     } | 
 | 164 |  | 
 | 165 |     // Write the new table to every metadata slot. | 
 | 166 |     bool ok = true; | 
 | 167 |     for (size_t i = 0; i < new_metadata->geometry.metadata_slot_count; i++) { | 
 | 168 |         ok &= UpdatePartitionTable(super.value(), *new_metadata.get(), i); | 
 | 169 |     } | 
 | 170 |  | 
 | 171 |     if (!ok) { | 
 | 172 |         return device->WriteFail("Unable to write new partition table"); | 
 | 173 |     } | 
 | 174 |     return device->WriteOkay("Successfully updated partition table"); | 
 | 175 | } |