int BCMINITFN(nvram_commit_internal)(bool nvram_corrupt) { struct nvram_header *header; int ret; uint32 *src, *dst; uint i; if (!(header = (struct nvram_header *) MALLOC(NULL, MAX_NVRAM_SPACE))) { printf("nvram_commit: out of memory\n"); return -12; /* -ENOMEM */ } NVRAM_LOCK(); /* Regenerate NVRAM */ ret = _nvram_commit(header); if (ret) goto done; src = (uint32 *) &header[1]; dst = src; for (i = sizeof(struct nvram_header); i < header->len && i < MAX_NVRAM_SPACE; i += 4) *dst++ = htol32(*src++); #ifdef _CFE_ if ((ret = cfe_open(flashdrv_nvram)) >= 0) { if (nvram_corrupt) { printf("Corrupting NVRAM...\n"); header->magic = NVRAM_INVALID_MAGIC; } cfe_writeblk(ret, 0, (unsigned char *) header, header->len); cfe_close(ret); } #else if (sysFlashInit(NULL) == 0) { /* set/write invalid MAGIC # (in case writing image fails/is interrupted) * write the NVRAM image to flash(with invalid magic) * set/write valid MAGIC # */ header->magic = NVRAM_CLEAR_MAGIC; nvWriteChars((unsigned char *)&header->magic, sizeof(header->magic)); header->magic = NVRAM_INVALID_MAGIC; nvWrite((unsigned short *) header, MAX_NVRAM_SPACE); header->magic = NVRAM_MAGIC; nvWriteChars((unsigned char *)&header->magic, sizeof(header->magic)); } #endif /* ifdef _CFE_ */ done: NVRAM_UNLOCK(); MFREE(NULL, header, MAX_NVRAM_SPACE); return ret; }
int nvram_commit(void) { struct nvram_header *header; int ret; uint32 *src, *dst; uint i; uint32 err_addr; if (!(header = (struct nvram_header *) MALLOC(NULL, NVRAM_SPACE))) { printf("nvram_commit: out of memory\n"); return -12; } NVRAM_LOCK(); ret = _nvram_commit(header); if (ret) goto done; src = (uint32 *) &header[1]; dst = src; for (i = sizeof(struct nvram_header); i < header->len && i < NVRAM_SPACE; i += 4) *dst++ = htol32(*src++); if (sysFlashInit(NULL) == 0) { /* set/write invalid MAGIC # (in case writing image fails/is interrupted) * write the NVRAM image to flash(with invalid magic) * set/write valid MAGIC # */ header->magic = NVRAM_CLEAR_MAGIC; nvWriteChars((unsigned char *)&header->magic, sizeof(header->magic)); header->magic = NVRAM_INVALID_MAGIC; nvWrite((unsigned short *) header, NVRAM_SPACE); header->magic = NVRAM_MAGIC; nvWriteChars((unsigned char *)&header->magic, sizeof(header->magic)); } done: NVRAM_UNLOCK(); MFREE(NULL, header, NVRAM_SPACE); return ret; }
int nvram_commit(void) { struct nvram_header *header; int ret; uint32 *src, *dst; uint i; if (!(header = (struct nvram_header *) MALLOC(NVRAM_SPACE))) { printf("nvram_commit: out of memory\n"); return -12; /* -ENOMEM */ } NVRAM_LOCK(); /* Regenerate NVRAM */ ret = _nvram_commit(header); if (ret) goto done; src = (uint32 *) &header[1]; dst = src; for (i = sizeof(struct nvram_header); i < header->len && i < NVRAM_SPACE; i += 4) *dst++ = htol32(*src++); #ifdef ASUS #else #ifdef _CFE_ if ((ret = cfe_open("flash0.nvram")) >= 0) { cfe_writeblk(ret, 0, (unsigned char *) header, NVRAM_SPACE); cfe_close(ret); } #else if (flash_init((void *) FLASH_BASE, NULL) == 0) nvWrite((unsigned short *) header, NVRAM_SPACE); #endif #endif done: NVRAM_UNLOCK(); MFREE(header, NVRAM_SPACE); return ret; }
int main(int argc, char* argv[]) { int opt; char type[100] = "local"; char hostName[200] = DEFAULT_HOSTNAME; int port = DEFAULT_RESMGR_TPM_PORT; int returnVal = 0; struct option sOpts[] = { { "index" , required_argument, NULL, 'x' }, { "authHandle" , required_argument, NULL, 'a' }, { "file" , required_argument, NULL, 'f' }, { "handlePasswd", required_argument, NULL, 'P' }, { "passwdInHex" , no_argument, NULL, 'X' }, { "port" , required_argument, NULL, 'p' }, { "dbg" , required_argument, NULL, 'd' }, { "help" , no_argument, NULL, 'h' }, { "version" , no_argument, NULL, 'v' }, { NULL , no_argument, NULL, 0 }, }; if(argc == 1) { showHelp(argv[0]); return 0; } if( argc > (int)(2*sizeof(sOpts)/sizeof(struct option)) ) { showArgMismatch(argv[0]); return -1; } while ( ( opt = getopt_long( argc, argv, "x:a:f:P:Xp:d:hv", sOpts, NULL ) ) != -1 ) { switch ( opt ) { case 'h': case '?': showHelp(argv[0]); return 0; case 'v': showVersion(argv[0]); return 0; case 'x': if( getSizeUint32Hex(optarg, &nvIndex) != 0 ) { return -2; } break; case 'a': if( getSizeUint32Hex(optarg, &authHandle) != 0 ) { return -3; } break; case 'P': if( optarg == NULL || (strlen(optarg) >= sizeof(TPMU_HA)) ) { printf("\nPlease input the handle password(optional,no more than %d characters).\n", (int)sizeof(TPMU_HA)-1); return -4; } safeStrNCpy(&handlePasswd[0], optarg, sizeof(handlePasswd)); break; case 'f': if( optarg == NULL ) { printf("\nPlease input the nv data file.\n"); return -5; } safeStrNCpy(&fileName[0], optarg, sizeof(fileName)); break; case 'X': hexPasswd = true; break; case 'p': if( getPort(optarg, &port) ) { printf("Incorrect port number.\n"); return -6; } break; case 'd': if( getDebugLevel(optarg, &debugLevel) ) { printf("Incorrect debug level.\n"); return -7; } break; default: showArgMismatch(argv[0]); return -8; } } if( nvIndex == 0 ) { printf("You must provide an index (!= 0) for the NVRAM area.\n"); return -9; } if( authHandle == 0 ) { printf("You must provide an right auth handle for this operation.\n"); return -10; } dataSize = MAX_NV_INDEX_SIZE; if(loadDataFromFile(fileName, nvBuffer, &dataSize)) { printf("Failed to read data from %s\n", fileName); return -11; } prepareTest(hostName, port, debugLevel); returnVal = nvWrite(); finishTest(); if(returnVal) return -12; return 0; }