download.c

来自「WinCE 3.0 BSP, 包含Inter SA1110, Intel_815」· C语言 代码 · 共 775 行 · 第 1/2 页

C
775
字号
				OEMFlashWrite(Vaddr+i, *(unsigned long *)(temp_buffer+i));
#endif ((SH_PLATFORM == PLATFORM_ASPEN)||(SH_PLATFORM==PLATFORM_BIGSUR))
			}
										// Read data into destination directly
			ulCheckSum=0;
			// Verify the checksum of image stored in FLASH.
			// pCheckSum= (unsigned char *)((unsigned)temp_buffer | FLASH_START);
			pCheckSum= (unsigned char *)((unsigned)temp_buffer);
			for (ulCheckOff=0; ulCheckOff<cbRecord; ulCheckOff++)
				ulCheckSum+= *pCheckSum++;
			if (ulCheckSum != checksum) {
				OutputFormatString("Address %Xh Length %Xh checksum %Xh, my_checksum=0x%x ",Vaddr,cbRecord,checksum, ulCheckSum);
				OutputFormatString("unread=%d ",result);
				OutputFormatString("Checksum error.\r\n");
				return DL_CHECKSUM_ERROR;
			}
		}
	} // while 1
#if ((SH_PLATFORM == PLATFORM_ASPEN)||(SH_PLATFORM==PLATFORM_BIGSUR))
	OEMFlashWriteEnd();
#endif ((SH_PLATFORM == PLATFORM_ASPEN)||(SH_PLATFORM==PLATFORM_BIGSUR))
	return DL_SUCCESS;
}

#else ((SH_PLATFORM == PLATFORM_ASPEN) || (SH_PLATFORM == PLATFORM_S1)||(SH_PLATFORM==PLATFORM_BIGSUR))

//***************************************************************************
//      Download2Flash - download image to flash
//
//
//***************************************************************************
unsigned int Download2Flash(void)
{
    unsigned int    result;
    unsigned int    ulCheckSum;                     // computed byte checksum
    union {
        struct {
            unsigned int    Vaddr;          // record starting address
            unsigned int    cbRecord;       // record length
            unsigned int    checksum;       // record checksum
        } fields;
        unsigned char   cHeader[12];
    } header;
    unsigned int    iHeaderBytes=0;         // header buffer counter
    union {                                                         // dword record buffer
        unsigned int lData;
        unsigned char cData[4];
    } record;
    unsigned int    iDataBytes=0;           // dword buffer counter
    unsigned int    iDone=0;                        // download finished flag
    unsigned int    iTimeout=0;
    unsigned int    bStalled=0;

    OutputFormatString("ulCheckSum address=%Xh\r\n",&ulCheckSum);
    InitRingBuffer();                                       // initialize ring buffer
    do {                                                    // if there is room in ring buffer
                                                            // and data ready on parallel port
        if (!RingBufferFull()) {
            if (bStalled) OutputString(">");
            bStalled=FALSE;
            if ((result=OEMParallelPortGetStatus())!=-1)    // insert in ring buffer
                InsertRingBuffer((unsigned char)result&0xff);
        } else {
            if (!bStalled) OutputString("<");
            bStalled=TRUE;
        }
                                                            // if we have a word and the flash is ready
        if (iHeaderBytes == 12 && iDataBytes == 2) {
            if (OEMFlashWriteStatus(header.fields.Vaddr)) {
                                                            // write it out
                if (OEMFlashWrite(header.fields.Vaddr, record.lData))
                    return DL_FLASH_ERROR;
                header.fields.Vaddr+=2;                     // bump address to next word
                iDataBytes=0;                               // clear byte counter
                                                            // if no more bytes in record
                if (!header.fields.cbRecord) {
                    iHeaderBytes=0;                         // clear header counter
                                                            // compare checksum
                    if (ulCheckSum != header.fields.checksum) {
                        OutputFormatString("\r\nChecksum error: expected %Xh actual=%Xh \r\n",header.fields.checksum,ulCheckSum);
                        OutputFormatString("RingBuffer=%Xh LastByte=%Xh Currbyte=%Xh LastAvailByte=%Xh\r\n",
                                                    RingBuffer,LastByte,CurrByte,LastAvailByte);
                        return DL_CHECKSUM_ERROR;
                    }
                }
            }
        } else if (RingBufferHasData()) {
            if (iHeaderBytes < 12) {                        // if we haven't gotten all 12 bytes of the header
                header.cHeader[iHeaderBytes++]=RemoveRingBuffer();  // get the next byte of the header
                if (iHeaderBytes == 12) {                   // if we now have all 12
                    if (!header.fields.Vaddr) {
                        iDone=1;
                        ulLaunch= header.fields.cbRecord;   // save launch address
                    }
                    ulCheckSum=0;                           // clear the checksum
                    WriteDbgOutput(DEB_PPSH, ("\r\nAddr %Xh Len %Xh\r\n",header.fields.Vaddr,header.fields.cbRecord));
                }
            } else if (header.fields.cbRecord && iDataBytes<2) {    // wait until we have 2 bytes before writing to flash
                record.cData[iDataBytes]=RemoveRingBuffer();
                header.fields.cbRecord--;
                ulCheckSum+= record.cData[iDataBytes++];
            }
        }
    } while (!iDone);
    OEMFlashWriteEnd();
    return DL_SUCCESS;
}
#endif ((SH_PLATFORM == PLATFORM_ASPEN) || (SH_PLATFORM == PLATFORM_S1)||(SH_PLATFORM==PLATFORM_BIGSUR))



//***************************************************************************
//      Download2RAM - download image to RAM
//
//
//***************************************************************************
unsigned int Download2RAM(void)
{
    unsigned int    Vaddr;                          // record starting address
    unsigned int    cbRecord;                       // record length
    unsigned int    checksum;                       // record checksu,
    unsigned int    ulCheckSum;                     // computed checksum
    unsigned int    ulCheckOff;                     // checksum index
    unsigned char   *pCheckSum;                     // checksum pointer
    unsigned int    result;
    unsigned int    ParallelRecvBuffer[3];          // record header buffer

    while (1) {
        // Read in the starting address and length in bytes
        ParallelPortRead_debug((unsigned char *)ParallelRecvBuffer, 3 * sizeof(unsigned int ));
        WRITE_REGISTER_ULONG(ALPHA_LED,0xECEEBEEF);
        Vaddr = ParallelRecvBuffer[0];
        cbRecord = ParallelRecvBuffer[1];
        checksum = ParallelRecvBuffer[2];
		WriteDbgOutput(DEB_PPSH, ("Vaddr:%xh cbRecord:%xh checksum:%xh\n\r", Vaddr, cbRecord, checksum));	//[NAR]

		OutputFormatString("Vaddr:%xh cbRecord:%xh checksum:%xh\n\r", Vaddr, cbRecord, checksum);	//[NAR]

        if (Vaddr == 0L) {                              // if this is the last record, launch it
            OutputFormatString("Done\r\nJumping to %xh instruction %xh", cbRecord, *(unsigned int*)cbRecord);
            Launch(cbRecord);
        }
        // otherwise copy directly to RAM
        if (cbRecord) {                                 // otherwise copy directly to RAM
            result= ParallelPortRead((unsigned char *)Vaddr, cbRecord);
                                                                                // Read data into destination directly
            ulCheckSum=0;
            pCheckSum= (unsigned char *)Vaddr;
            for (ulCheckOff=0; ulCheckOff<cbRecord; ulCheckOff++) ulCheckSum+= *(volatile unsigned char *)pCheckSum++;
            if (ulCheckSum != checksum) {
                OutputFormatString("Address %Xh Length %Xh checksum %Xh unread=%d ulCheckSum=%Xh Checksum error.\r\n",Vaddr,cbRecord,checksum,result,ulCheckSum);
                return DL_CHECKSUM_ERROR;
            } else{	//[NAR]
                WriteDbgOutput(DEB_PPSH, ("Address %Xh Length %Xh checksum %Xh OK!\n\r",Vaddr,cbRecord,checksum));
			}			
        }
    } // while 1
    return DL_SUCCESS;
}



/*****************************************************************************
*
*
*   @func   int |   DownloadImage | Down loads image from host
*
*   @rdesc  Returns 1 if sucessfully downloaded, 0 otherwise
*
*   @parm   const unsigned char * |   pPathName  |
*                          points to a ASCIIZ string for a file on the host file
*                          system to be downloaded.  The file must be in the format
*                          of SRE or BIN (preprocessed SRE file).
*
*   @parm   unsigned int * |   pStartingAddr    |
*                          points to a buffer to receive the starting address of
*                          the program.
*
*   @parm   int | fLaunch |
*                          Flag indicating if the downloaded image should be
*                          automatically launched or not.
*
*   @comm
*                  Initially sends a packet to host (running ppfs) to initiate a
*                  SRE or binary image  file transfer.
*
*/
int DownloadImage(const unsigned char * pPathName,unsigned int * pStartingAddr,int fLaunch)
{
    const unsigned char * pTemp;
    unsigned char   ParallelRecvBuffer[512];
    unsigned int    chksum;
    unsigned int    uiTemp;
    int                      bootType;
    unsigned                len;
    unsigned char * pDestByte;
    int                      fSre=0;
    int                      nReturn=0;
    int                      j;
    int                      i;

    OutputString("Ready to down load SRE/BIN file");

#if BIDI
    for (j = 0; j < 100; j++) {
        OutputString(".");
        //
        // Prepare boot packet
        //
        pDestByte = ParallelRecvBuffer;
        for (i = 0; i < BOOT_HEADER_SIZE; i++) {
            *pDestByte++ = BootHeader[i];
        }

        chksum = 0;
        len = sizeof(unsigned int) + 5;

        if (pPathName) {
            bootType = 1;              // The NULL byte
            for (pTemp = pPathName; *pTemp; pTemp++) bootType++;
            len += bootType;
        } else {
            bootType = BOOT_TYPE;
        }

        uiTemp = len;
        for (i = 0; i < 2; i++) {
            *pDestByte++ = (unsigned char)(uiTemp & 0xFF);
            chksum += (uiTemp & 0xFF);
            uiTemp >>= 8;
        }

        uiTemp = bootType;
        for (i = 0; i < sizeof(int); i++) {
            *pDestByte++ = (unsigned char)(uiTemp & 0xFF);
            chksum += (uiTemp & 0xFF);
            uiTemp >>= 8;
        }

        if (bootType > 0) {
            for (pTemp = pPathName; *pTemp; pTemp++) {
                *pDestByte++ = *pTemp;
                chksum += *pTemp;
            }
            *pDestByte++ = 0;
        }

        *pDestByte++ = (unsigned char)((~chksum) & 0xFF);

        for (i = 0; i < BOOT_TAIL_SIZE; i++) {
            *pDestByte++ = BootTail[i];
        }

        nReturn=ParallelPortWrite(ParallelRecvBuffer,(unsigned int)(pDestByte - ParallelRecvBuffer));
//              if (nReturn == -1)
//                      goto par_error;
#else
    for (;;) {
#endif //BIDI
        //
        // The first six bytes should be "S000FF" (for SRE file) or
        // "B000FF" (for files with internal binary format)
        //
        nReturn=ParallelPortRead(ParallelRecvBuffer, SIZE_OF_SYNC_BYTES);

        if (ParallelRecvBuffer[0] == 'S') {
            fSre = 1;
			// Read till the end of the current line to ignore it.
			{
				char ch;		
				ch = OEMParallelPortGetByte();
				while((ch != '\r') && (ch != '\n')) {
					ch = OEMParallelPortGetByte();
				}
			}
            break;
        } else if (ParallelRecvBuffer[0] == 'B') {
            fSre = 0;
            break;
        } else {
            nReturn = - 4;
            continue;
        }

        for (i = 1; i < SIZE_OF_SYNC_BYTES; i++) {
            if (ParallelRecvBuffer[i] != SyncBytes[i - 1]) {
                nReturn = - 4;
                goto cont;
            }
        }
cont:;
    }

    //
    //  display any errors returned
    //
    switch (nReturn) {
        case - 1:
            OutputString("\nCan't set parallel port direciton to write\nLaunching existing image...\r\n");
//            LaunchExisting();
        case - 2:
            OutputString("\nHost computer not responding\n");
            goto doom;
        case - 3:
            OutputString("\nCan't set parallel port direction to read\n");
            return 0;
        case - 4:
            OutputString("\nFail to synchonize with the host\n");
doom:
            OutputString("Make sure you have ppfs running on the host computer\n");
            OutputString("and reset the client to restart\r\nLaunching existing image...\r\n");
//            LaunchExisting();
            break;
        default :
            break;
    }

    //
    // Ok, we are synchronized with the host now
    // start streaming the data records
    //
    // if file type is .BIN, read physical start and length
    if (!fSre) {
		int retval;
        // get the next 8 bytes
        if (ParallelPortRead(ParallelRecvBuffer, 8)==-1) {
            OutputString("\r\nFailed to read sync bytes.\r\nLaunching existing image...\r\n");
//            LaunchExisting();
        }

        // get physical start and length
        ulPhysStart = *((unsigned int  *)ParallelRecvBuffer);
        ulPhysLen = *(((unsigned int  *)ParallelRecvBuffer) + 1);
        OutputFormatString("\r\nImage start %Xh length %Xh, ParallelRecvBuffer=%X.  \r\n",ulPhysStart,ulPhysLen, ParallelRecvBuffer);

        // if image is going to flash area
        // it will first be downloaded to RAM
        if (!(retval = OEMFlashWriteBegin(ulPhysStart,ulPhysLen))) 
		{
            OutputString("Image going to FLASH.\r\n");
            nReturn= Download2Flash();
        } else if(retval == 1) {
            OutputString("Image going to RAM.\r\n");
            nReturn= Download2RAM();
        }
		else {
			// There must be some error.
			OutputString("Error writing image.\r\n");
			return retval;
		}
        switch (nReturn) {
            case DL_SUCCESS:                        // successful download
#if ((SH_PLATFORM == PLATFORM_ASPEN)||(SH_PLATFORM==PLATFORM_BIGSUR))
				return 0;
#else ((SH_PLATFORM == PLATFORM_ASPEN)||(SH_PLATFORM==PLATFORM_BIGSUR))
                OutputFormatString("Download successfull! Jumping to flash image at %Xh...\r\n",ulLaunch);
                OutputFormatString("Instruction 0=%Xh, 1=%Xh, 2=%Xh\r\n",
                *(unsigned int *)ulLaunch,*(unsigned int *)(ulLaunch+4),*(unsigned int *)(ulLaunch+8));
                Launch(ulLaunch);
#endif ((SH_PLATFORM == PLATFORM_ASPEN)||(SH_PLATFORM==PLATFORM_BIGSUR))
                break;
            case DL_PARALLEL_ERROR:         // error on parallel port
                OutputString("ERROR: parallel port error. Download aborted.\r\n");
                break;
            case DL_FLASH_ERROR:            // error on flash
                OutputString("ERROR: flash error.  Download aborted.\r\n");
                break;
            case DL_CHECKSUM_ERROR:         // checksum error
                OutputString("ERROR: checksum mismatch.  Download aborted.\r\n");
                break;
            case DL_TIMEOUT:                        // inactivity timeout
                OutputString("ERROR: inactivity timeout.  Download aborted.\r\n");
                break;
        }
    }
	else {
		unsigned StartAddress = 0;
		// DownloadSreFile thru the parallel port.
		StartAddress = DownloadSreFile(1);
		OutputFormatString("Jumping to image at 0x%x\r\n", StartAddress);
		if(fLaunch)
			Launch(StartAddress);

	}
}

⌨️ 快捷键说明

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