misc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright 2016 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. */
  5. #include <stdint.h>
  6. #include "bdb.h"
  7. #include "bdb_api.h"
  8. #include "vboot_register.h"
  9. static int did_current_slot_fail(struct vba_context *ctx)
  10. {
  11. uint32_t val = vbe_get_vboot_register(VBOOT_REGISTER_PERSIST);
  12. if (ctx->slot)
  13. return val & VBOOT_REGISTER_FAILED_RW_SECONDARY;
  14. else
  15. return val & VBOOT_REGISTER_FAILED_RW_PRIMARY;
  16. }
  17. static int did_other_slot_fail(struct vba_context *ctx)
  18. {
  19. uint32_t val = vbe_get_vboot_register(VBOOT_REGISTER_PERSIST);
  20. if (ctx->slot)
  21. return val & VBOOT_REGISTER_FAILED_RW_PRIMARY;
  22. else
  23. return val & VBOOT_REGISTER_FAILED_RW_SECONDARY;
  24. }
  25. static void set_try_other_slot(struct vba_context *ctx)
  26. {
  27. uint32_t val = vbe_get_vboot_register(VBOOT_REGISTER_PERSIST);
  28. if (ctx->slot)
  29. val &= ~VBOOT_REGISTER_TRY_SECONDARY_BDB;
  30. else
  31. val |= VBOOT_REGISTER_TRY_SECONDARY_BDB;
  32. vbe_set_vboot_register(VBOOT_REGISTER_PERSIST, val);
  33. }
  34. static void set_recovery_request(struct vba_context *ctx)
  35. {
  36. uint32_t val = vbe_get_vboot_register(VBOOT_REGISTER_PERSIST);
  37. val |= VBOOT_REGISTER_RECOVERY_REQUEST;
  38. vbe_set_vboot_register(VBOOT_REGISTER_PERSIST, val);
  39. }
  40. static void get_current_slot(struct vba_context *ctx)
  41. {
  42. /* Assume SP-RO selects slot this way */
  43. ctx->slot = (vbe_get_vboot_register(VBOOT_REGISTER_PERSIST)
  44. & VBOOT_REGISTER_TRY_SECONDARY_BDB) ? 1 : 0;
  45. }
  46. static void set_current_slot_failed(struct vba_context *ctx)
  47. {
  48. uint32_t val = vbe_get_vboot_register(VBOOT_REGISTER_PERSIST);
  49. if (ctx->slot)
  50. val |= VBOOT_REGISTER_FAILED_RW_SECONDARY;
  51. else
  52. val |= VBOOT_REGISTER_FAILED_RW_PRIMARY;
  53. vbe_set_vboot_register(VBOOT_REGISTER_PERSIST, val);
  54. }
  55. static void unset_current_slot_failed(struct vba_context *ctx)
  56. {
  57. uint32_t val = vbe_get_vboot_register(VBOOT_REGISTER_PERSIST);
  58. if (ctx->slot)
  59. val &= ~VBOOT_REGISTER_FAILED_RW_SECONDARY;
  60. else
  61. val &= ~VBOOT_REGISTER_FAILED_RW_PRIMARY;
  62. vbe_set_vboot_register(VBOOT_REGISTER_PERSIST, val);
  63. }
  64. int vba_bdb_init(struct vba_context *ctx)
  65. {
  66. /* Get current slot */
  67. get_current_slot(ctx);
  68. /* Check current slot failed or not at the last boot */
  69. if (!did_current_slot_fail(ctx)) {
  70. /* If not, we try this slot. Prepare for any accidents */
  71. set_current_slot_failed(ctx);
  72. return BDB_SUCCESS;
  73. }
  74. /* Check other slot failed or not at the previous boot */
  75. if (!did_other_slot_fail(ctx)) {
  76. /* If not, we try the other slot after reboot. */
  77. set_try_other_slot(ctx);
  78. return BDB_ERROR_TRY_OTHER_SLOT;
  79. } else {
  80. /* Otherwise, both slots are bad. Reboot to recovery */
  81. set_recovery_request(ctx);
  82. return BDB_ERROR_RECOVERY_REQUEST;
  83. }
  84. }
  85. int vba_bdb_finalize(struct vba_context *ctx)
  86. {
  87. /* Mark the current slot good */
  88. unset_current_slot_failed(ctx);
  89. /* Disable NVM bus */
  90. return BDB_SUCCESS;
  91. }
  92. void vba_bdb_fail(struct vba_context *ctx)
  93. {
  94. /* We can do some logging here if we want */
  95. /* Unconditionally reboot. FailedRW flag is already set.
  96. * At the next boot, bdb_init will decide what to do. */
  97. vbe_reset();
  98. }