Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2014 The ChromiumOS Authors. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 16 | #include <boot/coreboot_tables.h> |
Aaron Durbin | b6981c0 | 2015-05-15 15:57:51 -0500 | [diff] [blame] | 17 | #include <boot_device.h> |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 18 | #include <cbmem.h> |
Patrick Georgi | 3e18aca | 2015-04-29 18:59:04 +0200 | [diff] [blame] | 19 | #include <console/cbmem_console.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 20 | #include <fmap.h> |
Furquan Shaikh | 95673af | 2018-04-25 18:15:44 -0700 | [diff] [blame] | 21 | #include <gbb_header.h> |
Vadim Bendebury | c83687d | 2015-04-10 17:50:11 -0700 | [diff] [blame] | 22 | #include <reset.h> |
| 23 | #include <stddef.h> |
| 24 | #include <string.h> |
Furquan Shaikh | 95673af | 2018-04-25 18:15:44 -0700 | [diff] [blame] | 25 | #include <security/vboot/gbb.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 26 | #include <security/vboot/vboot_common.h> |
Furquan Shaikh | 95673af | 2018-04-25 18:15:44 -0700 | [diff] [blame] | 27 | #include <security/vboot/vbnv.h> |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 28 | |
Aaron Durbin | 4e50cdd | 2015-05-15 23:25:46 -0500 | [diff] [blame] | 29 | int vboot_named_region_device(const char *name, struct region_device *rdev) |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 30 | { |
Aaron Durbin | 4e50cdd | 2015-05-15 23:25:46 -0500 | [diff] [blame] | 31 | return fmap_locate_area_as_rdev(name, rdev); |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Aaron Durbin | 5bb9e93 | 2016-08-12 12:46:07 -0500 | [diff] [blame] | 34 | int vboot_named_region_device_rw(const char *name, struct region_device *rdev) |
| 35 | { |
Aaron Durbin | a73a803 | 2016-08-23 13:19:14 -0500 | [diff] [blame] | 36 | return fmap_locate_area_as_rdev_rw(name, rdev); |
Aaron Durbin | 5bb9e93 | 2016-08-12 12:46:07 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Furquan Shaikh | 95673af | 2018-04-25 18:15:44 -0700 | [diff] [blame] | 39 | /* Check if it is okay to enable USB Device Controller (UDC). */ |
| 40 | int vboot_can_enable_udc(void) |
| 41 | { |
| 42 | /* Always disable if not in developer mode */ |
| 43 | if (!vboot_developer_mode_enabled()) |
| 44 | return 0; |
| 45 | /* Enable if GBB flag is set */ |
| 46 | if (gbb_is_flag_set(GBB_FLAG_ENABLE_UDC)) |
| 47 | return 1; |
| 48 | /* Enable if VBNV flag is set */ |
| 49 | if (vbnv_udc_enable_flag()) |
| 50 | return 1; |
| 51 | /* Otherwise disable */ |
| 52 | return 0; |
| 53 | } |
| 54 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 55 | /* ========================== VBOOT HANDOFF APIs =========================== */ |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 56 | int vboot_get_handoff_info(void **addr, uint32_t *size) |
| 57 | { |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 58 | /* |
| 59 | * vboot_handoff is present only after cbmem comes online. If we are in |
| 60 | * pre-ram stage, then bail out early. |
| 61 | */ |
| 62 | if (ENV_BOOTBLOCK || |
| 63 | (ENV_VERSTAGE && IS_ENABLED(CONFIG_VBOOT_STARTS_IN_BOOTBLOCK))) |
Aaron Durbin | 5e29106 | 2016-01-25 16:39:56 -0600 | [diff] [blame] | 64 | return -1; |
| 65 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 66 | struct vboot_handoff *vboot_handoff; |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 67 | vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF); |
| 68 | |
| 69 | if (vboot_handoff == NULL) |
| 70 | return -1; |
| 71 | |
| 72 | *addr = vboot_handoff; |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 73 | |
| 74 | if (size) |
| 75 | *size = sizeof(*vboot_handoff); |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 79 | static int vboot_get_handoff_flag(uint32_t flag) |
Paul Kocialkowski | 115360f | 2015-09-03 11:41:14 +0200 | [diff] [blame] | 80 | { |
| 81 | struct vboot_handoff *vbho; |
| 82 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 83 | /* |
| 84 | * If vboot_handoff cannot be found, return default value of flag as 0. |
| 85 | */ |
| 86 | if (vboot_get_handoff_info((void **)&vbho, NULL)) |
Paul Kocialkowski | 115360f | 2015-09-03 11:41:14 +0200 | [diff] [blame] | 87 | return 0; |
| 88 | |
| 89 | return !!(vbho->init_params.out_flags & flag); |
| 90 | } |
| 91 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 92 | int vboot_handoff_skip_display_init(void) |
Paul Kocialkowski | 115360f | 2015-09-03 11:41:14 +0200 | [diff] [blame] | 93 | { |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 94 | return !vboot_get_handoff_flag(VB_INIT_OUT_ENABLE_DISPLAY); |
Paul Kocialkowski | 115360f | 2015-09-03 11:41:14 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 97 | int vboot_handoff_check_developer_flag(void) |
Paul Kocialkowski | 115360f | 2015-09-03 11:41:14 +0200 | [diff] [blame] | 98 | { |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 99 | return vboot_get_handoff_flag(VB_INIT_OUT_ENABLE_DEVELOPER); |
Paul Kocialkowski | 115360f | 2015-09-03 11:41:14 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 102 | int vboot_handoff_check_recovery_flag(void) |
Paul Kocialkowski | 115360f | 2015-09-03 11:41:14 +0200 | [diff] [blame] | 103 | { |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 104 | return vboot_get_handoff_flag(VB_INIT_OUT_ENABLE_RECOVERY); |
Paul Kocialkowski | 115360f | 2015-09-03 11:41:14 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 107 | int vboot_handoff_get_recovery_reason(void) |
Subrata Banik | df13c31 | 2015-09-28 15:12:08 +0530 | [diff] [blame] | 108 | { |
| 109 | struct vboot_handoff *vbho; |
| 110 | VbSharedDataHeader *sd; |
| 111 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 112 | if (vboot_get_handoff_info((void **)&vbho, NULL)) |
Subrata Banik | df13c31 | 2015-09-28 15:12:08 +0530 | [diff] [blame] | 113 | return 0; |
| 114 | |
| 115 | sd = (VbSharedDataHeader *)vbho->shared_data; |
| 116 | |
| 117 | return sd->recovery_reason; |
| 118 | } |
| 119 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 120 | /* ============================ VBOOT REBOOT ============================== */ |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 121 | void __weak vboot_platform_prepare_reboot(void) |
Aaron Durbin | 5dbefd9 | 2016-01-22 16:33:57 -0600 | [diff] [blame] | 122 | { |
| 123 | } |
| 124 | |
Vadim Bendebury | c83687d | 2015-04-10 17:50:11 -0700 | [diff] [blame] | 125 | void vboot_reboot(void) |
| 126 | { |
Vadim Bendebury | 6e20e2f | 2015-04-10 18:04:04 -0700 | [diff] [blame] | 127 | if (IS_ENABLED(CONFIG_CONSOLE_CBMEM_DUMP_TO_UART)) |
| 128 | cbmem_dump_console(); |
Aaron Durbin | 5dbefd9 | 2016-01-22 16:33:57 -0600 | [diff] [blame] | 129 | vboot_platform_prepare_reboot(); |
Nico Huber | 4f32b64 | 2018-10-05 23:40:21 +0200 | [diff] [blame] | 130 | board_reset(); |
Vadim Bendebury | c83687d | 2015-04-10 17:50:11 -0700 | [diff] [blame] | 131 | } |