1 /* 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 <bootloader_message/bootloader_message.h> 18 19 #include <errno.h> 20 #include <fcntl.h> 21 #include <string.h> 22 #include <sys/system_properties.h> 23 24 #include <string> 25 #include <vector> 26 27 #include <android-base/file.h> 28 #include <android-base/stringprintf.h> 29 #include <android-base/unique_fd.h> 30 #include <fs_mgr.h> 31 32 static 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 48 static 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 61 // In recovery mode, recovery can get started and try to access the misc 62 // device before the kernel has actually created it. 63 static bool wait_for_device(const std::string& blk_device, std::string* err) { 64 int tries = 0; 65 int ret; 66 err->clear(); 67 do { 68 ++tries; 69 struct stat buf; 70 ret = stat(blk_device.c_str(), &buf); 71 if (ret == -1) { 72 *err += android::base::StringPrintf("failed to stat %s try %d: %s\n", 73 blk_device.c_str(), tries, strerror(errno)); 74 sleep(1); 75 } 76 } while (ret && tries < 10); 77 78 if (ret) { 79 *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str()); 80 } 81 return ret == 0; 82 } 83 84 static bool read_misc_partition(void* p, size_t size, size_t offset, std::string* err) { 85 std::string misc_blk_device = get_misc_blk_device(err); 86 if (misc_blk_device.empty()) { 87 return false; 88 } 89 if (!wait_for_device(misc_blk_device, err)) { 90 return false; 91 } 92 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY)); 93 if (fd.get() == -1) { 94 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), 95 strerror(errno)); 96 return false; 97 } 98 if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { 99 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), 100 strerror(errno)); 101 return false; 102 } 103 if (!android::base::ReadFully(fd.get(), p, size)) { 104 *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(), 105 strerror(errno)); 106 return false; 107 } 108 return true; 109 } 110 111 static bool write_misc_partition(const void* p, size_t size, size_t offset, std::string* err) { 112 std::string misc_blk_device = get_misc_blk_device(err); 113 if (misc_blk_device.empty()) { 114 return false; 115 } 116 android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY | O_SYNC)); 117 if (fd.get() == -1) { 118 *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(), 119 strerror(errno)); 120 return false; 121 } 122 if (lseek(fd.get(), static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) { 123 *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(), 124 strerror(errno)); 125 return false; 126 } 127 if (!android::base::WriteFully(fd.get(), p, size)) { 128 *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(), 129 strerror(errno)); 130 return false; 131 } 132 133 // TODO: O_SYNC and fsync duplicates each other? 134 if (fsync(fd.get()) == -1) { 135 *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(), 136 strerror(errno)); 137 return false; 138 } 139 return true; 140 } 141 142 bool read_bootloader_message(bootloader_message* boot, std::string* err) { 143 return read_misc_partition(boot, sizeof(*boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); 144 } 145 146 bool write_bootloader_message(const bootloader_message& boot, std::string* err) { 147 return write_misc_partition(&boot, sizeof(boot), BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err); 148 } 149 150 bool clear_bootloader_message(std::string* err) { 151 bootloader_message boot = {}; 152 return write_bootloader_message(boot, err); 153 } 154 155 bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) { 156 bootloader_message boot = {}; 157 strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); 158 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); 159 for (const auto& s : options) { 160 strlcat(boot.recovery, s.c_str(), sizeof(boot.recovery)); 161 if (s.back() != '\n') { 162 strlcat(boot.recovery, "\n", sizeof(boot.recovery)); 163 } 164 } 165 return write_bootloader_message(boot, err); 166 } 167 168 bool read_wipe_package(std::string* package_data, size_t size, std::string* err) { 169 package_data->resize(size); 170 return read_misc_partition(&(*package_data)[0], size, WIPE_PACKAGE_OFFSET_IN_MISC, err); 171 } 172 173 bool write_wipe_package(const std::string& package_data, std::string* err) { 174 return write_misc_partition(package_data.data(), package_data.size(), 175 WIPE_PACKAGE_OFFSET_IN_MISC, err); 176 } 177 178 extern "C" bool write_bootloader_message(const char* options) { 179 std::string err; 180 return write_bootloader_message({options}, &err); 181 } 182