forked from aspalmer/stfuboot
/
scratchpad.c
67 lines (49 loc) · 1.36 KB
/
scratchpad.c
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
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <libopencm3/stm32/crc.h>
/*
Total size of scratch area is 32 bytes
*/
struct stfub_scratchpad {
uint8_t boot_to_dfu;
uint8_t __reserved[27];
uint32_t crc;
} __attribute__ ((packed));
extern unsigned _scratch;
static void stfub_scratchpad_recalculate_crc(void)
{
struct stfub_scratchpad *scratchpad;
scratchpad = (struct stfub_scratchpad *)&_scratch;
scratchpad->crc = 0;
scratchpad->crc = crc_calculate_block((uint32_t *)scratchpad,
sizeof(*scratchpad) / 4);
}
bool stfub_scratchpad_is_valid(void)
{
u32 crc;
struct stfub_scratchpad *scratchpad;
scratchpad = (struct stfub_scratchpad *)&_scratch;
crc = crc_calculate_block((uint32_t *)scratchpad,
sizeof(*scratchpad) / 4);
return crc == scratchpad->crc;
}
bool stfub_scratchpad_dfu_switch_requested(void)
{
struct stfub_scratchpad *scratchpad;
scratchpad = (struct stfub_scratchpad *)&_scratch;
return scratchpad->boot_to_dfu;
}
void stfub_scratchpad_request_dfu_switch(void)
{
struct stfub_scratchpad *scratchpad;
scratchpad = (struct stfub_scratchpad *)&_scratch;
scratchpad->boot_to_dfu = true;
stfub_scratchpad_recalculate_crc();
}
void stfub_scratchpad_init(void)
{
struct stfub_scratchpad *scratchpad;
scratchpad = (struct stfub_scratchpad *)&_scratch;
memset(scratchpad, 0, sizeof(*scratchpad));
}