summaryrefslogtreecommitdiffstats
path: root/firmware/common/devicedata.c
blob: 75fe79d7fa0b4d5a13f892f4e37d7207c26ebb6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2024 by William Wilgus
 *
 * 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 "devicedata.h"
#include "crc32.h"
#include <stddef.h>
#include <string.h>
#include "debug.h"

#ifndef BOOTLOADER
void verify_device_data(void) INIT_ATTR;
void verify_device_data(void)
{
    DEBUGF("%s", __func__);
    /* verify payload with checksum */
    uint32_t crc = crc_32(device_data.payload, device_data.length, 0xffffffff);
    if (crc == device_data.crc)
        return; /* return if data is valid */

    /* Write the default if data is invalid */
    memset(device_data.payload, 0xff, DEVICE_DATA_PAYLOAD_SIZE); /* Invalid data */
    device_data.length = DEVICE_DATA_PAYLOAD_SIZE;
    device_data.crc = crc_32(device_data.payload, device_data.length, 0xffffffff);

}

/******************************************************************************/
#endif /* ndef BOOTLOADER ******************************************************/
/******************************************************************************/

#if defined(HAVE_DEVICEDATA)
void  __attribute__((weak)) fill_devicedata(struct device_data_t *data)
{
    memset(data->payload, 0xff, data->length);
}
#endif

/* Write bootdata into location in FIRMWARE marked by magic header
 * Assumes buffer is already loaded with the firmware image
 * We just need to find the location and write data into the
 * payload region along with the crc for later verification and use.
 * Returns payload len on success,
 * On error returns false
 */
bool write_devicedata(unsigned char* buf, int len)
{
    int search_len = MIN(len, DEVICE_DATA_SEARCH_SIZE) - sizeof(struct device_data_t);

    /* search for decvice data header prior to search_len */
    for(int i = 0; i < search_len; i++)
    {
        struct device_data_t *data = (struct device_data_t *)&buf[i];
        if (data->magic[0] != DEVICE_DATA_MAGIC0 ||
            data->magic[1] != DEVICE_DATA_MAGIC1)
            continue;

        /* Ignore it if the length extends past the end of the buffer. */
        int data_len = offsetof(struct device_data_t, payload) + data->length;
        if (i + data_len > len)
            continue;

        fill_devicedata(data);

        /* Calculate payload CRC */
        data->crc = crc_32(data->payload, data->length, 0xffffffff);
        return true;
    }

    return false;
}