PX4 Firmware
PX4 Autopilot Software http://px4.io
boot_app_shared.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015 PX4 Development Team. All rights reserved.
4  * Author: Ben Dyer <ben_dyer@mac.com>
5  * Pavel Kirienko <pavel.kirienko@zubax.com>
6  * David Sidrane <david_s5@nscdg.com>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  * 3. Neither the name PX4 nor the names of its contributors may be
19  * used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  ****************************************************************************/
36 
37 /****************************************************************************
38  * Included Files
39  ****************************************************************************/
40 
41 #include <nuttx/config.h>
42 
43 #include <stdint.h>
44 #include <string.h>
45 
46 #include "chip.h"
47 #include "stm32.h"
48 
49 #include <errno.h>
50 #include "boot_app_shared.h"
51 #include "systemlib/crc.h"
52 
53 /****************************************************************************
54  * Pre-processor Definitions
55  ****************************************************************************/
56 
57 #define BOOTLOADER_COMMON_APP_SIGNATURE 0xB0A04150u
58 #define BOOTLOADER_COMMON_BOOTLOADER_SIGNATURE 0xB0A0424Cu
59 
60 
61 /* CAN_FiRx where (i=0..27|13, x=1, 2)
62  * STM32_CAN1_FIR(i,x)
63  * Using i = 2 does not requier there block
64  * to be enabled nor FINIT in CAN_FMR to be set.
65  * todo:Validate this claim on F2, F3
66  */
67 
68 #define crc_HiLOC STM32_CAN1_FIR(2,1)
69 #define crc_LoLOC STM32_CAN1_FIR(2,2)
70 #define signature_LOC STM32_CAN1_FIR(3,1)
71 #define bus_speed_LOC STM32_CAN1_FIR(3,2)
72 #define node_id_LOC STM32_CAN1_FIR(4,1)
73 #define CRC_H 1
74 #define CRC_L 0
75 
76 /****************************************************************************
77  * Private Types
78  ****************************************************************************/
79 
80 /****************************************************************************
81  * Private Function Prototypes
82  ****************************************************************************/
83 
84 /****************************************************************************
85  * Private Data
86  ****************************************************************************/
87 
88 /****************************************************************************
89  * Public Data
90  ****************************************************************************/
91 
92 /****************************************************************************
93  * Private Functions
94  ****************************************************************************/
95 
96 /****************************************************************************
97  * Name: read
98  ****************************************************************************/
99 
100 inline static void read(bootloader_app_shared_t *pshared)
101 {
102  pshared->signature = getreg32(signature_LOC);
103  pshared->bus_speed = getreg32(bus_speed_LOC);
104  pshared->node_id = getreg32(node_id_LOC);
105  pshared->crc.ul[CRC_L] = getreg32(crc_LoLOC);
106  pshared->crc.ul[CRC_H] = getreg32(crc_HiLOC);
107 
108 }
109 
110 /****************************************************************************
111  * Name: write
112  ****************************************************************************/
113 
114 inline static void write(bootloader_app_shared_t *pshared)
115 {
116  putreg32(pshared->signature, signature_LOC);
117  putreg32(pshared->bus_speed, bus_speed_LOC);
118  putreg32(pshared->node_id, node_id_LOC);
119  putreg32(pshared->crc.ul[CRC_L], crc_LoLOC);
120  putreg32(pshared->crc.ul[CRC_H], crc_HiLOC);
121 
122 }
123 
124 /****************************************************************************
125  * Name: calulate_signature
126  ****************************************************************************/
127 
129 {
130  uint64_t crc;
131  crc = crc64_add_word(CRC64_INITIAL, pshared->signature);
132  crc = crc64_add_word(crc, pshared->bus_speed);
133  crc = crc64_add_word(crc, pshared->node_id);
134  crc ^= CRC64_OUTPUT_XOR;
135  return crc;
136 }
137 
138 /****************************************************************************
139  * Name: bootloader_app_shared_init
140  ****************************************************************************/
142 {
143  memset(pshared, 0, sizeof(bootloader_app_shared_t));
144 
145  if (role != Invalid) {
146  pshared->signature =
147  (role ==
150  }
151 
152 }
153 
154 /****************************************************************************
155  * Public Functions
156  ****************************************************************************/
157 /****************************************************************************
158  * Name: bootloader_app_shared_read
159  *
160  * Description:
161  * Based on the role requested, this function will conditionally populate
162  * a bootloader_app_shared_t structure from the physical locations used
163  * to transfer the shared data to/from an application (internal data) .
164  *
165  * The functions will only populate the structure and return a status
166  * indicating success, if the internal data has the correct signature as
167  * requested by the Role AND has a valid crc.
168  *
169  * Input Parameters:
170  * shared - A pointer to a bootloader_app_shared_t return the data in if
171  * the internal data is valid for the requested Role
172  * role - An eRole_t of App or BootLoader to validate the internal data
173  * against. For a Bootloader this would be the value of App to
174  * read the application passed data.
175  *
176  * Returned value:
177  * OK - Indicates that the internal data has been copied to callers
178  * bootloader_app_shared_t structure.
179  *
180  * -EBADR - The Role or crc of the internal data was not valid. The copy
181  * did not occur.
182  *
183  ****************************************************************************/
184 
185 __EXPORT
187  eRole_t role)
188 {
189  int rv = -EBADR;
190  bootloader_app_shared_t working;
191 
192  read(&working);
193 
194  if ((role == App ? working.signature == BOOTLOADER_COMMON_APP_SIGNATURE
196  && (working.crc.ull == calulate_signature(&working))) {
197  *shared = working;
198  rv = OK;
199  }
200 
201  return rv;
202 }
203 
204 /****************************************************************************
205  * Name: bootloader_app_shared_write
206  *
207  * Description:
208  * Based on the role, this function will commit the data passed
209  * into the physical locations used to transfer the shared data to/from
210  * an application (internal data) .
211  *
212  * The functions will populate the signature and crc the data
213  * based on the provided Role.
214  *
215  * Input Parameters:
216  * shared - A pointer to a bootloader_app_shared_t data to commit to
217  * the internal data for passing to/from an application.
218  * role - An eRole_t of App or BootLoader to use in the internal data
219  * to be passed to/from an application. For a Bootloader this
220  * would be the value of Bootloader to write to the passed data.
221  * to the application via the internal data.
222  *
223  * Returned value:
224  * None.
225  *
226  ****************************************************************************/
227 __EXPORT
229  eRole_t role)
230 {
231  bootloader_app_shared_t working = *shared;
232  working.signature =
233  (role ==
236  working.crc.ull = calulate_signature(&working);
237  write(&working);
238 
239 }
240 
241 /****************************************************************************
242  * Name: bootloader_app_shared_invalidate
243  *
244  * Description:
245  * Invalidates the data passed the physical locations used to transfer
246  * the shared data to/from an application (internal data) .
247  *
248  * The functions will invalidate the signature and crc and shoulf be used
249  * to prevent deja vu.
250  *
251  * Input Parameters:
252  * None.
253  *
254  * Returned value:
255  * None.
256  *
257  ****************************************************************************/
258 
259 __EXPORT
261 {
262  bootloader_app_shared_t working;
264  write(&working);
265 }
#define CRC64_INITIAL
Definition: crc.h:47
#define BOOTLOADER_COMMON_APP_SIGNATURE
__EXPORT void bootloader_app_shared_invalidate(void)
static void bootloader_app_shared_init(bootloader_app_shared_t *pshared, eRole_t role)
__EXPORT uint64_t crc64_add_word(uint64_t crc, uint32_t value)
Definition: crc.c:152
Definition: I2C.hpp:51
union bootloader_app_shared_t::@2 crc
#define CRC_L
__EXPORT int bootloader_app_shared_read(bootloader_app_shared_t *shared, eRole_t role)
static void read(bootloader_app_shared_t *pshared)
#define BOOTLOADER_COMMON_BOOTLOADER_SIGNATURE
#define CRC_H
__EXPORT void bootloader_app_shared_write(bootloader_app_shared_t *shared, eRole_t role)
#define crc_HiLOC
enum eRole eRole_t
#define CRC64_OUTPUT_XOR
Definition: crc.h:48
static void write(bootloader_app_shared_t *pshared)
#define signature_LOC
#define node_id_LOC
#define crc_LoLOC
#define OK
Definition: uavcan_main.cpp:71
static uint64_t calulate_signature(bootloader_app_shared_t *pshared)
#define bus_speed_LOC