1180cc7c6SAlex Deymo /*
2180cc7c6SAlex Deymo * Copyright (C) 2017 The Android Open Source Project
3180cc7c6SAlex Deymo *
4180cc7c6SAlex Deymo * SPDX-License-Identifier: BSD-2-Clause
5180cc7c6SAlex Deymo */
6180cc7c6SAlex Deymo
7180cc7c6SAlex Deymo #include <android_cmds.h>
8180cc7c6SAlex Deymo #include <android_ab.h>
9180cc7c6SAlex Deymo #include <common.h>
10180cc7c6SAlex Deymo #include <command.h>
11180cc7c6SAlex Deymo
do_android_ab_select(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])12180cc7c6SAlex Deymo static int do_android_ab_select(cmd_tbl_t *cmdtp, int flag, int argc,
13180cc7c6SAlex Deymo char * const argv[])
14180cc7c6SAlex Deymo {
15180cc7c6SAlex Deymo int ret;
16180cc7c6SAlex Deymo struct blk_desc *dev_desc;
17180cc7c6SAlex Deymo disk_partition_t part_info;
18180cc7c6SAlex Deymo char slot[2];
19180cc7c6SAlex Deymo
20180cc7c6SAlex Deymo if (argc != 4)
21180cc7c6SAlex Deymo return CMD_RET_USAGE;
22180cc7c6SAlex Deymo
23180cc7c6SAlex Deymo /* Lookup the "misc" partition from argv[2] and argv[3] */
24180cc7c6SAlex Deymo if (part_get_info_by_dev_and_name_or_num(argv[2], argv[3],
25180cc7c6SAlex Deymo &dev_desc, &part_info) < 0) {
26180cc7c6SAlex Deymo return CMD_RET_FAILURE;
27180cc7c6SAlex Deymo }
28180cc7c6SAlex Deymo
29180cc7c6SAlex Deymo ret = android_ab_select(dev_desc, &part_info);
30180cc7c6SAlex Deymo if (ret < 0) {
31180cc7c6SAlex Deymo printf("Android boot failed, error %d.\n", ret);
32180cc7c6SAlex Deymo return CMD_RET_FAILURE;
33180cc7c6SAlex Deymo }
34180cc7c6SAlex Deymo
35180cc7c6SAlex Deymo /* Android standard slot names are 'a', 'b', ... */
36180cc7c6SAlex Deymo slot[0] = ANDROID_BOOT_SLOT_NAME(ret);
37180cc7c6SAlex Deymo slot[1] = '\0';
38*170e9eb9SAlex Deymo env_set(argv[1], slot);
39180cc7c6SAlex Deymo printf("ANDROID: Booting slot: %s\n", slot);
40180cc7c6SAlex Deymo return CMD_RET_SUCCESS;
41180cc7c6SAlex Deymo }
42180cc7c6SAlex Deymo
43180cc7c6SAlex Deymo U_BOOT_CMD(
44180cc7c6SAlex Deymo android_ab_select, 4, 0, do_android_ab_select,
45180cc7c6SAlex Deymo "Select the slot used to boot from and register the boot attempt.",
46180cc7c6SAlex Deymo "<slot_var_name> <interface> <dev[:part|;part_name]>\n"
47180cc7c6SAlex Deymo " - Load the slot metadata from the partition 'part' on\n"
48180cc7c6SAlex Deymo " device type 'interface' instance 'dev' and store the active\n"
49180cc7c6SAlex Deymo " slot in the 'slot_var_name' variable. This also updates the\n"
50180cc7c6SAlex Deymo " Android slot metadata with a boot attempt, which can cause\n"
51180cc7c6SAlex Deymo " successive calls to this function to return a different result\n"
52180cc7c6SAlex Deymo " if the returned slot runs out of boot attempts.\n"
53180cc7c6SAlex Deymo " - If 'part_name' is passed, preceded with a ; instead of :, the\n"
54180cc7c6SAlex Deymo " partition name whose label is 'part_name' will be looked up in\n"
55180cc7c6SAlex Deymo " the partition table. This is commonly the \"misc\" partition.\n"
56180cc7c6SAlex Deymo );
57