summaryrefslogtreecommitdiff
path: root/util/ectool.c
blob: 5fa124e74394bd540a3bc6f469ce622c62d58839 (plain)
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
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/io.h>
#include <unistd.h>

#include "lpc_commands.h"

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
/* Don't use a macro where an inline will do... */
static inline int MIN(int a, int b) { return a < b ? a : b; }


const char help_str[] =
	"Commands:\n"
	"  flashinfo\n"
	"      Prints information on the EC flash\n"
	"  flashread <offset> <size> <outfile>\n"
	"      Reads from EC flash to a file\n"
	"  flashwrite <offset> <infile>\n"
	"      Writes to EC flash from a file\n"
	"  flasherase <offset> <size>\n"
	"      Erases EC flash\n"
	"  hello\n"
	"      Checks for basic communication with EC\n"
	"  readtest <patternoffset> <size>\n"
	"      Reads a pattern from the EC via LPC\n"
	"  sertest\n"
	"      Serial output test for COM2\n"
	"  version\n"
	"      Prints EC version\n"
	"  temps <sensorid>\n"
	"      Print temperature.\n"
	"  pwmgetfanrpm\n"
	"      Prints current fan RPM\n"
	"  pwmsetfanrpm <targetrpm>\n"
	"      Set target fan RPM\n"
	"  pwmgetkblight\n"
	"      Prints current keyboard backlight percent\n"
	"  pwmsetkblight <percent>\n"
	"      Set keyboard backlight in percent\n"
	"  usbchargemode <port> <mode>\n"
	"      Set USB charging mode\n"
	"\n"
	"Not working for you?  Make sure LPC I/O is configured:\n"
	"  pci_write32 0 0x1f 0 0x88 0x00fc0801\n"
	"  pci_write32 0 0x1f 0 0x8c 0x00fc0901\n"
	"  pci_write16 0 0x1f 0 0x80 0x0010\n"
	"  pci_write16 0 0x1f 0 0x82 0x3d01\n"
	"";


/* Waits for the EC to be unbusy.  Returns 0 if unbusy, non-zero if
 * timeout. */
int wait_for_ec(int status_addr, int timeout_usec)
{
	int i;
	for (i = 0; i < timeout_usec; i += 10) {
		usleep(10);  /* Delay first, in case we just sent a command */
		if (!(inb(status_addr) & EC_LPC_BUSY_MASK))
			return 0;
	}
	return -1;  /* Timeout */
}


/* Sends a command to the EC.  Returns the command status code, or
 * -1 if other error. */
int ec_command(int command, const void *indata, int insize,
	       void *outdata, int outsize) {
	uint8_t *d;
	int i;

	/* TODO: add command line option to use kernel command/param window */
	int cmd_addr = EC_LPC_ADDR_USER_CMD;
	int param_addr = EC_LPC_ADDR_USER_PARAM;

	if (insize > EC_LPC_PARAM_SIZE || outsize > EC_LPC_PARAM_SIZE) {
		fprintf(stderr, "Data size too big\n");
		return -1;
	}

	if (wait_for_ec(cmd_addr, 1000000)) {
		fprintf(stderr, "Timeout waiting for EC ready\n");
		return -1;
	}

	/* Write data, if any */
	/* TODO: optimized copy using outl() */
	for (i = 0, d = (uint8_t *)indata; i < insize; i++, d++)
		outb(*d, param_addr + i);

	outb(command, cmd_addr);

	if (wait_for_ec(cmd_addr, 1000000)) {
		fprintf(stderr, "Timeout waiting for EC response\n");
		return -1;
	}

	/* Check status */
	i = inb(cmd_addr);
	i = EC_LPC_GET_STATUS(i);
	if (i) {
		fprintf(stderr, "EC returned error status %d\n", i);
		return i;
	}

	/* Read data, if any */
	for (i = 0, d = (uint8_t *)outdata; i < outsize; i++, d++)
		*d = inb(param_addr + i);

	return 0;
}


void print_help(const char *prog)
{
	printf("Usage: %s <command> [params]\n\n", prog);
	puts(help_str);
}


int cmd_hello(void)
{
	struct lpc_params_hello p;
	struct lpc_response_hello r;
	int rv;

	p.in_data = 0xa0b0c0d0;

	rv = ec_command(EC_LPC_COMMAND_HELLO, &p, sizeof(p), &r, sizeof(r));
	if (rv)
		return rv;

	if (r.out_data != 0xa1b2c3d4) {
		fprintf(stderr, "Expected response 0x%08x, got 0x%08x\n",
			0xa1b2c3d4, r.out_data);
		return -1;
	}

	printf("EC says hello!\n");
	return 0;
}


int cmd_version(void)
{
  static const char * const fw_copies[] = {"unknown", "RO", "A", "B"};
	struct lpc_response_get_version r;
	int rv;

	rv = ec_command(EC_LPC_COMMAND_GET_VERSION, NULL, 0, &r, sizeof(r));
	if (rv)
		return rv;

	/* Ensure versions are null-terminated before we print them */
	r.version_string_ro[sizeof(r.version_string_ro) - 1] = '\0';
	r.version_string_rw_a[sizeof(r.version_string_rw_a) - 1] = '\0';
	r.version_string_rw_b[sizeof(r.version_string_rw_b) - 1] = '\0';

        /* Print versions */
	printf("RO version:    %s\n", r.version_string_ro);
	printf("RW-A version:  %s\n", r.version_string_rw_a);
	printf("RW-B version:  %s\n", r.version_string_rw_b);
	printf("Firmware copy: %s\n",
	       (r.current_image < ARRAY_SIZE(fw_copies) ?
		fw_copies[r.current_image] : "?"));
	return 0;
}


int cmd_read_test(int argc, char *argv[])
{
	struct lpc_params_read_test p;
	struct lpc_response_read_test r;
	int offset, size;
	int errors = 0;
	int rv;
	int i;
	char *e;
	char *buf;
	uint32_t *b;

	if (argc < 2) {
		fprintf(stderr, "Usage: readtest <pattern_offset> <size>\n");
		return -1;
	}
	offset = strtol(argv[0], &e, 0);
	size = strtol(argv[1], &e, 0);
	if ((e && *e) || size <= 0 || size > 0x100000) {
		fprintf(stderr, "Bad size.\n");
		return -1;
	}
	printf("Reading %d bytes with pattern offset 0x%x...\n", size, offset);

	buf = (char *)malloc(size);
	if (!buf) {
		fprintf(stderr, "Unable to allocate buffer.\n");
		return -1;
	}

	/* Read data in chunks */
	for (i = 0; i < size; i += sizeof(r.data)) {
		p.offset = offset + i / sizeof(uint32_t);
		p.size = MIN(size - i, sizeof(r.data));
		rv = ec_command(EC_LPC_COMMAND_READ_TEST, &p, sizeof(p),
				&r, sizeof(r));
		if (rv) {
			fprintf(stderr, "Read error at offset %d\n", i);
			free(buf);
			return -1;
		}
		memcpy(buf + i, r.data, p.size);
	}

	/* Check data */
	for (i = 0, b = (uint32_t *)buf; i < size / 4; i++, b++) {
		if (*b != i + offset) {
			printf("Mismatch at byte offset 0x%x: "
			       "expected 0x%08x, got 0x%08x\n",
			       (int)(i * sizeof(uint32_t)), i + offset, *b);
			errors++;
		}
	}

	free(buf);
	if (errors) {
		printf("Found %d errors\n", errors);
		return -1;
	}

	printf("done.\n");
	return 0;
}


int cmd_flash_info(void)
{
	struct lpc_response_flash_info r;
	int rv;

	rv = ec_command(EC_LPC_COMMAND_FLASH_INFO, NULL, 0, &r, sizeof(r));
	if (rv)
		return rv;

	printf("FlashSize %d\nWriteSize %d\nEraseSize %d\nProtectSize %d\n",
	       r.flash_size, r.write_block_size, r.erase_block_size,
	       r.protect_block_size);

	return 0;
}


int cmd_flash_read(int argc, char *argv[])
{
	struct lpc_params_flash_read p;
	struct lpc_response_flash_read r;
	int offset, size;
	int rv;
	int i;
	char *e;
	char *buf;
	FILE *f;

	if (argc < 3) {
		fprintf(stderr,
			"Usage: flashread <offset> <size> <filename>\n");
		return -1;
	}
	offset = strtol(argv[0], &e, 0);
	if ((e && *e) || offset < 0 || offset > 0x100000) {
		fprintf(stderr, "Bad offset.\n");
		return -1;
	}
	size = strtol(argv[1], &e, 0);
	if ((e && *e) || size <= 0 || size > 0x100000) {
		fprintf(stderr, "Bad size.\n");
		return -1;
	}
	printf("Reading %d bytes at offset %d...\n", size, offset);

	buf = (char *)malloc(size);
	if (!buf) {
		fprintf(stderr, "Unable to allocate buffer.\n");
		return -1;
	}

	/* Read data in chunks */
	for (i = 0; i < size; i += EC_LPC_FLASH_SIZE_MAX) {
		p.offset = offset + i;
		p.size = MIN(size - i, EC_LPC_FLASH_SIZE_MAX);
		rv = ec_command(EC_LPC_COMMAND_FLASH_READ,
				&p, sizeof(p), &r, sizeof(r));
		if (rv) {
			fprintf(stderr, "Read error at offset %d\n", i);
			free(buf);
			return -1;
		}
		memcpy(buf + i, r.data, p.size);
	}

	/* Write to file */
	f = fopen(argv[2], "wb");
	if (!f) {
		perror("Error opening output file");
		free(buf);
		return -1;
	}
	i = fwrite(buf, 1, size, f);
	fclose(f);
	free(buf);
	if (i != size) {
		perror("Error writing to file");
		return -1;
	}
	printf("done.\n");
	return 0;
}


int cmd_flash_write(int argc, char *argv[])
{
	struct lpc_params_flash_write p;
	int offset, size;
	int rv;
	int i;
	char *e;
	char *buf;
	FILE *f;

	if (argc < 2) {
		fprintf(stderr, "Usage: flashwrite <offset> <filename>\n");
		return -1;
	}
	offset = strtol(argv[0], &e, 0);
	if ((e && *e) || offset < 0 || offset > 0x100000) {
		fprintf(stderr, "Bad offset.\n");
		return -1;
	}

	/* Read the input file */
	f = fopen(argv[1], "rb");
	if (!f) {
		perror("Error opening input file");
		return -1;
	}
	fseek(f, 0, SEEK_END);
	size = ftell(f);
	rewind(f);
	if (size > 0x100000) {
		fprintf(stderr, "File seems unreasonably large\n");
		fclose(f);
		return -1;
	}

	buf = (char *)malloc(size);
	if (!buf) {
		fprintf(stderr, "Unable to allocate buffer.\n");
		fclose(f);
		return -1;
	}

	printf("Reading %d bytes from %s...\n", size, argv[1]);
	i = fread(buf, 1, size, f);
	if (i != size) {
		perror("Error reading file");
		free(buf);
		return -1;
	}

	printf("Writing to offset %d...\n", offset);

	/* Write data in chunks */
	for (i = 0; i < size; i += EC_LPC_FLASH_SIZE_MAX) {
		p.offset = offset + i;
		p.size = MIN(size - i, EC_LPC_FLASH_SIZE_MAX);
		memcpy(p.data, buf + i, p.size);
		rv = ec_command(EC_LPC_COMMAND_FLASH_WRITE,
				&p, sizeof(p), NULL, 0);
		if (rv) {
			fprintf(stderr, "Write error at offset %d\n", i);
			free(buf);
			return -1;
		}
	}

	free(buf);
	printf("done.\n");
	return 0;
}


int cmd_flash_erase(int argc, char *argv[])
{
	struct lpc_params_flash_erase p;
	char *e;

	if (argc < 2) {
		fprintf(stderr, "Usage: flasherase <offset> <size>\n");
		return -1;
	}
	p.offset = strtol(argv[0], &e, 0);
	if ((e && *e) || p.offset < 0 || p.offset > 0x100000) {
		fprintf(stderr, "Bad offset.\n");
		return -1;
	}
	p.size = strtol(argv[1], &e, 0);
	if ((e && *e) || p.size <= 0 || p.size > 0x100000) {
		fprintf(stderr, "Bad size.\n");
		return -1;
	}

	printf("Erasing %d bytes at offset %d...\n", p.size, p.offset);
	if (ec_command(EC_LPC_COMMAND_FLASH_ERASE, &p, sizeof(p), NULL, 0))
		return -1;

	printf("done.\n");
	return 0;
}


int cmd_serial_test(int argc, char *argv[])
{
	const char *c = "COM2 sample serial output from host!\r\n";

	printf("Writing sample serial output to COM2\n");

	while (*c) {
		/* Wait for space in transmit FIFO */
		while (!(inb(0x2fd) & 0x20)) {}

		/* Put the next character */
		outb(*c++, 0x2f8);
	}

	printf("done.\n");
	return 0;
}

int cmd_temperature(int argc, char *argv[])
{
	struct lpc_params_temp_sensor_get_readings p;
	struct lpc_response_temp_sensor_get_readings r;
	int rv;
	int id;
	char *e;

	if (argc != 1) {
		fprintf(stderr, "Usage: temps <sensorid>\n");
		return -1;
	}

	id = strtol(argv[0], &e, 0);
	if (e && *e) {
		fprintf(stderr, "Bad sensor ID.\n");
		return -1;
	}

	p.temp_sensor_id = id;
	printf("Reading temperature...");
	rv = ec_command(EC_LPC_COMMAND_TEMP_SENSOR_GET_READINGS,
			&p, sizeof(p), &r, sizeof(r));
	if (rv)
		printf("Error\n");
	else
		printf("%d\n", r.value);
	return rv;
}

int cmd_pwm_get_fan_rpm(void)
{
	struct lpc_response_pwm_get_fan_rpm r;
	int rv;

	rv = ec_command(EC_LPC_COMMAND_PWM_GET_FAN_RPM, NULL, 0, &r, sizeof(r));
	if (rv)
	        return rv;

	printf("Current fan RPM: %d\n", r.rpm);

	return 0;
}

int cmd_pwm_set_fan_rpm(int argc, char *argv[])
{
	struct lpc_params_pwm_set_fan_target_rpm p;
	char *e;
	int rv;

	if (argc != 1) {
	        fprintf(stderr,
	                "Usage: pwmsetfanrpm <targetrpm>\n");
	        return -1;
	}
	p.rpm = strtol(argv[0], &e, 0);
	if (e && *e) {
	        fprintf(stderr, "Bad RPM.\n");
	        return -1;
	}

	rv = ec_command(EC_LPC_COMMAND_PWM_SET_FAN_TARGET_RPM,
	                &p, sizeof(p), NULL, 0);
	if (rv)
	        return rv;

	printf("Fan target RPM set.\n");
	return 0;
}

int cmd_pwm_get_keyboard_backlight(void)
{
	struct lpc_response_pwm_get_keyboard_backlight r;
	int rv;

	rv = ec_command(EC_LPC_COMMAND_PWM_GET_KEYBOARD_BACKLIGHT,
			NULL, 0, &r, sizeof(r));
	if (rv)
	        return rv;

	printf("Current keyboard backlight percent: %d\n", r.percent);

	return 0;
}

int cmd_pwm_set_keyboard_backlight(int argc, char *argv[])
{
	struct lpc_params_pwm_set_keyboard_backlight p;
	char *e;
	int rv;

	if (argc != 1) {
	        fprintf(stderr,
	                "Usage: pwmsetkblight <percent>\n");
	        return -1;
	}
	p.percent = strtol(argv[0], &e, 0);
	if (e && *e) {
	        fprintf(stderr, "Bad percent.\n");
	        return -1;
	}

	rv = ec_command(EC_LPC_COMMAND_PWM_SET_KEYBOARD_BACKLIGHT,
	                &p, sizeof(p), NULL, 0);
	if (rv)
	        return rv;

	printf("Keyboard backlight set.\n");
	return 0;
}

int cmd_usb_charge_set_mode(int argc, char *argv[])
{
	struct lpc_params_usb_charge_set_mode p;
	char *e;
	int rv;

	if (argc != 2) {
		fprintf(stderr,
			"Usage: usbchargemode <port_id> <mode_id>\n");
		return -1;
	}
	p.usb_port_id = strtol(argv[0], &e, 0);
	if (e && *e) {
		fprintf(stderr, "Bad port ID.\n");
		return -1;
	}
	p.mode = strtol(argv[1], &e, 0);
	if (e && *e) {
		fprintf(stderr, "Bad mode ID.\n");
		return -1;
	}

	printf("Setting port %d to mode %d...\n", p.usb_port_id, p.mode);

	rv = ec_command(EC_LPC_COMMAND_USB_CHARGE_SET_MODE,
			&p, sizeof(p), NULL, 0);
	if (rv)
		return rv;

	printf("USB charging mode set.\n");
	return 0;
}

int main(int argc, char *argv[])
{
	if (argc < 2 || !strcasecmp(argv[1], "-?") ||
	    !strcasecmp(argv[1], "help")) {
		print_help(argv[0]);
		return -2;
	}

	/* Request I/O privilege */
	if (iopl(3) < 0) {
		perror("Error getting I/O privilege");
		return -3;
	}

	/* Handle commands */
	if (!strcasecmp(argv[1], "flashinfo"))
		return cmd_flash_info();
	if (!strcasecmp(argv[1], "flashread"))
		return cmd_flash_read(argc - 2, argv + 2);
	if (!strcasecmp(argv[1], "flashwrite"))
		return cmd_flash_write(argc - 2, argv + 2);
	if (!strcasecmp(argv[1], "flasherase"))
		return cmd_flash_erase(argc - 2, argv + 2);
	if (!strcasecmp(argv[1], "hello"))
		return cmd_hello();
	if (!strcasecmp(argv[1], "readtest"))
		return cmd_read_test(argc - 2, argv + 2);
	if (!strcasecmp(argv[1], "sertest"))
		return cmd_serial_test(argc - 2, argv + 2);
	if (!strcasecmp(argv[1], "version"))
		return cmd_version();
	if (!strcasecmp(argv[1], "temps"))
		return cmd_temperature(argc - 2, argv + 2);
	if (!strcasecmp(argv[1], "pwmgetfanrpm"))
	        return cmd_pwm_get_fan_rpm();
	if (!strcasecmp(argv[1], "pwmsetfanrpm"))
	        return cmd_pwm_set_fan_rpm(argc - 2, argv + 2);
	if (!strcasecmp(argv[1], "pwmgetkblight"))
	        return cmd_pwm_get_keyboard_backlight();
	if (!strcasecmp(argv[1], "pwmsetkblight"))
	        return cmd_pwm_set_keyboard_backlight(argc - 2, argv + 2);
	if (!strcasecmp(argv[1], "usbchargemode"))
	        return cmd_usb_charge_set_mode(argc - 2, argv + 2);

	/* If we're still here, command was unknown */
	fprintf(stderr, "Unknown command '%s'\n\n", argv[1]);
	print_help(argv[0]);
	return -2;
}