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

📄 mvupdateflash.c

📁 此代码为烧制bios程序的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
            temp = temp << 4;

            temp += (unsigned long) GDT;
            temp1 = (unsigned long *)(((unsigned int) GDT) + 2);
            * (unsigned long *) temp1 = temp;
        }
    }

    /* Fill adapterList data structure with all mvSata adapters on PCI */
    i = 0;
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    printf("###############################################################\n");
    printf("##                                                           ##\n");
    printf("## NeoCreatec Serial ATA BIOS upgrade utility                ##\n");
    printf("##                                                           ##\n");
    printf("###############################################################\n");
    printf("##                                                           ##\n");
    printf("## Detecting 88SX%04x Adapters ...                           ##\n",(unsigned long)deviceId);
    printf("##                                                           ##\n");
    while (1)
    {
        unsigned short temp;
        regs.h.ah = 0xb1;
        regs.h.al = 0x2;
        regs.w.cx = deviceId;
        regs.w.dx = 0x11ab;
        regs.w.si = i;
        int86 (0x1a, &regs,&regs);
        if (regs.w.cflag & 0x1)
        {
            break;
        }
        adapterList[i].bus = regs.h.bh;
        adapterList[i].devFunc = regs.h.bl;


        if (useIOBar)
        {
            if (readConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x18, &temp))
            {
                break;
            }
            adapterList[i].bar = temp & 0xfff0;


            if (readConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x1a, &temp))
            {
                break;
            }
            adapterList[i].bar = adapterList[i].bar | ((unsigned long)((unsigned long)(temp) << 16));
            if (adapterList[i].bar & 0xffff0000UL)
            {
                printf ("Error - Can't access IO Bar (offset %08lx)\n",
                        adapterList[i].bar);
                exit (-1);
            }
            if (adapterList[i].bar & 0xffffff00UL == 0)
            {
                printf ("Error - BIOS did not inialize IO BAR PCI resources\n");
                exit (-1);
            }
        } else
        {
            if (readConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x10, &temp))
            {
                break;
            }
            adapterList[i].bar = temp & 0xfff0;


            if (readConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x12, &temp))
            {
                break;
            }
            adapterList[i].bar = adapterList[i].bar | ((unsigned long)((unsigned long)(temp) << 16));
            /* 
             * In case BIOS did not allocate memory space to BAR 0, then disregard
             * the adapter
             */


            if (adapterList[i].bar == 0x14000000)
            {
                printf ("Error - BIOS did not inialize PCI resources\n");
                exit (-1);
            }

        }

        /* Print the device found */
        printf("## Found card %d at bus %03d   : dev %03d                       ##\n",i + 1,
               adapterList[i].bus, adapterList[i].devFunc >> 3);

        if ((deviceId == 0x5081) ||
            (deviceId == 0x5041))
        {
            unsigned long regVal;
            /* enabled flash controller clock */
            regVal = read32bitMemBar (adapterList[i].bar + 
                                      MV_PCI_EXPANSION_ROM_CONTROL_REG_OFFSET);
            regVal |= 0x1;
            write32bitMemBar (adapterList[i].bar + 
                              MV_PCI_EXPANSION_ROM_CONTROL_REG_OFFSET,regVal);
            /* Make sure the register is written */
            regVal = read32bitMemBar (adapterList[i].bar + 0xd2c);
            /* Set the GPIO mode to flash interface */
            write32bitMemBar (adapterList[i].bar +
                              MV_FLASH_GPIO_PORT_CONTROL_OFFSET, 0x2);
        }
        if ((deviceId == 0x6081) ||
            (deviceId == 0x6041))
        {
            unsigned long regVal;
            /* Enable flash */
            write32bitIoBar (adapterList[i].bar, 0x000104f0, 0x00020062);
            /* Enable BAR2 mapping to flash */
            regVal = read32bitIoBar (adapterList[i].bar, 0xc08);
            regVal &= 0xffffffc0UL;
            regVal |= 0x0000001fUL;
            write32bitIoBar (adapterList[i].bar, 0xc08, regVal);
        }


        if (!useIOBar)
        {
            /* Switch between BAR0 and EXP ROM BAR */
            writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x10, 0);
            writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x12, 0x1400);
            writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x30, (adapterList[i].bar & 0xffff) | 0x1);
            writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x32, ((adapterList[i].bar >> 16) & 0xffff));
        }

        /* Check if previous BIOS code exists */
        if (read16bit (adapterList[i].bar, 0) == 0xaa55)
        {
            unsigned char buff[50];
            int temp;
            for (temp = 0x60 ; temp <= 0x76 ; temp ++)
            {
                buff[temp - 0x60] = read8bit(adapterList[i].bar, temp);
            }
            if (strcmp(buff, "NeoCreatec TeraExpress"))
            {
                adapterList[i].version = 0xff;
                adapterList[i].subVersion = 0xff;
            } else
            {
                /* Valid exp. ROM - read the old version which is in bytes 0x40 */
                /* and 0x41 of the image */
                adapterList[i].version = read8bit (adapterList[i].bar, 0x40);
                adapterList[i].subVersion = read8bit (adapterList[i].bar, 0x41);
            }
        } else
        {
            adapterList[i].version = 0xff;
            adapterList[i].subVersion = 0xff;
        }
        if (!useIOBar)
        {
            /* Switch back between BAR0 and EXP ROM BAR */
            writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x30, 0);
            writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x32, 0);
            writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x10, (adapterList[i].bar & 0xffff));
            writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x12, ((adapterList[i].bar >> 16) & 0xffff));
        }

        i++;
    }
    printf("##                                                           ##\n");
    printf("###############################################################\n");

    if (i == 0)
    {
        printf("No NeoCreatec SATA devices found - exiting\n");
        exit(0);
    }
    numOfAdapters = i;

    printf("##                                                           ##\n");
    printf("##  Verifying requested image checksum                       ##\n");
    ch = 0 ;
    imageSize = 0;
    while (1)
    {
        int result = fgetc(romImage);
        if (result == EOF)
        {
            break;
        }
        ch += (unsigned char)result;
        if (imageSize == 0x40)
        {
            imageVersion = result;
        }
        if (imageSize == 0x41)
        {
            imageSubversion = result;
        }
        imageSize ++;
    }
    fclose (romImage);
    if (ch)
    {
        printf("Error - Image <%s> checksum is non-zero\n",argv[1]);
        exit (-1);
    }

    /* Image bigger than 512KByte */
    if (imageSize > 0x00080000)
    {
        printf("Error - Image too big\n");
    }
    printf("##  Checksum OK - Version %d.%d.%d.%d                            ##\n",
           imageVersion/10,imageVersion%10, imageSubversion/10,imageSubversion%10);
    printf("##                                                           ##\n");
    printf("###############################################################\n");
    while (1)
    {
        printf("##                                                           ##\n");
        printf("##  Select Adapter :                                         ##\n");
        for (i = 0 ; i < numOfAdapters ; i ++)
        {
            if ((adapterList[i].version == 0xff) && (adapterList[i].subVersion == 0xff))
            {
                printf("## [%1d] 88SX%04x On Bus %03d, device %03d                       ##\n",
                       i+1, deviceId, adapterList[i].bus,adapterList[i].devFunc>> 3);
            } else
            {
                printf("## [%1d] 88SX%04x On Bus %03d, device %03d (Current Ver. %d.%d.%d.%d)##\n",
                       i+1, deviceId,adapterList[i].bus,adapterList[i].devFunc>> 3,
                       adapterList[i].version / 10,
                       adapterList[i].version % 10,
                       adapterList[i].subVersion / 10,
                       adapterList[i].subVersion % 10);
            }
        }
        printf("##                                                           ##\n");
        printf("###############################################################\n");
        ch = getchar();
        if ((ch >= '0') && (ch <= '9'))
        {
            ch = ch - '0';
        } else
        {
            printf("Press a valid adapter number\n");
            continue;
        }
        if (ch > numOfAdapters)
        {
            printf("Press a valid adapter number\n");
            continue;
        } else
        {
            printf("Choosen adapter %d\n",ch);
            break;
        }
    }
    i = ch - 1;

    if (!useIOBar)
    {
        /* Switch between BAR0 and EXP ROM BAR */
        writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x10, 0);
        writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x12, 0x1400);
        writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x30, (adapterList[i].bar & 0xffff) | 0x1);
        writeConfig16bit(adapterList[i].bus, adapterList[i].devFunc, 0x32, ((adapterList[i].bar >> 16) & 0xffff));
    }
    printf("###############################################################\n");
    printf("##                                                           ##\n");
    flashSize = mvFlashInit (adapterList[i].bar , 1, PURE8);
    printf("##  Detected flash size 0x%08lx                           ##\n",flashSize);
    if (!flashSize)
    {
        printf("Error - flash size detected is 0\n");
        switchBackBetweenBar0andExpRom (adapterList[i].bus, adapterList[i].devFunc,
                                        adapterList[i].bar);
        exit (-1);
    }
    if (flashSize > 0x00080000)
    {
        printf("Error - Flash size too big\n");
    }
    if (flashSize < imageSize)
    {
        printf("Error - Image size (%08lx bytes) is bigger than the flash size (%08lx bytes)\n",
               imageSize, flashSize);
    }

    sectorsToErase = 0;
    while (1)
    {
        /* TODO - Rabeeh - Check if sector reached last sector - if so erase all flash */
        if (mvFlashGetSectorOffset(sectorsToErase + 1) > imageSize)
        {
            break;
        }
        sectorsToErase ++;

    }

    printf("##  Erasing %2d sector(s)                                     ##\n",
           sectorsToErase + 1);
    while (1)
    {
        if (mvFlashEraseSector(sectorsToErase) == false)
        {
            printf("Error erasing sector %d\n",sectorsToErase);
            switchBackBetweenBar0andExpRom (adapterList[i].bus, adapterList[i].devFunc,
                                            adapterList[i].bar);
            exit (-1);
        }
        if (sectorsToErase == 0)
        {
            break;
        }
        sectorsToErase --;
    }
    printf("##  Writing image     ");
    romImage = fopen (argv[1], "rob");
    if (!romImage)
    {
        printf("Error reopening image %s\n",argv[1]);
        switchBackBetweenBar0andExpRom (adapterList[i].bus, adapterList[i].devFunc,
                                        adapterList[i].bar);
        exit (-1);
    }
    offset = 0;
    while (1)
    {
        int result = fgetc(romImage);
        static int progress = 0;
        if (result == EOF)
        {
            break;
        }
        if (mvFlashWriteChar(offset, result) == false)
        {
            break;
        }
        offset ++;
        imageSize --;
        progress ++;
        if (!(progress % 1000))
        {
            progress = 0;
            printf(".");
            fflush (stdout);
        }
    }
    for (temp = 39 ; temp > (offset / 1000) ; temp --)
    {
        printf(" ");
    }
    printf("##\n");
    if (imageSize)
    {
        printf("Error - while writing image - couldn't write whole image (%d bytes left)\n",
               imageSize);
        switchBackBetweenBar0andExpRom (adapterList[i].bus, adapterList[i].devFunc,
                                        adapterList[i].bar);
        exit (-1);
    }
    fclose (romImage);

    for (imageSize = 0 ; imageSize < 512 ; imageSize ++)
    {
        mvFlashWriteChar(offset, 0);
        offset ++;
    }

    printf("##  Verifying image   ");
    romImage = fopen (argv[1], "rob");
    if (!romImage)
    {
        printf("\nError reopening image %s\n",argv[1]);
        switchBackBetweenBar0andExpRom (adapterList[i].bus, adapterList[i].devFunc,
                                        adapterList[i].bar);
        exit (-1);
    }
    offset = 0;
    ch = 0;
    while (1)
    {
        int result = fgetc(romImage);
        static int progress = 0;
        if (result == EOF)
        {
            break;
        }
        if (mvFlashReadChar(offset) != result)
        {
            printf("\nError - images are not identical (offset 0x%08lx)\n",offset);
            break;
        }
        ch += mvFlashReadChar(offset);
        offset ++;
        progress ++;
        if (!(progress % 1000))
        {
            progress = 0;
            printf(".");
            fflush (stdout);
        }
    }
    for (temp = 39 ; temp > (offset / 1000) ; temp --)
    {
        printf(" ");
    }
    printf("##\n");
    if (ch)
    {
        printf("\nError - Image on flash checksum is not zero\n");
    }
    fclose (romImage);
    printf("##                                                           ##\n");
    printf("##   *****  Flash Upgrade Finished Successfully  *****       ##\n");
    printf("##                                                           ##\n");
    printf("###############################################################\n");
    /* Switch between BAR0 and EXP ROM BAR */
    switchBackBetweenBar0andExpRom (adapterList[i].bus, adapterList[i].devFunc,
                                    adapterList[i].bar);

}

⌨️ 快捷键说明

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