1/*
2 * Copyright (c) 2021 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 __UPDATER_FS_MANAGER_PARTITIONS_H
17#define __UPDATER_FS_MANAGER_PARTITIONS_H
18
19#include <fcntl.h>
20#include <string>
21#include <vector>
22#include <sys/types.h>
23
24
25namespace Updater {
26#define RD_MODE (O_RDONLY)
27#define WR_MODE (O_WRONLY)
28#define RW_MODE (O_RDWR)
29#define EX_MODE (O_EXCL)
30
31typedef enum {
32    PARTITION_NEW,
33    PARTITION_OLD,
34} PartitionChange;
35
36typedef enum {
37    NORMAL_CHANGE,
38    NOT_CHANGE,
39    NEW_PARTITION,
40    START_CHANGE,
41    LENGTH_CHANGE,
42    PARTNUM_CHANGE,
43    PARTNAME_CHANGE,
44    FSTYPE_CHANGE,
45} PartitionChangeType;
46
47typedef enum {
48    GPT,
49    MBR,
50} DiskType;
51
52typedef enum {
53    NORMAL,
54    LOGICAL,
55    EXTENDED,
56} PartitionType;
57
58typedef enum {
59    DEVICE_UNKNOWN = 0,
60    DEVICE_SCSI = 1,
61    DEVICE_EMMC = 2,
62} DeviceType;
63
64/** We can address 2^63 sectors */
65typedef off64_t updater_sector_t;
66
67struct Partition;
68using PartitonList = std::vector<struct Partition *>;
69
70struct BlockDevice {
71    std::string devPath;
72    std::string model;         // description of hardware(manufacturer, model)
73    DeviceType type;           // SCSI, MMC, etc
74    size_t sectorSize;         // logical sector size
75    size_t physSectorSize;     // physical sector size
76    updater_sector_t length;   // device length (LBA) */
77    bool readOnly;
78    int  fd;
79    void *specific;
80};
81
82struct Partition {
83    size_t start;
84    size_t length;
85    int partNum; // Partition number.
86    int partfd;
87    std::string devName;
88    std::string partName;
89    std::string fsType; // File system type, ext4, f2fs etc.
90    PartitionType type;
91    PartitionChangeType changeType;
92};
93
94struct Disk {
95    struct BlockDevice* dev;
96    PartitonList partList;
97    DiskType type;
98    int partsum;
99};
100
101struct BlockSpecific {
102    int fd;
103    int major;
104    int minor;
105};
106
107#define BLOCK_SPECIFIC(dev) ((BlockSpecific*) (dev)->specific)
108extern int DiskAlloc(const std::string &path);
109extern int ProbeAllPartitions();
110extern Disk *GetRegisterBlockDisk(const std::string &path);
111extern struct Partition *GetPartition(const Disk &disk, int partn);
112extern int GetPartitionNumByPartName(const std::string &partname, const PartitonList &plist);
113extern int DoPartitions(PartitonList &nlist);
114extern bool SetBlockDeviceMode(BlockDevice &dev);
115} // Updater
116#endif // __UPDATER_FS_MANAGER_PARTITIONS_H
117
118