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

📄 mvupdateflash.c

📁 此代码为烧制bios程序的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*******************************************************************************
*
*                   Copyright 2003,MARVELL SEMICONDUCTOR ISRAEL, LTD.
* THIS CODE CONTAINS CONFIDENTIAL INFORMATION OF MARVELL.
* NO RIGHTS ARE GRANTED HEREIN UNDER ANY PATENT, MASK WORK RIGHT OR COPYRIGHT
* OF MARVELL OR ANY THIRD PARTY. MARVELL RESERVES THE RIGHT AT ITS SOLE
* DISCRETION TO REQUEST THAT THIS CODE BE IMMEDIATELY RETURNED TO MARVELL.
* THIS CODE IS PROVIDED "AS IS". MARVELL MAKES NO WARRANTIES, EXPRESSED,
* IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, COMPLETENESS OR PERFORMANCE.
*
* MARVELL COMPRISES MARVELL TECHNOLOGY GROUP LTD. (MTGL) AND ITS SUBSIDIARIES,
* MARVELL INTERNATIONAL LTD. (MIL), MARVELL TECHNOLOGY, INC. (MTI), MARVELL
* SEMICONDUCTOR, INC. (MSI), MARVELL ASIA PTE LTD. (MAPL), MARVELL JAPAN K.K.
* (MJKK), MARVELL SEMICONDUCTOR ISRAEL. (MSIL),  MARVELL TAIWAN, LTD. AND
* SYSKONNECT GMBH.
*
*******************************************************************************/
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include "mvFlash.h"
#include "mvRegs.h"
#include <string.h>



/* Defines */
#undef DEBUG
#define PCI_DATA_STRUCTURE_OFFSET	0x18
#define DEVICE_ID_OFFSET_IN_PCIDS	0x6
/* Global variables */
/* Global descriptors table */
unsigned int GDT[] = {  0xf, 0, 0, 0, 
    0xffff, 0, 0x9200, 0x8f};

int useIOBar = 0;

union REGS regs;
struct mvPciSataAdapter
{
    int valid;
    unsigned char bus;
    unsigned char devFunc;
    unsigned long bar;
    unsigned char version;
    unsigned char subVersion;

};

/*******************************************************************************
* activateA20Gate
*
*
* DESCRIPTION:
*   Queries keyboard controller to activate A20 gate line
*
* INPUT:
*	None
* RETURN:
*	None
*
*******************************************************************************/
static void activateA20Gate(void)
{
    _asm {
        cli

        call    a20wait
        mov     al,0xAD
        out     0x64,al

        call    a20wait
        mov     al,0xD0
        out     0x64,al

        call    a20wait2
        in      al,0x60
        push    eax

        call    a20wait
        mov     al,0xD1
        out     0x64,al

        call    a20wait
        pop     eax
        or      al,2
        out     0x60,al

        call    a20wait
        mov     al,0xAE
        out     0x64,al

        call    a20wait
        jmp go_out

        a20wait:
        l0: mov     ecx,65536
        l1: in      al,0x64
        test    al,2
        jz      l2
        loop    l1
        jmp     l0
        l2: ret


        a20wait2:
        c0: mov     ecx,65536
        c1: in      al,0x64
        test    al,1
        jnz     c2
        loop    c1
        jmp     c0
        c2: ret

        go_out :
        nop
    }
}

unsigned char read8bitIoBar (unsigned short offst, register unsigned long address)
{
    unsigned char value = 0;
    _asm{
        pushad
        mov dx, offst
        mov eax, address
        and al, 0xfc
        out dx, eax

        add dx, 0x80
        mov eax, address
        and ax, 0x3
        add dx, ax
        in al, dx

        mov value, al
        popad
    };
#ifdef DEBUG
    printf("Reading from IOBar %08lx : %x\n",offst, value);
#endif
    return value;
}

unsigned short read16bitIoBar (unsigned short offst, register unsigned long address)
{
    unsigned short value = 0;
    _asm{
        pushad
        mov dx, offst
        mov eax, address
        and al, 0xfc
        out dx, eax

        add dx, 0x80
        mov eax, address
        and ax, 0x3
        add dx, ax
        in ax, dx

        mov value, ax
        popad
    };
#ifdef DEBUG
    printf("Reading from IOBar %08lx : %x\n",offst, value);
#endif
    return value;
}


unsigned long read32bitIoBar (unsigned short offst, register unsigned long address)
{
    unsigned long value = 0;
    _asm{
        pushad
        mov dx, offst
        mov eax, address
        and al, 0xfc
        out dx, eax

        add dx, 0x80
        mov eax, address
        and ax, 0x3
        add dx, ax
        in eax, dx

        mov value, eax
        popad
    };
    return value;
}

void write8bitIoBar (unsigned short offst, register unsigned long address, unsigned char value)
{
    _asm{
        pushad
        mov dx, offst
        mov eax, address
        and al, 0xfc
        out dx, eax

        add dx, 0x80
        mov eax, address
        and ax, 0x3
        add dx, ax
        mov al, value
        out dx, al
        popad
    };
}

void write16bitIoBar (unsigned short offst, register unsigned long address, unsigned short value)
{
    _asm{
        pushad
        mov dx, offst
        mov eax, address
        and al, 0xfc
        out dx, eax

        add dx, 0x80
        mov eax, address
        and ax, 0x3
        add dx, ax
        mov ax, value
        out dx, ax
        popad
    };
}

void write32bitIoBar (unsigned short offst, register unsigned long address, unsigned long value)
{
    _asm{
        pushad
        mov dx, offst
        mov eax, address
        and al, 0xfc
        out dx, eax

        add dx, 0x80
        mov eax, address
        and ax, 0x3
        add dx, ax
        mov eax, value
        out dx, eax
        popad
    };
}

/*******************************************************************************
* read8bitMemBar
*
*
* DESCRIPTION:
*   Reads 8 bit from 32bit address space
*
* INPUT:
*   offset	- A 32bit offset 
*
* RETURN:
*   8 bit
*
* NOTE:
*	This function utilizes the protected mode of the processor in order to
*	access a 32bit region.
*******************************************************************************/
unsigned char read8bitMemBar (register unsigned long offset)
{
    /* Use protected mode */
    unsigned char value = 0;
    _asm{
        pushad
        shl edx, 16
        mov dx, ax

        nop
        nop
        cli                ;Don't allow interrupts during this
        push gs            ;Save real mode selectors
        push eax

        lgdt [GDT]         ;Load the GDT
        mov  eax, cr0      ;Switch to pmode
        inc  ax
        mov  cr0, eax
        ; Now we are in protected mode
        mov  ax, 0x8 ;Our only pmode selector
        mov  gs, ax        ;Install 4Gb limits (warning)

        mov  cl, gs:[edx]
        ;       You can add the code in protected mode code here
        mov eax, cr0
        dec  ax            ;Switch back to real mode
        mov  cr0, eax

        pop  eax
        pop  gs
        sti
        mov  value, cl
        popad
    };
#ifdef DEBUG
    printf("Reading from %08lx : %x\n",offset, value);
#endif
    return value;
}


/*******************************************************************************
* read16bitMemBar
*
*
* DESCRIPTION:
*   Reads 16 bit from 32bit address space
*
* INPUT:
*   offset	- A 32bit offset 
*
* RETURN:
*   8 bit
*
* NOTE:
*	This function utilizes the protected mode of the processor in order to
*	access a 32bit region.
*******************************************************************************/
unsigned short read16bitMemBar (register unsigned long offset)
{
    /* Use protected mode */
    unsigned short value = 0;
    _asm{
        pushad
        shl edx, 16
        mov dx, ax

        nop
        nop
        cli                ;Don't allow interrupts during this
        push gs            ;Save real mode selectors
        push eax

        lgdt [GDT]         ;Load the GDT
        mov  eax, cr0      ;Switch to pmode
        inc  ax
        mov  cr0, eax
        ; Now we are in protected mode
        mov  ax, 0x8 ;Our only pmode selector
        mov  gs, ax        ;Install 4Gb limits (warning)

        mov  cx, gs:[edx]
        ;       You can add the code in protected mode code here
        mov eax, cr0
        dec  ax            ;Switch back to real mode
        mov  cr0, eax

        pop  eax
        pop  gs
        sti
        mov  value, cx
        popad
    };
#ifdef DEBUG
    printf("Reading from %08lx : %x\n",offset, value);
#endif
    return value;
}

/*******************************************************************************
* read32bitMemBar
*
*
* DESCRIPTION:
*   Reads 32 bit from 32bit address space
*
* INPUT:
*   offset	- A 32bit offset 
*
* RETURN:
*   8 bit
*
* NOTE:
*	This function utilizes the protected mode of the processor in order to
*	access a 32bit region.
*******************************************************************************/
unsigned long read32bitMemBar (register unsigned long offset)
{
    /* Use protected mode */
    unsigned long value = 0;
    _asm{
        pushad
        shl edx, 16
        mov dx, ax

        nop
        nop
        cli                ;Don't allow interrupts during this
        push gs            ;Save real mode selectors
        push eax

        lgdt [GDT]         ;Load the GDT
        mov  eax, cr0      ;Switch to pmode
        inc  ax
        mov  cr0, eax
        ; Now we are in protected mode
        mov  ax, 0x8 ;Our only pmode selector
        mov  gs, ax        ;Install 4Gb limits (warning)

        mov  ecx, gs:[edx]
        ;       You can add the code in protected mode code here
        mov eax, cr0
        dec  ax            ;Switch back to real mode
        mov  cr0, eax

        pop  eax
        pop  gs
        sti
        mov  value, ecx
        popad
    };
#ifdef DEBUG
    printf("Reading from %08lx : %08lx\n",offset, value);
#endif
    return value;
}

/*******************************************************************************

⌨️ 快捷键说明

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