Steven Moreland | 1dba6a8 | 2024-02-16 22:32:43 +0000 | [diff] [blame] | 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 | */ |
Steven Moreland | f8396f1 | 2024-02-20 18:48:21 +0000 | [diff] [blame] | 16 | #include <android-base/logging.h> |
Steven Moreland | 5a4a41e | 2024-02-16 22:58:50 +0000 | [diff] [blame] | 17 | #include <android-base/properties.h> |
Steven Moreland | 1dba6a8 | 2024-02-16 22:32:43 +0000 | [diff] [blame] | 18 | #include <bootloader_message/bootloader_message.h> |
| 19 | #include <log/log.h> |
| 20 | |
| 21 | #include <string> |
| 22 | |
| 23 | #include <cstdio> |
| 24 | |
Steven Moreland | 5a4a41e | 2024-02-16 22:58:50 +0000 | [diff] [blame] | 25 | static int check_control_message() { |
| 26 | misc_control_message m; |
| 27 | std::string err; |
| 28 | if (!ReadMiscControlMessage(&m, &err)) { |
Steven Moreland | f8396f1 | 2024-02-20 18:48:21 +0000 | [diff] [blame] | 29 | LOG(ERROR) << "Could not read misctrl message: " << err.c_str(); |
Steven Moreland | 5a4a41e | 2024-02-16 22:58:50 +0000 | [diff] [blame] | 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | if (m.magic != MISC_CONTROL_MAGIC_HEADER || m.version != MISC_CONTROL_MESSAGE_VERSION) { |
Steven Moreland | f8396f1 | 2024-02-20 18:48:21 +0000 | [diff] [blame] | 34 | LOG(WARNING) << "misctrl message invalid, resetting it"; |
Steven Moreland | 5a4a41e | 2024-02-16 22:58:50 +0000 | [diff] [blame] | 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) { |
Steven Moreland | f8396f1 | 2024-02-20 18:48:21 +0000 | [diff] [blame] | 45 | LOG(ERROR) << "Unrecognized page size: " << ps; |
Steven Moreland | 5a4a41e | 2024-02-16 22:58:50 +0000 | [diff] [blame] | 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)) { |
Steven Moreland | f8396f1 | 2024-02-20 18:48:21 +0000 | [diff] [blame] | 57 | LOG(ERROR) << "Could not write misctrl message: " << err.c_str(); |
Steven Moreland | 5a4a41e | 2024-02-16 22:58:50 +0000 | [diff] [blame] | 58 | res |= 1; |
| 59 | } |
| 60 | |
| 61 | return res; |
| 62 | } |
| 63 | |
Steven Moreland | 1dba6a8 | 2024-02-16 22:32:43 +0000 | [diff] [blame] | 64 | static int check_reserved_space() { |
| 65 | bool empty; |
| 66 | std::string err; |
| 67 | bool success = CheckReservedSystemSpaceEmpty(&empty, &err); |
| 68 | if (!success) { |
Steven Moreland | f8396f1 | 2024-02-20 18:48:21 +0000 | [diff] [blame] | 69 | LOG(ERROR) << "Could not read reserved space: " << err.c_str(); |
Steven Moreland | 1dba6a8 | 2024-02-16 22:32:43 +0000 | [diff] [blame] | 70 | return 1; |
| 71 | } |
Steven Moreland | f8396f1 | 2024-02-20 18:48:21 +0000 | [diff] [blame] | 72 | LOG(INFO) << "System reserved space empty? " << empty; |
Steven Moreland | 1dba6a8 | 2024-02-16 22:32:43 +0000 | [diff] [blame] | 73 | |
| 74 | if (!err.empty()) { |
Steven Moreland | f8396f1 | 2024-02-20 18:48:21 +0000 | [diff] [blame] | 75 | LOG(ERROR) << "Reserved misc space being used: " << err; |
Steven Moreland | 1dba6a8 | 2024-02-16 22:32:43 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return empty ? 0 : 1; |
| 79 | } |
| 80 | |
Steven Moreland | f8396f1 | 2024-02-20 18:48:21 +0000 | [diff] [blame] | 81 | int main(int argc, char** argv) { |
| 82 | { |
| 83 | using namespace android::base; |
| 84 | (void)argc; |
| 85 | InitLogging(argv, TeeLogger(LogdLogger(), &StderrLogger)); |
| 86 | } |
Steven Moreland | 5a4a41e | 2024-02-16 22:58:50 +0000 | [diff] [blame] | 87 | int err = 0; |
| 88 | err |= check_control_message(); |
| 89 | err |= check_reserved_space(); |
| 90 | return err; |
Steven Moreland | 1dba6a8 | 2024-02-16 22:32:43 +0000 | [diff] [blame] | 91 | } |