blob: ee0aa582b85da39a32084a26647be8e1c847ca1d [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>
19#include <sys/stat.h>
20#include <unistd.h>
21
22#include <algorithm>
23#include <memory>
David Anderson23243492019-12-17 00:58:31 -080024#include <optional>
Mark Salyzyn044f04b2018-10-12 09:33:44 -070025#include <set>
26#include <string>
David Anderson12211d12018-07-24 15:21:20 -070027
Mark Salyzyn044f04b2018-10-12 09:33:44 -070028#include <android-base/file.h>
David Anderson12211d12018-07-24 15:21:20 -070029#include <android-base/logging.h>
joker.yang09090942021-03-25 16:03:02 +080030#include <android-base/properties.h>
David Anderson12211d12018-07-24 15:21:20 -070031#include <android-base/strings.h>
32#include <ext4_utils/ext4_utils.h>
Mark Salyzyn044f04b2018-10-12 09:33:44 -070033#include <fs_mgr_overlayfs.h>
34#include <fstab/fstab.h>
Steve Mucklea9b34432020-05-12 16:21:41 -070035#include <libavb/libavb.h>
David Anderson38b3c7a2018-08-15 16:27:42 -070036#include <liblp/builder.h>
37#include <liblp/liblp.h>
Yifan Hong0e13bba2019-08-29 16:29:22 -070038#include <libsnapshot/snapshot.h>
David Anderson12211d12018-07-24 15:21:20 -070039#include <sparse/sparse.h>
40
41#include "fastboot_device.h"
42#include "utility.h"
43
Mark Salyzyn044f04b2018-10-12 09:33:44 -070044using namespace android::fs_mgr;
45using namespace std::literals;
46
David Anderson12211d12018-07-24 15:21:20 -070047namespace {
48
49constexpr uint32_t SPARSE_HEADER_MAGIC = 0xed26ff3a;
50
Mark Salyzyn044f04b2018-10-12 09:33:44 -070051void WipeOverlayfsForPartition(FastbootDevice* device, const std::string& partition_name) {
52 // May be called, in the case of sparse data, multiple times so cache/skip.
53 static std::set<std::string> wiped;
54 if (wiped.find(partition_name) != wiped.end()) return;
55 wiped.insert(partition_name);
56 // Following appears to have a first time 2% impact on flashing speeds.
David Anderson12211d12018-07-24 15:21:20 -070057
Mark Salyzyn044f04b2018-10-12 09:33:44 -070058 // Convert partition_name to a validated mount point and wipe.
Tom Cherryb688d912019-01-28 12:34:33 -080059 Fstab fstab;
60 ReadDefaultFstab(&fstab);
61
David Anderson23243492019-12-17 00:58:31 -080062 std::optional<AutoMountMetadata> mount_metadata;
Tom Cherryb688d912019-01-28 12:34:33 -080063 for (const auto& entry : fstab) {
64 auto partition = android::base::Basename(entry.mount_point);
65 if ("/" == entry.mount_point) {
66 partition = "system";
67 }
68
Mark Salyzyn044f04b2018-10-12 09:33:44 -070069 if ((partition + device->GetCurrentSlot()) == partition_name) {
David Anderson23243492019-12-17 00:58:31 -080070 mount_metadata.emplace();
Yo Chiang66d0d962020-10-27 19:07:37 +080071 android::fs_mgr::TeardownAllOverlayForMountPoint(entry.mount_point);
Mark Salyzyn044f04b2018-10-12 09:33:44 -070072 }
73 }
74}
75
76} // namespace
David Anderson38b3c7a2018-08-15 16:27:42 -070077
David Anderson12211d12018-07-24 15:21:20 -070078int FlashRawDataChunk(int fd, const char* data, size_t len) {
79 size_t ret = 0;
80 while (ret < len) {
81 int this_len = std::min(static_cast<size_t>(1048576UL * 8), len - ret);
82 int this_ret = write(fd, data, this_len);
83 if (this_ret < 0) {
84 PLOG(ERROR) << "Failed to flash data of len " << len;
85 return -1;
86 }
87 data += this_ret;
88 ret += this_ret;
89 }
90 return 0;
91}
92
93int FlashRawData(int fd, const std::vector<char>& downloaded_data) {
94 int ret = FlashRawDataChunk(fd, downloaded_data.data(), downloaded_data.size());
95 if (ret < 0) {
96 return -errno;
97 }
98 return ret;
99}
100
101int WriteCallback(void* priv, const void* data, size_t len) {
102 int fd = reinterpret_cast<long long>(priv);
103 if (!data) {
104 return lseek64(fd, len, SEEK_CUR) >= 0 ? 0 : -errno;
105 }
106 return FlashRawDataChunk(fd, reinterpret_cast<const char*>(data), len);
107}
108
109int FlashSparseData(int fd, std::vector<char>& downloaded_data) {
Hridya Valsarajuaec0de52018-10-10 13:09:41 -0700110 struct sparse_file* file = sparse_file_import_buf(downloaded_data.data(), true, false);
David Anderson12211d12018-07-24 15:21:20 -0700111 if (!file) {
112 return -ENOENT;
113 }
114 return sparse_file_callback(file, false, false, WriteCallback, reinterpret_cast<void*>(fd));
115}
116
117int FlashBlockDevice(int fd, std::vector<char>& downloaded_data) {
118 lseek64(fd, 0, SEEK_SET);
119 if (downloaded_data.size() >= sizeof(SPARSE_HEADER_MAGIC) &&
120 *reinterpret_cast<uint32_t*>(downloaded_data.data()) == SPARSE_HEADER_MAGIC) {
121 return FlashSparseData(fd, downloaded_data);
122 } else {
123 return FlashRawData(fd, downloaded_data);
124 }
125}
126
Steve Mucklea9b34432020-05-12 16:21:41 -0700127static void CopyAVBFooter(std::vector<char>* data, const uint64_t block_device_size) {
128 if (data->size() < AVB_FOOTER_SIZE) {
129 return;
130 }
131 std::string footer;
132 uint64_t footer_offset = data->size() - AVB_FOOTER_SIZE;
133 for (int idx = 0; idx < AVB_FOOTER_MAGIC_LEN; idx++) {
134 footer.push_back(data->at(footer_offset + idx));
135 }
136 if (0 != footer.compare(AVB_FOOTER_MAGIC)) {
137 return;
138 }
139
140 // copy AVB footer from end of data to end of block device
141 uint64_t original_data_size = data->size();
142 data->resize(block_device_size, 0);
143 for (int idx = 0; idx < AVB_FOOTER_SIZE; idx++) {
144 data->at(block_device_size - 1 - idx) = data->at(original_data_size - 1 - idx);
145 }
146}
147
David Anderson12211d12018-07-24 15:21:20 -0700148int Flash(FastbootDevice* device, const std::string& partition_name) {
149 PartitionHandle handle;
150 if (!OpenPartition(device, partition_name, &handle)) {
151 return -ENOENT;
152 }
153
154 std::vector<char> data = std::move(device->download_data());
155 if (data.size() == 0) {
156 return -EINVAL;
Steve Mucklea9b34432020-05-12 16:21:41 -0700157 }
158 uint64_t block_device_size = get_block_device_size(handle.fd());
159 if (data.size() > block_device_size) {
David Anderson12211d12018-07-24 15:21:20 -0700160 return -EOVERFLOW;
Steve Mucklea9b34432020-05-12 16:21:41 -0700161 } else if (data.size() < block_device_size &&
162 (partition_name == "boot" || partition_name == "boot_a" ||
163 partition_name == "boot_b")) {
164 CopyAVBFooter(&data, block_device_size);
David Anderson12211d12018-07-24 15:21:20 -0700165 }
joker.yang09090942021-03-25 16:03:02 +0800166 if (android::base::GetProperty("ro.system.build.type", "") != "user") {
167 WipeOverlayfsForPartition(device, partition_name);
168 }
Tom Cherrye4a6ed82020-03-30 14:54:49 -0700169 int result = FlashBlockDevice(handle.fd(), data);
170 sync();
171 return result;
David Anderson12211d12018-07-24 15:21:20 -0700172}
David Anderson38b3c7a2018-08-15 16:27:42 -0700173
David Andersonb6134a62018-10-26 13:08:44 -0700174bool UpdateSuper(FastbootDevice* device, const std::string& super_name, bool wipe) {
David Anderson38b3c7a2018-08-15 16:27:42 -0700175 std::vector<char> data = std::move(device->download_data());
176 if (data.empty()) {
177 return device->WriteFail("No data available");
178 }
179
180 std::unique_ptr<LpMetadata> new_metadata = ReadFromImageBlob(data.data(), data.size());
181 if (!new_metadata) {
182 return device->WriteFail("Data is not a valid logical partition metadata image");
183 }
184
David Andersona48f86b2018-12-20 16:55:04 -0800185 if (!FindPhysicalPartition(super_name)) {
186 return device->WriteFail("Cannot find " + super_name +
187 ", build may be missing broken or missing boot_devices");
188 }
189
David Anderson38b3c7a2018-08-15 16:27:42 -0700190 // If we are unable to read the existing metadata, then the super partition
191 // is corrupt. In this case we reflash the whole thing using the provided
192 // image.
193 std::string slot_suffix = device->GetCurrentSlot();
194 uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
David Anderson96a9fd42018-11-05 15:21:44 -0800195 std::unique_ptr<LpMetadata> old_metadata = ReadMetadata(super_name, slot_number);
196 if (wipe || !old_metadata) {
David Andersonb6134a62018-10-26 13:08:44 -0700197 if (!FlashPartitionTable(super_name, *new_metadata.get())) {
David Anderson38b3c7a2018-08-15 16:27:42 -0700198 return device->WriteFail("Unable to flash new partition table");
199 }
Yo Chiang66d0d962020-10-27 19:07:37 +0800200 android::fs_mgr::TeardownAllOverlayForMountPoint();
Tom Cherrye4a6ed82020-03-30 14:54:49 -0700201 sync();
David Anderson38b3c7a2018-08-15 16:27:42 -0700202 return device->WriteOkay("Successfully flashed partition table");
203 }
204
David Anderson96a9fd42018-11-05 15:21:44 -0800205 std::set<std::string> partitions_to_keep;
206 for (const auto& partition : old_metadata->partitions) {
207 // Preserve partitions in the other slot, but not the current slot.
208 std::string partition_name = GetPartitionName(partition);
209 if (!slot_suffix.empty() && GetPartitionSlotSuffix(partition_name) == slot_suffix) {
210 continue;
211 }
Yifan Hong0e13bba2019-08-29 16:29:22 -0700212 std::string group_name = GetPartitionGroupName(old_metadata->groups[partition.group_index]);
213 // Skip partitions in the COW group
214 if (group_name == android::snapshot::kCowGroupName) {
215 continue;
216 }
David Anderson96a9fd42018-11-05 15:21:44 -0800217 partitions_to_keep.emplace(partition_name);
218 }
219
220 // Do not preserve the scratch partition.
221 partitions_to_keep.erase("scratch");
222
223 if (!partitions_to_keep.empty()) {
224 std::unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(*new_metadata.get());
225 if (!builder->ImportPartitions(*old_metadata.get(), partitions_to_keep)) {
226 return device->WriteFail(
227 "Old partitions are not compatible with the new super layout; wipe needed");
228 }
229
230 new_metadata = builder->Export();
231 if (!new_metadata) {
232 return device->WriteFail("Unable to build new partition table; wipe needed");
233 }
234 }
235
David Anderson38b3c7a2018-08-15 16:27:42 -0700236 // Write the new table to every metadata slot.
David Anderson4d307b02018-12-17 17:07:34 -0800237 if (!UpdateAllPartitionMetadata(device, super_name, *new_metadata.get())) {
David Anderson38b3c7a2018-08-15 16:27:42 -0700238 return device->WriteFail("Unable to write new partition table");
239 }
Yo Chiang66d0d962020-10-27 19:07:37 +0800240 android::fs_mgr::TeardownAllOverlayForMountPoint();
Tom Cherrye4a6ed82020-03-30 14:54:49 -0700241 sync();
David Anderson38b3c7a2018-08-15 16:27:42 -0700242 return device->WriteOkay("Successfully updated partition table");
243}