blob: 42b848e92fa655eefda796f15de89ab726c282e1 [file] [log] [blame]
Steven Moreland1dba6a82024-02-16 22:32:43 +00001/*
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 */
Steven Moreland5a4a41e2024-02-16 22:58:50 +000016#include <android-base/properties.h>
Steven Moreland1dba6a82024-02-16 22:32:43 +000017#include <bootloader_message/bootloader_message.h>
18#include <log/log.h>
19
20#include <string>
21
22#include <cstdio>
23
24static void log(const char* fmt, ...) __attribute__((format(printf, 1, 2)));
25static void log(const char* fmt, ...) {
26 va_list va;
27 va_start(va, fmt);
28
29 va_list vb;
30 va_copy(vb, va);
31
32 __android_log_vprint(ANDROID_LOG_ERROR, "misctrl", fmt, va);
33 va_end(va);
34
35 vfprintf(stderr, fmt, vb);
36 fprintf(stderr, "\n");
37 va_end(vb);
38}
39
Steven Moreland5a4a41e2024-02-16 22:58:50 +000040static int check_control_message() {
41 misc_control_message m;
42 std::string err;
43 if (!ReadMiscControlMessage(&m, &err)) {
44 log("Could not read misctrl message: %s", err.c_str());
45 return 1;
46 }
47
48 if (m.magic != MISC_CONTROL_MAGIC_HEADER || m.version != MISC_CONTROL_MESSAGE_VERSION) {
49 log("misctrl message invalid, resetting it");
50 m = { .version = MISC_CONTROL_MESSAGE_VERSION,
51 .magic = MISC_CONTROL_MAGIC_HEADER,
52 .misctrl_flags = 0 };
53 }
54
55 int res = 0;
56
57 const size_t ps = getpagesize();
58
59 if (ps != 4096 && ps != 16384) {
60 log("Unrecognized page size: %zu", ps);
61 res = 1;
62 }
63
64 if (ps == 16384) {
65 m.misctrl_flags |= MISC_CONTROL_16KB_BEFORE;
66 }
67
68 bool before_16kb = m.misctrl_flags & MISC_CONTROL_16KB_BEFORE;
69 res |= android::base::SetProperty("ro.misctrl.16kb_before", before_16kb ? "1" : "0");
70
71 if (!WriteMiscControlMessage(m, &err)) {
72 log("Could not write misctrl message: %s", err.c_str());
73 res |= 1;
74 }
75
76 return res;
77}
78
Steven Moreland1dba6a82024-02-16 22:32:43 +000079static int check_reserved_space() {
80 bool empty;
81 std::string err;
82 bool success = CheckReservedSystemSpaceEmpty(&empty, &err);
83 if (!success) {
84 log("Could not read reserved space: %s", err.c_str());
85 return 1;
86 }
87 log("System reserved space empty? %d.", empty);
88
89 if (!err.empty()) {
90 constexpr size_t kPrintChunkSize = 256;
91 for (size_t i = 0; i < err.size(); i += kPrintChunkSize) {
92 log("DATA: %s", err.substr(i, kPrintChunkSize).c_str());
93 }
94 }
95
96 return empty ? 0 : 1;
97}
98
99int main() {
Steven Moreland5a4a41e2024-02-16 22:58:50 +0000100 int err = 0;
101 err |= check_control_message();
102 err |= check_reserved_space();
103 return err;
Steven Moreland1dba6a82024-02-16 22:32:43 +0000104}