1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2020 Linaro Limited
4 */
5
6#include <common.h>
7#include <dfu.h>
8#include <env.h>
9#include <memalign.h>
10#include <mtd.h>
11
12#define DFU_ALT_BUF_LEN		SZ_1K
13
14static void board_get_alt_info(struct mtd_info *mtd, char *buf)
15{
16	struct mtd_info *part;
17	bool first = true;
18	const char *name;
19	int len, partnum = 0;
20
21	name = mtd->name;
22	len = strlen(buf);
23
24	if (buf[0] != '\0')
25		len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, "&");
26	len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
27			"mtd %s=", name);
28
29	list_for_each_entry(part, &mtd->partitions, node) {
30		partnum++;
31		if (!first)
32			len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, ";");
33		first = false;
34
35		len += snprintf(buf + len, DFU_ALT_BUF_LEN - len,
36				"%s part %d",
37				part->name, partnum);
38	}
39}
40
41void set_dfu_alt_info(char *interface, char *devstr)
42{
43	struct mtd_info *mtd;
44
45	ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
46
47	if (!IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) &&
48	    env_get("dfu_alt_info"))
49		return;
50
51	memset(buf, 0, DFU_ALT_BUF_LEN);
52
53	/*
54	 * Currently dfu_alt_info is needed on Qemu ARM64 for
55	 * capsule updates
56	*/
57	if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT) &&
58	    IS_ENABLED(CONFIG_TARGET_QEMU_ARM_64BIT)) {
59		/* probe all MTD devices */
60		mtd_probe_devices();
61
62		mtd = get_mtd_device_nm("nor0");
63		if (!IS_ERR_OR_NULL(mtd))
64			board_get_alt_info(mtd, buf);
65	}
66
67	env_set("dfu_alt_info", buf);
68	printf("dfu_alt_info set\n");
69}
70