Пример #1
0
void dfu_protect_enable(void)
{
#ifdef DFU_SELF_PROTECT
	if ((FLASH_WRPR & 0x03) != 0x00) {
		flash_unlock();
		FLASH_CR = 0;
		flash_erase_option_bytes();
		flash_program_option_bytes(FLASH_OBP_RDP, FLASH_OBP_RDP_KEY);
		/* CL Device: Protect 2 bits with (2 * 2k pages each)*/
		/* MD Device: Protect 2 bits with (4 * 1k pages each)*/
		flash_program_option_bytes(FLASH_OBP_WRP10, 0x03FC);
	}
#endif
}
Пример #2
0
void memory_protect(void) {
#if MEMORY_PROTECT
  // Reference STM32F205 Flash programming manual revision 5
  // http://www.st.com/resource/en/programming_manual/cd00233952.pdf Section 2.6
  // Option bytes
  //                     set RDP level 2                   WRP for sectors 0 and
  //                     1            flash option control register matches
  if (((FLASH_OPTION_BYTES_1 & 0xFFEC) == 0xCCEC) &&
      ((FLASH_OPTION_BYTES_2 & 0xFFF) == 0xFFC) &&
      (FLASH_OPTCR == 0x0FFCCCED)) {
    return;  // already set up correctly - bail out
  }
  for (int i = FLASH_STORAGE_SECTOR_FIRST; i <= FLASH_STORAGE_SECTOR_LAST;
       i++) {
    flash_erase_sector(i, FLASH_CR_PROGRAM_X32);
  }
  flash_unlock_option_bytes();
  // Section 2.8.6 Flash option control register (FLASH_OPTCR)
  //   Bits 31:28 Reserved, must be kept cleared.
  //   Bits 27:16 nWRP: Not write protect: write protect bootloader code in
  //   flash main memory sectors 0 and 1 (Section 2.3; table 2) Bits 15:8 RDP:
  //   Read protect: level 2 chip read protection active Bits 7:5 USER: User
  //   option bytes: no reset on standby, no reset on stop, software watchdog
  //   Bit 4 Reserved, must be kept cleared.
  //   Bits 3:2 BOR_LEV: BOR reset Level: BOR off
  //   Bit 1 OPTSTRT: Option start: ignored by flash_program_option_bytes
  //   Bit 0 OPTLOCK: Option lock: ignored by flash_program_option_bytes
  flash_program_option_bytes(0x0FFCCCEC);
  flash_lock_option_bytes();
#endif
}
Пример #3
0
void dfu_protect_enable(void)
{
#ifdef DFU_SELF_PROTECT
	if ((FLASH_OPTCR & 0x10000) != 0) {
		flash_program_option_bytes(FLASH_OPTCR & ~0x10000);
		flash_lock_option_bytes();
	}
#endif
}
Пример #4
0
void memory_unlock(void) {
#ifndef EMULATOR
    // This exercises a bug in the STM32F2 that allows writing to read-only
    // sectors of flash.
    flash_unlock_option_bytes();
    
#ifdef DEBUG_ON
    // 0xFFFAAEC: remove wp from all sectors, no RDP (unless previously set to level 2 which is irreversible), 
    // disable configurable resets. Low order two bits are don't care.
    flash_program_option_bytes(0x0FFFAAEC);
#else
    // Even though level 2 is described as sticky, this chip has a proven bug related to this register so 
    // to be sure rewrite the level two value for RDP for non-debug builds.
    flash_program_option_bytes(0x0FFFCCEC);
#endif

    flash_lock_option_bytes();
#endif
}
Пример #5
0
void dfu_protect(dfu_mode_t mode)
{
    if (mode == DFU_MODE) {
#ifdef DFU_SELF_PROTECT
	if ((FLASH_WRPR & 0x03) != 0x00) {
		flash_unlock();
		FLASH_CR = 0;
		flash_erase_option_bytes();
		flash_program_option_bytes(FLASH_OBP_RDP, FLASH_OBP_RDP_KEY);
		/* CL Device: Protect 2 bits with (2 * 2k pages each)*/
		/* MD Device: Protect 2 bits with (4 * 1k pages each)*/
		flash_program_option_bytes(FLASH_OBP_WRP10, 0x03FC);
	}
#endif
    }
    else if (mode == UPD_MODE) {
		flash_unlock();
		FLASH_CR = 0;
		flash_erase_option_bytes();
    }
}
Пример #6
0
void stfub_protect_pages_and_reset(u8 mask)
{
	/*
	  Fixme what would happen if Vcc dissapears in
	  the middle of this code's execution
	*/

	struct option_bytes *ob = (struct option_bytes *)&_option_bytes_start;

	flash_unlock();
	flash_program_option_bytes((u32)&ob->wrp0, ob->wrp0 & (~mask));
	flash_lock();

	scb_reset_system();
}
Пример #7
0
/*
 * memory_protect() - Set option bytes for memory protection
 *
 * INPUT
 *     none
 * OUTPUT
 *     none
 */
void memory_protect(void)
{
#ifndef EMULATOR
    /*                     set RDP level 2                   WRP for sectors 0,5,6  */
    if((((*OPTION_BYTES_1) & 0xFFFF) == OPTION_RDP) && (((*OPTION_BYTES_2) & 0xFFFF) == OPTION_WRP))
    {
        return; // already set up correctly - bail out
    }

    flash_unlock_option_bytes();
    /*                              WRP +    RDP */

#if !defined(DEBUG_ON)  // safety check to make sure mem protect disabled in debug builds
    flash_program_option_bytes((uint32_t)OPTION_WRP << 16 | OPTION_RDP);  //RDP BLevel 2 (Irreversible)
#endif

    flash_lock_option_bytes();
#else
    printf("memory protect ON\n");
#endif
}
Пример #8
0
// Remove write-protection on all flash sectors.
//
// This is an undocumented feature/bug of STM32F205/F405 microcontrollers,
// where flash controller reads its write protection bits from FLASH_OPTCR
// register not from OPTION_BYTES, rendering write protection useless.
// This behaviour is fixed in future designs of flash controller used for
// example in STM32F427, where the protection bits are read correctly
// from OPTION_BYTES and not form FLASH_OPCTR register.
//
// Read protection is unaffected and always stays locked to the desired value.
void memory_write_unlock(void) {
  flash_unlock_option_bytes();
  flash_program_option_bytes(0x0FFFCCEC);
  flash_lock_option_bytes();
}