| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 | //	Copyright (C) 2014 Michael McMaster <michael@codesrc.com>////	This file is part of SCSI2SD.////	SCSI2SD 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 3 of the License, or//	(at your option) any later version.////	SCSI2SD 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 SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.#include "SCSI2SD_Bootloader.hh"#include <iostream>#include <sstream>#include <stdexcept>#include <string.h>using namespace SCSI2SD;hid_device* SCSI2SDHID_handle = NULL;// cybtldr interface.extern "C" intSCSI2SDHID_OpenConnection(void){	return 0;}extern "C" intSCSI2SDHID_CloseConnection(void){	return 0;}extern "C" intSCSI2SDHID_ReadData(unsigned char* data, int count){	uint8_t buf[65];	buf[0] = 0; // Report ID	int result = hid_read(SCSI2SDHID_handle, buf, count);	if (result < 0)	{		std::cerr << "USB HID Read Failure: " <<			hid_error(SCSI2SDHID_handle) << std::endl;	}	memcpy(data, buf, count);	return (result >= 0) ? 0 : -1;}extern "C" intSCSI2SDHID_WriteData(unsigned char* data, int count){	uint8_t buf[65];	buf[0] = 0; // report ID	int i;	for (i = 0; i < count; ++i)	{		buf[i+1] = data[i];	}	int result = -1;	for (int retry = 0; retry < 3 && result < 0; ++retry)	{		result = hid_write(SCSI2SDHID_handle, buf, count + 1);	}	if (result < 0)	{		std::cerr << "USB HID Write Failure: " <<			hid_error(SCSI2SDHID_handle) << std::endl;	}	return (result >= 0) ? 0 : -1;}static CyBtldr_CommunicationsData g_cyComms ={	&SCSI2SDHID_OpenConnection,	&SCSI2SDHID_CloseConnection,	&SCSI2SDHID_ReadData,	&SCSI2SDHID_WriteData,	Bootloader::HID_PACKET_SIZE};Bootloader::OperationScope::OperationScope(){	unsigned long blVer;	if (CyBtldr_StartBootloadOperation(&g_cyComms, 0x2e133069, 0, &blVer)		!= CYRET_SUCCESS)	{		throw std::runtime_error("Could not start bootloader operation");	}}Bootloader::OperationScope::~OperationScope(){	CyBtldr_EndBootloadOperation();}Bootloader::Bootloader(hid_device_info* hidInfo) :	myHidInfo(hidInfo),	myBootloaderHandle(NULL){	myBootloaderHandle = hid_open_path(hidInfo->path);	if (!myBootloaderHandle)	{		std::stringstream msg;		msg << "Error opening HID device " << hidInfo->path << std::endl;		hid_free_enumeration(myHidInfo);		myHidInfo = NULL;		throw std::runtime_error(msg.str());	}	else	{		SCSI2SDHID_handle = myBootloaderHandle;	}}Bootloader::~Bootloader(){	if (myBootloaderHandle)	{		hid_close(myBootloaderHandle);	}	SCSI2SDHID_handle = NULL;	hid_free_enumeration(myHidInfo);}Bootloader*Bootloader::Open(){	hid_device_info* dev = hid_enumerate(VENDOR_ID, PRODUCT_ID);	if (dev)	{		return new Bootloader(dev);	}	else	{		return NULL;	}}Bootloader::HWInfoBootloader::getHWInfo() const{	HWInfo info = {"unknown", "unknown", "unknown"};	switch (myHidInfo->release_number)	{	case 0x3001:		info.desc = "3.5\" SCSI2SD (green)";		info.version = "V3.0";		info.firmwareName = "SCSI2SD-V3.cyacd";		break;	case 0x3002:		info.desc = "3.5\" SCSI2SD (yellow/red) or 2.5\" SCSI2SD for Apple Powerbook";		info.version = "V4.1/V4.2/V5.0";		info.firmwareName = "SCSI2SD-V4.cyacd";		break;	}	return info;}boolBootloader::isCorrectFirmware(const std::string& path) const{	HWInfo info = getHWInfo();	return path.rfind(info.firmwareName) != std::string::npos;}std::stringBootloader::getDevicePath() const{	return myHidInfo->path;}std::wstringBootloader::getManufacturer() const{	return myHidInfo->manufacturer_string;}std::wstringBootloader::getProductString() const{	return myHidInfo->product_string;}voidBootloader::load(const std::string& path, void (*progress)(uint8_t, uint16_t)){	int result = CyBtldr_Program(		path.c_str(),		&g_cyComms,		progress);	if (result)	{		throw std::runtime_error("Firmware update failed");	}}boolBootloader::ping() const{	try	{		Bootloader::OperationScope operationGuard;		int result = CyBtldr_VerifyRow(0, 0, 0);		switch (result)		{		case CYRET_SUCCESS:		case CYRET_ERR_CHECKSUM: // We supplied a dummy value of 0.			return true;		default: return false;		}	}	catch (std::exception& e)	{		return false;	}}
 |