blob: 3bb106aa09ea5632f8b37508510b5350700968c0 [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
61static bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
62 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 }
72 if (!android::base::WriteFully(fd.get(), &boot, sizeof(boot))) {
73 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
74 strerror(errno));
75 return false;
76 }
77 // TODO: O_SYNC and fsync duplicates each other?
78 if (fsync(fd.get()) == -1) {
79 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
80 strerror(errno));
81 return false;
82 }
83 return true;
84}
85
86bool clear_bootloader_message(std::string* err) {
87 bootloader_message boot = {};
88 return write_bootloader_message(boot, err);
89}
90
91bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
92 bootloader_message boot = {};
93 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
94 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
95 for (const auto& s : options) {
96 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery));
97 if (s.back() != '\n') {
98 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
99 }
100 }
101 return write_bootloader_message(boot, err);
102}
103
104extern "C" bool write_bootloader_message(const char* options) {
105 std::string err;
106 return write_bootloader_message({options}, &err);
107}