uint8_t flashrom_erase(uint8_t *addr) { uint8_t sec = iap_get_sector((uint32_t) addr); unsigned intstate; if (sec == INVALID_ADDRESS) { DEBUG("Invalid address\n"); return 0; } /* check sector */ if (!blank_check_sector(sec, sec)) { DEBUG("Sector already blank!\n"); return 1; } /* prepare sector */ if (prepare_sectors(sec, sec)) { DEBUG("-- ERROR: PREPARE_SECTOR_FOR_WRITE_OPERATION --\n"); return 0; } intstate = disableIRQ(); /* erase sector */ if (erase_sectors(sec, sec)) { DEBUG("-- ERROR: ERASE SECTOR --\n"); restoreIRQ(intstate); return 0; } restoreIRQ(intstate); /* check again */ if (blank_check_sector(sec, sec)) { DEBUG("-- ERROR: BLANK_CHECK_SECTOR\n"); return 0; } DEBUG("Sector successfully erased.\n"); return 1; }
/****************************************************************************** * P U B L I C F U N C T I O N S *****************************************************************************/ uint8_t flashrom_write(uint8_t *dst, char *src, size_t size) { char err; unsigned intstate; uint8_t sec; //buffer_vic = VICIntEnable; // save interrupt enable //VICIntEnClr = 0xFFFFFFFF; // clear vic sec = iap_get_sector((uint32_t) dst); if (sec == INVALID_ADDRESS) { DEBUG("Invalid address\n"); return 0; } /* check sector */ if(blank_check_sector(sec, sec) == SECTOR_NOT_BLANK) { DEBUG("Warning: Sector %i not blank\n", sec); } /* prepare sector */ err = prepare_sectors(sec, sec); if (err) { DEBUG("\n-- ERROR: PREPARE_SECTOR_FOR_WRITE_OPERATION: %u\n", err); /* set interrupts back and return */ // VICIntEnable = buffer_vic; return 0; } /* write flash */ else { intstate = disableIRQ(); err = copy_ram_to_flash((uint32_t) dst, (uint32_t) src, 256); restoreIRQ(intstate); if(err) { DEBUG("ERROR: COPY_RAM_TO_FLASH: %u\n", err); /* set interrupts back and return */ restoreIRQ(intstate); // VICIntEnable = buffer_vic; return 0; } /* check result */ else { err = compare((uint32_t) dst, (uint32_t) src, 256); if (err) { DEBUG("ERROR: COMPARE: %i (at position %u)\n", err, iap_result[1]); /* set interrupts back and return */ // VICIntEnable = buffer_vic; return 0; } else { DEBUG("Data successfully written!\n"); /* set interrupts back and return */ // VICIntEnable = buffer_vic; return 1; } } } }
/****************************************************************************** * P U B L I C F U N C T I O N S *****************************************************************************/ uint8_t flashrom_write(uint8_t *dst, const uint8_t *src, size_t size) { (void) size; /* unused */ char err; uint8_t sec; sec = iap_get_sector((uint32_t) dst); if (sec == INVALID_ADDRESS) { DEBUG("Invalid address\n"); return 0; } /* check sector */ if (blank_check_sector(sec, sec) == SECTOR_NOT_BLANK) { DEBUG("Warning: Sector %i not blank\n", sec); } /* prepare sector */ err = prepare_sectors(sec, sec); if (err) { DEBUG("\n-- ERROR: PREPARE_SECTOR_FOR_WRITE_OPERATION: %u\n", err); return 0; } /* write flash */ unsigned intstate = irq_disable(); err = copy_ram_to_flash((uint32_t) dst, (uint32_t) src, 256); irq_restore(intstate); if (err) { DEBUG("ERROR: COPY_RAM_TO_FLASH: %u\n", err); /* set interrupts back and return */ irq_restore(intstate); return 0; } /* check result */ err = compare((uint32_t) dst, (uint32_t) src, 256); if (err) { DEBUG("ERROR: COMPARE: %i (at position %u)\n", err, iap_result[1]); return 0; } DEBUG("Data successfully written!\n"); return 1; }
// Check if a sector is blank and if not, erase it // wipe_sector isn't working for unknown reasons // Doesn't return an error, just seems to freeze the MBED // uint16_t sector => sector id to wipe unsigned wipe_sector(uint16_t sector) { #if DEBUG==1 tty_writeln("Wiping sector"); #endif blank_check_sector(sector, sector); if(output[0] == SECTOR_NOT_BLANK) { prepare_sector_write(sector, sector); if (output[0] !=0) { write_error(output[0]); return output[0]; } erase_sector(sector, sector); if (output[0] !=0) { write_error(output[0]); return output[0]; } } #if DEBUG==1 tty_writeln("Sector wipe"); #endif return 0; }