diff options
Diffstat (limited to 'firmware/common/bootdata.c')
-rw-r--r-- | firmware/common/bootdata.c | 74 |
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 | |||
29 | bool boot_data_valid; | ||
30 | |||
31 | static bool verify_boot_data_v0(void) INIT_ATTR; | ||
32 | static 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 | |||
45 | struct verify_bd_entry | ||
46 | { | ||
47 | int version; | ||
48 | bool (*verify) (void); | ||
49 | }; | ||
50 | |||
51 | static const struct verify_bd_entry verify_bd[] INITDATA_ATTR = { | ||
52 | { 0, verify_boot_data_v0 }, | ||
53 | }; | ||
54 | |||
55 | void 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 | } | ||