📄 flashutilmgr.c
字号:
sscanf(buf2,"%s",buf); *val = buf; return OK; } }/********************************************************************************* freeFlashBuf - Free the buffer allocated by updateFlash in case we crash** This routine will free fuBuf if necessary. This can be used in case* updateFlash gets an error and leaves the buffer allocated.* If the buffer was alread freed, then nothing is done.*/void freeFlashBuf() { if (NULL != fuBuf) { free(fuBuf); fuBuf = NULL; } } /********************************************************************************* programFlash - Program the flash from a buffer** This routine updates the flash with the contents of a buffer, optionally* skipping the block containing the NVRam value. It displays a status* update during the programming process.** RETURNS: ERROR or OK*/STATUS programFlash(UINT8* buf, UINT32 flashStart, UINT32 flashEnd, BOOL skipNVRam, BOOL skipFUtil) { UINT32 NVOffset; UINT32 FUtil_start, FUtil_end; UINT32 addr, next_addr;#ifdef FLASH_ADRS if (skipNVRam) NVOffset = NVRAM_OFFSET; else#endif NVOffset = (UINT32)(-1);#ifdef FUTIL_ADRS if (skipFUtil) { FUtil_start = FUTIL_ADRS; FUtil_end = FUTIL_ADRS + FUTIL_LEN; } else#endif FUtil_start = FUtil_end = (UINT32)(-1); fprintf(fuFp,"Progress:\n"); for (addr = flashStart; addr < flashEnd; addr = next_addr) { fprintf(fuFp,"\r%3d%% ",(100 * addr) / (flashEnd - flashStart)); fflush(fuFp); next_addr = addr; flashNextBlock(&next_addr); if (addr == NVOffset) continue; if ((addr >= FUtil_start) && (addr < FUtil_end)) continue; if (OK != flashErase(addr)) { fprintf(fuFp,"Error erasing flash block %X\n",addr); return ERROR; } if (OK != flashWrite(CAST_FLASH (buf + addr - flashStart), next_addr - addr, addr, 0)) { fprintf(fuFp,"Error writing flash block %X\n",addr); return ERROR; } if (0 != bcmp((char*)(buf + addr - flashStart), (char*)(flashBase + addr), next_addr - addr)) { fprintf(fuFp,"Error: data miscompares after write, block = %X.\n",addr); /* return ERROR; */ } } /* end for addr */ fprintf(fuFp,"\r100%% ... done\n"); return OK;}/********************************************************************************* updateFlash - Enter a dialog with the user to update the flash** This routine enters into a dialog with the user over the serial port* to get enough information to update the flash image. It is assumed that* the routine is executing out of RAM.** It will mark the flash pages as writable, prompt the user (over the serial* port) for necessary information, read the specified file, sanity check* it, and then update the flash image.** RETURNS: ERROR or OK*/STATUS updateFlash() { UINT32 size; UINT32 flashStart, flashEnd, fileOffset; UINT32 nextBlock1, nextBlock2; BOOL skipNVRam, skipFUtil; char *fileName = ""; char *answer; STATUS status = OK; flashStart = 0; flashEnd = FLASH_TOTAL_SIZE; fileOffset = 0; fuFp = fopen(SERPORTNAME, "r+"); if (fuFp == NULL) { fprintf(stderr,"Can't open serial port: '%s'\n",SERPORTNAME); return ERROR; } fprintf(stderr, "Input and Output are being done on the serial port.\n" "Switch to the serial port to continue operation.\n"); /* Make sure all of flash is writable and valid */ MAKE_PAGES_WRITABLE(flashBase, flashBase + FLASH_TOTAL_SIZE); /* check for errors in flash utils */ nextBlock1 = 0; if (OK != flashNextBlock(&nextBlock1)) { fprintf(fuFp,"Error in flashNextBlock\n"); return ERROR; } fprintf(fuFp, "Note that reprogramming the flash is a risky endeavor.\n" "If the machine crashes halfway through, or if you load an\n" "invalid image, then the machine will not be bootable.\n" "In that event, you will need to remove the flash parts and\n" "reprogram them externally.\n\n" "Note also that all of the numeric parameters are considered\n" "hexadecimal, *without* the leading '0x'.\n\n" "Enter a period ('.') to abort this function.\n" "Enter a newline to accept the default.\n\n" ); while(1) { if (OK != fuPromptString("Filename", &fileName)) goto abort; fuBuf = fuReadFile(fileName, &size); if (fuBuf == NULL) continue; if (fuSanityCheck(fuBuf) != OK) { answer = "No"; if (OK != fuPromptString("This does not appear to be a valid ARM " "file, do you want to continue", &answer)) goto abort;; if ((*answer != 'y') && (*answer != 'Y')) { free(fuBuf); continue; } } while (1) { if (OK != fuPromptHex("Starting offset in flash", &flashStart)) goto abort; if (flashStart != 0) { nextBlock1 = flashStart; nextBlock2 = flashStart-1; flashNextBlock(&nextBlock1); flashNextBlock(&nextBlock2); if (nextBlock1 == nextBlock2) { fprintf(fuFp, "Starting offset %X does not begin on a flash block boundary.\n", flashStart); continue; } } if (OK != fuPromptHex("Ending offset in flash ", &flashEnd)) goto abort; nextBlock1 = flashEnd; nextBlock2 = flashEnd-1; flashNextBlock(&nextBlock1); flashNextBlock(&nextBlock2); if (nextBlock1 == nextBlock2) { fprintf(fuFp, "Ending offset %X does not begin on a flash block boundary.\n", flashEnd); continue; } if (OK != fuPromptHex("Starting offset in file ", &fileOffset)) goto abort; if (fileOffset & 3) { fprintf(fuFp,"File offset %X is not longword aligned.\n", fileOffset); continue; } if (fileOffset + (flashEnd - flashStart) > FLASH_TOTAL_SIZE) { fprintf(fuFp,"Area specified extends beyond end of buffer.\n"); continue; }#ifdef FUTIL_ADRS if ((flashStart < FUTIL_ADRS + FUTIL_LEN) && (flashEnd > FUTIL_ADRS)) { answer = "Yes"; if (OK != fuPromptString("Do you wish to skip the FUtil block", &answer)) goto abort; skipFUtil = ((*answer == 'y') || (*answer == 'Y')); } else#endif skipFUtil = FALSE;#ifdef FLASH_ADRS if ((flashStart <= NVRAM_OFFSET) && (flashEnd > NVRAM_OFFSET)) { answer = "Yes"; if (OK != fuPromptString("Do you wish to skip the NVRAM block", &answer)) goto abort; skipNVRam = ((*answer == 'y') || (*answer == 'Y')); } else#endif skipNVRam = FALSE; if (programFlash(fuBuf + fileOffset, flashStart, flashEnd, skipNVRam, skipFUtil) == ERROR) { status = ERROR; break; } if ((flashStart == 0) && (flashEnd == FLASH_TOTAL_SIZE)) break; answer = "No"; if (OK != fuPromptString("Do you want to program other offsets", &answer)) goto abort; if ((*answer == 'y') || (*answer == 'Y')) continue; break; } /* while (1): loop for programming offsets */ break; } /* while (1): main loop */abort: if (fuBuf != NULL) { free(fuBuf); fuBuf = NULL; } if (fuFp != NULL) { fclose(fuFp); fuFp = NULL; } return status; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -