aboutsummaryrefslogtreecommitdiffstats
path: root/src/lg-downloader.c
blob: 827cc4b1651c58432e6e0d0910d3d5994454df53 (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
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
/*
 * Copyright (C) 2016 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 <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "lg-downloader.h"
#include "gpt.h"
#include "download.h"
#include "usb.h"

static int usage_print(void)
{
	printf("Usage: lg-downloader [OPTIONS] [OPERATION]\n\n"
		"Options:\n"
		" -h                             help\n"
		" -v                             verbose\n"
		" -w                             wait for device\n\n"
		"Operations:\n"
		" reboot                         reboot device\n"
		" erase [partition] [filename]   erase a partition\n"
		" flash [partition] [filename]   flash a file to partition\n"
		" dump [partition] [filename]    dump a partition to file\n"
		" partitions                     list partitions\n");
}

static int arguments_parse(struct context *context, int argc, char *argv[])
{
	int i;

	if (context == NULL || argc < 2)
		return -1;

	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "reboot") == 0) {
			if (context->operation)
				return -1;

			context->operation = 'r';
		} else if (strcmp(argv[i], "erase") == 0) {
			if (context->operation || argc <= (i + 1))
				return -1;

			context->operation = 'e';
			context->partition = argv[++i];
			context->filename = argv[++i];
		} else if (strcmp(argv[i], "flash") == 0) {
			if (context->operation || argc <= (i + 2))
				return -1;

			context->operation = 'f';
			context->partition = argv[++i];
			context->filename = argv[++i];
		} else if (strcmp(argv[i], "dump") == 0) {
			if (context->operation || argc <= (i + 2))
				return -1;

			context->operation = 'd';
			context->partition = argv[++i];
			context->filename = argv[++i];
		} else if (strcmp(argv[i], "partitions") == 0) {
			if (context->operation)
				return -1;

			context->operation = 'p';
		} else if (strcmp(argv[i], "-h") == 0) {
			if (context->operation)
				return -1;

			context->operation = 'h';
		} else if (strcmp(argv[i], "-v") == 0) {
			context->verbose = 1;
		} else if (strcmp(argv[i], "-w") == 0) {
			context->wait = 1;
		} else {
			return -1;
		}
	}

	if (!context->operation)
		return -1;

	return 0;
}

int erase(struct context *context)
{
	int rc;

	rc = gpt_partition_find(context);
	if (rc < 0) {
		fprintf(stderr, "Finding GPT partition failed\n");
		return -1;
	}

	if (context->verbose)
		printf("Erasing partition with address %d blocks and length %d bytes\n", (int) context->address, (int) context->length);

	rc = download_erase(context, context->address, context->length);
	if (rc < 0)
		return -1;

	return 0;
}

int flash(struct context *context)
{
	void *buffer = NULL;
	off_t address;
	size_t length;
	size_t count;
	size_t chunk;
	uint8_t *p;
	size_t i;
	struct stat st;
	int fd = -1;
	int rc;

	if (context == NULL || context->filename == NULL)
		return -1;

	rc = gpt_partition_find(context);
	if (rc < 0) {
		fprintf(stderr, "Finding GPT partition failed\n");
		goto error;
	}

	rc = stat(context->filename, &st);
	if (rc < 0) {
		fprintf(stderr, "Stating file failed\n");
		goto error;
	}

	length = st.st_size;

	if (length > context->length) {
		fprintf(stderr, "File size (%d bytes) is too big for partition (%d bytes)\n", (int) length, (int) context->length);
		goto error;
	}

	fd = open(context->filename, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "Opening file failed\n");
		goto error;
	}

	if (context->verbose)
		printf("Writing to partition with address %d blocks and length %d bytes\n", (int) context->address, (int) length);

	buffer = calloc(1, GPT_BLOCK_SIZE);

	rc = download_start_flash_write(context);
	if (rc < 0)
		goto error;

	address = context->address;
	count = 0;

	rc = download_initialize_partition(context, address, GPT_BLOCK_SIZE);
	if (rc < 0)
		goto error;

	while (count < length) {
		chunk = (length - count) < GPT_BLOCK_SIZE ? (length - count) : GPT_BLOCK_SIZE;

		/* Writes are always block-aligned: fill the rest with zeros */
		if (chunk < GPT_BLOCK_SIZE)
			memset(buffer, 0, GPT_BLOCK_SIZE);

		p = (uint8_t *) buffer;
		i = 0;

		while (i < chunk) {
			rc = read(fd, p, chunk - i);
			if (rc <= 0)
				goto error;

			i += rc;
			p += rc;
		}

		rc = download_write_async(context, buffer, GPT_BLOCK_SIZE);
		if (rc < 0)
			goto error;

		count += chunk;
		address++;
	}

	rc = 0;
	goto complete;

error:
	rc = -1;

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

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

	return rc;
}

int dump(struct context *context)
{
	void *buffer = NULL;
	off_t address;
	size_t length;
	size_t count;
	size_t chunk;
	uint8_t *p;
	size_t c;
	int fd = -1;
	int rc;

	if (context == NULL || context->filename == NULL)
		return -1;

	rc = gpt_partition_find(context);
	if (rc < 0) {
		fprintf(stderr, "Finding GPT partition failed\n");
		goto error;
	}

	fd = open(context->filename, O_WRONLY | O_CREAT | O_TRUNC, 644);
	if (fd < 0) {
		fprintf(stderr, "Opening file failed\n");
		goto error;
	}

	if (context->verbose)
		printf("Dumping partition with address %d blocks and length %d bytes\n", (int) context->address, (int) context->length);

	buffer = calloc(1, GPT_BLOCK_SIZE);

	address = context->address;
	length = context->length;
	count = 0;

	rc = download_ready_read(context, address, GPT_BLOCK_SIZE);
	if (rc < 0)
		goto error;

	while (count < length) {
		chunk = (length - count) < GPT_BLOCK_SIZE ? (length - count) : GPT_BLOCK_SIZE;

		if (chunk != GPT_BLOCK_SIZE) {
			rc = download_ready_read(context, address, chunk);
			if (rc < 0)
				goto error;
		}

		rc = download_read(context, buffer, chunk);
		if (rc < 0)
			goto error;

		c = 0;
		p = (uint8_t *) buffer;

		while (c < chunk) {
			rc = write(fd, p, chunk - c);
			if (rc <= 0)
				goto error;

			c += rc;
			p += rc;
		}

		count += chunk;
		address++;
	}

	rc = 0;
	goto complete;

error:
	rc = -1;

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

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

	return rc;
}

static int partitions(struct context *context)
{
	int rc;

	rc = gpt_partitions_print(context);
	if (rc < 0)
		return -1;

	return 0;
}

int main(int argc, char *argv[])
{
	struct context context = { 0 };
	int rc;

	rc = arguments_parse(&context, argc, argv);
	if (rc < 0) {
		usage_print();
		return -1;
	}

	if (context.operation == 'h') {
		usage_print();
		return 0;
	}

	printf("Finding and opening USB device\n");

	rc = usb_open(&context);
	if (rc < 0)
		goto error;

	if (context.verbose)
		printf("Notifying download\n");

	rc = download_notify_start_dl(&context);
	if (rc < 0)
		goto error;

	switch (context.operation) {
		case 'r':
			printf("Rebooting device...\n");

			rc = download_reset(&context);
			if (rc < 0) {
				fprintf(stderr, "Rebooting device failed\n");
				goto error;
			}

			break;
		case 'e':
			printf("Erasing partition %s...\n", context.partition);

			/* Give the user a chance to hit Ctrl+C */
			sleep(1);

			rc = erase(&context);
			if (rc < 0) {
				fprintf(stderr, "Erasing partition failed\n");
				goto error;
			}

			break;
		case 'f':
			printf("Flashing %s to partition %s...\n", context.filename, context.partition);

			rc = flash(&context);
			if (rc < 0) {
				fprintf(stderr, "Flashing partition failed\n");
				goto error;
			}

			break;
		case 'd':
			printf("Dumping partition %s to %s...\n", context.partition, context.filename);

			rc = dump(&context);
			if (rc < 0) {
				fprintf(stderr, "Dumping partition failed\n");
				goto error;
			}

			break;
		case 'p':
			printf("Reading partitions table...\n");

			rc = partitions(&context);
			if (rc < 0) {
				fprintf(stderr, "Reading partitions table failed\n");
				goto error;
			}

			break;
		default:
			goto error;
	}

	printf("Done!\n");

	rc = 0;
	goto complete;

error:
	rc = 1;

complete:
	usb_close(&context);

	return rc;
}