aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/modems/xmm616/xmm616.c
blob: e7fc48763a61c9609dae46859fb3f4bf2572738b (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
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
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2011-2014 Paul Kocialkowski <contact@paulk.fr>
 *
 * libsamsung-ipc 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 2 of the License, or
 * (at your option) any later version.
 *
 * libsamsung-ipc 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 libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <sys/select.h>

#include <samsung-ipc.h>

#include "xmm616.h"

int xmm616_psi_send(struct ipc_client *client, int serial_fd,
		    const void *psi_data, unsigned short psi_size)
{
	char at[] = XMM616_AT;
	unsigned char version;
	unsigned char info;
	unsigned char psi_magic;
	unsigned char psi_crc;
	unsigned char psi_ack;
	struct termios termios;
	struct timeval timeout;
	fd_set fds;
	size_t length;
	unsigned char *p;
	int rc;
	int i;

	if (client == NULL || serial_fd < 0 || psi_data == NULL ||
	    psi_size == 0) {
		return -1;
	}

	tcgetattr(serial_fd, &termios);

	cfmakeraw(&termios);
	cfsetispeed(&termios, B115200);
	cfsetospeed(&termios, B115200);

	tcsetattr(serial_fd, TCSANOW, &termios);

	length = strlen(at);
	for (i = 0; i < XMM616_AT_COUNT; i++) {
		rc = write(serial_fd, at, length);
		if (rc < (int) length) {
			ipc_client_log(client, "Writing AT in ASCII failed");
			goto error;
		}

		usleep(50000);
	}
	ipc_client_log(client, "Wrote AT in ASCII");

	usleep(50000);

	version = 0;

	rc = read(serial_fd, &version, sizeof(version));
	if (rc < (int) sizeof(version)) {
		ipc_client_log(client, "Reading bootcore version failed");
		goto error;
	}

	if (version != XMM616_BOOTCORE_VERSION) {
		ipc_client_log(client, "Read wrong bootcore version (0x%x)",
			       version);
		goto error;
	}

	ipc_client_log(client, "Read bootcore version (0x%x)", version);

	rc = read(serial_fd, &info, sizeof(info));
	if (rc < (int) sizeof(info)) {
		ipc_client_log(client, "Reading info size failed");
		goto error;
	}
	ipc_client_log(client, "Read info size (0x%x)", info);

	psi_magic = XMM616_PSI_MAGIC;

	rc = write(serial_fd, &psi_magic, sizeof(psi_magic));
	if (rc < (int) sizeof(psi_magic)) {
		ipc_client_log(client, "Writing PSI magic failed");
		goto error;
	}
	ipc_client_log(client, "Wrote PSI magic (0x%x)", psi_magic);

	rc = write(serial_fd, &psi_size, sizeof(psi_size));
	if (rc < (int) sizeof(psi_size)) {
		ipc_client_log(client, "Writing PSI size failed");
		goto error;
	}
	ipc_client_log(client, "Wrote PSI size (0x%x)", psi_size);

	FD_ZERO(&fds);
	FD_SET(serial_fd, &fds);

	timeout.tv_sec = 4;
	timeout.tv_usec = 0;

	p = (unsigned char *) psi_data;
	psi_crc = 0;

	for (i = 0; i < psi_size; i++) {
		rc = select(serial_fd + 1, NULL, &fds, NULL, &timeout);
		if (rc <= 0) {
			ipc_client_log(client, "Writing PSI failed");
			goto error;
		}

		rc = write(serial_fd, p, 1);
		if (rc < 1) {
			ipc_client_log(client, "Writing PSI failed");
			goto error;
		}

		psi_crc ^= *p++;
	}
	ipc_client_log(client, "Wrote PSI, CRC is 0x%x", psi_crc);

	rc = select(serial_fd + 1, NULL, &fds, NULL, &timeout);
	if (rc <= 0) {
		ipc_client_log(client, "Writing PSI crc failed");
		goto error;
	}

	rc = write(serial_fd, &psi_crc, sizeof(psi_crc));
	if (rc < (int) sizeof(psi_crc)) {
		ipc_client_log(client, "Writing PSI crc failed");
		goto error;
	}
	ipc_client_log(client, "Wrote PSI CRC (0x%x)", psi_crc);

	timeout.tv_sec = 4;
	timeout.tv_usec = 0;

	i = 0;
	do {
		rc = select(serial_fd + 1, &fds, NULL, NULL, &timeout);
		if (rc <= 0) {
			ipc_client_log(client, "Reading PSI ACK failed");
			goto error;
		}

		rc = read(serial_fd, &psi_ack, sizeof(psi_ack));
		if (rc < (int) sizeof(psi_ack)) {
			ipc_client_log(client, "Reading PSI ACK failed");
			goto error;
		}

		if (i++ > 50) {
			ipc_client_log(client, "Reading PSI ACK failed");
			goto error;
		}
	} while (psi_ack != XMM616_PSI_ACK);

	ipc_client_log(client, "Read PSI ACK (0x%x)", psi_ack);

	rc = 0;
	goto complete;

error:
	rc = -1;

complete:
	return rc;
}

int xmm616_firmware_send(struct ipc_client *client, int device_fd,
			 void *device_address, const void *firmware_data,
			 size_t firmware_size)
{
	size_t wc;
	unsigned char *p;
	int rc;

	if (client == NULL || (device_fd < 0 && device_address == NULL) ||
	    firmware_data == NULL || firmware_size == 0) {
		return -1;
	}

	p = (unsigned char *) firmware_data;

	if (device_address != NULL) {
		memcpy(device_address, (void *) p, firmware_size);
	} else {
		wc = 0;
		while (wc < firmware_size) {
			rc = write(device_fd, (void *) p, firmware_size - wc);
			if (rc <= 0) {
				ipc_client_log(client,
					       "Writing firmware failed");
				goto error;
			}

			p += rc;
			wc += rc;
		}
	}
	ipc_client_log(client, "Wrote firmware");

	rc = 0;
	goto complete;

error:
	rc = -1;

complete:
	return rc;
}

int xmm616_nv_data_send(struct ipc_client *client, int device_fd,
			void *device_address)
{
	void *nv_data = NULL;
	size_t nv_size;
	size_t wc;
	unsigned char *p;
	int rc;

	if (client == NULL || (device_fd < 0 && device_address == NULL))
		return -1;

	nv_size = ipc_client_nv_data_size(client);
	if (nv_size == 0)
		return -1;

	nv_data = ipc_nv_data_load(client);
	if (nv_data == NULL) {
		ipc_client_log(client, "Loading nv_data failed");
		goto error;
	}
	ipc_client_log(client, "Loaded nv_data");

	p = (unsigned char *) nv_data;

	if (device_address != NULL) {
		memcpy(device_address, p, nv_size);
	} else {
		wc = 0;
		while (wc < nv_size) {
			rc = write(device_fd, p, nv_size - wc);
			if (rc <= 0) {
				ipc_client_log(client,
					       "Writing modem image failed");
				goto error;
			}

			p += rc;
			wc += rc;
		}
	}

	rc = 0;
	goto complete;

error:
	rc = -1;

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

	return rc;
}