1 |
/* |
---|
2 |
* Copyright (C) 2005-2007 by Daniel Wagner |
---|
3 |
* |
---|
4 |
* This file is part of FFADO |
---|
5 |
* FFADO = Free Firewire (pro-)audio drivers for linux |
---|
6 |
* |
---|
7 |
* FFADO is based upon FreeBoB |
---|
8 |
* |
---|
9 |
* This program is free software: you can redistribute it and/or modify |
---|
10 |
* it under the terms of the GNU General Public License as published by |
---|
11 |
* the Free Software Foundation, either version 3 of the License, or |
---|
12 |
* (at your option) any later version. |
---|
13 |
* |
---|
14 |
* This program is distributed in the hope that it will be useful, |
---|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 |
* GNU General Public License for more details. |
---|
18 |
* |
---|
19 |
* You should have received a copy of the GNU General Public License |
---|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
21 |
* |
---|
22 |
*/ |
---|
23 |
|
---|
24 |
#include "bebob_dl_mgr.h" |
---|
25 |
#include "bebob_dl_codes.h" |
---|
26 |
#include "bebob_dl_bcd.h" |
---|
27 |
|
---|
28 |
#include "libieee1394/configrom.h" |
---|
29 |
#include "libieee1394/ieee1394service.h" |
---|
30 |
|
---|
31 |
#include "libutil/cmd_serialize.h" |
---|
32 |
#include "libutil/Time.h" |
---|
33 |
|
---|
34 |
#include <netinet/in.h> |
---|
35 |
|
---|
36 |
#include <cstdio> |
---|
37 |
|
---|
38 |
namespace BeBoB { |
---|
39 |
enum { |
---|
40 |
AddrRegInfo = 0xffffc8020000ULL, |
---|
41 |
AddrRegReq = 0xffffc8021000ULL, |
---|
42 |
AddrRegReqBuf = 0xffffc8021040ULL, |
---|
43 |
AddrRegResp = 0xffffc8029000ULL, |
---|
44 |
AddrRegRespBuf = 0xffffc8029040ULL, |
---|
45 |
}; |
---|
46 |
|
---|
47 |
enum { |
---|
48 |
RegInfoManufactorIdOffset = 0x00, |
---|
49 |
RegInfoProtocolVersionOffset = 0x08, |
---|
50 |
RegInfoBootloaderVersionOffset = 0x0c, |
---|
51 |
RegInfoGUID = 0x10, |
---|
52 |
RegInfoHardwareModelId = 0x18, |
---|
53 |
RegInfoHardwareRevision = 0x1c, |
---|
54 |
RegInfoSoftwareDate = 0x20, |
---|
55 |
RegInfoSoftwareTime = 0x28, |
---|
56 |
RegInfoSoftwareId = 0x30, |
---|
57 |
RegInfoSoftwareVersion = 0x34, |
---|
58 |
RegInfoBaseAddress = 0x38, |
---|
59 |
RegInfoMaxImageLen = 0x3c, |
---|
60 |
RegInfoBootloaderDate = 0x40, |
---|
61 |
RegInfoBootloaderTime = 0x48, |
---|
62 |
RegInfoDebuggerDate = 0x50, |
---|
63 |
RegInfoDebuggerTime = 0x58, |
---|
64 |
RegInfoDebuggerId = 0x60, |
---|
65 |
RegInfoDebuggerVersion = 0x64 |
---|
66 |
}; |
---|
67 |
|
---|
68 |
enum { |
---|
69 |
MaxRetries = 10, |
---|
70 |
}; |
---|
71 |
|
---|
72 |
IMPL_DEBUG_MODULE( BootloaderManager, BootloaderManager, DEBUG_LEVEL_NORMAL ); |
---|
73 |
} |
---|
74 |
|
---|
75 |
BeBoB::BootloaderManager::BootloaderManager(Ieee1394Service& ieee1349service, |
---|
76 |
fb_nodeid_t nodeId ) |
---|
77 |
: m_ieee1394service( &ieee1349service ) |
---|
78 |
, m_protocolVersion( eBPV_Unknown ) |
---|
79 |
, m_isAppRunning( false ) |
---|
80 |
, m_forceEnabled( false ) |
---|
81 |
, m_bStartBootloader( true ) |
---|
82 |
{ |
---|
83 |
memset( &m_cachedInfoRegs, 0, sizeof( m_cachedInfoRegs ) ); |
---|
84 |
|
---|
85 |
m_configRom = new ConfigRom( *m_ieee1394service, nodeId ); |
---|
86 |
// XXX throw exception if initialize fails! |
---|
87 |
m_configRom->initialize(); |
---|
88 |
if ( !cacheInfoRegisters() ) { |
---|
89 |
debugError( "BootloaderManager: could not cache info registers\n" ); |
---|
90 |
} |
---|
91 |
|
---|
92 |
switch( m_cachedInfoRegs.m_protocolVersion ) { |
---|
93 |
case 1: |
---|
94 |
m_protocolVersion = eBPV_V1; |
---|
95 |
break; |
---|
96 |
case 3: |
---|
97 |
m_protocolVersion = eBPV_V3; |
---|
98 |
break; |
---|
99 |
default: |
---|
100 |
// exception? |
---|
101 |
break; |
---|
102 |
} |
---|
103 |
|
---|
104 |
pthread_mutex_init( &m_mutex, 0 ); |
---|
105 |
pthread_cond_init( &m_cond, 0 ); |
---|
106 |
|
---|
107 |
m_functor = new MemberFunctor0< BeBoB::BootloaderManager*, |
---|
108 |
void (BeBoB::BootloaderManager::*)() > |
---|
109 |
( this, &BeBoB::BootloaderManager::busresetHandler, false ); |
---|
110 |
m_ieee1394service->addBusResetHandler( m_functor ); |
---|
111 |
} |
---|
112 |
|
---|
113 |
BeBoB::BootloaderManager::~BootloaderManager() |
---|
114 |
{ |
---|
115 |
m_ieee1394service->remBusResetHandler( m_functor ); |
---|
116 |
delete( m_functor ); |
---|
117 |
|
---|
118 |
delete m_configRom; |
---|
119 |
|
---|
120 |
pthread_cond_destroy( &m_cond ); |
---|
121 |
pthread_mutex_destroy( &m_mutex ); |
---|
122 |
} |
---|
123 |
|
---|
124 |
bool |
---|
125 |
BeBoB::BootloaderManager::cacheInfoRegisters() |
---|
126 |
{ |
---|
127 |
if ( !m_configRom->updatedNodeId() ) { |
---|
128 |
debugError( "cacheInfoRegisters: did not find device anymore\n" ); |
---|
129 |
return false; |
---|
130 |
} |
---|
131 |
|
---|
132 |
if ( !m_ieee1394service->read( |
---|
133 |
0xffc0 | m_configRom->getNodeId(), |
---|
134 |
AddrRegInfo, |
---|
135 |
sizeof( m_cachedInfoRegs )/4, |
---|
136 |
reinterpret_cast<fb_quadlet_t*>( &m_cachedInfoRegs ) ) ) |
---|
137 |
{ |
---|
138 |
return false; |
---|
139 |
} |
---|
140 |
|
---|
141 |
if ( m_cachedInfoRegs.m_bootloaderVersion != 0x0 ) { |
---|
142 |
m_isAppRunning = false; |
---|
143 |
} else { |
---|
144 |
m_isAppRunning = true; |
---|
145 |
} |
---|
146 |
|
---|
147 |
m_cachedInfoRegs.m_guid = ( m_cachedInfoRegs.m_guid >> 32 ) |
---|
148 |
| ( m_cachedInfoRegs.m_guid << 32 ); |
---|
149 |
|
---|
150 |
return true; |
---|
151 |
} |
---|
152 |
|
---|
153 |
bool |
---|
154 |
BeBoB::BootloaderManager::cacheInfoRegisters( int retries ) |
---|
155 |
{ |
---|
156 |
for ( int i = 0; i < retries; ++i ) { |
---|
157 |
if ( cacheInfoRegisters() ) { |
---|
158 |
return true; |
---|
159 |
} |
---|
160 |
sleep( 1 ); |
---|
161 |
} |
---|
162 |
|
---|
163 |
return false; |
---|
164 |
} |
---|
165 |
|
---|
166 |
void |
---|
167 |
BeBoB::BootloaderManager::printInfoRegisters() |
---|
168 |
{ |
---|
169 |
using namespace std; |
---|
170 |
|
---|
171 |
if ( !cacheInfoRegisters() ) { |
---|
172 |
debugError( "Could not read info registers\n" ); |
---|
173 |
return; |
---|
174 |
} |
---|
175 |
|
---|
176 |
printf( "Info Registers\n" ); |
---|
177 |
printf( "\tManufactors Id:\t\t%s\n", |
---|
178 |
makeString( m_cachedInfoRegs.m_manId ).c_str() ); |
---|
179 |
printf( "\tProtocol Version:\t0x%08x\n", |
---|
180 |
m_cachedInfoRegs.m_protocolVersion ); |
---|
181 |
printf( "\tBootloader Version:\t0x%08x\n", |
---|
182 |
m_cachedInfoRegs.m_bootloaderVersion ); |
---|
183 |
printf( "\tGUID:\t\t\t0x%08x%08x\n", |
---|
184 |
( unsigned int )( m_cachedInfoRegs.m_guid >> 32 ), |
---|
185 |
( unsigned int )( m_cachedInfoRegs.m_guid & 0xffffffff ) ); |
---|
186 |
printf( "\tHardware Model ID:\t0x%08x\n", |
---|
187 |
m_cachedInfoRegs.m_hardwareModelId ); |
---|
188 |
printf( "\tHardware Revision:\t0x%08x\n", |
---|
189 |
m_cachedInfoRegs.m_hardwareRevision ); |
---|
190 |
if ( m_cachedInfoRegs.m_softwareDate |
---|
191 |
&& m_cachedInfoRegs.m_softwareTime ) |
---|
192 |
{ |
---|
193 |
printf( "\tSoftware Date:\t\t%s, %s\n", |
---|
194 |
makeDate( m_cachedInfoRegs.m_softwareDate ).c_str(), |
---|
195 |
makeTime( m_cachedInfoRegs.m_softwareTime ).c_str() ); |
---|
196 |
} |
---|
197 |
printf( "\tSoftware Id:\t\t0x%08x\n", m_cachedInfoRegs.m_softwareId ); |
---|
198 |
printf( "\tSoftware Version:\t0x%08x\n", |
---|
199 |
m_cachedInfoRegs.m_softwareVersion ); |
---|
200 |
printf( "\tBase Address:\t\t0x%08x\n", m_cachedInfoRegs.m_baseAddress ); |
---|
201 |
printf( "\tMax. Image Len:\t\t0x%08x\n", m_cachedInfoRegs.m_maxImageLen ); |
---|
202 |
if ( m_cachedInfoRegs.m_bootloaderDate |
---|
203 |
&& m_cachedInfoRegs.m_bootloaderTime ) |
---|
204 |
{ |
---|
205 |
printf( "\tBootloader Date:\t%s, %s\n", |
---|
206 |
makeDate( m_cachedInfoRegs.m_bootloaderDate ).c_str(), |
---|
207 |
makeTime( m_cachedInfoRegs.m_bootloaderTime ).c_str() ); |
---|
208 |
} |
---|
209 |
if ( m_cachedInfoRegs.m_debuggerDate |
---|
210 |
&& m_cachedInfoRegs.m_debuggerTime ) |
---|
211 |
{ |
---|
212 |
printf( "\tDebugger Date:\t\t%s, %s\n", |
---|
213 |
makeDate( m_cachedInfoRegs.m_debuggerDate ).c_str(), |
---|
214 |
makeTime( m_cachedInfoRegs.m_debuggerTime ).c_str() ); |
---|
215 |
} |
---|
216 |
printf( "\tDebugger Id:\t\t0x%08x\n", m_cachedInfoRegs.m_debuggerId ); |
---|
217 |
printf( "\tDebugger Version:\t0x%08x\n", |
---|
218 |
m_cachedInfoRegs.m_debuggerVersion ); |
---|
219 |
} |
---|
220 |
|
---|
221 |
bool |
---|
222 |
BeBoB::BootloaderManager::downloadFirmware( std::string filename ) |
---|
223 |
{ |
---|
224 |
using namespace std; |
---|
225 |
|
---|
226 |
printf( "parse BCD file\n" ); |
---|
227 |
std::auto_ptr<BCD> bcd = std::auto_ptr<BCD>( new BCD( filename ) ); |
---|
228 |
if ( !bcd.get() ) { |
---|
229 |
debugError( "downloadFirmware: Could not open or parse BCD '%s'\n", |
---|
230 |
filename.c_str() ); |
---|
231 |
return false; |
---|
232 |
} |
---|
233 |
if ( !bcd->parse() ) { |
---|
234 |
debugError( "downloadFirmware: BCD parsing failed\n" ); |
---|
235 |
return false; |
---|
236 |
} |
---|
237 |
|
---|
238 |
printf( "check firmware device compatibility... " ); |
---|
239 |
if ( !m_forceEnabled ) { |
---|
240 |
if ( !checkDeviceCompatibility( *bcd ) ) { |
---|
241 |
printf( "failed.\n" ); |
---|
242 |
return false; |
---|
243 |
} |
---|
244 |
printf( "ok\n" ); |
---|
245 |
} else { |
---|
246 |
printf( "forced\n" ); |
---|
247 |
} |
---|
248 |
|
---|
249 |
if ( m_bStartBootloader ) { |
---|
250 |
printf( "prepare for download (start bootloader)\n" ); |
---|
251 |
if ( !startBootloaderCmd() ) { |
---|
252 |
debugError( "downloadFirmware: Could not start bootloader\n" ); |
---|
253 |
return false; |
---|
254 |
} |
---|
255 |
} |
---|
256 |
|
---|
257 |
printf( "start downloading protocol for application image\n" ); |
---|
258 |
if ( !downloadObject( *bcd, eOT_Application ) ) { |
---|
259 |
debugError( "downloadFirmware: Firmware download failed\n" ); |
---|
260 |
return false; |
---|
261 |
} |
---|
262 |
|
---|
263 |
printf( "start downloading protocol for CnE\n" ); |
---|
264 |
if ( !downloadObject( *bcd, eOT_CnE ) ) { |
---|
265 |
debugError( "downloadFirmware: CnE download failed\n" ); |
---|
266 |
return false; |
---|
267 |
} |
---|
268 |
|
---|
269 |
printf( "setting CnE to factory default settings\n" ); |
---|
270 |
if ( !initializeConfigToFactorySettingCmd() ) { |
---|
271 |
debugError( "downloadFirmware: Could not reinitalize CnE\n" ); |
---|
272 |
return false; |
---|
273 |
} |
---|
274 |
|
---|
275 |
printf( "start application\n" ); |
---|
276 |
if ( !startApplicationCmd() ) { |
---|
277 |
debugError( "downloadFirmware: Could not restart application\n" ); |
---|
278 |
return false; |
---|
279 |
} |
---|
280 |
|
---|
281 |
return true; |
---|
282 |
} |
---|
283 |
|
---|
284 |
bool |
---|
285 |
BeBoB::BootloaderManager::downloadCnE( std::string filename ) |
---|
286 |
{ |
---|
287 |
using namespace std; |
---|
288 |
|
---|
289 |
printf( "parse BCD file\n" ); |
---|
290 |
std::auto_ptr<BCD> bcd = std::auto_ptr<BCD>( new BCD( filename ) ); |
---|
291 |
if ( !bcd.get() ) { |
---|
292 |
debugError( "downloadCnE: Could not open or parse BCD '%s'\n", |
---|
293 |
filename.c_str() ); |
---|
294 |
return false; |
---|
295 |
} |
---|
296 |
if ( !bcd->parse() ) { |
---|
297 |
debugError( "downloadCnE: BCD parsing failed\n" ); |
---|
298 |
return false; |
---|
299 |
} |
---|
300 |
|
---|
301 |
printf( "check firmware device compatibility... " ); |
---|
302 |
if ( !m_forceEnabled ) { |
---|
303 |
if ( !checkDeviceCompatibility( *bcd ) ) { |
---|
304 |
printf( "failed.\n" ); |
---|
305 |
return false; |
---|
306 |
} |
---|
307 |
printf( "ok\n" ); |
---|
308 |
} else { |
---|
309 |
printf( "forced\n" ); |
---|
310 |
} |
---|
311 |
|
---|
312 |
if ( m_bStartBootloader ) { |
---|
313 |
printf( "prepare for download (start bootloader)\n" ); |
---|
314 |
if ( !startBootloaderCmd() ) { |
---|
315 |
debugError( "downloadCnE: Could not start bootloader\n" ); |
---|
316 |
return false; |
---|
317 |
} |
---|
318 |
} |
---|
319 |
|
---|
320 |
printf( "start downloading protocol for CnE\n" ); |
---|
321 |
if ( !downloadObject( *bcd, eOT_CnE ) ) { |
---|
322 |
debugError( "downloadCnE: CnE download failed\n" ); |
---|
323 |
return false; |
---|
324 |
} |
---|
325 |
|
---|
326 |
printf( "setting CnE to factory default settings\n" ); |
---|
327 |
if ( !initializeConfigToFactorySettingCmd() ) { |
---|
328 |
debugError( "downloadFirmware: Could not reinitalize CnE\n" ); |
---|
329 |
return false; |
---|
330 |
} |
---|
331 |
|
---|
332 |
printf( "start application\n" ); |
---|
333 |
if ( !startApplicationCmd() ) { |
---|
334 |
debugError( "downloadCnE: Could not restart application\n" ); |
---|
335 |
return false; |
---|
336 |
} |
---|
337 |
|
---|
338 |
return true; |
---|
339 |
} |
---|
340 |
|
---|
341 |
|
---|
342 |
bool |
---|
343 |
BeBoB::BootloaderManager::downloadObject( BCD& bcd, EObjectType eObject ) |
---|
344 |
{ |
---|
345 |
using namespace std; |
---|
346 |
|
---|
347 |
CommandCodesDownloadStart::EObject eCCDSObject; |
---|
348 |
fb_quadlet_t baseAddress; |
---|
349 |
fb_quadlet_t imageLength; |
---|
350 |
fb_quadlet_t crc; |
---|
351 |
fb_quadlet_t offset; |
---|
352 |
|
---|
353 |
switch ( eObject ) { |
---|
354 |
case eOT_Application: |
---|
355 |
eCCDSObject = CommandCodesDownloadStart::eO_Application; |
---|
356 |
baseAddress = bcd.getImageBaseAddress(); |
---|
357 |
imageLength = bcd.getImageLength(); |
---|
358 |
crc = bcd.getImageCRC(); |
---|
359 |
offset = bcd.getImageOffset(); |
---|
360 |
break; |
---|
361 |
case eOT_CnE: |
---|
362 |
eCCDSObject = CommandCodesDownloadStart::eO_Config; |
---|
363 |
baseAddress = 0; |
---|
364 |
imageLength = bcd.getCnELength(); |
---|
365 |
crc = bcd.getCnECRC(); |
---|
366 |
offset = bcd.getCnEOffset(); |
---|
367 |
break; |
---|
368 |
default: |
---|
369 |
return false; |
---|
370 |
} |
---|
371 |
|
---|
372 |
CommandCodesDownloadStart ccDStart ( m_protocolVersion, eCCDSObject ); |
---|
373 |
ccDStart.setDate( bcd.getSoftwareDate() ); |
---|
374 |
ccDStart.setTime( bcd.getSoftwareTime() ); |
---|
375 |
ccDStart.setId( bcd.getSoftwareId() ); |
---|
376 |
ccDStart.setVersion( bcd.getSoftwareVersion() ); |
---|
377 |
ccDStart.setBaseAddress( baseAddress ); |
---|
378 |
ccDStart.setLength( imageLength ); |
---|
379 |
ccDStart.setCRC( crc ); |
---|
380 |
|
---|
381 |
if ( !writeRequest( ccDStart ) ) { |
---|
382 |
debugError( "downloadObject: start command write request failed\n" ); |
---|
383 |
return false; |
---|
384 |
} |
---|
385 |
|
---|
386 |
// bootloader erases the flash, have to wait until is ready |
---|
387 |
// to answer our next request |
---|
388 |
printf( "wait until flash ereasing has terminated\n" ); |
---|
389 |
sleep( 30 ); |
---|
390 |
|
---|
391 |
if ( !readResponse( ccDStart ) ) { |
---|
392 |
debugError( "downloadObject: (start) command read request failed\n" ); |
---|
393 |
return false; |
---|
394 |
} |
---|
395 |
|
---|
396 |
if ( ccDStart.getMaxBlockSize() < 0 ) { |
---|
397 |
debugError( "downloadObject: (start) command reported error %d\n", |
---|
398 |
ccDStart.getMaxBlockSize() ); |
---|
399 |
return false; |
---|
400 |
} |
---|
401 |
|
---|
402 |
unsigned int maxBlockSize = m_configRom->getAsyMaxPayload(); |
---|
403 |
unsigned int i = 0; |
---|
404 |
fb_quadlet_t address = 0; |
---|
405 |
bool result = true; |
---|
406 |
int totalBytes = imageLength; |
---|
407 |
int downloadedBytes = 0; |
---|
408 |
while ( imageLength > 0 ) { |
---|
409 |
unsigned int blockSize = imageLength > maxBlockSize ? |
---|
410 |
maxBlockSize : imageLength; |
---|
411 |
|
---|
412 |
fb_byte_t block[blockSize]; |
---|
413 |
if ( !bcd.read( offset, block, blockSize ) ) { |
---|
414 |
result = false; |
---|
415 |
break; |
---|
416 |
} |
---|
417 |
|
---|
418 |
if ( !get1394Serivce()->write( |
---|
419 |
0xffc0 | getConfigRom()->getNodeId(), |
---|
420 |
AddrRegReqBuf, |
---|
421 |
( blockSize + 3 ) / 4, |
---|
422 |
reinterpret_cast<fb_quadlet_t*>( block ) ) ) |
---|
423 |
{ |
---|
424 |
debugError( "downloadObject: Could not write to node %d\n", |
---|
425 |
getConfigRom()->getNodeId() ); |
---|
426 |
return false; |
---|
427 |
} |
---|
428 |
|
---|
429 |
CommandCodesDownloadBlock ccBlock( m_protocolVersion ); |
---|
430 |
ccBlock.setSeqNumber( i ); |
---|
431 |
ccBlock.setAddress( baseAddress + address ); |
---|
432 |
ccBlock.setNumberBytes( blockSize ); |
---|
433 |
|
---|
434 |
if ( !writeRequest( ccBlock ) ) { |
---|
435 |
debugError( "downloadObject: (block) write request failed" ); |
---|
436 |
result = false; |
---|
437 |
break; |
---|
438 |
} |
---|
439 |
SleepRelativeUsec( 100 ); |
---|
440 |
|
---|
441 |
if ( !readResponse( ccBlock ) ) { |
---|
442 |
debugError( "downloadObject: (block) read request failed\n" ); |
---|
443 |
result = false; |
---|
444 |
break; |
---|
445 |
} |
---|
446 |
|
---|
447 |
if ( i != ccBlock.getRespSeqNumber() ) { |
---|
448 |
debugError( "downloadObject: (block) wrong sequence number " |
---|
449 |
"reported. %d expected, %d reported\n", |
---|
450 |
i, ccBlock.getRespSeqNumber() ); |
---|
451 |
result = false; |
---|
452 |
break; |
---|
453 |
} |
---|
454 |
if ( ccBlock.getRespErrorCode() != 0 ) { |
---|
455 |
debugError( "downloadObject: (block) failed download with " |
---|
456 |
"error code 0x%08x\n", ccBlock.getRespErrorCode() ); |
---|
457 |
result = false; |
---|
458 |
break; |
---|
459 |
} |
---|
460 |
|
---|
461 |
downloadedBytes += blockSize; |
---|
462 |
if ( ( i % 100 ) == 0 ) { |
---|
463 |
printf( "%10d/%d bytes downloaded\n", |
---|
464 |
downloadedBytes, totalBytes ); |
---|
465 |
} |
---|
466 |
|
---|
467 |
imageLength -= blockSize; |
---|
468 |
address += blockSize; |
---|
469 |
offset += blockSize; |
---|
470 |
i++; |
---|
471 |
} |
---|
472 |
printf( "%10d/%d bytes downloaded\n", |
---|
473 |
downloadedBytes, totalBytes ); |
---|
474 |
|
---|
475 |
if ( !result ) { |
---|
476 |
debugError( "downloadObject: seqNumber = %d, " |
---|
477 |
"address = 0%08x, offset = 0x%08x, " |
---|
478 |
"restImageLength = 0x%08x\n", |
---|
479 |
i, address, offset, imageLength ); |
---|
480 |
} |
---|
481 |
|
---|
482 |
CommandCodesDownloadEnd ccEnd( m_protocolVersion ); |
---|
483 |
if ( !writeRequest( ccEnd ) ) { |
---|
484 |
debugError( "downloadObject: (end) command write failed\n" ); |
---|
485 |
} |
---|
486 |
|
---|
487 |
printf( "wait for transaction completion\n" ); |
---|
488 |
sleep( 10 ); |
---|
489 |
|
---|
490 |
if ( !readResponse( ccEnd ) ) { |
---|
491 |
debugError( "downloadObject: (end) command read failed\n" ); |
---|
492 |
} |
---|
493 |
|
---|
494 |
if ( result ) { |
---|
495 |
if ( ccEnd.getRespIsValid() ) { |
---|
496 |
if ( ccEnd.getRespCrc32() == crc ) { |
---|
497 |
debugOutput( DebugModule::eDL_Normal, |
---|
498 |
"downloadObject: CRC match\n" ); |
---|
499 |
} else { |
---|
500 |
debugError( "downloadObject: CRC mismatch. 0x%08x expected, " |
---|
501 |
"0x%08x reported", |
---|
502 |
crc, ccEnd.getRespCrc32() ); |
---|
503 |
result = false; |
---|
504 |
} |
---|
505 |
} else { |
---|
506 |
debugError( "downloadObject: (end) object is not valid\n" ); |
---|
507 |
result = false; |
---|
508 |
} |
---|
509 |
} |
---|
510 |
printf( "download protocol successfuly completed\n" ); |
---|
511 |
return result; |
---|
512 |
} |
---|
513 |
|
---|
514 |
bool |
---|
515 |
BeBoB::BootloaderManager::programGUID( fb_octlet_t guid ) |
---|
516 |
{ |
---|
517 |
if ( m_bStartBootloader ) { |
---|
518 |
if ( !startBootloaderCmd() ) { |
---|
519 |
debugError( "programGUID: Could not start bootloader\n" ); |
---|
520 |
return false; |
---|
521 |
} |
---|
522 |
} |
---|
523 |
|
---|
524 |
if ( !programGUIDCmd( guid ) ) { |
---|
525 |
debugError( "programGUID: Could not program guid\n" ); |
---|
526 |
return false; |
---|
527 |
} |
---|
528 |
|
---|
529 |
if ( !startApplicationCmd() ) { |
---|
530 |
debugError( "Could not restart application\n"); |
---|
531 |
return false; |
---|
532 |
} |
---|
533 |
|
---|
534 |
return true; |
---|
535 |
} |
---|
536 |
|
---|
537 |
void |
---|
538 |
BeBoB::BootloaderManager::busresetHandler() |
---|
539 |
{ |
---|
540 |
pthread_cond_signal( &m_cond ); |
---|
541 |
} |
---|
542 |
|
---|
543 |
void |
---|
544 |
BeBoB::BootloaderManager::waitForBusReset() |
---|
545 |
{ |
---|
546 |
pthread_cond_wait( &m_cond, &m_mutex ); |
---|
547 |
} |
---|
548 |
|
---|
549 |
bool |
---|
550 |
BeBoB::BootloaderManager::writeRequest( CommandCodes& cmd ) |
---|
551 |
{ |
---|
552 |
unsigned char buf[ ( ( cmd.getMaxSize()+3 )/4 ) * 4 ]; |
---|
553 |
memset( buf, 0, sizeof( buf ) ); |
---|
554 |
|
---|
555 |
Util::BufferSerialize se( buf, sizeof( buf ) ); |
---|
556 |
if ( !cmd.serialize( se ) ) { |
---|
557 |
debugError( "writeRequest: Could not serialize command code %d\n", |
---|
558 |
cmd.getCommandCode() ); |
---|
559 |
return false; |
---|
560 |
} |
---|
561 |
|
---|
562 |
if ( !get1394Serivce()->write( |
---|
563 |
0xffc0 | getConfigRom()->getNodeId(), |
---|
564 |
AddrRegReq, |
---|
565 |
sizeof( buf )/4, |
---|
566 |
reinterpret_cast<fb_quadlet_t*>( buf ) ) ) |
---|
567 |
{ |
---|
568 |
debugError( "writeRequest: Could not ARM write to node %d\n", |
---|
569 |
getConfigRom()->getNodeId() ); |
---|
570 |
return false; |
---|
571 |
} |
---|
572 |
|
---|
573 |
return true; |
---|
574 |
} |
---|
575 |
|
---|
576 |
bool |
---|
577 |
BeBoB::BootloaderManager::readResponse( CommandCodes& writeRequestCmd ) |
---|
578 |
{ |
---|
579 |
const size_t buf_length = 0x40; |
---|
580 |
unsigned char raw[buf_length]; |
---|
581 |
if ( !get1394Serivce()->read( |
---|
582 |
0xffc0 | getConfigRom()->getNodeId(), |
---|
583 |
AddrRegResp, |
---|
584 |
writeRequestCmd.getRespSizeInQuadlets(), |
---|
585 |
reinterpret_cast<fb_quadlet_t*>( raw ) ) ) |
---|
586 |
{ |
---|
587 |
return false; |
---|
588 |
} |
---|
589 |
|
---|
590 |
Util::BufferDeserialize de( raw, buf_length ); |
---|
591 |
if ( !writeRequestCmd.deserialize( de ) ) { |
---|
592 |
debugError( "readResponse: deserialize failed\n" ); |
---|
593 |
return false; |
---|
594 |
} |
---|
595 |
|
---|
596 |
bool result = |
---|
597 |
writeRequestCmd.getProtocolVersion() |
---|
598 |
== writeRequestCmd.getRespProtocolVersion(); |
---|
599 |
result &= |
---|
600 |
writeRequestCmd.getCommandId() |
---|
601 |
== writeRequestCmd.getRespCommandId(); |
---|
602 |
result &= |
---|
603 |
writeRequestCmd.getCommandCode() |
---|
604 |
== writeRequestCmd.getRespCommandCode(); |
---|
605 |
#ifdef DEBUG |
---|
606 |
if ( !result ) { |
---|
607 |
debugError( "readResponse: protocol version: %d expected, " |
---|
608 |
" %d reported\n", |
---|
609 |
writeRequestCmd.getProtocolVersion(), |
---|
610 |
writeRequestCmd.getRespProtocolVersion() ); |
---|
611 |
debugError( "readResponse: command id: %d expected, " |
---|
612 |
" %d reported\n", |
---|
613 |
writeRequestCmd.getCommandId(), |
---|
614 |
writeRequestCmd.getRespCommandId() ); |
---|
615 |
debugError( "readResponse: command code: %d expected, " |
---|
616 |
" %d reported\n", |
---|
617 |
writeRequestCmd.getCommandCode(), |
---|
618 |
writeRequestCmd.getRespCommandCode() ); |
---|
619 |
} |
---|
620 |
#endif |
---|
621 |
return result; |
---|
622 |
} |
---|
623 |
|
---|
624 |
bool |
---|
625 |
BeBoB::BootloaderManager::startBootloaderCmd() |
---|
626 |
{ |
---|
627 |
CommandCodesReset cmd( m_protocolVersion, |
---|
628 |
CommandCodesReset::eSM_Bootloader ) ; |
---|
629 |
if ( !writeRequest( cmd ) ) { |
---|
630 |
debugError( "startBootloaderCmd: writeRequest failed\n" ); |
---|
631 |
return false; |
---|
632 |
} |
---|
633 |
|
---|
634 |
waitForBusReset(); |
---|
635 |
if ( !cacheInfoRegisters( MaxRetries ) ) { |
---|
636 |
debugError( "startBootloaderCmd: Could not read info registers\n" ); |
---|
637 |
return false; |
---|
638 |
} |
---|
639 |
|
---|
640 |
// wait for bootloader finish startup sequence |
---|
641 |
// there is no way to find out when it has finished |
---|
642 |
sleep( 10 ); |
---|
643 |
|
---|
644 |
return true; |
---|
645 |
} |
---|
646 |
|
---|
647 |
bool |
---|
648 |
BeBoB::BootloaderManager::startApplicationCmd() |
---|
649 |
{ |
---|
650 |
CommandCodesGo cmd( m_protocolVersion, |
---|
651 |
CommandCodesGo::eSM_Application ) ; |
---|
652 |
if ( !writeRequest( cmd ) ) { |
---|
653 |
debugError( "startApplicationCmd: writeRequest failed\n" ); |
---|
654 |
return false; |
---|
655 |
} |
---|
656 |
|
---|
657 |
return true; |
---|
658 |
} |
---|
659 |
|
---|
660 |
bool |
---|
661 |
BeBoB::BootloaderManager::programGUIDCmd( fb_octlet_t guid ) |
---|
662 |
{ |
---|
663 |
CommandCodesProgramGUID cmd( m_protocolVersion, guid ); |
---|
664 |
if ( !writeRequest( cmd ) ) { |
---|
665 |
debugError( "programGUIDCmd: writeRequest failed\n" ); |
---|
666 |
return false; |
---|
667 |
} |
---|
668 |
|
---|
669 |
sleep( 1 ); |
---|
670 |
|
---|
671 |
return true; |
---|
672 |
} |
---|
673 |
|
---|
674 |
bool |
---|
675 |
BeBoB::BootloaderManager::initializePersParamCmd() |
---|
676 |
{ |
---|
677 |
CommandCodesInitializePersParam cmd( m_protocolVersion ); |
---|
678 |
if ( !writeRequest( cmd ) ) { |
---|
679 |
debugError( "initializePersParamCmd: writeRequest failed\n" ); |
---|
680 |
return false; |
---|
681 |
} |
---|
682 |
|
---|
683 |
sleep( 1 ); |
---|
684 |
|
---|
685 |
return true; |
---|
686 |
} |
---|
687 |
|
---|
688 |
bool |
---|
689 |
BeBoB::BootloaderManager::initializeConfigToFactorySettingCmd() |
---|
690 |
{ |
---|
691 |
CommandCodesInitializeConfigToFactorySetting cmd( m_protocolVersion ); |
---|
692 |
if ( !writeRequest( cmd ) ) { |
---|
693 |
debugError( "initializeConfigToFactorySettingCmd: writeRequest failed\n" ); |
---|
694 |
return false; |
---|
695 |
} |
---|
696 |
|
---|
697 |
sleep( 5 ); |
---|
698 |
|
---|
699 |
return true; |
---|
700 |
} |
---|
701 |
|
---|
702 |
bool |
---|
703 |
BeBoB::BootloaderManager::checkDeviceCompatibility( BCD& bcd ) |
---|
704 |
{ |
---|
705 |
fb_quadlet_t vendorOUI = ( m_cachedInfoRegs.m_guid >> 40 ); |
---|
706 |
|
---|
707 |
|
---|
708 |
if ( ( vendorOUI == bcd.getVendorOUI() ) |
---|
709 |
&& ( m_cachedInfoRegs.m_softwareId == bcd.getSoftwareId() ) ) |
---|
710 |
{ |
---|
711 |
return true; |
---|
712 |
} |
---|
713 |
|
---|
714 |
printf( "vendorOUI = 0x%08x\n", vendorOUI ); |
---|
715 |
printf( "BCD vendorOUI = 0x%08x\n", bcd.getVendorOUI() ); |
---|
716 |
printf( "software ID = 0x%08x\n", m_cachedInfoRegs.m_softwareId ); |
---|
717 |
printf( "BCD software ID = 0x%08x\n", bcd.getSoftwareId() ); |
---|
718 |
|
---|
719 |
return false; |
---|
720 |
} |
---|