forked from DesmondWu/lfdk1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfdk.c
491 lines (367 loc) · 10.6 KB
/
lfdk.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
/*
* LFDK - Linux Firmware Debug Kit
* File: lfdk.c
*
* Copyright (C) 2006 - 2010 Merck Hung <merckhung@gmail.com>
* Copyright (C) 2013 Desmond Wu <wkunhui@gmail.com>
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program 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.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <time.h>
#include <ncurses.h>
#include <panel.h>
//#include "../lfdd/lfdd.h"
#include "lfdk.h"
#include<sys/mman.h>
#include <sys/io.h>
static const char *progname;
static BasePanel BaseScreen;
int x = 0, y = 0;
int curr_index = 0, last_index;
int input = 0;
unsigned int counter = 0;
int ibuf;
char wbuf;
int func = PCI_DEVICE_FUNC;
int maxpcibus = 255;
char pciname[ LFDK_MAX_PATH ];
char enter_mem = 0;
unsigned int io_int_get(unsigned int u32addr)
{
unsigned int idata = 0;
ioperm(u32addr,4,1);
idata = inl(u32addr);
return idata;
}
void io_int_set(unsigned int u32addr, unsigned int u32data)
{
ioperm(u32addr,4, 1); // ioperm(address from, byte num, enable or disable);
outl(u32data, u32addr );
}
void io_byte_set(unsigned int u32addr, unsigned char u8data)
{
ioperm(u32addr, 1, 1);
outb(u8data, u32addr );
}
unsigned int pci_int_get(unsigned char u8bus, unsigned char u8dev, unsigned char u8fun, unsigned char u8reg)
{
unsigned int u32addr=0,u32data=0;
u32addr = u8reg | (u8fun << 8) | (u8dev << 11) | (u8bus << 16) | 0x80000000;
io_int_set(0x0cf8, u32addr);
u32data = io_int_get((unsigned short int) 0x0cfc);
return u32data;
}
void pci_int_set(unsigned char u8bus, unsigned char u8dev, unsigned char u8fun, unsigned char u8reg, unsigned int u32data)
{
unsigned int u32addr=0;
u32addr = u8reg | (u8fun << 8) | (u8dev << 11) | (u8bus << 16) | 0x80000000;
io_int_set(0x0cf8, u32addr);
io_int_set(0x0cfc, u32data);
}
void pci_byte_set(unsigned char u8bus, unsigned char u8dev, unsigned char u8fun, unsigned char u8reg, unsigned char u8data)
{
unsigned int u32addr=0,u32data=0;
u32addr = u8reg | (u8fun << 8) | (u8dev << 11) | (u8bus << 16) | 0x80000000;
io_int_set(0x0cf8, u32addr);
u32data = io_int_get((unsigned short int) 0x0cfc);
u32data = (u32data&0xffffff00) | u8data;
io_int_set(0x0cfc, u32data);
}
int mem_int_set( unsigned int u32addr, unsigned int u32data )
{
int fd = 0;
unsigned int *pu32mmap = NULL;
fd = open("/dev/mem", O_RDWR);
if ( fd == -1 )
{
return -1;
}
pu32mmap = (unsigned int *)mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, u32addr&0xfffff000);
if(pu32mmap == (void *) -1)
{
if (fd) close(fd);
return -1;
}
pu32mmap[(u32addr&0xfff)>>2] = u32data; // Get data from register
munmap(pu32mmap, 0xfff);
if (fd) close(fd);
return 0;
}
int mem_byte_set( unsigned int u32addr, unsigned char u8data )
{
unsigned int u32buff=0;
int fd = 0;
unsigned int *pu32mmap = NULL;
fd = open("/dev/mem", O_RDWR);
if ( fd == -1 )
{
return -1;
}
pu32mmap = (unsigned int *)mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, u32addr&0xfffff000);
if(pu32mmap == (void *) -1)
{
if (fd) close(fd);
return -1;
}
u32buff = pu32mmap[(u32addr&0xfff)>>2];
*(((unsigned char*)&u32buff) + (unsigned char)(u32addr&0x3)) = u8data;
pu32mmap[(u32addr&0xfff)>>2] = u32buff; // Get data from register
usleep(100);
munmap(pu32mmap, 0xfff);
if (fd) close(fd);
return 0;
}
int mem_int_get( unsigned int u32addr, unsigned int *pu32data )
{
int fd = 0;
unsigned int *pu32mmap = NULL;
fd = open("/dev/mem", O_RDWR);
if ( fd == -1 )
{
return -1;
}
pu32mmap = (unsigned int *)mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, u32addr&0xfffff000);
if(pu32mmap == (void *) -1)
{
if (fd) close(fd);
return -1;
}
*pu32data = pu32mmap[(u32addr&0xfff)>>2] ;
munmap(pu32mmap, 0xfff);
if (fd) close(fd);
return 0;
}
void PrintBaseScreen( void );
static void usage( void ) {
fprintf( stderr, "\n"LFDK_VERTEXT"\n" );
fprintf( stderr, "Copyright (C) 2006 - 2010, Merck Hung <merckhung@gmail.com>\n" );
fprintf( stderr, "Copyright (C) 2013 Desmond Wu <wkunhui@gmail.com>\n" );
fprintf( stderr, "Usage: "LFDK_PROGNAME" [-h] [-d /dev/lfdd] [-n ./pci.ids] [-b 255]\n" );
fprintf( stderr, "\t-n\tFilename of PCI Name Database, default is /usr/share/misc/pci.ids\n" );
fprintf( stderr, "\t-d\tDevice name of Linux Firmware Debug Driver, default is /dev/lfdd\n" );
fprintf( stderr, "\t-b\tMaximum PCI Bus number to scan, default is 255\n" );
fprintf( stderr, "\t-h\tprint this message.\n");
fprintf( stderr, "\n");
}
void InitColorPairs( void ) {
init_pair( WHITE_RED, COLOR_WHITE, COLOR_RED );
init_pair( WHITE_BLUE, COLOR_WHITE, COLOR_BLUE );
init_pair( BLACK_WHITE, COLOR_BLACK, COLOR_WHITE );
init_pair( CYAN_BLUE, COLOR_CYAN, COLOR_BLUE );
init_pair( RED_BLUE, COLOR_RED, COLOR_BLUE );
init_pair( YELLOW_BLUE, COLOR_YELLOW, COLOR_BLUE );
init_pair( BLACK_GREEN, COLOR_BLACK, COLOR_GREEN );
init_pair( BLACK_YELLOW, COLOR_BLACK, COLOR_YELLOW );
init_pair( YELLOW_RED, COLOR_YELLOW, COLOR_RED );
init_pair( YELLOW_BLACK, COLOR_YELLOW, COLOR_BLACK );
init_pair( WHITE_YELLOW, COLOR_WHITE, COLOR_YELLOW );
}
void PrintBaseScreen( void ) {
//
// Background Color
//
PrintWin( BaseScreen, bg, 23, 80, 0, 0, WHITE_BLUE, "" );
//
// Base Screen
//
PrintWin( BaseScreen, logo, 1, 80, 0, 0, WHITE_RED, "Linux Firmware Debug Kit "LFDK_VERSION );
PrintWin( BaseScreen, copyright, 1, 32, 0, 48, WHITE_RED, "Merck Hung <merckhung@gmail.com>" );
PrintWin( BaseScreen, help, 1, 80, 23, 0, BLACK_WHITE, "(Q)uit (P)CI (M)emory (I)O CM(O)S" );
update_panels();
doupdate();
}
int main( int argc, char **argv ) {
extern char *optarg;
extern int optind;
char c, device[ LFDK_MAX_PATH ];
int i, fd, orig_fl;
struct tm *nowtime;
time_t timer;
int last_sec;
//
// Initialize & set default value
//
// strncpy( device, LFDD_DEFAULT_PATH, LFDK_MAX_PATH );
strncpy( pciname, LFDK_DEFAULT_PCINAME, LFDK_MAX_PATH );
while( (c = getopt( argc, argv, "b:n:d:h" )) != EOF ) {
switch( c ) {
//
// Change default path of device
//
case 'd' :
strncpy( device, optarg, LFDK_MAX_PATH );
break;
//
// Change default path of PCI name database
//
case 'n' :
strncpy( pciname, optarg, LFDK_MAX_PATH );
break;
case 'b' :
maxpcibus = atoi( optarg );
if( maxpcibus >= LFDK_MAX_PCIBUS ) {
fprintf( stderr, "Maximum PCI Bus value must be less than %d\n", LFDK_MAX_PCIBUS );
return 1;
}
break;
case 'v' :
break;
case 'h' :
default:
usage();
return 1;
}
}
//
// Start communicate with LFDD I/O control driver
//
fd = open("/dev/mem", O_RDWR);
if( fd < 0 ) {
fprintf( stderr, "Cannot open device: %s\n", "/dev/mem" );
return 1;
}
if (fd) close(fd);
//
// Ncurse start
//
initscr();
start_color();
cbreak();
noecho();
nodelay( stdscr, 1 );
keypad( stdscr, 1 );
curs_set( 0 );
//
// Initialize color pairs for later use
//
InitColorPairs();
//
// Prepare Base Screen
//
PrintBaseScreen();
//
// Scan PCI devices
//
ScanPCIDevice();
for( ; ; ) {
ibuf = getch();
if( (ibuf == 'q') || (ibuf == 'Q') ) {
//
// Exit when ESC pressed
//
break;
}
//
// Major function switch key binding
//
if( (ibuf == 'p') || (ibuf == 'P') ) {
func = PCI_LIST_FUNC;
ClearPCIScreen();
ClearMemScreen();
ClearIOScreen();
ClearCmosScreen();
continue;
}
else if( (ibuf == 'm') || (ibuf == 'M') ) {
enter_mem = 1;
func = MEM_SPACE_FUNC;
ClearPCIScreen();
ClearPCILScreen();
ClearIOScreen();
ClearCmosScreen();
continue;
}
else if( (ibuf == 'i') || (ibuf == 'I') ) {
enter_mem = 1;
func = IO_SPACE_FUNC;
ClearPCIScreen();
ClearPCILScreen();
ClearMemScreen();
ClearCmosScreen();
continue;
}
else if( (ibuf == 'o') || (ibuf == 'O') ) {
enter_mem = 1;
func = CMOS_SPACE_FUNC;
ClearPCIScreen();
ClearPCILScreen();
ClearMemScreen();
ClearIOScreen();
continue;
}
/*
else if( ibuf == '2' ) {
enter_mem = 1;
func = I2C_SPACE_FUNC;
ClearPCIScreen();
ClearPCILScreen();
ClearMemScreen();
ClearIOScreen();
ClearCmosScreen();
continue;
}
*/
//
// Update timer
//
time( &timer );
nowtime = localtime( &timer );
last_sec = nowtime->tm_sec;
// Skip redundant update of timer
if( last_sec == nowtime->tm_sec ) {
PrintFixedWin( BaseScreen, time, 1, 8, 23, 71, BLACK_WHITE, "%2.2d:%2.2d:%2.2d", nowtime->tm_hour, nowtime->tm_min, nowtime->tm_sec );
}
//
// Major Functions
//
switch( func ) {
case PCI_DEVICE_FUNC:
PrintPCIScreen( fd );
break;
case PCI_LIST_FUNC:
PrintPCILScreen();
break;
case MEM_SPACE_FUNC:
PrintMemScreen( fd );
break;
case IO_SPACE_FUNC:
PrintIOScreen( fd );
break;
case CMOS_SPACE_FUNC:
PrintCmosScreen( fd );
break;
case I2C_SPACE_FUNC:
PrintCmosScreen( fd );
break;
default:
break;
}
//
// Refresh Screen
//
update_panels();
doupdate();
usleep( 1000 );
}
endwin();
close( fd );
fprintf( stderr, "\n" );
usage();
return 0;
}