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 #ifndef __CORE_FS_MGR_H
18 #define __CORE_FS_MGR_H
19 
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <linux/dm-ioctl.h>
24 
25 // C++ only headers
26 // TODO: move this into separate header files under include/fs_mgr/*.h
27 #ifdef __cplusplus
28 #include <string>
29 #endif
30 
31 // Magic number at start of verity metadata
32 #define VERITY_METADATA_MAGIC_NUMBER 0xb001b001
33 
34 // Replacement magic number at start of verity metadata to cleanly
35 // turn verity off in userdebug builds.
36 #define VERITY_METADATA_MAGIC_DISABLE 0x46464f56 // "VOFF"
37 
38 __BEGIN_DECLS
39 
40 // Verity modes
41 enum verity_mode {
42     VERITY_MODE_EIO = 0,
43     VERITY_MODE_LOGGING = 1,
44     VERITY_MODE_RESTART = 2,
45     VERITY_MODE_LAST = VERITY_MODE_RESTART,
46     VERITY_MODE_DEFAULT = VERITY_MODE_RESTART
47 };
48 
49 // Mount modes
50 enum mount_mode {
51     MOUNT_MODE_DEFAULT = 0,
52     MOUNT_MODE_EARLY = 1,
53     MOUNT_MODE_LATE = 2
54 };
55 
56 /*
57  * The entries must be kept in the same order as they were seen in the fstab.
58  * Unless explicitly requested, a lookup on mount point should always
59  * return the 1st one.
60  */
61 struct fstab {
62     int num_entries;
63     struct fstab_rec *recs;
64     char *fstab_filename;
65 };
66 
67 struct fstab_rec {
68     char *blk_device;
69     char *mount_point;
70     char *fs_type;
71     unsigned long flags;
72     char *fs_options;
73     int fs_mgr_flags;
74     char *key_loc;
75     char *verity_loc;
76     long long length;
77     char *label;
78     int partnum;
79     int swap_prio;
80     int max_comp_streams;
81     unsigned int zram_size;
82     uint64_t reserved_size;
83     unsigned int file_contents_mode;
84     unsigned int file_names_mode;
85     unsigned int erase_blk_size;
86     unsigned int logical_blk_size;
87 };
88 
89 // Callback function for verity status
90 typedef void (*fs_mgr_verity_state_callback)(struct fstab_rec *fstab,
91         const char *mount_point, int mode, int status);
92 
93 struct fstab *fs_mgr_read_fstab_default();
94 struct fstab *fs_mgr_read_fstab_dt();
95 struct fstab *fs_mgr_read_fstab(const char *fstab_path);
96 void fs_mgr_free_fstab(struct fstab *fstab);
97 
98 #define FS_MGR_MNTALL_DEV_FILE_ENCRYPTED 5
99 #define FS_MGR_MNTALL_DEV_NEEDS_RECOVERY 4
100 #define FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION 3
101 #define FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED 2
102 #define FS_MGR_MNTALL_DEV_NOT_ENCRYPTED 1
103 #define FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE 0
104 #define FS_MGR_MNTALL_FAIL (-1)
105 int fs_mgr_mount_all(struct fstab *fstab, int mount_mode);
106 
107 #define FS_MGR_DOMNT_FAILED (-1)
108 #define FS_MGR_DOMNT_BUSY (-2)
109 
110 int fs_mgr_do_mount(struct fstab *fstab, const char *n_name, char *n_blk_device,
111                     char *tmp_mount_point);
112 int fs_mgr_do_mount_one(struct fstab_rec *rec);
113 int fs_mgr_do_tmpfs_mount(const char *n_name);
114 int fs_mgr_unmount_all(struct fstab *fstab);
115 int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc,
116                           char *real_blk_device, int size);
117 bool fs_mgr_load_verity_state(int* mode);
118 bool fs_mgr_update_verity_state(fs_mgr_verity_state_callback callback);
119 int fs_mgr_add_entry(struct fstab *fstab,
120                      const char *mount_point, const char *fs_type,
121                      const char *blk_device);
122 struct fstab_rec *fs_mgr_get_entry_for_mount_point(struct fstab *fstab, const char *path);
123 int fs_mgr_is_voldmanaged(const struct fstab_rec *fstab);
124 int fs_mgr_is_nonremovable(const struct fstab_rec *fstab);
125 int fs_mgr_is_verified(const struct fstab_rec *fstab);
126 int fs_mgr_is_verifyatboot(const struct fstab_rec *fstab);
127 int fs_mgr_is_avb(const struct fstab_rec *fstab);
128 int fs_mgr_is_encryptable(const struct fstab_rec *fstab);
129 int fs_mgr_is_file_encrypted(const struct fstab_rec *fstab);
130 void fs_mgr_get_file_encryption_modes(const struct fstab_rec *fstab,
131                                       const char **contents_mode_ret,
132                                       const char **filenames_mode_ret);
133 int fs_mgr_is_convertible_to_fbe(const struct fstab_rec *fstab);
134 int fs_mgr_is_noemulatedsd(const struct fstab_rec *fstab);
135 int fs_mgr_is_notrim(struct fstab_rec *fstab);
136 int fs_mgr_is_formattable(struct fstab_rec *fstab);
137 int fs_mgr_is_slotselect(struct fstab_rec *fstab);
138 int fs_mgr_is_nofail(struct fstab_rec *fstab);
139 int fs_mgr_is_latemount(struct fstab_rec *fstab);
140 int fs_mgr_is_quota(struct fstab_rec *fstab);
141 int fs_mgr_swapon_all(struct fstab *fstab);
142 
143 int fs_mgr_do_format(struct fstab_rec *fstab, bool reserve_footer);
144 
145 #define FS_MGR_SETUP_VERITY_DISABLED (-2)
146 #define FS_MGR_SETUP_VERITY_FAIL (-1)
147 #define FS_MGR_SETUP_VERITY_SUCCESS 0
148 int fs_mgr_setup_verity(struct fstab_rec *fstab, bool wait_for_verity_dev);
149 
150 __END_DECLS
151 
152 // C++ only functions
153 // TODO: move this into separate header files under include/fs_mgr/*.h
154 #ifdef __cplusplus
155 std::string fs_mgr_get_slot_suffix();
156 #endif
157 
158 #endif /* __CORE_FS_MGR_H */
159