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,
52 fn usb_serial_read(c: *mut u8) -> bool;
54 static mut cdc_tx_buf: Buffer;
57 fn try_read_u8() -> Option<u8> {
61 if usb_serial_read(&mut c) {
73 while !usb_serial_read(&mut b) {
80 fn read_u32_le() -> u32 {
92 cdc_tx_buf.write(&[b]);
97 fn write_u32_le(u: u32) {
98 let b = u.to_le_bytes();
101 cdc_tx_buf.write(&b);
110 fn write_nak(e: Error) {
115 pub fn new() -> Bootloader {
119 sector_data: [0u8; flash::SECTOR_SIZE],
120 sector_data_length: 0,
124 pub fn run(&mut self) -> bool {
125 if self.command_bytes < 4 {
126 if let Some(b) = try_read_u8() {
128 self.command |= (b as u32) << 24;
130 self.command_bytes += 1;
134 if self.command_bytes != 4 {
137 self.command_bytes = 0;
139 self.process_command();
141 self.command != COMMAND_START_APP
145 fn process_command(&mut self) {
148 if let Err(e) = self.exec_erase() {
154 COMMAND_LOADCHUNK => {
155 if let Err(e) = self.exec_load_sector_data() {
162 let result = self.exec_verify();
164 if let Err(e) = result {
166 } else if let Ok((a, b)) = result {
174 if let Err(e) = self.exec_program() {
180 COMMAND_START_APP => {
184 write_nak(Error::UnknownCommand);
189 fn exec_erase(&mut self) -> Result<(), Error> {
190 let sector = read_u32_le();
193 Err(Error::InvalidArgument)
195 flash::erase(sector);
201 fn exec_load_sector_data(&mut self) -> Result<(), Error> {
202 let num_bytes = read_u32_le() as usize;
205 Err(Error::InvalidArgument)
206 } else if num_bytes > flash::SECTOR_SIZE {
207 Err(Error::InvalidArgument)
208 } else if (num_bytes & 3) != 0 {
209 Err(Error::InvalidArgument)
213 self.sector_data_length = num_bytes;
215 for i in 0..num_bytes {
216 self.sector_data[i] = read_u8();
219 let expected_crc32 = read_u32_le();
221 let chunk = &self.sector_data[0..self.sector_data_length];
223 let mut actual_crc32 = Crc32::new();
224 actual_crc32.update(chunk);
226 if actual_crc32.finish() != expected_crc32 {
227 Err(Error::ChecksumMismatch)
234 fn exec_verify(&mut self) -> Result<(u32, u32), Error> {
235 let sector = read_u32_le();
238 Err(Error::InvalidArgument)
239 } else if self.sector_data_length == 0 {
240 Err(Error::InvalidArgument)
242 let chunk = &self.sector_data[0..self.sector_data_length];
244 Ok(flash::verify(sector, chunk))
248 fn exec_program(&mut self) -> Result<(), Error> {
249 let sector = read_u32_le();
252 Err(Error::InvalidArgument)
253 } else if self.sector_data_length == 0 {
254 Err(Error::InvalidArgument)
256 let chunk = &self.sector_data[0..self.sector_data_length];
258 Ok(flash::program(sector, chunk))