2 * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 use common::buffer::Buffer;
25 use common::crc32::Crc32;
28 const COMMAND_ERASE : u32 = 0x49e89a20;
29 const COMMAND_PROGRAM : u32 = 0x37f7dc8d;
30 const COMMAND_VERIFY : u32 = 0x4a213efb;
31 const COMMAND_LOADCHUNK : u32 = 0x1b329768;
32 const COMMAND_START_APP : u32 = 0xd27df1bf;
34 pub struct Bootloader {
37 sector_data: [u8; flash::SECTOR_SIZE],
39 // The number of bytes in sector_data.
40 // This is always 32 bit aligned and never greater than flash::SECTOR_SIZE.
41 sector_data_length: usize,
53 fn usb_serial_read(c: *mut u8) -> bool;
55 static mut cdc_tx_buf: Buffer;
58 fn try_read_u8() -> Option<u8> {
62 if usb_serial_read(&mut c) {
74 while !usb_serial_read(&mut b) {
81 fn read_u32_le() -> u32 {
93 cdc_tx_buf.write(&[b]);
98 fn write_u32_le(u: u32) {
99 let b = u.to_le_bytes();
102 cdc_tx_buf.write(&b);
111 fn write_nak(e: Error) {
116 pub fn new() -> Bootloader {
120 sector_data: [0u8; flash::SECTOR_SIZE],
121 sector_data_length: 0,
125 pub fn run(&mut self) -> bool {
126 if self.command_bytes < 4 {
127 if let Some(b) = try_read_u8() {
129 self.command |= (b as u32) << 24;
131 self.command_bytes += 1;
135 if self.command_bytes != 4 {
138 self.command_bytes = 0;
140 self.process_command();
142 self.command != COMMAND_START_APP
146 fn process_command(&mut self) {
149 if let Err(e) = self.exec_erase() {
155 COMMAND_LOADCHUNK => {
156 if let Err(e) = self.exec_load_sector_data() {
163 let result = self.exec_verify();
165 if let Err(e) = result {
167 } else if let Ok((a, b)) = result {
175 if let Err(e) = self.exec_program() {
181 COMMAND_START_APP => {
185 write_nak(Error::UnknownCommand);
190 fn exec_erase(&mut self) -> Result<(), Error> {
191 let sector = read_u32_le();
194 Err(Error::InvalidArgument)
195 } else if sector == 0x01 {
196 // The second sector contains the flash configuration field,
197 // and keeping it in the erased state has the potential
198 // to brick the device.
199 Err(Error::PermissionDenied)
201 flash::erase(sector);
207 fn exec_load_sector_data(&mut self) -> Result<(), Error> {
208 let num_bytes = read_u32_le() as usize;
211 Err(Error::InvalidArgument)
212 } else if num_bytes > flash::SECTOR_SIZE {
213 Err(Error::InvalidArgument)
214 } else if (num_bytes & 3) != 0 {
215 Err(Error::InvalidArgument)
219 self.sector_data_length = num_bytes;
221 for i in 0..num_bytes {
222 self.sector_data[i] = read_u8();
225 let expected_crc32 = read_u32_le();
227 let chunk = &self.sector_data[0..self.sector_data_length];
229 let mut actual_crc32 = Crc32::new();
230 actual_crc32.update(chunk);
232 if actual_crc32.finish() != expected_crc32 {
233 Err(Error::ChecksumMismatch)
240 fn exec_verify(&mut self) -> Result<(u32, u32), Error> {
241 let sector = read_u32_le();
244 Err(Error::InvalidArgument)
245 } else if self.sector_data_length == 0 {
246 Err(Error::InvalidArgument)
248 let chunk = &self.sector_data[0..self.sector_data_length];
250 Ok(flash::verify(sector, chunk))
254 fn exec_program(&mut self) -> Result<(), Error> {
255 let sector = read_u32_le();
258 Err(Error::InvalidArgument)
259 } else if self.sector_data_length == 0 {
260 Err(Error::InvalidArgument)
262 let chunk = &self.sector_data[0..self.sector_data_length];
264 Ok(flash::program(sector, chunk))