-
Notifications
You must be signed in to change notification settings - Fork 7
/
peeknpoke.c
503 lines (467 loc) · 13.8 KB
/
peeknpoke.c
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
/*
* peeknpoke
*
* Copyright (c) 2012-2016, Intel Corporation.
* Hari Kanigeri <[email protected]>
* Asutosh Pathak <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "pnp_utils_inc.h"
static void usage(void)
{
printf("Usage commands: ./peeknpoke.out <arg>\n"
"\t <arg> values are\n"
"\t r for Register read\n"
"\t w for Register write\n"
"\t d for Register range dump\n"
"\t m r to read MSIC register\n"
"\t m w to write MSIC register\n"
"\t s r to read MSR register\n"
"\t s w to write to MSR register\n"
"\t i r to read from I2C device\n"
"\t i w to write to I2C device\n"
"\t n w to write to North Complex unit register\n"
"\t n r to read from North Complex unit register\n"
"\t p r to read from Port\n"
"\t p w to write to the Port\n"
"\t b w to write to the PCI device\n"
"\t b r to read from the PCI device\n"
"\t b d to dump config space from the PCI device\n"
"\t v to get the version for peeknpoke\n");
}
static int process_i2c_args(int argc, char **argv)
{
unsigned int bus;
unsigned int reg;
unsigned int size;
unsigned int addr;
int result;
unsigned int value;
uint8_t *values = NULL;
int status = 0;
int i, j;
unsigned int array_size;
if (argc < 3) {
printf("\n***Invalid argument list: check usage -\n");
usage();
return -1;
}
if (argv[2][0] == 'r' || argv[2][0] == 'R') {
if (argc < 7) {
printf("Usage to read:<r> <size: byte=2, word=3, data=5> <bus_no> "
"<bus_addr in Hex without 0x prefix> "
"<Hex registerAddress without 0x prefix>\n");
printf("Eg: to read word data from I2C bus 1 at address 0x36 and register 0x19 use\n"
" ./peeknpoke i r 3 1 36 19\n\n");
}
else {
hexstring_to_int(argv[3], &size);
if (size == 5 || size == 8) {
if (argc != 8) {
printf("Usage to read:<r> <size: byte=2, word=3, data=5> <bus_no> "
"<bus_addr in Hex without 0x prefix> "
"<Hex registerAddress without 0x prefix> <block size to read>\n");
printf("Eg: to read 10 block data from I2C bus 1 at address 0x36 and register 0x19 use\n"
" ./peeknpoke i r 8 1 36 19 a\n\n");
status = -1;
return status;
}
hexstring_to_int(argv[7], &array_size);
if (array_size <= 0) {
printf("Please use the proper size to read\n");
status = -1;
return status;
}
values = malloc((array_size + 1) * sizeof(unsigned int));
if (!values) {
perror("Out of Memory\n");
status = -1;
return status;
}
hexstring_to_int(argv[6], ®);
hexstring_to_int(argv[5], &addr);
hexstring_to_int(argv[4], &bus);
status = block_read_i2c_device(bus, addr, reg, size, array_size + 1, values);
}
else {
hexstring_to_int(argv[6], ®);
hexstring_to_int(argv[5], &addr);
hexstring_to_int(argv[4], &bus);
status = read_i2c_device(bus, addr, reg, size, &result);
}
}
}
else if (argv[2][0] == 'w' || argv[2][0] == 'W') {
if (argc < 8) {
printf("Usage to Write:<w> <size: byte=2, word=3, data=5 OR 8> <bus_no> "
"<bus_addr in Hex without 0x prefix> "
"<Hex registerAddress without 0x prefix>\n\n");
printf("data=5 (I2C_SMBUS_BLOCK_DATA): I2C frame format: [START|bus_addr|reg_addr|byte_count|data0|...|dataN|STOP]"
"\n./peeknpoke i w 5 1 36 19 10 15 45\n"
"I2C frame format: [START|36|19|3|10|15|45|STOP]\n\n");
printf("data=8 (I2C_SMBUS_I2C_BLOCK_DATA): I2C frame format: [START|bus_addr|reg_addr|data0|...|dataN|STOP]"
"\n./peeknpoke i w 8 1 36 19 10 15 45\n"
"I2C frame format: [START|36|19|10|15|45|STOP]\n\n");
printf("Eg: to write word 0x10 to I2C bus 1 at address 0x36 and register 0x19"
"\n./peeknpoke i w 3 1 36 19 10\n\n");
}
else {
hexstring_to_int(argv[3], &size);
if (size == 5 || size == 8) {
array_size = argc - 7;
/* To send 1 byte, the Byte Mode has to be used */
if (array_size <= 1) {
printf("Please use byte values to write\n");
status = -1;
return status;
}
values = malloc((array_size) * sizeof(unsigned int));
if (!values) {
perror("Out of Memory\n");
status = -1;
return status;
}
for (i = argc, j = (argc - 8);i >= 8; i--, j-- ) {
hexstring_to_int(argv[i - 1], &value);
values[j] = value;
}
hexstring_to_int(argv[6], ®);
hexstring_to_int(argv[5], &addr);
hexstring_to_int(argv[4], &bus);
status = block_write_i2c_device(bus, addr, reg, size, array_size, values);
}
else {
hexstring_to_int(argv[7], &value);
hexstring_to_int(argv[6], ®);
hexstring_to_int(argv[5], &addr);
hexstring_to_int(argv[4], &bus);
status = write_i2c_device(bus, addr, reg, size, value);
}
}
}
else {
printf("\n***Invalid I2C operation\n");
status = -1;
}
if (values) {
free(values);
}
return status;
}
static int process_nc_args(int argc, char **argv)
{
unsigned int port;
unsigned int reg;
int result;
unsigned int value;
int status = 0;
if (argc < 3) {
printf("\n***Invalid argument list: check usage -\n");
usage();
return -1;
}
if (argv[2][0] == 'r' || argv[2][0] == 'R') {
if (argc != 5 ) {
printf("Usage to read:<r> <Port number in Hex without 0x prefix> "
"<Hex registerAddress without 0x prefix>\n");
printf("PUNIT port - 4, AUNIT - 0, HUNIT - 2 ...etc");
printf("Eg: to read word data from Port 4 at address 0xB1 use\n"
" ./peeknpoke.out n r 4 B1\n\n");
}
else {
hexstring_to_int(argv[4], ®);
hexstring_to_int(argv[3], &port);
status = read_nc_port(reg, port, &result);
}
}
else if (argv[2][0] == 'w' || argv[2][0] == 'W') {
if (argc != 6 ) {
printf("Usage to write:<w> <Port number in Hex without 0x prefix> "
"<Hex registerAddress without 0x prefix>\n"
"<Hex value without 0x prefix>\n");
printf("PUNIT port - 4, AUNIT - 0, HUNIT - 2 ...etc\n");
printf("This works only on Secure parts !!!\n");
printf("Eg: to write word 0xff to Port 4 at address 0x70 use\n"
" ./peeknpoke.out n w 4 70 ff\n\n");
}
else {
hexstring_to_int(argv[5], &value);
hexstring_to_int(argv[4], ®);
hexstring_to_int(argv[3], &port);
status = write_nc_port(reg, port, value);
}
}
else {
printf("\n***Invalid NC operation\n");
status = -1;
}
return status;
}
static int process_port_args(int argc, char **argv)
{
unsigned int port;
int result;
unsigned int value;
int status = 0;
if (argc < 3) {
printf("\n***Invalid argument list: check usage -\n");
usage();
return -1;
}
if (argv[2][0] == 'r' || argv[2][0] == 'R') {
if (argc != 4 ) {
printf("Usage to read:<r> <Port number in Hex without 0x prefix>\n");
printf("Eg: to read word data from Port 0x1000 use\n"
" ./peeknpoke.out p r 1000\n\n");
}
else {
hexstring_to_int(argv[3], &port);
status = read_port(port, &result);
}
}
else if (argv[2][0] == 'w' || argv[2][0] == 'W') {
if (argc != 5 ) {
printf("Usage to write:<w> <Port number in Hex without 0x prefix> "
"<Hex value without 0x prefix>\n");
printf("Eg: to write word 0xff to Port 1000 use\n"
" ./peeknpoke.out p w 1000 ff\n\n");
}
else {
hexstring_to_int(argv[4], &value);
hexstring_to_int(argv[3], &port);
status = write_port(port, value);
}
}
else {
printf("\n***Invalid Port operation\n");
status = -1;
}
return status;
}
static int process_pci_args(int argc, char **argv)
{
unsigned int bus;
unsigned int dev;
unsigned int func;
unsigned int reg;
int result;
unsigned int value;
int status = 0;
if (argc < 6) {
printf("Usage to dump:<d> <Bus in Hex without 0x prefix> "
"<Dev number in Hex without 0x prefix>"
"<Func number in Hex without 0x prefix>\n\n");
printf("Usage to read:<r> <Bus in Hex without 0x prefix> "
"<Dev number in Hex without 0x prefix>"
"<Func number in Hex without 0x prefix>"
"<Reg number in Hex without 0x prefix>\n\n");
printf("Usage to write:<r> <Bus in Hex without 0x prefix> "
"<Dev number in Hex without 0x prefix>"
"<Func number in Hex without 0x prefix>"
"<Reg number in Hex without 0x prefix>"
"<Value to write in Hex without 0x prefix>\n");
return -1;
}
if (argv[2][0] == 'd' || argv[2][0] == 'D') {
if (argc != 6) {
printf("Usage to read:<r> <Bus in Hex without 0x prefix> "
"<Dev number in Hex without 0x prefix>"
"<Func number in Hex without 0x prefix>\n");
printf("Eg: to dump data from Bus 0 Dev 0 Func 0 use\n"
" ./peeknpok b d 0 0 0\n\n");
}
else {
hexstring_to_int(argv[5], &func);
hexstring_to_int(argv[4], &dev);
hexstring_to_int(argv[3], &bus);
status = read_pci_dump(bus, dev, func);
}
}
else if (argv[2][0] == 'r' || argv[2][0] == 'R') {
if (argc != 7 ) {
printf("Usage to read:<r> <Bus in Hex without 0x prefix> "
"<Dev number in Hex without 0x prefix>"
"<Func number in Hex without 0x prefix>"
"<Reg number in Hex without 0x prefix>\n");
printf("Eg: to read data from Bus 0 Dev 0 Func 0 Reg 8 use\n"
" ./peeknpoke.out b r 0 0 0 9\n\n");
}
else {
hexstring_to_int(argv[6], ®);
hexstring_to_int(argv[5], &func);
hexstring_to_int(argv[4], &dev);
hexstring_to_int(argv[3], &bus);
status = read_pci_reg(bus, dev, func, reg, &result);
}
}
else if (argv[2][0] == 'w' || argv[2][0] == 'W') {
if (argc != 8 ) {
printf("Usage to write:<r> <Bus in Hex without 0x prefix> "
"<Dev number in Hex without 0x prefix>"
"<Func number in Hex without 0x prefix>"
"<Reg number in Hex without 0x prefix>"
"<Value to write in Hex without 0x prefix>\n");
printf("Eg: to write data to Bus 0 Dev 0 Func 0 Reg 8 use\n"
" ./peeknpoke.out b w 0 0 0 9 ff\n\n");
}
else {
hexstring_to_int(argv[7], &value);
hexstring_to_int(argv[6], ®);
hexstring_to_int(argv[5], &func);
hexstring_to_int(argv[4], &dev);
hexstring_to_int(argv[3], &bus);
status = write_pci_reg(bus, dev, func, reg, value);
}
}
else {
printf("\n***Invalid PCI operation\n");
status = -1;
}
return status;
}
/*
* ======== process_args ========
* Process command line arguments for this sample
*/
static int process_args(int argc, char **argv) {
int cmd_type;
unsigned int reg_addr;
unsigned int reg_val;
uint64_t msr_val;
int status = 0;
if (argc < 2) {
usage();
return -1;
}
cmd_type = argv[1][0];
switch (cmd_type) {
case 'd':
if (argc < 4)
printf("Usage: <r> <Hex registerAddress without 0x prefix>"
" <numOfWordsToDump>\n");
else {
hexstring_to_int(argv[2], ®_addr);
status = addr_range_dump(reg_addr, atoi(argv[3]));
}
break;
case 'r':
if (argc < 4)
printf("Usage: <r> <Hex registerAddress without 0x prefix>"
" <regAccessSize =8, 16, 32>\n");
else {
hexstring_to_int(argv[2], ®_addr);
status = reg_read(reg_addr, atoi(argv[3]));
}
break;
case 'w':
if (argc < 5)
printf("Usage: <w> <Hex registerAddress without 0x prefix>"
" <regAccessSize =8, 16, 32> <value>\n ");
else {
hexstring_to_int(argv[2], ®_addr);
hexstring_to_int(argv[4], ®_val);
status = reg_write(reg_addr, atoi(argv[3]), reg_val);
}
break;
case 'm':
if (argc < 3) {
printf("\n***Invalid argument list: check usage -\n");
usage();
return -1;
}
else {
if (argv[2][0] == 'r' || argv[2][0] == 'R') {
if (argc != 4 ) {
printf("Usage: <r> <Hex register Address without 0x prefix>\n");
}
else {
hexstring_to_int(argv[3], ®_addr);
msic_read((unsigned short)reg_addr);
}
}
else if (argv[2][0] == 'w' || argv[2][0] == 'W') {
if (argc != 5) {
printf("Usage: <w> <Hex register Address without 0x prefix> "
"<Hex Value without 0x prefix>\n");
}
else {
hexstring_to_int(argv[3], ®_addr);
hexstring_to_int(argv[4], ®_val);
msic_write((unsigned short)reg_addr, (unsigned short)reg_val);
}
}
else {
printf("\n***Invalid MSIC operation\n");
}
}
break;
case 's':
if (argv[2][0] == 'r' || argv[2][0] == 'R') {
if (argc != 5 ) {
printf("Usage to read:<r> <cpu_no> <Hex registerAddress without 0x prefix>\n");
}
else {
hexstring_to_int(argv[4], ®_addr);
status = msr_reg_read(atoi(argv[3]), reg_addr, 1);
}
}
else if (argv[2][0] == 'w' || argv[2][0] == 'W') {
if (argc != 6) {
printf("Usage to write: <w> <cpu_no> <Hex registerAddress without"
" 0x prefix> <hex value without 0x prefix>\n");
}
else {
hexstring_to_int(argv[4], ®_addr);
hexstring_to_long(argv[5], &msr_val);
status = msr_reg_write(atoi(argv[3]), reg_addr, msr_val);
}
}
else {
printf("\n***Invalid MSR operation\n");
}
break;
case 'i':
status = process_i2c_args(argc, argv);
break;
case 'n':
status = process_nc_args(argc, argv);
break;
case 'p':
status = process_port_args(argc, argv);
break;
case 'b':
status = process_pci_args(argc, argv);
break;
case 'v':
printf("peeknpoke version %.2f (INTEL DEBUG TOOL)\n", VERSION_INFO);
break;
case 'z':
printf("PeeknPoke Architect - Hari Kanigeri\n");
printf("Authors - Hari Kanigeri, Asutosh Pathak\n");
break;
default:
usage();
break;
}
return (status);
}
/*
* ======== main ========
*/
int main(int argc, char **argv) {
int status = 0;
process_args(argc, argv);
return status;
}