main.c

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

C
1,072
字号
        if ((ulSectionSize - ulSectionOffset) < CHUNKSIZE)
        {
            usReadSize = (USHORT)(ulSectionSize - ulSectionOffset);
        }
        else
        {
            usReadSize = CHUNKSIZE;
        }

        xmsError = XmsMoveExtendedMemory(
            0, (ULONG)(UCHAR far *)ucBuffer,
            usBlockHandle, ulSectionOffset, usReadSize);

        if (xmsError != XMS_SUCCESS)
        {
            fprintf(stderr, "\r                          \r");
            printf("Error zeroing extended memory - %s\n", XmsErrorString(xmsError));
            if (!bParallelDownload && !bSerialDownload)
                _close(hImage);

            return FALSE;
        }
    }
#endif

    /*
    ** Open image file
    */
    if (bSerialDownload)
        OEMInitDebugSerial();
    if (bParallelDownload || bSerialDownload) {
        //
        // Prepare boot packet
        //
        pDestByte = BootPacket;
        for (i = 0; i < BOOT_HEADER_SIZE; i++) {
            *pDestByte++ = BootHeader[i];
        }

        chksum = 0;
        len = 9;

        if (pszFileName) {
            if (bSerialDownload)
                printf("Loading image %s via serial port.\r\n",pszFileName);
            else
                printf("Loading image %s via parallel port.\r\n",pszFileName);
            bootType = 1;              // The NULL byte
            for (pTemp = (UCHAR*)pszFileName; *pTemp; pTemp++) {
                bootType++;
            }

            len += (unsigned)bootType;

        } else {
            if (bSerialDownload)
                printf("Loading host default image via serial port.\r\n");
            else
                printf("Loading host default image via parallel port.\r\n");
            bootType = BOOT_TYPE;
        }

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

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

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

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

        for (i = 0; i < BOOT_TAIL_SIZE; i++) {
            *pDestByte++ = BootTail[i];
        }
        if (bVerbose)
            printf("Sending boot packet: ");
        usIndex=0;
        while (&BootPacket[usIndex] < pDestByte) {
            if (NoPPFS)
                return FALSE;
            if (bVerbose)
                printf("%x ",BootPacket[usIndex]);
            if (bSerialDownload)
                OEMWriteDebugByte(BootPacket[usIndex]);
            else
                OEMParallelPortSendByte(BootPacket[usIndex]);
            usIndex++;
        }
        if (bVerbose)
            printf("Sent.\r\n");
    } else {
        hImage = _open(pszFileName, _O_BINARY | _O_RDONLY);

        if (hImage == -1)
        {
            printf("%s: Error opening file - %s\n", pszFileName, _strerror(NULL));
            return FALSE;
        }

        if ((lImageSize = _filelength(hImage)) == -1)
        {
            printf("%s: Error obtaining file size - %s\n", pszFileName, _strerror(NULL));
            if (!bParallelDownload && !bSerialDownload)
                _close(hImage);

            return FALSE;
        }
        if (bVerbose)
        {
            printf("Loading %s, size = %ld\n", pszFileName, lImageSize);
        }
    }

    /*
    ** Read initial signature and physical start and size
    */

    usReadSize = sizeof(ucSignature) + 2 * sizeof(ULONG);

    if (DownloadRead(hImage, ucBuffer, usReadSize, bParallelDownload) != usReadSize)
    {
        printf("Error reading signature - %s\n", _strerror(NULL));
        if (!bParallelDownload && !bSerialDownload)
            _close(hImage);

        return FALSE;
    }

    if (memcmp(ucBuffer, ucSignature, sizeof(ucSignature)) != 0)
    {
        printf("Error invalid signature\nData: ");
        for (i = 0; i < (int)usReadSize; i++)
            printf("%x ",ucBuffer[i]);
        printf("\r\n");
        if (!bParallelDownload && !bSerialDownload)
            _close(hImage);

        return FALSE;
    }

    ulReadTotal=*(PULONG)&ucBuffer[sizeof(ucSignature) + sizeof(ULONG)];
    if (bVerbose)
    {
        /*
        ** Print Physical start and size
        */

        printf(
            "Image physical start = 0x%8.8lX, size = %ld\n",
            *(PULONG)&ucBuffer[sizeof(ucSignature)],
            *(PULONG)&ucBuffer[sizeof(ucSignature) + sizeof(ULONG)]);
    }
    // Initialize the percent thingie.
    DrawPercent((DWORD)-1, (DWORD)"");

    /*
    ** Copy file to upper memory in CHUNKSIZE chunks
    */

    for ( ; ; )
    {
        usAmountRead = DownloadRead(hImage, ucBuffer, 3 * sizeof(ULONG), bParallelDownload);
        ulReadProgress+=12;
        if (usAmountRead != 3 * sizeof(ULONG))
        {
            fprintf(stderr, "\r                          \r");
            printf("Error reading header - %s\n", XmsErrorString(xmsError));
            if (!bParallelDownload && !bSerialDownload)
                _close(hImage);

            return FALSE;
        }

        ulSectionAddress = *(PULONG)&ucBuffer[0];
        ulSectionSize = *(PULONG)&ucBuffer[4];
        ulSectionChecksum = *(PULONG)&ucBuffer[8];

        if (ulSectionAddress == 0)
        {
            *pulEntryPoint = ulSectionSize;

            break;
        }

        if (ulSectionAddress < ulLinearAddress ||
            (ulSectionAddress + ulSectionSize) >
                (ulLinearAddress + (ULONG)usLargestBlock * 1024))
        {
            fprintf(stderr, "\r                          \r");
            printf(
                "Error image section doesn't fit in allocated block\n"
                "Block allocated at 0x%lX, size = %ld\n"
                "Section physical start = 0x%8.8lX, size = %ld\n",
                ulLinearAddress, (ULONG)usLargestBlock * 1024,
                ulSectionAddress, ulSectionSize);

            if (!bParallelDownload && !bSerialDownload)
                _close(hImage);

            return FALSE;
        }

        if (bVerbose)
        {
            fprintf(stderr, "\r                          \r");
            printf(
                "Section physical start = 0x%8.8lX, size = %ld\n",
                ulSectionAddress, ulSectionSize);
        }

        ulChecksum = 0;

        for (ulSectionOffset = 0; ulSectionOffset < ulSectionSize; )
        {
            if ((ulSectionSize - ulSectionOffset) < CHUNKSIZE)
            {
                usReadSize = (USHORT)(ulSectionSize - ulSectionOffset);
            }
            else
            {
                usReadSize = CHUNKSIZE;
            }
            usAmountRead = DownloadRead(hImage, ucBuffer, usReadSize,bParallelDownload);

            if (usAmountRead != usReadSize)
            {
                fprintf(stderr, "\r                          \r");
                printf("Error reading section - %s\n", XmsErrorString(xmsError));
                if (!bParallelDownload && !bSerialDownload)
                    _close(hImage);

                return FALSE;
            }
            ulReadProgress+= usReadSize;
            DrawPercent(ulReadProgress, ulReadTotal);
#if 0

            for (usIndex = 0; usIndex < usAmountRead; usIndex++)
            {
                ulChecksum += ucBuffer[usIndex];
            }
#endif
#if 1
            xmsError = XmsMoveExtendedMemory(
                0, (ULONG)(UCHAR far *)ucBuffer,
                usBlockHandle, ulSectionAddress - ulLinearAddress + ulSectionOffset,
                (usAmountRead + 1) & ~1U);

            if (xmsError != XMS_SUCCESS)
            {
                fprintf(stderr, "\r                          \r");
                printf("Error moving extended memory - %s\n", XmsErrorString(xmsError));
                if (!bParallelDownload && !bSerialDownload)
                    _close(hImage);

                return FALSE;
            }
#endif
            ulSectionOffset += usAmountRead;
        }
#if 0
        if (ulChecksum != ulSectionChecksum)
        {
            fprintf(stderr, "\r                          \r");
            printf(
                "Bad checksum 0x%8.8lX, expected 0x%8.8lX\n",
                ulChecksum, ulSectionChecksum);

            if (!bParallelDownload && !bSerialDownload)
                _close(hImage);

            return FALSE;
        }
#endif
    }

    DrawPercent(ulReadTotal, ulReadTotal);
    if (!bParallelDownload && !bSerialDownload)
        _close(hImage);

    /*
    ** Stop the floppy motor in case it contained the image file
    */

    __asm
    {
        push    dx
        mov     dx, 03F2h   ; Floppy motor and DMA control
        in      al, dx
        and     al, 00Fh    ; Clear motor on bits
        out     dx, al
        pop     dx
    }
    fprintf(stderr, "\r                          \r");

    return TRUE;
}

UCHAR
GetPCIConfigMechanism()
{
    UCHAR   ucPCIVersionMajor;
    UCHAR   ucPCIVersionMinor;
    UCHAR   ucPCIBus;
    UCHAR   ucPCIConfigMech;

    __asm
    {
        mov     ax, 0xB101
        int     0x1A

        jc      noPCI

        cmp     dx, 0x4350     ; 'CP'

        jne     noPCI

        or      ah, ah
        jnz     noPCI

        mov     ucPCIVersionMajor, bh
        mov     ucPCIVersionMinor, bl
        mov     ucPCIBus, cl

        and     al, 0x03
        mov     ucPCIConfigMech, al
    }

    printf(
        "%d PCI bus%s (Version %X.%2.2X) using Configuration Mechanism # %d\n",
        ucPCIBus + 1, ucPCIBus == 0 ? "" : "ses",
        ucPCIVersionMajor, ucPCIVersionMinor, ucPCIConfigMech);

    return (UCHAR)((ucPCIBus << 2) | ucPCIConfigMech);

noPCI:
    printf("PCI bus not detected\n");

    return 0;
}

⌨️ 快捷键说明

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