/* verify firmware for a rev3 bootloader */ bool AP_IOMCU::verify_rev3(uint32_t fw_size_local) { bool ret; uint32_t sum = 0; uint32_t bytes_read = 0; uint32_t crc = 0; uint32_t fw_size_remote; uint8_t fill_blank = 0xff; debug("verify..."); ret = get_info(INFO_FLASH_SIZE, fw_size_remote); send(PROTO_EOC); if (!ret) { debug("could not read firmware size"); return ret; } /* read through the firmware file again and calculate the checksum */ while (bytes_read < fw_size_local) { uint32_t n = fw_size_local - bytes_read; if (n > 4) { n = 4; } /* calculate crc32 sum */ sum = crc_crc32(sum, &fw[bytes_read], n); bytes_read += n; } /* fill the rest with 0xff */ while (bytes_read < fw_size_remote) { sum = crc_crc32(sum, &fill_blank, 1); bytes_read++; } /* request CRC from IO */ send(PROTO_GET_CRC); send(PROTO_EOC); ret = recv_bytes((uint8_t *)(&crc), sizeof(crc)); if (!ret) { debug("did not receive CRC checksum"); return ret; } /* compare the CRC sum from the IO with the one calculated */ if (sum != crc) { debug("CRC wrong: received: 0x%x, expected: 0x%x", (unsigned)crc, (unsigned)sum); return false; } crc_is_ok = true; return true; }
/* verify firmware for a rev3 bootloader */ bool AP_IOMCU::verify_rev3(uint32_t fw_size_local) { bool ret; uint32_t sum = 0; uint32_t crc = 0; uint32_t fw_size_remote; const uint8_t fill_blank = 0xff; debug("verify..."); ret = get_info(INFO_FLASH_SIZE, fw_size_remote); send(PROTO_EOC); if (!ret) { debug("could not read firmware size"); return ret; } sum = crc_crc32(0, fw, fw_size_local); /* fill the rest of CRC with 0xff */ for (uint32_t i=0; i<fw_size_remote - fw_size_local; i++) { sum = crc_crc32(sum, &fill_blank, 1); } /* request CRC from IO */ send(PROTO_GET_CRC); send(PROTO_EOC); ret = recv_bytes((uint8_t *)(&crc), sizeof(crc)); if (!ret) { debug("did not receive CRC checksum"); return ret; } /* compare the CRC sum from the IO with the one calculated */ if (sum != crc) { debug("CRC wrong: received: 0x%x, expected: 0x%x", (unsigned)crc, (unsigned)sum); return false; } crc_is_ok = true; return true; }
void AP_IOMCU_FW::calculate_fw_crc(void) { #define APP_SIZE_MAX 0xf000 #define APP_LOAD_ADDRESS 0x08001000 // compute CRC of the current firmware uint32_t sum = 0; for (unsigned p = 0; p < APP_SIZE_MAX; p += 4) { uint32_t bytes = *(uint32_t *)(p + APP_LOAD_ADDRESS); sum = crc_crc32(sum, (const uint8_t *)&bytes, sizeof(bytes)); } reg_setup.crc[0] = sum & 0xFFFF; reg_setup.crc[1] = sum >> 16; }