blob: e2b7cd2c6f0b6d7c2551c13864afbd9c73fe3f86 [file] [log] [blame]
David Anderson12211d12018-07-24 15:21:20 -07001/*
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 Vyshetskyd5f3da82021-11-04 10:27:06 -070019#include <string.h>
David Anderson12211d12018-07-24 15:21:20 -070020#include <sys/stat.h>
21#include <unistd.h>
22
23#include <algorithm>
24#include <memory>
David Anderson23243492019-12-17 00:58:31 -080025#include <optional>
Mark Salyzyn044f04b2018-10-12 09:33:44 -070026#include <set>
27#include <string>
David Anderson12211d12018-07-24 15:21:20 -070028
Mark Salyzyn044f04b2018-10-12 09:33:44 -070029#include <android-base/file.h>
David Anderson12211d12018-07-24 15:21:20 -070030#include <android-base/logging.h>
joker.yang09090942021-03-25 16:03:02 +080031#include <android-base/properties.h>
David Anderson12211d12018-07-24 15:21:20 -070032#include <android-base/strings.h>
33#include <ext4_utils/ext4_utils.h>
Mark Salyzyn044f04b2018-10-12 09:33:44 -070034#include <fs_mgr_overlayfs.h>
35#include <fstab/fstab.h>
Steve Mucklea9b34432020-05-12 16:21:41 -070036#include <libavb/libavb.h>
David Anderson38b3c7a2018-08-15 16:27:42 -070037#include <liblp/builder.h>
38#include <liblp/liblp.h>
Yifan Hong0e13bba2019-08-29 16:29:22 -070039#include <libsnapshot/snapshot.h>
David Anderson12211d12018-07-24 15:21:20 -070040#include <sparse/sparse.h>
41
42#include "fastboot_device.h"
43#include "utility.h"
44
Mark Salyzyn044f04b2018-10-12 09:33:44 -070045using namespace android::fs_mgr;
46using namespace std::literals;
47
David Anderson12211d12018-07-24 15:21:20 -070048namespace {
49
50constexpr uint32_t SPARSE_HEADER_MAGIC = 0xed26ff3a;
51
Mark Salyzyn044f04b2018-10-12 09:33:44 -070052void 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 Anderson12211d12018-07-24 15:21:20 -070058
Mark Salyzyn044f04b2018-10-12 09:33:44 -070059 // Convert partition_name to a validated mount point and wipe.
Tom Cherryb688d912019-01-28 12:34:33 -080060 Fstab fstab;
61 ReadDefaultFstab(&fstab);
62
David Anderson23243492019-12-17 00:58:31 -080063 std::optional<AutoMountMetadata> mount_metadata;
Tom Cherryb688d912019-01-28 12:34:33 -080064 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 Salyzyn044f04b2018-10-12 09:33:44 -070070 if ((partition + device->GetCurrentSlot()) == partition_name) {
David Anderson23243492019-12-17 00:58:31 -080071 mount_metadata.emplace();
Yo Chiang66d0d962020-10-27 19:07:37 +080072 android::fs_mgr::TeardownAllOverlayForMountPoint(entry.mount_point);
Mark Salyzyn044f04b2018-10-12 09:33:44 -070073 }
74 }
75}
76
77} // namespace
David Anderson38b3c7a2018-08-15 16:27:42 -070078
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -070079int FlashRawDataChunk(PartitionHandle* handle, const char* data, size_t len) {
David Anderson12211d12018-07-24 15:21:20 -070080 size_t ret = 0;
Konstantin Vyshetskyd5f3da82021-11-04 10:27:06 -070081 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 Anderson12211d12018-07-24 15:21:20 -070091 while (ret < len) {
Konstantin Vyshetskyd5f3da82021-11-04 10:27:06 -070092 int this_len = std::min(max_write_size, len - ret);
93 memcpy(aligned_buffer_unique_ptr.get(), data, this_len);
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -070094 int this_ret = write(handle->fd(), aligned_buffer_unique_ptr.get(), this_len);
David Anderson12211d12018-07-24 15:21:20 -070095 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
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700105int FlashRawData(PartitionHandle* handle, const std::vector<char>& downloaded_data) {
106 int ret = FlashRawDataChunk(handle, downloaded_data.data(), downloaded_data.size());
David Anderson12211d12018-07-24 15:21:20 -0700107 if (ret < 0) {
108 return -errno;
109 }
110 return ret;
111}
112
113int WriteCallback(void* priv, const void* data, size_t len) {
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700114 PartitionHandle* handle = reinterpret_cast<PartitionHandle*>(priv);
David Anderson12211d12018-07-24 15:21:20 -0700115 if (!data) {
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700116 return lseek64(handle->fd(), len, SEEK_CUR) >= 0 ? 0 : -errno;
David Anderson12211d12018-07-24 15:21:20 -0700117 }
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700118 return FlashRawDataChunk(handle, reinterpret_cast<const char*>(data), len);
David Anderson12211d12018-07-24 15:21:20 -0700119}
120
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700121int FlashSparseData(PartitionHandle* handle, std::vector<char>& downloaded_data) {
Keith Moka3b72062021-12-31 05:09:32 +0000122 struct sparse_file* file = sparse_file_import_buf(downloaded_data.data(),
123 downloaded_data.size(), true, false);
David Anderson12211d12018-07-24 15:21:20 -0700124 if (!file) {
Keith Moka3b72062021-12-31 05:09:32 +0000125 // Invalid sparse format
126 return -EINVAL;
David Anderson12211d12018-07-24 15:21:20 -0700127 }
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700128 return sparse_file_callback(file, false, false, WriteCallback, reinterpret_cast<void*>(handle));
David Anderson12211d12018-07-24 15:21:20 -0700129}
130
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700131int FlashBlockDevice(PartitionHandle* handle, std::vector<char>& downloaded_data) {
132 lseek64(handle->fd(), 0, SEEK_SET);
David Anderson12211d12018-07-24 15:21:20 -0700133 if (downloaded_data.size() >= sizeof(SPARSE_HEADER_MAGIC) &&
134 *reinterpret_cast<uint32_t*>(downloaded_data.data()) == SPARSE_HEADER_MAGIC) {
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700135 return FlashSparseData(handle, downloaded_data);
David Anderson12211d12018-07-24 15:21:20 -0700136 } else {
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700137 return FlashRawData(handle, downloaded_data);
David Anderson12211d12018-07-24 15:21:20 -0700138 }
139}
140
Steve Mucklea9b34432020-05-12 16:21:41 -0700141static 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 Anderson12211d12018-07-24 15:21:20 -0700162int Flash(FastbootDevice* device, const std::string& partition_name) {
163 PartitionHandle handle;
Konstantin Vyshetskyd5f3da82021-11-04 10:27:06 -0700164 if (!OpenPartition(device, partition_name, &handle, O_WRONLY | O_DIRECT)) {
David Anderson12211d12018-07-24 15:21:20 -0700165 return -ENOENT;
166 }
167
168 std::vector<char> data = std::move(device->download_data());
169 if (data.size() == 0) {
170 return -EINVAL;
Steve Mucklea9b34432020-05-12 16:21:41 -0700171 }
172 uint64_t block_device_size = get_block_device_size(handle.fd());
173 if (data.size() > block_device_size) {
David Anderson12211d12018-07-24 15:21:20 -0700174 return -EOVERFLOW;
Steve Mucklea9b34432020-05-12 16:21:41 -0700175 } else if (data.size() < block_device_size &&
176 (partition_name == "boot" || partition_name == "boot_a" ||
Devin Moore1af12022022-01-06 01:11:23 +0000177 partition_name == "boot_b" || partition_name == "init_boot" ||
178 partition_name == "init_boot_a" || partition_name == "init_boot_b")) {
Steve Mucklea9b34432020-05-12 16:21:41 -0700179 CopyAVBFooter(&data, block_device_size);
David Anderson12211d12018-07-24 15:21:20 -0700180 }
joker.yang09090942021-03-25 16:03:02 +0800181 if (android::base::GetProperty("ro.system.build.type", "") != "user") {
182 WipeOverlayfsForPartition(device, partition_name);
183 }
Konstantin Vyshetskyb3e18292022-03-17 17:33:35 -0700184 int result = FlashBlockDevice(&handle, data);
Tom Cherrye4a6ed82020-03-30 14:54:49 -0700185 sync();
186 return result;
David Anderson12211d12018-07-24 15:21:20 -0700187}
David Anderson38b3c7a2018-08-15 16:27:42 -0700188
David Anderson982c3412022-02-08 22:06:44 -0800189static void RemoveScratchPartition() {
190 AutoMountMetadata mount_metadata;
191 android::fs_mgr::TeardownAllOverlayForMountPoint();
192}
193
David Andersonb6134a62018-10-26 13:08:44 -0700194bool UpdateSuper(FastbootDevice* device, const std::string& super_name, bool wipe) {
David Anderson38b3c7a2018-08-15 16:27:42 -0700195 std::vector<char> data = std::move(device->download_data());
196 if (data.empty()) {
197 return device->WriteFail("No data available");
198 }
199
200 std::unique_ptr<LpMetadata> new_metadata = ReadFromImageBlob(data.data(), data.size());
201 if (!new_metadata) {
202 return device->WriteFail("Data is not a valid logical partition metadata image");
203 }
204
David Andersona48f86b2018-12-20 16:55:04 -0800205 if (!FindPhysicalPartition(super_name)) {
206 return device->WriteFail("Cannot find " + super_name +
207 ", build may be missing broken or missing boot_devices");
208 }
209
David Anderson1f670ef2021-08-09 12:33:46 -0700210 std::string slot_suffix = device->GetCurrentSlot();
211 uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
212
213 std::string other_slot_suffix;
214 if (!slot_suffix.empty()) {
215 other_slot_suffix = (slot_suffix == "_a") ? "_b" : "_a";
216 }
217
David Anderson38b3c7a2018-08-15 16:27:42 -0700218 // If we are unable to read the existing metadata, then the super partition
219 // is corrupt. In this case we reflash the whole thing using the provided
220 // image.
David Anderson96a9fd42018-11-05 15:21:44 -0800221 std::unique_ptr<LpMetadata> old_metadata = ReadMetadata(super_name, slot_number);
222 if (wipe || !old_metadata) {
David Andersonb6134a62018-10-26 13:08:44 -0700223 if (!FlashPartitionTable(super_name, *new_metadata.get())) {
David Anderson38b3c7a2018-08-15 16:27:42 -0700224 return device->WriteFail("Unable to flash new partition table");
225 }
David Anderson982c3412022-02-08 22:06:44 -0800226 RemoveScratchPartition();
Tom Cherrye4a6ed82020-03-30 14:54:49 -0700227 sync();
David Anderson38b3c7a2018-08-15 16:27:42 -0700228 return device->WriteOkay("Successfully flashed partition table");
229 }
230
David Anderson96a9fd42018-11-05 15:21:44 -0800231 std::set<std::string> partitions_to_keep;
David Anderson1f670ef2021-08-09 12:33:46 -0700232 bool virtual_ab = android::base::GetBoolProperty("ro.virtual_ab.enabled", false);
David Anderson96a9fd42018-11-05 15:21:44 -0800233 for (const auto& partition : old_metadata->partitions) {
234 // Preserve partitions in the other slot, but not the current slot.
235 std::string partition_name = GetPartitionName(partition);
David Anderson1f670ef2021-08-09 12:33:46 -0700236 if (!slot_suffix.empty()) {
237 auto part_suffix = GetPartitionSlotSuffix(partition_name);
238 if (part_suffix == slot_suffix || (part_suffix == other_slot_suffix && virtual_ab)) {
239 continue;
240 }
David Anderson96a9fd42018-11-05 15:21:44 -0800241 }
Yifan Hong0e13bba2019-08-29 16:29:22 -0700242 std::string group_name = GetPartitionGroupName(old_metadata->groups[partition.group_index]);
243 // Skip partitions in the COW group
244 if (group_name == android::snapshot::kCowGroupName) {
245 continue;
246 }
David Anderson96a9fd42018-11-05 15:21:44 -0800247 partitions_to_keep.emplace(partition_name);
248 }
249
250 // Do not preserve the scratch partition.
251 partitions_to_keep.erase("scratch");
252
253 if (!partitions_to_keep.empty()) {
254 std::unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(*new_metadata.get());
255 if (!builder->ImportPartitions(*old_metadata.get(), partitions_to_keep)) {
256 return device->WriteFail(
257 "Old partitions are not compatible with the new super layout; wipe needed");
258 }
259
260 new_metadata = builder->Export();
261 if (!new_metadata) {
262 return device->WriteFail("Unable to build new partition table; wipe needed");
263 }
264 }
265
David Anderson38b3c7a2018-08-15 16:27:42 -0700266 // Write the new table to every metadata slot.
David Anderson4d307b02018-12-17 17:07:34 -0800267 if (!UpdateAllPartitionMetadata(device, super_name, *new_metadata.get())) {
David Anderson38b3c7a2018-08-15 16:27:42 -0700268 return device->WriteFail("Unable to write new partition table");
269 }
David Anderson982c3412022-02-08 22:06:44 -0800270 RemoveScratchPartition();
Tom Cherrye4a6ed82020-03-30 14:54:49 -0700271 sync();
David Anderson38b3c7a2018-08-15 16:27:42 -0700272 return device->WriteOkay("Successfully updated partition table");
273}