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

📄 mediatek.cpp

📁 DVD
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//---------------------------------------------------------------------------
// CHECK DUMP ADDRES AND LENGTH PARAMETERS
//---------------------------------------------------------------------------

int TMediatek::CheckTarget(ulong ulAddress, ulong ulLength, uchar bTargetMem)
{
    ulong ulMemSize;

    // Check target
    ulMemSize = GetTargetSize(bTargetMem);

    if(ulAddress > ulMemSize)
    {
        return MEDIATEK_ERROR_DUMP_ADDRESS;
    }
    else if(ulLength > ulMemSize)
    {
        return MEDIATEK_ERROR_DUMP_LENGTH;
    }
    else if((ulAddress + ulLength) > ulMemSize)
    {
        return MEDIATEK_ERROR_DUMP_SIZE;
    }

    return MEDIATEK_RET_TRUE;
}

//---------------------------------------------------------------------------
// PARSE MEMORY DUMP COMMAND
//---------------------------------------------------------------------------

uchar TMediatek::PriMemoryCmdParser(uchar bTargetMem, ulong ulAddress, uchar *OutBuf)
{
    uchar bCommand;
    uchar bCount;
    uchar i;

    if((bTargetMem == TARGET_NULL)||(bTargetMem >= TARGET_END))
        return 0;

    // Get read cmd
    bCommand = MemCommand[bTargetMem].DumpCommand;
    bCount = MemCommand[bTargetMem].DumpBytesSend;

    if(bCount > 4)
        return 0;

    OutBuf[0] = DUMP_PARSER;
    OutBuf[1] = bCommand;

    for(i=0; i < bCount; i++)
    {
        OutBuf[(i*2)+2] = DUMP_PARSER;
        OutBuf[(i*2)+2+1] = (ulAddress >> ((bCount - i - 1) * 8)) & 0xFF;
    }

    return ((bCount+1) * 2);
}

//---------------------------------------------------------------------------
// COMMON COMMANDS
//---------------------------------------------------------------------------

int TMediatek::Common(uchar bCommand)
{
    ulong ulSendCount;
    // Most of these commands are not found from 8032 rs232 log function.
    // So I think that these are Bootloader specific commands

    switch (bCommand)
    {
        case SEND_BOOT :
        {
            // 2 bytes : 00 (13 or 10)
            ComPort->Send->Command(0x00, BootMode);
            break;
        }

        case SEND_INIT_END :
        {
            // 2A 0B (seems odd maybe something else)
            ComPort->Send->Command(0x2A, 0x0B);
            break;
        }

        case SEND_INIT_TOP :
        {
            // Chip align top : 20 02
            ComPort->Send->Command(MTK_ALIGN, 0x02);
            break;
        }

        case SEND_INIT_BOTTOM :
        {
            // Chip align bottom : 20 06
            ComPort->Send->Command(MTK_ALIGN, 0x06);
            break;
        }

        case SEND_START :
        {
            // Send after init byte : 01 00
            ComPort->Send->Command(0x01, 0x00);
            break;
        }

        case SEND_STOP :
        {
            // Send before reset : 02 00
            ComPort->Send->Command(0x02, 0x00);
            break;
        }

        case SEND_LOG_ON :
        {
            // Long ON
            // - 4 bytes : 50 FF 50 F1 50 F1 50 F1
            //   byte[0] = FF
            //   (byte[1] | byte[2] | byte[3]) = F1

            ComPort->Send->Command(DUMP_PARSER, MTK_LOG);
            ComPort->Send->Command(DUMP_PARSER, LOG_ON);
            ComPort->Send->Command(DUMP_PARSER, LOG_ON);
            ComPort->Send->Command(DUMP_PARSER, LOG_ON);
            break;
        }

        case SEND_LOG_OFF :
        {
            // Long OFF
            // - 4 bytes : 50 FF 50 F0 50 F0 50 F0
            //   byte[0] = FF
            //   (byte[1] | byte[2] | byte[3]) = F0

            ComPort->Send->Command(DUMP_PARSER, MTK_LOG);
            ComPort->Send->Command(DUMP_PARSER, LOG_OFF);
            ComPort->Send->Command(DUMP_PARSER, LOG_OFF);
            ComPort->Send->Command(DUMP_PARSER, LOG_OFF);
            break;
        }

        case SEND_NOP :
        {
            // 00 00 00 00
            ComPort->Send->Command(0x00, 0x00);
            ComPort->Send->Command(0x00, 0x00);
            break;
        }

        case SEND_TEST :
        {
            // Send IDATA request for com test
            // 50 03 50 00
            ComPort->Send->Command(DUMP_PARSER, READ_IDATA);
            ComPort->Send->Command(DUMP_PARSER, 0x00);
            break;
        }
    }
	return COM_ID_SUCCESS;
}

//---------------------------------------------------------------------------
// SEND COMMAND TO CHIP
//---------------------------------------------------------------------------

bool TMediatek::SendCommand(uchar bAddress, uchar bCommand)
{
    // Send command address
    ComPort->Send->Command(0x21, bAddress);
    ComPort->Send->Command(0x22, bAddress);
    ComPort->Send->Command(0x23, 0x00);

    // Send command
    ComPort->Send->Command(0x28, bCommand);

    return true;
}

//---------------------------------------------------------------------------
// VERIFY CHIP
//---------------------------------------------------------------------------

bool TMediatek::Verify(ulong ulAddress, uchar bData)
{
    ulong bCarry;
    ulong ulSendCount;
    uchar cData[2];

//??    if(Direct == 0)
    {
        if(ulAddress & 0x01)
        {
            bCarry = 0x80000;
        }

        ulAddress = (ulAddress > 1) | bCarry;
    }

    // Send data address
    ComPort->Send->Command(0x21, (ulAddress >> 0)  & 0xFF);
    ComPort->Send->Command(0x22, (ulAddress >> 8)  & 0xFF);
    ComPort->Send->Command(0x23, (ulAddress >> 16) & 0xFF);

    // Send data
    ComPort->Send->Command(0x29, bData);

    return true;
}

//---------------------------------------------------------------------------
// SEND COMMAND TO CHIP
//---------------------------------------------------------------------------

int TMediatek::PriReadFlash(ulong ulAddress, uchar &bCount, uchar **prtBuf)
{
    bool Ret;

    uchar bReadSize;
    uchar sTempBuf[0xFF];
    uword usCount;

    // No retry here
    // - Fast retry in com and other should be in main function
    //   with COM Port reset etc
    //uchar bRetry;

    *prtBuf = NULL;
    bReadSize = bCount;
    bCount = 0;

/*
    ulong bCarry;
	bCarry = 0;

//??    if(Bottom == 0)
	if(0)
    {
        if(ulAddress & 0x01)
        {
            bCarry = 0x80000;
        }

        ulAddress = (ulAddress > 1) | bCarry;
    }
*/

    // Send data address
    ComPort->Send->Command(0x21, (ulAddress >> 0)  & 0xFF);
    ComPort->Send->Command(0x22, (ulAddress >> 8)  & 0xFF);
    ComPort->Send->Command(0x23, (ulAddress >> 16) & 0xFF);

    // Send data
    ComPort->Send->Command(0x25, bReadSize);

    // Set data to send
    ComPort->Send->Byte(0x24 | 0x80); // Wait answer

    // Set temp / return count
    usCount = (uword) bReadSize;

    // Try read those bytes (Max 0xFF -> 255 bytes)
    Ret = ComPort->Read->Read(usCount, sTempBuf);

    if(Ret == false)
    {
        // If read fails return false
        return false;
    }

    if(usCount != (uword)(bReadSize))
    {
        // Set return values even if read fails
        *prtBuf = sTempBuf;
        bCount = (uchar) usCount;

        // Read fails
        return false;
    }

    *prtBuf = sTempBuf;
    bCount = (uchar) usCount;

    return true;
}

//---------------------------------------------------------------------------
// SEND COMMAND TO CHIP
//---------------------------------------------------------------------------

bool TMediatek::PriReadFlashByte(ulong ulAddress, uchar &bData)
{
	bool Ret;

/*
	ulong bCarry;
	bCarry = 0;

//??    if(Bottom == 0)
	if(0)
	{
		if(ulAddress & 0x01)
		{
			bCarry = 0x80000;
		}

		ulAddress = (ulAddress > 1) | bCarry;
	}
*/

	// Send data address
	ComPort->Send->Command(0x21, (ulAddress >> 0)  & 0xFF);
	ComPort->Send->Command(0x22, (ulAddress >> 8)  & 0xFF);
	ComPort->Send->Command(0x23, (ulAddress >> 16) & 0xFF);

	// Send data
	ComPort->Send->Command(0x25, 0x00);

	// Set data to send
	ComPort->Send->Byte(0x24 | 0x80); // Wait answer

    // Wait answer from ComRead functions
    Ret = ComPort->Read->Byte(bData);

    return Ret;
}

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

bool TMediatek::PriReadMemory(ulong ulAddress, uchar bTargetMem, uchar **prtBuf)
{
    bool Ret;

    uchar bCount;
    uchar sTempBuf[20];

    uword usCount;

    // No retry here
    // - Fast retry in com and other should be in main function
    //   with COM Port reset etc
    //uchar bRetry;


    *prtBuf = NULL;

    bCount = PriMemoryCmdParser(bTargetMem, ulAddress, sTempBuf);

    if(bCount == 0)
    {
        return false;
    }

    ComPort->Send->Data(sTempBuf, bCount);

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

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

    // Try read those bytes
    Ret = ComPort->Read->Read(usCount, sTempBuf);

    if(Ret == true)
    {
        if(usCount != (uword)(bCount+1))
            return false;

        // Count mich match -> Wrong return data
        if(bCount != sTempBuf[0])
            return false;
    }
    else
    {
        // If read fails return false
        return false;
    }

    *prtBuf = sTempBuf;

    return true;
}

//---------------------------------------------------------------------------

⌨️ 快捷键说明

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