blob: 42df31efa99246d084d283dfa6d5ce7ca9ab5f08 [file] [log] [blame]
Yabin Cuia58a6db2016-04-06 15:52:18 -07001/*
2 * Copyright (C) 2016 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
17#include <errno.h>
18#include <fcntl.h>
19#include <string.h>
20#include <sys/system_properties.h>
21
22#include <string>
23#include <vector>
24
25#include <android-base/file.h>
26#include <android-base/stringprintf.h>
27#include <android-base/unique_fd.h>
28#include <fs_mgr.h>
29
30#include "bootloader.h"
31
32static struct fstab* read_fstab(std::string* err) {
33 // The fstab path is always "/fstab.${ro.hardware}".
34 std::string fstab_path = "/fstab.";
35 char value[PROP_VALUE_MAX];
36 if (__system_property_get("ro.hardware", value) == 0) {
37 *err = "failed to get ro.hardware";
38 return nullptr;
39 }
40 fstab_path += value;
41 struct fstab* fstab = fs_mgr_read_fstab(fstab_path.c_str());
42 if (fstab == nullptr) {
43 *err = "failed to read " + fstab_path;
44 }
45 return fstab;
46}
47
48static std::string get_misc_blk_device(std::string* err) {
49 struct fstab* fstab = read_fstab(err);
50 if (fstab == nullptr) {
51 return "";
52 }
53 fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab, "/misc");
54 if (record == nullptr) {
55 *err = "failed to find /misc partition";
56 return "";
57 }
58 return record->blk_device;
59}
60
Yabin Cui6faf0262016-06-09 14:09:39 -070061static bool write_misc_partition(const void* p, size_t size, size_t misc_offset, std::string* err) {
Yabin Cuia58a6db2016-04-06 15:52:18 -070062 std::string misc_blk_device = get_misc_blk_device(err);
63 if (misc_blk_device.empty()) {
64 return false;
65 }
66 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC));
67 if (fd.get() == -1) {
68 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
69 strerror(errno));
70 return false;
71 }
Yabin Cui6faf0262016-06-09 14:09:39 -070072 if (lseek(fd.get(), static_cast<off_t>(misc_offset), SEEK_SET) !=
73 static_cast<off_t>(misc_offset)) {
74 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
75 strerror(errno));
76 return false;
77 }
78 if (!android::base::WriteFully(fd.get(), p, size)) {
Yabin Cuia58a6db2016-04-06 15:52:18 -070079 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
80 strerror(errno));
81 return false;
82 }
Yabin Cui6faf0262016-06-09 14:09:39 -070083
Yabin Cuia58a6db2016-04-06 15:52:18 -070084 // TODO: O_SYNC and fsync duplicates each other?
85 if (fsync(fd.get()) == -1) {
86 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
87 strerror(errno));
88 return false;
89 }
90 return true;
91}
92
Yabin Cui6faf0262016-06-09 14:09:39 -070093static bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
94 return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
95}
96
Yabin Cuia58a6db2016-04-06 15:52:18 -070097bool clear_bootloader_message(std::string* err) {
98 bootloader_message boot = {};
99 return write_bootloader_message(boot, err);
100}
101
102bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
103 bootloader_message boot = {};
104 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
105 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
106 for (const auto& s : options) {
107 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
108 if (s.back() != '\n') {
109 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
110 }
111 }
112 return write_bootloader_message(boot, err);
113}
114
Yabin Cui6faf0262016-06-09 14:09:39 -0700115bool write_wipe_package(const std::string& package_data, std::string* err) {
116 return write_misc_partition(package_data.data(), package_data.size(),
117 WIPE_PACKAGE_OFFSET_IN_MISC, err);
118}
119
Yabin Cuia58a6db2016-04-06 15:52:18 -0700120extern "C" bool write_bootloader_message(const char* options) {
121 std::string err;
122 return write_bootloader_message({options}, &err);
123}