-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrwx.cc
1957 lines (1587 loc) · 47.2 KB
/
rwx.cc
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* bcm2-utils
* Copyright (C) 2016 Joseph Lehner <[email protected]>
*
* bcm2-utils 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 3 of the License, or
* (at your option) any later version.
*
* bcm2-utils 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 bcm2-utils. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <unistd.h>
#include <iostream>
#include <cstddef>
#include <fstream>
#include <algorithm>
#include "progress.h"
#include "rwcode2.h"
#include "util.h"
#include "rwx.h"
#include "ps.h"
#ifdef BCM2DUMP_WITH_SNMP
#include "snmp.h"
#endif
#define BFC_FLASH_READ_DIRECT
using namespace std;
namespace bcm2dump {
namespace {
class bad_chunk_line : public runtime_error
{
bool m_critical;
public:
template<class T> static bad_chunk_line critical(const T& t)
{ return { t, true }; }
template<class T> static bad_chunk_line regular(const T& t)
{ return { t, false }; }
static bad_chunk_line regular()
{ return { "bad_chunk_line", false }; }
bool critical() const
{ return m_critical; }
private:
bad_chunk_line(const exception& cause, bool critical)
: runtime_error(cause.what()), m_critical(critical)
{}
bad_chunk_line(const string& what, bool critical)
: runtime_error(what), m_critical(critical)
{}
};
const unsigned max_retry_count = 5;
template<class T> T hex_cast(const std::string& str)
{
return lexical_cast<T>(str, 16);
}
inline void patch32(string& buf, string::size_type offset, uint32_t n)
{
patch<uint32_t>(buf, offset, h_to_be(n));
}
template<class T> T align_to(const T& num, const T& alignment)
{
if (num % alignment) {
return ((num / alignment) + 1) * alignment;
}
return num;
}
streampos tell(ostream& os)
{
return os.tellp();
}
streampos tell(istream& is)
{
return is.tellg();
}
void seek(istream& is, streamoff off, ios_base::seekdir dir)
{
is.seekg(off, dir);
}
void seek(ostream& os, streamoff off, ios_base::seekdir dir)
{
os.seekp(off, dir);
}
template<class T> uint32_t get_stream_size(T& stream)
{
auto ioex = scoped_ios_exceptions::none(stream);
auto cur = tell(stream);
if (cur == -1) {
return 0;
}
seek(stream, 0, ios_base::end);
if (!stream.good() || tell(stream) <= cur) {
throw runtime_error("failed to determine length of stream");
}
uint32_t length = tell(stream) - cur;
seek(stream, cur, ios_base::beg);
return length;
}
uint32_t parse_num(const string& str)
{
return lexical_cast<uint32_t>(str, 0);
}
string parse_hex_data(const string& line, bool is_byte_data)
{
auto tok = split(line, ' ', false);
if (tok.empty()) {
throw bad_chunk_line::regular();
}
string linebuf;
for (auto num : tok) {
uint32_t n;
try {
n = hex_cast<uint32_t>(num);
} catch (const exception& e) {
throw bad_chunk_line::regular(e);
}
if (is_byte_data) {
if (n > 0xff) {
throw bad_chunk_line::regular("invalid byte: 0x" + to_hex(n));
}
linebuf += char(n);
} else {
linebuf += to_buf(be_to_h(n));
}
}
return linebuf;
}
uint32_t read_image_length(rwx& rwx, uint32_t offset)
{
rwx.silent(true);
ps_header hdr(rwx.read(offset, 92));
rwx.silent(false);
return hdr.hcs_valid() ? 92 + hdr.length() : 0;
}
void parse_offset_size(rwx& rwx, const string& arg, uint32_t& offset, uint32_t& length, bool write)
{
auto tokens = split(arg, ',');
if (tokens.empty() || tokens.size() > 2) {
throw user_error("invalid argument '" + arg + "'");
}
bool read_hdr = false;
offset = 0;
length = 0;
if (tokens.size() == 2) {
if (tokens[1] != "auto") {
length = parse_num(tokens[1]);
} else if (!write) {
read_hdr = true;
}
}
try {
offset = parse_num(tokens[0]);
if (!length && !write) {
length = read_image_length(rwx, offset);
}
} catch (const bad_lexical_cast& e) {
tokens = split(tokens[0], '+');
if (tokens.empty() || tokens.size() > 2) {
throw user_error("invalid argument '" + arg + "'");
}
const addrspace::part& p = rwx.space().partition(tokens[0]);
rwx.set_partition(p);
offset = p.offset();
if (!length && !read_hdr) {
length = p.size();
}
if (!length && !write) {
length = read_image_length(rwx, offset);
}
if (!write && !length && !p.size()) {
throw user_error("size of partition '" + p.name() + "' is unknown, and size argument is missing");
}
if (tokens.size() == 2) {
uint32_t n = parse_num(tokens[1]);
offset += n;
if (!write && !length) {
length = p.size() - n;
}
} else if (!length && !write) {
length = p.size();
}
}
}
// rwx base class for a command line interface where you
// enter a command which in turn displays a (hex) dump of the
// data. for now, all rwx implementations are based on this
// class.
class parsing_rwx : public rwx
{
public:
virtual ~parsing_rwx() {}
unsigned capabilities() const override
{ return cap_read; }
protected:
virtual string read_chunk(uint32_t offset, uint32_t length) override final
{
return read_chunk_impl(offset, length, 0);
}
virtual string read_special(uint32_t offset, uint32_t length) override;
virtual unsigned chunk_timeout(uint32_t offset, uint32_t length) const
{ return 0; }
virtual string read_chunk_impl(uint32_t offset, uint32_t length, uint32_t retries);
// issues a command that displays the requested chunk
virtual void do_read_chunk(uint32_t offset, uint32_t length) = 0;
// checks if the line is junk (as opposed to a possible chunk line)
virtual bool is_ignorable_line(const string& line) = 0;
// parses one line of data
virtual string parse_chunk_line(const string& line, uint32_t offset) = 0;
// called if a chunk was not successfully read
virtual void on_chunk_retry(uint32_t offset, uint32_t length) {}
bcm2dump::sp<cmdline_interface> interface() const
{ return dynamic_pointer_cast<cmdline_interface>(m_intf); }
};
string parsing_rwx::read_special(uint32_t offset, uint32_t length)
{
require_capability(cap_special);
string buf = read_chunk(0, 0);
if (offset >= buf.size()) {
return "";
} else if (!length) {
return buf.substr(offset);
} else {
return buf.substr(offset, min(buf.size(), string::size_type(length)));
}
}
string parsing_rwx::read_chunk_impl(uint32_t offset, uint32_t length, uint32_t retries)
{
logger::t() << "read_chunk_impl: calling do_read_chunk" << endl;
do_read_chunk(offset, length);
uint32_t pos = offset;
string chunk;
logger::t() << "read_chunk_impl: consuming lines" << endl;
interface()->foreach_line_raw([this, &chunk, &pos, &length, &retries] (const string& line) {
throw_if_interrupted();
string tline = trim(line);
if (!is_ignorable_line(tline)) {
try {
string linebuf = parse_chunk_line(tline, pos);
pos += linebuf.size();
chunk += linebuf;
update_progress(pos, chunk.size());
if (linebuf.empty()) {
logger::t() << "no bytes found in '" << tline << "'" << endl;
}
} catch (const bad_chunk_line& e) {
string msg = "bad chunk line @" + to_hex(pos) + ": '" + tline + "' (" + e.what() + ")";
if (e.critical() && retries >= max_retry_count) {
throw runtime_error(msg);
}
logger::t() << endl << msg << endl;
} catch (const exception& e) {
logger::d() << "error while parsing '" << tline << "': " << e.what() << endl;
return true;
}
}
return !(chunk.size() < length);
}, 10000);
logger::t() << "read_chunk_impl: done reading lines" << endl;
// consume any more output
interface()->wait_quiet(20);
if (length && (chunk.size() != length)) {
string msg = "read incomplete chunk 0x" + to_hex(offset)
+ ": " + to_string(chunk.size()) + "/" +to_string(length);
if (retries < max_retry_count) {
// if the dump is still underway, we need to wait for it to finish
// before issuing the next command. wait for up to 10 seconds.
if (interface()->wait_ready()) {
logger::d() << endl << msg << "; retrying" << endl;
on_chunk_retry(offset, length);
return read_chunk_impl(offset, length, retries + 1);
}
}
throw runtime_error(msg);
}
return chunk;
}
class bfc_ram : public parsing_rwx
{
public:
virtual ~bfc_ram() {}
virtual limits limits_read() const override;
virtual limits limits_write() const override
{
// diag writemem only supports writing bytes
if (m_diag_cmd.empty()) {
return limits(4, 1, 4);
} else {
return limits(1, 1, 1);
}
}
virtual void set_interface(const interface::sp& intf) override;
unsigned capabilities() const override
{ return m_space.is_ram() ? m_ram_caps : (cap_read | (m_space.is_writable() ? cap_write : 0)); }
protected:
virtual bool exec_impl(uint32_t offset) override;
virtual bool write_chunk(uint32_t offset, const string& chunk) override;
virtual bool is_ignorable_line(const string& line) override;
virtual void do_read_chunk(uint32_t offset, uint32_t length) override;
virtual string parse_chunk_line(const string& line, uint32_t offset) override;
private:
string m_diag_cmd;
unsigned m_ram_caps = cap_rwx;
};
rwx::limits bfc_ram::limits_read() const
{
auto v = interface()->version();
// FIXME unify this for all parsing_rwx types
return { 4, 16, v.get_opt_num("bfc:ram_readsize", 2 * 8192) };
}
void bfc_ram::set_interface(const interface::sp& intf)
{
parsing_rwx::set_interface(intf);
m_diag_cmd = "";
m_ram_caps = 0;
auto lines = interface()->run_raw("/find_command call", 200);
if (find(lines.begin(), lines.end(), "/call") != lines.end()) {
m_ram_caps = cap_exec;
} else {
logger::d() << "no /call command found" << endl;
}
// TODO actually check for write capabilities too
lines = interface()->run_raw("/find_command read_memory", 200);
if (find(lines.begin(), lines.end(), "/read_memory") != lines.end()) {
// we have a system-wide /read_memory command
m_ram_caps |= (cap_read | cap_write);
return;
}
bool strip_console_prefix = false;
interface()->run("cd /");
lines = interface()->run_raw("/find_command readmem", 200);
auto it = find_if(lines.begin(), lines.end(), [&strip_console_prefix](auto l) {
// Some devices show a CM/Console> prompt after telnet login,
// even after a `cd /` command. In that case, `/find_command readmem`
// might return `/Console/system/diag`, even though it must be called
// as `/system/diag`.
if (l.find("CM/Console>") != string::npos) {
strip_console_prefix = true;
}
return ends_with(l, "/diag readmem");
});
if (it != lines.end()) {
m_diag_cmd = it->substr(0, it->size() - strlen(" readmem"));
if (strip_console_prefix && starts_with(m_diag_cmd, "/Console/")) {
// Keep the leading slash!
m_diag_cmd = m_diag_cmd.substr(strlen("/Console"));
}
logger::d() << "using " << m_diag_cmd << " command for memory access" << endl;
m_ram_caps |= (cap_read | cap_write);
return;
}
}
bool bfc_ram::exec_impl(uint32_t offset)
{
return interface()->run("/call func -a 0x" + to_hex(offset), "Calling function 0x");
};
bool bfc_ram::write_chunk(uint32_t offset, const string& chunk)
{
if (m_diag_cmd.empty()) {
uint32_t val = chunk.size() == 4 ? be_to_h(extract<uint32_t>(chunk)) : chunk[0];
return interface()->run("/write_memory -s " + to_string(chunk.size()) + " 0x" +
to_hex(offset, 0) + " 0x" + to_hex(val, 0), "Writing");
} else {
return interface()->run(m_diag_cmd + " writemem 0x" + to_hex(offset, 0) + " 0x" +
to_hex(chunk[0] & 0xff, 0), "Writing");
}
}
void bfc_ram::do_read_chunk(uint32_t offset, uint32_t length)
{
if (m_diag_cmd.empty()) {
interface()->writeln("/read_memory -s 4 -n " + to_string(length) + " 0x" + to_hex(offset));
} else {
interface()->writeln(m_diag_cmd + " readmem -s 4 -n " + to_string(length) + " 0x" + to_hex(offset));
}
}
bool bfc_ram::is_ignorable_line(const string& line)
{
if (line.size() >= 50) {
if (line.substr(8, 2) == ": " && line.substr(48, 2) == " |") {
return false;
} else if (contains(line, ": ") && (contains(line, " | ") || ends_with(line, " |"))) {
// if another message is printed by the firmware, the dump
// output sometimes switches to an all-decimal format.
return false;
}
}
return true;
}
string bfc_ram::parse_chunk_line(const string& line, uint32_t offset)
{
const char* fmts[] = {
"%x: %x %x %x %x",
"%u: %u %u %u %u",
};
uint32_t data[4];
uint32_t off;
int n;
for (const char* fmt : fmts) {
n = sscanf(line.c_str(), fmt, &off, &data[0],
&data[1], &data[2], &data[3]);
if (n > 1 && off == offset) {
break;
}
}
if (!n) {
throw bad_chunk_line::regular();
} else if (off != offset) {
throw bad_chunk_line::critical("offset mismatch");
}
string linebuf;
for (int i = 0; i < (n - 1); ++i) {
linebuf += to_buf(be_to_h(data[i]));
}
return linebuf;
}
class bfc_flash2 : public bfc_ram
{
public:
virtual ~bfc_flash2() {}
virtual unsigned capabilities() const override
{ return cap_read; }
static bool is_supported(const interface::sp& intf, const string& space)
{
auto ver = intf->version();
if (ver.name().empty()) {
return false;
}
auto cfg = ver.codecfg();
if (!cfg["buffer"]) {
return false;
}
auto funcs = ver.functions(space);
if (!funcs["read"].addr() /*&& !funcs["write"].addr()*/) {
return false;
}
return true;
}
protected:
virtual void init(uint32_t offset, uint32_t length, bool write) override
{
bfc_ram::init(offset, length, write);
auto ver = interface()->version();
m_cfg = ver.codecfg();
uint32_t buflen = m_cfg["buflen"];
if (buflen && length > buflen) {
throw user_error("requested length exceeds buffer size ("
+ to_string(buflen) + " b)");
}
m_dump_offset = offset;
m_dump_length = length;
m_funcs = ver.functions(m_space.name());
call_open_close("open", offset, length);
patch(m_funcs["read"]);
}
virtual void cleanup() override
{
bfc_ram::cleanup();
call_open_close("close", m_dump_offset, m_dump_length);
}
virtual unsigned chunk_timeout(uint32_t offset, uint32_t length) const override
{
return 5 * 1000;
}
virtual string parse_chunk_line(const string& line, uint32_t offset) override
{
return bfc_ram::parse_chunk_line(line, m_cfg["buffer"] + (offset % limits_read().max));
}
virtual void do_read_chunk(uint32_t offset, uint32_t length) override
{
call_read(offset, length);
bfc_ram::do_read_chunk(m_cfg["buffer"], length);
}
private:
void patch(const func& f)
{
for (auto p : f.patches()) {
if (p->addr && !write_chunk(p->addr | interface()->profile()->kseg1(), to_buf(h_to_be(p->word)))) {
throw runtime_error("failed to patch word at 0x" + to_hex(p->addr));
}
}
}
void call(const string& cmd, const string& name, unsigned timeout = 100)
{
interface()->run(cmd, timeout);
}
void call_read(uint32_t offset, uint32_t length)
{
func read = m_funcs["read"];
if (read.addr()) {
string cmd = mkcmd(read);
if (read.args() == BCM2_READ_FUNC_BOL) {
add_args(cmd, { m_cfg["buffer"], offset, length });
} else if (read.args() == BCM2_READ_FUNC_OBL) {
add_args(cmd, { offset, m_cfg["buffer"], length });
} else {
throw runtime_error("unsupported 'read' args");
}
//patch(read);
call(cmd, "read");
}
}
void call_open_close(const string& name, uint32_t offset, uint32_t length)
{
func f = m_funcs[name];
if (f.addr()) {
string cmd = mkcmd(f, { offset });
if (f.args() == BCM2_ARGS_OL) {
add_arg(cmd, length);
} else if (f.args() == BCM2_ARGS_OE) {
add_arg(cmd, offset + length);
} else {
throw runtime_error("unsupported '" + name + "' args");
}
//patch(f);
call(cmd, name);
}
}
string mkcmd(const func& f, vector<uint32_t> a = {})
{
string ret = "/call func -a 0x" + to_hex(f.addr() | interface()->profile()->kseg1());
add_args(ret, a);
return ret;
}
static void add_arg(string& cmd, uint32_t arg)
{ cmd += " 0x" + to_hex(arg); }
static void add_args(string& cmd, vector<uint32_t> args)
{
for (auto a : args) {
add_arg(cmd, a);
}
}
uint32_t m_dump_offset = 0;
uint32_t m_dump_length = 0;
version::funcmap m_funcs;
version::u32map m_cfg;
};
class bfc_flash : public parsing_rwx
{
public:
virtual ~bfc_flash()
{ cleanup(); }
virtual limits limits_read() const override;
virtual limits limits_write() const override
{ return limits(1, 1, 4); }
protected:
virtual void init(uint32_t offset, uint32_t length, bool write) override;
virtual void cleanup() override;
virtual bool write_chunk(uint32_t offset, const string& buf) override;
virtual void do_read_chunk(uint32_t offset, uint32_t length) override;
virtual bool is_ignorable_line(const string& line) override;
virtual string parse_chunk_line(const string& line, uint32_t offset) override;
virtual void on_chunk_retry(uint32_t offset, uint32_t length) override;
private:
uint32_t to_partition_offset(uint32_t offset) const;
bool use_direct_read() const;
};
rwx::limits bfc_flash::limits_read() const
{
auto v = interface()->version();
// FIXME unify this for all parsing_rwx types
auto readsize = v.get_opt_num("bfc:flash_readsize", 0);
if (readsize) {
return { readsize, readsize, readsize };
} else {
return { 1, 16, 4096 };
}
}
void bfc_flash::init(uint32_t, uint32_t, bool write)
{
if (m_partition.name().empty()) {
throw user_error("partition name required");
}
for (unsigned pass = 0; pass < 2; ++pass) {
auto lines = interface()->run("/flash/open " + m_partition.altname(), 5000);
bool opened = false;
for (auto line : lines) {
if (contains(line, "opened twice")) {
opened = false;
} else if (contains(line, "driver opened")) {
opened = true;
}
}
if (opened) {
break;
} else if (pass == 0) {
logger::d() << "reinitializing flash driver" << endl;
cleanup();
interface()->run("/flash/deinit", "Deinitializing");
interface()->run("/flash/init", 5000);
} else {
throw runtime_error("failed to open partition " + m_partition.name());
}
}
}
void bfc_flash::cleanup()
{
interface()->run("/flash/close", "driver closed");
}
bool bfc_flash::write_chunk(uint32_t offset, const std::string& chunk)
{
offset = to_partition_offset(offset);
uint32_t val = chunk.size() == 4 ? be_to_h(extract<uint32_t>(chunk)) : chunk[0];
return interface()->run("/flash/write " + to_string(chunk.size()) + " 0x"
+ to_hex(offset) + " 0x" + to_hex(val), "successfully written");
}
void bfc_flash::do_read_chunk(uint32_t offset, uint32_t length)
{
offset = to_partition_offset(offset);
if (use_direct_read()) {
interface()->writeln("/flash/readDirect " + to_string(length) + " " + to_string(offset));
} else {
interface()->writeln("/flash/read 4 " + to_string(length) + " " + to_string(offset));
}
}
bool bfc_flash::is_ignorable_line(const string& line)
{
if (use_direct_read()) {
if (line.size() >= 53) {
if (line.substr(11, 3) == " " && line.substr(25, 3) == " ") {
return false;
}
}
} else if (line.size() >= 36) {
if (line[8] == ' ' && line[17] == ' ' && line[26] == ' ') {
return false;
}
}
return true;
}
string bfc_flash::parse_chunk_line(const string& line, uint32_t offset)
{
return parse_hex_data(line, use_direct_read());
}
uint32_t bfc_flash::to_partition_offset(uint32_t offset) const
{
if (offset < m_partition.offset()) {
// just to be safe. this should never happen
throw runtime_error("offset 0x" + to_hex(offset) + " is less than partition offset");
}
return offset - m_partition.offset();
}
bool bfc_flash::use_direct_read() const
{
auto v = interface()->version();
if (v.has_opt("bfc:flash_read_direct")) {
return v.get_opt_num("bfc:flash_read_direct");
}
#ifdef BFC_FLASH_READ_DIRECT
return true;
#else
return false;
#endif
}
void bfc_flash::on_chunk_retry(uint32_t offset, uint32_t length)
{
auto v = interface()->version();
if (v.get_opt_num("bfc:flash_reinit_on_retry", false)) {
cleanup();
init(0, 0, false);
}
}
class bootloader_ram : public parsing_rwx
{
public:
virtual ~bootloader_ram() {}
virtual limits limits_read() const override
{ return limits(4, 4, 4); }
virtual limits limits_write() const override
{ return limits(4, 4, 4); }
virtual unsigned capabilities() const override
{ return cap_rwx; }
protected:
virtual void init(uint32_t offset, uint32_t length, bool write) override;
virtual void cleanup() override;
virtual bool write_chunk(uint32_t offset, const string& chunk) override;
virtual bool exec_impl(uint32_t offset) override;
virtual void do_read_chunk(uint32_t offset, uint32_t length) override;
virtual bool is_ignorable_line(const string& line) override;
virtual string parse_chunk_line(const string& line, uint32_t offset) override;
private:
bool m_write = false;
};
void bootloader_ram::init(uint32_t offset, uint32_t length, bool write)
{
if (!write) {
interface()->write("r");
} else {
interface()->writeln();
}
m_write = write;
}
void bootloader_ram::cleanup()
{
if (!m_write) {
interface()->run("\r");
} else {
interface()->writeln();
}
}
bool bootloader_ram::write_chunk(uint32_t offset, const string& chunk)
{
try {
if (!interface()->run("w", "Write memory.", true)) {
return false;
}
interface()->writeln(to_hex(offset, 0));
uint32_t val = be_to_h(extract<uint32_t>(chunk));
interface()->writeln(to_hex(val));
return true;
} catch (const exception& e) {
// ensure that we're in a sane state
interface()->run("\r");
return false;
}
}
void bootloader_ram::do_read_chunk(uint32_t offset, uint32_t length)
{
interface()->writeln("0x" + to_hex(offset, 0));
}
bool bootloader_ram::is_ignorable_line(const string& line)
{
if (contains(line, "Value at") || contains(line, "(hex)")) {
return false;
}
return true;
}
string bootloader_ram::parse_chunk_line(const string& line, uint32_t offset)
{
if (line.find("Value at") == 0) {
if (offset != hex_cast<uint32_t>(line.substr(9, 8))) {
throw bad_chunk_line::critical("offset mismatch");
}
return to_buf(h_to_be(hex_cast<uint32_t>(line.substr(19, 8))));
}
throw bad_chunk_line::regular();
}
bool bootloader_ram::exec_impl(uint32_t offset)
{
interface()->run("");
if (interface()->run("j", "address (hex):", true)) {
interface()->writeln(to_hex(offset));
return true;
}
return false;
}
// some Netgear bootloaders have a hidden option `c`, that allows reading from flash directly
class bootloader_flash : public parsing_rwx
{
public:
virtual ~bootloader_flash() {}
virtual limits limits_write() const override
{ return limits(0, 0, 0); }
virtual limits limits_read() const override
{ return limits(4, 4, 0x4000); }
virtual unsigned capabilities() const override
{ return cap_read; }
static bool is_supported(const interface::sp& intf, const addrspace& space)
{
auto i = dynamic_pointer_cast<cmdline_interface>(intf);
if (i && enter_flash_read_mode(i)) {
// exit mode
i->writeln("");
return true;
}
return false;
}
protected:
virtual void do_read_chunk(uint32_t offset, uint32_t length) override
{
if (enter_flash_read_mode(interface())) {
interface()->writeln(to_hex(offset) + " " + to_string(length));
} else {
throw runtime_error("failed to enter flash read mode");
}
}
virtual bool is_ignorable_line(const string& line) override
{
return line.size() < 10 || line.substr(8, 2) != "h: ";
}
virtual string parse_chunk_line(const string& line, uint32_t offset) override
{
if (offset != hex_cast<uint32_t>(line.substr(0, 8))) {
throw bad_chunk_line::critical("offset mismatch");
}
return parse_hex_data(line.substr(10), true);
}
private:
static bool enter_flash_read_mode(const bcm2dump::sp<cmdline_interface>& intf)
{
intf->run("");
return intf->run("c", "read flash. offset(H) count:", true);
}
};
/**
* TODO
*
* bootloader2 interface:
* > r hexAddr [width] Display memory location (1/2/4 bytes)
* > w hexAddr hexVal [width] Write memory location (1/2/4 bytes)
* > d hexAddr length [width] Dump memory
*
* There's no command to execute code, so we have to hijack the function
* of another command, updating its code to
*
* li $at, [jump address]
* jr $at
* nop
*
* A good candidate would be 'z' (Cause exception).
*/
class bolt_ram : public parsing_rwx
{
public: