ab_select.c 2.1 KB

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