summaryrefslogtreecommitdiffstats
path: root/drivers/staging/gdm72xx/usb_boot.c
blob: fef290c38db67ae8620a8ff92179275fb564730b (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
/*
 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 */

#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/usb.h>
#include <linux/unistd.h>
#include <linux/slab.h>

#include <asm/byteorder.h>
#include "gdm_usb.h"
#include "usb_boot.h"

#define DN_KERNEL_MAGIC_NUMBER		0x10760001
#define DN_ROOTFS_MAGIC_NUMBER		0x10760002

#define DOWNLOAD_SIZE	1024

#define DH2B(x)		__cpu_to_be32(x)
#define DL2H(x)		__le32_to_cpu(x)

#define MIN(a, b)	((a) > (b) ? (b) : (a))

#define MAX_IMG_CNT		16
#define UIMG_PATH		"/lib/firmware/gdm72xx/gdmuimg.bin"
#define KERN_PATH		"/lib/firmware/gdm72xx/zImage"
#define FS_PATH			"/lib/firmware/gdm72xx/ramdisk.jffs2"

struct dn_header {
	u32	magic_num;
	u32	file_size;
};

struct img_header {
	u32		magic_code;
	u32		count;
	u32		len;
	u32		offset[MAX_IMG_CNT];
	char	hostname[32];
	char	date[32];
};

struct fw_info {
	u32		id;
	u32		len;
	u32		kernel_len;
	u32		rootfs_len;
	u32		kernel_offset;
	u32		rootfs_offset;
	u32		fw_ver;
	u32		mac_ver;
	char	hostname[32];
	char	userid[16];
	char	date[32];
	char	user_desc[128];
};

static void array_le32_to_cpu(u32 *arr, int num)
{
	int i;
	for (i = 0; i < num; i++, arr++)
		*arr = DL2H(*arr);
}

static u8 *tx_buf;

static int gdm_wibro_send(struct usb_device *usbdev, void *data, int len)
{
	int ret;
	int actual;

	ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 1), data, len,
			&actual, 1000);

	if (ret < 0) {
		printk(KERN_ERR "Error : usb_bulk_msg ( result = %d )\n", ret);
		return ret;
	}
	return 0;
}

static int gdm_wibro_recv(struct usb_device *usbdev, void *data, int len)
{
	int ret;
	int actual;

	ret = usb_bulk_msg(usbdev, usb_rcvbulkpipe(usbdev, 2), data, len,
			&actual, 5000);

	if (ret < 0) {
		printk(KERN_ERR "Error : usb_bulk_msg(recv) ( result = %d )\n",
			ret);
		return ret;
	}
	return 0;
}

static int download_image(struct usb_device *usbdev, struct file *filp,
				loff_t *pos, u32 img_len, u32 magic_num)
{
	struct dn_header h;
	int ret = 0;
	u32 size;
	int len, readn;

	size = (img_len + DOWNLOAD_SIZE - 1) & ~(DOWNLOAD_SIZE - 1);
	h.magic_num = DH2B(magic_num);
	h.file_size = DH2B(size);

	ret = gdm_wibro_send(usbdev, &h, sizeof(h));
	if (ret < 0)
		goto out;

	readn = 0;
	while ((len = filp->f_op->read(filp, tx_buf, DOWNLOAD_SIZE, pos))) {

		if (len < 0) {
			ret = -1;
			goto out;
		}
		readn += len;

		ret = gdm_wibro_send(usbdev, tx_buf, DOWNLOAD_SIZE);
		if (ret < 0)
			goto out;
		if (readn >= img_len)
			break;
	}

	if (readn < img_len) {
		printk(KERN_ERR "gdmwm: Cannot read to the requested size. "
			"Read = %d Requested = %d\n", readn, img_len);
		ret = -EIO;
	}
out:

	return ret;
}

int usb_boot(struct usb_device *usbdev, u16 pid)
{
	int i, ret = 0;
	struct file *filp = NULL;
	struct inode *inode = NULL;
	static mm_segment_t fs;
	struct img_header hdr;
	struct fw_info fw_info;
	loff_t pos = 0;
	char *img_name = UIMG_PATH;
	int len;

	tx_buf = kmalloc(DOWNLOAD_SIZE, GFP_KERNEL);
	if (tx_buf == NULL) {
		printk(KERN_ERR "Error: kmalloc\n");
		return -ENOMEM;
	}

	fs = get_fs();
	set_fs(get_ds());

	filp = filp_open(img_name, O_RDONLY | O_LARGEFILE, 0);
	if (IS_ERR(filp)) {
		printk(KERN_ERR "Can't find %s.\n", img_name);
		set_fs(fs);
		ret = PTR_ERR(filp);
		goto restore_fs;
	}

	if (filp->f_dentry)
		inode = filp->f_dentry->d_inode;
	if (!inode || !S_ISREG(inode->i_mode)) {
		printk(KERN_ERR "Invalid file type: %s\n", img_name);
		ret = -EINVAL;
		goto out;
	}

	len = filp->f_op->read(filp, (u8 *)&hdr, sizeof(hdr), &pos);
	if (len != sizeof(hdr)) {
		printk(KERN_ERR "gdmwm: Cannot read the image info.\n");
		ret = -EIO;
		goto out;
	}

	array_le32_to_cpu((u32 *)&hdr, 19);
#if 0
	if (hdr.magic_code != 0x10767fff) {
		printk(KERN_ERR "gdmwm: Invalid magic code 0x%08x\n",
			hdr.magic_code);
		ret = -EINVAL;
		goto out;
	}
#endif
	if (hdr.count > MAX_IMG_CNT) {
		printk(KERN_ERR "gdmwm: Too many images. %d\n", hdr.count);
		ret = -EINVAL;
		goto out;
	}

	for (i = 0; i < hdr.count; i++) {
		if (hdr.offset[i] > hdr.len) {
			printk(KERN_ERR "gdmwm: Invalid offset. "
				"Entry = %d Offset = 0x%08x "
				"Image length = 0x%08x\n",
				i, hdr.offset[i], hdr.len);
			ret = -EINVAL;
			goto out;
		}

		pos = hdr.offset[i];
		len = filp->f_op->read(filp, (u8 *)&fw_info, sizeof(fw_info),
					&pos);
		if (len != sizeof(fw_info)) {
			printk(KERN_ERR "gdmwm: Cannot read the FW info.\n");
			ret = -EIO;
			goto out;
		}

		array_le32_to_cpu((u32 *)&fw_info, 8);
#if 0
		if ((fw_info.id & 0xfffff000) != 0x10767000) {
			printk(KERN_ERR "gdmwm: Invalid FW id. 0x%08x\n",
				fw_info.id);
			ret = -EIO;
			goto out;
		}
#endif

		if ((fw_info.id & 0xffff) != pid)
			continue;

		pos = hdr.offset[i] + fw_info.kernel_offset;
		ret = download_image(usbdev, filp, &pos, fw_info.kernel_len,
				DN_KERNEL_MAGIC_NUMBER);
		if (ret < 0)
			goto out;
		printk(KERN_INFO "GCT: Kernel download success.\n");

		pos = hdr.offset[i] + fw_info.rootfs_offset;
		ret = download_image(usbdev, filp, &pos, fw_info.rootfs_len,
				DN_ROOTFS_MAGIC_NUMBER);
		if (ret < 0)
			goto out;
		printk(KERN_INFO "GCT: Filesystem download success.\n");

		break;
	}

	if (i == hdr.count) {
		printk(KERN_ERR "Firmware for gsk%x is not installed.\n", pid);
		ret = -EINVAL;
	}
out:
	filp_close(filp, current->files);

restore_fs:
	set_fs(fs);
	kfree(tx_buf);
	return ret;
}

/*#define GDM7205_PADDING		256 */
#define DOWNLOAD_CHUCK			2048
#define KERNEL_TYPE_STRING		"linux"
#define FS_TYPE_STRING			"rootfs"

static int em_wait_ack(struct usb_device *usbdev, int send_zlp)
{
	int ack;
	int ret = -1;

	if (send_zlp) {
		/*Send ZLP*/
		ret = gdm_wibro_send(usbdev, NULL, 0);
		if (ret < 0)
			goto out;
	}

	/*Wait for ACK*/
	ret = gdm_wibro_recv(usbdev, &ack, sizeof(ack));
	if (ret < 0)
		goto out;
out:
	return ret;
}

static int em_download_image(struct usb_device *usbdev, char *path,
				char *type_string)
{
	struct file *filp;
	struct inode *inode;
	static mm_segment_t fs;
	char *buf = NULL;
	loff_t pos = 0;
	int ret = 0;
	int len, readn = 0;
	#if defined(GDM7205_PADDING)
	const int pad_size = GDM7205_PADDING;
	#else
	const int pad_size = 0;
	#endif

	fs = get_fs();
	set_fs(get_ds());

	filp = filp_open(path, O_RDONLY | O_LARGEFILE, 0);
	if (IS_ERR(filp)) {
		printk(KERN_ERR "Can't find %s.\n", path);
		set_fs(fs);
		ret = -ENOENT;
		goto restore_fs;
	}

	if (filp->f_dentry) {
		inode = filp->f_dentry->d_inode;
		if (!inode || !S_ISREG(inode->i_mode)) {
			printk(KERN_ERR "Invalid file type: %s\n", path);
			ret = -EINVAL;
			goto out;
		}
	}

	buf = kmalloc(DOWNLOAD_CHUCK + pad_size, GFP_KERNEL);
	if (buf == NULL) {
		printk(KERN_ERR "Error: kmalloc\n");
		return -ENOMEM;
	}

	strcpy(buf+pad_size, type_string);
	ret = gdm_wibro_send(usbdev, buf, strlen(type_string)+pad_size);
	if (ret < 0)
		goto out;

	while ((len = filp->f_op->read(filp, buf+pad_size, DOWNLOAD_CHUCK,
					&pos))) {
		if (len < 0) {
			ret = -1;
			goto out;
		}
		readn += len;

		ret = gdm_wibro_send(usbdev, buf, len+pad_size);
		if (ret < 0)
			goto out;

		ret = em_wait_ack(usbdev, ((len+pad_size) % 512 == 0));
		if (ret < 0)
			goto out;
	}

	ret = em_wait_ack(usbdev, 1);
	if (ret < 0)
		goto out;

out:
	filp_close(filp, current->files);

restore_fs:
	set_fs(fs);

	kfree(buf);

	return ret;
}

static int em_fw_reset(struct usb_device *usbdev)
{
	int ret;

	/*Send ZLP*/
	ret = gdm_wibro_send(usbdev, NULL, 0);
	return ret;
}

int usb_emergency(struct usb_device *usbdev)
{
	int ret;

	ret = em_download_image(usbdev, KERN_PATH, KERNEL_TYPE_STRING);
	if (ret < 0)
		goto out;
	printk(KERN_INFO "GCT Emergency: Kernel download success.\n");

	ret = em_download_image(usbdev, FS_PATH, FS_TYPE_STRING);
	if (ret < 0)
		goto out;
	printk(KERN_INFO "GCT Emergency: Filesystem download success.\n");

	ret = em_fw_reset(usbdev);
out:
	return ret;
}