summaryrefslogtreecommitdiff
path: root/cmd/thordown.c
blob: c1dc115db6363ddbbb4ff7238564eeb1d1a711df (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * cmd_thordown.c -- USB TIZEN "THOR" Downloader gadget
 *
 * Copyright (C) 2013 Lukasz Majewski <l.majewski@samsung.com>
 * All rights reserved.
 */

#include <common.h>
#include <command.h>
#include <thor.h>
#include <dfu.h>
#include <g_dnl.h>
#include <usb.h>
#include <libtizen.h>

int do_thor_down(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
{
	char *usb_controller;
	char *interface;
	char *devstring;
	int ret;

	puts("TIZEN \"THOR\" Downloader\n");

	switch (argc) {
	case 1:
		usb_controller = strdup(env_get("dfu_usb_con"));
		interface = strdup(env_get("dfu_interface"));
		devstring = strdup(env_get("dfu_device"));

		if (!usb_controller || !interface || !devstring) {
			puts("DFU: default device environment is not set.\n");
			ret = CMD_RET_USAGE;
			goto bad_args;
		}
		break;
	case 4:
		usb_controller = argv[1];
		interface = argv[2];
		devstring = argv[3];
		break;
	default:
		return CMD_RET_USAGE;
	}

	ret = dfu_init_env_entities(interface, devstring);
	if (ret)
		goto done;

	int controller_index = simple_strtoul(usb_controller, NULL, 0);
	ret = usb_gadget_initialize(controller_index);
	if (ret) {
		pr_err("USB init failed: %d\n", ret);
		ret = CMD_RET_FAILURE;
		goto exit;
	}

	ret = g_dnl_register("usb_dnl_thor");
	if (ret) {
		pr_err("g_dnl_register failed %d\n", ret);
		ret = CMD_RET_FAILURE;
		goto exit;
	}

	ret = thor_init();
	if (ret) {
		if (ret == -EINTR) {
			printk("THOR DOWNLOAD stopped\n");
			ret = CMD_RET_SUCCESS;
		} else {
			printk("THOR DOWNLOAD failed: %d\n", ret);
			ret = CMD_RET_FAILURE;
		}
		goto exit;
	}

	do {
		ret = thor_handle();
		if (ret == THOR_DFU_REINIT_NEEDED) {
			dfu_free_entities();
			ret = dfu_init_env_entities(interface, devstring);
		}
		if (ret) {
			pr_err("THOR failed: %d\n", ret);
			ret = CMD_RET_FAILURE;
			goto exit;
		}
	} while (ret == 0);
exit:
	g_dnl_unregister();
	usb_gadget_release(controller_index);
done:
	dfu_free_entities();

#ifdef CONFIG_TIZEN
#ifdef CONFIG_LCD	/* TODO : Need to enable LCD */
	if (ret != CMD_RET_SUCCESS)
		draw_thor_fail_screen();
	else
		lcd_clear();
#endif
	if (ret != CMD_RET_SUCCESS)
		thor_status_notify(THOR_NOTIFY_DOWNLOAD_FAILED, NULL);
#endif
bad_args:
	if (argc == 1) {
		free(usb_controller);
		free(interface);
		free(devstring);
	}
	return ret;
}

__weak void thor_status_notify(enum thor_notify_type type,
			struct thor_notify_data *data)
{
}

U_BOOT_CMD(thordown, CONFIG_SYS_MAXARGS, 1, do_thor_down,
	   "TIZEN \"THOR\" downloader",
	   "<USB_controller> <interface> <dev>\n"
	   "  - device software upgrade via LTHOR TIZEN download\n"
	   "    program via <USB_controller> on device <dev>,\n"
	   "	attached to interface <interface>\n"
);