1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2010 SUSE Linux Products GmbH
7 * Thomas Renninger <trenn@suse.de>
18 #include <sys/types.h>
22 #define EC_SPACE_SIZE 256
23 #define SYSFS_PATH "/sys/kernel/debug/ec/ec0/io"
26 - Provide param for accessing different ECs (not supported by kernel yet)
29 static int read_mode = -1;
30 static int sleep_time;
31 static int write_byte_offset = -1;
32 static int read_byte_offset = -1;
33 static uint8_t write_value = -1;
35 void usage(char progname[], int exit_status)
38 printf("1) %s -r [-s sleep]\n", basename(progname));
39 printf("2) %s -b byte_offset\n", basename(progname));
40 printf("3) %s -w byte_offset -v value\n\n", basename(progname));
42 puts("\t-r [-s sleep] : Dump EC registers");
43 puts("\t If sleep is given, sleep x seconds,");
44 puts("\t re-read EC registers and show changes");
45 puts("\t-b offset : Read value at byte_offset (in hex)");
46 puts("\t-w offset -v value : Write value at byte_offset");
47 puts("\t-h : Print this help\n\n");
48 puts("Offsets and values are in hexadecimal number system.");
49 puts("The offset and value must be between 0 and 0xff.");
53 void parse_opts(int argc, char *argv[])
57 while ((c = getopt(argc, argv, "rs:b:w:v:h")) != -1) {
62 usage(argv[0], EXIT_FAILURE);
66 if (read_mode != -1 && read_mode != 1)
67 usage(argv[0], EXIT_FAILURE);
69 sleep_time = atoi(optarg);
70 if (sleep_time <= 0) {
72 usage(argv[0], EXIT_FAILURE);
73 printf("Bad sleep time: %s\n", optarg);
78 usage(argv[0], EXIT_FAILURE);
80 read_byte_offset = strtoul(optarg, NULL, 16);
84 usage(argv[0], EXIT_FAILURE);
86 write_byte_offset = strtoul(optarg, NULL, 16);
89 write_value = strtoul(optarg, NULL, 16);
92 usage(argv[0], EXIT_SUCCESS);
94 fprintf(stderr, "Unknown option!\n");
95 usage(argv[0], EXIT_FAILURE);
99 if (write_byte_offset < 0 ||
100 write_byte_offset >= EC_SPACE_SIZE) {
101 fprintf(stderr, "Wrong byte offset 0x%.2x, valid: "
103 write_byte_offset, EC_SPACE_SIZE - 1);
104 usage(argv[0], EXIT_FAILURE);
106 if (write_value < 0 ||
107 write_value >= 255) {
108 fprintf(stderr, "Wrong byte offset 0x%.2x, valid:"
109 "[0-0xff]\n", write_byte_offset);
110 usage(argv[0], EXIT_FAILURE);
113 if (read_mode == 1 && read_byte_offset != -1) {
114 if (read_byte_offset < -1 ||
115 read_byte_offset >= EC_SPACE_SIZE) {
116 fprintf(stderr, "Wrong byte offset 0x%.2x, valid: "
118 read_byte_offset, EC_SPACE_SIZE - 1);
119 usage(argv[0], EXIT_FAILURE);
122 /* Add additional parameter checks here */
127 char buf[EC_SPACE_SIZE];
128 char buf2[EC_SPACE_SIZE];
129 int byte_off, bytes_read;
131 bytes_read = read(fd, buf, EC_SPACE_SIZE);
133 if (bytes_read == -1)
134 err(EXIT_FAILURE, "Could not read from %s\n", SYSFS_PATH);
136 if (bytes_read != EC_SPACE_SIZE)
137 fprintf(stderr, "Could only read %d bytes\n", bytes_read);
139 printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
140 for (byte_off = 0; byte_off < bytes_read; byte_off++) {
141 if ((byte_off % 16) == 0)
142 printf("\n%.2X: ", byte_off);
143 printf(" %.2x ", (uint8_t)buf[byte_off]);
151 lseek(fd, 0, SEEK_SET);
154 bytes_read = read(fd, buf2, EC_SPACE_SIZE);
156 if (bytes_read == -1)
157 err(EXIT_FAILURE, "Could not read from %s\n", SYSFS_PATH);
159 if (bytes_read != EC_SPACE_SIZE)
160 fprintf(stderr, "Could only read %d bytes\n", bytes_read);
162 printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
163 for (byte_off = 0; byte_off < bytes_read; byte_off++) {
164 if ((byte_off % 16) == 0)
165 printf("\n%.2X: ", byte_off);
167 if (buf[byte_off] == buf2[byte_off])
168 printf(" %.2x ", (uint8_t)buf2[byte_off]);
170 printf("*%.2x ", (uint8_t)buf2[byte_off]);
175 void read_ec_val(int fd, int byte_offset)
180 error = lseek(fd, byte_offset, SEEK_SET);
181 if (error != byte_offset)
182 err(EXIT_FAILURE, "Cannot set offset to 0x%.2x", byte_offset);
184 error = read(fd, &buf, 1);
186 err(EXIT_FAILURE, "Could not read byte 0x%.2x from %s\n",
187 byte_offset, SYSFS_PATH);
188 printf("0x%.2x\n", buf);
192 void write_ec_val(int fd, int byte_offset, uint8_t value)
196 error = lseek(fd, byte_offset, SEEK_SET);
197 if (error != byte_offset)
198 err(EXIT_FAILURE, "Cannot set offset to 0x%.2x", byte_offset);
200 error = write(fd, &value, 1);
202 err(EXIT_FAILURE, "Cannot write value 0x%.2x to offset 0x%.2x",
206 int main(int argc, char *argv[])
208 int file_mode = O_RDONLY;
211 parse_opts(argc, argv);
214 file_mode = O_WRONLY;
215 else if (read_mode == 1)
216 file_mode = O_RDONLY;
218 usage(argv[0], EXIT_FAILURE);
220 fd = open(SYSFS_PATH, file_mode);
222 err(EXIT_FAILURE, "%s", SYSFS_PATH);
225 if (read_byte_offset == -1)
227 else if (read_byte_offset < 0 ||
228 read_byte_offset >= EC_SPACE_SIZE)
229 usage(argv[0], EXIT_FAILURE);
231 read_ec_val(fd, read_byte_offset);
233 write_ec_val(fd, write_byte_offset, write_value);