2api.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Copyright (c) 2014 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. * Externally-callable APIs
  6. * (Firmware portion)
  7. */
  8. #include "2sysincludes.h"
  9. #include "2api.h"
  10. #include "2common.h"
  11. #include "2misc.h"
  12. #include "2nvstorage.h"
  13. #include "2secdata.h"
  14. #include "2sha.h"
  15. #include "2rsa.h"
  16. #include "2tpm_bootmode.h"
  17. int vb2api_secdata_check(const struct vb2_context *ctx)
  18. {
  19. return vb2_secdata_check_crc(ctx);
  20. }
  21. int vb2api_secdata_create(struct vb2_context *ctx)
  22. {
  23. return vb2_secdata_create(ctx);
  24. }
  25. void vb2api_fail(struct vb2_context *ctx, uint8_t reason, uint8_t subcode)
  26. {
  27. /* Initialize the vboot context if it hasn't been yet */
  28. vb2_init_context(ctx);
  29. vb2_fail(ctx, reason, subcode);
  30. }
  31. int vb2api_fw_phase1(struct vb2_context *ctx)
  32. {
  33. int rv;
  34. /* Initialize the vboot context if it hasn't been yet */
  35. vb2_init_context(ctx);
  36. /* Initialize NV context */
  37. vb2_nv_init(ctx);
  38. /*
  39. * Handle caller-requested reboot due to secdata. Do this before we
  40. * even look at secdata. If we fail because of a reboot loop we'll be
  41. * the first failure so will get to set the recovery reason.
  42. */
  43. if (!(ctx->flags & VB2_CONTEXT_SECDATA_WANTS_REBOOT)) {
  44. /* No reboot requested */
  45. vb2_nv_set(ctx, VB2_NV_TPM_REQUESTED_REBOOT, 0);
  46. } else if (vb2_nv_get(ctx, VB2_NV_TPM_REQUESTED_REBOOT)) {
  47. /*
  48. * Reboot requested... again. Fool me once, shame on you.
  49. * Fool me twice, shame on me. Fail into recovery to avoid
  50. * a reboot loop.
  51. */
  52. vb2_fail(ctx, VB2_RECOVERY_RO_TPM_REBOOT, 0);
  53. } else {
  54. /* Reboot requested for the first time */
  55. vb2_nv_set(ctx, VB2_NV_TPM_REQUESTED_REBOOT, 1);
  56. return VB2_ERROR_API_PHASE1_SECDATA_REBOOT;
  57. }
  58. /* Initialize secure data */
  59. rv = vb2_secdata_init(ctx);
  60. if (rv)
  61. vb2_fail(ctx, VB2_RECOVERY_SECDATA_INIT, rv);
  62. /* Load and parse the GBB header */
  63. rv = vb2_fw_parse_gbb(ctx);
  64. if (rv)
  65. vb2_fail(ctx, VB2_RECOVERY_GBB_HEADER, rv);
  66. /*
  67. * Check for recovery. Note that this function returns void, since any
  68. * errors result in requesting recovery. That's also why we don't
  69. * return error from failures in the preceding two steps; those
  70. * failures simply cause us to detect recovery mode here.
  71. */
  72. vb2_check_recovery(ctx);
  73. /* Check for dev switch */
  74. rv = vb2_check_dev_switch(ctx);
  75. if (rv && !(ctx->flags & VB2_CONTEXT_RECOVERY_MODE)) {
  76. /*
  77. * Error in dev switch processing, and we weren't already
  78. * headed for recovery mode. Reboot into recovery mode, since
  79. * it's too late to handle those errors this boot, and we need
  80. * to take a different path through the dev switch checking
  81. * code in that case.
  82. */
  83. vb2_fail(ctx, VB2_RECOVERY_DEV_SWITCH, rv);
  84. return rv;
  85. }
  86. /* Return error if recovery is needed */
  87. if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
  88. /* Always clear RAM when entering recovery mode */
  89. ctx->flags |= VB2_CONTEXT_CLEAR_RAM;
  90. return VB2_ERROR_API_PHASE1_RECOVERY;
  91. }
  92. return VB2_SUCCESS;
  93. }
  94. int vb2api_fw_phase2(struct vb2_context *ctx)
  95. {
  96. int rv;
  97. /*
  98. * Use the slot from the last boot if this is a resume. Do not set
  99. * VB2_SD_STATUS_CHOSE_SLOT so the try counter is not decremented on
  100. * failure as we are explicitly not attempting to boot from a new slot.
  101. */
  102. if (ctx->flags & VB2_CONTEXT_S3_RESUME) {
  103. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  104. /* Set the current slot to the last booted slot */
  105. sd->fw_slot = vb2_nv_get(ctx, VB2_NV_FW_TRIED);
  106. /* Set context flag if we're using slot B */
  107. if (sd->fw_slot)
  108. ctx->flags |= VB2_CONTEXT_FW_SLOT_B;
  109. return VB2_SUCCESS;
  110. }
  111. /* Always clear RAM when entering developer mode */
  112. if (ctx->flags & VB2_CONTEXT_DEVELOPER_MODE)
  113. ctx->flags |= VB2_CONTEXT_CLEAR_RAM;
  114. /* Check for explicit request to clear TPM */
  115. rv = vb2_check_tpm_clear(ctx);
  116. if (rv) {
  117. vb2_fail(ctx, VB2_RECOVERY_TPM_CLEAR_OWNER, rv);
  118. return rv;
  119. }
  120. /* Decide which firmware slot to try this boot */
  121. rv = vb2_select_fw_slot(ctx);
  122. if (rv) {
  123. vb2_fail(ctx, VB2_RECOVERY_FW_SLOT, rv);
  124. return rv;
  125. }
  126. return VB2_SUCCESS;
  127. }
  128. int vb2api_extend_hash(struct vb2_context *ctx,
  129. const void *buf,
  130. uint32_t size)
  131. {
  132. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  133. struct vb2_digest_context *dc = (struct vb2_digest_context *)
  134. (ctx->workbuf + sd->workbuf_hash_offset);
  135. /* Must have initialized hash digest work area */
  136. if (!sd->workbuf_hash_size)
  137. return VB2_ERROR_API_EXTEND_HASH_WORKBUF;
  138. /* Don't extend past the data we expect to hash */
  139. if (!size || size > sd->hash_remaining_size)
  140. return VB2_ERROR_API_EXTEND_HASH_SIZE;
  141. sd->hash_remaining_size -= size;
  142. if (dc->using_hwcrypto)
  143. return vb2ex_hwcrypto_digest_extend(buf, size);
  144. else
  145. return vb2_digest_extend(dc, buf, size);
  146. }
  147. int vb2api_get_pcr_digest(struct vb2_context *ctx,
  148. enum vb2_pcr_digest which_digest,
  149. uint8_t *dest,
  150. uint32_t *dest_size)
  151. {
  152. const uint8_t *digest;
  153. uint32_t digest_size;
  154. switch (which_digest) {
  155. case BOOT_MODE_PCR:
  156. digest = vb2_get_boot_state_digest(ctx);
  157. digest_size = VB2_SHA1_DIGEST_SIZE;
  158. break;
  159. case HWID_DIGEST_PCR:
  160. digest = vb2_get_sd(ctx)->gbb_hwid_digest;
  161. digest_size = VB2_GBB_HWID_DIGEST_SIZE;
  162. break;
  163. default:
  164. return VB2_ERROR_API_PCR_DIGEST;
  165. }
  166. if (digest == NULL || *dest_size < digest_size)
  167. return VB2_ERROR_API_PCR_DIGEST_BUF;
  168. memcpy(dest, digest, digest_size);
  169. if (digest_size < *dest_size)
  170. memset(dest + digest_size, 0, *dest_size - digest_size);
  171. *dest_size = digest_size;
  172. return VB2_SUCCESS;
  173. }