rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hooks.c
Go to the documentation of this file.
1/************************************************************************************//**
2* \file Demo/ARMCM4_STM32F4_Nucleo_F429ZI_GCC/Boot/hooks.c
3* \brief Bootloader callback source file.
4* \ingroup Boot_ARMCM4_STM32F4_Nucleo_F429ZI_GCC
5* \internal
6*----------------------------------------------------------------------------------------
7* C O P Y R I G H T
8*----------------------------------------------------------------------------------------
9* Copyright (c) 2021 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 have received a copy of the GNU General Public License along with OpenBLT. It
24* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
25*
26* \endinternal
27****************************************************************************************/
28
29/****************************************************************************************
30* Include files
31****************************************************************************************/
32#include "boot.h" /* bootloader generic header */
33#include "led.h" /* LED driver header */
34#ifdef STM32F429xx
35#include "stm32f4xx.h" /* STM32 CPU and HAL header */
36#endif
37#ifdef STM32F767xx
38#include "stm32f7xx.h" /* STM32 CPU and HAL header */
39#endif
40#ifdef STM32H743xx
41#include "stm32h7xx.h" /* STM32 CPU and HAL header */
42#endif
43
44/****************************************************************************************
45* B A C K D O O R E N T R Y H O O K F U N C T I O N S
46****************************************************************************************/
47
48#if (BOOT_BACKDOOR_HOOKS_ENABLE > 0)
49/************************************************************************************//**
50** \brief Initializes the backdoor entry option.
51** \return none.
52**
53****************************************************************************************/
54__attribute__((weak)) void BackDoorInitHook(void)
55{
56} /*** end of BackDoorInitHook ***/
57
58
59/************************************************************************************//**
60** \brief Checks if a backdoor entry is requested.
61** \return BLT_TRUE if the backdoor entry is requested, BLT_FALSE otherwise.
62**
63****************************************************************************************/
64
66{
67 return BLT_TRUE;
68} /*** end of BackDoorEntryHook ***/
69#endif /* BOOT_BACKDOOR_HOOKS_ENABLE > 0 */
70
71
72/****************************************************************************************
73* C P U D R I V E R H O O K F U N C T I O N S
74****************************************************************************************/
75
76#if (BOOT_CPU_USER_PROGRAM_START_HOOK > 0)
77/************************************************************************************//**
78** \brief Callback that gets called when the bootloader is about to exit and
79** hand over control to the user program. This is the last moment that
80** some final checking can be performed and if necessary prevent the
81** bootloader from activiting the user program.
82** \return BLT_TRUE if it is okay to start the user program, BLT_FALSE to keep
83** keep the bootloader active.
84**
85****************************************************************************************/
87{
88 /* additional and optional backdoor entry through the pushbutton on the board. to
89 * force the bootloader to stay active after reset, keep the pushbutton pressed while
90 * resetting the microcontroller.
91 */
92 if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_SET)
93 {
94 /* pushbutton pressed, so do not start the user program and keep the
95 * bootloader active instead.
96 */
97 return BLT_FALSE;
98 }
99
100 /* clean up the LED driver */
101 LedBlinkExit();
102
103 /* okay to start the user program */
104 return BLT_TRUE;
105} /*** end of CpuUserProgramStartHook ***/
106#endif /* BOOT_CPU_USER_PROGRAM_START_HOOK > 0 */
107
108
109/****************************************************************************************
110* W A T C H D O G D R I V E R H O O K F U N C T I O N S
111****************************************************************************************/
112
113#if (BOOT_COP_HOOKS_ENABLE > 0)
114
115// CopInitHook() and CopServiceHook() are defined in bootloader_main.cpp */
116
117#endif /* BOOT_COP_HOOKS_ENABLE > 0 */
118
119
120/****************************************************************************************
121* U S B C O M M U N I C A T I O N I N T E R F A C E H O O K F U N C T I O N S
122****************************************************************************************/
123
124#if (BOOT_COM_USB_ENABLE > 0)
125/************************************************************************************//**
126** \brief Callback that gets called whenever the USB device should be connected
127** to the USB bus.
128** \param connect BLT_TRUE to connect and BLT_FALSE to disconnect.
129** \return none.
130**
131****************************************************************************************/
133{
134 /* Note that this is handled automatically by the OTG peripheral. */
135} /*** end of UsbConnect ***/
136
137
138/************************************************************************************//**
139** \brief Callback that gets called whenever the USB host requests the device
140** to enter a low power mode.
141** \return none.
142**
143****************************************************************************************/
145{
146 /* support to enter a low power mode can be implemented here */
147} /*** end of UsbEnterLowPowerMode ***/
148
149
150/************************************************************************************//**
151** \brief Callback that gets called whenever the USB host requests the device to
152** exit low power mode.
153** \return none.
154**
155****************************************************************************************/
157{
158 /* support to leave a low power mode can be implemented here */
159} /*** end of UsbLeaveLowPowerMode ***/
160#endif /* BOOT_COM_USB_ENABLE > 0 */
161
162
163/****************************************************************************************
164* N O N - V O L A T I L E M E M O R Y D R I V E R H O O K F U N C T I O N S
165****************************************************************************************/
166
167#if (BOOT_NVM_HOOKS_ENABLE > 0)
168/************************************************************************************//**
169** \brief Callback that gets called at the start of the internal NVM driver
170** initialization routine.
171** \return none.
172**
173****************************************************************************************/
174void NvmInitHook(void)
175{
176} /*** end of NvmInitHook ***/
177
178
179/************************************************************************************//**
180** \brief Callback that gets called at the start of a firmware update to reinitialize
181** the NVM driver.
182** \return none.
183**
184****************************************************************************************/
186{
187} /*** end of NvmReinitHook ***/
188
189
190/************************************************************************************//**
191** \brief Callback that gets called at the start of the NVM driver write
192** routine. It allows additional memory to be operated on. If the address
193** is not within the range of the additional memory, then
194** BLT_NVM_NOT_IN_RANGE must be returned to indicate that the data hasn't
195** been written yet.
196** \param addr Start address.
197** \param len Length in bytes.
198** \param data Pointer to the data buffer.
199** \return BLT_NVM_OKAY if successful, BLT_NVM_NOT_IN_RANGE if the address is
200** not within the supported memory range, or BLT_NVM_ERROR is the write
201** operation failed.
202**
203****************************************************************************************/
205{
206 return BLT_NVM_NOT_IN_RANGE;
207} /*** end of NvmWriteHook ***/
208
209
210/************************************************************************************//**
211** \brief Callback that gets called at the start of the NVM driver erase
212** routine. It allows additional memory to be operated on. If the address
213** is not within the range of the additional memory, then
214** BLT_NVM_NOT_IN_RANGE must be returned to indicate that the memory
215** hasn't been erased yet.
216** \param addr Start address.
217** \param len Length in bytes.
218** \return BLT_NVM_OKAY if successful, BLT_NVM_NOT_IN_RANGE if the address is
219** not within the supported memory range, or BLT_NVM_ERROR is the erase
220** operation failed.
221**
222****************************************************************************************/
224{
225 return BLT_NVM_NOT_IN_RANGE;
226} /*** end of NvmEraseHook ***/
227
228
229/************************************************************************************//**
230** \brief Callback that gets called at the end of the NVM programming session.
231** \return BLT_TRUE is successful, BLT_FALSE otherwise.
232**
233****************************************************************************************/
235{
236 return BLT_TRUE;
237} /*** end of NvmDoneHook ***/
238#endif /* BOOT_NVM_HOOKS_ENABLE > 0 */
239
240
241#if (BOOT_NVM_CHECKSUM_HOOKS_ENABLE > 0)
242/************************************************************************************//**
243** \brief Verifies the checksum, which indicates that a valid user program is
244** present and can be started.
245** \return BLT_TRUE if successful, BLT_FALSE otherwise.
246**
247****************************************************************************************/
249{
250 return BLT_TRUE;
251} /*** end of NvmVerifyChecksum ***/
252
253
254/************************************************************************************//**
255** \brief Writes a checksum of the user program to non-volatile memory. This is
256** performed once the entire user program has been programmed. Through
257** the checksum, the bootloader can check if a valid user programming is
258** present and can be started.
259** \return BLT_TRUE if successful, BLT_FALSE otherwise.
260**
261****************************************************************************************/
263{
264 return BLT_TRUE;
265}
266#endif /* BOOT_NVM_CHECKSUM_HOOKS_ENABLE > 0 */
267
268
269/****************************************************************************************
270* S E E D / K E Y S E C U R I T Y H O O K F U N C T I O N S
271****************************************************************************************/
272
273#if (BOOT_XCP_SEED_KEY_ENABLE > 0)
274/************************************************************************************//**
275** \brief Provides a seed to the XCP master that will be used for the key
276** generation when the master attempts to unlock the specified resource.
277** Called by the GET_SEED command.
278** \param resource Resource that the seed if requested for (XCP_RES_XXX).
279** \param seed Pointer to byte buffer wher the seed will be stored.
280** \return Length of the seed in bytes.
281**
282****************************************************************************************/
284{
285 /* request seed for unlocking ProGraMming resource */
286 if ((resource & XCP_RES_PGM) != 0)
287 {
288 seed[0] = 0x55;
289 }
290
291 /* return seed length */
292 return 1;
293} /*** end of XcpGetSeedHook ***/
294
295
296/************************************************************************************//**
297** \brief Called by the UNLOCK command and checks if the key to unlock the
298** specified resource was correct. If so, then the resource protection
299** will be removed.
300** \param resource resource to unlock (XCP_RES_XXX).
301** \param key pointer to the byte buffer holding the key.
302** \param len length of the key in bytes.
303** \return 1 if the key was correct, 0 otherwise.
304**
305****************************************************************************************/
307{
308 /* suppress compiler warning for unused parameter */
309 len = len;
310
311 /* the example key algorithm in "libseednkey.dll" works as follows:
312 * - PGM will be unlocked if key = seed - 1
313 */
314
315 /* check key for unlocking ProGraMming resource */
316 if ((resource == XCP_RES_PGM) && (key[0] == (0x55-1)))
317 {
318 /* correct key received for unlocking PGM resource */
319 return 1;
320 }
321
322 /* still here so key incorrect */
323 return 0;
324} /*** end of XcpVerifyKeyHook ***/
325#endif /* BOOT_XCP_SEED_KEY_ENABLE > 0 */
326
327
328/*********************************** end of hooks.c ************************************/
constexpr uint8_t addr
Definition ads1015.cpp:14
typedef __attribute__
Ignition Mode.
static bool connect(void *instance)
blt_int8u XcpVerifyKeyHook(blt_int8u resource, blt_int8u *key, blt_int8u len)
Called by the UNLOCK command and checks if the key to unlock the specified resource was correct....
Definition hooks.c:306
blt_bool NvmVerifyChecksumHook(void)
Verifies the checksum, which indicates that a valid user program is present and can be started.
Definition hooks.c:248
blt_int8u NvmWriteHook(blt_addr addr, blt_int32u len, blt_int8u *data)
Callback that gets called at the start of the NVM driver write routine. It allows additional memory t...
Definition hooks.c:204
void UsbConnectHook(blt_bool connect)
Callback that gets called whenever the USB device should be connected to the USB bus.
Definition hooks.c:132
blt_bool CpuUserProgramStartHook(void)
Callback that gets called when the bootloader is about to exit and hand over control to the user prog...
Definition hooks.c:86
blt_bool NvmDoneHook(void)
Callback that gets called at the end of the NVM programming session.
Definition hooks.c:234
blt_int8u XcpGetSeedHook(blt_int8u resource, blt_int8u *seed)
Provides a seed to the XCP master that will be used for the key generation when the master attempts t...
Definition hooks.c:283
void NvmInitHook(void)
Callback that gets called at the start of the internal NVM driver initialization routine.
Definition hooks.c:174
blt_bool NvmWriteChecksumHook(void)
Writes a checksum of the user program to non-volatile memory. This is performed once the entire user ...
Definition hooks.c:262
void UsbLeaveLowPowerModeHook(void)
Callback that gets called whenever the USB host requests the device to exit low power mode.
Definition hooks.c:156
void NvmReinitHook(void)
Callback that gets called at the start of a firmware update to reinitialize the NVM driver.
Definition hooks.c:185
blt_bool BackDoorEntryHook(void)
Checks if a backdoor entry is requested.
Definition hooks.c:65
void UsbEnterLowPowerModeHook(void)
Callback that gets called whenever the USB host requests the device to enter a low power mode.
Definition hooks.c:144
blt_int8u NvmEraseHook(blt_addr addr, blt_int32u len)
Callback that gets called at the start of the NVM driver erase routine. It allows additional memory t...
Definition hooks.c:223
void LedBlinkExit(void)
Cleans up the LED blink driver. This is intended to be used upon program exit.
Definition led.c:101
unsigned char blt_int8u
Definition types.h:49
unsigned char blt_bool
Definition types.h:46
unsigned int blt_int32u
Definition types.h:53
unsigned long blt_addr
Definition types.h:48