1 /****************************************************************************
2  * boot/nuttx/src/watchdog/watchdog.c
3  *
4  * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  ****************************************************************************/
19 
20 /****************************************************************************
21  * Included Files
22  ****************************************************************************/
23 
24 #include "watchdog/watchdog.h"
25 
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/ioctl.h>
30 
31 #include <nuttx/timers/watchdog.h>
32 
33 #include <bootutil/bootutil_log.h>
34 
35 /****************************************************************************
36  * Public Functions
37  ****************************************************************************/
38 
39 /****************************************************************************
40  * Name: mcuboot_watchdog_feed
41  *
42  * Description:
43  *   Reset the watchdog timer to the current timeout value, preventing any
44  *   imminent watchdog timeouts.
45  *
46  * Input Parameters:
47  *   None.
48  *
49  * Returned Value:
50  *   None.
51  *
52  ****************************************************************************/
53 
mcuboot_watchdog_feed(void)54 void mcuboot_watchdog_feed(void)
55 {
56   int fd;
57   int ret;
58 
59   fd = open(CONFIG_MCUBOOT_WATCHDOG_DEVPATH, O_RDONLY);
60   if (fd < 0)
61     {
62       BOOT_LOG_ERR("Failed to open %s", CONFIG_MCUBOOT_WATCHDOG_DEVPATH);
63 
64       return;
65     }
66 
67   ret = ioctl(fd, WDIOC_KEEPALIVE, 0);
68   if (ret < 0)
69     {
70       int errcode = errno;
71 
72       BOOT_LOG_ERR("Failed to ping watchdog device: %d", errcode);
73     }
74 
75   close(fd);
76 }
77 
78 /****************************************************************************
79  * Name: mcuboot_watchdog_init
80  *
81  * Description:
82  *   Initialize the watchdog timer by setting the trigger timeout and
83  *   starting it.
84  *
85  * Input Parameters:
86  *   None.
87  *
88  * Returned Value:
89  *   OK on success, ERROR if not.
90  *
91  ****************************************************************************/
92 
mcuboot_watchdog_init(void)93 int mcuboot_watchdog_init(void)
94 {
95   int fd;
96   int ret;
97 
98   fd = open(CONFIG_MCUBOOT_WATCHDOG_DEVPATH, O_RDONLY);
99   if (fd < 0)
100     {
101       BOOT_LOG_ERR("Failed to open %s", CONFIG_MCUBOOT_WATCHDOG_DEVPATH);
102       goto errout;
103     }
104 
105   ret = ioctl(fd, WDIOC_SETTIMEOUT, (unsigned long)CONFIG_MCUBOOT_WATCHDOG_TIMEOUT);
106   if (ret < 0)
107     {
108       int errcode = errno;
109 
110       BOOT_LOG_ERR("Failed to set timeout in watchdog device: %d", errcode);
111       goto errout_with_dev;
112     }
113 
114   ret = ioctl(fd, WDIOC_START, 0);
115   if (ret < 0)
116     {
117       int errcode = errno;
118 
119       BOOT_LOG_ERR("Failed to start watchdog device: %d", errcode);
120       goto errout_with_dev;
121     }
122 
123   close(fd);
124   return OK;
125 
126 errout_with_dev:
127   close(fd);
128 errout:
129   return ERROR;
130 }
131