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.h"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <string.h>
22 
23 #include <string>
24 #include <vector>
25 
26 #include <android-base/file.h>
27 #include <android-base/properties.h>
28 #include <android-base/stringprintf.h>
29 #include <android-base/unique_fd.h>
30 #include <fstab/fstab.h>
31 
32 using android::fs_mgr::Fstab;
33 using android::fs_mgr::GetEntryForMountPoint;
34 using android::fs_mgr::ReadDefaultFstab;
35 
36 constexpr off_t kBootloaderControlOffset = offsetof(bootloader_message_ab, slot_suffix);
37 
get_misc_blk_device(std::string * err)38 static std::string get_misc_blk_device(std::string* err) {
39   Fstab fstab;
40   if (!ReadDefaultFstab(&fstab)) {
41     *err = "failed to read default fstab";
42     return "";
43   }
44   auto record = GetEntryForMountPoint(&fstab, "/misc");
45   if (record == nullptr) {
46     *err = "failed to find /misc partition";
47     return "";
48   }
49   return record->blk_device;
50 }
51 
52 // In recovery mode, recovery can get started and try to access the misc
53 // device before the kernel has actually created it.
wait_for_device(const std::string & blk_device,std::string * err)54 static bool wait_for_device(const std::string& blk_device, std::string* err) {
55   int tries = 0;
56   int ret;
57   err->clear();
58   do {
59     ++tries;
60     struct stat buf;
61     ret = stat(blk_device.c_str(), &buf);
62     if (ret == -1) {
63       *err += android::base::StringPrintf("failed to stat %s try %d: %s\n",
64                                           blk_device.c_str(), tries, strerror(errno));
65       sleep(1);
66     }
67   } while (ret && tries < 10);
68 
69   if (ret) {
70     *err += android::base::StringPrintf("failed to stat %s\n", blk_device.c_str());
71   }
72   return ret == 0;
73 }
74 
read_misc_partition(void * p,size_t size,const std::string & misc_blk_device,size_t offset,std::string * err)75 static bool read_misc_partition(void* p, size_t size, const std::string& misc_blk_device,
76                                 size_t offset, std::string* err) {
77   if (!wait_for_device(misc_blk_device, err)) {
78     return false;
79   }
80   android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
81   if (fd == -1) {
82     *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
83                                        strerror(errno));
84     return false;
85   }
86   if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
87     *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
88                                        strerror(errno));
89     return false;
90   }
91   if (!android::base::ReadFully(fd, p, size)) {
92     *err = android::base::StringPrintf("failed to read %s: %s", misc_blk_device.c_str(),
93                                        strerror(errno));
94     return false;
95   }
96   return true;
97 }
98 
write_misc_partition(const void * p,size_t size,const std::string & misc_blk_device,size_t offset,std::string * err)99 static bool write_misc_partition(const void* p, size_t size, const std::string& misc_blk_device,
100                                  size_t offset, std::string* err) {
101   android::base::unique_fd fd(open(misc_blk_device.c_str(), O_WRONLY));
102   if (fd == -1) {
103     *err = android::base::StringPrintf("failed to open %s: %s", misc_blk_device.c_str(),
104                                        strerror(errno));
105     return false;
106   }
107   if (lseek(fd, static_cast<off_t>(offset), SEEK_SET) != static_cast<off_t>(offset)) {
108     *err = android::base::StringPrintf("failed to lseek %s: %s", misc_blk_device.c_str(),
109                                        strerror(errno));
110     return false;
111   }
112   if (!android::base::WriteFully(fd, p, size)) {
113     *err = android::base::StringPrintf("failed to write %s: %s", misc_blk_device.c_str(),
114                                        strerror(errno));
115     return false;
116   }
117   if (fsync(fd) == -1) {
118     *err = android::base::StringPrintf("failed to fsync %s: %s", misc_blk_device.c_str(),
119                                        strerror(errno));
120     return false;
121   }
122   return true;
123 }
124 
get_bootloader_message_blk_device(std::string * err)125 std::string get_bootloader_message_blk_device(std::string* err) {
126   std::string misc_blk_device = get_misc_blk_device(err);
127   if (misc_blk_device.empty()) return "";
128   if (!wait_for_device(misc_blk_device, err)) return "";
129   return misc_blk_device;
130 }
131 
read_bootloader_message_from(bootloader_message * boot,const std::string & misc_blk_device,std::string * err)132 bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
133                                   std::string* err) {
134   return read_misc_partition(boot, sizeof(*boot), misc_blk_device,
135                              BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
136 }
137 
read_bootloader_message(bootloader_message * boot,std::string * err)138 bool read_bootloader_message(bootloader_message* boot, std::string* err) {
139   std::string misc_blk_device = get_misc_blk_device(err);
140   if (misc_blk_device.empty()) {
141     return false;
142   }
143   return read_bootloader_message_from(boot, misc_blk_device, err);
144 }
145 
read_bootloader_control_from(bootloader_control * boot_ctrl,const std::string & misc_blk_device,std::string * err)146 bool read_bootloader_control_from(bootloader_control* boot_ctrl, const std::string& misc_blk_device,
147                                   std::string* err) {
148   return read_misc_partition(boot_ctrl, sizeof(bootloader_control), misc_blk_device,
149                              kBootloaderControlOffset, err);
150 }
151 
write_bootloader_message_to(const bootloader_message & boot,const std::string & misc_blk_device,std::string * err)152 bool write_bootloader_message_to(const bootloader_message& boot, const std::string& misc_blk_device,
153                                  std::string* err) {
154   return write_misc_partition(&boot, sizeof(boot), misc_blk_device,
155                               BOOTLOADER_MESSAGE_OFFSET_IN_MISC, err);
156 }
157 
write_bootloader_message(const bootloader_message & boot,std::string * err)158 bool write_bootloader_message(const bootloader_message& boot, std::string* err) {
159   std::string misc_blk_device = get_misc_blk_device(err);
160   if (misc_blk_device.empty()) {
161     return false;
162   }
163   return write_bootloader_message_to(boot, misc_blk_device, err);
164 }
165 
write_bootloader_control_to(const bootloader_control * boot_ctrl,const std::string & misc_blk_device,std::string * err)166 bool write_bootloader_control_to(const bootloader_control* boot_ctrl, const std::string& misc_blk_device,
167                                  std::string* err) {
168   return write_misc_partition(boot_ctrl, sizeof(bootloader_control), misc_blk_device,
169                               kBootloaderControlOffset, err);
170 }
171 
clear_bootloader_message(std::string * err)172 bool clear_bootloader_message(std::string* err) {
173   bootloader_message boot = {};
174   return write_bootloader_message(boot, err);
175 }
176 
write_bootloader_message(const std::vector<std::string> & options,std::string * err)177 bool write_bootloader_message(const std::vector<std::string>& options, std::string* err) {
178   bootloader_message boot = {};
179   update_bootloader_message_in_struct(&boot, options);
180 
181   return write_bootloader_message(boot, err);
182 }
183 
update_bootloader_message(const std::vector<std::string> & options,std::string * err)184 bool update_bootloader_message(const std::vector<std::string>& options, std::string* err) {
185   bootloader_message boot;
186   if (!read_bootloader_message(&boot, err)) {
187     return false;
188   }
189   update_bootloader_message_in_struct(&boot, options);
190 
191   return write_bootloader_message(boot, err);
192 }
193 
update_bootloader_message_in_struct(bootloader_message * boot,const std::vector<std::string> & options)194 bool update_bootloader_message_in_struct(bootloader_message* boot,
195                                          const std::vector<std::string>& options) {
196   if (!boot) return false;
197   // Replace the command & recovery fields.
198   memset(boot->command, 0, sizeof(boot->command));
199   memset(boot->recovery, 0, sizeof(boot->recovery));
200 
201   strlcpy(boot->command, "boot-recovery", sizeof(boot->command));
202   strlcpy(boot->recovery, "recovery\n", sizeof(boot->recovery));
203   for (const auto& s : options) {
204     strlcat(boot->recovery, s.c_str(), sizeof(boot->recovery));
205     if (s.back() != '\n') {
206       strlcat(boot->recovery, "\n", sizeof(boot->recovery));
207     }
208   }
209   return true;
210 }
211 
write_reboot_bootloader(std::string * err)212 bool write_reboot_bootloader(std::string* err) {
213   bootloader_message boot;
214   if (!read_bootloader_message(&boot, err)) {
215     return false;
216   }
217   if (boot.command[0] != '\0') {
218     *err = "Bootloader command pending.";
219     return false;
220   }
221   strlcpy(boot.command, "bootonce-bootloader", sizeof(boot.command));
222   return write_bootloader_message(boot, err);
223 }
224 
read_wipe_package(std::string * package_data,size_t size,std::string * err)225 bool read_wipe_package(std::string* package_data, size_t size, std::string* err) {
226   std::string misc_blk_device = get_misc_blk_device(err);
227   if (misc_blk_device.empty()) {
228     return false;
229   }
230   package_data->resize(size);
231   return read_misc_partition(&(*package_data)[0], size, misc_blk_device,
232                              WIPE_PACKAGE_OFFSET_IN_MISC, err);
233 }
234 
write_wipe_package(const std::string & package_data,std::string * err)235 bool write_wipe_package(const std::string& package_data, std::string* err) {
236   std::string misc_blk_device = get_misc_blk_device(err);
237   if (misc_blk_device.empty()) {
238     return false;
239   }
240   return write_misc_partition(package_data.data(), package_data.size(), misc_blk_device,
241                               WIPE_PACKAGE_OFFSET_IN_MISC, err);
242 }
243 
write_reboot_bootloader(void)244 extern "C" bool write_reboot_bootloader(void) {
245   std::string err;
246   return write_reboot_bootloader(&err);
247 }
248 
write_bootloader_message(const char * options)249 extern "C" bool write_bootloader_message(const char* options) {
250   std::string err;
251   return write_bootloader_message({options}, &err);
252 }
253