1*180cc7c6SAlex Deymo /* 2*180cc7c6SAlex Deymo * Copyright (C) 2017 The Android Open Source Project 3*180cc7c6SAlex Deymo * 4*180cc7c6SAlex Deymo * SPDX-License-Identifier: BSD-2-Clause 5*180cc7c6SAlex Deymo */ 6*180cc7c6SAlex Deymo 7*180cc7c6SAlex Deymo #include <android_cmds.h> 8*180cc7c6SAlex Deymo #include <android_ab.h> 9*180cc7c6SAlex Deymo #include <common.h> 10*180cc7c6SAlex Deymo #include <command.h> 11*180cc7c6SAlex Deymo 12*180cc7c6SAlex Deymo static int do_android_ab_select(cmd_tbl_t *cmdtp, int flag, int argc, 13*180cc7c6SAlex Deymo char * const argv[]) 14*180cc7c6SAlex Deymo { 15*180cc7c6SAlex Deymo int ret; 16*180cc7c6SAlex Deymo struct blk_desc *dev_desc; 17*180cc7c6SAlex Deymo disk_partition_t part_info; 18*180cc7c6SAlex Deymo char slot[2]; 19*180cc7c6SAlex Deymo 20*180cc7c6SAlex Deymo if (argc != 4) 21*180cc7c6SAlex Deymo return CMD_RET_USAGE; 22*180cc7c6SAlex Deymo 23*180cc7c6SAlex Deymo /* Lookup the "misc" partition from argv[2] and argv[3] */ 24*180cc7c6SAlex Deymo if (part_get_info_by_dev_and_name_or_num(argv[2], argv[3], 25*180cc7c6SAlex Deymo &dev_desc, &part_info) < 0) { 26*180cc7c6SAlex Deymo return CMD_RET_FAILURE; 27*180cc7c6SAlex Deymo } 28*180cc7c6SAlex Deymo 29*180cc7c6SAlex Deymo ret = android_ab_select(dev_desc, &part_info); 30*180cc7c6SAlex Deymo if (ret < 0) { 31*180cc7c6SAlex Deymo printf("Android boot failed, error %d.\n", ret); 32*180cc7c6SAlex Deymo return CMD_RET_FAILURE; 33*180cc7c6SAlex Deymo } 34*180cc7c6SAlex Deymo 35*180cc7c6SAlex Deymo /* Android standard slot names are 'a', 'b', ... */ 36*180cc7c6SAlex Deymo slot[0] = ANDROID_BOOT_SLOT_NAME(ret); 37*180cc7c6SAlex Deymo slot[1] = '\0'; 38*180cc7c6SAlex Deymo setenv(argv[1], slot); 39*180cc7c6SAlex Deymo printf("ANDROID: Booting slot: %s\n", slot); 40*180cc7c6SAlex Deymo return CMD_RET_SUCCESS; 41*180cc7c6SAlex Deymo } 42*180cc7c6SAlex Deymo 43*180cc7c6SAlex Deymo U_BOOT_CMD( 44*180cc7c6SAlex Deymo android_ab_select, 4, 0, do_android_ab_select, 45*180cc7c6SAlex Deymo "Select the slot used to boot from and register the boot attempt.", 46*180cc7c6SAlex Deymo "<slot_var_name> <interface> <dev[:part|;part_name]>\n" 47*180cc7c6SAlex Deymo " - Load the slot metadata from the partition 'part' on\n" 48*180cc7c6SAlex Deymo " device type 'interface' instance 'dev' and store the active\n" 49*180cc7c6SAlex Deymo " slot in the 'slot_var_name' variable. This also updates the\n" 50*180cc7c6SAlex Deymo " Android slot metadata with a boot attempt, which can cause\n" 51*180cc7c6SAlex Deymo " successive calls to this function to return a different result\n" 52*180cc7c6SAlex Deymo " if the returned slot runs out of boot attempts.\n" 53*180cc7c6SAlex Deymo " - If 'part_name' is passed, preceded with a ; instead of :, the\n" 54*180cc7c6SAlex Deymo " partition name whose label is 'part_name' will be looked up in\n" 55*180cc7c6SAlex Deymo " the partition table. This is commonly the \"misc\" partition.\n" 56*180cc7c6SAlex Deymo ); 57