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