📄 mvupdateflash.c
字号:
* write8bitMemBar
*
*
* DESCRIPTION:
* Writes 8 bit to 32bit address space
*
* INPUT:
* offset - A 32bit offset
* value - 8 bit value
*
* RETURN:
* None
*
* NOTE:
* This function utilizes the protected mode of the processor in order to
* access a 32bit region.
*******************************************************************************/
void write8bitMemBar (unsigned long offset, unsigned char value)
{
/* Use protected mode */
_asm{
pushad
; value
shl ecx, 16
mov cx, bx
; offset
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 gs:[edx], cl
; 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
popad
};
#ifdef DEBUG
printf("Writing to %08lx : %x\n",offset, value);
#endif
return;
}
/*******************************************************************************
* write16bitMemBar
*
*
* DESCRIPTION:
* Writes 16 bit to 32bit address space
*
* INPUT:
* offset - A 32bit offset
* value - 16 bit value
*
* RETURN:
* None
*
* NOTE:
* This function utilizes the protected mode of the processor in order to
* access a 32bit region.
*******************************************************************************/
void write16bitMemBar (unsigned long offset, unsigned short value)
{
/* Use protected mode */
_asm{
pushad
; value
shl ecx, 16
mov cx, bx
; offset
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 gs:[edx], cx
; 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
popad
};
#ifdef DEBUG
printf("Writing to %08lx : %x\n",offset, value);
#endif
return;
}
/*******************************************************************************
* write32bitMemBar
*
*
* DESCRIPTION:
* Writes 32 bit to 32bit address space
*
* INPUT:
* offset - A 32bit offset
* value - 32 bit value
*
* RETURN:
* None
*
* NOTE:
* This function utilizes the protected mode of the processor in order to
* access a 32bit region.
*******************************************************************************/
void write32bitMemBar (unsigned long offset, unsigned long value)
{
/* Use protected mode */
_asm{
pushad
; value
shl ecx, 16
mov cx, bx
; offset
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 gs:[edx], ecx
; 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
popad
};
#ifdef DEBUG
printf("Writing to %08lx : %08lx\n",offset, value);
#endif
return;
}
unsigned char read8bit (unsigned long offset, unsigned long address)
{
if (useIOBar)
{
return read8bitIoBar ((unsigned short)offset,address | 0x80000000UL);
} else
{
return read8bitMemBar (offset + address);
}
}
unsigned short read16bit (unsigned long offset, unsigned long address)
{
if (useIOBar)
{
return read16bitIoBar ((unsigned short)offset,address | 0x80000000UL);
} else
{
return read16bitMemBar (offset + address);
}
}
unsigned long read32bit (unsigned long offset, unsigned long address)
{
if (useIOBar)
{
return read32bitIoBar ((unsigned short)offset,address | 0x80000000UL);
} else
{
return read32bitMemBar (offset + address);
}
}
void write8bit (unsigned long offset, unsigned long address, unsigned char value)
{
if (useIOBar)
{
write8bitIoBar (offset, address | 0x80000000UL, value);
} else
{
write8bitMemBar (offset + address, value);
}
}
void write16bit (unsigned long offset, unsigned long address, unsigned short value)
{
if (useIOBar)
{
write8bitIoBar (offset, address | 0x80000000UL, value);
} else
{
write16bitMemBar (offset + address, value);
}
}
void write32bit (unsigned long offset, unsigned long address, unsigned long value)
{
if (useIOBar)
{
write32bitIoBar (offset, address | 0x80000000UL, value);
} else
{
write32bitMemBar (offset + address, value);
}
}
int readConfig16bit(unsigned char bus, unsigned char devFunc,
unsigned short offset, unsigned short *value)
{
regs.h.ah = 0xb1;
regs.h.al = 0x9;
regs.h.bh = bus;
regs.h.bl = devFunc;
regs.w.di = offset;
int86 (0x1a, ®s,®s);
* value = regs.w.cx;
if (regs.w.cflag & 0x1)
{
return regs.h.ah;
}
return 0;
}
int writeConfig16bit(unsigned char bus, unsigned char devFunc,
unsigned short offset, unsigned short value)
{
regs.h.ah = 0xb1;
regs.h.al = 0xc;
regs.h.bh = bus;
regs.h.bl = devFunc;
regs.w.di = offset;
regs.w.cx = value;
int86 (0x1a, ®s,®s);
if (regs.w.cflag & 0x1)
{
return regs.h.ah;
}
return 0;
}
void switchBackBetweenBar0andExpRom(unsigned char bus, unsigned char devFunc,
unsigned long bar0)
{
if (!useIOBar)
{
/* Switch between BAR0 and EXP ROM BAR */
writeConfig16bit(bus, devFunc, 0x30, 0);
writeConfig16bit(bus, devFunc, 0x32, 0);
writeConfig16bit(bus, devFunc, 0x10, bar0 & 0xffff);
writeConfig16bit(bus, devFunc, 0x12, (bar0 >> 16) & 0xffff);
}
}
void main (int argc, char **argv)
{
int i, numOfAdapters;
struct mvPciSataAdapter adapterList[10] = {0,};
FILE *romImage;
unsigned long imageSize;
unsigned char ch;
unsigned long temp, *temp1;
unsigned int dataSegment;
unsigned long flashSize, offset;
int sectorsToErase;
unsigned char imageVersion, imageSubversion;
unsigned short deviceId = 0, PCIDS = 0;
if (argc != 2)
{
printf("Usage - %s <rom image>\n",argv[0]);
exit (-1);
}
romImage = fopen (argv[1], "rob");
if (!romImage)
{
printf("ROM Image %s not found\n",argv[1]);
exit (-1);
}
/* Read the device ID of the adapter from the requested ROM image */
/*
* First look in PnP Option ROM header file for offset of PCI data
* structure.
*/
i = PCI_DATA_STRUCTURE_OFFSET;
while (i)
{
int result = fgetc(romImage);
if (result == EOF)
{
break;
}
i--;
}
if (i != 0)
{
printf("Error seeking for Device ID in requested ROM image\n");
exit(-1);
}
for (i = 0 ; i < 2 ; i ++)
{
int result;
result = fgetc(romImage);
if (result == EOF)
{
printf("Error seeking for Device ID in requested ROM image\n");
exit(-1);
}
PCIDS = (PCIDS >> 8) | (((unsigned char) result) << 8);
}
/*
* Seek to Device ID. This is offset 0x6 in PCI Data structure minus
* what was already read from the image.
*/
i = PCIDS+ DEVICE_ID_OFFSET_IN_PCIDS -
(PCI_DATA_STRUCTURE_OFFSET + 0x2);
while (i)
{
int result = fgetc(romImage);
if (result == EOF)
{
break;
}
i--;
}
if (i != 0)
{
printf("Error seeking for Device ID in requested ROM image\n");
exit(-1);
}
for (i = 0 ; i < 2 ; i ++)
{
int result;
result = fgetc(romImage);
if (result == EOF)
{
printf("Error seeking for Device ID in requested ROM image\n");
exit(-1);
}
deviceId = (deviceId >> 8) | (((unsigned char) result) << 8);
}
if ((deviceId != 0x5081) && (deviceId != 0x5041) &&
(deviceId != 0x6081) && (deviceId != 0x6041))
{
printf("Error - Device ID in requested ROM image is invalid (%04x)\n",
(unsigned long)deviceId);
exit (-1);
}
if ((deviceId == 0x6081) ||
(deviceId == 0x6041))
{
useIOBar = 1;
} else
{
useIOBar = 0;
}
fclose (romImage);
romImage = fopen (argv[1], "rob");
if (!romImage)
{
printf("ROM Image %s not found\n",argv[1]);
exit (-1);
}
if (!useIOBar)
{
activateA20Gate();
{
temp = 0;
dataSegment = 0;
_asm{mov dataSegment, ds};
temp = dataSegment;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -