⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediatek.cpp

📁 DVD
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//---------------------------------------------------------------------------

ulong TMediatek::GetTargetSize(uchar bTargetMem)
{
    if((bTargetMem == TARGET_NULL)||(bTargetMem >= TARGET_END))
        return 0;

    return (MemCommand[bTargetMem].MemorySize);
}

//---------------------------------------------------------------------------
// DUMP MEMORY
//---------------------------------------------------------------------------

int TMediatek::ReadMemory(ulong ulAddress, ulong &ulLength, uchar bTargetMem, uchar **ptrBuffer)
{
    uchar *sTmpBuffer; //[MTK_READ_BUFFER];

    int   iRet;
    int   iRetry;
    ulong ulTargetLength;
    ulong ulBytesRead;
    ulong ulDumpCount;

    try
    {
      InitializeCriticalSection(&cs);
      EnterCriticalSection( &cs );
        // Set ulLength to 0 for return value
        ulTargetLength = ulLength;
        ulLength = 0;
        *ptrBuffer = NULL;
        ElapsedTime = 0;
        StartTime = clock();
      LeaveCriticalSection( &cs );

        // Check input values
        iRet = CheckTarget(ulAddress, ulTargetLength, bTargetMem);
        if(iRet != MEDIATEK_RET_TRUE)
        {
            throw iRet;
        }

        // Allocate memory for file
        *ptrBuffer = (uchar *) malloc(ulTargetLength);
        if (*ptrBuffer == NULL)
        {
            throw MEDIATEK_ERROR_ALLOCATE;
        }

        // Open port with Dump Mode
        iRet = ComOpen(MODE_MEMORY);
        if(iRet != MEDIATEK_RET_TRUE)
        {
            throw iRet;
        }

        //Print->Main("- MTK COM Opened");

        // Main retry (MtkCom restart)
        iRetry = (int) ComPort->bRetry;

        // Read data loop
        ulBytesRead=0;
        while(ulBytesRead < ulTargetLength)
        {
            // TODO - If les than 4 bytes then read with some 1 byte command

            // Try read those bytes
            if(PriReadMemory(ulAddress+ulBytesRead, bTargetMem, &sTmpBuffer) == true)
            {
                ulDumpCount = (ulong) sTmpBuffer[0];

                // Copy data to output buffer
                memcpy(*ptrBuffer+ulBytesRead, sTmpBuffer+1, ulDumpCount);

                // Set retry back after succesfull read
                iRetry = (int) ComPort->bRetry;

                // Increment read counter
                ulBytesRead += ulDumpCount;
                if(ulBytesRead > ulTargetLength)
                    ulBytesRead = ulTargetLength;

              EnterCriticalSection( &cs );
                ulLength = ulBytesRead;
                Progress = (double) ulBytesRead / (double) ulTargetLength;
              LeaveCriticalSection( &cs );
            }
            else
            {
                // No more retry
                if(iRetry-- <= 0)
                    break;

                //Print->Int("- RETRY.. ", iRetry);

                // Try restart MtkCom
                ComReset();
            }

          EnterCriticalSection( &cs );
            // Set process time
            ElapsedTime = clock() - StartTime;
          LeaveCriticalSection( &cs );
        }

        if(ulBytesRead == ulTargetLength)
        {
            iRet = MEDIATEK_RET_TRUE;
        }
        else
        {
            iRet = MEDIATEK_RET_FALSE;
        }
    }
    catch(int E)
    {
        return ErrorMessage(E);
    }
    catch(...)
    {
        return ErrorMessage(MEDIATEK_RET_ERROR);
    }

    return iRet;
}

//---------------------------------------------------------------------------
// BACKUP FLASH
//---------------------------------------------------------------------------

int TMediatek::ReadFlash(ulong ulAddress, ulong &ulLength, uchar **ptrBuffer)
{
    uchar *sTmpBuffer;

    bool  ReadRet;

    int   iRet;
    int   iRetry;
    uchar bCount;
    ulong ulReadSize;
    ulong ulDumpCount;

    try
    {
        // Set ulLength to 0 for return value
        ulReadSize = ulLength;
        ulLength = 0;
        *ptrBuffer = NULL;
        ElapsedTime = 0;
        StartTime = clock();

        // Check input values

        // TODO : Need to call Flash Chip functions
        if(0)
        {
            throw MEDIATEK_RET_ERROR;
        }

        // Allocate memory for file
        *ptrBuffer = (uchar *) malloc(ulReadSize);
        if (*ptrBuffer == NULL)
        {
            throw MEDIATEK_ERROR_ALLOCATE;
        }

        // Open port with Dump Mode
        iRet = ComOpen(MODE_BACKUP);
        if(iRet != MEDIATEK_RET_TRUE)
        {
            throw iRet;
        }

        //Print->Main("- MTK COM Opened");

        // Main retry (MtkCom restart)
        iRetry = (int) ComPort->bRetry;

        // Read data loop
        for(ulLength=0; ulLength < ulReadSize; ulLength += ulDumpCount)
        {
            // Set read elngth
            ulDumpCount = ulReadSize - ulLength;

            if(ulDumpCount > 255)
                ulDumpCount = 255;

            bCount = (uchar) ulDumpCount;

            // Try read those bytes
            ReadRet = PriReadFlash(ulAddress+ulLength, bCount, &sTmpBuffer);

            if(ReadRet == true)
            {
                // Readed bytes Should be correct already
                //ulDumpCount = (ulong) bCount;

                // Set retry back after succesfull read
                iRetry = (int) ComPort->bRetry;
            }
            else
            {
                // Readed bytes
                ulDumpCount = (ulong) bCount;
            }

            if(ulDumpCount > 0)
            {
                Progress = (double) (ulLength + ulDumpCount) / ulReadSize;

                // Copy data to output buffer
                memcpy(*ptrBuffer+ulLength, sTmpBuffer, ulDumpCount);
            }
            else
            {
                if(iRetry-- <= 0)
                {
                    break;
                }
                //Print->Int("- RETRY.. ", iRetry);

                // Try restart MtkCom
                ComReset();
            }

            ElapsedTime = clock() - StartTime;
        }

        if(ulLength >= ulReadSize)
        {
            ulLength = ulReadSize;
            iRet = MEDIATEK_RET_TRUE;
        }
        else
        {
            iRet = MEDIATEK_RET_FALSE;
        }
    }
    catch(int E)
    {
        return ErrorMessage(E);
    }
    catch(...)
    {
        return ErrorMessage(MEDIATEK_RET_ERROR);
    }

    return iRet;
}

// **********************************************************************
//  PRIVATE FUNCTIONS
// **********************************************************************

//---------------------------------------------------------------------------
// ERROR MESSAGES
//---------------------------------------------------------------------------

int TMediatek::ErrorMessage(int iMsg)
{
    if(iMsg == MEDIATEK_RET_TRUE)
        return MEDIATEK_RET_TRUE;

    if(ErrorPrint == false)
    {
    	switch(iMsg)
	    {
		    case MEDIATEK_ERROR_COM_IO :
                Print->ErrorMsg("No answer from player ! Check that DVD\nplayer is ON and Data cable connected !", "No Connection");
    			break;

	    	case MEDIATEK_ERROR_COM_OPEN :
                Print->ErrorMsg("Failed to open selected COM port", "COM Port");
			    break;
        }

        // Return error
        return MEDIATEK_RET_ERROR;
    }

	switch(iMsg)
	{
        // 2000 - Common
		case MEDIATEK_RET_ERROR :
            Print->Error("Unknow error on Mediatek functions", 2000);
            break;

		case MEDIATEK_ERROR_ALLOCATE :
            Print->Error("Failed to allocate memory", 2001);
            break;

        // 2010 - Com port
		case MEDIATEK_ERROR_COM :
            Print->Error("Failed to execute COM action", 2010);
			break;

		case MEDIATEK_ERROR_COM_OPEN :
            Print->Error("Failed to open COM port", 2011);
			break;

		case MEDIATEK_ERROR_COM_CLOSE :
            Print->Error("Failed to close COM port", 2012);
			break;

		case MEDIATEK_ERROR_COM_SEND :
            Print->Error("Failed to send data to COM port", 2013);
			break;

		case MEDIATEK_ERROR_COM_READ :
            Print->Error("Failed to read data from COM port", 2014);
			break;

		case MEDIATEK_ERROR_COM_IO :
            Print->Error("No answer from player ! Check player and cable !", 2015);
			break;

        // 2020 - Memory dump
        case MEDIATEK_ERROR_DUMP :
            Print->Error("Unknow error while dumping memory", 2020);
            break;

        case MEDIATEK_ERROR_DUMP_ADDRESS :
            Print->Error("Address is higher than Memory size", 2021);
            break;

        case MEDIATEK_ERROR_DUMP_LENGTH :
            Print->Error("Read length is more than Memory size", 2022);
            break;

        case MEDIATEK_ERROR_DUMP_SIZE :
            Print->Error("Read address+length goes over Memory size", 2023);
            break;

		default:
			break;
	}

    // return Error
    return MEDIATEK_RET_ERROR;
}

//---------------------------------------------------------------------------
// TEST IF PLAYER ANSWERS THRUE COM
//---------------------------------------------------------------------------

int TMediatek::TestCom(void)
{
    // Test Communication with player
    char  sData[10];
    uchar bCount;
    uchar bTarget;
    int   iRet;
    uword usCount;

    // Send IDATA read request
    bTarget = TARGET_IDATA;
    bCount = PriMemoryCmdParser(bTarget, 0, sData);

    if(ComPort->Send->Data(sData, bCount) == false)
        return MEDIATEK_ERROR_COM_IO;

    // Get read count
    bCount = MemCommand[bTarget].DumpBytesRead;

    // Set temp / return count
    usCount = bCount+1;

    // Try read those bytes
    if(ComPort->Read->Read(usCount, sData) == true)
    {
        // Count mich match -> Wrong return data
        if(usCount != (uword)(bCount+1))
            return MEDIATEK_ERROR_COM_IO;

        if(bCount != sData[0])
            return MEDIATEK_ERROR_COM_IO;
    }
    else
    {
        // If read fails return false
        return MEDIATEK_ERROR_COM_IO;
    }

    // Test succeed
    return MEDIATEK_RET_TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -