summaryrefslogtreecommitdiff
path: root/cmd/odroid_fastboot.c
blob: 9a3201432b598956997e75232e7bde08d62a8cac (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
 * Copyright 2008 - 2009 Windriver, <www.windriver.com>
 * Author: Tom Rix <Tom.Rix@windriver.com>
 *
 * (C) Copyright 2014 Linaro, Ltd.
 * Rob Herring <robh@kernel.org>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */
#include <common.h>
#include <command.h>
#include <console.h>
#include <malloc.h>

#include <mach/power.h>
#include <fastboot.h>
#include <samsung/odroid_misc.h>
#include <mmc.h>

/*---------------------------------------------------------------------------*/
static unsigned int download_size;
static unsigned int download_bytes;
static unsigned int download_error;
static int rx_handler (const unsigned char *buffer, unsigned int buffer_size);

/*---------------------------------------------------------------------------*/
static struct cmd_fastboot_interface interface =
{
	.rx_handler            = rx_handler,
	.reset_handler         = NULL,
	.product_name          = NULL,
	.serial_no             = NULL,
	.nand_block_size       = 0,
	.transfer_buffer       = (unsigned char *)0xffffffff,
	.transfer_buffer_size  = 0,
};

/*---------------------------------------------------------------------------*/
static int flashing_raw_data(struct partition_info *pinfo,
	unsigned int addr, unsigned int size, unsigned int dev_no)
{
	struct mmc *mmc;
	char cmd[64] = { 0, };
	unsigned int blk_start = pinfo->blk_start;
	unsigned int blk_cnt  = size / MOVI_BLK_SIZE;

	mmc = find_mmc_device(dev_no);

	if (!IS_SD(mmc) && pinfo->raw_en) {
		memset(cmd, 0x00, sizeof(cmd));
		sprintf(cmd, "emmc open %d", dev_no);
		run_command(cmd, 0);
		blk_start = pinfo->blk_start -1;
	}

	mmc->block_dev.block_write(&mmc->block_dev,
		blk_start,
		blk_cnt,
		(const void *)addr);
	printf("mmc block write, dev %d, addr 0x%x, blk start %d, blk cnt %d\n",
		dev_no,
		(unsigned int)addr,
		blk_start,
		blk_cnt);

	if (!IS_SD(mmc) && pinfo->raw_en) {
		memset(cmd, 0x00, sizeof(cmd));
		sprintf(cmd, "emmc close %d", dev_no);
		run_command(cmd, 0);
	}
	return	0;
}

/*---------------------------------------------------------------------------*/
static void erase_partition(struct partition_info *pinfo, unsigned int dev_no)
{
	#define	BLOCK_ERASE_SIZE	(512 * 1024)

	unsigned int blk_start = pinfo->blk_start;
	unsigned int blk_cnt  = pinfo->size / MOVI_BLK_SIZE;
	struct mmc *mmc;
	unsigned char *clrbuf =
		(unsigned char *)calloc(sizeof(char), BLOCK_ERASE_SIZE);

	printf("Erasing partition(%s)... blk_st = 0x%08x, blk_cnt = 0x%08x\n",
		pinfo->name, blk_start, blk_cnt);

	mmc = find_mmc_device(dev_no);

	if (blk_start & 0x3FF) {
		mmc->block_dev.block_write(&mmc->block_dev,
			blk_start,
			1024 - (blk_start & 0x3ff),
			clrbuf);
		printf("*** erase start block 0x%x ***\n", blk_start);
		blk_cnt = blk_cnt - (1024 - (blk_start & 0x3FFF));
		blk_start = (blk_start & (~0x3FFF)) + 1024;
	}
	if (blk_cnt & 0x3FF) {
		mmc->block_dev.block_write(&mmc->block_dev,
			blk_start + blk_cnt - (blk_cnt & 0x3FF),
			(blk_cnt & 0x3FF),
			clrbuf);
		printf("*** erase block length 0x%x ***\n", blk_cnt);
		blk_cnt = blk_cnt - (blk_cnt & 0x3FFF);
	}
	if (blk_cnt >> 10) {
		mmc->block_dev.block_erase(&mmc->block_dev, blk_start, blk_cnt);
		printf("*** erase block start 0x%x, cnt 0x%x ***\n", blk_start, blk_cnt);
	}

	free(clrbuf);
}

/*---------------------------------------------------------------------------*/
static int check_compress_ext4(char *img_base, unsigned long long parti_size) {
	ext4_file_header *file_header;

	file_header = (ext4_file_header*)img_base;

	if (file_header->magic != EXT4_FILE_HEADER_MAGIC) {
		return -1;
	}

	if (file_header->major != EXT4_FILE_HEADER_MAJOR) {
		printf("Invalid Version Info! 0x%2x\n", file_header->major);
		return -1;
	}

	if (file_header->file_header_size != EXT4_FILE_HEADER_SIZE) {
		printf("Invalid File Header Size! 0x%8x\n",
			file_header->file_header_size);
		return -1;
	}

	if (file_header->chunk_header_size != EXT4_CHUNK_HEADER_SIZE) {
		printf("Invalid Chunk Header Size! 0x%8x\n",
			file_header->chunk_header_size);
		return -1;
	}

	if (file_header->block_size != EXT4_FILE_BLOCK_SIZE) {
		printf("Invalid Block Size! 0x%8x\n", file_header->block_size);
		return -1;
	}

	if ((parti_size/file_header->block_size)  < file_header->total_blocks) {
		printf("Invalid Volume Size! Image is bigger than partition size!\n");
		printf("partion size %lld , image size %d \n",
			(parti_size/file_header->block_size), file_header->total_blocks);
		while(1);
	}

	/* image is compressed ext4 */
	return 0;
}

/*---------------------------------------------------------------------------*/
static int write_compressed_ext4(char* img_base, unsigned int sector_base,
	unsigned int dev_no)
{
	#define SECTOR_BITS	9	/* 512B */

	unsigned int sector_size;
	int total_chunks;
	ext4_chunk_header *chunk_header;
	ext4_file_header *file_header;
	struct mmc *mmc;

	mmc = find_mmc_device(dev_no);

	file_header = (ext4_file_header*)img_base;
	total_chunks = file_header->total_chunks;

	printf("%s : total chunk = %d \n", __func__, total_chunks);

	img_base += EXT4_FILE_HEADER_SIZE;

	odroid_led_ctrl	(GPIO_LED_G, 1);
	while(total_chunks) {
		chunk_header = (ext4_chunk_header*)img_base;
		sector_size =
			(chunk_header->chunk_size *
				file_header->block_size) >> SECTOR_BITS;

		switch(chunk_header->type)
		{
		case EXT4_CHUNK_TYPE_RAW:
			mmc->block_dev.block_write(&mmc->block_dev,
				sector_base,
				sector_size,
				(const void *)(img_base + EXT4_CHUNK_HEADER_SIZE));
			sector_base += sector_size;
			break;

		case EXT4_CHUNK_TYPE_FILL:
			printf("*** fill_chunk ***\n");
			sector_base += sector_size;
			break;

		case EXT4_CHUNK_TYPE_NONE:
			printf("none chunk \n");
			sector_base += sector_size;
			break;

		default:
			printf("*** unknown chunk type ***\n");
			sector_base += sector_size;
			break;
		}
		total_chunks--;
		printf("mmc write dev %d, blk = 0x%08x, size = 0x%08x, remain chunks = %d\n",
			dev_no,
			sector_base,
			sector_size,
			total_chunks);

		img_base += chunk_header->total_size;
	};

	odroid_led_ctrl	(GPIO_LED_G, 0);
	printf("write done \n");
	return 0;
}

/*---------------------------------------------------------------------------*/
static int flashing_data(struct partition_info *pinfo,
	unsigned int addr, unsigned int size, unsigned int dev_no)
{
	if (check_compress_ext4((char *)addr, pinfo->size))
		flashing_raw_data(pinfo, addr, pinfo->size, dev_no);
	else {
		erase_partition(pinfo, dev_no);
		write_compressed_ext4((char*)addr, pinfo->blk_start, dev_no);
	}

	printf("\npartition '%s' flashed.\n\n", pinfo->name);
	return	0;
}

/*---------------------------------------------------------------------------*/
static int rx_handler (const unsigned char *buffer, unsigned int buffer_size)
{
	int ret = 1;

	/* Use 65 instead of 64
	   null gets dropped
	   strcpy's need the extra byte */
	char response[65];

	if (download_size) {
		/* Something to download */

		if (buffer_size) {
			/* Handle possible overflow */
			unsigned int transfer_size = download_size - download_bytes;

			if (buffer_size < transfer_size)
				transfer_size = buffer_size;

			/* Save the data to the transfer buffer */
			memcpy (interface.transfer_buffer + download_bytes,
				buffer, transfer_size);

			download_bytes += transfer_size;

			/* Check if transfer is done */
			if (download_bytes >= download_size) {
				/* Reset global transfer variable,
				   Keep download_bytes because it will be
				   used in the next possible flashing command */
				download_size = 0;

				if (download_error) {
					/* There was an earlier error */
					sprintf(response, "ERROR");
				} else {
					/* Everything has transferred,
					   send the OK response */
					sprintf(response, "OKAY");
				}
				fastboot_tx_status(response, strlen(response), FASTBOOT_TX_ASYNC);

				printf("\ndownloading of %d bytes finished\n", download_bytes);
			}

			/* Provide some feedback */
			if (download_bytes && download_size &&
			    0 == (download_bytes & (0x100000 - 1))) {
				/* Some feeback that the download is happening */
				if (download_error)
					printf("X");
				else
					printf(".");
				if (0 == (download_bytes %
					  (80 * 0x100000)))
					printf("\n");
			}
		} else {
			/* Ignore empty buffers */
			printf("Warning empty download buffer\n");
			printf("Ignoring\n");
		}
		ret = 0;
	} else {
		/* A command */

		/* Cast to make compiler happy with string functions */
		const char *cmdbuf = (char *) buffer;

		/* Generic failed response */
		sprintf(response, "FAIL");

		/* reboot
		   Reboot the board. */
		if (memcmp(cmdbuf, "reboot", 6) == 0) {
			if (!strcmp(cmdbuf + 6, "-bootloader")) {
				struct exynos5_power *pmu =
					(struct exynos5_power *)samsung_get_base_power();
				pmu->sysip_dat0 = FASTBOOT_MAGIC_REBOOT_CMD; 
				printf("reboot-bootloader (reboot fastboot)\n");			    
			} else {
				memset(interface.transfer_buffer, 0x0, FASTBOOT_REBOOT_MAGIC_SIZE);
			}

			sprintf(response,"OKAY");
			fastboot_tx_status(response, strlen(response), FASTBOOT_TX_SYNC);

			do_reset (NULL, 0, 0, NULL);

			/* This code is unreachable,
			   leave it to make the compiler happy */
			return 0;
		}

		/* getvar
		   Get common fastboot variables
		   Board has a chance to handle other variables */
		if (memcmp(cmdbuf, "getvar:", 7) == 0)
		{
			strcpy(response,"OKAY");

			if (!strcmp(cmdbuf + 7, "version")) {
				strcpy(response + 4, FASTBOOT_VERSION);
			} else if (!strcmp(cmdbuf + 7, "product")) {
				if (interface.product_name)
					strcpy(response + 4, interface.product_name);
			} else if (!strcmp(cmdbuf + 7, "serialno")) {
				if (interface.serial_no)
					strcpy(response + 4, interface.serial_no);
			} else if (!strcmp(cmdbuf + 7, "downloadsize")) {
				if (interface.transfer_buffer_size)
					sprintf(response + 4, "%08x", interface.transfer_buffer_size);
			} else {
				fastboot_getvar(cmdbuf + 7, response + 4);
			}
			ret = 0;
			goto send_tx_status;
		}

		/* erase
		   Erase a register flash partition
		   Board has to set up flash partitions */
		if (memcmp(cmdbuf, "erase:", 6) == 0)
		{
			struct partition_info pinfo;

			ret = 0;
			printf("partition '%s' erased\n", cmdbuf + 6);
			if (!strncmp(cmdbuf + 6, "fat", sizeof("fat"))) {
				run_command("fatformat mmc 0:1", 0);
				sprintf(response, "OKAY");
				goto send_tx_status;
			}
			if (odroid_get_partition_info(&cmdbuf[6], &pinfo))
				sprintf(response, "FAILUnsupport partitiond");
			else {
				if (!pinfo.raw_en) {
					erase_partition(&pinfo, 0);
					sprintf(response, "OKAY");
				}
				else
					sprintf(response, "FAILUnsupport or Unknown partitiond");
			}
			goto send_tx_status;
		}

		/* download
		   download something ..
		   What happens to it depends on the next command after data */
		if (memcmp(cmdbuf, "download:", 9) == 0)
		{
			/* save the size */
			download_size = simple_strtoul(cmdbuf + 9, NULL, 16);
			/* Reset the bytes count, now it is safe */
			download_bytes = 0;
			/* Reset error */
			download_error = 0;

			printf("Starting download of %d bytes\n", download_size);

			if (0 == download_size) {
				/* bad user input */
				sprintf(response, "FAILdata invalid size");
			} else if (download_size > interface.transfer_buffer_size) {
				/* set download_size to 0 because this is an error */
				download_size = 0;
				sprintf(response, "FAILdata too large");
			} else {
				/* The default case, the transfer fits
				   completely in the interface buffer */
				sprintf(response, "DATA%08x", download_size);
			}
			ret = 0;
			goto send_tx_status;
		}

		/* flash
		   Flash what was downloaded */
		if (memcmp(cmdbuf, "flash:", 6) == 0) {
			struct partition_info pinfo;

			ret = 0;
			if (download_bytes == 0) {
				sprintf(response, "FAILno image downloaded");
				goto send_tx_status;
			}
			if (odroid_get_partition_info(&cmdbuf[6], &pinfo)) {
				sprintf(response, "FAILunknown(%s) partition!",
					cmdbuf + 6);
				goto send_tx_status;
			}
			if (pinfo.size < download_bytes) {
				sprintf(response, "FAILimage too large for partition(%s)!",
					cmdbuf + 6);
				goto send_tx_status;
			}
			if (flashing_data(&pinfo,
				(unsigned int)interface.transfer_buffer,
				download_bytes, 0)) {
					sprintf(response, "FAILfailed to flash %s partition!",
					cmdbuf + 6);
				goto send_tx_status;
			}
			sprintf(response, "OKAY");
			goto send_tx_status;
		}
		/* verify */
		/* continue */
		/* powerdown */
send_tx_status:
		fastboot_tx_status(response, strlen(response), FASTBOOT_TX_ASYNC);
	} /* End of command */

	return ret;
}

/*---------------------------------------------------------------------------*/
static int fastboot_cmd_flash(int argc, char *const argv[])
{
	struct partition_info pinfo;
	unsigned int addr, dev_no = 0;

	if (odroid_get_partition_info(argv[2], &pinfo))
		return	1;

	addr = simple_strtoul(argv[3], NULL, 16);
	if (argc == 5)
		dev_no = simple_strtoul(argv[4], NULL, 16);

	if ((dev_no != 0) && (dev_no != 1))
		dev_no = 0;

	flashing_data(&pinfo, addr, pinfo.size, dev_no);

	if (dev_no)
		run_command("mmc dev 0", 0);

	return	0;
}

/*---------------------------------------------------------------------------*/
static int fastboot_cmd_parsing(int argc, char *const argv[], int *retval)
{
	int	ret = 0;
	*retval = CMD_RET_FAILURE;

	switch(argc) {
	case	2:
		if (!strncmp(argv[1], "poweroff", sizeof("poweroff"))) {
			odroid_power_off();
			while(1);
		}
		*retval = CMD_RET_USAGE;
		break;
	case	5:	case	6:
		if (argv[4] != NULL)
			ret = odroid_partition_setup(argv[4]);
		else
			ret = odroid_partition_setup("0");

		if (!ret)
			ret = fastboot_cmd_flash(argc, argv);

		*retval = ret ? CMD_RET_FAILURE:CMD_RET_SUCCESS;
		break;
	default:
		*retval = CMD_RET_USAGE;
		break;
	/* normal fastboot */
	case	1:
		ret = odroid_partition_setup("0");
		*retval = ret ? CMD_RET_FAILURE:CMD_RET_SUCCESS;
		return	*retval;
	}
	return	1;
}

/*---------------------------------------------------------------------------*/
static int do_fastboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
{
	int continue_from_disconnect = 0, poll_status, retval;
	int led_status = 0, led_blink_cnt = 0;
	int ret = 1;

	/* fastboot command parsing */
	if (fastboot_cmd_parsing(argc, argv, &retval))
		return retval;
	do
	{
		continue_from_disconnect = 0;

		/* Initialize the board specific support */
		if (0 == fastboot_init(&interface))
		{
			/* If we got this far, we are a success */
			ret = 0;

			while (1)
			{
				/* Green LED Blink */
				if (led_blink_cnt++ > 0x8000) {
					if (led_status) {
						odroid_led_ctrl	(GPIO_LED_G, led_status);
						led_status = 0;
					} else {
						odroid_led_ctrl	(GPIO_LED_G, led_status);
						led_status = 1;
					}
					led_blink_cnt = 0;
				}

				poll_status = fastboot_poll();

				/* Check if the user wanted to terminate with ^C */
				if ((FASTBOOT_OK != poll_status) && ctrlc()) {
					printf("Fastboot ended by user\n");
					continue_from_disconnect = 0;
					break;
				}

				if (FASTBOOT_ERROR == poll_status) {
					/* Error */
					printf("Fastboot error \n");
					break;
				}
				else if (FASTBOOT_DISCONNECT == poll_status) {
					/* break, cleanup and re-init */
					printf("Fastboot disconnect detected\n");
					continue_from_disconnect = 1;
					break;
				}
			} /* while (1) */
		}
		/* Reset the board specific support */
		fastboot_shutdown();
		/* restart the loop if a disconnect was detected */
	} while (continue_from_disconnect);

	odroid_led_ctrl	(GPIO_LED_G, 0);
	return	ret;
}

U_BOOT_CMD(
	fastboot, 5, 1, do_fastboot,
	"use USB Fastboot protocol",
	"[ poweroff | flash [kernel|system|userdata|cache] [addr] [dev no] ]\n"
	"[] - options for self update"
);

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/