ec_sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* Copyright (c) 2013 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. * EC software sync routines for vboot
  6. */
  7. #include "2sysincludes.h"
  8. #include "2common.h"
  9. #include "2misc.h"
  10. #include "2nvstorage.h"
  11. #include "sysincludes.h"
  12. #include "ec_sync.h"
  13. #include "gbb_header.h"
  14. #include "vboot_common.h"
  15. #include "vboot_kernel.h"
  16. #define VB2_SD_FLAG_ECSYNC_RW \
  17. (VB2_SD_FLAG_ECSYNC_EC_RW | VB2_SD_FLAG_ECSYNC_PD_RW)
  18. #define VB2_SD_FLAG_ECSYNC_ANY \
  19. (VB2_SD_FLAG_ECSYNC_EC_RO | VB2_SD_FLAG_ECSYNC_RW)
  20. #define VB2_SD_FLAG_ECSYNC_IN_RW \
  21. (VB2_SD_FLAG_ECSYNC_EC_IN_RW | VB2_SD_FLAG_ECSYNC_PD_IN_RW)
  22. #define IN_RW(devidx) \
  23. ((devidx) ? VB2_SD_FLAG_ECSYNC_PD_IN_RW : VB2_SD_FLAG_ECSYNC_EC_IN_RW)
  24. #define WHICH_EC(devidx, select) \
  25. ((select) == VB_SELECT_FIRMWARE_READONLY ? VB2_SD_FLAG_ECSYNC_EC_RO : \
  26. ((devidx) ? VB2_SD_FLAG_ECSYNC_PD_RW : VB2_SD_FLAG_ECSYNC_EC_RW))
  27. static void request_recovery(struct vb2_context *ctx, uint32_t recovery_request)
  28. {
  29. VB2_DEBUG("request_recovery(%u)\n", recovery_request);
  30. vb2_nv_set(ctx, VB2_NV_RECOVERY_REQUEST, recovery_request);
  31. }
  32. /**
  33. * Wrapper around VbExEcProtect() which sets recovery reason on error.
  34. */
  35. static VbError_t protect_ec(struct vb2_context *ctx, int devidx,
  36. enum VbSelectFirmware_t select)
  37. {
  38. int rv = VbExEcProtect(devidx, select);
  39. if (rv == VBERROR_EC_REBOOT_TO_RO_REQUIRED) {
  40. VB2_DEBUG("VbExEcProtect() needs reboot\n");
  41. } else if (rv != VBERROR_SUCCESS) {
  42. VB2_DEBUG("VbExEcProtect() returned %d\n", rv);
  43. request_recovery(ctx, VB2_RECOVERY_EC_PROTECT);
  44. }
  45. return rv;
  46. }
  47. /**
  48. * Print a hash to debug output
  49. *
  50. * @param hash Pointer to the hash
  51. * @param hash_size Size of the hash in bytes
  52. * @param desc Description of what's being hashed
  53. */
  54. static void print_hash(const uint8_t *hash, uint32_t hash_size,
  55. const char *desc)
  56. {
  57. int i;
  58. VB2_DEBUG("%s hash: ", desc);
  59. for (i = 0; i < hash_size; i++)
  60. VB2_DEBUG_RAW("%02x", hash[i]);
  61. VB2_DEBUG_RAW("\n");
  62. }
  63. /**
  64. * Check if the hash of the EC code matches the expected hash.
  65. *
  66. * @param ctx Vboot2 context
  67. * @param devidx Index of EC device to check
  68. * @param select Which firmware image to check
  69. * @return VB2_SUCCESS, or non-zero error code.
  70. */
  71. static int check_ec_hash(struct vb2_context *ctx, int devidx,
  72. enum VbSelectFirmware_t select)
  73. {
  74. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  75. /* Get current EC hash. */
  76. const uint8_t *ec_hash = NULL;
  77. int ec_hash_size;
  78. int rv = VbExEcHashImage(devidx, select, &ec_hash, &ec_hash_size);
  79. if (rv) {
  80. VB2_DEBUG("VbExEcHashImage() returned %d\n", rv);
  81. request_recovery(ctx, VB2_RECOVERY_EC_HASH_FAILED);
  82. return VB2_ERROR_EC_HASH_IMAGE;
  83. }
  84. print_hash(ec_hash, ec_hash_size,
  85. select == VB_SELECT_FIRMWARE_READONLY ? "RO" : "RW");
  86. /* Get expected EC hash. */
  87. const uint8_t *hash = NULL;
  88. int hash_size;
  89. rv = VbExEcGetExpectedImageHash(devidx, select, &hash, &hash_size);
  90. if (rv) {
  91. VB2_DEBUG("VbExEcGetExpectedImageHash() returned %d\n", rv);
  92. request_recovery(ctx, VB2_RECOVERY_EC_EXPECTED_HASH);
  93. return VB2_ERROR_EC_HASH_EXPECTED;
  94. }
  95. if (ec_hash_size != hash_size) {
  96. VB2_DEBUG("EC uses %d-byte hash, but AP-RW contains %d bytes\n",
  97. ec_hash_size, hash_size);
  98. request_recovery(ctx, VB2_RECOVERY_EC_HASH_SIZE);
  99. return VB2_ERROR_EC_HASH_SIZE;
  100. }
  101. if (vb2_safe_memcmp(ec_hash, hash, hash_size)) {
  102. print_hash(hash, hash_size, "Expected");
  103. sd->flags |= WHICH_EC(devidx, select);
  104. }
  105. return VB2_SUCCESS;
  106. }
  107. /**
  108. * Update the specified EC and verify the update succeeded
  109. *
  110. * @param ctx Vboot2 context
  111. * @param devidx Index of EC device to check
  112. * @param select Which firmware image to check
  113. * @return VBERROR_SUCCESS, or non-zero error code.
  114. */
  115. static VbError_t update_ec(struct vb2_context *ctx, int devidx,
  116. enum VbSelectFirmware_t select)
  117. {
  118. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  119. VB2_DEBUG("updating %s...\n",
  120. select == VB_SELECT_FIRMWARE_READONLY ? "RO" : "RW");
  121. /* Get expected EC image */
  122. const uint8_t *want = NULL;
  123. int want_size;
  124. int rv = VbExEcGetExpectedImage(devidx, select, &want, &want_size);
  125. if (rv) {
  126. VB2_DEBUG("VbExEcGetExpectedImage() returned %d\n", rv);
  127. request_recovery(ctx, VB2_RECOVERY_EC_EXPECTED_IMAGE);
  128. return rv;
  129. }
  130. VB2_DEBUG("image len = %d\n", want_size);
  131. rv = VbExEcUpdateImage(devidx, select, want, want_size);
  132. if (rv != VBERROR_SUCCESS) {
  133. VB2_DEBUG("VbExEcUpdateImage() returned %d\n", rv);
  134. /*
  135. * The EC may know it needs a reboot. It may need to
  136. * unprotect the region before updating, or may need to
  137. * reboot after updating. Either way, it's not an error
  138. * requiring recovery mode.
  139. *
  140. * If we fail for any other reason, trigger recovery
  141. * mode.
  142. */
  143. if (rv != VBERROR_EC_REBOOT_TO_RO_REQUIRED)
  144. request_recovery(ctx, VB2_RECOVERY_EC_UPDATE);
  145. return rv;
  146. }
  147. /* Verify the EC was updated properly */
  148. sd->flags &= ~WHICH_EC(devidx, select);
  149. if (check_ec_hash(ctx, devidx, select) != VB2_SUCCESS)
  150. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  151. if (sd->flags & WHICH_EC(devidx, select)) {
  152. VB2_DEBUG("Failed to update\n");
  153. request_recovery(ctx, VB2_RECOVERY_EC_UPDATE);
  154. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  155. }
  156. return VBERROR_SUCCESS;
  157. }
  158. /**
  159. * Check if the EC has the correct image active.
  160. *
  161. * @param ctx Vboot2 context
  162. * @param devidx Which device (EC=0, PD=1)
  163. * @return VBERROR_SUCCESS, or non-zero if error.
  164. */
  165. static VbError_t check_ec_active(struct vb2_context *ctx, int devidx)
  166. {
  167. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  168. /* Determine whether the EC is in RO or RW */
  169. int in_rw = 0;
  170. int rv = VbExEcRunningRW(devidx, &in_rw);
  171. if (in_rw) {
  172. sd->flags |= IN_RW(devidx);
  173. }
  174. if (sd->recovery_reason) {
  175. /*
  176. * Recovery mode; just verify the EC is in RO code. Don't do
  177. * software sync, since we don't have a RW image.
  178. */
  179. if (rv == VBERROR_SUCCESS && in_rw == 1) {
  180. /*
  181. * EC is definitely in RW firmware. We want it in
  182. * read-only code, so preserve the current recovery
  183. * reason and reboot.
  184. *
  185. * We don't reboot on error or unknown EC code, because
  186. * we could end up in an endless reboot loop. If we
  187. * had some way to track that we'd already rebooted for
  188. * this reason, we could retry only once.
  189. */
  190. VB2_DEBUG("want recovery but got EC-RW\n");
  191. request_recovery(ctx, sd->recovery_reason);
  192. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  193. }
  194. VB2_DEBUG("in recovery; EC-RO\n");
  195. return VBERROR_SUCCESS;
  196. }
  197. /*
  198. * Not in recovery. If we couldn't determine where the EC was,
  199. * reboot to recovery.
  200. */
  201. if (rv != VBERROR_SUCCESS) {
  202. VB2_DEBUG("VbExEcRunningRW() returned %d\n", rv);
  203. request_recovery(ctx, VB2_RECOVERY_EC_UNKNOWN_IMAGE);
  204. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  205. }
  206. return VBERROR_SUCCESS;
  207. }
  208. #define RO_RETRIES 2 /* Maximum times to retry flashing RO */
  209. /**
  210. * Sync, jump, and protect one EC device
  211. *
  212. * @param ctx Vboot2 context
  213. * @param devidx Which device (EC=0, PD=1)
  214. * @return VBERROR_SUCCESS, or non-zero if error.
  215. */
  216. static VbError_t sync_one_ec(struct vb2_context *ctx, int devidx,
  217. VbCommonParams *cparams)
  218. {
  219. VbSharedDataHeader *shared =
  220. (VbSharedDataHeader *)cparams->shared_data_blob;
  221. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  222. const enum VbSelectFirmware_t select_rw =
  223. shared->firmware_index ? VB_SELECT_FIRMWARE_B :
  224. VB_SELECT_FIRMWARE_A;
  225. int rv;
  226. VB2_DEBUG("devidx=%d\n", devidx);
  227. /* Update the RW Image */
  228. if (sd->flags & VB2_SD_FLAG_ECSYNC_RW) {
  229. if (VB2_SUCCESS != update_ec(ctx, devidx, select_rw))
  230. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  231. }
  232. /* Tell EC to jump to its RW image */
  233. if (!(sd->flags & IN_RW(devidx))) {
  234. VB2_DEBUG("jumping to EC-RW\n");
  235. rv = VbExEcJumpToRW(devidx);
  236. if (rv != VBERROR_SUCCESS) {
  237. VB2_DEBUG("VbExEcJumpToRW() returned %x\n", rv);
  238. /*
  239. * If a previous AP boot has called VbExEcStayInRO(),
  240. * we need to reboot the EC to unlock the ability to
  241. * jump to the RW firmware.
  242. *
  243. * All other errors trigger recovery mode.
  244. */
  245. if (rv != VBERROR_EC_REBOOT_TO_RO_REQUIRED)
  246. request_recovery(ctx, VB2_RECOVERY_EC_JUMP_RW);
  247. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  248. }
  249. }
  250. /* Might need to update EC-RO (but not PD-RO) */
  251. if (sd->flags & VB2_SD_FLAG_ECSYNC_EC_RO) {
  252. VB2_DEBUG("RO Software Sync\n");
  253. /* Reset RO Software Sync NV flag */
  254. vb2_nv_set(ctx, VB2_NV_TRY_RO_SYNC, 0);
  255. /*
  256. * Get the current recovery request (if any). This gets
  257. * overwritten by a failed try. If a later try succeeds, we'll
  258. * need to restore this request (or the lack of a request), or
  259. * else we'll end up in recovery mode even though RO software
  260. * sync did eventually succeed.
  261. */
  262. uint32_t recovery_request =
  263. vb2_nv_get(ctx, VB2_NV_RECOVERY_REQUEST);
  264. /* Update the RO Image. */
  265. int num_tries;
  266. for (num_tries = 0; num_tries < RO_RETRIES; num_tries++) {
  267. if (VB2_SUCCESS ==
  268. update_ec(ctx, devidx, VB_SELECT_FIRMWARE_READONLY))
  269. break;
  270. }
  271. if (num_tries == RO_RETRIES) {
  272. /* Ran out of tries */
  273. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  274. } else if (num_tries) {
  275. /*
  276. * Update succeeded after a failure, so we've polluted
  277. * the recovery request. Restore it.
  278. */
  279. request_recovery(ctx, recovery_request);
  280. }
  281. }
  282. /* Protect RO flash */
  283. rv = protect_ec(ctx, devidx, VB_SELECT_FIRMWARE_READONLY);
  284. if (rv != VBERROR_SUCCESS)
  285. return rv;
  286. /* Protect RW flash */
  287. rv = protect_ec(ctx, devidx, select_rw);
  288. if (rv != VBERROR_SUCCESS)
  289. return rv;
  290. rv = VbExEcDisableJump(devidx);
  291. if (rv != VBERROR_SUCCESS) {
  292. VB2_DEBUG("VbExEcDisableJump() returned %d\n", rv);
  293. request_recovery(ctx, VB2_RECOVERY_EC_SOFTWARE_SYNC);
  294. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  295. }
  296. return rv;
  297. }
  298. VbError_t ec_sync_phase1(struct vb2_context *ctx, VbCommonParams *cparams)
  299. {
  300. VbSharedDataHeader *shared =
  301. (VbSharedDataHeader *)cparams->shared_data_blob;
  302. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  303. /* Reasons not to do sync at all */
  304. if (!(shared->flags & VBSD_EC_SOFTWARE_SYNC))
  305. return VBERROR_SUCCESS;
  306. if (cparams->gbb->flags & GBB_FLAG_DISABLE_EC_SOFTWARE_SYNC)
  307. return VBERROR_SUCCESS;
  308. #ifdef PD_SYNC
  309. const int do_pd_sync = !(cparams->gbb->flags &
  310. GBB_FLAG_DISABLE_PD_SOFTWARE_SYNC);
  311. #else
  312. const int do_pd_sync = 0;
  313. #endif
  314. /* Make sure the EC is running the correct image */
  315. if (check_ec_active(ctx, 0))
  316. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  317. if (do_pd_sync && check_ec_active(ctx, 1))
  318. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  319. /*
  320. * In recovery mode; just verify the EC is in RO code. Don't do
  321. * software sync, since we don't have a RW image.
  322. */
  323. if (sd->recovery_reason)
  324. return VBERROR_SUCCESS;
  325. /* See if we need to update RW. Failures trigger recovery mode. */
  326. const enum VbSelectFirmware_t select_rw =
  327. shared->firmware_index ? VB_SELECT_FIRMWARE_B :
  328. VB_SELECT_FIRMWARE_A;
  329. if (check_ec_hash(ctx, 0, select_rw))
  330. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  331. if (do_pd_sync && check_ec_hash(ctx, 1, select_rw))
  332. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  333. /*
  334. * See if we need to update EC-RO (devidx=0).
  335. *
  336. * If we want to extend this in the future to update PD-RO, we'll use a
  337. * different NV flag so we can track EC-RO and PD-RO updates
  338. * separately.
  339. */
  340. if (vb2_nv_get(ctx, VB2_NV_TRY_RO_SYNC) &&
  341. !(shared->flags & VBSD_BOOT_FIRMWARE_WP_ENABLED) &&
  342. check_ec_hash(ctx, 0, VB_SELECT_FIRMWARE_READONLY)) {
  343. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  344. }
  345. /*
  346. * If we're in RW, we need to reboot back to RO because RW can't be
  347. * updated while we're running it.
  348. *
  349. * TODO: Technically this isn't true for ECs which don't execute from
  350. * flash. For example, if the EC loads code from SPI into RAM before
  351. * executing it.
  352. */
  353. if ((sd->flags & VB2_SD_FLAG_ECSYNC_RW) &&
  354. (sd->flags & VB2_SD_FLAG_ECSYNC_IN_RW)) {
  355. return VBERROR_EC_REBOOT_TO_RO_REQUIRED;
  356. }
  357. return VBERROR_SUCCESS;
  358. }
  359. int ec_will_update_slowly(struct vb2_context *ctx, VbCommonParams *cparams)
  360. {
  361. VbSharedDataHeader *shared =
  362. (VbSharedDataHeader *)cparams->shared_data_blob;
  363. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  364. return ((sd->flags & VB2_SD_FLAG_ECSYNC_ANY) &&
  365. (shared->flags & VBSD_EC_SLOW_UPDATE));
  366. }
  367. VbError_t ec_sync_phase2(struct vb2_context *ctx, VbCommonParams *cparams)
  368. {
  369. VbSharedDataHeader *shared =
  370. (VbSharedDataHeader *)cparams->shared_data_blob;
  371. struct vb2_shared_data *sd = vb2_get_sd(ctx);
  372. /* Reasons not to do sync at all */
  373. if (!(shared->flags & VBSD_EC_SOFTWARE_SYNC))
  374. return VBERROR_SUCCESS;
  375. if (cparams->gbb->flags & GBB_FLAG_DISABLE_EC_SOFTWARE_SYNC)
  376. return VBERROR_SUCCESS;
  377. if (sd->recovery_reason)
  378. return VBERROR_SUCCESS;
  379. /* Handle updates and jumps for EC */
  380. VbError_t retval = sync_one_ec(ctx, 0, cparams);
  381. if (retval != VBERROR_SUCCESS)
  382. return retval;
  383. #ifdef PD_SYNC
  384. /* Handle updates and jumps for PD */
  385. if (!(cparams->gbb->flags & GBB_FLAG_DISABLE_PD_SOFTWARE_SYNC)) {
  386. retval = sync_one_ec(ctx, 1, cparams);
  387. if (retval != VBERROR_SUCCESS)
  388. return retval;
  389. }
  390. #endif
  391. return VBERROR_SUCCESS;
  392. }
  393. VbError_t ec_sync_phase3(struct vb2_context *ctx, VbCommonParams *cparams)
  394. {
  395. VbSharedDataHeader *shared =
  396. (VbSharedDataHeader *)cparams->shared_data_blob;
  397. /* EC verification (and possibly updating / jumping) is done */
  398. VbError_t rv = VbExEcVbootDone(!!shared->recovery_reason);
  399. if (rv)
  400. return rv;
  401. /* Check if we need to cut-off battery. This must be done after EC
  402. * firmware updating and before kernel started. */
  403. if (vb2_nv_get(ctx, VB2_NV_BATTERY_CUTOFF_REQUEST)) {
  404. VB2_DEBUG("Request to cut-off battery\n");
  405. vb2_nv_set(ctx, VB2_NV_BATTERY_CUTOFF_REQUEST, 0);
  406. VbExEcBatteryCutOff();
  407. return VBERROR_SHUTDOWN_REQUESTED;
  408. }
  409. return VBERROR_SUCCESS;
  410. }