blob: db52121eb0409d02570544dd92081b4d3be1f48d [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>
Yabin Cuia58a6db2016-04-06 15:52:18 -070020
21#include <string>
22#include <vector>
23
24#include <android-base/file.h>
Elliott Hughescb220402016-09-23 15:30:55 -070025#include <android-base/properties.h>
Yabin Cuia58a6db2016-04-06 15:52:18 -070026#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) {
Elliott Hughescb220402016-09-23 15:30:55 -070033 std::string ro_hardware = android::base::GetProperty("ro.hardware", "");
34 if (ro_hardware.empty()) {
Yabin Cuia58a6db2016-04-06 15:52:18 -070035 *err = "failed to get ro.hardware";
36 return nullptr;
37 }
Elliott Hughescb220402016-09-23 15:30:55 -070038 // The fstab path is always "/fstab.${ro.hardware}".
39 std::string fstab_path = "/fstab." + ro_hardware;
Yabin Cuia58a6db2016-04-06 15:52:18 -070040 struct fstab* fstab = fs_mgr_read_fstab(fstab_path.c_str());
41 if (fstab == nullptr) {
42 *err = "failed to read " + fstab_path;
43 }
44 return fstab;
45}
46
47static std::string get_misc_blk_device(std::string* err) {
48 struct fstab* fstab = read_fstab(err);
49 if (fstab == nullptr) {
50 return "";
51 }
52 fstab_rec* record = fs_mgr_get_entry_for_mount_point(fstab, "/misc");
53 if (record == nullptr) {
54 *err = "failed to find /misc partition";
55 return "";
56 }
57 return record->blk_device;
58}
59
60static bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
61 std::string misc_blk_device = get_misc_blk_device(err);
62 if (misc_blk_device.empty()) {
63 return false;
64 }
65 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC));
66 if (fd.get() == -1) {
67 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
68 strerror(errno));
69 return false;
70 }
71 if (!android::base::WriteFully(fd.get(), &boot, sizeof(boot))) {
72 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
73 strerror(errno));
74 return false;
75 }
76 // TODO: O_SYNC and fsync duplicates each other?
77 if (fsync(fd.get()) == -1) {
78 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
79 strerror(errno));
80 return false;
81 }
82 return true;
83}
84
85bool clear_bootloader_message(std::string* err) {
86 bootloader_message boot = {};
87 return write_bootloader_message(boot, err);
88}
89
90bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
91 bootloader_message boot = {};
92 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
93 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
94 for (const auto& s : options) {
95 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
96 if (s.back() != '\n') {
97 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
98 }
99 }
100 return write_bootloader_message(boot, err);
101}
102
103extern "C" bool write_bootloader_message(const char* options) {
104 std::string err;
105 return write_bootloader_message({options}, &err);
106}