1 /*
2  * Copyright (C) 2024 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 #include <android-base/logging.h>
17 #include <android-base/properties.h>
18 #include <bootloader_message/bootloader_message.h>
19 #include <log/log.h>
20 
21 #include <string>
22 
23 #include <cstdio>
24 
check_control_message()25 static int check_control_message() {
26   misc_control_message m;
27   std::string err;
28   if (!ReadMiscControlMessage(&m, &err)) {
29     LOG(ERROR) << "Could not read misctrl message: " << err.c_str();
30     return 1;
31   }
32 
33   if (m.magic != MISC_CONTROL_MAGIC_HEADER || m.version != MISC_CONTROL_MESSAGE_VERSION) {
34     LOG(WARNING) << "misctrl message invalid, resetting it";
35     m = { .version = MISC_CONTROL_MESSAGE_VERSION,
36           .magic = MISC_CONTROL_MAGIC_HEADER,
37           .misctrl_flags = 0 };
38   }
39 
40   int res = 0;
41 
42   const size_t ps = getpagesize();
43 
44   if (ps != 4096 && ps != 16384) {
45     LOG(ERROR) << "Unrecognized page size: " << ps;
46     res = 1;
47   }
48 
49   if (ps == 16384) {
50     m.misctrl_flags |= MISC_CONTROL_16KB_BEFORE;
51   }
52 
53   bool before_16kb = m.misctrl_flags & MISC_CONTROL_16KB_BEFORE;
54   res |= android::base::SetProperty("ro.misctrl.16kb_before", before_16kb ? "1" : "0");
55 
56   if (!WriteMiscControlMessage(m, &err)) {
57     LOG(ERROR) << "Could not write misctrl message: " << err.c_str();
58     res |= 1;
59   }
60 
61   return res;
62 }
63 
check_reserved_space()64 static int check_reserved_space() {
65   bool empty;
66   std::string err;
67   bool success = CheckReservedSystemSpaceEmpty(&empty, &err);
68   if (!success) {
69     LOG(ERROR) << "Could not read reserved space: " << err.c_str();
70     return 1;
71   }
72   LOG(INFO) << "System reserved space empty? " << empty;
73 
74   if (!err.empty()) {
75     LOG(ERROR) << "Reserved misc space being used: " << err;
76   }
77 
78   return empty ? 0 : 1;
79 }
80 
main(int argc,char ** argv)81 int main(int argc, char** argv) {
82   {
83     using namespace android::base;
84     (void)argc;
85     InitLogging(argv, TeeLogger(LogdLogger(), &StderrLogger));
86   }
87   int err = 0;
88   err |= check_control_message();
89   err |= check_reserved_space();
90   return err;
91 }
92