xref: /rk3399_rockchip-uboot/common/android_bootloader.c (revision 06f4a8746e68c25013788a9d46f4abca1fd920a3)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #include <android_bootloader.h>
8 #include <android_bootloader_message.h>
9 
10 #include <cli.h>
11 #include <common.h>
12 #include <malloc.h>
13 
14 #define ANDROID_PARTITION_BOOT "boot"
15 #define ANDROID_PARTITION_SYSTEM "system"
16 
17 #define ANDROID_ARG_SLOT_SUFFIX "androidboot.slot_suffix="
18 #define ANDROID_ARG_ROOT "root="
19 
20 static int android_bootloader_message_load(
21 	struct blk_desc *dev_desc,
22 	const disk_partition_t *part_info,
23 	struct android_bootloader_message *message)
24 {
25 	ulong message_blocks = sizeof(struct android_bootloader_message) /
26 	    part_info->blksz;
27 	if (message_blocks > part_info->size) {
28 		printf("misc partition too small.\n");
29 		return -1;
30 	}
31 
32 	if (blk_dread(dev_desc, part_info->start, message_blocks, message) !=
33 	    message_blocks) {
34 		printf("Could not read from misc partition\n");
35 		return -1;
36 	}
37 	debug("ANDROID: Loaded BCB, %lu blocks.\n", message_blocks);
38 	return 0;
39 }
40 
41 static int android_bootloader_message_write(
42 	struct blk_desc *dev_desc,
43 	const disk_partition_t *part_info,
44 	struct android_bootloader_message *message)
45 {
46 	ulong message_blocks = sizeof(struct android_bootloader_message) /
47 	    part_info->blksz;
48 	if (message_blocks > part_info->size) {
49 		printf("misc partition too small.\n");
50 		return -1;
51 	}
52 
53 	if (blk_dwrite(dev_desc, part_info->start, message_blocks, message) !=
54 	    message_blocks) {
55 		printf("Could not write to misc partition\n");
56 		return -1;
57 	}
58 	debug("ANDROID: Wrote new BCB, %lu blocks.\n", message_blocks);
59 	return 0;
60 }
61 
62 static enum android_boot_mode android_bootloader_load_and_clear_mode(
63 	struct blk_desc *dev_desc,
64 	const disk_partition_t *misc_part_info)
65 {
66 	struct android_bootloader_message bcb;
67 
68 #ifdef CONFIG_FASTBOOT
69 	char *bootloader_str;
70 
71 	/* Check for message from bootloader stored in RAM from a previous boot.
72 	 */
73 	bootloader_str = (char *)CONFIG_FASTBOOT_BUF_ADDR;
74 	if (!strcmp("reboot-bootloader", bootloader_str)) {
75 		bootloader_str[0] = '\0';
76 		return ANDROID_BOOT_MODE_BOOTLOADER;
77 	}
78 #endif
79 
80 	/* Check and update the BCB message if needed. */
81 	if (android_bootloader_message_load(dev_desc, misc_part_info, &bcb) <
82 	    0) {
83 		printf("WARNING: Unable to load the BCB.\n");
84 		return ANDROID_BOOT_MODE_NORMAL;
85 	}
86 
87 	if (!strcmp("bootonce-bootloader", bcb.command)) {
88 		/* Erase the message in the BCB since this value should be used
89 		 * only once.
90 		 */
91 		memset(bcb.command, 0, sizeof(bcb.command));
92 		android_bootloader_message_write(dev_desc, misc_part_info,
93 						 &bcb);
94 		return ANDROID_BOOT_MODE_BOOTLOADER;
95 	}
96 
97 	if (!strcmp("boot-recovery", bcb.command))
98 		return ANDROID_BOOT_MODE_RECOVERY;
99 
100 	return ANDROID_BOOT_MODE_NORMAL;
101 }
102 
103 /**
104  * Return the reboot reason string for the passed boot mode.
105  *
106  * @param mode	The Android Boot mode.
107  * @return a pointer to the reboot reason string for mode.
108  */
109 static const char *android_boot_mode_str(enum android_boot_mode mode)
110 {
111 	switch (mode) {
112 	case ANDROID_BOOT_MODE_NORMAL:
113 		return "(none)";
114 	case ANDROID_BOOT_MODE_RECOVERY:
115 		return "recovery";
116 	case ANDROID_BOOT_MODE_BOOTLOADER:
117 		return "bootloader";
118 	}
119 	return NULL;
120 }
121 
122 static int android_part_get_info_by_name_suffix(struct blk_desc *dev_desc,
123 						const char *base_name,
124 						const char *slot_suffix,
125 						disk_partition_t *part_info)
126 {
127 	char *part_name;
128 	int part_num;
129 
130 	part_name = malloc(strlen(base_name) + strlen(slot_suffix) + 1);
131 	if (!part_name)
132 		return -1;
133 	strcpy(part_name, base_name);
134 	strcat(part_name, slot_suffix);
135 
136 	part_num = part_get_info_by_name(dev_desc, part_name, part_info);
137 	if (part_num < 0) {
138 		debug("ANDROID: Could not find partition \"%s\"\n", part_name);
139 		part_num = -1;
140 	}
141 
142 	free(part_name);
143 	return part_num;
144 }
145 
146 static int android_bootloader_boot_bootloader(void)
147 {
148 	const char *fastboot_cmd = env_get("fastbootcmd");
149 
150 	if (fastboot_cmd)
151 		return run_command(fastboot_cmd, CMD_FLAG_ENV);
152 	return -1;
153 }
154 
155 static int android_bootloader_boot_kernel(unsigned long kernel_address)
156 {
157 	char kernel_addr_str[12];
158 	char *fdt_addr = env_get("fdt_addr");
159 	char *bootm_args[] = { "bootm", kernel_addr_str, "-", fdt_addr, NULL };
160 
161 	sprintf(kernel_addr_str, "0x%lx", kernel_address);
162 
163 	printf("Booting kernel at %s with fdt at %s...\n\n\n",
164 	       kernel_addr_str, fdt_addr);
165 	do_bootm(NULL, 0, 4, bootm_args);
166 
167 	return -1;
168 }
169 
170 static char *strjoin(const char **chunks, char separator)
171 {
172 	int len, joined_len = 0;
173 	char *ret, *current;
174 	const char **p;
175 
176 	for (p = chunks; *p; p++)
177 		joined_len += strlen(*p) + 1;
178 
179 	if (!joined_len) {
180 		ret = malloc(1);
181 		if (ret)
182 			ret[0] = '\0';
183 		return ret;
184 	}
185 
186 	ret = malloc(joined_len);
187 	current = ret;
188 	if (!ret)
189 		return ret;
190 
191 	for (p = chunks; *p; p++) {
192 		len = strlen(*p);
193 		memcpy(current, *p, len);
194 		current += len;
195 		*current = separator;
196 		current++;
197 	}
198 	*current = '\0';
199 	return ret;
200 }
201 
202 /** android_assemble_cmdline - Assemble the command line to pass to the kernel
203  * @return a newly allocated string
204  */
205 static char *android_assemble_cmdline(const char *slot_suffix,
206 				      const char *extra_args)
207 {
208 	const char *cmdline_chunks[16];
209 	const char **current_chunk = cmdline_chunks;
210 	char *env_cmdline, *cmdline, *rootdev_input;
211 	char *allocated_suffix = NULL;
212 	char *allocated_rootdev = NULL;
213 	unsigned long rootdev_len;
214 
215 	env_cmdline = env_get("bootargs");
216 	if (env_cmdline)
217 		*(current_chunk++) = env_cmdline;
218 
219 	/* The |slot_suffix| needs to be passed to the kernel to know what
220 	 * slot to boot from.
221 	 */
222 	if (slot_suffix) {
223 		allocated_suffix = malloc(strlen(ANDROID_ARG_SLOT_SUFFIX) +
224 					  strlen(slot_suffix));
225 		strcpy(allocated_suffix, ANDROID_ARG_SLOT_SUFFIX);
226 		strcat(allocated_suffix, slot_suffix);
227 		*(current_chunk++) = allocated_suffix;
228 	}
229 
230 	rootdev_input = env_get("android_rootdev");
231 	if (rootdev_input) {
232 		rootdev_len = strlen(ANDROID_ARG_ROOT) + CONFIG_SYS_CBSIZE + 1;
233 		allocated_rootdev = malloc(rootdev_len);
234 		strcpy(allocated_rootdev, ANDROID_ARG_ROOT);
235 		cli_simple_process_macros(rootdev_input,
236 					  allocated_rootdev +
237 					  strlen(ANDROID_ARG_ROOT));
238 		/* Make sure that the string is null-terminated since the
239 		 * previous could not copy to the end of the input string if it
240 		 * is too big.
241 		 */
242 		allocated_rootdev[rootdev_len - 1] = '\0';
243 		*(current_chunk++) = allocated_rootdev;
244 	}
245 
246 	if (extra_args)
247 		*(current_chunk++) = extra_args;
248 
249 	*(current_chunk++) = NULL;
250 	cmdline = strjoin(cmdline_chunks, ' ');
251 	free(allocated_suffix);
252 	free(allocated_rootdev);
253 	return cmdline;
254 }
255 
256 int android_bootloader_boot_flow(struct blk_desc *dev_desc,
257 				 const disk_partition_t *misc_part_info,
258 				 unsigned long kernel_address)
259 {
260 	enum android_boot_mode mode;
261 	disk_partition_t boot_part_info;
262 	disk_partition_t system_part_info;
263 	int boot_part_num, system_part_num;
264 	int ret;
265 	char *command_line;
266 	/* TODO: lookup the slot_suffix based on the BCB. */
267 	const char *slot_suffix = "_a";
268 	const char *mode_cmdline = NULL;
269 
270 	/* Determine the boot mode and clear its value for the next boot if
271 	 * needed.
272 	 */
273 	mode = android_bootloader_load_and_clear_mode(dev_desc, misc_part_info);
274 	printf("ANDROID: reboot reason: \"%s\"\n", android_boot_mode_str(mode));
275 
276 	switch (mode) {
277 	case ANDROID_BOOT_MODE_NORMAL:
278 		/* In normal mode, we load the kernel from "boot" but append
279 		 * "skip_initramfs" to the cmdline to make it ignore the
280 		 * recovery initramfs in the boot partition.
281 		 */
282 		mode_cmdline = "skip_initramfs";
283 		break;
284 	case ANDROID_BOOT_MODE_RECOVERY:
285 		/* In recovery mode we still boot the kernel from "boot" but
286 		 * don't skip the initramfs so it boots to recovery.
287 		 */
288 		break;
289 	case ANDROID_BOOT_MODE_BOOTLOADER:
290 		/* Bootloader mode enters fastboot. If this operation fails we
291 		 * simply return since we can't recover from this situation by
292 		 * switching to another slot.
293 		 */
294 		return android_bootloader_boot_bootloader();
295 	}
296 
297 	/* Load the kernel from the desired "boot" partition. */
298 	boot_part_num =
299 	    android_part_get_info_by_name_suffix(dev_desc,
300 						 ANDROID_PARTITION_BOOT,
301 						 slot_suffix, &boot_part_info);
302 	if (boot_part_num < 0)
303 		return -1;
304 	debug("ANDROID: Loading kernel from \"%s\", partition %d.\n",
305 	      boot_part_info.name, boot_part_num);
306 
307 	system_part_num =
308 	    android_part_get_info_by_name_suffix(dev_desc,
309 						 ANDROID_PARTITION_SYSTEM,
310 						 slot_suffix,
311 						 &system_part_info);
312 	if (system_part_num < 0)
313 		return -1;
314 	debug("ANDROID: Using system image from \"%s\", partition %d.\n",
315 	      system_part_info.name, system_part_num);
316 
317 	ret = android_image_load(dev_desc, &boot_part_info, kernel_address,
318 				 -1UL);
319 	if (ret < 0)
320 		return ret;
321 
322 	/* Set Android root variables. */
323 	env_set_ulong("android_root_devnum", dev_desc->devnum);
324 	env_set_ulong("android_root_partnum", system_part_num);
325 	env_set("android_slotsufix", slot_suffix);
326 
327 	/* Assemble the command line */
328 	command_line = android_assemble_cmdline(slot_suffix, mode_cmdline);
329 	env_set("bootargs", command_line);
330 
331 	debug("ANDROID: bootargs: \"%s\"\n", command_line);
332 
333 	android_bootloader_boot_kernel(kernel_address);
334 
335 	/* TODO: If the kernel doesn't boot mark the selected slot as bad. */
336 	return -1;
337 }
338