blob: 7c178c728ca4c8b5bcba02a04eef2d6a7c86afb6 [file] [log] [blame]
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +03001// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * Copyright (C) 2017 The Android Open Source Project
4 */
5
6#include <android_ab.h>
7#include <command.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <env.h>
Simon Glass655306c2020-05-10 11:39:58 -06009#include <part.h>
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030010
Simon Glassed38aef2020-05-10 11:40:03 -060011static int do_ab_select(struct cmd_tbl *cmdtp, int flag, int argc,
12 char *const argv[])
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030013{
14 int ret;
15 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060016 struct disk_partition part_info;
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030017 char slot[2];
Joshua Watt9d5251c2023-06-23 17:05:48 -050018 bool dec_tries = true;
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030019
Joshua Watt9d5251c2023-06-23 17:05:48 -050020 if (argc < 4)
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030021 return CMD_RET_USAGE;
22
Joshua Watt9d5251c2023-06-23 17:05:48 -050023 for (int i = 4; i < argc; i++) {
24 if (strcmp(argv[i], "--no-dec") == 0) {
25 dec_tries = false;
26 } else {
27 return CMD_RET_USAGE;
28 }
29 }
30
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030031 /* Lookup the "misc" partition from argv[2] and argv[3] */
32 if (part_get_info_by_dev_and_name_or_num(argv[2], argv[3],
Sean Andersonc81028d2021-02-05 09:38:56 -050033 &dev_desc, &part_info,
34 false) < 0) {
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030035 return CMD_RET_FAILURE;
36 }
37
Joshua Watt9d5251c2023-06-23 17:05:48 -050038 ret = ab_select_slot(dev_desc, &part_info, dec_tries);
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030039 if (ret < 0) {
40 printf("Android boot failed, error %d.\n", ret);
41 return CMD_RET_FAILURE;
42 }
43
44 /* Android standard slot names are 'a', 'b', ... */
45 slot[0] = BOOT_SLOT_NAME(ret);
46 slot[1] = '\0';
47 env_set(argv[1], slot);
48 printf("ANDROID: Booting slot: %s\n", slot);
49 return CMD_RET_SUCCESS;
50}
51
Joshua Watt9d5251c2023-06-23 17:05:48 -050052U_BOOT_CMD(ab_select, 5, 0, do_ab_select,
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030053 "Select the slot used to boot from and register the boot attempt.",
Joshua Watt9d5251c2023-06-23 17:05:48 -050054 "<slot_var_name> <interface> <dev[:part|#part_name]> [--no-dec]\n"
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030055 " - Load the slot metadata from the partition 'part' on\n"
56 " device type 'interface' instance 'dev' and store the active\n"
57 " slot in the 'slot_var_name' variable. This also updates the\n"
58 " Android slot metadata with a boot attempt, which can cause\n"
59 " successive calls to this function to return a different result\n"
60 " if the returned slot runs out of boot attempts.\n"
61 " - If 'part_name' is passed, preceded with a # instead of :, the\n"
62 " partition name whose label is 'part_name' will be looked up in\n"
63 " the partition table. This is commonly the \"misc\" partition.\n"
Joshua Watt9d5251c2023-06-23 17:05:48 -050064 " - If '--no-dec' is set, the number of tries remaining will not\n"
65 " decremented for the selected boot slot\n"
Ruslan Trofymenkoa24f9b42019-07-05 15:37:33 +030066);