blob: cb14ba6f521c262ef6a19417d85014332b8cf2d3 [file] [log] [blame]
David Brazdilc439f782021-10-06 10:27:00 +00001/*
2 * Copyright (C) 2021 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#define LOG_TAG "pkvm_enabler"
18
19#include <errno.h>
20#include <stdlib.h>
21#include <sys/types.h>
22#include <sys/wait.h>
23#include <unistd.h>
24
25#include <log/log.h>
26
27#define KVM_DEVICE "/dev/kvm"
28#define MISC_WRITER "/vendor/bin/misc_writer"
29
30int main() {
31 char *newargv[] = { MISC_WRITER, "--set-enable-pkvm", NULL };
32 char *newenvp[] = { NULL };
33 pid_t pid;
34 int ret, wstatus;
35
36 /* Check whether KVM device exists. */
37 ret = access(KVM_DEVICE, F_OK);
38
39 /* If KVM device exists, return SUCCESS to continue booting. */
40 if (ret == 0) {
41 exit(EXIT_SUCCESS);
42 }
43
44 if (ret != -ENOENT) {
45 ALOGW("Unexpected error from access(): %d", ret);
46 }
47
48 /*
49 * If KVM device does not exist, run misc_writer and return FAILURE
50 * to force a reboot.
51 */
52 pid = fork();
53 if (pid == -1) {
54 ALOGE("Could not fork: %d", errno);
55 exit(EXIT_FAILURE);
56 }
57
58 if (pid == 0) {
59 execve(MISC_WRITER, newargv, newenvp);
60 ALOGE("Could not execute " MISC_WRITER ": %d", errno);
61 _exit(EXIT_FAILURE);
62 }
63
64 waitpid(pid, &wstatus, 0);
65 if (WIFEXITED(wstatus)) {
66 ret = WEXITSTATUS(wstatus);
67 if (ret) {
68 ALOGE(MISC_WRITER " exit status: %d", ret);
69 }
70 } else {
71 ALOGE(MISC_WRITER " terminated unexpectedly: %d", wstatus);
72 }
73
74 exit(EXIT_FAILURE);
75}