Skip to content

Commit 7ba196d

Browse files
authored
Merge pull request #20 from tock/usb-cdc
Implement bootloader for Nano 33 BLE
2 parents 98f6d1e + 3b3f528 commit 7ba196d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+4572
-1388
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
build
22
target
33
local_cargo
4+
Cargo.lock
45

README.md

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The Tock bootloader provides a utility for flashing applications onto
44
a board over USB. It is compatible with the
5-
[tockloader](https://github.com/helena-project/tockloader) utility.
5+
[tockloader](https://github.com/tock/tockloader) utility.
66

77
The Tock bootloader is implemented on top of the Tock OS itself.
88

@@ -25,12 +25,20 @@ make flash
2525
Bootloader Operation
2626
--------------------
2727

28-
When reset, the board enters the bootloader and checks the status of the
29-
`BOOTLOADER_SELECT_PIN`. If the pin is high, the bootloader exits by
30-
moving the location of the vector table to address `0x10000` and then
31-
jumping to that address. If the pin is low, the board enters bootloader mode
32-
and waits for commands to be sent over UART. To exit the bootloader mode,
33-
the chip must be reset with the `BOOTLOADER_SELECT_PIN` pulled high.
28+
When reset, the board enters the bootloader and checks whether it should
29+
continue to run the bootloader, or jump to the kernel. The bootloader uses the
30+
`BootloaderEntry` trait to determine if it should stay in the bootloader. The
31+
method of staying in the bootloader can vary based on the board. Some options:
32+
33+
- Check a GPIO pin. If the pin is high then stay in the bootloader.
34+
- Check a special memory address or register. If a magic value is stored there,
35+
then stay in the bootloader.
36+
37+
If the bootloader exists, it uses the `Jumper` trait to start executing from a
38+
different starting address. This implementation is likely architecture-specific.
39+
40+
The address the bootloader jumps to is stored in the bootloader flags section in
41+
flash. See below for more information.
3442

3543
The list of valid commands the bootloader accepts is in the
3644
[Protocol](#over-the-wire-protocol) section. At a high level, the commands
@@ -342,15 +350,78 @@ Set a new baud rate for the bootloader.
342350
- `Message`: `None`.
343351

344352

353+
#### `EXIT`
345354

355+
Exit the bootloader.
346356

347-
Future Goals
348-
------------
357+
##### Command
358+
- `Command`: `0x22`.
359+
- `Message`: `None`.
349360

350-
The bootloader is stable, but there are future improvements we would like
351-
to see added:
361+
##### Response
362+
None.
363+
364+
365+
366+
Flags and Attributes
367+
--------------------
368+
369+
The bootloader includes space near the beginning of the bootloader's space in flash for
370+
flags and attributes. Attributes are key-value pairs used to store various properties of
371+
the board. Flags are specified values for use by the bootloader.
372+
373+
### Flags
374+
375+
The flag section is 512 bytes long. Most of the section is reserved for future
376+
use.
377+
378+
```
379+
Bytes
380+
0 1 2 3
381+
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
382+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
383+
|T|O|C|K|B|O|O|T|L|O|A|D|E|R| Version Str | Reserved |
384+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
385+
| Start | Reserved...
386+
+-+-+-+-+-+-+-+-+-+-+-+-+
387+
```
388+
389+
- `TOCKBOOTLOADER`: The first fourteen bytes contain the string
390+
"TOCKBOOTLOADER".
391+
- `Version Str`: The next 8 bytes contain an ASCII string of the bootloader
392+
version.
393+
- `Start`: Four bytes of the start address to jump to if the bootloader is not
394+
entered.
395+
396+
### Attributes
397+
398+
The attributes section occupies the next 1024 bytes. Each attribute uses 64
399+
bytes of space. There can be up to 16 attributes.
400+
401+
Attribute format:
402+
403+
```
404+
Bytes
405+
0 1 2 3
406+
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
407+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
408+
| Key | Value
409+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
410+
|
411+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
412+
```
413+
414+
- `Key`: The key can be up to 8 bytes long. Keys are `\0` padded to consume all
415+
8 bytes.
416+
- `Value`: Values can be up to 56 bytes long.
417+
418+
419+
420+
421+
Changelog
422+
--------------------
352423

353-
- Arbitrary code start location. Right now the bootloader assumes that the main
354-
application code is at address `0x10000`, and that is currently a hardcoded
355-
value. This should ideally be a special flag that can get updated if we want
356-
to move the main code (for Tock the kernel) to a different address.
424+
- Version 1.1.0
425+
- Added Exit command.
426+
- Added SetStartAddress command.
427+
- Restore `BADADDR` error when trying to overwrite the bootloader itself.

arch/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Architecture-Specific Bootloader Helpers
2+
========================================
3+
4+
Code like moving the vector table is architecture-specific.
5+

arch/bootloader_cortexm/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "bootloader_cortexm"
3+
version = "0.1.0"
4+
authors = ["Tock Project Developers <[email protected]>"]
5+
6+
[dependencies]
7+
kernel = { git = "https://github.com/tock/tock", branch = "master" }
8+
#kernel = { path = "../../../tock/kernel" }
9+
10+
bootloader = { path = "../../bootloader" }

arch/bootloader_cortexm/src/jumper.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub struct CortexMJumper {}
2+
3+
impl CortexMJumper {
4+
pub fn new() -> CortexMJumper {
5+
CortexMJumper {}
6+
}
7+
}
8+
9+
impl bootloader::interfaces::Jumper for CortexMJumper {
10+
fn jump(&self, address: u32) -> ! {
11+
unsafe {
12+
asm!(
13+
".syntax unified \n\
14+
mov r0, {0} // The address of the payload's .vectors \n\
15+
ldr r1, =0xe000ed08 // The address of the VTOR register (0xE000E000(SCS) + 0xD00(SCB) + 0x8(VTOR)) \n\
16+
str r0, [r1] // Move the payload's VT address into the VTOR register \n\
17+
ldr r1, [r0] // Move the payload's initial SP into r1 \n\
18+
mov sp, r1 // Set our SP to that \n\
19+
ldr r0, [r0, #4] // Load the payload's ENTRY into r0 \n\
20+
bx r0 // Whoopee",
21+
in(reg) address,
22+
);
23+
}
24+
loop {}
25+
}
26+
}

arch/bootloader_cortexm/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![feature(const_fn, asm)]
2+
#![no_std]
3+
4+
pub mod jumper;

0 commit comments

Comments
 (0)