From 6b3b4df6f64f1523b76d5f4f26c568ffc208a899 Mon Sep 17 00:00:00 2001 From: James Buren Date: Sat, 21 Nov 2020 17:58:01 +0000 Subject: [PATCH] rockbox: move firmware checksum algorithms into a common header Before this was just implemented inline wherever it was needed. Now it is provided as 2 inline functions in a header called checksum.h. There should be no differences in actual behavior. Change-Id: I5d756cc01dc6225f5cc8b6af90911a3fc7b57cd5 --- apps/plugins/crypt_firmware.c | 14 +----------- apps/plugins/iriver_flash.c | 9 ++------ firmware/common/rb-loader.c | 13 +++-------- firmware/include/checksum.h | 41 +++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 firmware/include/checksum.h diff --git a/apps/plugins/crypt_firmware.c b/apps/plugins/crypt_firmware.c index f9bdd4d031..14e9c2be96 100644 --- a/apps/plugins/crypt_firmware.c +++ b/apps/plugins/crypt_firmware.c @@ -36,8 +36,7 @@ */ #include "plugin.h" - - +#include "checksum.h" static void aes_encrypt(void* data, uint32_t size) { @@ -133,17 +132,6 @@ static void put_uint32be(unsigned char* buf, uint32_t x) buf[3] = x & 0xff; } -static uint32_t calc_checksum(uint32_t sum, unsigned char* buf, int len) -{ - int i; - - for (i = 0; i < len ; i++) { - sum += buf[i]; - } - - return sum; -} - enum plugin_status plugin_start(const void* parameter) { int fd; diff --git a/apps/plugins/iriver_flash.c b/apps/plugins/iriver_flash.c index 9454ac63c8..152ac339ff 100644 --- a/apps/plugins/iriver_flash.c +++ b/apps/plugins/iriver_flash.c @@ -23,6 +23,7 @@ ****************************************************************************/ #include "plugin.h" #include "lib/helper.h" +#include "checksum.h" /* * Flash commands may rely on null pointer dereferences to work correctly. @@ -395,7 +396,6 @@ static bool load_firmware(const char* filename, enum firmware firmware, { uint32_t checksum; uint8_t model[4]; - uint32_t sum; /* subtract the header length */ fd_len -= sizeof(checksum) + sizeof(model); @@ -418,13 +418,8 @@ static bool load_firmware(const char* filename, enum firmware firmware, goto bail; } - /* calculate the checksum */ - sum = MODEL_NUMBER; - for (off_t i = 0; i < fd_len; i++) - sum += buffer[i]; - /* verify the checksum */ - if (sum != checksum) + if (!verify_checksum(checksum, buffer, fd_len)) { msg = "Aborting: checksum mismatch"; goto bail; diff --git a/firmware/common/rb-loader.c b/firmware/common/rb-loader.c index 1ded8c2335..300ba55401 100644 --- a/firmware/common/rb-loader.c +++ b/firmware/common/rb-loader.c @@ -24,6 +24,7 @@ #include "system.h" #include "file.h" #include "loader_strerror.h" +#include "checksum.h" #if defined(HAVE_BOOTDATA) #include "bootdata.h" @@ -129,8 +130,7 @@ static int load_firmware_filename(unsigned char* buf, int buffer_size) { int len; - unsigned long chksum, sum; - int i; + unsigned long chksum; int ret; int fd = open(filename, O_RDONLY); @@ -162,14 +162,7 @@ static int load_firmware_filename(unsigned char* buf, goto end; } - sum = MODEL_NUMBER; - - for(i = 0;i < len;i++) - { - sum += buf[i]; - } - - if (sum != chksum) + if (!verify_checksum(chksum, buf, len)) { ret = EBAD_CHKSUM; goto end; diff --git a/firmware/include/checksum.h b/firmware/include/checksum.h new file mode 100644 index 0000000000..6e66368bcf --- /dev/null +++ b/firmware/include/checksum.h @@ -0,0 +1,41 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2020 James Buren + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include +#include + +#ifndef _CHECKSUM_H +#define _CHECKSUM_H + +/* rockbox firmware checksum algorithm */ +static inline uint32_t calc_checksum(uint32_t sum, const uint8_t* buf, size_t len) +{ + for (size_t i = 0; i < len; i++) + sum += buf[i]; + return sum; +} + +/* similar to above but only used for verification */ +static inline bool verify_checksum(uint32_t cs, const uint8_t* buf, size_t len) +{ + return (calc_checksum(MODEL_NUMBER, buf, len) == cs); +} + +#endif