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