Documentation: Update copyrights to 2019.
[pub/lufa.git] / Bootloaders / DFU / BootloaderAPI.c
index d6d1847..21d13bb 100644 (file)
@@ -1,13 +1,13 @@
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2016.
+     Copyright (C) Dean Camera, 2019.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2016  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2019  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
 
 #include "BootloaderAPI.h"
 
+static bool IsPageAddressValid(const uint32_t Address)
+{
+       /* Determine if the given page address is correctly aligned to the
+          start of a flash page. */
+       bool PageAddressIsAligned = !(Address & (SPM_PAGESIZE - 1));
+
+       return (Address < BOOT_START_ADDR) && PageAddressIsAligned;
+}
+
 void BootloaderAPI_ErasePage(const uint32_t Address)
 {
-       boot_page_erase_safe(Address);
-       boot_spm_busy_wait();
-       boot_rww_enable();
+       if (! IsPageAddressValid(Address))
+               return;
+
+       ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
+       {
+               boot_page_erase_safe(Address);
+               boot_spm_busy_wait();
+               boot_rww_enable();
+       }
 }
 
 void BootloaderAPI_WritePage(const uint32_t Address)
 {
-       boot_page_write_safe(Address);
-       boot_spm_busy_wait();
-       boot_rww_enable();
+       if (! IsPageAddressValid(Address))
+               return;
+
+       ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
+       {
+               boot_page_write_safe(Address);
+               boot_spm_busy_wait();
+               boot_rww_enable();
+       }
 }
 
 void BootloaderAPI_FillWord(const uint32_t Address, const uint16_t Word)
@@ -71,6 +92,8 @@ uint8_t BootloaderAPI_ReadLock(void)
 
 void BootloaderAPI_WriteLock(const uint8_t LockBits)
 {
-       boot_lock_bits_set_safe(LockBits);
+       ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
+       {
+               boot_lock_bits_set_safe(LockBits);
+       }
 }
-