1 /*
2  * Copyright (c) 2024-2024 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 #include <sys/stat.h>
17 #include <sys/wait.h>
18 #include "securec.h"
19 #include "init_utils.h"
20 
21 #include "erofs_overlay_common.h"
22 
IsOverlayEnable(void)23 bool IsOverlayEnable(void)
24 {
25     char oemMode[MAX_BUFFER_LEN] = {0};
26     int ret = GetParameterFromCmdLine("oemmode", oemMode, MAX_BUFFER_LEN);
27     if (ret) {
28         BEGET_LOGE("Failed get oenmode from cmdline.");
29         return false;
30     }
31 
32     char buildvariant[MAX_BUFFER_LEN] = {0};
33     ret = GetParameterFromCmdLine("buildvariant", buildvariant, MAX_BUFFER_LEN);
34     if (ret) {
35         BEGET_LOGE("Failed get buildvariant from cmdline.");
36         return false;
37     }
38 
39     if (strcmp(oemMode, "user") == 0 || strcmp(buildvariant, "eng") != 0) {
40         BEGET_LOGI("not support overlay, oemMode:[%s] buildvariant:[%s]", oemMode, buildvariant);
41         return false;
42     }
43     BEGET_LOGI("overlay enable.");
44     return true;
45 }
46 
CheckIsExt4(const char * dev,uint64_t offset)47 bool CheckIsExt4(const char *dev, uint64_t offset)
48 {
49     int fd = open(dev, O_RDONLY | O_LARGEFILE);
50     if (fd < 0) {
51         BEGET_LOGE("cannot open [dev]:%s", dev);
52         return false;
53     }
54 
55     if (lseek(fd, offset + EXT4_SUPER_BLOCK_START_POSITION, SEEK_SET) < 0) {
56         BEGET_LOGE("cannot seek [dev]:%s", dev);
57         close(fd);
58         return false;
59     }
60     ext4_super_block superBlock;
61     ssize_t nbytes = read(fd, &superBlock, sizeof(superBlock));
62     if (nbytes != sizeof(superBlock)) {
63         BEGET_LOGE("read ext4 super block fail");
64         close(fd);
65         return false;
66     }
67 
68     if (superBlock.s_magic == EXT4_SUPER_MAGIC) {
69         BEGET_LOGI("this [dev] %s  is ext4:[block cout]: %d, [size]: %d", dev,
70             superBlock.s_blocks_count_lo, (superBlock.s_blocks_count_lo * BLOCK_SIZE_UNIT));
71         close(fd);
72         return true;
73     }
74     close(fd);
75     return false;
76 }
77 
CheckIsErofs(const char * dev)78 bool CheckIsErofs(const char *dev)
79 {
80     int fd = open(dev, O_RDONLY | O_LARGEFILE);
81     if (fd < 0) {
82         BEGET_LOGE("cannot open [dev]:%s", dev);
83         return false;
84     }
85 
86     if (lseek(fd, EROFS_SUPER_BLOCK_START_POSITION, SEEK_SET) < 0) {
87         BEGET_LOGE("cannot seek [dev]:%s", dev);
88         close(fd);
89         return false;
90     }
91     struct erofs_super_block superBlock;
92     ssize_t nbytes = read(fd, &superBlock, sizeof(superBlock));
93     if (nbytes != sizeof(superBlock)) {
94         BEGET_LOGE("read erofs super block fail");
95         close(fd);
96         return false;
97     }
98 
99     BEGET_LOGI("the [dev] %s magic [%u]", dev, superBlock.magic);
100     if (superBlock.magic == EROFS_SUPER_MAGIC) {
101         BEGET_LOGI("this [dev] %s is erofs", dev);
102         close(fd);
103         return true;
104     }
105     close(fd);
106     return false;
107 }