1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/*
* Copyright 2010, 2011 Michael Ossmann
*
* This file is part of Project Ubertooth.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "dfu.h"
#include <string.h>
struct memory_policy {
static bool write_permitted(const uint32_t flash_address) {
// Protect flash region containing bootloader.
return (flash_address >= 0x4000);
}
};
DFU::DFU(Flash& flash) :
flash(flash),
status(OK),
state(DFUIDLE),
virginity(true) {
}
bool DFU::request_handler(TSetupPacket *pSetup, uint32_t *piLen, uint8_t **ppbData) {
uint8_t* pbData = *ppbData;
switch( pSetup->bRequest ) {
case DETACH:
return request_detach(pSetup, piLen, pbData);
case DNLOAD:
return request_dnload(pSetup, piLen, pbData);
case UPLOAD:
return request_upload(pSetup, piLen, pbData);
case GETSTATUS:
return request_getstatus(pSetup, piLen, pbData);
case CLRSTATUS:
return request_clrstatus(pSetup, piLen, pbData);
case GETSTATE:
return request_getstate(pSetup, piLen, pbData);
case ABORT:
return request_abort(pSetup, piLen, pbData);
default:
return false;
}
}
bool DFU::in_dfu_mode() const {
return (get_state() >= DFUIDLE);
}
bool DFU::dfu_virgin() const {
return virginity;
}
void DFU::set_state(const State new_state) {
state = new_state;
virginity = false;
}
uint8_t DFU::get_state() const {
return state;
}
void DFU::set_status(const Status new_status) {
status = new_status;
}
uint8_t DFU::get_status() const {
return status;
}
bool DFU::error(const Status new_status) {
if( (get_state() != APPIDLE) &&
(get_state() != APPDETACH) ) {
set_state(DFUERROR);
}
set_status(new_status);
return false;
}
uint8_t DFU::get_status_string_id() const {
return 0;
}
uint32_t DFU::get_poll_timeout() const {
return 20; // milliseconds
}
bool DFU::request_detach(TSetupPacket *pSetup, uint32_t *piLen, uint8_t* pbData) {
if( (pSetup->wLength == 0) && (pSetup->wValue <= detach_timeout_ms) ) {
// TODO: Check DFU vs. APP mode, and reboot device if in DFU mode?
set_state(APPDETACH);
return true;
} else {
return error(ERRUNKNOWN);
}
}
bool DFU::request_dnload(TSetupPacket *pSetup, uint32_t *piLen, uint8_t *pbData) {
if( pSetup->wLength == 0 ) {
if( get_state() != DFUDNLOAD_IDLE ) {
return error(ERRSTALLEDPKT);
}
// End of transfer
set_state(DFUMANIFEST_SYNC);
return true;
} else if( pSetup->wLength == transfer_size ) {
if( (get_state() != DFUIDLE) && (get_state() != DFUDNLOAD_IDLE) ) {
return error(ERRSTALLEDPKT);
}
// TODO: Improve returned error values.
const uint32_t length = pSetup->wLength;
const uint32_t flash_address = pSetup->wValue * transfer_size;
const uint32_t source_address = reinterpret_cast<uint32_t>(pbData);
if( memory_policy::write_permitted(flash_address) ) {
if( flash.write(flash_address, source_address, length) ) {
set_state(DFUDNLOAD_SYNC);
} else {
return error(ERRPROG);
}
return true;
} else {
return error(ERRWRITE);
}
} else {
return error(ERRUNKNOWN);
}
}
bool DFU::request_upload(TSetupPacket *pSetup, uint32_t *piLen, uint8_t* pbData) {
if( pSetup->wLength == transfer_size ) {
if( (get_state() != DFUIDLE) && (get_state() != DFUUPLOAD_IDLE) ) {
return error(ERRSTALLEDPKT);
}
const uint32_t length = pSetup->wLength;
const uint32_t flash_address_start = pSetup->wValue * transfer_size;
const uint32_t flash_address_end = flash_address_start + length;
if( flash.valid_address(flash_address_start) && flash.valid_address(flash_address_end - 1) ) {
memcpy(pbData, reinterpret_cast<const void*>(flash_address_start), length);
*piLen = length;
set_state(DFUIDLE);
return true;
} else {
return error(ERRADDRESS);
}
} else {
return error(ERRUNKNOWN);
}
}
bool DFU::request_getstatus(TSetupPacket *pSetup, uint32_t *piLen, uint8_t* pbData) {
if( (pSetup->wValue == 0) && (pSetup->wLength == 6) ) {
switch( get_state() ) {
case DFUDNLOAD_SYNC:
set_state(DFUDNLOAD_IDLE);
break;
case DFUMANIFEST_SYNC:
set_state(DFUIDLE);
break;
default:
break;
}
pbData[0] = get_status();
pbData[1] = (get_poll_timeout() >> 0) & 0xFF;
pbData[2] = (get_poll_timeout() >> 8) & 0xFF;
pbData[3] = (get_poll_timeout() >> 16) & 0xFF;
pbData[4] = get_state();
pbData[5] = get_status_string_id();
*piLen = 6;
return true;
} else {
return false;
}
}
bool DFU::request_clrstatus(TSetupPacket *pSetup, uint32_t *piLen, uint8_t* pbData) {
if( (pSetup->wValue == 0) && (pSetup->wLength == 0) ) {
if( get_state() == DFUERROR ) {
set_status(OK);
set_state(DFUIDLE);
return true;
} else {
return error(ERRUNKNOWN);
}
} else {
return error(ERRUNKNOWN);
}
}
bool DFU::request_getstate(TSetupPacket *pSetup, uint32_t *piLen, uint8_t* pbData) {
if( (pSetup->wValue == 0) && (pSetup->wLength == 1) ) {
pbData[0] = get_state();
*piLen = 1;
return true;
} else {
return error(ERRUNKNOWN);
}
}
bool DFU::request_abort(TSetupPacket *pSetup, uint32_t *piLen, uint8_t* pbData) {
if( (pSetup->wValue == 0) && (pSetup->wLength == 0) ) {
if( get_state() != DFUERROR ) {
set_state(DFUIDLE);
return true;
} else {
return error(ERRUNKNOWN);
}
} else {
return error(ERRUNKNOWN);
}
}
|