forked from system76/ec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
671 lines (614 loc) · 22.8 KB
/
main.rs
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
// SPDX-License-Identifier: MIT
use clap::{Arg, App, AppSettings, SubCommand};
use ectool::{
Access,
AccessHid,
AccessLpcLinux,
AccessLpcSim,
Ec,
Error,
Firmware,
SecurityState,
StdTimeout,
Spi,
SpiRom,
SpiTarget,
};
use hidapi::HidApi;
use std::{
fs,
process,
str,
time::Duration,
thread,
};
unsafe fn console(ec: &mut Ec<Box<dyn Access>>) -> Result<(), Error> {
//TODO: driver support for reading debug region?
let access = ec.access();
let mut head = access.read_debug(0)? as usize;
loop {
let tail = access.read_debug(0)? as usize;
if tail == 0 || head == tail {
thread::sleep(Duration::from_millis(1));
} else {
while head != tail {
head += 1;
if head >= 256 { head = 1; }
let c = access.read_debug(head as u8)?;
print!("{}", c as char);
}
}
}
}
unsafe fn flash_read<S: Spi>(spi: &mut SpiRom<S, StdTimeout>, rom: &mut [u8], sector_size: usize) -> Result<(), Error> {
let mut address = 0;
while address < rom.len() {
eprint!("\rSPI Read {}K", address / 1024);
let next_address = address + sector_size;
let count = spi.read_at(address as u32, &mut rom[address..next_address])?;
if count != sector_size {
eprintln!("\ncount {} did not match sector size {}", count, sector_size);
return Err(Error::Verify);
}
address = next_address;
}
eprintln!("\rSPI Read {}K", address / 1024);
Ok(())
}
unsafe fn flash_inner(ec: &mut Ec<Box<dyn Access>>, firmware: &Firmware, target: SpiTarget, scratch: bool) -> Result<(), Error> {
let rom_size = 128 * 1024;
let mut new_rom = firmware.data.to_vec();
while new_rom.len() < rom_size {
new_rom.push(0xFF);
}
let mut spi_bus = ec.spi(target, scratch)?;
let mut spi = SpiRom::new(
&mut spi_bus,
StdTimeout::new(Duration::new(1, 0))
);
let sector_size = spi.sector_size();
let mut rom = vec![0xFF; rom_size];
flash_read(&mut spi, &mut rom, sector_size)?;
eprintln!("Saving ROM to backup.rom");
fs::write("backup.rom", &rom).map_err(|_| Error::Verify)?;
// Program chip, sector by sector
//TODO: write signature last
{
let mut address = 0;
while address < rom_size {
eprint!("\rSPI Write {}K", address / 1024);
let next_address = address + sector_size;
let mut matches = true;
let mut erased = true;
let mut new_erased = true;
for i in address..next_address {
if rom[i] != new_rom[i] {
matches = false;
}
if rom[i] != 0xFF {
erased = false;
}
if new_rom[i] != 0xFF {
new_erased = false;
}
}
if ! matches {
if ! erased {
spi.erase_sector(address as u32)?;
}
if ! new_erased {
let count = spi.write_at(address as u32, &new_rom[address..next_address])?;
if count != sector_size {
eprintln!("\nWrite count {} did not match sector size {}", count, sector_size);
return Err(Error::Verify);
}
}
}
address = next_address;
}
eprintln!("\rSPI Write {}K", address / 1024);
// Verify chip write
flash_read(&mut spi, &mut rom, sector_size)?;
for i in 0..rom.len() {
if rom[i] != new_rom[i] {
eprintln!("Failed to program: {:X} is {:X} instead of {:X}", i, rom[i], new_rom[i]);
return Err(Error::Verify);
}
}
}
eprintln!("Successfully programmed SPI ROM");
Ok(())
}
unsafe fn flash(ec: &mut Ec<Box<dyn Access>>, path: &str, target: SpiTarget, force: bool) -> Result<(), Error> {
let scratch = true;
//TODO: remove unwraps
let firmware_data = fs::read(path).unwrap();
let firmware = Firmware::new(&firmware_data).unwrap();
println!("file board: {:?}", str::from_utf8(firmware.board));
println!("file version: {:?}", str::from_utf8(firmware.version));
let data_size = ec.access().data_size();
{
let mut data = vec![0; data_size];
let size = ec.board(&mut data)?;
let ec_board = &data[..size];
println!("ec board: {:?}", str::from_utf8(ec_board));
assert!(force || ec_board == firmware.board, "file board does not match ec board");
}
{
let mut data = vec![0; data_size];
let size = ec.version(&mut data)?;
let ec_version = &data[..size];
println!("ec version: {:?}", str::from_utf8(ec_version));
}
if scratch {
// Wait for any key releases
eprintln!("Waiting 5 seconds for all keys to be released");
thread::sleep(Duration::new(5, 0));
}
eprintln!("Sync");
let _ = process::Command::new("sync").status();
let res = flash_inner(ec, &firmware, target, scratch);
eprintln!("Result: {:X?}", res);
eprintln!("Sync");
let _ = process::Command::new("sync").status();
if scratch {
eprintln!("System will shut off in 5 seconds");
thread::sleep(Duration::new(5, 0));
eprintln!("Sync");
let _ = process::Command::new("sync").status();
ec.reset()?;
}
res
}
unsafe fn info(ec: &mut Ec<Box<dyn Access>>) -> Result<(), Error> {
let data_size = ec.access().data_size();
{
print!("board: ");
let mut data = vec![0; data_size];
let size = ec.board(&mut data)?;
for &b in data[..size].iter() {
print!("{}", b as char);
}
println!();
}
{
print!("version: ");
let mut data = vec![0; data_size];
let size = ec.version(&mut data)?;
for &b in data[..size].iter() {
print!("{}", b as char);
}
println!();
}
Ok(())
}
unsafe fn matrix(ec: &mut Ec<Box<dyn Access>>) -> Result<(), Error> {
let data_size = ec.access().data_size();
let mut data = vec![0; data_size];
ec.matrix_get(&mut data)?;
let rows = *data.get(0).unwrap_or(&0);
let cols = *data.get(1).unwrap_or(&0);
let mut byte = 2;
let mut bit = 0;
for _row in 0..rows {
for _col in 0..cols {
if (data.get(byte).unwrap_or(&0) & (1 << bit)) != 0 {
print!("#");
} else {
print!("-");
}
bit += 1;
if bit >= 8 {
byte += 1;
bit = 0;
}
}
println!();
}
Ok(())
}
unsafe fn print(ec: &mut Ec<Box<dyn Access>>, message: &[u8]) -> Result<(), Error> {
ec.print(message)?;
Ok(())
}
unsafe fn fan_get(ec: &mut Ec<Box<dyn Access>>, index: u8) -> Result<(), Error> {
let duty = ec.fan_get(index)?;
println!("{}", duty);
Ok(())
}
unsafe fn fan_set(ec: &mut Ec<Box<dyn Access>>, index: u8, duty: u8) -> Result<(), Error> {
ec.fan_set(index, duty)
}
unsafe fn keymap_get(ec: &mut Ec<Box<dyn Access>>, layer: u8, output: u8, input: u8) -> Result<(), Error> {
let value = ec.keymap_get(layer, output, input)?;
println!("{:04X}", value);
Ok(())
}
unsafe fn keymap_set(ec: &mut Ec<Box<dyn Access>>, layer: u8, output: u8, input: u8, value: u16) -> Result<(), Error> {
ec.keymap_set(layer, output, input, value)
}
unsafe fn security_get(ec: &mut Ec<Box<dyn Access>>) -> Result<(), Error> {
println!("{:?}", ec.security_get()?);
Ok(())
}
unsafe fn security_set(ec: &mut Ec<Box<dyn Access>>, state: SecurityState) -> Result<(), Error> {
ec.security_set(state)?;
println!("Shut down the system for the security state to take effect");
Ok(())
}
fn parse_color(s: &str) -> Result<(u8, u8, u8), String> {
let r = u8::from_str_radix(&s[0..2], 16);
let g = u8::from_str_radix(&s[2..4], 16);
let b = u8::from_str_radix(&s[4..6], 16);
match (r, g, b) {
(Ok(r), Ok(g), Ok(b)) if s.len() == 6 => Ok((r, g, b)),
_ => Err(format!("Invalid color '{}'", s)),
}
}
fn main() {
let matches = App::new("dasharo_ectool")
.setting(AppSettings::SubcommandRequired)
.arg(Arg::with_name("access")
.long("access")
.possible_values(&["lpc-linux", "lpc-sim", "hid"])
.default_value("lpc-linux")
)
.subcommand(SubCommand::with_name("console"))
.subcommand(SubCommand::with_name("fan")
.arg(Arg::with_name("index")
.allow_invalid_utf8(true)
.required(true)
)
.arg(Arg::with_name("duty")
.value_parser(clap::value_parser!(u8))
)
)
.subcommand(SubCommand::with_name("flash")
.arg(Arg::with_name("path")
.required(true)
)
.arg(Arg::with_name("force")
.long("force")
.help("Bypass board compatibility check. This option will force firmware flash even if target board does not match. Use with caution."))
)
.subcommand(SubCommand::with_name("flash_backup")
.arg(Arg::with_name("path")
.required(true)
)
.arg(Arg::with_name("force")
.long("force")
.help("Bypass board compatibility check. This option will force firmware flash even if target board does not match. Use with caution."))
)
.subcommand(SubCommand::with_name("info"))
.subcommand(SubCommand::with_name("keymap")
.arg(Arg::with_name("layer")
.value_parser(clap::value_parser!(u8))
.required(true)
)
.arg(Arg::with_name("output")
.value_parser(clap::value_parser!(u8))
.required(true)
)
.arg(Arg::with_name("input")
.value_parser(clap::value_parser!(u8))
.required(true)
)
.arg(Arg::with_name("value"))
)
.subcommand(SubCommand::with_name("led_color")
.arg(Arg::with_name("index")
.value_parser(clap::value_parser!(u8))
.required(true)
)
.arg(Arg::with_name("value")
.value_parser(parse_color)
)
)
.subcommand(SubCommand::with_name("led_value")
.arg(Arg::with_name("index")
.value_parser(clap::value_parser!(u8))
.required(true)
)
.arg(Arg::with_name("value")
.value_parser(clap::value_parser!(u8))
)
)
.subcommand(SubCommand::with_name("led_mode")
.arg(Arg::with_name("layer")
.value_parser(clap::value_parser!(u8))
.required(true)
)
.arg(Arg::with_name("mode")
.value_parser(clap::value_parser!(u8))
.requires("speed")
)
.arg(Arg::with_name("speed")
.value_parser(clap::value_parser!(u8))
)
)
.subcommand(SubCommand::with_name("led_save"))
.subcommand(SubCommand::with_name("matrix"))
.subcommand(SubCommand::with_name("print")
.arg(Arg::with_name("message")
.required(true)
.multiple(true)
)
)
.subcommand(SubCommand::with_name("set_no_input")
.arg(Arg::with_name("value")
.possible_values(&["true", "false"])
.required(true)
)
)
.subcommand(SubCommand::with_name("security")
.arg(Arg::with_name("state")
.possible_values(&["lock", "unlock"])
)
)
.get_matches();
let get_ec = || -> Result<_, Error> {
unsafe {
match matches.value_of("access").unwrap() {
"lpc-linux" => {
let access = AccessLpcLinux::new(Duration::new(1, 0))?;
Ok(Ec::new(access)?.into_dyn())
},
"lpc-sim" => {
let access = AccessLpcSim::new(Duration::new(1, 0))?;
Ok(Ec::new(access)?.into_dyn())
},
"hid" => {
let api = HidApi::new()?;
for info in api.device_list() {
#[allow(clippy::single_match)]
match (info.vendor_id(), info.product_id(), info.interface_number()) {
// System76 launch_1
(0x3384, 0x0001, 1) |
// System76 launch_lite_1
(0x3384, 0x0005, 1) |
// System76 launch_2
(0x3384, 0x0006, 1) |
// System76 launch_heavy_1
(0x3384, 0x0007, 1) => {
let device = info.open_device(&api)?;
let access = AccessHid::new(device, 10, 100)?;
return Ok(Ec::new(access)?.into_dyn());
}
_ => {},
}
}
Err(hidapi::HidError::OpenHidDeviceError.into())
}
_ => unreachable!(),
}
}
};
let mut ec = match get_ec() {
Ok(ec) => ec,
Err(err) => {
eprintln!("failed to connect to EC: {:X?}", err);
process::exit(1);
}
};
match matches.subcommand() {
Some(("console", _sub_m)) => match unsafe { console(&mut ec) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to read console: {:X?}", err);
process::exit(1);
},
},
Some(("fan", sub_m)) => {
let index = sub_m.value_of_os("index").unwrap().to_string_lossy().parse::<u8>().unwrap();
let duty_opt = sub_m.value_of("duty").map(|x| x.parse::<u8>().unwrap());
match duty_opt {
Some(duty) => match unsafe { fan_set(&mut ec, index, duty) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set fan {} to {}: {:X?}", index, duty, err);
process::exit(1);
},
},
None => match unsafe { fan_get(&mut ec, index) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to get fan {}: {:X?}", index, err);
process::exit(1);
},
},
}
},
Some(("flash", sub_m)) => {
let path = sub_m.value_of("path").unwrap();
let force = sub_m.is_present("force");
println!("force = {}", force);
match unsafe { flash(&mut ec, path, SpiTarget::Main, force) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to flash '{}': {:X?}", path, err);
process::exit(1);
},
}
},
Some(("flash_backup", sub_m)) => {
let path = sub_m.value_of("path").unwrap();
let force = sub_m.is_present("force");
match unsafe { flash(&mut ec, path, SpiTarget::Backup, force) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to flash '{}': {:X?}", path, err);
process::exit(1);
},
}
},
Some(("info", _sub_m)) => match unsafe { info(&mut ec) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to read info: {:X?}", err);
process::exit(1);
},
},
Some(("keymap", sub_m)) => {
let layer = sub_m.value_of("layer").unwrap().parse::<u8>().unwrap();
let output = sub_m.value_of("output").unwrap().parse::<u8>().unwrap();
let input = sub_m.value_of("input").unwrap().parse::<u8>().unwrap();
match sub_m.value_of("value") {
Some(value_str) => match u16::from_str_radix(value_str.trim_start_matches("0x"), 16) {
Ok(value) => match unsafe { keymap_set(&mut ec, layer, output, input, value) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set keymap {}, {}, {} to {}: {:X?}", layer, output, input, value, err);
process::exit(1);
},
},
Err(err) => {
eprintln!("failed to parse value: '{}': {}", value_str, err);
process::exit(1);
}
},
None => match unsafe { keymap_get(&mut ec, layer, output, input) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to get keymap {}, {}, {}: {:X?}", layer, output, input, err);
process::exit(1);
},
},
}
},
Some(("led_color", sub_m)) => {
let index = sub_m.value_of("index").unwrap().parse::<u8>().unwrap();
let value = sub_m.value_of("value");
if let Some(value) = value {
let (r, g, b) = parse_color(value).unwrap();
match unsafe { ec.led_set_color(index, r, g, b) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set color {}: {:X?}", value, err);
process::exit(1);
},
}
} else {
match unsafe { ec.led_get_color(index) } {
Ok((r, g, b)) => println!("{:02x}{:02x}{:02x}", r, g, b),
Err(err) => {
eprintln!("failed to get color: {:X?}", err);
process::exit(1);
},
}
}
},
Some(("led_value", sub_m)) => {
let index = sub_m.value_of("index").unwrap().parse::<u8>().unwrap();
let value = sub_m.value_of("value").map(|x| x.parse::<u8>().unwrap());
if let Some(value) = value {
match unsafe { ec.led_set_value(index, value) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set value {}: {:X?}", value, err);
process::exit(1);
},
}
} else {
match unsafe { ec.led_get_value(index) } {
Ok((value, max)) => {
println!("value: {}", value);
println!("max: {}", max);
},
Err(err) => {
eprintln!("failed to get value: {:X?}", err);
process::exit(1);
},
}
}
},
Some(("led_mode", sub_m)) => {
let layer = sub_m.value_of("layer").unwrap().parse::<u8>().unwrap();
let mode = sub_m.value_of("mode").map(|x| x.parse::<u8>().unwrap());
let speed = sub_m.value_of("speed").map(|x| x.parse::<u8>().unwrap());
if let (Some(mode), Some(speed)) = (mode, speed) {
match unsafe { ec.led_set_mode(layer, mode, speed) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set layer {} mode {} at speed {}: {:X?}", layer, mode, speed, err);
process::exit(1);
},
}
} else {
match unsafe { ec.led_get_mode(layer) } {
Ok((mode, speed)) => {
println!("mode: {}", mode);
println!("speed: {}", speed);
},
Err(err) => {
eprintln!("failed to get mode for layer {}: {:X?}", layer, err);
process::exit(1);
},
}
}
},
Some(("led_save", _sub_m)) => match unsafe { ec.led_save() } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to save LED settings: {:X?}", err);
process::exit(1);
},
},
Some(("matrix", _sub_m)) => match unsafe { matrix(&mut ec) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to read matrix: {:X?}", err);
process::exit(1);
},
},
Some(("print", sub_m)) => for arg in sub_m.values_of("message").unwrap() {
let mut arg = arg.to_owned();
arg.push('\n');
match unsafe { print(&mut ec, arg.as_bytes()) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to print '{}': {:X?}", arg, err);
process::exit(1);
},
}
},
Some(("set_no_input", sub_m)) => {
let no_input = sub_m.value_of("value").unwrap().parse::<bool>().unwrap();
match unsafe { ec.set_no_input(no_input) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set no_input mode: {:X?}", err);
process::exit(1);
}
}
},
Some(("security", sub_m)) => {
match sub_m.value_of("state") {
Some(value) => {
let state = match value {
"lock" => SecurityState::PrepareLock,
"unlock" => SecurityState::PrepareUnlock,
_ => {
eprintln!("invalid security state '{}': must be 'lock' or 'unlock'", value);
process::exit(1);
}
};
match unsafe { security_set(&mut ec, state) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to set security state to '{}': {:X?}", value, err);
process::exit(1);
},
}
},
None => match unsafe { security_get(&mut ec) } {
Ok(()) => (),
Err(err) => {
eprintln!("failed to get security state: {:X?}", err);
process::exit(1);
},
},
}
},
_ => unreachable!()
}
}