xref: /OK3568_Linux_fs/u-boot/arch/arm/mach-rockchip/dfu_alt_info.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * (C) Copyright 2021 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <boot_rkimg.h>
9 #include <memalign.h>
10 #include <android_avb/rk_avb_ops_user.h>
11 
12 #define CONFIG_SET_DFU_ALT_BUF_LEN	(SZ_1K)
get_dfu_alt(char * interface,char * devstr)13 static char *get_dfu_alt(char *interface, char *devstr)
14 {
15 	struct blk_desc *dev_desc;
16 	char *alt_boot;
17 #ifdef CONFIG_ANDROID_AB
18 	char current_slot[3] = {0};
19 #endif
20 
21 	dev_desc = rockchip_get_bootdev();
22 	if (!dev_desc) {
23 		printf("%s: dev_desc is NULL!\n", __func__);
24 		return NULL;
25 	}
26 
27 	switch (dev_desc->if_type) {
28 #ifdef CONFIG_DFU_MMC
29 	case IF_TYPE_MMC:
30 		alt_boot = DFU_ALT_BOOT_EMMC;
31 		break;
32 #endif
33 #ifdef CONFIG_DFU_MTD
34 	case IF_TYPE_MTD:
35 #ifdef CONFIG_ANDROID_AB
36 		rk_avb_get_current_slot(current_slot);
37 
38 		if (!strcmp(current_slot, "_a")) {
39 			alt_boot = DFU_ALT_BOOT_MTD_B;
40 			printf("\ncurrent boot from slot A!\n");
41 		} else if (!strcmp(current_slot, "_b")) {
42 			alt_boot = DFU_ALT_BOOT_MTD_A;
43 			printf("\ncurrent boot from slot B!\n");
44 		} else {
45 			return NULL;
46 		}
47 #else
48 		alt_boot = DFU_ALT_BOOT_MTD;
49 #endif
50 		break;
51 #endif /* CONFIG_DFU_MTD */
52 	default:
53 		printf("[dfu ERROR]:Boot device type is invalid!\n");
54 		return NULL;
55 	}
56 
57 	return alt_boot;
58 }
59 
set_dfu_alt_info(char * interface,char * devstr)60 void set_dfu_alt_info(char *interface, char *devstr)
61 {
62 	size_t buf_size = CONFIG_SET_DFU_ALT_BUF_LEN;
63 	ALLOC_CACHE_ALIGN_BUFFER(char, buf, buf_size);
64 	char *alt_info = "Settings not found!";
65 	char *status = "error!\n";
66 	char *alt_setting;
67 	int offset = 0;
68 
69 	puts("DFU alt info setting: ");
70 
71 	alt_setting = get_dfu_alt(interface, devstr);
72 	if (alt_setting) {
73 		env_set("dfu_alt_boot", alt_setting);
74 		offset = snprintf(buf, buf_size, "%s", alt_setting);
75 	}
76 
77 	if (offset) {
78 		alt_info = buf;
79 		status = "done\n";
80 	}
81 
82 	env_set("dfu_alt_info", alt_info);
83 	puts(status);
84 }
85