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