summaryrefslogtreecommitdiff
path: root/firmware/common
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/common')
-rw-r--r--firmware/common/devicedata.c88
-rw-r--r--firmware/common/rb-loader.c8
2 files changed, 95 insertions, 1 deletions
diff --git a/firmware/common/devicedata.c b/firmware/common/devicedata.c
new file mode 100644
index 0000000000..75fe79d7fa
--- /dev/null
+++ b/firmware/common/devicedata.c
@@ -0,0 +1,88 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2024 by William Wilgus
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21#include "devicedata.h"
22#include "crc32.h"
23#include <stddef.h>
24#include <string.h>
25#include "debug.h"
26
27#ifndef BOOTLOADER
28void verify_device_data(void) INIT_ATTR;
29void verify_device_data(void)
30{
31 DEBUGF("%s", __func__);
32 /* verify payload with checksum */
33 uint32_t crc = crc_32(device_data.payload, device_data.length, 0xffffffff);
34 if (crc == device_data.crc)
35 return; /* return if data is valid */
36
37 /* Write the default if data is invalid */
38 memset(device_data.payload, 0xff, DEVICE_DATA_PAYLOAD_SIZE); /* Invalid data */
39 device_data.length = DEVICE_DATA_PAYLOAD_SIZE;
40 device_data.crc = crc_32(device_data.payload, device_data.length, 0xffffffff);
41
42}
43
44/******************************************************************************/
45#endif /* ndef BOOTLOADER ******************************************************/
46/******************************************************************************/
47
48#if defined(HAVE_DEVICEDATA)
49void __attribute__((weak)) fill_devicedata(struct device_data_t *data)
50{
51 memset(data->payload, 0xff, data->length);
52}
53#endif
54
55/* Write bootdata into location in FIRMWARE marked by magic header
56 * Assumes buffer is already loaded with the firmware image
57 * We just need to find the location and write data into the
58 * payload region along with the crc for later verification and use.
59 * Returns payload len on success,
60 * On error returns false
61 */
62bool write_devicedata(unsigned char* buf, int len)
63{
64 int search_len = MIN(len, DEVICE_DATA_SEARCH_SIZE) - sizeof(struct device_data_t);
65
66 /* search for decvice data header prior to search_len */
67 for(int i = 0; i < search_len; i++)
68 {
69 struct device_data_t *data = (struct device_data_t *)&buf[i];
70 if (data->magic[0] != DEVICE_DATA_MAGIC0 ||
71 data->magic[1] != DEVICE_DATA_MAGIC1)
72 continue;
73
74 /* Ignore it if the length extends past the end of the buffer. */
75 int data_len = offsetof(struct device_data_t, payload) + data->length;
76 if (i + data_len > len)
77 continue;
78
79 fill_devicedata(data);
80
81 /* Calculate payload CRC */
82 data->crc = crc_32(data->payload, data->length, 0xffffffff);
83 return true;
84 }
85
86 return false;
87}
88
diff --git a/firmware/common/rb-loader.c b/firmware/common/rb-loader.c
index 61d8b1ddd2..2f5e06e165 100644
--- a/firmware/common/rb-loader.c
+++ b/firmware/common/rb-loader.c
@@ -30,6 +30,9 @@
30#include "multiboot.h" 30#include "multiboot.h"
31#endif 31#endif
32 32
33#ifdef HAVE_DEVICEDATA
34#include "devicedata.h"
35#endif
33/* loads a firmware file from supplied filename 36/* loads a firmware file from supplied filename
34 * file opened, checks firmware size and checksum 37 * file opened, checks firmware size and checksum
35 * if no error, firmware loaded to supplied buffer 38 * if no error, firmware loaded to supplied buffer
@@ -118,7 +121,6 @@ int load_firmware(unsigned char* buf, const char* firmware, int buffer_size)
118 /* if ret is valid breaks from loop to continue loading */ 121 /* if ret is valid breaks from loop to continue loading */
119 } 122 }
120#endif 123#endif
121
122 if (ret < 0) /* Check default volume, no valid firmware file loaded yet */ 124 if (ret < 0) /* Check default volume, no valid firmware file loaded yet */
123 { 125 {
124 /* First check in BOOTDIR */ 126 /* First check in BOOTDIR */
@@ -141,5 +143,9 @@ int load_firmware(unsigned char* buf, const char* firmware, int buffer_size)
141 else /* full path passed ROLO etc.*/ 143 else /* full path passed ROLO etc.*/
142 ret = load_firmware_filename(buf, firmware, buffer_size); 144 ret = load_firmware_filename(buf, firmware, buffer_size);
143 145
146#ifdef HAVE_DEVICEDATA
147 write_devicedata(buf, ret);
148#endif
149
144 return ret; 150 return ret;
145} 151}