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