summaryrefslogtreecommitdiffstats
path: root/pcm-analysis/pcm-analysis.c
blob: d7d469948273f9668c75a65b1db5d48e6e2f3693 (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
/*
 * Copyright (C) 2016-2017 Paul Kocialkowski <contact@paulk.fr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <pcm/pcm.h>
#include "pcm-analysis.h"

struct pcm_ops_data pcm_ops_data = {
	.address = 0,
};

struct pcm_ops pcm_ops = {
	.asm_write = NULL,
	.disasm_write = pcm_disasm_write,
	.data = (void *) &pcm_ops_data,
};

int pcm_disasm_write(void *data, unsigned int value, char *mnemonic)
{
	struct pcm_ops_data *ops_data;

	if (data == NULL)
		return -1;

	ops_data = (struct pcm_ops_data *) data;

	if (mnemonic != NULL)
		printf("[0x%04x] 0x%08x ; %s\n", ops_data->address, value, mnemonic);
	else
		printf("[0x%04x] 0x%08x\n", ops_data->address, value);

	ops_data->address++;

	return 0;
}

int firmware_values(char *file, unsigned int **values)
{
	struct stat stat_data;
	size_t size;
	int fd = -1;
	int rc;

	if (file == NULL || values == NULL)
		return -1;

	rc = stat(file, &stat_data);
	if (rc < 0) {
		fprintf(stderr, "Invalid file: %s\n", file);
		goto error;
	}

	size = stat_data.st_size;
	*values = malloc(size);

	fd = open(file, O_RDONLY, 0644);
	if (fd < 0) {
		fprintf(stderr, "Unable to open file: %s\n", file);
		goto error;
	}

	rc = read(fd, *values, size);
	if (rc <= 0) {
		fprintf(stderr, "Unable to read file: %s\n", file);
		goto error;
	}

	rc = size / sizeof(unsigned int);
	goto complete;

error:
	rc = -1;

	if (*values != NULL)
		free(*values);

complete:
	if (fd >= 0)
		close(fd);

	return rc;
}

int main(int argc, char *argv[])
{
	unsigned int *values = NULL;
	unsigned int count;
	int rc;
	int i;

	if (argc < 2 || argv[1] == NULL)
		return 1;

	rc = firmware_values(argv[1], &values);
	if (rc < 0 || values == NULL)
		goto error;

	count = rc;

	rc = pcm_ops_register(&pcm_ops);
	if (rc < 0) {
		fprintf(stderr, "Unable to set PCM OPS\n");
		goto error;
	}

	i = 0;

	while (count > 0) {
		rc = pcm_op_disasm(&values[i], count);
		if (rc < 0) {
			pcm_disasm_write(pcm_ops.data, values[i], NULL);
			rc = 0;
		}

		count -= 1 + rc;
		i += 1 + rc;
	}

	rc = 0;
	goto complete;

error:
	rc = 1;

complete:
	if (values != NULL)
		free(values);

	return rc;
}