forked from lowRISC/ftditool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.hh
More file actions
477 lines (407 loc) · 13.8 KB
/
Copy pathcommands.hh
File metadata and controls
477 lines (407 loc) · 13.8 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
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
473
474
475
476
477
// Copyright lowRISC contributors.
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "flash/flash.hh"
#include "embeddedpp/gpio.hh"
#include "visuals/progressbar.hh"
#include "visuals/throughput.hh"
#include <fstream>
#include <iostream>
#include <picosha2.h>
#include <print>
#include <elfio/elfio.hpp>
namespace commands {
template <typename T>
struct Commands {
flash::Generic<T> flash;
Commands(flash::Generic<T> f) : flash(f) {}
virtual ~Commands() = default;
virtual int run() = 0;
};
template <typename T>
struct ReadJedec : public Commands<T> {
ReadJedec(flash::Generic<T> f) : Commands<T>(f) {}
int run() override {
if (auto jedec = this->flash.jedec()) {
std::println("{}", *jedec);
return 0;
}
std::println("Jedec failed");
return 1;
}
};
template <typename T>
struct ReadSfdp : public Commands<T> {
ReadSfdp(flash::Generic<T> f) : Commands<T>(f) {}
int run() override {
if (auto sfdp = this->flash.sfdp()) {
std::println("{}", *sfdp);
std::println("quad-mode mechanism: {}", sfdp->get_quad_enable_mechanism());
return 0;
}
std::println("Sfdp read failed");
return 1;
}
};
template <typename T>
struct ReadPage : public Commands<T> {
std::size_t addr;
ReadPage(flash::Generic<T> f, std::size_t addr = 0) : Commands<T>(f), addr(addr) {}
int run() override {
if (auto page = this->flash.single_read_page(addr)) {
std::println("Single read {:#x} : {}", addr, *page);
return 0;
}
std::println("page failed");
return 1;
}
};
template <typename T>
struct TestPage : public Commands<T> {
std::size_t addr;
bool quad;
TestPage(flash::Generic<T> f, std::size_t addr = 0, bool quad = false)
: Commands<T>(f), addr(addr), quad(quad) {}
int run() override {
if (!this->flash.reset() || !this->flash.erase(addr)) {
std::println("Erase failed");
return 0;
}
if (quad && !this->flash.enable_quad(true)) {
std::println("enable quad failed");
}
std::vector<uint8_t> data(256, 0x5a);
if (quad && !this->flash.quad_page_program(addr, std::span<uint8_t, 256>(data))) {
std::println("Program failed");
return 0;
} else if (!quad && !this->flash.single_page_program(addr, std::span<uint8_t, 256>(data))) {
std::println("Program failed");
return 0;
}
std::optional<flash::Page> page;
if (quad) {
page = this->flash.template quad_read_page(addr);
} else {
page = this->flash.single_read_page(addr);
}
if (page) {
std::println("page read {:#x} : {}", addr, *page);
return 0;
}
std::println("page failed");
return 1;
}
};
template <typename T>
struct VerifyFile : public Commands<T> {
std::size_t addr;
std::string& filename;
bool quad;
VerifyFile(flash::Generic<T> f, std::string& filename, std::size_t addr = 0, bool quad = false)
: Commands<T>(f), filename(filename), addr(addr), quad(quad) {}
int run() override {
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::println("Could not open the file {}!", filename);
return 0;
}
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
if (file_size == 0) {
std::println("File is empty");
return 0;
}
std::vector<uint8_t> file_hash(picosha2::k_digest_size, 0xdd);
picosha2::hash256(file, file_hash.begin(), file_hash.end());
this->flash.reset();
if (quad && !this->flash.enable_quad(true)) {
std::println("enable quad failed");
return 0;
}
picosha2::hash256_one_by_one flash_hasher;
auto progress_bar = ProgressBar(file_size, 50, "Verifying").with_throughput();
size_t remainder = file_size;
while (remainder > 0) {
std::optional<flash::Page> page;
if (quad) {
page = this->flash.template quad_read_page(addr);
} else {
page = this->flash.single_read_page(addr);
}
if (!page) {
std::println("Read page {:#x} failed.", addr);
return 0;
}
uint32_t chunk = std::min(remainder, std::size_t{flash::PageSize});
std::span<uint8_t> data(*page);
if (chunk < data.size()) {
data = data.subspan(0, chunk);
}
flash_hasher.process(data.begin(), data.end());
addr += chunk;
remainder -= chunk;
progress_bar.update(file_size - remainder);
}
flash_hasher.finish();
std::vector<uint8_t> flash_hash(picosha2::k_digest_size, 0xee);
flash_hasher.get_hash_bytes(flash_hash.begin(), flash_hash.end());
if (file_hash != flash_hash) {
std::println("Expected: {}\nbut got: {}", file_hash, flash_hash);
}
return 1;
}
};
template <typename T>
struct LoadFile : public Commands<T> {
std::size_t start_addr;
std::string& filename;
bool quad;
bool bootstrap;
bool skip_erase;
LoadFile(flash::Generic<T> f, std::string& filename, std::size_t addr = 0, bool bootstrap = false,
bool skip_erase = false, bool quad = false)
: Commands<T>(f),
filename(filename),
start_addr(addr),
quad(quad),
bootstrap(bootstrap),
skip_erase(skip_erase) {}
int run() override {
if ((this->start_addr % flash::SectorSize) != 0) {
// TODO: Support Unaligned addresses.
std::println("Only {} aligned addresses are supported!", size_t{flash::SectorSize});
return 0;
}
std::ifstream file(filename, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
std::println("Could not open the file {}!", filename);
return 0;
}
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
if (file_size == 0) {
std::println("File is empty");
return 0;
}
auto size = (file_size + flash::PageSize - 1) & ~(flash::PageSize - 1);
std::vector<uint8_t> buffer(size, 0xff);
if (!file.read(reinterpret_cast<char*>(buffer.data()), file_size)) {
std::println("Error reading the file.");
return 0;
}
bool addr4b = this->start_addr > 0xFFFFFF;
if (!bootstrap) {
this->flash.reset();
}
if (addr4b && !this->flash.enter_4b_addr()) {
std::println("Enter 4-byte address mode failed");
return 0;
}
if (quad && !this->flash.enable_quad(true)) {
std::println("enable quad failed");
return 0;
}
std::span<uint8_t> data(buffer);
auto progress_bar = ProgressBar(buffer.size(), 50, "Loading").with_throughput();
size_t addr = start_addr;
this->flash.write_enable();
while (data.size() > 0) {
if (!skip_erase && (addr % flash::SectorSize) == 0) {
auto erased = addr4b ? this->flash.template erase<4, flash::Opcode::SectorErase4b>(addr)
: this->flash.erase(addr);
if (!erased) {
std::println("Failed to erase block {:#x}", addr);
return 0;
}
}
this->flash.wait_not_busy();
std::optional<bool> res;
if (addr4b) {
res = this->flash.template single_page_program_non_blocking<4>(
addr, data.first<flash::PageSize>());
} else if (quad) {
res = this->flash.quad_page_program(addr, data.first<flash::PageSize>());
} else {
res = this->flash.single_page_program_non_blocking(addr, data.first<flash::PageSize>());
}
if (!res) {
std::println("Program page {:#x} failed.", addr);
return 0;
}
addr += flash::PageSize;
data = data.subspan(std::min(data.size(), std::size_t{flash::PageSize}));
progress_bar.update(buffer.size() - data.size());
}
this->flash.wait_not_busy();
this->flash.write_enable(false);
if (bootstrap) {
this->flash.reset();
}
return 1;
}
};
template <typename T>
struct LoadFileElf : public Commands<T> {
std::string& filename;
bool quad;
bool bootstrap;
bool skip_erase;
LoadFileElf(flash::Generic<T> f, std::string& filename, bool bootstrap = false,
bool skip_erase = false, bool quad = false)
: Commands<T>(f),
filename(filename),
quad(quad),
bootstrap(bootstrap),
skip_erase(skip_erase) {}
int run() override {
ELFIO::elfio reader;
if (!reader.load(filename)) {
std::println("Invalid ELF file");
return 0;
}
std::vector<ELFIO::segment*> load_segments;
for (auto& segment : reader.segments) {
if (segment->get_type() == ELFIO::PT_LOAD) {
load_segments.push_back(std::to_address(segment));
}
}
// Given a start and end interval, return the interval in sectors containing them,
// by rounding the start address down and end address up to the next sector-aligned
// address.
auto containing_sectors = [](auto start, auto end) {
return std::make_pair(start & ~(flash::SectorSize - 1),
(end + flash::SectorSize - 1) & ~(flash::SectorSize - 1));
};
bool addr4b = false;
auto erase_size = 0;
auto load_size = 0;
for (auto segment : load_segments) {
auto phys_start = segment->get_physical_address();
auto phys_end = phys_start + segment->get_memory_size();
auto sectors = containing_sectors(phys_start, phys_end);
load_size += segment->get_file_size();
erase_size += (sectors.second - sectors.first);
if (phys_start > 0xFFFFFF || phys_end > 0xFFFFFF) {
addr4b = true;
}
}
if (!bootstrap) {
this->flash.reset();
}
if (addr4b && !this->flash.enter_4b_addr()) {
std::println("Enter 4-byte address mode failed");
return 0;
}
if (quad && !this->flash.enable_quad(true)) {
std::println("enable quad failed");
return 0;
}
this->flash.write_enable(true);
std::optional<bool> res;
// The length of the data to be loaded into memory (file size) may be less than
// the size of the segment in memory (memory size), in which case the excess represents
// zero-initialised data (e.g .bss section). Therefore, the memory size is used for
// erasing the flash, and then the file resident data is loaded into it, which may be
// shorter.
// Erase sectors containing segment data.
if (!skip_erase) {
auto erased = 0;
auto erase_progress = ProgressBar(erase_size, 50, "Erasing").with_throughput();
for (auto segment : load_segments) {
auto phys_start = segment->get_physical_address();
auto phys_end = phys_start + segment->get_memory_size();
auto sectors = containing_sectors(phys_start, phys_end);
auto addr = sectors.first;
while (addr < sectors.second) {
res = addr4b ? this->flash.template erase<4, flash::Opcode::SectorErase4b>(addr)
: this->flash.erase(addr);
if (!res) {
std::println("Failed to erase block {:#x}", addr);
return 0;
}
addr += flash::SectorSize;
erased += flash::SectorSize;
erase_progress.update(erased);
}
}
}
this->flash.wait_not_busy();
// Then, load the segment data from the file.
auto loaded = 0;
auto load_progress = ProgressBar(load_size, 50, "Loading").with_throughput();
for (auto segment : load_segments) {
auto addr = segment->get_physical_address();
std::span<const uint8_t> data(reinterpret_cast<const uint8_t*>(segment->get_data()),
segment->get_file_size());
// Segments may start at an address that is not aligned to the flash page size.
// Page program commands may start within a page, but may wrap-around or be invalid
// if going over the page boundary, so only write until we are aligned to the page size.
if ((addr % flash::PageSize) != 0) {
auto to_next = flash::PageSize - (addr % flash::PageSize);
auto n = std::min(to_next, data.size());
std::vector<uint8_t> page(data.first(n).begin(), data.first(n).end());
if (addr4b) {
res = this->flash.template single_page_program_non_blocking<4>(addr, page);
} else if (quad) {
res = this->flash.quad_page_program(addr, page);
} else {
res = this->flash.single_page_program_non_blocking(addr, page);
}
if (!res) {
std::println("Program page {:#x} failed.", addr);
return 0;
}
this->flash.wait_not_busy();
addr += n;
loaded += n;
data = data.subspan(n);
load_progress.update(loaded);
}
// Now we are aligned to the flash page size, write pages as normal.
while (data.size() > 0) {
auto n = std::min(data.size(), std::size_t{flash::PageSize});
std::vector<uint8_t> page(data.first(n).begin(), data.first(n).end());
this->flash.wait_not_busy();
if (addr4b) {
res = this->flash.template single_page_program_non_blocking<4>(addr, page);
} else if (quad) {
res = this->flash.quad_page_program(addr, page);
} else {
res = this->flash.single_page_program_non_blocking(addr, page);
}
if (!res) {
std::println("Program page {:#x} failed.", addr);
return 0;
}
addr += n;
loaded += n;
data = data.subspan(n);
load_progress.update(loaded);
}
}
this->flash.wait_not_busy();
this->flash.write_enable(false);
if (bootstrap) {
this->flash.reset();
}
return 1;
}
};
template <typename T>
requires embeddedpp::Gpio<T>
struct GpioWrite {
T& gpio;
uint8_t pin;
bool value;
GpioWrite(T& gpio, uint8_t pin, bool value) : gpio(gpio), pin(pin), value(value) {}
int run() {
if (is_error(gpio.set_pin(pin, value))) {
std::println("GPIO write failed");
return 1;
}
std::println("GPIO{} = {}", pin, value ? 1 : 0);
return 0;
}
};
} // namespace commands