xref: /rk3399_rockchip-uboot/common/android_bootloader.c (revision 170e9eb9d1a08bef196b8c4ded4119cb531a8c1c)
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 	size_t part_name_len;
130 
131 	part_name_len = strlen(base_name) + 1;
132 	if (slot_suffix)
133 		part_name_len += strlen(slot_suffix);
134 	part_name = malloc(part_name_len);
135 	if (!part_name)
136 		return -1;
137 	strcpy(part_name, base_name);
138 	if (slot_suffix)
139 		strcat(part_name, slot_suffix);
140 
141 	part_num = part_get_info_by_name(dev_desc, part_name, part_info);
142 	if (part_num < 0) {
143 		debug("ANDROID: Could not find partition \"%s\"\n", part_name);
144 		part_num = -1;
145 	}
146 
147 	free(part_name);
148 	return part_num;
149 }
150 
151 static int android_bootloader_boot_bootloader(void)
152 {
153 	const char *fastboot_cmd = env_get("fastbootcmd");
154 
155 	if (fastboot_cmd)
156 		return run_command(fastboot_cmd, CMD_FLAG_ENV);
157 	return -1;
158 }
159 
160 static int android_bootloader_boot_kernel(unsigned long kernel_address)
161 {
162 	char kernel_addr_str[12];
163 	char *fdt_addr = env_get("fdt_addr");
164 	char *bootm_args[] = { "bootm", kernel_addr_str, "-", fdt_addr, NULL };
165 
166 	sprintf(kernel_addr_str, "0x%lx", kernel_address);
167 
168 	printf("Booting kernel at %s with fdt at %s...\n\n\n",
169 	       kernel_addr_str, fdt_addr);
170 	do_bootm(NULL, 0, 4, bootm_args);
171 
172 	return -1;
173 }
174 
175 static char *strjoin(const char **chunks, char separator)
176 {
177 	int len, joined_len = 0;
178 	char *ret, *current;
179 	const char **p;
180 
181 	for (p = chunks; *p; p++)
182 		joined_len += strlen(*p) + 1;
183 
184 	if (!joined_len) {
185 		ret = malloc(1);
186 		if (ret)
187 			ret[0] = '\0';
188 		return ret;
189 	}
190 
191 	ret = malloc(joined_len);
192 	current = ret;
193 	if (!ret)
194 		return ret;
195 
196 	for (p = chunks; *p; p++) {
197 		len = strlen(*p);
198 		memcpy(current, *p, len);
199 		current += len;
200 		*current = separator;
201 		current++;
202 	}
203 	/* Replace the last separator by a \0. */
204 	current[-1] = '\0';
205 	return ret;
206 }
207 
208 /** android_assemble_cmdline - Assemble the command line to pass to the kernel
209  * @return a newly allocated string
210  */
211 static char *android_assemble_cmdline(const char *slot_suffix,
212 				      const char *extra_args)
213 {
214 	const char *cmdline_chunks[16];
215 	const char **current_chunk = cmdline_chunks;
216 	char *env_cmdline, *cmdline, *rootdev_input;
217 	char *allocated_suffix = NULL;
218 	char *allocated_rootdev = NULL;
219 	unsigned long rootdev_len;
220 
221 	env_cmdline = env_get("bootargs");
222 	if (env_cmdline)
223 		*(current_chunk++) = env_cmdline;
224 
225 	/* The |slot_suffix| needs to be passed to the kernel to know what
226 	 * slot to boot from.
227 	 */
228 	if (slot_suffix) {
229 		allocated_suffix = malloc(strlen(ANDROID_ARG_SLOT_SUFFIX) +
230 					  strlen(slot_suffix));
231 		strcpy(allocated_suffix, ANDROID_ARG_SLOT_SUFFIX);
232 		strcat(allocated_suffix, slot_suffix);
233 		*(current_chunk++) = allocated_suffix;
234 	}
235 
236 	rootdev_input = env_get("android_rootdev");
237 	if (rootdev_input) {
238 		rootdev_len = strlen(ANDROID_ARG_ROOT) + CONFIG_SYS_CBSIZE + 1;
239 		allocated_rootdev = malloc(rootdev_len);
240 		strcpy(allocated_rootdev, ANDROID_ARG_ROOT);
241 		cli_simple_process_macros(rootdev_input,
242 					  allocated_rootdev +
243 					  strlen(ANDROID_ARG_ROOT));
244 		/* Make sure that the string is null-terminated since the
245 		 * previous could not copy to the end of the input string if it
246 		 * is too big.
247 		 */
248 		allocated_rootdev[rootdev_len - 1] = '\0';
249 		*(current_chunk++) = allocated_rootdev;
250 	}
251 
252 	if (extra_args)
253 		*(current_chunk++) = extra_args;
254 
255 	*(current_chunk++) = NULL;
256 	cmdline = strjoin(cmdline_chunks, ' ');
257 	free(allocated_suffix);
258 	free(allocated_rootdev);
259 	return cmdline;
260 }
261 
262 int android_bootloader_boot_flow(struct blk_desc *dev_desc,
263 				 const disk_partition_t *misc_part_info,
264 				 const char *slot,
265 				 unsigned long kernel_address)
266 {
267 	enum android_boot_mode mode;
268 	disk_partition_t boot_part_info;
269 	disk_partition_t system_part_info;
270 	int boot_part_num, system_part_num;
271 	int ret;
272 	char *command_line;
273 	char slot_suffix[3];
274 	const char *mode_cmdline = NULL;
275 
276 	/* Determine the boot mode and clear its value for the next boot if
277 	 * needed.
278 	 */
279 	mode = android_bootloader_load_and_clear_mode(dev_desc, misc_part_info);
280 	printf("ANDROID: reboot reason: \"%s\"\n", android_boot_mode_str(mode));
281 
282 	switch (mode) {
283 	case ANDROID_BOOT_MODE_NORMAL:
284 		/* In normal mode, we load the kernel from "boot" but append
285 		 * "skip_initramfs" to the cmdline to make it ignore the
286 		 * recovery initramfs in the boot partition.
287 		 */
288 		mode_cmdline = "skip_initramfs";
289 		break;
290 	case ANDROID_BOOT_MODE_RECOVERY:
291 		/* In recovery mode we still boot the kernel from "boot" but
292 		 * don't skip the initramfs so it boots to recovery.
293 		 */
294 		break;
295 	case ANDROID_BOOT_MODE_BOOTLOADER:
296 		/* Bootloader mode enters fastboot. If this operation fails we
297 		 * simply return since we can't recover from this situation by
298 		 * switching to another slot.
299 		 */
300 		return android_bootloader_boot_bootloader();
301 	}
302 
303 	slot_suffix[0] = '\0';
304 	if (slot && slot[0]) {
305 		slot_suffix[0] = '_';
306 		slot_suffix[1] = slot[0];
307 		slot_suffix[2] = '\0';
308 	}
309 
310 	/* Load the kernel from the desired "boot" partition. */
311 	boot_part_num =
312 	    android_part_get_info_by_name_suffix(dev_desc,
313 						 ANDROID_PARTITION_BOOT,
314 						 slot_suffix, &boot_part_info);
315 	if (boot_part_num < 0)
316 		return -1;
317 	debug("ANDROID: Loading kernel from \"%s\", partition %d.\n",
318 	      boot_part_info.name, boot_part_num);
319 
320 	system_part_num =
321 	    android_part_get_info_by_name_suffix(dev_desc,
322 						 ANDROID_PARTITION_SYSTEM,
323 						 slot_suffix,
324 						 &system_part_info);
325 	if (system_part_num < 0)
326 		return -1;
327 	debug("ANDROID: Using system image from \"%s\", partition %d.\n",
328 	      system_part_info.name, system_part_num);
329 
330 	ret = android_image_load(dev_desc, &boot_part_info, kernel_address,
331 				 -1UL);
332 	if (ret < 0)
333 		return ret;
334 
335 	/* Set Android root variables. */
336 	env_set_ulong("android_root_devnum", dev_desc->devnum);
337 	env_set_ulong("android_root_partnum", system_part_num);
338 	env_set("android_slotsufix", slot_suffix);
339 
340 	/* Assemble the command line */
341 	command_line = android_assemble_cmdline(slot_suffix, mode_cmdline);
342 	env_set("bootargs", command_line);
343 
344 	debug("ANDROID: bootargs: \"%s\"\n", command_line);
345 
346 	android_bootloader_boot_kernel(kernel_address);
347 
348 	/* TODO: If the kernel doesn't boot mark the selected slot as bad. */
349 	return -1;
350 }
351