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 
board_get_alt_info(struct mtd_info * mtd,char * buf)14 static 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 
set_dfu_alt_info(char * interface,char * devstr)41 void 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 (env_get("dfu_alt_info"))
48 		return;
49 
50 	memset(buf, 0, sizeof(buf));
51 
52 	/*
53 	 * Currently dfu_alt_info is needed on Qemu ARM64 for
54 	 * capsule updates
55 	*/
56 	if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT) &&
57 	    IS_ENABLED(CONFIG_TARGET_QEMU_ARM_64BIT)) {
58 		/* probe all MTD devices */
59 		mtd_probe_devices();
60 
61 		mtd = get_mtd_device_nm("nor0");
62 		if (!IS_ERR_OR_NULL(mtd))
63 			board_get_alt_info(mtd, buf);
64 	}
65 
66 	env_set("dfu_alt_info", buf);
67 	printf("dfu_alt_info set\n");
68 }
69