summaryrefslogtreecommitdiff
path: root/debug_qmrom.c
blob: 70ab8523d80bee5a385f9f172eb0ccde5ee7446c (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
// SPDX-License-Identifier: GPL-2.0
/*
 * This file is part of the QM35 UCI stack for linux.
 *
 * Copyright (c) 2022 Qorvo US, Inc.
 *
 * This software is provided under the GNU General Public License, version 2
 * (GPLv2), as well as under a Qorvo commercial license.
 *
 * You may choose to use this software under the terms of the GPLv2 License,
 * version 2 ("GPLv2"), as published by the Free Software Foundation.
 * You should have received a copy of the GPLv2 along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * This program is distributed under the GPLv2 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 GPLv2 for more
 * details.
 *
 * If you cannot meet the requirements of the GPLv2, you may not use this
 * software for any purpose without first obtaining a commercial license from
 * Qorvo.
 * Please contact Qorvo to inquire about licensing terms.
 *
 * QM35 LOG layer HSSPI Protocol
 */

#include <linux/interrupt.h>
#include <linux/version.h>
#include <linux/printk.h>
#include <linux/debugfs.h>
#include <linux/fs.h>
#include <linux/fsnotify.h>
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0))
#include <linux/kernel_read_file.h>
#endif
#include <linux/poll.h>
#include <linux/vmalloc.h>

#include <qmrom.h>
#include <qmrom_log.h>
#include <qmrom_spi.h>
#include <qmrom_utils.h>

#include <fwupdater.h>

#include "qm35.h"
#include "debug.h"

extern int fu_spi_speed_hz;
extern int qmrom_spi_speed_hz;

#define QMROM_RETRIES 10

static void *priv_from_file(const struct file *filp)
{
	return filp->f_path.dentry->d_inode->i_private;
}

static struct qm35_ctx *rom_test_prepare(struct file *filp)
{
	struct debug *debug = priv_from_file(filp);
	struct qm35_ctx *qm35_hdl = container_of(debug, struct qm35_ctx, debug);

	qm35_hsspi_stop(qm35_hdl);
	qmrom_set_log_device(&qm35_hdl->spi->dev, LOG_DBG);

	enable_irq(qm35_hdl->ss_rdy_irq);

	qm35_hdl->flashing = true;
	return qm35_hdl;
}

static void rom_test_unprepare(struct qm35_ctx *qm35_hdl)
{
	disable_irq_nosync(qm35_hdl->ss_rdy_irq);

	qm35_hdl->flashing = false;
	qmrom_set_log_device(&qm35_hdl->spi->dev, LOG_WARN);
	qm35_hsspi_start(qm35_hdl);
}

static struct firmware *file2firmware(const char *filename)
{
	struct firmware *firmware =
		kmalloc(sizeof(struct firmware), GFP_KERNEL | __GFP_ZERO);
	if (!firmware) {
		goto fail;
	}

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0))
	{
		ssize_t bytes_read = kernel_read_file_from_path(
			filename, 0, (void **)&firmware->data, INT_MAX,
			&firmware->size, READING_FIRMWARE);
		if (bytes_read < 0) {
			pr_err("qm35: kernel_read_file_from_path(%s) returned %d\n",
			       filename, (int)bytes_read);
			goto fail;
		}
		if (bytes_read != firmware->size) {
			pr_err("qm35: kernel_read_file_from_path returned %zu; expected %zu\n",
			       bytes_read, firmware->size);
			goto fail;
		}
	}
#else
	{
		loff_t size = 0;
		int ret = kernel_read_file_from_path(filename,
						     (void **)&firmware->data,
						     &size, INT_MAX,
						     READING_FIRMWARE);
		if (ret < 0) {
			pr_err("qm35: kernel_read_file_from_path(%s) returned %d\n",
			       filename, ret);
			goto fail;
		}
		firmware->size = (size_t)size;
	}
#endif

	print_hex_dump(KERN_DEBUG, "qm35: Bin file:", DUMP_PREFIX_ADDRESS, 16,
		       1, firmware->data, 16, false);
	return firmware;

fail:
	if (firmware) {
		if (firmware->data)
			vfree(firmware->data);
		kfree(firmware);
	}
	return NULL;
}

static ssize_t rom_probe(struct file *filp, const char __user *buff,
			 size_t count, loff_t *off)
{
	struct qm35_ctx *qm35_hdl = rom_test_prepare(filp);
	struct qmrom_handle *h;

	pr_info("qm35: Starting the probe test...\n");
	h = qmrom_init(&qm35_hdl->spi->dev, qm35_hdl, qm35_hdl,
		       qm35_hdl->gpio_ss_irq, qmrom_spi_speed_hz, QMROM_RETRIES,
		       qmrom_spi_reset_device, DEVICE_GEN_QM357XX);
	if (!h) {
		pr_err("qm35: qmrom_init failed\n");
		goto end;
	}

	pr_info("qm35: chip_revision %#2x\n", h->chip_rev);
	pr_info("qm35: device version %#02x\n", h->device_version);
	if (h->chip_rev != 0xa001) {
		pr_info("qm35: lcs_state %u\n", h->qm357xx_soc_info.lcs_state);
		print_hex_dump(KERN_DEBUG, "qm35: soc_id:", DUMP_PREFIX_NONE,
			       16, 1, h->qm357xx_soc_info.soc_id,
			       sizeof(h->qm357xx_soc_info.soc_id), false);
		print_hex_dump(KERN_DEBUG, "qm35: uuid:", DUMP_PREFIX_NONE, 16,
			       1, h->qm357xx_soc_info.uuid,
			       sizeof(h->qm357xx_soc_info.uuid), false);
	}
	qmrom_deinit(h);
	qmrom_spi_reset_device(qm35_hdl);
	qmrom_msleep(100);

end:
	rom_test_unprepare(qm35_hdl);
	return count;
}

static ssize_t rom_flash_dbg_cert(struct file *filp, const char __user *buff,
				  size_t count, loff_t *off)
{
	struct qm35_ctx *qm35_hdl = rom_test_prepare(filp);
	struct firmware *certificate = NULL;
	struct qmrom_handle *h = NULL;
	char *filename;
	int err;

	filename = kmalloc(count, GFP_KERNEL);
	if (!filename) {
		count = -ENOMEM;
		goto end;
	}

	err = copy_from_user(filename, buff, count);
	if (err) {
		pr_err("qm35: copy_from_user failed with error %d\n", err);
		count = err;
		goto end;
	}
	filename[count - 1] = '\0';
	certificate = file2firmware(filename);
	if (!certificate || certificate->size != DEBUG_CERTIFICATE_SIZE) {
		pr_err("qm35: %s: file retrieval failed, abort (%s)\n",
		       __func__, certificate ? "wrong size" : "not found");
		count = -1;
		goto end;
	}

	/* Flash the debug certificate */
	pr_info("Flashing debug certificate %s...\n", filename);

	h = qmrom_init(&qm35_hdl->spi->dev, qm35_hdl, qm35_hdl,
		       qm35_hdl->gpio_ss_irq, qmrom_spi_speed_hz, QMROM_RETRIES,
		       qmrom_spi_reset_device, DEVICE_GEN_QM357XX);
	if (!h) {
		pr_err("qm35: qmrom_init failed\n");
		goto end;
	}
	err = qm357xx_rom_flash_dbg_cert(h, certificate);
	if (err)
		pr_err("qm35: Flashing debug certificate %s failed with %d!\n",
		       filename, err);
	else
		pr_info("qm35: Flashing debug certificate %s succeeded!\n",
			filename);

end:
	if (filename)
		kfree(filename);
	if (h)
		qmrom_deinit(h);
	if (certificate) {
		if (certificate->data)
			vfree(certificate->data);
		kfree(certificate);
	}
	qmrom_spi_reset_device(qm35_hdl);
	qmrom_msleep(100);

	rom_test_unprepare(qm35_hdl);
	return count;
}

static ssize_t rom_erase_dbg_cert(struct file *filp, const char __user *buff,
				  size_t count, loff_t *off)
{
	struct qm35_ctx *qm35_hdl = rom_test_prepare(filp);
	struct qmrom_handle *h = NULL;
	int err;

	pr_info("qm35: Erasing debug certificate...\n");

	h = qmrom_init(&qm35_hdl->spi->dev, qm35_hdl, qm35_hdl,
		       qm35_hdl->gpio_ss_irq, qmrom_spi_speed_hz, QMROM_RETRIES,
		       qmrom_spi_reset_device, DEVICE_GEN_QM357XX);
	if (!h) {
		pr_err("qm35: qmrom_init failed\n");
		goto end;
	}
	err = qm357xx_rom_erase_dbg_cert(h);
	if (err)
		pr_err("qm35: Erasing debug certificate failed with %d!\n",
		       err);
	else
		pr_info("qm35: Erasing debug certificate succeeded!\n");

end:
	if (h)
		qmrom_deinit(h);

	qmrom_spi_reset_device(qm35_hdl);
	qmrom_msleep(100);

	rom_test_unprepare(qm35_hdl);
	return count;
}

static ssize_t rom_flash_fw(struct file *filp, const char __user *buff,
			    size_t count, loff_t *off)
{
	struct qm35_ctx *qm35_hdl = rom_test_prepare(filp);
	struct firmware *fw = NULL;
	struct qmrom_handle *h = NULL;
	char *filename;
	int rc;

	filename = kmalloc(count, GFP_KERNEL);
	if (!filename) {
		count = -ENOMEM;
		goto end;
	}

	rc = copy_from_user(filename, buff, count);
	if (rc) {
		pr_err("qm35: copy_from_user failed with error %d\n", rc);
		goto end;
	}
	filename[count - 1] = '\0';
	fw = file2firmware(filename);
	if (!fw) {
		pr_err("qm35: %s: file %s retrieval failed, abort\n", __func__,
		       filename);
		goto end;
	}

	pr_info("qm35: Flashing image %s (%pK->data %pK)...\n", filename, fw,
		fw->data);

	h = qmrom_init(&qm35_hdl->spi->dev, qm35_hdl, qm35_hdl,
		       qm35_hdl->gpio_ss_irq, qmrom_spi_speed_hz, QMROM_RETRIES,
		       qmrom_spi_reset_device, DEVICE_GEN_QM357XX);
	if (!h) {
		pr_err("qm35: qmrom_init failed\n");
		goto end;
	}
	rc = qm357xx_rom_flash_fw(h, fw);
	if (rc)
		pr_err("qm35: Flashing firmware %s failed with %d!\n", filename,
		       rc);
	else
		pr_info("qm35: Flashing firmware %s succeeded!\n", filename);

end:
	kfree(filename);
	if (h)
		qmrom_deinit(h);
	if (fw) {
		if (fw->data)
			vfree(fw->data);
		kfree(fw);
	}
	rom_test_unprepare(qm35_hdl);
	return count;
}

static ssize_t rom_flash_fw_pkg(struct file *filp, const char __user *buff,
				size_t count, loff_t *off)
{
	struct qm35_ctx *qm35_hdl = rom_test_prepare(filp);
	const struct firmware *fw = NULL;
	struct qmrom_handle *h = NULL;
	char *filename;
	const uint8_t *fw_data;
	uint32_t fw_size;
	int rc;

	if (fu_spi_speed_hz == 0)
		fu_spi_speed_hz = FWUPDATER_SPI_SPEED_HZ;

	filename = kmalloc(count, GFP_KERNEL);
	if (!filename) {
		count = -ENOMEM;
		goto end;
	}
	rc = copy_from_user(filename, buff, count);
	if (rc) {
		pr_err("qm35: copy_from_user failed with error %d\n", rc);
		goto end;
	}
	filename[count - 1] = '\0';
	fw = file2firmware(filename);
	if (!fw || !fw->data) {
		pr_err("qm35: %s file %s retrieval failed (%s), abort\n",
		       __func__, filename, fw ? "no data read" : "not found");
		goto end;
	}

	pr_info("qm35: Flashing fw_updater...\n");
	h = qmrom_init(&qm35_hdl->spi->dev, qm35_hdl, qm35_hdl,
		       qm35_hdl->gpio_ss_irq, qmrom_spi_speed_hz, QMROM_RETRIES,
		       qmrom_spi_reset_device, DEVICE_GEN_QM357XX);
	if (!h) {
		pr_err("qm35: qmrom_init failed\n");
		goto end;
	}
	h->skip_check_fw_boot = true;
	rc = qm357xx_rom_flash_fw(h, fw);
	h->skip_check_fw_boot = false;
	if (rc) {
		pr_err("qm35: Flashing fw_updater failed with %d!\n", rc);
		goto end;
	}

	pr_info("qm35: Flashing fw_updater succeeded, flashing the fw package now...\n");
	qmrom_spi_set_freq(fu_spi_speed_hz);

	rc = qm357xx_rom_fw_macro_pkg_get_fw_idx(fw, 1, &fw_size, &fw_data);
	if (rc) {
		pr_err("qm35: %s FW MACRO PACKAGE corrupted = %d\n", __func__,
		       rc);
		goto end;
	}

	if (*(uint32_t *)fw_data == CRYPTO_FIRMWARE_PACK_MAGIC_VALUE) {
		rc = run_fwupdater(h, fw_data, fw_size);
	} else {
		pr_err("qm35: FW PACKAGE not found - %04x! fw_size = %d\n",
		       *(uint32_t *)fw_data, fw_size);
		goto end;
	}
	pr_info("qm35: FW package flashing %s (rc = %d), rebooting the QM...\n",
		rc ? "failed" : "succeeded", rc);

end:
	qmrom_spi_reset_device(qm35_hdl);

	if (filename)
		kfree(filename);
	if (h)
		qmrom_deinit(h);
	if (fw) {
		if (fw->data)
			vfree(fw->data);
		kfree(fw);
	}
	rom_test_unprepare(qm35_hdl);
	return count;
}

static const struct file_operations rom_probe_fops = { .owner = THIS_MODULE,
						       .write = rom_probe };

static const struct file_operations rom_flash_dbg_cert_fops = {
	.owner = THIS_MODULE,
	.write = rom_flash_dbg_cert
};

static const struct file_operations rom_erase_dbg_cert_fops = {
	.owner = THIS_MODULE,
	.write = rom_erase_dbg_cert
};

static const struct file_operations rom_flash_fw_fops = {
	.owner = THIS_MODULE,
	.write = rom_flash_fw
};

static const struct file_operations rom_flash_fw_pkg_fops = {
	.owner = THIS_MODULE,
	.write = rom_flash_fw_pkg
};

void debug_rom_code_init(struct debug *debug)
{
	struct dentry *file;
	file = debugfs_create_file("rom_probe", 0200, debug->fw_dir, debug,
				   &rom_probe_fops);
	if (!file) {
		pr_err("qm35: failed to create uwb0/fw/rom_probe\n");
		return;
	}

	file = debugfs_create_file("rom_flash_dbg_cert", 0200, debug->fw_dir,
				   debug, &rom_flash_dbg_cert_fops);
	if (!file) {
		pr_err("qm35: failed to create uwb0/fw/rom_flash_dbg_cert\n");
		return;
	}

	file = debugfs_create_file("rom_erase_dbg_cert", 0200, debug->fw_dir,
				   debug, &rom_erase_dbg_cert_fops);
	if (!file) {
		pr_err("qm35: failed to create uwb0/fw/rom_erase_dbg_cert\n");
		return;
	}

	file = debugfs_create_file("rom_flash_fw", 0200, debug->fw_dir, debug,
				   &rom_flash_fw_fops);
	if (!file) {
		pr_err("qm35: failed to create uwb0/fw/rom_flash_fw\n");
		return;
	}

	file = debugfs_create_file("rom_flash_fw_pkg", 0200, debug->fw_dir,
				   debug, &rom_flash_fw_pkg_fops);
	if (!file) {
		pr_err("qm35: failed to create uwb0/fw/rom_flash_fw_pkg\n");
		return;
	}
}