blob: e3d84e098d89b961f7d27f1d62b8028f1ca6207e [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>
24
25#include <android-base/logging.h>
26#include <android-base/strings.h>
27#include <ext4_utils/ext4_utils.h>
David Anderson38b3c7a2018-08-15 16:27:42 -070028#include <liblp/builder.h>
29#include <liblp/liblp.h>
David Anderson12211d12018-07-24 15:21:20 -070030#include <sparse/sparse.h>
31
32#include "fastboot_device.h"
33#include "utility.h"
34
35namespace {
36
37constexpr uint32_t SPARSE_HEADER_MAGIC = 0xed26ff3a;
38
39} // namespace
40
David Anderson38b3c7a2018-08-15 16:27:42 -070041using namespace android::fs_mgr;
42
David Anderson12211d12018-07-24 15:21:20 -070043int FlashRawDataChunk(int fd, const char* data, size_t len) {
44 size_t ret = 0;
45 while (ret < len) {
46 int this_len = std::min(static_cast<size_t>(1048576UL * 8), len - ret);
47 int this_ret = write(fd, data, this_len);
48 if (this_ret < 0) {
49 PLOG(ERROR) << "Failed to flash data of len " << len;
50 return -1;
51 }
52 data += this_ret;
53 ret += this_ret;
54 }
55 return 0;
56}
57
58int FlashRawData(int fd, const std::vector<char>& downloaded_data) {
59 int ret = FlashRawDataChunk(fd, downloaded_data.data(), downloaded_data.size());
60 if (ret < 0) {
61 return -errno;
62 }
63 return ret;
64}
65
66int WriteCallback(void* priv, const void* data, size_t len) {
67 int fd = reinterpret_cast<long long>(priv);
68 if (!data) {
69 return lseek64(fd, len, SEEK_CUR) >= 0 ? 0 : -errno;
70 }
71 return FlashRawDataChunk(fd, reinterpret_cast<const char*>(data), len);
72}
73
74int FlashSparseData(int fd, std::vector<char>& downloaded_data) {
Hridya Valsarajuaec0de52018-10-10 13:09:41 -070075 struct sparse_file* file = sparse_file_import_buf(downloaded_data.data(), true, false);
David Anderson12211d12018-07-24 15:21:20 -070076 if (!file) {
77 return -ENOENT;
78 }
79 return sparse_file_callback(file, false, false, WriteCallback, reinterpret_cast<void*>(fd));
80}
81
82int FlashBlockDevice(int fd, std::vector<char>& downloaded_data) {
83 lseek64(fd, 0, SEEK_SET);
84 if (downloaded_data.size() >= sizeof(SPARSE_HEADER_MAGIC) &&
85 *reinterpret_cast<uint32_t*>(downloaded_data.data()) == SPARSE_HEADER_MAGIC) {
86 return FlashSparseData(fd, downloaded_data);
87 } else {
88 return FlashRawData(fd, downloaded_data);
89 }
90}
91
92int Flash(FastbootDevice* device, const std::string& partition_name) {
93 PartitionHandle handle;
94 if (!OpenPartition(device, partition_name, &handle)) {
95 return -ENOENT;
96 }
97
98 std::vector<char> data = std::move(device->download_data());
99 if (data.size() == 0) {
100 return -EINVAL;
101 } else if (data.size() > get_block_device_size(handle.fd())) {
102 return -EOVERFLOW;
103 }
104 return FlashBlockDevice(handle.fd(), data);
105}
David Anderson38b3c7a2018-08-15 16:27:42 -0700106
David Andersonb6134a62018-10-26 13:08:44 -0700107bool UpdateSuper(FastbootDevice* device, const std::string& super_name, bool wipe) {
David Anderson38b3c7a2018-08-15 16:27:42 -0700108 std::vector<char> data = std::move(device->download_data());
109 if (data.empty()) {
110 return device->WriteFail("No data available");
111 }
112
113 std::unique_ptr<LpMetadata> new_metadata = ReadFromImageBlob(data.data(), data.size());
114 if (!new_metadata) {
115 return device->WriteFail("Data is not a valid logical partition metadata image");
116 }
117
118 // If we are unable to read the existing metadata, then the super partition
119 // is corrupt. In this case we reflash the whole thing using the provided
120 // image.
121 std::string slot_suffix = device->GetCurrentSlot();
122 uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
David Andersonb6134a62018-10-26 13:08:44 -0700123 if (wipe || !ReadMetadata(super_name, slot_number)) {
124 if (!FlashPartitionTable(super_name, *new_metadata.get())) {
David Anderson38b3c7a2018-08-15 16:27:42 -0700125 return device->WriteFail("Unable to flash new partition table");
126 }
127 return device->WriteOkay("Successfully flashed partition table");
128 }
129
David Anderson38b3c7a2018-08-15 16:27:42 -0700130 // Write the new table to every metadata slot.
131 bool ok = true;
132 for (size_t i = 0; i < new_metadata->geometry.metadata_slot_count; i++) {
David Andersonb6134a62018-10-26 13:08:44 -0700133 ok &= UpdatePartitionTable(super_name, *new_metadata.get(), i);
David Anderson38b3c7a2018-08-15 16:27:42 -0700134 }
135
136 if (!ok) {
137 return device->WriteFail("Unable to write new partition table");
138 }
139 return device->WriteOkay("Successfully updated partition table");
140}