PX4 Firmware
PX4 Autopilot Software http://px4.io
boot_app_shared.h
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 #pragma once
38 
39 /****************************************************************************
40  * Included Files
41  ****************************************************************************/
42 
43 #include <nuttx/compiler.h>
44 
46 
47 /****************************************************************************
48  * Pre-processor Definitions
49  ****************************************************************************/
50 
51 /* Define the signature for the Application descriptor as 'APDesc' and a
52  * revision number of 00 used in app_descriptor_t
53  */
54 
55 #define APP_DESCRIPTOR_SIGNATURE_ID 'A','P','D','e','s','c'
56 #define APP_DESCRIPTOR_SIGNATURE_REV '0','0'
57 #define APP_DESCRIPTOR_SIGNATURE APP_DESCRIPTOR_SIGNATURE_ID, APP_DESCRIPTOR_SIGNATURE_REV
58 
59 /* N.B. the .ld file must emit this sections */
60 # define boot_app_shared_section __attribute__((section(".app_descriptor")))
61 
62 /****************************************************************************
63  * Public Type Definitions
64  ****************************************************************************/
65 
66 /* eRole defines the role of the bootloader_app_shared_t structure */
67 
68 typedef enum eRole {
70  App,
72 } eRole_t;
73 
74 /****************************************************************************
75  *
76  * Bootloader and Application shared structure.
77  *
78  * The data in this structure is passed in SRAM or the the CAN filter
79  * registers from bootloader to application and application to bootloader.
80  *
81  * Do not assume any mapping or location for the passing of this data
82  * that is done in the read and write routines and is abstracted by design.
83  *
84  * For reference, the following is performed based on eRole in API calls
85  * defined below:
86  *
87  * The application must write BOOTLOADER_COMMON_APP_SIGNATURE to the
88  * signature field when passing data to the bootloader; when the
89  * bootloader passes data to the application, it must write
90  * BOOTLOADER_COMMON_BOOTLOADER_SIGNATURE to the signature field.
91  *
92  * The CRC is calculated over the structure from signature to the
93  * last byte. The resulting values are then copied to the CAN filter
94  * registers by bootloader_app_shared_read and
95  * bootloader_app_shared_write.
96  *
97 ****************************************************************************/
98 
99 #pragma GCC diagnostic push
100 #pragma GCC diagnostic ignored "-Wattributes"
101 typedef begin_packed_struct struct bootloader_app_shared_t {
102  union {
103  uint64_t ull;
104  uint32_t ul[2];
105  uint8_t valid;
106  } crc;
107  uint32_t signature;
108  uint32_t bus_speed;
109  uint32_t node_id;
110 } end_packed_struct bootloader_app_shared_t;
111 #pragma GCC diagnostic pop
112 
113 /****************************************************************************
114  *
115  * Application firmware descriptor.
116  *
117  * This structure located by the linker script somewhere after the vector table.
118  * (within the first several kilobytes of the beginning address of the
119  * application);
120  *
121  * This structure must be aligned on an 8-byte boundary.
122  *
123  * The bootloader will scan through the application FLASH image until it
124  * finds the signature.
125  *
126  * The image_crc is calculated as follows:
127  * 1) All fields of this structure must be initialized with the correct
128  * information about the firmware image bin file
129  * (Here after refereed to as image)
130  * 2) image_crc set to 0;
131  * 3) The CRC 64 is calculated over the image from offset 0 up to and including the
132  * last byte of the image file.
133  * 4) The calculated CRC 64 is stored in image_crc
134  * 5) The new image file is then written to a file a ".img" extension.
135  *
136 ****************************************************************************/
137 
138 #pragma GCC diagnostic push
139 #pragma GCC diagnostic ignored "-Wattributes"
140 #pragma GCC diagnostic ignored "-Wpacked"
141 typedef begin_packed_struct struct app_descriptor_t {
142  uint8_t signature[sizeof(uint64_t)];
143  uint64_t image_crc;
144  uint32_t image_size;
145  uint32_t vcs_commit;
146  uint8_t major_version;
147  uint8_t minor_version;
148  uint8_t reserved[6];
149 } end_packed_struct app_descriptor_t;
150 #pragma GCC diagnostic pop
151 
152 /****************************************************************************
153  * Global Variables
154  ****************************************************************************/
155 
156 /****************************************************************************
157  * Public Function Prototypes
158  ****************************************************************************/
159 /****************************************************************************
160  * Name: bootloader_app_shared_read
161  *
162  * Description:
163  * Based on the role requested, this function will conditionally populate
164  * a bootloader_app_shared_t structure from the physical locations used
165  * to transfer the shared data to/from an application (internal data) .
166  *
167  * The functions will only populate the structure and return a status
168  * indicating success, if the internal data has the correct signature as
169  * requested by the Role AND has a valid crc.
170  *
171  * Input Parameters:
172  * shared - A pointer to a bootloader_app_shared_t return the data in if
173  * the internal data is valid for the requested Role
174  * role - An eRole_t of App or BootLoader to validate the internal data
175  * against. For a Bootloader this would be the value of App to
176  * read the application passed data.
177  *
178  * Returned value:
179  * OK - Indicates that the internal data has been copied to callers
180  * bootloader_app_shared_t structure.
181  *
182  * -EBADR - The Role or crc of the internal data was not valid. The copy
183  * did not occur.
184  *
185  ****************************************************************************/
186 
188  eRole_t role);
189 
190 /****************************************************************************
191  * Name: bootloader_app_shared_write
192  *
193  * Description:
194  * Based on the role, this function will commit the data passed
195  * into the physical locations used to transfer the shared data to/from
196  * an application (internal data) .
197  *
198  * The functions will populate the signature and crc the data
199  * based on the provided Role.
200  *
201  * Input Parameters:
202  * shared - A pointer to a bootloader_app_shared_t data to commit to
203  * the internal data for passing to/from an application.
204  * role - An eRole_t of App or BootLoader to use in the internal data
205  * to be passed to/from an application. For a Bootloader this
206  * would be the value of Bootloader to write to the passed data.
207  * to the application via the internal data.
208  *
209  * Returned value:
210  * None.
211  *
212  ****************************************************************************/
213 
215  eRole_t role);
216 
217 /****************************************************************************
218  * Name: bootloader_app_shared_invalidate
219  *
220  * Description:
221  * Invalidates the data passed the physical locations used to transfer
222  * the shared data to/from an application (internal data) .
223  *
224  * The functions will invalidate the signature and crc and should be used
225  * to prevent deja vu.
226  *
227  * Input Parameters:
228  * None.
229  *
230  * Returned value:
231  * None.
232  *
233  ****************************************************************************/
234 
236 
#define __END_DECLS
Definition: visibility.h:59
begin_packed_struct struct bootloader_app_shared_t bootloader_app_shared_t
int bootloader_app_shared_read(bootloader_app_shared_t *shared, eRole_t role)
union bootloader_app_shared_t::@2 crc
void bootloader_app_shared_write(bootloader_app_shared_t *shared, eRole_t role)
#define __BEGIN_DECLS
Definition: visibility.h:58
enum eRole eRole_t
eRole
begin_packed_struct struct app_descriptor_t app_descriptor_t
void bootloader_app_shared_invalidate(void)