1 /*
2    Xceive XC2028/3028 tuner module firmware manipulation tool
3 
4    Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com>
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation version 2
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #include "standards.h"
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include <asm/byteorder.h>
29 #include <asm/types.h>
30 
31 #define MAX(a,b) ((a) >= (b) ? (a) : (b))
32 
33 struct vector {
34 	unsigned char* data;
35 	unsigned int size;
36 };
37 
alloc_vector(unsigned int size)38 static struct vector* alloc_vector(unsigned int size) {
39 	struct vector *v = malloc(sizeof(*v));
40 	v->data = malloc(size);
41 	v->size = size;
42 	return v;
43 }
44 
free_vector(struct vector * v)45 static void free_vector(struct vector* v) {
46 	free(v->data);
47 	free(v);
48 }
49 
enlarge_vector(struct vector * v,unsigned int new_size)50 static void enlarge_vector(struct vector* v, unsigned int new_size) {
51 	unsigned char *n_data;
52 	unsigned int old_size = v->size;
53 
54 	v->size = MAX(v->size, new_size);
55 	n_data = malloc(v->size);
56 	memcpy(n_data, v->data, old_size);
57 	free(v->data);
58 	v->data = n_data;
59 }
60 
copy_vector(struct vector * v,unsigned int i,unsigned char * ptr,unsigned int len)61 static void copy_vector(struct vector *v, unsigned int i,
62 		 unsigned char* ptr, unsigned int len) {
63 	if(i + len > v->size) {
64 		enlarge_vector(v, MAX(2 * v->size, i + len));
65 	}
66 	memcpy(v->data + i, ptr, len);
67 }
68 
write_vector8(struct vector * v,unsigned int i,uint8_t value)69 static void write_vector8(struct vector *v, unsigned int i, uint8_t value) {
70 	uint8_t buf[1];
71 
72 	buf[0] = value;
73 	copy_vector(v, i, buf, 1);
74 }
75 
write_vector16(struct vector * v,unsigned int i,uint16_t value)76 static void write_vector16(struct vector *v, unsigned int i, uint16_t value) {
77 	uint8_t buf[2];
78 
79 	buf[0] = value & 0xff;
80 	buf[1] = value >> 8;
81 	copy_vector(v, i, buf, 2);
82 }
83 
84 static const char reset_tuner_str[] = "RESET_TUNER";
85 static const char reset_clk_str[] = "RESET_CLK";
86 
create_standard_data(char * filename,unsigned char ** data,unsigned int * r_len)87 void create_standard_data(char* filename, unsigned char** data, unsigned int *r_len) {
88 	FILE *file;
89 	char* line = NULL;
90 	ssize_t r = 0;
91 	size_t len = 0;
92 	struct vector *v = alloc_vector(1);
93 	unsigned int v_i = 0;
94 
95 	if (!(file = fopen(filename, "r"))) {
96 		perror("Cannot open the firmware standard file.\n");
97 		*data = NULL;
98 	}
99 
100 	while ((r = getline(&line, &len, file)) != -1) {
101 		unsigned int i = 0;
102 		unsigned int val, count = 0;
103 		unsigned int values[len];
104 
105 		printf("read line \"%s\"\n", line);
106 
107 		if(len >= 9 && memcmp(reset_clk_str, line, strlen(reset_clk_str) - 1) == 0) {
108 			printf("adding RESET_CLK\n");
109 			write_vector16(v, v_i, (uint16_t) 0xff00);
110 			v_i += 2;
111 			continue;
112 		}
113 		else if(len >= 11 && memcmp(reset_tuner_str, line, strlen(reset_tuner_str) - 1) == 0) {
114 			printf("adding RESET_TUNER\n");
115 			write_vector16(v, v_i, (uint16_t) 0x0000);
116 			v_i += 2;
117 			continue;
118 		}
119 
120 		while(i < len && sscanf(line + i*sizeof(char), "%2x", &val) == 1) {
121 			printf("%2x ", val);
122 			values[count] = val;
123 			++count;
124 			i += 2;
125 			while(line[i] == ' ') {
126 				++i;
127 			}
128 		}
129 
130 		write_vector16(v, v_i, __cpu_to_le16((uint16_t) count));
131 		v_i += 2;
132 
133 
134 		for(i = 0; i < count; ++i) {
135 			write_vector8(v, v_i, (uint8_t) values[i]);
136 			++v_i;
137 		}
138 
139 		printf("\n");
140 	}
141 	write_vector16(v, v_i, 0xffff);
142 	v_i += 2;
143 
144 	free(line);
145 	fclose(file);
146 	*data = malloc(v_i);
147 	memcpy(*data, v->data, v_i);
148 	free_vector(v);
149 	*r_len = v_i;
150 }
151 
152