summaryrefslogtreecommitdiff
path: root/firmware/common/bootdata.c
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2022-12-22 19:23:29 +0000
committerAidan MacDonald <amachronic@protonmail.com>2024-03-31 16:57:19 +0100
commit6ffd42548bf10cda13a01555ff4fa56d4213cdf2 (patch)
tree975d6a4f88a0a3469c1450476e841ef0bf8fbb85 /firmware/common/bootdata.c
parentaf644e02a151bb6d4c229cc1d4846c7ffe952135 (diff)
downloadrockbox-6ffd42548bf10cda13a01555ff4fa56d4213cdf2.tar.gz
rockbox-6ffd42548bf10cda13a01555ff4fa56d4213cdf2.zip
multiboot: Refactor boot data validation, add version numbers
Instead of verifying the CRC before every access of the boot data, verify the CRC once at startup and set a flag to indicate the boot data is valid. Also add a framework to support multiple boot protocol versions. Firmware declares the maximum supported protocol version using a version byte in the boot data header. The bootloader chooses the highest version supported by it and the firmware when deciding what boot protocol to use. Change-Id: I810194625dc0833f026d2a23b8d64ed467fa6aca
Diffstat (limited to 'firmware/common/bootdata.c')
-rw-r--r--firmware/common/bootdata.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/firmware/common/bootdata.c b/firmware/common/bootdata.c
new file mode 100644
index 0000000000..fa74c5fe91
--- /dev/null
+++ b/firmware/common/bootdata.c
@@ -0,0 +1,74 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2022 by Aidan MacDonald
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 "bootdata.h"
22#include "crc32.h"
23#include <stddef.h>
24
25#ifdef BOOTLOADER
26# error "not to be included in bootloader builds"
27#endif
28
29bool boot_data_valid;
30
31static bool verify_boot_data_v0(void) INIT_ATTR;
32static bool verify_boot_data_v0(void)
33{
34 /* validate protocol version */
35 if (boot_data.version != 0)
36 return false;
37
38 /* validate length */
39 if (boot_data.length != 4)
40 return false;
41
42 return true;
43}
44
45struct verify_bd_entry
46{
47 int version;
48 bool (*verify) (void);
49};
50
51static const struct verify_bd_entry verify_bd[] INITDATA_ATTR = {
52 { 0, verify_boot_data_v0 },
53};
54
55void verify_boot_data(void)
56{
57 /* verify payload with checksum - all protocol versions */
58 uint32_t crc = crc_32(boot_data.payload, boot_data.length, 0xffffffff);
59 if (crc != boot_data.crc)
60 return;
61
62 /* apply verification specific to the protocol version */
63 for (size_t i = 0; i < ARRAYLEN(verify_bd); ++i)
64 {
65 const struct verify_bd_entry *e = &verify_bd[i];
66 if (e->version == boot_data.version)
67 {
68 if (e->verify())
69 boot_data_valid = true;
70
71 return;
72 }
73 }
74}