void AJ_NVRAM_Init() { AJ_NVRAM_BASE_ADDRESS = AJ_EMULATED_NVRAM; _AJ_LoadNVFromFile(); if (*((uint32_t*)AJ_NVRAM_BASE_ADDRESS) != AJ_NV_SENTINEL) { AJ_NVRAM_Clear(); _AJ_StoreNVToFile(); } }
int AJ_Main() { AJ_Status status = AJ_OK; AJ_Printf("AJ_Main 1\n"); AJ_Initialize(); AJ_NVRAM_Clear(); AJ_Printf("Clearing NVRAM\n"); AJ_Printf("AJ_Main 2\n"); status = TestNVRAM(); AJ_Printf("AJ_Main 3\n"); AJ_ASSERT(status == AJ_OK); status = TestCreds(); AJ_ASSERT(status == AJ_OK); return 0; }
int AJ_Main_nvramtest() { AJ_Status status = AJ_OK; static uint16_t oRand = 0; while (status == AJ_OK) { AJ_AlwaysPrintf(("AJ Initialize\n")); AJ_Initialize(); #ifdef OBS_ONLY AJ_RandBytes(&oRand, sizeof(oRand)); AJ_InfoPrintf(("BEGIN OBSWRITE TEST\n")); status = testObsWrite(); if (oRand % 2 == 0) { AJ_InfoPrintf(("CALLING REBOOT WITHOUT REWRITING TO 0")); #ifdef READABLE_LOG AJ_Sleep(1500); #endif AJ_Reboot(); } AJ_InfoPrintf(("REWRITE OBS TO 0 AND READ TEST\n")); #ifdef READABLE_LOG AJ_Sleep(1500); #endif status = TestNvramDelete(); AJ_Reboot(); #endif AJ_NVRAM_Clear(); AJ_AlwaysPrintf(("TEST LOCAL AND REMOTE CREDS\n")); status = TestCreds(); AJ_ASSERT(status == AJ_OK); #ifdef READABLE_LOG AJ_Sleep(1500); #endif AJ_AlwaysPrintf(("AJ_Main 2\n")); status = TestNVRAM(); #ifdef READABLE_LOG AJ_Sleep(1500); #endif #ifdef RAND_DATA Randomizer(); #endif AJ_InfoPrintf(("\nBEGIN GUID EXIST TEST\n")); status = TestExist(); #ifdef READABLE_LOG AJ_Sleep(1500); #endif AJ_InfoPrintf(("\nBEGIN OBSWRITE TEST\n")); status = TestObsWrite(); #ifdef READABLE_LOG AJ_Sleep(1500); #endif AJ_InfoPrintf(("\nOBSWRITE STATUS %u, BEGIN WRITE TEST\n", status)); #ifdef READABLE_LOG AJ_Sleep(1500); #endif status = TestNvramWrite(); AJ_InfoPrintf(("\nWRITE STATUS %u, BEGIN READ TEST\n", status)); #ifdef READABLE_LOG AJ_Sleep(1500); #endif status = TestNvramRead(); AJ_InfoPrintf(("\nREAD STATUS %u, BEGIN DELETE TEST\n", status)); #ifdef READABLE_LOG AJ_Sleep(1500); #endif status = TestNvramDelete(); AJ_InfoPrintf(("\nDONE\n")); AJ_AlwaysPrintf(("AJ_Main 3\n")); AJ_ASSERT(status == AJ_OK); AJ_InfoPrintf(("\nDELETE STATUS %u, NVRAMTEST RUN %u TIMES\n", status, count++)); #ifdef READABLE_LOG AJ_Sleep(3000); #endif status = TestECCCreds(); AJ_InfoPrintf(("\nECC STATUS %u, NVRAMTEST RUN %u TIMES\n", status, count++)); AJ_ASSERT(status == AJ_OK); } return 0; }
AJ_Status TestNvramDelete() { AJ_Status status = AJ_OK; AJ_NV_DATASET* nvramHandle; if (tid1 % 2 == 1) { #ifndef OBS_ONLY if (AJ_NVRAM_Exist(tid1)) { AJ_ASSERT(AJ_NVRAM_Delete(tid1) == AJ_OK); } AJ_InfoPrintf(("LAYOUT AFTER DELETE 1\n")); AJ_NVRAM_Layout_Print(); if (AJ_NVRAM_Exist(tid2)) { AJ_ASSERT(AJ_NVRAM_Delete(tid2) == AJ_OK); } AJ_InfoPrintf(("LAYOUT AFTER DELETE 2\n")); AJ_NVRAM_Layout_Print(); if (AJ_NVRAM_Exist(tid3)) { AJ_ASSERT(AJ_NVRAM_Delete(tid3) == AJ_OK); } AJ_InfoPrintf(("LAYOUT AFTER DELETE 3\n")); AJ_NVRAM_Layout_Print(); if (AJ_NVRAM_Exist(tid4)) { AJ_ASSERT(AJ_NVRAM_Delete(tid4) == AJ_OK); } AJ_InfoPrintf(("LAYOUT AFTER DELETE 4\n")); AJ_NVRAM_Layout_Print(); #endif if (AJ_NVRAM_Exist(AJ_NVRAM_ID_FOR_APPS)) { AJOBS_Info_Test emptyInfo; AJ_Status status = AJ_OK; size_t size = sizeof(AJOBS_Info_Test); memset(&emptyInfo, 0, sizeof(emptyInfo)); AJ_AlwaysPrintf(("Going to write Info values: state=%d, ssid=%s authType=%d pc=%s\n", emptyInfo.state, emptyInfo.ssid, emptyInfo.authType, emptyInfo.pc)); //AJ_NV_DATASET* nvramHandle = AJ_NVRAM_Open(AJ_NVRAM_ID_CREDS_MAX + 1, "w", size); //PROPERTY STORE DEVICE ID nvramHandle = AJ_NVRAM_Open(AJ_NVRAM_ID_FOR_APPS, "w", size); if (nvramHandle != NULL) { int sizeWritten = AJ_NVRAM_Write(&emptyInfo, size, nvramHandle); status = AJ_NVRAM_Close(nvramHandle); if (sizeWritten != size) { status = AJ_ERR_WRITE; goto _TEST_NVRAM_DELETE_EXIT; } } //nvramHandle = AJ_NVRAM_Open(AJ_NVRAM_ID_CREDS_MAX + 1, "r", 0); //PROPERTY STORE DEVICE ID nvramHandle = AJ_NVRAM_Open(AJ_NVRAM_ID_FOR_APPS, "r", 0); if (nvramHandle != NULL) { int sizeRead = AJ_NVRAM_Read(&emptyInfo, size, nvramHandle); status = AJ_NVRAM_Close(nvramHandle); if (sizeRead != sizeRead) { status = AJ_ERR_READ; } else { AJ_AlwaysPrintf(("Read Info values: state=%d, ssid=%s authType=%d pc=%s\n", emptyInfo.state, emptyInfo.ssid, emptyInfo.authType, emptyInfo.pc)); } } } AJ_InfoPrintf(("LAYOUT AFTER DELETE OBS\n")); AJ_NVRAM_Layout_Print(); } else { AJ_NVRAM_Clear(); AJ_InfoPrintf(("LAYOUT AFTER CLEAR ALL\n")); AJ_NVRAM_Layout_Print(); } return status; _TEST_NVRAM_DELETE_EXIT: AJ_NVRAM_Close(nvramHandle); return status; }
AJ_Status CreateTrailOfBreadcrumbs(void) { uint16_t minNvramSpaceNeeded; uint16_t currentAvailableNvramSpace; uint16_t someNvramId = 0; AJ_NV_DATASET* someDataHandle = NULL; uint8_t sizeOfEachSlice; uint16_t i; size_t numBytesExpectingToWrite; size_t numBytesActuallyWritten; /* * Test program would write (place breadcrumbs) over the NVRAM, anyway. */ AJ_NVRAM_Clear(); currentAvailableNvramSpace = AJ_NVRAM_GetSizeRemaining(); /* * At minimum, the test needs to store: * a. The message itself * b. The number of breadcrumbs in the trail (the mininum value is 1) * (this is essentially the value held by lengthOfBreadcrumbTrail) */ minNvramSpaceNeeded = (estimatedOverheadPerNvramItem + sizeof(sensumManifestum)) + (estimatedOverheadPerNvramItem + sizeof(lengthOfBreadcrumbTrail)); if (currentAvailableNvramSpace < minNvramSpaceNeeded) { AJ_Printf("ERROR: Available NVRAM space (%u bytes) is less than needed (%u bytes).\n", currentAvailableNvramSpace, minNvramSpaceNeeded); return AJ_ERR_RESOURCES; } /* * Any remaining space can be used to add more breadcrumbs. * max_num_bread_crumbs = nvram_size_available / size_occupied_by_each_crumb * * size_occupied_by_each_crumb = estimatedOverheadPerNvramItem + sizeof(id) */ lengthOfBreadcrumbTrail = (currentAvailableNvramSpace - minNvramSpaceNeeded) / (estimatedOverheadPerNvramItem + sizeof(someNvramId)); /* * Create the trail of bread crumbs starting at smId * * Generate a random list of nvram ids, lengthOfBreadcrumbTrail number of * elements. The list should not have any duplicates and should not include * marker ids viz. smId and countId. * * This is necessary to ensure that the trail of breadcrumbs is without * any loops. The simplest way to generate a list of unique nvram ids * would be to divide the available space into equal slices and generate * one id from each slice. * * The starting id is AJ_NVRAM_ID_APPS_BEGIN and the ending id is 0xFFFF. * * There are a total of (lengthOfBreadcrumbTrail + 1) items to * go through, including the starting point smId. */ sizeOfEachSlice = (0xFFFF - AJ_NVRAM_ID_APPS_BEGIN) / lengthOfBreadcrumbTrail; /* The starting point has to be the constant marker, smId */ someNvramId = smId; for (i = 0; i < lengthOfBreadcrumbTrail + 1; i++) { uint8_t randByte; uint16_t startId; uint16_t nextId; void* pointerToData; AJ_RandBytes(&randByte, sizeof(randByte)); startId = AJ_NVRAM_ID_APPS_BEGIN + sizeOfEachSlice * i; nextId = startId + randByte % sizeOfEachSlice; /* Ensure uniqueness of id - no conflicts with well-known markers */ if (smId == nextId || countId == nextId) { nextId += (0 == i % 2) ? -1 : 1; } numBytesExpectingToWrite = (lengthOfBreadcrumbTrail != i) ? sizeof(nextId) : sizeof(sensumManifestum); currentAvailableNvramSpace = AJ_NVRAM_GetSizeRemaining(); someDataHandle = AJ_NVRAM_Open(someNvramId, AJTestWriteMode, numBytesExpectingToWrite); if (NULL == someDataHandle) { /* Cannot proceed any further due to failed breadcrumb access */ return AJ_ERR_NVRAM_WRITE; } pointerToData = (lengthOfBreadcrumbTrail != i) ? (void*) (&nextId) : (void*) sensumManifestum; numBytesActuallyWritten = AJ_NVRAM_Write(pointerToData, numBytesExpectingToWrite, someDataHandle); /* done writing the data, can close the handle */ if (AJ_OK != AJ_NVRAM_Close(someDataHandle)) { AJ_Printf("WARN: For id: %u, AJ_NVRAM_Close did NOT return %s (code: %u)\n", someNvramId, AJ_StatusText(AJ_OK), AJ_OK); } if (AJTestNvramWriteFailure == numBytesActuallyWritten || numBytesExpectingToWrite != numBytesActuallyWritten) { /* Cannot proceed any further due to breadcrumb write failure */ return AJ_ERR_NVRAM_WRITE; } /* * The write has been successful. * * Check whether estimatedOverheadPerNvramItem (rough estimate) is * accurate. Overestimating estimatedOverheadPerNvramItem is fine * (erring on the side on caution). */ if (estimatedOverheadPerNvramItem < currentAvailableNvramSpace - AJ_NVRAM_GetSizeRemaining() - numBytesExpectingToWrite) { AJ_Printf("ERROR: The estimated overhead per NVRAM item (%u bytes) is not accurate. It needs to be increased.\n", estimatedOverheadPerNvramItem); return AJ_ERR_FAILURE; } /* Move to the next breadcrumb */ someNvramId = nextId; } /* * All the items are written. * Write the value of lengthOfBreadcrumbTrail */ someDataHandle = AJ_NVRAM_Open(countId, AJTestWriteMode, sizeof(lengthOfBreadcrumbTrail)); if (NULL == someDataHandle) { return AJ_ERR_NVRAM_WRITE; } numBytesExpectingToWrite = sizeof(lengthOfBreadcrumbTrail); numBytesActuallyWritten = AJ_NVRAM_Write((void*)&lengthOfBreadcrumbTrail, numBytesExpectingToWrite, someDataHandle); /* done writing the data, can close the handle */ if (AJ_OK != AJ_NVRAM_Close(someDataHandle)) { AJ_Printf("WARN: For id: %u, AJ_NVRAM_Close did NOT return %s (code: %u)\n", countId, AJ_StatusText(AJ_OK), AJ_OK); } if (AJTestNvramWriteFailure == numBytesActuallyWritten || numBytesExpectingToWrite != numBytesActuallyWritten) { return AJ_ERR_NVRAM_WRITE; } return AJ_OK; }