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