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
This commit is contained in:
James Buren 2020-11-21 17:58:01 +00:00 committed by William Wilgus
parent 2eb191a3f4
commit 6b3b4df6f6
4 changed files with 47 additions and 30 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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 <stdint.h>
#include <stdbool.h>
#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