/alps/pcitool

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/pcitool
220 by Suren A. Chilingaryan
Add HEB scripts and re-organize the structure
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdint.h>
4
#include <unistd.h>
5
#include <sys/types.h>
6
#include <sys/stat.h>
7
8
int main(int argc, char *argv[]) {
9
    long i, num;
10
    size_t count = 0, total = 0, size;
11
    int offset = 3, toread = 1, toskip = 3;
12
    uint32_t value;
13
    uint32_t *buf;
14
    uint32_t expected = 0;
15
    uint32_t blocks = 0, status_good = 0;
16
    
17
    char fixed[4096];
18
    struct stat st_buf;
19
    
20
    if ((argc != 2)&&(argc != 5)) {
21
	printf("Usage: %s <file> [offset_dwords read_dwords skip_dwords] \n", argv[0]);
22
	exit(0);
23
    }
24
    
25
    FILE *f = fopen(argv[1], "r");
26
    if (!f) {
27
	printf("Can't open %s\n", argv[1]);
28
	exit(1);
29
    }
30
31
    stat(argv[1], &st_buf);
32
    size = st_buf.st_size / sizeof(uint32_t);
33
34
35
    buf = malloc(size * sizeof(uint32_t));
36
    if (!buf) {
37
	printf("Can't allocate %lu bytes of memory\n", size * sizeof(uint32_t));
38
	exit(1);
39
    }
40
    
41
    if (argc == 5) {
42
	offset = atoi(argv[2]);
43
	toread = atoi(argv[3]);
44
	toskip = atoi(argv[4]);
45
    }
46
    
47
48
    num = fread(buf, 4, size, f);
49
    if (num != size) {
50
	printf("Failed to read %lu dwords, only %lu read\n", size, num);
51
	exit(1);
52
    }
53
    fclose(f);
54
    
55
    sprintf(fixed, "%s.fixed", argv[1]);
56
    f = fopen(fixed, "w");
57
    if (!f) {
58
	printf("Failed to open %s for output\n", fixed);
59
	exit(1);
60
    }
61
    
62
    expected = (buf[offset]>>24) + 2;
63
    for (i = 1; i < size; i += (toread + toskip)) {
64
	total++;
65
	    
66
	value = buf[i + offset] >> 24;
67
//	printf("0x%lx: value (%lx) = expected (%lx)\n", i + offset, value, expected);
68
	if (value == expected) {
69
	    if (!status_good) {
70
		status_good = 1;
71
		blocks++;
72
	    }
73
	    fwrite(&buf[i], 4, toread + toskip, f);
74
	    expected += 2;
75
	    if (expected == 0xb8) 
76
		expected = 0;
77
	} else if ((!status_good)&&(value == 0)&&((i + toread + toskip)< size)) {
78
	    value = buf[i + offset + toread + toskip] >> 24;
79
	    if (value == 2) {
80
		status_good = 1;
81
		blocks++;
82
		fwrite(&buf[i], 4, toread + toskip, f);
83
		expected = 2;
84
	    } else {
85
		count++;
86
	    }
87
	} else {
88
	    printf("0x%lx: value (%x) = expected (%x)\n", (i + offset)*sizeof(uint32_t), value, expected);
89
	    status_good = 0;
90
	    count++;
91
	}
92
    }
93
    fclose(f);
94
    free(buf);
95
    
96
    printf("%lu of %lu is wrong\n", count, total);
97
    return 0;
98
}