Statistics
| Branch: | Tag: | Revision:

amiro-blt / Target / Source / backdoor.c @ 6886c3b6

History | View | Annotate | Download (9.253 KB)

1 69661903 Thomas Schöpping
/************************************************************************************//**
2
* \file         Source\backdoor.c
3
* \brief        Bootloader backdoor entry source file.
4
* \ingroup      Core
5
* \internal
6
*----------------------------------------------------------------------------------------
7
*                          C O P Y R I G H T
8
*----------------------------------------------------------------------------------------
9
*   Copyright (c) 2011  by Feaser    http://www.feaser.com    All rights reserved
10
*
11
*----------------------------------------------------------------------------------------
12
*                            L I C E N S E
13
*----------------------------------------------------------------------------------------
14
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
15
* modify it under the terms of the GNU General Public License as published by the Free
16
* Software Foundation, either version 3 of the License, or (at your option) any later
17
* version.
18
*
19
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21
* PURPOSE. See the GNU General Public License for more details.
22
*
23
* You should have received a copy of the GNU General Public License along with OpenBLT.
24
* If not, see <http://www.gnu.org/licenses/>.
25
*
26
* A special exception to the GPL is included to allow you to distribute a combined work 
27
* that includes OpenBLT without being obliged to provide the source code for any 
28
* proprietary components. The exception text is included at the bottom of the license
29
* file <license.html>.
30
* 
31
* \endinternal
32
****************************************************************************************/
33
34
/****************************************************************************************
35
* Include files
36
****************************************************************************************/
37
#include "boot.h"                                /* bootloader generic header          */
38
39
40
/****************************************************************************************
41
* Macro definitions
42
****************************************************************************************/
43
#if (BOOT_BACKDOOR_HOOKS_ENABLE == 0)
44
  #ifndef BACKDOOR_ENTRY_TIMEOUT_MS
45
  /** \brief Sets the time in milliseconds that the backdoor is open, but allow an 
46
   *         override for this time. note that this time should be at least 2.5 times
47
   *         as long as the time that is configured in Microboot's XCP settings for the
48
   *         connect command response. This is the last entry on XCP Timeouts tab. By 
49
   *         default the connect command response is configured as 20ms by Microboot,
50
   *         except for TCP/IP where it is 300ms due to accomodate for worldwide
51
   *         network latency. For CAN this was also adjusted to 500ms so that Microboot
52
   *         can wait for the bootloader to initialize. Otherwise errorframes can be
53
   *         generated on the CAN bus.
54
   */
55
    #if (BOOT_COM_NET_ENABLE == 1)
56
      #define BACKDOOR_ENTRY_TIMEOUT_MS  (750)
57
    #elif (BOOT_COM_CAN_ENABLE == 1)
58
      #define BACKDOOR_ENTRY_TIMEOUT_MS  (500)
59
    #else
60
      #define BACKDOOR_ENTRY_TIMEOUT_MS  (600)
61
    #endif
62
  #endif
63
#endif
64
65
/* Sets the time (ms) while the bootloader is in flashing mode but not connected */
66
#ifndef FLASHING_WITHOUT_CONNECTION_TIMEOUT_MS
67
#define FLASHING_WITHOUT_CONNECTION_TIMEOUT_MS 30000 // 30 seconds
68
#endif
69
/****************************************************************************************
70
* Hook functions
71
****************************************************************************************/
72
#if (BOOT_BACKDOOR_HOOKS_ENABLE > 0)
73
extern void BackDoorInitHook(void);
74
extern blt_bool BackDoorEntryHook(void);
75
extern blt_bool BackDoorEntryCheck(void);
76
extern void BackDoorComIsConnected(void);
77
#endif
78
79
80
/****************************************************************************************
81
* Local data declarations
82
****************************************************************************************/
83
#if (BOOT_BACKDOOR_HOOKS_ENABLE == 0)
84
/** \brief To determine if the backdoor is open or closed. */
85
static blt_bool backdoorOpen;
86
/** \brief To determine how long the backdoor has been open in milliseconds. */
87
static blt_int32u backdoorOpenTime;
88
#endif /* (BOOT_BACKDOOR_HOOKS_ENABLE == 0) */
89
#if (BOOTLOADER_OF_MAIN_DEVICE == 1)
90
static blt_bool wasConnection;
91
static blt_int32u inFlashingModeWithoutConnection;
92
#endif /* (BOOTLOADER_OF_MAIN_DEVICE == 1) */
93
94
95
/************************************************************************************//**
96
** \brief     Initializes the backdoor entry option.
97
** \return    none
98
**
99
****************************************************************************************/
100
void BackDoorInit(void)
101
{
102
#if (BOOT_BACKDOOR_HOOKS_ENABLE > 0)
103
  /* initialize application's backdoor functionality */
104
  BackDoorInitHook();
105
  
106
  /* attempt to start the user program when no backdoor entry is requested */
107
  if (BackDoorEntryHook() == BLT_FALSE)
108
  {
109
    /* this function does not return if a valid user program is present */
110
    CpuStartUserProgram();
111
  }
112
  #if (BOOT_FILE_SYS_ENABLE > 0)
113
  else
114
  {
115
    /* the backdoor is open so we should check if a update from locally  attached storage 
116
     * is requested and, if so, start it.
117
     */
118
    FileHandleFirmwareUpdateRequest();
119
  }
120
  #endif
121
#else
122
  /* open the backdoor after a reset */
123
  backdoorOpen = BLT_TRUE;
124
  backdoorOpenTime = TimerGet();
125
#endif
126
127
#if (BOOTLOADER_OF_MAIN_DEVICE == 1)
128
  wasConnection = BLT_FALSE;
129
  inFlashingModeWithoutConnection = TimerGet();
130
#endif /* (BOOTLOADER_OF_MAIN_DEVICE == 1) */
131
132
  /* perform the first check that open/closes the backdoor */
133
  BackDoorCheck();
134
} /*** end of BackDoorInit ***/
135
136
137
/************************************************************************************//**
138
** \brief     The default backdoor entry feature keeps the bootloader active for a
139
**            predetermined time after reset, allowing the host application to
140
**            establish a connection and start a programming sequence. This function
141
**            controls the opening/closing of the backdoor.
142
** \return    none
143
**
144
****************************************************************************************/
145
void BackDoorCheck(void)
146
{
147
#if (BOOT_COM_ENABLE > 0 || BOOT_GATE_ENABLE > 0)
148
  /* check if a connection with the host was already established. in this case the
149
   * backdoor stays open anyway, so no need to check if it needs to be closed. 
150
   */
151
  if (ComIsConnected() == BLT_TRUE)
152
  {
153
  #if (BOOT_BACKDOOR_HOOKS_ENABLE > 0)
154
    BackDoorComIsConnected();
155
  #endif
156
  #if (BOOTLOADER_OF_MAIN_DEVICE == 1)
157
    /* set timer for flashing mode timeout */
158
    inFlashingModeWithoutConnection = TimerGet();
159
    /* save that there has been a connection */
160
    if (ComWasConnectedToMain() == BLT_TRUE) {
161
      wasConnection = BLT_TRUE;
162
    }
163
  #endif
164
    return;
165
  }
166
  #if (BOOTLOADER_OF_MAIN_DEVICE == 1)
167
  /* check if a connection with the host has been closed. In this case the
168
   * backdoor stays open (bootloader stays in "flashing mode") for the defined time. 
169
   */
170
  else if (wasConnection == BLT_TRUE)
171
  {
172
    /* check flashing mode timeout */
173
    if (TimerGet() >= (FLASHING_WITHOUT_CONNECTION_TIMEOUT_MS + inFlashingModeWithoutConnection))
174
    {
175
      /* user program can be started because there hasn't been announced any connection in
176
       * the defined flashing mode time.
177
       */
178
      CpuStartUserProgram();
179
    }
180
    return;
181
  }
182
  #endif
183
#endif /* BOOT_COM_ENABLE > 0 || BOOT_GATE_ENABLE > 0 */
184
185
#if (BOOT_BACKDOOR_HOOKS_ENABLE == 0)
186
  #if (BOOT_FILE_SYS_ENABLE > 0)
187
  /* check if the file module is busy, indicating that a firmware update through the
188
   * locally attached storage is in progress. in this case the backdoor stays open 
189
   * anyway, so no need to check if it needs to be closed. 
190
   */
191
  if (FileIsIdle() == BLT_FALSE)
192
  {
193
    return;
194
  }
195
  #endif  
196
  
197
  /* when the backdoor is still open, check if it's time to close it */
198
  if (backdoorOpen == BLT_TRUE)
199
  {
200
    /* check if the backdoor entry time window elapsed */
201
    if (TimerGet() >= (BACKDOOR_ENTRY_TIMEOUT_MS + backdoorOpenTime))
202
    {
203
      /* close the backdoor */
204
      backdoorOpen = BLT_FALSE;
205
      #if (BOOT_FILE_SYS_ENABLE > 0)
206
      /* during the timed backdoor no remote update request was detected. now do one
207
       * last check to see if a firmware update from locally attached storage is
208
       * pending.
209
       */
210
      if (FileHandleFirmwareUpdateRequest() == BLT_FALSE)
211
      #endif
212
      {
213
        /* no firmware update requests detected, so attempt to start the user program.
214
         * this function does not return if a valid user program is present.
215
         */
216
        CpuStartUserProgram();
217
      }
218
    }
219
  }
220
#else
221
  /* check if the bootloader has to stay in the backdoor loop */
222
  if (BackDoorEntryCheck() == BLT_TRUE) {
223
    return;
224
  }
225
226
  /* user program can be started because there is nothing else to check */
227
  CpuStartUserProgram();
228
#endif
229
} /*** end of BackDoorCheck ***/
230
231
232
/*********************************** end of backdoor.c *********************************/