/* Read flash status register to determine if write protect is active */ int nvm_is_write_protected(void) { if (nvm_init() < 0) return -1; if (IS_ENABLED(CONFIG_CHROMEOS)) { u8 sr1; u8 wp_gpio; u8 wp_spi; /* Read Write Protect GPIO if available */ wp_gpio = get_write_protect_state(); /* Read Status Register 1 */ if (flash->status(flash, &sr1) < 0) { printk(BIOS_ERR, "Failed to read SPI status register 1\n"); return -1; } wp_spi = !!(sr1 & 0x80); printk(BIOS_DEBUG, "SPI flash protection: WPSW=%d SRP0=%d\n", wp_gpio, wp_spi); return wp_gpio && wp_spi; } return 0; }
int nvm_erase(void *start, size_t size) { if (nvm_init() < 0) return -1; flash->erase(flash, to_flash_offset(start), size); return 0; }
/* Write data to NVM. Returns 0 on success < 0 on error. */ int nvm_write(void *start, const void *data, size_t size) { if (nvm_init() < 0) return -1; flash->write(flash, to_flash_offset(start), size, data); return 0; }
/*! \brief Test routine that writes to the non volatile memory, reads back * and compares the values */ static status_code_t test_mem(mem_type_t mem, uint32_t test_address) { static uint8_t write_buf[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; static uint8_t read_buf[8], i = 0; /* Initialize the non volatile memory */ if (nvm_init(mem) != STATUS_OK) { return ERR_INVALID_ARG; } /* Write test pattern to the specified address */ nvm_write(mem, test_address, (void *)write_buf, sizeof(write_buf)); /* Read back data from the same address */ nvm_read(mem, test_address, (void *)read_buf, sizeof(read_buf)); /* Validate the read data */ for (i = 0; i < sizeof(write_buf); i++) { if (read_buf[i] != write_buf[i]) { return ERR_BAD_DATA; } } return STATUS_OK; }
/** * \brief Performs a memory check on the non volatile memory * * This function will simply test the output of the function * \ref nvm_init and returns an error in case of failure. * * \param test Current test case. */ static void run_memory_check_test(const struct test_case *test) { status_code_t status; status = nvm_init(INT_FLASH); test_assert_true(test, status == STATUS_OK, "Error memory check failed"); }
void AJ_NVRAM_Init() { nvm_init(INT_FLASH); nvm_get_page_size(INT_FLASH, &AJ_NVRAM_PageSize); if (*((uint32_t*)AJ_NVRAM_BASE_ADDRESS) != AJ_NV_SENTINEL) { AJ_AlwaysPrintf(("Sentinel has not been set, clearing NVRAM\n")); _AJ_NVRAM_Clear(); } }
/* Apply protection to a range of flash */ int nvm_protect(void *start, size_t size) { #if IS_ENABLED(CONFIG_MRC_SETTINGS_PROTECT) if (nvm_init() < 0) return -1; return spi_flash_protect(nvm_mmio_to_flash_offset(start), size); #else return -1; #endif }
retval_t pal_init(void) { #if (PAL_USE_SPI_TRX == 1) trx_spi_init(); #endif /* #if (PAL_USE_SPI_TRX = 1) */ #ifdef ENABLE_STACK_NVM #if (SAMD20) || (SAMD21) || (SAMR21) nvm_init(INT_FLASH); #endif #endif return MAC_SUCCESS; }
void sys_Init(void) { SystemInit(); #if CONFIG_DRIVER_WDT == 1 wdt_init(CONFIG_WDT_PERIOD); #endif time_Init(); #if CONFIG_DRIVER_LED == 1 led_Init(); #endif bsp_init(); #if (CONFIG_IAR_REDIRECT == 1) || (CONFIG_TASK_SHELL == 1) console_Init(); #endif #if (CONFIG_FLASH_NVM == 1) nvm_init(); #endif }
static int dlg_ChangeAutoSteps(const osd_command_t *cmd) { int result = -1; int steps = sm_GetAutoSteps(); switch(cmd->event){ case KEY_LEFT: steps --; result = sm_SetAutoSteps(steps); key_SetKeyScenario(DELAY_MS, REPEAT_MS); break; case KEY_RIGHT: steps ++; result = sm_SetAutoSteps(steps); key_SetKeyScenario(DELAY_MS, REPEAT_MS); break; case KEY_RESET: case KEY_MENU: /*get config from flash*/ nvm_init(); steps = sm_GetAutoSteps(); result = sm_SetAutoSteps(steps); break; case KEY_ENTER: case KEY_OSD: /*store config to flash*/ nvm_save(); break; default: steps = key_SetEntryAndGetDigit(); result = sm_SetAutoSteps(steps); break; } return result; }
int nvm_erase(void *start, size_t size) { if (nvm_init() < 0) return -1; return flash->erase(flash, nvm_mmio_to_flash_offset(start), size); }
int main(int argc, char *argv[]) { /* don't complain about unused variable */ (void)argc; if (argc > 1 && !strcmp(argv[1], "--write")){ /* opening the testing file */ fp = fopen("bytecode.nc", "wb"); /* writing version numbers */ BYTE major = NVM_VERSION_MAJOR; BYTE minor = NVM_VERSION_MINOR; BYTE patch = NVM_VERSION_PATCH; fwrite(&major, sizeof major, 1, fp); fwrite(&minor, sizeof minor, 1, fp); fwrite(&patch, sizeof patch, 1, fp); /* end */ void *parser = ParseAlloc(malloc); /* input: * * a = 7; * b = a - 3; */ TokenType token; token.s = "a"; Parse(parser, STRING, token); token.i = 0; Parse(parser, EQ, token); token.i = 7; Parse(parser, NUMBER, token); token.i = 0; Parse(parser, SEMICOLON, token); token.s = "b"; Parse(parser, STRING, token); token.i = 0; Parse(parser, EQ, token); token.s = "a"; Parse(parser, STRING, token); token.i = 0; Parse(parser, MINUS, token); token.i = 3; Parse(parser, NUMBER, token); token.i = 0; Parse(parser, SEMICOLON, token); /* finish parsing */ token.i = 0; Parse(parser, 0, token); fclose(fp); ParseFree(parser, free); } /* init the VM */ nvm_t *vm = nvm_init("bytecode.nc", malloc, free); if (!vm){ fprintf(stderr, "error with initializing the VM :C\n"); return 1; } /* validate the bytecode first */ if (nvm_validate(vm) != 0){ fprintf(stderr, "nvm: bytecode validation failed\n"); exit(1); } /* starts off the reading from file and executing the ops process */ nvm_blastoff(vm); /* print what's on the stack */ nvm_print_stack(vm); /* clean after yourself */ nvm_destroy(vm); return 0; }