blob: befdafacdb9de77384cca4107bb55279c0e74f31 [file] [log] [blame]
Christopher N. Hessee74d3b72018-02-24 23:33:49 -08001/*
2 * Copyright (C) 2018 The LineageOS 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 "verity_tool.h"
18
19#include <getopt.h>
20#include <stdio.h>
21#include <string.h>
22
23static void print_usage() {
24 printf("veritytool - toggle block device verification\n"
25 " --help show this help\n"
26 " --enable enable dm-verity\n"
Christopher N. Hesseb20fd512018-06-26 19:56:24 +020027 " --disable disable dm-verity\n"
28 " --show show current dm-verity state\n");
Christopher N. Hessee74d3b72018-02-24 23:33:49 -080029}
30
31int main(int argc, char** argv) {
32 int c, rc;
33 int enable = 0;
Christopher N. Hesseb20fd512018-06-26 19:56:24 +020034 int show = 0;
Christopher N. Hessee74d3b72018-02-24 23:33:49 -080035 bool flag_set = false;
36 struct option long_opts[] = {
37 {"disable", no_argument, &enable, 0},
38 {"enable", no_argument, &enable, 1},
Christopher N. Hesseb20fd512018-06-26 19:56:24 +020039 {"show", no_argument, &show, 1},
Christopher N. Hessee74d3b72018-02-24 23:33:49 -080040 {NULL, 0, NULL, 0},
41 };
42
Christopher N. Hesseb20fd512018-06-26 19:56:24 +020043 while ((c = getopt_long(argc, argv, "des", long_opts, NULL)) != -1) {
Christopher N. Hessee74d3b72018-02-24 23:33:49 -080044 switch (c) {
45 case 0:
46 flag_set = true;
47 break;
48 default:
49 print_usage();
50 exit(0);
51 }
52 }
53
54 if (!flag_set) {
55 print_usage();
56 exit(0);
57 }
58
Christopher N. Hesseb20fd512018-06-26 19:56:24 +020059 if (show) {
60 printf("dm-verity state: ");
61 switch (get_verity_state()) {
62 case VERITY_STATE_NO_DEVICE:
63 printf("NO DEVICE");
64 break;
65 case VERITY_STATE_DISABLED:
66 printf("DISABLED");
67 break;
68 case VERITY_STATE_ENABLED:
69 printf("ENABLED");
70 break;
71 default:
72 printf("UNKNOWN");
73 break;
74 }
75 printf("\n");
76 return 0;
77 }
78
Christopher N. Hessee74d3b72018-02-24 23:33:49 -080079 if (!set_verity_enabled(enable)) {
80 printf("Error occurred in set_verity_enable\n");
81 exit(EXIT_FAILURE);
82 }
83
84 printf("Set verity mode to: %s\n", enable ? "enabled" : "disabled");
85 printf("Now reboot your device for settings to take effect\n");
86 return 0;
87}