8000 stm32/mboot: Add support for loading gzip'd firmware from a filesystem. · lolsborn/micropython@ff04b78 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff04b78

Browse files
committed
stm32/mboot: Add support for loading gzip'd firmware from a filesystem.
This adds support to mboot to load and program application firmware from a .dfu.gz file on the board's filesystem. See mboot/README.md for details.
1 parent 4daee31 commit ff04b78

File tree

9 files changed

+636
-16
lines changed

9 files changed

+636
-16
lines changed

ports/stm32/mboot/Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ CFLAGS += -I$(BOARD_DIR)
5454
CFLAGS += -DSTM32_HAL_H='<stm32$(MCU_SERIES)xx_hal.h>'
5555
CFLAGS += -DBOARD_$(BOARD)
5656
CFLAGS += -DAPPLICATION_ADDR=$(TEXT0_ADDR)
57+
CFLAGS += -DFFCONF_H=\"ports/stm32/mboot/ffconf.h\"
5758

5859
LDFLAGS = -nostdlib -L . -T stm32_generic.ld -Map=$(@:.elf=.map) --cref
5960
LIBS = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
@@ -70,12 +71,20 @@ else
7071
COPT += -Os -DNDEBUG
7172
endif
7273

73-
SRC_LIB = $(addprefix lib/,\
74-
libc/string0.c \
75-
)
74+
SRC_LIB = \
75+
lib/libc/string0.c \
76+
lib/oofatfs/ff.c \
77+
lib/oofatfs/option/unicode.c \
78+
extmod/uzlib/crc32.c \
79+
extmod/uzlib/adler32.c \
80+
extmod/uzlib/tinflate.c \
81+
extmod/uzlib/tinfgzip.c
7682

7783
SRC_C = \
7884
main.c \
85+
elem.c \
86+
fsload.c \
87+
diskio.c \
7988
drivers/bus/softspi.c \
8089
drivers/bus/softqspi.c \
8190
drivers/memory/spiflash.c \

ports/stm32/mboot/README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Mboot - MicroPython boot loader
44
Mboot is a custom bootloader for STM32 MCUs, and currently supports the
55
STM32F4xx and STM32F7xx families. It can provide a standard USB DFU interface
66
on either the FS or HS peripherals, as well as a sophisticated, custom I2C
7-
interface. It fits in 16k of flash space.
7+
interface. It can also load and program firmware in .dfu.gz format from a
8+
filesystem. It can fit in 16k of flash space, but all features enabled requires
9+
32k.
810

911
How to use
1012
----------
@@ -57,6 +59,10 @@ How to use
5759
second one use the same configuration names as above but with
5860
`SPIFLASH2`, ie `MBOOT_SPIFLASH2_ADDR` etc.
5961

62+
To enable loading firmware from a filesystem use:
63+
64+
#define MBOOT_FSLOAD (1)
65+
6066
2. Build the board's main application firmware as usual.
6167

6268
3. Build mboot via:
@@ -77,6 +83,53 @@ How to use
7783
to communicate with the I2C boot loader interface. It should be run on a
7884
pyboard connected via I2C to the target board.
7985

86+
Entering Mboot from application code
87+
------------------------------------
88+
89+
To enter Mboot from a running application do the following:
90+
91+
1. Make sure I and D caches are disabled.
92+
93+
2. Load register r0 with the value 0x70ad0000. The lower 7 bits can be
94+
optionally or'd with the desired I2C address.
95+
96+
3. Load the MSP with the value held at 0x08000000.
97+
98+
4. Jump to the value held at 0x08000004.
99+
100+
Additional data can be passed to Mboot from application code by storing this
101+
data in a special region of RAM. This region begins at the address held at
102+
location 0x08000000 (which will point to just after Mboot's stack). A
103+
maximum of 1024 bytes can be stored here. To indicate to Mboot that this
104+
region is valid load register r0 with 0x70ad0080 (instead of step 2 above),
105+
optionally or'd with the desired I2C address.
106+
107+
Data in this region is a sequence of elements. Each element has the form:
108+
109+
<type:u8> <len:u8> <payload...>
110+
111+
where `type` and `len` are bytes (designated by `u8`) and `payload` is 0 or
112+
more bytes. `len` must be the number of bytes in `payload`.
113+
114+
The last element in the data sequence must be the end element:
115+
116+
* END: type=1, len=0
117+
118+
Loading firmware from a filesystem
119+
----------------------------------
120+
121+
To get Mboot to load firmware from a filesystem and automatically program it
122+
requires passing data elements (see above) which tell where the filesystems
123+
are located and what filename to program. The elements to use are:
124+
125+
* MOUNT: type=2, len=10, payload=(<mount-point:u8> <fs-type:u8> <base-addr:u32> <byte-len:u32>)
126+
127+
* FSLOAD: type=3, len=1+n, payload=(<mount-point:u8> <filename...>)
128+
129+
`u32` means unsigned 32-bit little-endian integer.
130+
131+
The firmware to load must be a gzip'd DfuSe file (.dfu.gz).
132+
80133
Example: Mboot on PYBv1.x
81134
-------------------------
82135

ports/stm32/mboot/diskio.c

Lines changed: 80 additions & 0 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/mphal.h"
28+
#include "lib/oofatfs/ff.h"
29+
#include "lib/oofatfs/diskio.h"
30+
#include "mboot.h"
31+
32+
#if MBOOT_FSLOAD
33+
34+
#if _MAX_SS == _MIN_SS
35+
#define SECSIZE (_MIN_SS)
36+
#else
37+
#error Unsupported
38+
#endif
39+
40+
DRESULT disk_read(void *pdrv, BYTE *buf, DWORD sector, UINT count) {
41+
fsload_bdev_t *bdev = pdrv;
42+
43+
if (0 <= sector && sector < bdev->byte_len / 512) {
44+
do_read(bdev->base_addr + sector * SECSIZE, count * SECSIZE, buf);
45+
return RES_OK;
46+
}
47+
48+
return RES_PARERR;
49+
}
50+
51+
DRESULT disk_ioctl(void *pdrv, BYTE cmd, void *buf) {
52+
fsload_bdev_t *bdev = pdrv;
53+
54+
switch (cmd) {
55+
case CTRL_SYNC:
56+
return RES_OK;
57+
58+
case GET_SECTOR_COUNT:
59+
*((DWORD*)buf) = bdev->byte_len / SECSIZE;
60+
return RES_OK;
61+
62+
case GET_SECTOR_SIZE:
63+
*((WORD*)buf) = SECSIZE;
64+
return RES_OK;
65+
66+
case GET_BLOCK_SIZE:
67+
*((DWORD*)buf) = 1; // erase block size in units of sector size
68+
return RES_OK;
69+
70+
case IOCTL_INIT:
71+
case IOCTL_STATUS:
72+
*((DSTATUS*)buf) = STA_PROTECT;
73+
return RES_OK;
74+
75+
default:
76+
return RES_PARERR;
77+
}
78+
}
79+
80+
#endif // MBOOT_FSLOAD

ports/stm32/mboot/elem.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "mboot.h"
28+
29+
// Elements are of the form: (type:u8, len:u8, payload)
30+
31+
const uint8_t *elem_search(const uint8_t *elem, uint8_t elem_id) {
32+
while (elem + 2 + elem[1] <= ELEM_DATA_MAX) {
33+
if (elem[0] == elem_id) {
34+
// Found element, return a pointer to the element data
35+
return elem + 2;
36+
}
37+
if (elem[0] == ELEM_TYPE_END) {
38+
// End of elements
39+
return NULL;
40+
}
41+
elem += 2 + elem[1];
42+
}
43+
return NULL;
44+
}

ports/stm32/mboot/ffconf.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#define _FFCONF 68020
28+
29+
#define _FS_READONLY 1
30+
#define _FS_MINIMIZE 0
31+
#define _USE_STRFUNC 0
32+
33+
#define _USE_FIND 0
34+
#define _USE_MKFS 0
35+
#define _USE_FASTSEEK 0
36+
#define _USE_EXPAND 0
37+
#define _USE_CHMOD 0
38+
#define _USE_LABEL 0
39+
#define _USE_FORWARD 0
40+
41+
#define _CODE_PAGE 437
42+
#define _USE_LFN 1
43+
#define _MAX_LFN 255
44+
#define _LFN_UNICODE 0
45+
#define _STRF_ENCODE 3
46+
#define _FS_RPATH 0
47+
48+
#define _VOLUMES 1
49+
#define _STR_VOLUME_ID 0
50+
#define _MULTI_PARTITION 0
51+
#define _MIN_SS 512
52+
#define _MAX_SS 512
53+
#define _USE_TRIM 0
54+
#define _FS_NOFSINFO 0
55+
56+
#define _FS_TINY 1
57+
#define _FS_EXFAT 0
58+
#define _FS_NORTC 1
59+
#define _NORTC_MON 1
60+
#define _NORTC_MDAY 1
61+
#define _NORTC_YEAR 2019
62+
#define _FS_LOCK 0
63+
#define _FS_REENTRANT 0

0 commit comments

Comments
 (0)
0