2misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. * Misc functions which need access to vb2_context but are not public APIs
  6. */
  7. #include "2sysincludes.h"
  8. #include "2api.h"
  9. #include "2common.h"
  10. #include "2misc.h"
  11. #include "2nvstorage.h"
  12. #include "2secdata.h"
  13. #include "2sha.h"
  14. #include "2rsa.h"
  15. int vb2_validate_gbb_signature(uint8_t *sig) {
  16. const static uint8_t sig_xor[VB2_GBB_SIGNATURE_SIZE] =
  17. VB2_GBB_XOR_SIGNATURE;
  18. int i;
  19. for (i = 0; i < VB2_GBB_SIGNATURE_SIZE; i++) {
  20. if (sig[i] != (sig_xor[i] ^ VB2_GBB_XOR_CHARS[i]))
  21. return VB2_ERROR_GBB_MAGIC;
  22. }
  23. return VB2_SUCCESS;
  24. }
  25. void vb2_workbuf_from_ctx(struct vb2_context *ctx, struct vb2_workbuf *wb)
  26. {
  27. vb2_workbuf_init(wb, ctx->workbuf + ctx->workbuf_used,
  28. ctx->workbuf_size - ctx->workbuf_used);
  29. }
  30. int vb2_read_gbb_header(struct vb2_context *ctx, struct vb2_gbb_header *gbb)
  31. {
  32. int rv;
  33. /* Read the entire header */
  34. rv = vb2ex_read_resource(ctx, VB2_RES_GBB, 0, gbb, sizeof(*gbb));
  35. if (rv)
  36. return rv;
  37. /* Make sure it's really a GBB */
  38. rv = vb2_validate_gbb_signature(gbb->signature);
  39. if (rv)
  40. return rv;
  41. /* Check for compatible version */
  42. if (gbb->major_version != VB2_GBB_MAJOR_VER)
  43. return VB2_ERROR_GBB_VERSION;
  44. /* Current code is not backwards-compatible to 1.1 headers or older */
  45. if (gbb->minor_version < VB2_GBB_MINOR_VER)
  46. return VB2_ERROR_GBB_TOO_OLD;
  47. /*
  48. * Header size should be at least as big as we expect. It could be
  49. * bigger, if the header has grown.
  50. */
  51. if (gbb->header_size < sizeof(*gbb))
  52. return VB2_ERROR_GBB_HEADER_SIZE;
  53. return VB2_SUCCESS;
  54. }
  55. void vb2_fail(struct vb2_context *ctx, uint8_t reason, uint8_t subcode)
  56. {
  57. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  58. /* If NV data hasn't been initialized, initialize it now */
  59. if (!(sd->status & VB2_SD_STATUS_NV_INIT))
  60. vb2_nv_init(ctx);
  61. /* See if we were far enough in the boot process to choose a slot */
  62. if (sd->status & VB2_SD_STATUS_CHOSE_SLOT) {
  63. /* Boot failed */
  64. vb2_nv_set(ctx, VB2_NV_FW_RESULT, VB2_FW_RESULT_FAILURE);
  65. /* Use up remaining tries */
  66. vb2_nv_set(ctx, VB2_NV_TRY_COUNT, 0);
  67. /*
  68. * Try the other slot next time. We'll alternate
  69. * between slots, which may help if one or both slots is
  70. * flaky.
  71. */
  72. vb2_nv_set(ctx, VB2_NV_TRY_NEXT, 1 - sd->fw_slot);
  73. /*
  74. * If we didn't try the other slot last boot, or we tried it
  75. * and it didn't fail, try it next boot.
  76. */
  77. if (sd->last_fw_slot != 1 - sd->fw_slot ||
  78. sd->last_fw_result != VB2_FW_RESULT_FAILURE)
  79. return;
  80. }
  81. /*
  82. * If we're still here, we failed before choosing a slot, or both
  83. * this slot and the other slot failed in successive boots. So we
  84. * need to go to recovery.
  85. *
  86. * Set a recovery reason and subcode only if they're not already set.
  87. * If recovery is already requested, it's a more specific error code
  88. * than later code is providing and we shouldn't overwrite it.
  89. */
  90. VB2_DEBUG("Need recovery, reason: %#x / %#x\n", reason, subcode);
  91. if (!vb2_nv_get(ctx, VB2_NV_RECOVERY_REQUEST)) {
  92. vb2_nv_set(ctx, VB2_NV_RECOVERY_REQUEST, reason);
  93. vb2_nv_set(ctx, VB2_NV_RECOVERY_SUBCODE, subcode);
  94. }
  95. }
  96. int vb2_init_context(struct vb2_context *ctx)
  97. {
  98. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  99. /* Don't do anything if the context has already been initialized */
  100. if (ctx->workbuf_used)
  101. return VB2_SUCCESS;
  102. /*
  103. * Workbuf had better be big enough for our shared data struct and
  104. * aligned. Not much we can do if it isn't; we'll die before we can
  105. * store a recovery reason.
  106. */
  107. if (ctx->workbuf_size < sizeof(*sd))
  108. return VB2_ERROR_INITCTX_WORKBUF_SMALL;
  109. if (!vb2_aligned(ctx->workbuf, VB2_WORKBUF_ALIGN))
  110. return VB2_ERROR_INITCTX_WORKBUF_ALIGN;
  111. /* Initialize the shared data at the start of the work buffer */
  112. memset(sd, 0, sizeof(*sd));
  113. ctx->workbuf_used = sizeof(*sd);
  114. return VB2_SUCCESS;
  115. }
  116. void vb2_check_recovery(struct vb2_context *ctx)
  117. {
  118. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  119. uint32_t reason = vb2_nv_get(ctx, VB2_NV_RECOVERY_REQUEST);
  120. uint32_t subcode = vb2_nv_get(ctx, VB2_NV_RECOVERY_SUBCODE);
  121. VB2_DEBUG("Recovery reason from previous boot: %#x / %#x\n",
  122. reason, subcode);
  123. /*
  124. * Sets the current recovery request, unless there's already been a
  125. * failure earlier in the boot process.
  126. */
  127. if (!sd->recovery_reason)
  128. sd->recovery_reason = reason;
  129. /* Clear request and subcode so we don't get stuck in recovery mode */
  130. vb2_nv_set(ctx, VB2_NV_RECOVERY_REQUEST, VB2_RECOVERY_NOT_REQUESTED);
  131. vb2_nv_set(ctx, VB2_NV_RECOVERY_SUBCODE, VB2_RECOVERY_NOT_REQUESTED);
  132. if (ctx->flags & VB2_CONTEXT_FORCE_RECOVERY_MODE) {
  133. VB2_DEBUG("Recovery was requested manually\n");
  134. if (subcode && !sd->recovery_reason)
  135. /*
  136. * Recovery was requested at 'broken' screen.
  137. * Promote subcode to reason.
  138. */
  139. sd->recovery_reason = subcode;
  140. else
  141. /* Recovery was forced. Override recovery reason */
  142. sd->recovery_reason = VB2_RECOVERY_RO_MANUAL;
  143. sd->flags |= VB2_SD_FLAG_MANUAL_RECOVERY;
  144. }
  145. /* If recovery reason is non-zero, tell caller we need recovery mode */
  146. if (sd->recovery_reason) {
  147. ctx->flags |= VB2_CONTEXT_RECOVERY_MODE;
  148. VB2_DEBUG("We have a recovery request: %#x / %#x\n",
  149. sd->recovery_reason,
  150. vb2_nv_get(ctx, VB2_NV_RECOVERY_SUBCODE));
  151. }
  152. }
  153. int vb2_fw_parse_gbb(struct vb2_context *ctx)
  154. {
  155. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  156. struct vb2_gbb_header *gbb;
  157. struct vb2_workbuf wb;
  158. int rv;
  159. vb2_workbuf_from_ctx(ctx, &wb);
  160. /* Read GBB into next chunk of work buffer */
  161. gbb = vb2_workbuf_alloc(&wb, sizeof(*gbb));
  162. if (!gbb)
  163. return VB2_ERROR_GBB_WORKBUF;
  164. rv = vb2_read_gbb_header(ctx, gbb);
  165. if (rv)
  166. return rv;
  167. /* Extract the only things we care about at firmware time */
  168. sd->gbb_flags = gbb->flags;
  169. sd->gbb_rootkey_offset = gbb->rootkey_offset;
  170. sd->gbb_rootkey_size = gbb->rootkey_size;
  171. memcpy(sd->gbb_hwid_digest, gbb->hwid_digest, VB2_GBB_HWID_DIGEST_SIZE);
  172. return VB2_SUCCESS;
  173. }
  174. int vb2_check_dev_switch(struct vb2_context *ctx)
  175. {
  176. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  177. uint32_t flags = 0;
  178. uint32_t old_flags;
  179. int is_dev = 0;
  180. int use_secdata = 1;
  181. int rv;
  182. /* Read secure flags */
  183. rv = vb2_secdata_get(ctx, VB2_SECDATA_FLAGS, &flags);
  184. if (rv) {
  185. if (ctx->flags & VB2_CONTEXT_RECOVERY_MODE) {
  186. /*
  187. * Recovery mode needs to check other ways developer
  188. * mode can be enabled, so don't give up yet. But
  189. * since we can't read secdata, assume dev mode was
  190. * disabled.
  191. */
  192. use_secdata = 0;
  193. flags = 0;
  194. } else {
  195. /* Normal mode simply fails */
  196. return rv;
  197. }
  198. }
  199. old_flags = flags;
  200. /* Handle dev disable request */
  201. if (use_secdata && vb2_nv_get(ctx, VB2_NV_DISABLE_DEV_REQUEST)) {
  202. flags &= ~VB2_SECDATA_FLAG_DEV_MODE;
  203. /* Clear the request */
  204. vb2_nv_set(ctx, VB2_NV_DISABLE_DEV_REQUEST, 0);
  205. }
  206. /*
  207. * Check if we've been asked by the caller to disable dev mode. Note
  208. * that hardware switch and GBB flag will take precedence over this.
  209. */
  210. if (ctx->flags & VB2_DISABLE_DEVELOPER_MODE)
  211. flags &= ~VB2_SECDATA_FLAG_DEV_MODE;
  212. /* Check virtual dev switch */
  213. if (flags & VB2_SECDATA_FLAG_DEV_MODE)
  214. is_dev = 1;
  215. /* Handle forcing dev mode via physical switch */
  216. if (ctx->flags & VB2_CONTEXT_FORCE_DEVELOPER_MODE)
  217. is_dev = 1;
  218. /* Check if GBB is forcing dev mode */
  219. if (sd->gbb_flags & VB2_GBB_FLAG_FORCE_DEV_SWITCH_ON)
  220. is_dev = 1;
  221. /* Handle whichever mode we end up in */
  222. if (is_dev) {
  223. /* Developer mode */
  224. sd->flags |= VB2_SD_DEV_MODE_ENABLED;
  225. ctx->flags |= VB2_CONTEXT_DEVELOPER_MODE;
  226. flags |= VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER;
  227. } else {
  228. /* Normal mode */
  229. flags &= ~VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER;
  230. /*
  231. * Disable dev_boot_* flags. This ensures they will be
  232. * initially disabled if the user later transitions back into
  233. * developer mode.
  234. */
  235. vb2_nv_set(ctx, VB2_NV_DEV_BOOT_USB, 0);
  236. vb2_nv_set(ctx, VB2_NV_DEV_BOOT_LEGACY, 0);
  237. vb2_nv_set(ctx, VB2_NV_DEV_BOOT_SIGNED_ONLY, 0);
  238. vb2_nv_set(ctx, VB2_NV_DEV_BOOT_FASTBOOT_FULL_CAP, 0);
  239. vb2_nv_set(ctx, VB2_NV_DEV_DEFAULT_BOOT, 0);
  240. vb2_nv_set(ctx, VB2_NV_FASTBOOT_UNLOCK_IN_FW, 0);
  241. }
  242. if (ctx->flags & VB2_CONTEXT_FORCE_WIPEOUT_MODE)
  243. vb2_nv_set(ctx, VB2_NV_REQ_WIPEOUT, 1);
  244. if (flags != old_flags) {
  245. /*
  246. * Just changed dev mode state. Clear TPM owner. This must be
  247. * done here instead of simply passing a flag to
  248. * vb2_check_tpm_clear(), because we don't want to update
  249. * last_boot_developer and then fail to clear the TPM owner.
  250. *
  251. * Note that we do this even if we couldn't read secdata, since
  252. * the TPM owner and secdata may be independent, and we want
  253. * the owner to be cleared if *this boot* is different than the
  254. * last one (perhaps due to GBB or hardware override).
  255. */
  256. rv = vb2ex_tpm_clear_owner(ctx);
  257. if (use_secdata) {
  258. /* Check for failure to clear owner */
  259. if (rv) {
  260. /*
  261. * Note that this truncates rv to 8 bit. Which
  262. * is not as useful as the full error code, but
  263. * we don't have NVRAM space to store the full
  264. * 32-bit code.
  265. */
  266. vb2_fail(ctx, VB2_RECOVERY_TPM_CLEAR_OWNER, rv);
  267. return rv;
  268. }
  269. /* Save new flags */
  270. rv = vb2_secdata_set(ctx, VB2_SECDATA_FLAGS, flags);
  271. if (rv)
  272. return rv;
  273. }
  274. }
  275. return VB2_SUCCESS;
  276. }
  277. int vb2_check_tpm_clear(struct vb2_context *ctx)
  278. {
  279. int rv;
  280. /* Check if we've been asked to clear the owner */
  281. if (!vb2_nv_get(ctx, VB2_NV_CLEAR_TPM_OWNER_REQUEST))
  282. return VB2_SUCCESS; /* No need to clear */
  283. /* Request applies one time only */
  284. vb2_nv_set(ctx, VB2_NV_CLEAR_TPM_OWNER_REQUEST, 0);
  285. /* Try clearing */
  286. rv = vb2ex_tpm_clear_owner(ctx);
  287. if (rv) {
  288. /*
  289. * Note that this truncates rv to 8 bit. Which is not as
  290. * useful as the full error code, but we don't have NVRAM space
  291. * to store the full 32-bit code.
  292. */
  293. vb2_fail(ctx, VB2_RECOVERY_TPM_CLEAR_OWNER, rv);
  294. return rv;
  295. }
  296. /* Clear successful */
  297. vb2_nv_set(ctx, VB2_NV_CLEAR_TPM_OWNER_DONE, 1);
  298. return VB2_SUCCESS;
  299. }
  300. int vb2_select_fw_slot(struct vb2_context *ctx)
  301. {
  302. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  303. uint32_t tries;
  304. /* Get result of last boot */
  305. sd->last_fw_slot = vb2_nv_get(ctx, VB2_NV_FW_TRIED);
  306. sd->last_fw_result = vb2_nv_get(ctx, VB2_NV_FW_RESULT);
  307. /* Save to the previous result fields in NV storage */
  308. vb2_nv_set(ctx, VB2_NV_FW_PREV_TRIED, sd->last_fw_slot);
  309. vb2_nv_set(ctx, VB2_NV_FW_PREV_RESULT, sd->last_fw_result);
  310. /* Clear result, since we don't know what will happen this boot */
  311. vb2_nv_set(ctx, VB2_NV_FW_RESULT, VB2_FW_RESULT_UNKNOWN);
  312. /* Get slot to try */
  313. sd->fw_slot = vb2_nv_get(ctx, VB2_NV_TRY_NEXT);
  314. /* Check try count */
  315. tries = vb2_nv_get(ctx, VB2_NV_TRY_COUNT);
  316. if (sd->last_fw_result == VB2_FW_RESULT_TRYING &&
  317. sd->last_fw_slot == sd->fw_slot &&
  318. tries == 0) {
  319. /*
  320. * We used up our last try on the previous boot, so fall back
  321. * to the other slot this boot.
  322. */
  323. sd->fw_slot = 1 - sd->fw_slot;
  324. vb2_nv_set(ctx, VB2_NV_TRY_NEXT, sd->fw_slot);
  325. }
  326. if (tries > 0) {
  327. /* Still trying this firmware */
  328. vb2_nv_set(ctx, VB2_NV_FW_RESULT, VB2_FW_RESULT_TRYING);
  329. /* Decrement non-zero try count, unless told not to */
  330. if (!(ctx->flags & VB2_CONTEXT_NOFAIL_BOOT))
  331. vb2_nv_set(ctx, VB2_NV_TRY_COUNT, tries - 1);
  332. }
  333. /* Store the slot we're trying */
  334. vb2_nv_set(ctx, VB2_NV_FW_TRIED, sd->fw_slot);
  335. /* Set context flag if we're using slot B */
  336. if (sd->fw_slot)
  337. ctx->flags |= VB2_CONTEXT_FW_SLOT_B;
  338. /* Set status flag */
  339. sd->status |= VB2_SD_STATUS_CHOSE_SLOT;
  340. return VB2_SUCCESS;
  341. }