| 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> | 
| Mark Salyzyn | 044f04b | 2018-10-12 09:33:44 -0700 | [diff] [blame] | 24 | #include <set> | 
|  | 25 | #include <string> | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 26 |  | 
| Mark Salyzyn | 044f04b | 2018-10-12 09:33:44 -0700 | [diff] [blame] | 27 | #include <android-base/file.h> | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 28 | #include <android-base/logging.h> | 
|  | 29 | #include <android-base/strings.h> | 
|  | 30 | #include <ext4_utils/ext4_utils.h> | 
| Mark Salyzyn | 044f04b | 2018-10-12 09:33:44 -0700 | [diff] [blame] | 31 | #include <fs_mgr_overlayfs.h> | 
|  | 32 | #include <fstab/fstab.h> | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 33 | #include <liblp/builder.h> | 
|  | 34 | #include <liblp/liblp.h> | 
| Yifan Hong | 0e13bba | 2019-08-29 16:29:22 -0700 | [diff] [blame] | 35 | #include <libsnapshot/snapshot.h> | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 36 | #include <sparse/sparse.h> | 
|  | 37 |  | 
|  | 38 | #include "fastboot_device.h" | 
|  | 39 | #include "utility.h" | 
|  | 40 |  | 
| Mark Salyzyn | 044f04b | 2018-10-12 09:33:44 -0700 | [diff] [blame] | 41 | using namespace android::fs_mgr; | 
|  | 42 | using namespace std::literals; | 
|  | 43 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 44 | namespace { | 
|  | 45 |  | 
|  | 46 | constexpr uint32_t SPARSE_HEADER_MAGIC = 0xed26ff3a; | 
|  | 47 |  | 
| Mark Salyzyn | 044f04b | 2018-10-12 09:33:44 -0700 | [diff] [blame] | 48 | void WipeOverlayfsForPartition(FastbootDevice* device, const std::string& partition_name) { | 
|  | 49 | // May be called, in the case of sparse data, multiple times so cache/skip. | 
|  | 50 | static std::set<std::string> wiped; | 
|  | 51 | if (wiped.find(partition_name) != wiped.end()) return; | 
|  | 52 | wiped.insert(partition_name); | 
|  | 53 | // Following appears to have a first time 2% impact on flashing speeds. | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 54 |  | 
| Mark Salyzyn | 044f04b | 2018-10-12 09:33:44 -0700 | [diff] [blame] | 55 | // Convert partition_name to a validated mount point and wipe. | 
| Tom Cherry | b688d91 | 2019-01-28 12:34:33 -0800 | [diff] [blame] | 56 | Fstab fstab; | 
|  | 57 | ReadDefaultFstab(&fstab); | 
|  | 58 |  | 
|  | 59 | for (const auto& entry : fstab) { | 
|  | 60 | auto partition = android::base::Basename(entry.mount_point); | 
|  | 61 | if ("/" == entry.mount_point) { | 
|  | 62 | partition = "system"; | 
|  | 63 | } | 
|  | 64 |  | 
| Mark Salyzyn | 044f04b | 2018-10-12 09:33:44 -0700 | [diff] [blame] | 65 | if ((partition + device->GetCurrentSlot()) == partition_name) { | 
| Tom Cherry | b688d91 | 2019-01-28 12:34:33 -0800 | [diff] [blame] | 66 | fs_mgr_overlayfs_teardown(entry.mount_point.c_str()); | 
| Mark Salyzyn | 044f04b | 2018-10-12 09:33:44 -0700 | [diff] [blame] | 67 | } | 
|  | 68 | } | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | }  // namespace | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 72 |  | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 73 | int FlashRawDataChunk(int fd, const char* data, size_t len) { | 
|  | 74 | size_t ret = 0; | 
|  | 75 | while (ret < len) { | 
|  | 76 | int this_len = std::min(static_cast<size_t>(1048576UL * 8), len - ret); | 
|  | 77 | int this_ret = write(fd, data, this_len); | 
|  | 78 | if (this_ret < 0) { | 
|  | 79 | PLOG(ERROR) << "Failed to flash data of len " << len; | 
|  | 80 | return -1; | 
|  | 81 | } | 
|  | 82 | data += this_ret; | 
|  | 83 | ret += this_ret; | 
|  | 84 | } | 
|  | 85 | return 0; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | int FlashRawData(int fd, const std::vector<char>& downloaded_data) { | 
|  | 89 | int ret = FlashRawDataChunk(fd, downloaded_data.data(), downloaded_data.size()); | 
|  | 90 | if (ret < 0) { | 
|  | 91 | return -errno; | 
|  | 92 | } | 
|  | 93 | return ret; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | int WriteCallback(void* priv, const void* data, size_t len) { | 
|  | 97 | int fd = reinterpret_cast<long long>(priv); | 
|  | 98 | if (!data) { | 
|  | 99 | return lseek64(fd, len, SEEK_CUR) >= 0 ? 0 : -errno; | 
|  | 100 | } | 
|  | 101 | return FlashRawDataChunk(fd, reinterpret_cast<const char*>(data), len); | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | int FlashSparseData(int fd, std::vector<char>& downloaded_data) { | 
| Hridya Valsaraju | aec0de5 | 2018-10-10 13:09:41 -0700 | [diff] [blame] | 105 | 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] | 106 | if (!file) { | 
|  | 107 | return -ENOENT; | 
|  | 108 | } | 
|  | 109 | return sparse_file_callback(file, false, false, WriteCallback, reinterpret_cast<void*>(fd)); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | int FlashBlockDevice(int fd, std::vector<char>& downloaded_data) { | 
|  | 113 | lseek64(fd, 0, SEEK_SET); | 
|  | 114 | if (downloaded_data.size() >= sizeof(SPARSE_HEADER_MAGIC) && | 
|  | 115 | *reinterpret_cast<uint32_t*>(downloaded_data.data()) == SPARSE_HEADER_MAGIC) { | 
|  | 116 | return FlashSparseData(fd, downloaded_data); | 
|  | 117 | } else { | 
|  | 118 | return FlashRawData(fd, downloaded_data); | 
|  | 119 | } | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | int Flash(FastbootDevice* device, const std::string& partition_name) { | 
|  | 123 | PartitionHandle handle; | 
|  | 124 | if (!OpenPartition(device, partition_name, &handle)) { | 
|  | 125 | return -ENOENT; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | std::vector<char> data = std::move(device->download_data()); | 
|  | 129 | if (data.size() == 0) { | 
|  | 130 | return -EINVAL; | 
|  | 131 | } else if (data.size() > get_block_device_size(handle.fd())) { | 
|  | 132 | return -EOVERFLOW; | 
|  | 133 | } | 
| Mark Salyzyn | 044f04b | 2018-10-12 09:33:44 -0700 | [diff] [blame] | 134 | WipeOverlayfsForPartition(device, partition_name); | 
| David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 135 | return FlashBlockDevice(handle.fd(), data); | 
|  | 136 | } | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 137 |  | 
| David Anderson | b6134a6 | 2018-10-26 13:08:44 -0700 | [diff] [blame] | 138 | bool UpdateSuper(FastbootDevice* device, const std::string& super_name, bool wipe) { | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 139 | std::vector<char> data = std::move(device->download_data()); | 
|  | 140 | if (data.empty()) { | 
|  | 141 | return device->WriteFail("No data available"); | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | std::unique_ptr<LpMetadata> new_metadata = ReadFromImageBlob(data.data(), data.size()); | 
|  | 145 | if (!new_metadata) { | 
|  | 146 | return device->WriteFail("Data is not a valid logical partition metadata image"); | 
|  | 147 | } | 
|  | 148 |  | 
| David Anderson | a48f86b | 2018-12-20 16:55:04 -0800 | [diff] [blame] | 149 | if (!FindPhysicalPartition(super_name)) { | 
|  | 150 | return device->WriteFail("Cannot find " + super_name + | 
|  | 151 | ", build may be missing broken or missing boot_devices"); | 
|  | 152 | } | 
|  | 153 |  | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 154 | // If we are unable to read the existing metadata, then the super partition | 
|  | 155 | // is corrupt. In this case we reflash the whole thing using the provided | 
|  | 156 | // image. | 
|  | 157 | std::string slot_suffix = device->GetCurrentSlot(); | 
|  | 158 | uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix); | 
| David Anderson | 96a9fd4 | 2018-11-05 15:21:44 -0800 | [diff] [blame] | 159 | std::unique_ptr<LpMetadata> old_metadata = ReadMetadata(super_name, slot_number); | 
|  | 160 | if (wipe || !old_metadata) { | 
| David Anderson | b6134a6 | 2018-10-26 13:08:44 -0700 | [diff] [blame] | 161 | if (!FlashPartitionTable(super_name, *new_metadata.get())) { | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 162 | return device->WriteFail("Unable to flash new partition table"); | 
|  | 163 | } | 
| Mark Salyzyn | 307a41f | 2018-11-26 09:57:17 -0800 | [diff] [blame] | 164 | fs_mgr_overlayfs_teardown(); | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 165 | return device->WriteOkay("Successfully flashed partition table"); | 
|  | 166 | } | 
|  | 167 |  | 
| David Anderson | 96a9fd4 | 2018-11-05 15:21:44 -0800 | [diff] [blame] | 168 | std::set<std::string> partitions_to_keep; | 
|  | 169 | for (const auto& partition : old_metadata->partitions) { | 
|  | 170 | // Preserve partitions in the other slot, but not the current slot. | 
|  | 171 | std::string partition_name = GetPartitionName(partition); | 
|  | 172 | if (!slot_suffix.empty() && GetPartitionSlotSuffix(partition_name) == slot_suffix) { | 
|  | 173 | continue; | 
|  | 174 | } | 
| Yifan Hong | 0e13bba | 2019-08-29 16:29:22 -0700 | [diff] [blame] | 175 | std::string group_name = GetPartitionGroupName(old_metadata->groups[partition.group_index]); | 
|  | 176 | // Skip partitions in the COW group | 
|  | 177 | if (group_name == android::snapshot::kCowGroupName) { | 
|  | 178 | continue; | 
|  | 179 | } | 
| David Anderson | 96a9fd4 | 2018-11-05 15:21:44 -0800 | [diff] [blame] | 180 | partitions_to_keep.emplace(partition_name); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | // Do not preserve the scratch partition. | 
|  | 184 | partitions_to_keep.erase("scratch"); | 
|  | 185 |  | 
|  | 186 | if (!partitions_to_keep.empty()) { | 
|  | 187 | std::unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(*new_metadata.get()); | 
|  | 188 | if (!builder->ImportPartitions(*old_metadata.get(), partitions_to_keep)) { | 
|  | 189 | return device->WriteFail( | 
|  | 190 | "Old partitions are not compatible with the new super layout; wipe needed"); | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | new_metadata = builder->Export(); | 
|  | 194 | if (!new_metadata) { | 
|  | 195 | return device->WriteFail("Unable to build new partition table; wipe needed"); | 
|  | 196 | } | 
|  | 197 | } | 
|  | 198 |  | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 199 | // Write the new table to every metadata slot. | 
| David Anderson | 4d307b0 | 2018-12-17 17:07:34 -0800 | [diff] [blame] | 200 | if (!UpdateAllPartitionMetadata(device, super_name, *new_metadata.get())) { | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 201 | return device->WriteFail("Unable to write new partition table"); | 
|  | 202 | } | 
| Mark Salyzyn | 307a41f | 2018-11-26 09:57:17 -0800 | [diff] [blame] | 203 | fs_mgr_overlayfs_teardown(); | 
| David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 204 | return device->WriteOkay("Successfully updated partition table"); | 
|  | 205 | } |