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 #include "init_stage.h"
17 
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <sys/ioctl.h>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include "init_log.h"
27 
SendCmd(int cmd,unsigned long arg)28 static int SendCmd(int cmd, unsigned long arg)
29 {
30     int fd = open(QUICKSTART_NODE, O_RDONLY);
31     if (fd != -1) {
32         int ret = ioctl(fd, cmd, arg);
33         if (ret == -1) {
34             INIT_LOGE("[Init] [ERR] %d!", errno);
35         }
36         close(fd);
37         return ret;
38     }
39     INIT_LOGE("[Init] [ERR] %d!", errno);
40     return fd;
41 }
42 
InitListen(unsigned long eventMask,unsigned int wait)43 int InitListen(unsigned long eventMask, unsigned int wait)
44 {
45     QuickstartListenArgs args;
46     args.wait = wait;
47     args.events = eventMask;
48     return SendCmd(QUICKSTART_LISTEN, (uintptr_t)&args);
49 }
50 
NotifyInit(unsigned long event)51 int NotifyInit(unsigned long event)
52 {
53     return SendCmd(QUICKSTART_NOTIFY, event);
54 }
55 
SystemInitStage(QuickstartStage stage)56 int SystemInitStage(QuickstartStage stage)
57 {
58     if (stage >= QS_STAGE_LIMIT || stage < QS_STAGE1) {
59         INIT_LOGE("[Init] the stage(%d) is not expected!", stage);
60         return -1;
61     }
62     return SendCmd(QUICKSTART_STAGE(stage), 0);
63 }
64 
TriggerStage(unsigned int event,unsigned int wait,QuickstartStage stagelevel)65 void TriggerStage(unsigned int event, unsigned int wait, QuickstartStage stagelevel)
66 {
67     int ret = InitListen(event, wait);
68     if (ret != 0) {
69         SystemInitStage(stagelevel);
70     }
71 }
72 
InitStageFinished(void)73 int InitStageFinished(void)
74 {
75     return unlink(QUICKSTART_NODE);
76 }
77