-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootloader_client.h
More file actions
212 lines (172 loc) · 5.3 KB
/
bootloader_client.h
File metadata and controls
212 lines (172 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* basic library for uploading firmware via a serial port to an STM32
* running Bootloader software
* (https://github.com/arudpilot/Bootloader)
*/
#ifndef _BOOTLOADER_CLIENT_H
#define _BOOTLOADER_CLIENT_H
#include <stdint.h>
#include <unistd.h>
#include <stdarg.h>
#include <stddef.h>
typedef uint32_t bl_timeout_t;
/*
* higher-level functions
*/
/*
* attempt to reboot the STM32 by sending a mavlink reboot
*/
int bootloader_send_mavlink_reboot();
/*
* nops are ignored by the bootloader
*/
int bootloader_send_nop();
/*
* attempt to synchronise with bootloader
*/
int bootloader_get_sync();
/*
* exit bootloader and attempt to run app
*/
int bootloader_boot();
/*
* return bootloader revision
*/
int bootloader_get_bl_rev(uint32_t *ret);
/*
* return board id
*/
int bootloader_get_board_id(uint32_t *ret);
/*
* return board revision
*/
int bootloader_get_board_rev(uint32_t *ret);
/*
* return size of flash firm area
*/
int bootloader_get_fw_size(uint32_t *ret);
/*
* return boot vectors
* ret must be 4*uint32 long
*
*/
int bootloader_get_vec_area(uint32_t *ret);
/*
* return STM Chip ID
*/
int bootloader_get_chip_id(uint32_t *ret);
/*
* retrieve serial number from processor
* ret must be d*uint32_t bytes in size
*/
int bootloader_get_sn(uint32_t *ret);
/*
* retrieve One-Time-Programmable bytes from processor;
* ret must be 512 bytes in size
*/
int bootloader_get_otp(uint8_t *ret);
/*
* get textual description of chip
*/
int bootloader_get_chip_des(uint8_t *ret, const uint32_t retlen);
/*
* retrieve crc of programmed flash area
*/
int bootloader_get_crc(uint32_t *ret);
/*
* send erase command to bootloader
*
* Caller must wait for sync using bootloader_recv_sync(); it may take
* 10+ seconds to complete an erase
*/
int bootloader_erase_send();
/*
* bootloader waits d seconds before launching app
*/
int bootloader_set_boot_delay(const uint8_t d);
/*
* write fw of length fwlen to flash storage
*
* f is called periodically, its argument being percentage-complete
* chip must be erased before calling this funtion
*/
int bootloader_program(uint8_t *fw, const uint32_t fwlen, void (*f)(uint8_t));
/*
* write another chunk of firmware to flash storage
*
* chunk is the data to be written
*
* chunksize is the exact number of bytes to be written. It must be
* at most bl_prog_multi_chunksize bytes in length and must be evenly
* divisible by 4 except the final chunk to be written
*/
int bootloader_program_chunk(const uint8_t *chunk, const uint8_t chunksize);
/*
* calculate a CRC for the supplied firmware
* this should match what the PixHawk returns after flashing is complete
*/
uint32_t bootloader_crc32_firmware(const uint8_t *fw, uint32_t fwlen, const uint32_t padlen);
/*
* update a crc32 calculation based on a chunk of data
*
*/
uint32_t bootloader_crc32_chunk(const uint8_t *chunk, const uint32_t chunklen, uint32_t state);
/*
* set a baudrate on the bootloader side of the serial connection
*/
int bootloader_set_baudrate(uint32_t baud);
/*
* lower-level functions
*/
/*
* these two functions must be implemented by the user of this library:
*/
ssize_t bootloader_write(const uint8_t *bytes, const size_t bytecount, const bl_timeout_t timeout);
ssize_t bootloader_readbyte(uint8_t *byte, const bl_timeout_t timeout);
#ifdef __STDC__
void bootloader_debug(const char *format, ...);
#else
void bootloader_debug(const char *format, arg...);
#endif
enum bl_reply {
BL_REPLY_OK = '\x10',
BL_REPLY_FAILED = '\x11',
BL_REPLY_INVALID = '\x13',
};
typedef uint8_t bl_reply_t;
enum {
BL_INSYNC = '\x12',
BL_EOC = '\x20',
BL_GET_SYNC = '\x21',
BL_GET_DEVICE = '\x22',
BL_CHIP_ERASE = '\x23',
BL_PROG_MULTI = '\x27',
BL_GET_CRC = '\x29',
BL_GET_OTP = '\x2a',
BL_GET_SN = '\x2b',
BL_GET_CHIP = '\x2c',
BL_SET_DELAY = '\x2d',
BL_GET_CHIP_DES = '\x2e',
BL_BOOT = '\x30',
BL_SET_BAUD = '\x33',
} bl_command;
typedef uint8_t bl_command_t;
typedef enum bl_info_req {
BL_INFO_REV = '\x01',
BL_INFO_BOARD_ID = '\x02',
BL_INFO_BOARD_REV = '\x03',
BL_INFO_FW_SIZE = '\x04',
BL_INFO_VEC_AREA = '\x05',
} bl_info_req_t;
int bootloader_recv_sync(bl_reply_t *status, const bl_timeout_t timeout);
int bootloader_cmd_blword(bl_command_t cmd, const uint8_t *params, const uint8_t paramlen, uint32_t *ret, const bl_timeout_t timeout);
int bootloader_read_blword(uint32_t *ret, const bl_timeout_t timeout);
int bootloader_get_device_info_bytes(const bl_info_req_t what, uint8_t *ret, const uint8_t count, const bl_timeout_t timeout);
int bootloader_get_device_info_blword(const bl_info_req_t what, uint32_t *ret, const uint32_t timeout);
int _bootloader_get_sn_chunk(uint8_t chunk, uint32_t *ret, const bl_timeout_t timeout);
int _bootloader_get_otp_chunk(uint8_t chunk, uint32_t *ret, const bl_timeout_t timeout);
int bootloader_cmd_simple(const bl_command_t cmd, const bl_timeout_t timeout);
int bootloader_send_command(const bl_command_t command, const uint8_t *params, const uint8_t paramlen, const bl_timeout_t timeout);;
int _bootloader_recv_sync_ok(const bl_timeout_t timeout);
extern const uint8_t bl_prog_multi_chunksize;
#endif