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