1 /*
2  * Copyright (C) 2012 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 #pragma once
18 
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <linux/dm-ioctl.h>
23 
24 #include <functional>
25 #include <string>
26 
27 #include <fstab/fstab.h>
28 
29 // Magic number at start of verity metadata
30 #define VERITY_METADATA_MAGIC_NUMBER 0xb001b001
31 
32 // Replacement magic number at start of verity metadata to cleanly
33 // turn verity off in userdebug builds.
34 #define VERITY_METADATA_MAGIC_DISABLE 0x46464f56 // "VOFF"
35 
36 // Verity modes
37 enum verity_mode {
38     VERITY_MODE_EIO = 0,
39     VERITY_MODE_LOGGING = 1,
40     VERITY_MODE_RESTART = 2,
41     VERITY_MODE_LAST = VERITY_MODE_RESTART,
42     VERITY_MODE_DEFAULT = VERITY_MODE_RESTART
43 };
44 
45 // Mount modes
46 enum mount_mode {
47     MOUNT_MODE_DEFAULT = 0,
48     MOUNT_MODE_EARLY = 1,
49     MOUNT_MODE_LATE = 2,
50     // TODO(b/135984674): remove this after refactoring fs_mgr_mount_all.
51     MOUNT_MODE_ONLY_USERDATA = 3
52 };
53 
54 #define FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED 7
55 #define FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION 6
56 #define FS_MGR_MNTALL_DEV_FILE_ENCRYPTED 5
57 #define FS_MGR_MNTALL_DEV_NEEDS_RECOVERY 4
58 #define FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION 3
59 #define FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED 2
60 #define FS_MGR_MNTALL_DEV_NOT_ENCRYPTED 1
61 #define FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE 0
62 #define FS_MGR_MNTALL_FAIL (-1)
63 // fs_mgr_mount_all() updates fstab entries that reference device-mapper.
64 int fs_mgr_mount_all(android::fs_mgr::Fstab* fstab, int mount_mode);
65 
66 #define FS_MGR_DOMNT_FAILED (-1)
67 #define FS_MGR_DOMNT_BUSY (-2)
68 #define FS_MGR_DOMNT_SUCCESS 0
69 int fs_mgr_do_mount(android::fs_mgr::Fstab* fstab, const char* n_name, char* n_blk_device,
70                     char* tmp_mount_point);
71 int fs_mgr_do_mount(android::fs_mgr::Fstab* fstab, const char* n_name, char* n_blk_device,
72                     char* tmp_mount_point, bool need_cp, bool metadata_encrypted);
73 int fs_mgr_do_mount_one(const android::fs_mgr::FstabEntry& entry,
74                         const std::string& mount_point = "");
75 int fs_mgr_do_tmpfs_mount(const char *n_name);
76 bool fs_mgr_load_verity_state(int* mode);
77 // Returns true if verity is enabled on this particular FstabEntry.
78 bool fs_mgr_is_verity_enabled(const android::fs_mgr::FstabEntry& entry);
79 bool fs_mgr_swapon_all(const android::fs_mgr::Fstab& fstab);
80 bool fs_mgr_update_logical_partition(android::fs_mgr::FstabEntry* entry);
81 
82 // Returns true if the given fstab entry has verity enabled, *and* the verity
83 // device is in "check_at_most_once" mode.
84 bool fs_mgr_verity_is_check_at_most_once(const android::fs_mgr::FstabEntry& entry);
85 
86 int fs_mgr_do_format(const android::fs_mgr::FstabEntry& entry, bool reserve_footer);
87 
88 #define FS_MGR_SETUP_VERITY_SKIPPED  (-3)
89 #define FS_MGR_SETUP_VERITY_DISABLED (-2)
90 #define FS_MGR_SETUP_VERITY_FAIL (-1)
91 #define FS_MGR_SETUP_VERITY_SUCCESS 0
92 int fs_mgr_setup_verity(android::fs_mgr::FstabEntry* fstab, bool wait_for_verity_dev);
93 
94 // Return the name of the super partition if it exists. If a slot number is
95 // specified, the super partition for the corresponding metadata slot will be
96 // returned. Otherwise, it will use the current slot.
97 std::string fs_mgr_get_super_partition_name(int slot = -1);
98 
99 enum FsMgrUmountStatus : int {
100     SUCCESS = 0,
101     ERROR_UNKNOWN = 1 << 0,
102     ERROR_UMOUNT = 1 << 1,
103     ERROR_VERITY = 1 << 2,
104     ERROR_DEVICE_MAPPER = 1 << 3,
105 };
106 // fs_mgr_umount_all() is the reverse of fs_mgr_mount_all. In particular,
107 // it destroys verity devices from device mapper after the device is unmounted.
108 int fs_mgr_umount_all(android::fs_mgr::Fstab* fstab);
109 
110 // Finds a entry in |fstab| that was used to mount a /data on |data_block_device|.
111 android::fs_mgr::FstabEntry* fs_mgr_get_mounted_entry_for_userdata(
112         android::fs_mgr::Fstab* fstab, const std::string& data_block_device);
113 int fs_mgr_remount_userdata_into_checkpointing(android::fs_mgr::Fstab* fstab);
114 
115 // Finds the dm_bow device on which this block device is stacked, or returns
116 // empty string
117 std::string fs_mgr_find_bow_device(const std::string& block_device);
118