blob: d3dd82c92aa3bb557ab03661401eeb21f37ae2cb [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>
28#include <fs_mgr.h>
29#include <sparse/sparse.h>
30
31#include "fastboot_device.h"
32#include "utility.h"
33
34namespace {
35
36constexpr uint32_t SPARSE_HEADER_MAGIC = 0xed26ff3a;
37
38} // namespace
39
40int FlashRawDataChunk(int fd, const char* data, size_t len) {
41 size_t ret = 0;
42 while (ret < len) {
43 int this_len = std::min(static_cast<size_t>(1048576UL * 8), len - ret);
44 int this_ret = write(fd, data, this_len);
45 if (this_ret < 0) {
46 PLOG(ERROR) << "Failed to flash data of len " << len;
47 return -1;
48 }
49 data += this_ret;
50 ret += this_ret;
51 }
52 return 0;
53}
54
55int FlashRawData(int fd, const std::vector<char>& downloaded_data) {
56 int ret = FlashRawDataChunk(fd, downloaded_data.data(), downloaded_data.size());
57 if (ret < 0) {
58 return -errno;
59 }
60 return ret;
61}
62
63int WriteCallback(void* priv, const void* data, size_t len) {
64 int fd = reinterpret_cast<long long>(priv);
65 if (!data) {
66 return lseek64(fd, len, SEEK_CUR) >= 0 ? 0 : -errno;
67 }
68 return FlashRawDataChunk(fd, reinterpret_cast<const char*>(data), len);
69}
70
71int FlashSparseData(int fd, std::vector<char>& downloaded_data) {
72 struct sparse_file* file = sparse_file_import_buf(downloaded_data.data(), true, false);
73 if (!file) {
74 return -ENOENT;
75 }
76 return sparse_file_callback(file, false, false, WriteCallback, reinterpret_cast<void*>(fd));
77}
78
79int FlashBlockDevice(int fd, std::vector<char>& downloaded_data) {
80 lseek64(fd, 0, SEEK_SET);
81 if (downloaded_data.size() >= sizeof(SPARSE_HEADER_MAGIC) &&
82 *reinterpret_cast<uint32_t*>(downloaded_data.data()) == SPARSE_HEADER_MAGIC) {
83 return FlashSparseData(fd, downloaded_data);
84 } else {
85 return FlashRawData(fd, downloaded_data);
86 }
87}
88
89int Flash(FastbootDevice* device, const std::string& partition_name) {
90 PartitionHandle handle;
91 if (!OpenPartition(device, partition_name, &handle)) {
92 return -ENOENT;
93 }
94
95 std::vector<char> data = std::move(device->download_data());
96 if (data.size() == 0) {
97 return -EINVAL;
98 } else if (data.size() > get_block_device_size(handle.fd())) {
99 return -EOVERFLOW;
100 }
101 return FlashBlockDevice(handle.fd(), data);
102}