1/*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef STARTUP_FS_MANAGER_H
17#define STARTUP_FS_MANAGER_H
18
19#include <stdbool.h>
20#include <stdio.h>
21
22#ifdef __cplusplus
23#if __cplusplus
24extern "C" {
25#endif
26#endif
27
28/* Fs manager flags definition */
29#define FS_MANAGER_CHECK  0x00000001
30#define FS_MANAGER_WAIT  0x00000002
31#define FS_MANAGER_REQUIRED  0x00000004
32#define FS_MANAGER_NOFAIL  0x00000008
33#define FS_MANAGER_HVB  0x00000010
34#define FS_MANAGER_PROJQUOTA  0x00000020
35#define FS_MANAGER_CASEFOLD  0x00000040
36#define FS_MANAGER_COMPRESSION  0x00000080
37#define FS_MANAGER_DEDUP  0x00000100
38#define FS_MANAGER_FORMATTABLE  0x00000200
39#define NAME_SIZE 32
40#define MAX_SLOT 2
41
42#define VALID_FS_MANAGER_FLAGS (FS_MANAGER_CHECK | FS_MANAGER_WAIT | FS_MANAGER_REQUIRED)
43#define FS_MANAGER_FLAGS_ENABLED(fsMgrFlags, flag) (((fsMgrFlags) & FS_MANAGER_##flag) != 0)
44
45#define FM_MANAGER_CHECK_ENABLED(fsMgrFlags) FS_MANAGER_FLAGS_ENABLED((fsMgrFlags), CHECK)
46#define FM_MANAGER_WAIT_ENABLED(fsMgrFlags) FS_MANAGER_FLAGS_ENABLED((fsMgrFlags), WAIT)
47#define FM_MANAGER_REQUIRED_ENABLED(fsMgrFlags) FS_MANAGER_FLAGS_ENABLED((fsMgrFlags), REQUIRED)
48#define FM_MANAGER_NOFAIL_ENABLED(fsMgrFlags) FS_MANAGER_FLAGS_ENABLED((fsMgrFlags), NOFAIL)
49#define FM_MANAGER_FORMATTABLE_ENABLED(fsMgrFlags) FS_MANAGER_FLAGS_ENABLED((fsMgrFlags), FORMATTABLE)
50
51typedef enum MountStatus {
52    MOUNT_ERROR = -1,
53    MOUNT_UMOUNTED = 0,
54    MOUNT_MOUNTED = 1,
55} MountStatus;
56
57typedef struct FstabItem {
58    char *deviceName;  // Block device name
59    char *mountPoint;  // Mount point
60    char *fsType;      // File system type
61    char *mountOptions;  // File system mount options. readonly, rw, remount etc.
62    unsigned int fsManagerFlags;  // flags defined by fs manager.
63    struct FstabItem *next;
64} FstabItem;
65
66typedef struct {
67    struct FstabItem *head;
68    struct FstabItem *tail;
69} Fstab;
70
71typedef enum SlotFlag {
72    UNBOOT = 0,
73    ACTIVE = 1,
74} SlotFlag;
75
76typedef struct SlotInfo {
77    int slotName;
78    char *slotSuffix;
79    SlotFlag slotFlag;
80    unsigned int retryCount;
81    unsigned int reserved;
82} SlotInfo;
83
84Fstab* LoadFstabFromCommandLine(void);
85int GetBootSlots(void);
86int GetCurrentSlot(void);
87void ReleaseFstab(Fstab *fstab);
88Fstab *ReadFstabFromFile(const char *file, bool procMounts);
89FstabItem *FindFstabItemForPath(Fstab fstab, const char *path);
90FstabItem* FindFstabItemForMountPoint(Fstab fstab, const char *mp);
91int ParseFstabPerLine(char *str, Fstab *fstab, bool procMounts, const char *separator);
92
93int GetBlockDeviceByMountPoint(const char *mountPoint, const Fstab *fstab, char *deviceName, int nameLen);
94int GetBlockDeviceByName(const char *deviceName, const Fstab *fstab, char* miscDev, size_t size);
95bool IsSupportedFilesystem(const char *fsType);
96int DoFormat(const char *devPath, const char *fsType);
97int MountOneItem(FstabItem *item);
98MountStatus GetMountStatusForMountPoint(const char *mp);
99int MountAllWithFstabFile(const char *fstabFile, bool required);
100int MountAllWithFstab(const Fstab *fstab, bool required);
101int UmountAllWithFstabFile(const char *file);
102int MountOneWithFstabFile(const char *fstabFile, const char *devName, bool required);
103int FsManagerDmRemoveDevice(const char *devName);
104unsigned long GetMountFlags(char *mountFlag, char *fsSpecificFlags, size_t fsSpecificFlagSize,
105    const char *mountPoint);
106
107int GetBlockDevicePath(const char *partName, char *path, size_t size);
108
109// Get fscrypt policy if exist
110int LoadFscryptPolicy(char *buf, size_t size);
111#ifdef __cplusplus
112#if __cplusplus
113}
114#endif
115#endif
116
117#endif // STARTUP_FS_MANAGER_H
118