forked from riscv-collab/riscv-openocd
-
Notifications
You must be signed in to change notification settings - Fork 23
/
custom.c
472 lines (431 loc) · 14.5 KB
/
custom.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/***************************************************************************
* Copyright (C) 2010 by Antonio Borneo <[email protected]> *
* Modified by Yanwen Wang <[email protected]> based on fespi.c *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "imp.h"
#include "spi.h"
#include <jtag/jtag.h>
#include <helper/time_support.h>
#include <target/algorithm.h>
#include "target/riscv/riscv.h"
#include <helper/configuration.h>
#define ERASE_CMD (1)
#define WRITE_CMD (2)
#define READ_CMD (3)
#define PROBE_CMD (4)
struct flash_bank_msg {
bool probed;
const struct flash_device *dev;
target_addr_t ctrl_base;
char *loader_path;
uint8_t cs;
uint8_t *buffer;
uint32_t param_0;
uint32_t param_1;
bool simulation;
uint32_t sectorsize;
};
static int custom_run_algorithm(struct flash_bank *bank)
{
struct flash_bank_msg *bank_msg = bank->driver_priv;
struct target *target = bank->target;
int retval = ERROR_OK;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}
int xlen = riscv_xlen(target);
struct working_area *algorithm_wa = NULL;
struct working_area *data_wa = NULL;
uint8_t* bin = (uint8_t*)malloc(target->working_area_size);
size_t bin_size;
FILE* fd = fopen((char*)bank_msg->loader_path, "rb");
if (NULL == fd) {
fd = fopen(find_file(strrchr((char*)bank_msg->loader_path, '/')), "rb");
}
if (fd) {
fseek(fd, 0, SEEK_END);
bin_size = ftell(fd);
rewind(fd);
if (target->working_area_size < bin_size) {
LOG_ERROR("working_area_size less than loader_bin_size");
goto err;
}
if (1 != fread(bin, bin_size, 1, fd)) {
LOG_ERROR("read loader error");
goto err;
}
fclose(fd);
} else {
LOG_ERROR("Failed to open loader:%s ", bank_msg->loader_path);
goto err;
}
unsigned data_wa_size = 0;
if (target_alloc_working_area(target, bin_size, &algorithm_wa) == ERROR_OK) {
retval = target_write_buffer(target, algorithm_wa->address, bin_size, bin);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to write code to " TARGET_ADDR_FMT ": %d",
algorithm_wa->address, retval);
target_free_working_area(target, algorithm_wa);
algorithm_wa = NULL;
} else {
data_wa_size = MIN(target->working_area_size - algorithm_wa->size, bank_msg->param_0);
while (1) {
if (target_alloc_working_area_try(target, data_wa_size, &data_wa) == ERROR_OK) {
break;
}
data_wa_size = data_wa_size * 3 / 4;
}
}
} else {
LOG_WARNING("Couldn't allocate %zd-byte working area.", bin_size);
algorithm_wa = NULL;
}
if (algorithm_wa) {
uint32_t count = 0;
uint32_t offset = 0;
uint32_t first_addr = 0;
uint32_t end_addr = 0;
uint32_t cur_count = 0;
int algorithm_result = 0;
struct reg_param reg_params[5];
init_reg_param(®_params[0], "a0", xlen, PARAM_IN_OUT);
init_reg_param(®_params[1], "a1", xlen, PARAM_OUT);
init_reg_param(®_params[2], "a2", xlen, PARAM_OUT);
init_reg_param(®_params[3], "a3", xlen, PARAM_OUT);
init_reg_param(®_params[4], "a4", xlen, PARAM_OUT);
switch (bank_msg->cs)
{
case ERASE_CMD:
first_addr = bank_msg->param_0;
end_addr = bank_msg->param_1;
buf_set_u64(reg_params[0].value, 0, xlen, bank_msg->cs);
buf_set_u64(reg_params[1].value, 0, xlen, bank_msg->ctrl_base);
buf_set_u64(reg_params[2].value, 0, xlen, first_addr);
buf_set_u64(reg_params[3].value, 0, xlen, end_addr);
buf_set_u64(reg_params[4].value, 0, xlen, 0);
if (bank_msg->simulation) {
retval = target_run_algorithm(target, 0, NULL,
ARRAY_SIZE(reg_params), reg_params,
algorithm_wa->address, 0, 0x7FFFFFFF, NULL);
} else {
retval = target_run_algorithm(target, 0, NULL,
ARRAY_SIZE(reg_params), reg_params,
algorithm_wa->address, 0, (end_addr - first_addr) * 2, NULL);
}
if (retval != ERROR_OK) {
LOG_ERROR("Failed to execute algorithm at " TARGET_ADDR_FMT ": %d",
algorithm_wa->address, retval);
goto err;
}
algorithm_result = buf_get_u64(reg_params[0].value, 0, xlen);
if (algorithm_result != 0) {
LOG_ERROR("Algorithm returned error %d", algorithm_result);
LOG_ERROR("erase command error");
retval = ERROR_FAIL;
goto err;
}
break;
case WRITE_CMD:
count = bank_msg->param_0;
offset = bank_msg->param_1;
cur_count = 0;
while (count > 0) {
cur_count = MIN(count, data_wa_size);
buf_set_u64(reg_params[0].value, 0, xlen, bank_msg->cs);
buf_set_u64(reg_params[1].value, 0, xlen, bank_msg->ctrl_base);
buf_set_u64(reg_params[2].value, 0, xlen, data_wa->address);
buf_set_u64(reg_params[3].value, 0, xlen, offset);
buf_set_u64(reg_params[4].value, 0, xlen, cur_count);
retval = target_write_buffer(target, data_wa->address, cur_count, bank_msg->buffer);
if (retval != ERROR_OK) {
LOG_DEBUG("Failed to write %d bytes to " TARGET_ADDR_FMT ": %d",
cur_count, data_wa->address, retval);
goto err;
}
if (bank_msg->simulation) {
retval = target_run_algorithm(target, 0, NULL,
ARRAY_SIZE(reg_params), reg_params,
algorithm_wa->address, 0, 0x7FFFFFFF, NULL);
} else {
retval = target_run_algorithm(target, 0, NULL,
ARRAY_SIZE(reg_params), reg_params,
algorithm_wa->address, 0, cur_count * 2, NULL);
}
if (retval != ERROR_OK) {
LOG_ERROR("Failed to execute algorithm at " TARGET_ADDR_FMT ": %d",
algorithm_wa->address, retval);
goto err;
}
algorithm_result = buf_get_u64(reg_params[0].value, 0, xlen);
if (algorithm_result != 0) {
LOG_ERROR("Algorithm returned error %d", algorithm_result);
LOG_ERROR("write command error");
retval = ERROR_FAIL;
goto err;
}
bank_msg->buffer += cur_count;
offset += cur_count;
count -= cur_count;
}
break;
case READ_CMD:
count = bank_msg->param_0;
offset = bank_msg->param_1;
cur_count = 0;
while (count > 0) {
cur_count = MIN(count, data_wa_size);
buf_set_u64(reg_params[0].value, 0, xlen, bank_msg->cs);
buf_set_u64(reg_params[1].value, 0, xlen, bank_msg->ctrl_base);
buf_set_u64(reg_params[2].value, 0, xlen, data_wa->address);
buf_set_u64(reg_params[3].value, 0, xlen, offset);
buf_set_u64(reg_params[4].value, 0, xlen, cur_count);
if (bank_msg->simulation) {
retval = target_run_algorithm(target, 0, NULL,
ARRAY_SIZE(reg_params), reg_params,
algorithm_wa->address, 0, 0x7FFFFFFF, NULL);
} else {
retval = target_run_algorithm(target, 0, NULL,
ARRAY_SIZE(reg_params), reg_params,
algorithm_wa->address, 0, cur_count * 2, NULL);
}
if (retval != ERROR_OK) {
LOG_ERROR("Failed to execute algorithm at " TARGET_ADDR_FMT ": %d",
algorithm_wa->address, retval);
goto err;
}
algorithm_result = buf_get_u64(reg_params[0].value, 0, xlen);
if (algorithm_result != 0) {
LOG_ERROR("Algorithm returned error %d", algorithm_result);
LOG_ERROR("read command error");
retval = ERROR_FAIL;
goto err;
}
retval = target_read_buffer(target, data_wa->address, cur_count, bank_msg->buffer);
if (retval != ERROR_OK) {
LOG_DEBUG("Failed to read %d bytes from " TARGET_ADDR_FMT ": %d",
cur_count, data_wa->address, retval);
goto err;
}
bank_msg->buffer += cur_count;
offset += cur_count;
count -= cur_count;
}
break;
case PROBE_CMD:
buf_set_u64(reg_params[0].value, 0, xlen, bank_msg->cs);
buf_set_u64(reg_params[1].value, 0, xlen, bank_msg->ctrl_base);
buf_set_u64(reg_params[2].value, 0, xlen, 0);
buf_set_u64(reg_params[3].value, 0, xlen, 0);
buf_set_u64(reg_params[4].value, 0, xlen, 0);
if (bank_msg->simulation) {
retval = target_run_algorithm(target, 0, NULL,
ARRAY_SIZE(reg_params), reg_params,
algorithm_wa->address, 0, 0x7FFFFFFF, NULL);
} else {
retval = target_run_algorithm(target, 0, NULL,
ARRAY_SIZE(reg_params), reg_params,
algorithm_wa->address, 0, 10000, NULL);
}
if (retval != ERROR_OK) {
LOG_ERROR("Failed to execute algorithm at " TARGET_ADDR_FMT ": %d",
algorithm_wa->address, retval);
goto err;
}
algorithm_result = buf_get_u64(reg_params[0].value, 0, xlen);
retval = algorithm_result;
break;
default:
break;
}
target_free_working_area(target, data_wa);
target_free_working_area(target, algorithm_wa);
}
err:
if (bin) {
free(bin);
}
if (algorithm_wa) {
target_free_working_area(target, data_wa);
target_free_working_area(target, algorithm_wa);
}
return retval;
}
FLASH_BANK_COMMAND_HANDLER(custom_flash_bank_command)
{
struct flash_bank_msg *bank_msg;
LOG_DEBUG("%s", __func__);
if (CMD_ARGC < 8) {
LOG_ERROR("Parameter error:");
LOG_ERROR("flash bank $FLASHNAME custom 0x20000000 0 0 0 $TARGETNAME 0x10014000 ~/work/riscv.bin [simulation] [sectorsize=]");
return ERROR_COMMAND_SYNTAX_ERROR;
}
bank_msg = malloc(sizeof(struct flash_bank_msg));
if (bank_msg == NULL) {
LOG_ERROR("not enough memory");
return ERROR_FAIL;
}
bank->driver_priv = bank_msg;
bank_msg->probed = false;
bank_msg->ctrl_base = 0;
bank_msg->loader_path = NULL;
bank_msg->cs = 0;
bank_msg->buffer = NULL;
bank_msg->param_0 = 0;
bank_msg->param_1 = 0;
COMMAND_PARSE_ADDRESS(CMD_ARGV[6], bank_msg->ctrl_base);
LOG_DEBUG("ASSUMING CUSTOM device at ctrl_base = " TARGET_ADDR_FMT,
bank_msg->ctrl_base);
bank_msg->loader_path = malloc(strlen(CMD_ARGV[7]));
strcpy((char*)bank_msg->loader_path, CMD_ARGV[7]);
for (char *p = bank_msg->loader_path; *p; p++) {
if (*p == '\\')
*p = '/';
}
bank_msg->simulation = false;
bank_msg->sectorsize = 0;
for (unsigned int i = 8; i < CMD_ARGC; i++) {
if(strcmp(CMD_ARGV[i], "simulation") == 0) {
bank_msg->simulation = true;
LOG_DEBUG("Custom Simulation Mode");
}
if(strncmp(CMD_ARGV[i], "sectorsize=", strlen("sectorsize=")) == 0) {
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[i]+strlen("sectorsize="), bank_msg->sectorsize);
LOG_DEBUG("Custom flash sectorsize is %x", bank_msg->sectorsize);
}
}
return ERROR_OK;
}
static int custom_erase(struct flash_bank *bank, unsigned int first,
unsigned int last)
{
struct flash_bank_msg *bank_msg = bank->driver_priv;
bank_msg->cs = ERASE_CMD;
bank_msg->buffer = NULL;
bank_msg->param_0 = bank->sectors[first].offset;
bank_msg->param_1 = bank->sectors[last].offset + bank_msg->sectorsize;
return custom_run_algorithm(bank);
}
static int custom_write(struct flash_bank *bank, const uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct flash_bank_msg *bank_msg = bank->driver_priv;
bank_msg->cs = WRITE_CMD;
bank_msg->buffer = (uint8_t*)buffer;
bank_msg->param_0 = count;
bank_msg->param_1 = offset;
return custom_run_algorithm(bank);
}
static int custom_read(struct flash_bank *bank, uint8_t *buffer,
uint32_t offset, uint32_t count)
{
struct flash_bank_msg *bank_msg = bank->driver_priv;
bank_msg->cs = READ_CMD;
bank_msg->buffer = buffer;
bank_msg->param_0 = count;
bank_msg->param_1 = offset;
return custom_run_algorithm(bank);
}
static int custom_probe(struct flash_bank *bank)
{
struct flash_bank_msg *bank_msg = bank->driver_priv;
uint32_t id = 0x12345678;
struct flash_sector *sectors;
if (bank_msg->probed)
free(bank->sectors);
bank_msg->probed = false;
bank_msg->dev = NULL;
for (const struct flash_device *p = flash_devices; p->name ; p++) {
if (p->device_id == id) {
bank_msg->dev = p;
break;
}
}
/* Set correct size value */
bank->size = bank_msg->dev->size_in_bytes;
/* if no sectors, treat whole bank as single sector */
if (0 == bank_msg->sectorsize) {
bank_msg->sectorsize = bank_msg->dev->sectorsize ?
bank_msg->dev->sectorsize : bank_msg->dev->size_in_bytes;
}
/* create and fill sectors array */
bank->num_sectors = bank_msg->dev->size_in_bytes / bank_msg->sectorsize;
sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
if (sectors == NULL) {
LOG_ERROR("not enough memory");
return ERROR_FAIL;
}
for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
sectors[sector].offset = sector * bank_msg->sectorsize;
sectors[sector].size = bank_msg->sectorsize;
sectors[sector].is_erased = -1;
sectors[sector].is_protected = 0;
}
bank->sectors = sectors;
bank_msg->cs = PROBE_CMD;
bank_msg->buffer = NULL;
bank_msg->param_0 = 0;
bank_msg->param_1 = 0;
id = custom_run_algorithm(bank);
LOG_INFO("Found custom flash device (ID 0x%08" PRIx32 ")", id);
bank_msg->probed = true;
return ERROR_OK;
}
static int custom_info(struct flash_bank *bank, struct command_invocation *command)
{
struct flash_bank_msg *bank_msg = bank->driver_priv;
if (!(bank_msg->probed)) {
return ERROR_OK;
}
return ERROR_OK;
}
static int custom_auto_probe(struct flash_bank *bank)
{
struct flash_bank_msg *bank_msg = bank->driver_priv;
if (bank_msg->probed)
return ERROR_OK;
return custom_probe(bank);
}
static int custom_protect(struct flash_bank *bank, int set,
unsigned int first, unsigned int last)
{
for (unsigned int sector = first; sector <= last; sector++)
bank->sectors[sector].is_protected = set;
return ERROR_OK;
}
static int custom_protect_check(struct flash_bank *bank)
{
/* Nothing to do. Protection is only handled in SW. */
return ERROR_OK;
}
const struct flash_driver custom_flash = {
.name = "custom",
.flash_bank_command = custom_flash_bank_command,
.erase = custom_erase,
.protect = custom_protect,
.write = custom_write,
.read = custom_read,
.probe = custom_probe,
.auto_probe = custom_auto_probe,
.erase_check = default_flash_blank_check,
.protect_check = custom_protect_check,
.info = custom_info,
.free_driver_priv = default_flash_free_driver_priv
};