Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 1 | // 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 8 | #include <env.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 9 | #include <part.h> |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 10 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 11 | static int do_ab_select(struct cmd_tbl *cmdtp, int flag, int argc, |
| 12 | char *const argv[]) |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 13 | { |
| 14 | int ret; |
| 15 | struct blk_desc *dev_desc; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 16 | struct disk_partition part_info; |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 17 | char slot[2]; |
Joshua Watt | 9d5251c | 2023-06-23 17:05:48 -0500 | [diff] [blame] | 18 | bool dec_tries = true; |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 19 | |
Joshua Watt | 9d5251c | 2023-06-23 17:05:48 -0500 | [diff] [blame] | 20 | if (argc < 4) |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 21 | return CMD_RET_USAGE; |
| 22 | |
Joshua Watt | 9d5251c | 2023-06-23 17:05:48 -0500 | [diff] [blame] | 23 | 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 Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 31 | /* 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 Anderson | c81028d | 2021-02-05 09:38:56 -0500 | [diff] [blame] | 33 | &dev_desc, &part_info, |
| 34 | false) < 0) { |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 35 | return CMD_RET_FAILURE; |
| 36 | } |
| 37 | |
Joshua Watt | 9d5251c | 2023-06-23 17:05:48 -0500 | [diff] [blame] | 38 | |
| 39 | ret = ab_select_slot(dev_desc, &part_info, dec_tries); |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 40 | if (ret < 0) { |
| 41 | printf("Android boot failed, error %d.\n", ret); |
| 42 | return CMD_RET_FAILURE; |
| 43 | } |
| 44 | |
| 45 | /* Android standard slot names are 'a', 'b', ... */ |
| 46 | slot[0] = BOOT_SLOT_NAME(ret); |
| 47 | slot[1] = '\0'; |
| 48 | env_set(argv[1], slot); |
| 49 | printf("ANDROID: Booting slot: %s\n", slot); |
| 50 | return CMD_RET_SUCCESS; |
| 51 | } |
| 52 | |
Joshua Watt | 9d5251c | 2023-06-23 17:05:48 -0500 | [diff] [blame] | 53 | U_BOOT_CMD(ab_select, 5, 0, do_ab_select, |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 54 | "Select the slot used to boot from and register the boot attempt.", |
Joshua Watt | 9d5251c | 2023-06-23 17:05:48 -0500 | [diff] [blame] | 55 | "<slot_var_name> <interface> <dev[:part|#part_name]> [--no-dec]\n" |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 56 | " - Load the slot metadata from the partition 'part' on\n" |
| 57 | " device type 'interface' instance 'dev' and store the active\n" |
| 58 | " slot in the 'slot_var_name' variable. This also updates the\n" |
| 59 | " Android slot metadata with a boot attempt, which can cause\n" |
| 60 | " successive calls to this function to return a different result\n" |
| 61 | " if the returned slot runs out of boot attempts.\n" |
| 62 | " - If 'part_name' is passed, preceded with a # instead of :, the\n" |
| 63 | " partition name whose label is 'part_name' will be looked up in\n" |
| 64 | " the partition table. This is commonly the \"misc\" partition.\n" |
Joshua Watt | 9d5251c | 2023-06-23 17:05:48 -0500 | [diff] [blame] | 65 | " - If '--no-dec' is set, the number of tries remaining will not\n" |
| 66 | " decremented for the selected boot slot\n" |
Ruslan Trofymenko | a24f9b4 | 2019-07-05 15:37:33 +0300 | [diff] [blame] | 67 | ); |