1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2017-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 #ifndef MICROPY_INCLUDED_STM32_MBOOT_DFU_H
27 #define MICROPY_INCLUDED_STM32_MBOOT_DFU_H
28 
29 #include <stdbool.h>
30 #include <stdint.h>
31 
32 // DFU spec: https://www.usb.org/sites/default/files/DFU_1.1.pdf
33 
34 #define DFU_XFER_SIZE (2048)
35 
36 // The DFU standard allows for error messages to be sent (via str index) in GETSTATUS responses
37 #define MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX 0x10
38 #define MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER "Can't overwrite mboot"
39 
40 #define MBOOT_ERROR_STR_INVALID_ADDRESS_IDX 0x11
41 #define MBOOT_ERROR_STR_INVALID_ADDRESS "Address out of range"
42 
43 #if MBOOT_ENABLE_PACKING
44 #define MBOOT_ERROR_STR_INVALID_SIG_IDX 0x12
45 #define MBOOT_ERROR_STR_INVALID_SIG "Invalid signature in file"
46 
47 #define MBOOT_ERROR_STR_INVALID_READ_IDX 0x13
48 #define MBOOT_ERROR_STR_INVALID_READ "Read support disabled on encrypted bootloader"
49 #endif
50 
51 // DFU class requests
52 enum {
53     DFU_DETACH = 0,
54     DFU_DNLOAD = 1,
55     DFU_UPLOAD = 2,
56     DFU_GETSTATUS = 3,
57     DFU_CLRSTATUS = 4,
58     DFU_GETSTATE = 5,
59     DFU_ABORT = 6,
60 };
61 
62 // DFU States
63 typedef enum {
64     DFU_STATE_IDLE = 2,
65     DFU_STATE_BUSY = 4,
66     DFU_STATE_DNLOAD_IDLE = 5,
67     DFU_STATE_MANIFEST = 7,
68     DFU_STATE_UPLOAD_IDLE = 9,
69     DFU_STATE_ERROR = 0xa,
70 } dfu_state_t;
71 
72 typedef enum {
73     DFU_CMD_NONE = 0,
74     DFU_CMD_EXIT = 1,
75     DFU_CMD_UPLOAD = 7,
76     DFU_CMD_DNLOAD = 8,
77 } dfu_cmd_t;
78 
79 enum {
80     DFU_CMD_DNLOAD_SET_ADDRESS = 0x21,
81     DFU_CMD_DNLOAD_ERASE = 0x41,
82     DFU_CMD_DNLOAD_READ_UNPROTECT = 0x92,
83 };
84 
85 // Error status flags
86 typedef enum {
87     DFU_STATUS_OK = 0x00,  // No error condition is present.
88     DFU_STATUS_ERROR_TARGET = 0x01,  // File is not targeted for use by this device.
89     DFU_STATUS_ERROR_FILE = 0x02,  // File is for this device but fails some vendor-specific verification test.
90     DFU_STATUS_ERROR_WRITE = 0x03,  // Device is unable to write memory.
91     DFU_STATUS_ERROR_ERASE = 0x04,  // Memory erase function failed.
92     DFU_STATUS_ERROR_CHECK_ERASED = 0x05,  // Memory erase check failed.
93     DFU_STATUS_ERROR_PROG = 0x06,  // Program memory function failed.
94     DFU_STATUS_ERROR_VERIFY = 0x07,  // Programmed memory failed verification.
95     DFU_STATUS_ERROR_ADDRESS = 0x08,  // Cannot program memory due to received address that is out of range.
96     DFU_STATUS_ERROR_NOTDONE = 0x09,  // Received DFU_DNLOAD with wLength = 0, but device does not think it has all of the data yet.
97     DFU_STATUS_ERROR_FIRMWARE = 0x0A,  // Device's firmware is corrupt. It cannot return to run-time (non-DFU) operations.
98     DFU_STATUS_ERROR_VENDOR = 0x0B,  // iString indicates a vendor-specific error.
99     DFU_STATUS_ERROR_USBR = 0x0C,  // Device detected unexpected USB reset signaling.
100     DFU_STATUS_ERROR_POR = 0x0D,  // Device detected unexpected power on reset.
101     DFU_STATUS_ERROR_UNKNOWN = 0x0E,  // Something went wrong, but the device does not know what it was.
102     DFU_STATUS_ERROR_STALLEDPKT = 0x0F,  // Device stalled an unexpected request.
103 } dfu_status_t;
104 
105 typedef struct _dfu_state_t {
106     dfu_state_t state;
107     dfu_cmd_t cmd;
108     dfu_status_t status;
109     uint8_t error;
110     bool leave_dfu;
111     uint16_t wBlockNum;
112     uint16_t wLength;
113     uint32_t addr;
114     uint8_t buf[DFU_XFER_SIZE] __attribute__((aligned(4)));
115 } dfu_context_t;
116 
117 extern dfu_context_t dfu_context;
118 
119 #endif // MICROPY_INCLUDED_STM32_MBOOT_DFU_H
120