• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
24 #include <set>
25 #include <string>
26 
27 #include <android-base/file.h>
28 #include <android-base/logging.h>
29 #include <android-base/strings.h>
30 #include <ext4_utils/ext4_utils.h>
31 #include <fs_mgr_overlayfs.h>
32 #include <fstab/fstab.h>
33 #include <liblp/builder.h>
34 #include <liblp/liblp.h>
35 #include <sparse/sparse.h>
36 
37 #include "fastboot_device.h"
38 #include "utility.h"
39 
40 using namespace android::fs_mgr;
41 using namespace std::literals;
42 
43 namespace {
44 
45 constexpr uint32_t SPARSE_HEADER_MAGIC = 0xed26ff3a;
46 
WipeOverlayfsForPartition(FastbootDevice * device,const std::string & partition_name)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.
53 
54     // Convert partition_name to a validated mount point and wipe.
55     Fstab fstab;
56     ReadDefaultFstab(&fstab);
57 
58     for (const auto& entry : fstab) {
59         auto partition = android::base::Basename(entry.mount_point);
60         if ("/" == entry.mount_point) {
61             partition = "system";
62         }
63 
64         if ((partition + device->GetCurrentSlot()) == partition_name) {
65             fs_mgr_overlayfs_teardown(entry.mount_point.c_str());
66         }
67     }
68 }
69 
70 }  // namespace
71 
FlashRawDataChunk(int fd,const char * data,size_t len)72 int FlashRawDataChunk(int fd, const char* data, size_t len) {
73     size_t ret = 0;
74     while (ret < len) {
75         int this_len = std::min(static_cast<size_t>(1048576UL * 8), len - ret);
76         int this_ret = write(fd, data, this_len);
77         if (this_ret < 0) {
78             PLOG(ERROR) << "Failed to flash data of len " << len;
79             return -1;
80         }
81         data += this_ret;
82         ret += this_ret;
83     }
84     return 0;
85 }
86 
FlashRawData(int fd,const std::vector<char> & downloaded_data)87 int FlashRawData(int fd, const std::vector<char>& downloaded_data) {
88     int ret = FlashRawDataChunk(fd, downloaded_data.data(), downloaded_data.size());
89     if (ret < 0) {
90         return -errno;
91     }
92     return ret;
93 }
94 
WriteCallback(void * priv,const void * data,size_t len)95 int WriteCallback(void* priv, const void* data, size_t len) {
96     int fd = reinterpret_cast<long long>(priv);
97     if (!data) {
98         return lseek64(fd, len, SEEK_CUR) >= 0 ? 0 : -errno;
99     }
100     return FlashRawDataChunk(fd, reinterpret_cast<const char*>(data), len);
101 }
102 
FlashSparseData(int fd,std::vector<char> & downloaded_data)103 int FlashSparseData(int fd, std::vector<char>& downloaded_data) {
104     struct sparse_file* file = sparse_file_import_buf(downloaded_data.data(), true, false);
105     if (!file) {
106         return -ENOENT;
107     }
108     return sparse_file_callback(file, false, false, WriteCallback, reinterpret_cast<void*>(fd));
109 }
110 
FlashBlockDevice(int fd,std::vector<char> & downloaded_data)111 int FlashBlockDevice(int fd, std::vector<char>& downloaded_data) {
112     lseek64(fd, 0, SEEK_SET);
113     if (downloaded_data.size() >= sizeof(SPARSE_HEADER_MAGIC) &&
114         *reinterpret_cast<uint32_t*>(downloaded_data.data()) == SPARSE_HEADER_MAGIC) {
115         return FlashSparseData(fd, downloaded_data);
116     } else {
117         return FlashRawData(fd, downloaded_data);
118     }
119 }
120 
Flash(FastbootDevice * device,const std::string & partition_name)121 int Flash(FastbootDevice* device, const std::string& partition_name) {
122     PartitionHandle handle;
123     if (!OpenPartition(device, partition_name, &handle)) {
124         return -ENOENT;
125     }
126 
127     std::vector<char> data = std::move(device->download_data());
128     if (data.size() == 0) {
129         return -EINVAL;
130     } else if (data.size() > get_block_device_size(handle.fd())) {
131         return -EOVERFLOW;
132     }
133     WipeOverlayfsForPartition(device, partition_name);
134     return FlashBlockDevice(handle.fd(), data);
135 }
136 
UpdateSuper(FastbootDevice * device,const std::string & super_name,bool wipe)137 bool UpdateSuper(FastbootDevice* device, const std::string& super_name, bool wipe) {
138     std::vector<char> data = std::move(device->download_data());
139     if (data.empty()) {
140         return device->WriteFail("No data available");
141     }
142 
143     std::unique_ptr<LpMetadata> new_metadata = ReadFromImageBlob(data.data(), data.size());
144     if (!new_metadata) {
145         return device->WriteFail("Data is not a valid logical partition metadata image");
146     }
147 
148     if (!FindPhysicalPartition(super_name)) {
149         return device->WriteFail("Cannot find " + super_name +
150                                  ", build may be missing broken or missing boot_devices");
151     }
152 
153     // If we are unable to read the existing metadata, then the super partition
154     // is corrupt. In this case we reflash the whole thing using the provided
155     // image.
156     std::string slot_suffix = device->GetCurrentSlot();
157     uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
158     std::unique_ptr<LpMetadata> old_metadata = ReadMetadata(super_name, slot_number);
159     if (wipe || !old_metadata) {
160         if (!FlashPartitionTable(super_name, *new_metadata.get())) {
161             return device->WriteFail("Unable to flash new partition table");
162         }
163         fs_mgr_overlayfs_teardown();
164         return device->WriteOkay("Successfully flashed partition table");
165     }
166 
167     std::set<std::string> partitions_to_keep;
168     for (const auto& partition : old_metadata->partitions) {
169         // Preserve partitions in the other slot, but not the current slot.
170         std::string partition_name = GetPartitionName(partition);
171         if (!slot_suffix.empty() && GetPartitionSlotSuffix(partition_name) == slot_suffix) {
172             continue;
173         }
174         partitions_to_keep.emplace(partition_name);
175     }
176 
177     // Do not preserve the scratch partition.
178     partitions_to_keep.erase("scratch");
179 
180     if (!partitions_to_keep.empty()) {
181         std::unique_ptr<MetadataBuilder> builder = MetadataBuilder::New(*new_metadata.get());
182         if (!builder->ImportPartitions(*old_metadata.get(), partitions_to_keep)) {
183             return device->WriteFail(
184                     "Old partitions are not compatible with the new super layout; wipe needed");
185         }
186 
187         new_metadata = builder->Export();
188         if (!new_metadata) {
189             return device->WriteFail("Unable to build new partition table; wipe needed");
190         }
191     }
192 
193     // Write the new table to every metadata slot.
194     if (!UpdateAllPartitionMetadata(device, super_name, *new_metadata.get())) {
195         return device->WriteFail("Unable to write new partition table");
196     }
197     fs_mgr_overlayfs_teardown();
198     return device->WriteOkay("Successfully updated partition table");
199 }
200