1/*
2 * Copyright (c) 2020-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 "los_config.h"
17#ifdef LOSCFG_SHELL
18#include "reset_shell.h"
19#include "signal.h"
20#include "asm/io.h"
21#include "stdlib.h"
22#include "soc/sys_ctrl.h"
23#include "unistd.h"
24#include "shcmd.h"
25
26#ifdef __cplusplus
27#if __cplusplus
28extern "C" {
29#endif /* __cplusplus */
30#endif /* __cplusplus */
31
32Hook_Func_Node *g_hook_func_node = (Hook_Func_Node*)NULL;
33
34extern void OsReboot(void);
35void cmd_reset(void)
36{
37    osReHookFuncHandle();
38    sleep(1);/*lint !e534*/
39    /* Any value to this reg will reset the cpu */
40    OsReboot();
41}
42
43UINT32 osReHookFuncAdd(STORAGE_HOOK_FUNC handler, VOID *param)
44{
45    Hook_Func_Node *pstFuncNode;
46
47    pstFuncNode = (Hook_Func_Node*)malloc(sizeof(Hook_Func_Node));
48    if (pstFuncNode == NULL) {
49        return OS_ERROR;
50    }
51
52    pstFuncNode->pHandler = handler;
53    pstFuncNode->pParam = param;
54
55    pstFuncNode->pNext = g_hook_func_node;
56    g_hook_func_node = pstFuncNode;
57
58    return  LOS_OK;
59}
60
61UINT32 osReHookFuncDel(STORAGE_HOOK_FUNC handler)
62{
63    Hook_Func_Node *pstFuncNode = NULL;
64    Hook_Func_Node *pstCurFuncNode = NULL;
65    while (g_hook_func_node) {
66        pstCurFuncNode = g_hook_func_node;
67        if (g_hook_func_node->pHandler == handler) {
68            g_hook_func_node = g_hook_func_node->pNext;
69            free(pstCurFuncNode);
70            continue;
71        }
72        break;
73    }
74
75    if (g_hook_func_node) {
76        pstCurFuncNode = g_hook_func_node;
77        while (pstCurFuncNode->pNext) {
78            pstFuncNode = pstCurFuncNode->pNext;
79            if (pstFuncNode->pHandler == handler) {
80                pstCurFuncNode->pNext = pstFuncNode->pNext;
81                free(pstFuncNode);
82                continue;
83            }
84            pstCurFuncNode = pstCurFuncNode->pNext;
85        }
86    }
87    return LOS_OK;
88}
89
90VOID osReHookFuncHandle(VOID)
91{
92    Hook_Func_Node *pstFuncNode;
93
94    pstFuncNode = g_hook_func_node;
95    while (pstFuncNode) {
96        (void)pstFuncNode->pHandler(pstFuncNode->pParam);
97        pstFuncNode = pstFuncNode->pNext;
98    }
99}
100
101SHELLCMD_ENTRY(reset_shellcmd, CMD_TYPE_EX, "reset", 0, (CmdCallBackFunc)cmd_reset); /*lint !e19*/
102
103#ifdef __cplusplus
104#if __cplusplus
105}
106#endif
107#endif
108#endif /* LOSCFG_SHELL */
109