+static inline u32 get_u32(u32 addr)
+{
+ return *(u32*)addr;
+}
+
+static inline u16 get_u16(u32 addr)
+{
+ return *(u16*)addr;
+}
+
+static bool verify_firmware(void)
+{
+ u32 len = get_u32(BOOTLOADER_APP_START + HDR_LENGTH);
+ u16 flash_in_progress = get_u16(BOOTLOADER_APP_START + HDR_FLASH_IN_PROGRESS);
+
+ // FIXME: Should check if len is reasonable
+
+ crc_reset();
+ u32 crc = crc_calculate_block((u32 *)BOOTLOADER_APP_START, len/4);
+ u32 want_crc = get_u32(BOOTLOADER_APP_START + len);
+ DEBUG("DFU: fip=%04x crc=%08x/%08x len=%u\n", (uint) flash_in_progress, (uint) crc, (uint) want_crc, (uint) len);
+ if (flash_in_progress || crc != want_crc) {
+ DEBUG("DFU: Bad firmware\n");
+ return 0;
+ }
+
+ return 1;
+}
+