-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathda.c
227 lines (188 loc) · 5.16 KB
/
da.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "da.h"
#include <stdio.h>
#include <string.h>
#include "endianness.h"
#define EP_IN 0x85 // (0x80 | 0x05)
#define EP_OUT 0x06
#define TIMEOUT_MS 0
#define DA_MAX_LENGTH 4096
#define DA_MAX_DATA_LENGTH (DA_MAX_LENGTH - 4 - 2)
static uint8_t da_buf[DA_MAX_LENGTH];
static uint16_t crc16(const void *data, size_t length) {
const uint8_t *d = data;
uint16_t crc = 0;
for (size_t i = 0; i < length; i++) {
crc ^= d[i] << 8;
for (uint8_t j = 0; j < 8; j++) {
if (crc & 0x8000) {
crc = (crc << 1) ^ 0x1021;
} else {
crc = crc << 1;
}
}
}
return crc;
}
int da_receive(libusb_device_handle *handle, uint16_t *cmd, uint16_t *data_length, void **data) {
int transferred;
int ret = libusb_bulk_transfer(
handle,
EP_IN,
da_buf,
sizeof(da_buf),
&transferred,
TIMEOUT_MS
);
if (ret) {
printf("libusb_bulk_transfer failed: %s\n", libusb_error_name(ret));
return ret;
}
// printf("Received %d bytes\n", transferred);
if (transferred < 3) {
printf("transferred < 3\n");
return -1;
}
*cmd = READ_BE16(da_buf + 0);
*data_length = READ_BE16(da_buf + 2);
if (*data_length > DA_MAX_DATA_LENGTH) {
printf("length too long\n");
return -1;
}
if (transferred != (4 + *data_length + 2)) {
printf("transferred != (4 + *data_length + 2)\n");
return -1;
}
*data = da_buf + 4;
uint16_t crc = READ_BE16(da_buf + 4 + *data_length);
if (crc != crc16(da_buf, 4 + *data_length)) {
printf("crc != crc16(da_buf, 4 + length)\n");
return -1;
}
return 0;
}
int da_receive_status(libusb_device_handle *handle, da_status_t *status) {
uint16_t cmd;
uint16_t data_length;
void *data;
int ret = da_receive(handle, &cmd, &data_length, &data);
if (ret) {
return ret;
}
if (cmd != CMD_STATUS) {
printf("cmd != CMD_STATUS\n");
return -1;
}
if (data_length != 2) {
printf("data_length != 2\n");
return -1;
}
*status = READ_BE16(data);
return 0;
}
int da_check_status(libusb_device_handle *handle) {
da_status_t status;
int ret = da_receive_status(handle, &status);
if (ret) {
return ret;
}
if (status != STATUS_OK) {
printf("status != STATUS_OK\n");
return -1;
}
return 0;
}
int da_send(libusb_device_handle *handle, uint16_t cmd, uint16_t data_length, const void *data) {
if (data_length > DA_MAX_DATA_LENGTH) {
printf("data_length too long\n");
return -1;
}
WRITE_BE16(da_buf + 0, cmd);
WRITE_BE16(da_buf + 2, data_length);
memcpy(da_buf + 4, data, data_length);
uint16_t crc = crc16(da_buf, 4 + data_length);
WRITE_BE16(da_buf + 4 + data_length, crc);
int transferred;
int ret = libusb_bulk_transfer(
handle,
EP_OUT,
da_buf,
4 + data_length + 2,
&transferred,
TIMEOUT_MS
);
if (ret) {
printf("libusb_bulk_transfer failed: %s\n", libusb_error_name(ret));
return ret;
}
// printf("Sent %d bytes\n", transferred);
if (transferred != (4 + data_length + 2)) {
printf("transferred != (4 + data_length + 2)\n");
return -1;
}
return 0;
}
int da_emmc_init(libusb_device_handle *handle) {
int ret = da_send(handle, CMD_EMMC_INIT, 0, NULL);
if (ret) {
return ret;
}
return da_check_status(handle);
}
int da_emmc_switch(libusb_device_handle *handle, uint8_t index, uint8_t value) {
uint8_t buf[2];
buf[0] = index;
buf[1] = value;
int ret = da_send(handle, CMD_EMMC_SWITCH, 2, buf);
if (ret) {
return ret;
}
return da_check_status(handle);
}
int da_get_sec_count(libusb_device_handle *handle, uint32_t *sec_count) {
int ret = da_send(handle, CMD_EMMC_GET_SEC_COUNT, 0, NULL);
if (ret) {
return ret;
}
uint16_t cmd;
uint16_t data_length;
void *data;
ret = da_receive(handle, &cmd, &data_length, &data);
if (ret) {
return ret;
}
if (cmd != CMD_EMMC_GET_SEC_COUNT) {
printf("cmd != CMD_EMMC_GET_SEC_COUNT (0x%04X)\n", cmd);
return -1;
}
if (data_length != 4) {
printf("data_length != 4\n");
return -1;
}
*sec_count = READ_BE32(data);
return 0;
}
int da_read_single_block(libusb_device_handle *handle, uint32_t lba, void *data) {
uint8_t buf[4];
WRITE_BE32(buf, lba);
int ret = da_send(handle, CMD_EMMC_READ_SINGLE_BLOCK, 4, buf);
if (ret) {
return ret;
}
uint16_t cmd;
uint16_t data_length;
void *received_data;
ret = da_receive(handle, &cmd, &data_length, &received_data);
if (ret) {
return ret;
}
if (cmd != CMD_EMMC_READ_SINGLE_BLOCK) {
printf("cmd != CMD_EMMC_READ_SINGLE_BLOCK\n");
return -1;
}
if (data_length != EMMC_BLOCK_SIZE) {
printf("data_length != EMMC_BLOCK_SIZE\n");
return -1;
}
memcpy(data, received_data, EMMC_BLOCK_SIZE);
return 0;
}