-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFU.cpp
More file actions
146 lines (107 loc) · 3.02 KB
/
Copy pathDFU.cpp
File metadata and controls
146 lines (107 loc) · 3.02 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "DFU.h"
#undef ERROR_SUCCESS // Fuck windows
enum DFU_ERRORS
{
ERROR_SUCCESS = 0x70000000,
ERROR_INVALID_PACKAGE_LENGTH = 0x40000000,
ERROR_INVALID_OFFSET,
ERROR_INVALID_LENGTH,
ERROR_FLASH_FAILED,
ERROR_FAILED_TO_UPDATE_OB,
ERROR_UNIMPLEMENTED = 0x50000000
};
enum COMMANDS
{
PING = 0xA0F0,
SET_OFFSET,
READ_FLASH,
READ_OB,
SET_OB,
JUMP_TO_FIRMWARE_HANDLER,
};
bool DFU::ping()
{
uint8_t outbuffer[2];
memset(outbuffer, 0, sizeof(outbuffer));
*(uint16_t*)outbuffer = PING;
write(outbuffer, sizeof(outbuffer));
uint8_t inbuffer[4];
read(inbuffer, sizeof(inbuffer));
/*printf("%s\n", __func__);
printf("0x%08x\n", *(uint32_t*)inbuffer);
printf("\n");*/
return *(uint32_t*)inbuffer == ERROR_SUCCESS;
}
bool DFU::set_offset(uint32_t offset)
{
uint8_t outbuffer[6];
memset(outbuffer, 0, sizeof(outbuffer));
*(uint16_t*)outbuffer = SET_OFFSET;
*(uint32_t*)&outbuffer[2] = offset;
write(outbuffer, sizeof(outbuffer));
uint8_t inbuffer[4];
read(inbuffer, sizeof(inbuffer));
/*printf("%s\n", __func__);
printf("0x%08x\n", *(uint32_t*)inbuffer);
printf("\n");*/
return *(uint32_t*)inbuffer == ERROR_SUCCESS;
}
bool DFU::read_flash(uint32_t offset, uint8_t* buffer, uint8_t size)
{
uint8_t outbuffer[10];
memset(outbuffer, 0, sizeof(outbuffer));
*(uint16_t *)outbuffer = READ_FLASH;
*(uint32_t*)&outbuffer[2] = offset;
*(uint32_t*)&outbuffer[6] = size;
write(outbuffer, sizeof(outbuffer));
uint8_t inbuffer[4];
read(inbuffer, sizeof(inbuffer));
/*printf("%s\n", __func__);
printf("0x%08x\n", *(uint32_t*)inbuffer);
printf("\n");
*/
if (*(uint32_t*)inbuffer == ERROR_SUCCESS)
read(buffer, size);
return *(uint32_t*)inbuffer == ERROR_SUCCESS;
}
bool DFU::read_ob()
{
uint8_t outbuffer[2];
memset(outbuffer, 0, sizeof(outbuffer));
*(uint16_t*)outbuffer = READ_OB;
write(outbuffer, sizeof(outbuffer));
uint8_t inbuffer[4];
read(inbuffer, sizeof(inbuffer));
/*printf("%s\n", __func__);
printf("0x%08x\n", *(uint32_t*)inbuffer);*/
if (*(uint32_t*)inbuffer == ERROR_SUCCESS)
{
uint8_t inbuffer2[12];
read(inbuffer2, sizeof(inbuffer2));
/*for (int i = 0; i < 12; i++)
printf("0x%02x ", inbuffer2[i]);
printf("\n\n");*/
}
return *(uint32_t*)inbuffer == ERROR_SUCCESS;
}
bool DFU::set_ob(uint8_t ob)
{
uint8_t outbuffer[3];
memset(outbuffer, 0, sizeof(outbuffer));
*(uint16_t*)outbuffer = SET_OB;
*(uint8_t*)&outbuffer[2] = ob;
write(outbuffer, sizeof(outbuffer));
uint8_t inbuffer[4];
read(inbuffer, sizeof(inbuffer));
/*printf("%s\n", __func__);
printf("0x%08x\n", *(uint32_t*)inbuffer);
printf("\n");*/
return *(uint32_t*)inbuffer == ERROR_SUCCESS;
}
bool DFU::send_data(uint8_t* buffer)
{
write(buffer, 64);
uint8_t inbuffer[4];
read(inbuffer, sizeof(inbuffer));
return *(uint32_t*)inbuffer == ERROR_SUCCESS;
}