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

📄 flash.c

📁 完整的Bell实验室的嵌入式文件系统TFS
💻 C
📖 第 1 页 / 共 2 页
字号:
sectortoaddr(int sector,int *size,uchar **base){    struct flashinfo *fbnk;    struct  sectorinfo *sinfo;    int     dev, sec, i;    sec = 0;    for(dev=0;dev<FLASHBANKS;dev++) {        fbnk = &FlashBank[dev];        for(i=0;i<fbnk->sectorcnt;i++,sec++) {            if (sec == sector) {                sinfo = &fbnk->sectors[i];                if (base) *base = sinfo->begin;                if (size) *size = sinfo->size;                return(0);            }        }    }    printf("sectortoaddr(%d) failed\n",sector);    return(-1);}/* flashbankinfo(): *  Based on the incoming bank number, return the beginning, end and *  number of sectors within that bank. */intflashbankinfo(int bank,uchar **begin,uchar **end,int *sectorcnt){    struct flashinfo *fbnk;    if (bank >= FLASHBANKS)        return(-1);    fbnk = &FlashBank[bank];    if (begin)        *begin = fbnk->base;    if (end)        *end = fbnk->end;    if (sectorcnt)        *sectorcnt = fbnk->sectorcnt;    return(0);}/* lastlargesector(): *  Incoming bank number is used to populate the sector information *  (sector number, sector size and address) of the last large sector *  in the specified bank. *  Return 0 if successful; else -1. */intlastlargesector(int bank,int *sector,int *size,uchar **base){    struct flashinfo    *fbnk;    struct sectorinfo   *sinfo;    uchar               *largest_sbase;    int                 i, largest_ssize, largest_snum;    if (bank >= FLASHBANKS) {        printf("lastlargesector(%d) failed\n",bank);        return(-1);    }    fbnk = &FlashBank[bank];    sinfo = fbnk->sectors;    largest_ssize = 0;    largest_snum = 0;    largest_sbase = (uchar *)0;    for(i=0;i<fbnk->sectorcnt;i++,sinfo++) {        if (sinfo->size >= largest_ssize) {            largest_ssize = sinfo->size;            largest_snum = sinfo->snum;            largest_sbase = sinfo->begin;        }    }    if (sector)        *sector = largest_snum;    if (size)        *size = largest_ssize;    if (base)        *base = largest_sbase;    return(0);}voidLowerFlashProtectWindow(){    if (FlashProtectWindow)        FlashProtectWindow--;}/* AppFlashWrite(): *  Takes in a source, destination and byte count and converts that to *  the appropriate flashwrite() call.  This function supports the possibility *  of having one write request span across multiple devices in contiguous *  memory space. */intAppFlashWrite(dest,src,bytecnt)ulong   *src, *dest;long bytecnt;{    struct flashinfo *fbnk;    ulong   oints;    int     ret, tmpint;    long    tmpcnt;    ret = 0;    while(bytecnt > 0) {        fbnk = addrtobank((uchar *)dest);        if (!fbnk)            return(-1);            if (((int)dest + bytecnt) <= (int)(fbnk->end))            tmpcnt = bytecnt;        else            tmpcnt = ((int)(fbnk->end) - (int)dest) + 1;            oints = FLASH_INTSOFF();        ret = flashwrite(fbnk,(uchar *)dest,(uchar *)src,tmpcnt);        FLASH_INTSRESTORE(oints);        if (ret < 0) {            printf("AppFlashWrite(0x%lx,0x%lx,%ld) failed\n",                (ulong)dest,(ulong)src,bytecnt);            break;        }        tmpint = (int)dest;        tmpint += tmpcnt;        dest = (ulong *)tmpint;        tmpint = (int)src;        tmpint += tmpcnt;        src = (ulong *)tmpint;        bytecnt -= tmpcnt;    }    return(ret);}intAppFlashEraseAll(){    int     ret, i;    ulong   oints;    struct  flashinfo *fbnk;    ret = 0;    oints = FLASH_INTSOFF();    fbnk = FlashBank;    for(i=0;i<FLASHBANKS;i++,fbnk++) {        ret = flasherase(fbnk,ALL_SECTORS);        if (ret == -1)            break;    }    FLASH_INTSRESTORE(oints);    return(ret);}/* Erase the flash sector specified. */intAppFlashErase(snum) /* erase specified sector */int snum;{    ulong   oints;    uchar   *base;    int     ret, size;    struct  flashinfo *fbnk;    sectortoaddr(snum,&size,&base);    fbnk = addrtobank(base);    if (!fbnk)        return(-1);    oints = FLASH_INTSOFF();    ret = flasherase(fbnk,snum);    FLASH_INTSRESTORE(oints);    return(ret);}/* sectorProtect(): *  Set or clear (based on value of protect) the protected flag for the *  specified range of sectors... *  This supports incoming ranges that can be dash and/or comma delimited. *  For example a range can be "0", "0-3", or "0,2-4", etc... */intsectorProtect(char *range, int protect){    struct  flashinfo *fbnk;    int i, dev, snum;    snum = 0;    for(dev = 0;dev < FLASHBANKS;dev++) {        fbnk = &FlashBank[dev];        for(i = 0;i < fbnk->sectorcnt;i++,snum++) {            if (inRange(range,snum))                fbnk->sectors[i].protected = protect;        }    }    return(0);}#ifdef FLASHRAM_BASE/* FlashRamInit(): * This monitor supports TFS space allocated across multiple flash devices * that may not be in contiguous memory space.  To allow BBRAM to be seen * as a "flash-like" device to TFS, we set it up in sectors similar to * those in a real flash device. * Input... *  snum:   All the "flash" space is broken up into individual sectors. *          This is the starting sector number that is to be used for *          the block of sectors within this BBRAM space. *  fbnk:   Pointer to the structure that must be populated with the *          flash bank information.  Usually this contains pointers to the *          functions that operate on the flash; but for RAM they aren't *          necessary. *  sinfo:  Table populated with the characteristics (size, start, etc...) *          of each sector. *  ssizes: A table containing the size of each of the sectors.  This is *          copied to the sinfo space. */intFlashRamInit(int snum, int scnt, struct flashinfo *fbnk,            struct sectorinfo *sinfo, int *ssizes){    int i;    uchar   *begin;    /* FLASHRAM_SECTORCOUNT (in config.h) must match the number of sectors     * allocated to the flash ram device in flashdev.c...     */    if (scnt != FLASHRAM_SECTORCOUNT)        printf("Warning: flashram sector count inconsistency\n");    fbnk->id = FLASHRAM;                        /* Device id. */    fbnk->base = (uchar *)FLASHRAM_BASE;        /* Base address of bank. */    fbnk->end = (uchar *)FLASHRAM_END;          /* End address of bank. */    fbnk->sectorcnt = scnt;                     /* Number of sectors. */    fbnk->width = 1;                            /* Width (in bytes). */    fbnk->fltype = NotUsed;                     /* Flashtype() function. */    fbnk->flerase = NotUsed;                    /* Flasherase() function. */    fbnk->flwrite = NotUsed;                    /* Flashwrite() function. */    fbnk->flewrite = NotUsed;                   /* Flashewrite() function. */    fbnk->sectors = sinfo;                  /* Ptr to sector size table. */    begin = fbnk->base;    for(i=0;i<fbnk->sectorcnt;i++,snum++) {        sinfo[i].snum = snum;        sinfo[i].size = ssizes[i];        sinfo[i].begin = begin;        sinfo[i].end = sinfo[i].begin + sinfo[i].size - 1;        sinfo[i].protected = 0;        begin += sinfo[i].size;    }    return(snum);}#endifchar *FlashHelp[] = {    "Flash memory operations",    "{op} [args]",    "Ops...",    "  opw",    "  info [rnge]",    "  init",    "  bank [#]",    "  prot {rnge}",    "  unprot {rnge}",#if FLASH_LOCK_SUPPORTED    "  lock {rnge}",    "  unlock {rnge}",    "  lockdwn {rnge}",#endif    "  erase {rnge}",    "  write {dest} {src} {byte_cnt}",    "  ewrite {dest} {src} {byte_cnt}",    "",    "  rnge = range of affected sectors",    0,};/* FlashCmd(): *  Code that handles the user interface.  See FlashHelp[] below for usage. */intFlashCmd(int argc,char *argv[]){    int     snum, ret;    ulong   dest, src, oints;    long    bytecnt, rslt;    struct  flashinfo *fbnk;    oints = FLASH_INTSOFF();    fbnk = &FlashBank[FlashCurrentBank];    ret = CMD_SUCCESS;    if (strcmp(argv[1],"init") == 0)        FlashInit();    else if (strcmp(argv[1],"info") == 0) {        showflashinfo(fbnk,argv[2]);    }    else if (strcmp(argv[1],"bank") == 0)  {        int tmpbank;        if (argc == 3) {            tmpbank = atoi(argv[2]);            if (tmpbank < FLASHBANKS)                FlashCurrentBank = tmpbank;            printf("Subsequent flash ops apply to bank %d\n",FlashCurrentBank);        }        else             printf("Current flash bank: %d\n",FlashCurrentBank);    }    else if (strcmp(argv[1],"ewrite") == 0) {        if (argc == 5) {            dest = strtoul(argv[2],(char **)0,0);            src = strtoul(argv[3],(char **)0,0);            bytecnt = (long)strtoul(argv[4],(char **)0,0);            if (flashewrite(fbnk,(uchar *)dest,(uchar *)src,bytecnt) == -1) {                printf("ewrite failed\n");                ret = CMD_FAILURE;            }        }        else            ret = CMD_PARAM_ERROR;    }    else if (!strcmp(argv[1],"write")) {        if (argc == 5) {            dest = strtoul(argv[2],(char **)0,0);            src = strtoul(argv[3],(char **)0,0);            bytecnt = (long)strtoul(argv[4],(char **)0,0);#if 0            rslt = flashwrite(fbnk,(uchar *)dest,(uchar *)src,bytecnt);#else            rslt = AppFlashWrite((ulong *)dest,(ulong *)src,bytecnt);#endif            if (rslt == -1) {                printf("Write failed\n");                ret = CMD_FAILURE;            }        }        else            ret = CMD_PARAM_ERROR;    }    else if (!strcmp(argv[1],"opw")) {        if (getUsrLvl() != MAXUSRLEVEL)            printf("Must be user level %d\n",MAXUSRLEVEL);        else                FlashProtectWindow = 2;    }    else if (!strcmp(argv[1],"unprot")) {        if (argc != 3)            ret = CMD_PARAM_ERROR;        else            sectorProtect(argv[2],0);    }    else if (!strcmp(argv[1],"prot")) {        if (argc != 3)             ret = CMD_PARAM_ERROR;        else            sectorProtect(argv[2],1);    }    else if (!strcmp(argv[1],"erase")) {        if (argc != 3) {            ret = CMD_PARAM_ERROR;        }        else {            for(snum=fbnk->sectors[0].snum;snum<fbnk->sectorcnt;snum++) {                if (inRange(argv[2],snum)) {                    if (flasherase(fbnk,snum) == -1) {                        printf("Erase failed\n");                        ret = CMD_FAILURE;                        break;                    }                }            }        }    }#if FLASH_LOCK_SUPPORTED    else if ((!strcmp(argv[1],"lock")) || (!strcmp(argv[1],"unlock")) ||        (!strcmp(argv[1],"lockdwn"))) {        extern int flashlock(struct flashinfo *fbnk,int snum,int operation);        int operation, snum;        if (!strcmp(argv[1],"lock"))             operation = FLASH_LOCK;        else if (!strcmp(argv[1],"unlock"))             operation = FLASH_UNLOCK;        else            operation = FLASH_LOCKDWN;        if (argc != 3)            ret = CMD_PARAM_ERROR;        else {            for(snum=fbnk->sectors[0].snum;snum<fbnk->sectorcnt;snum++) {                if (inRange(argv[2],snum)) {                    if (flashlock(fbnk,snum,operation) == -1) {                        printf("Erase failed\n");                        ret = CMD_FAILURE;                        break;                    }                }            }        }    }#endif    else {        ret = CMD_PARAM_ERROR;    }    FLASH_INTSRESTORE(oints);    return(ret);}intNotUsed(){    printf("ERROR: flash operation not supported\n");    return(0);}#endif

⌨️ 快捷键说明

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