efi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * EFI support for Xen.
  3. *
  4. * Copyright (C) 1999 VA Linux Systems
  5. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  6. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. * Stephane Eranian <eranian@hpl.hp.com>
  9. * Copyright (C) 2005-2008 Intel Co.
  10. * Fenghua Yu <fenghua.yu@intel.com>
  11. * Bibo Mao <bibo.mao@intel.com>
  12. * Chandramouli Narayanan <mouli@linux.intel.com>
  13. * Huang Ying <ying.huang@intel.com>
  14. * Copyright (C) 2011 Novell Co.
  15. * Jan Beulich <JBeulich@suse.com>
  16. * Copyright (C) 2011-2012 Oracle Co.
  17. * Liang Tang <liang.tang@oracle.com>
  18. * Copyright (c) 2014 Oracle Co., Daniel Kiper
  19. */
  20. #include <linux/bug.h>
  21. #include <linux/efi.h>
  22. #include <linux/init.h>
  23. #include <linux/string.h>
  24. #include <xen/interface/xen.h>
  25. #include <xen/interface/platform.h>
  26. #include <xen/xen.h>
  27. #include <xen/xen-ops.h>
  28. #include <asm/page.h>
  29. #include <asm/xen/hypercall.h>
  30. #define INIT_EFI_OP(name) \
  31. {.cmd = XENPF_efi_runtime_call, \
  32. .u.efi_runtime_call.function = XEN_EFI_##name, \
  33. .u.efi_runtime_call.misc = 0}
  34. #define efi_data(op) (op.u.efi_runtime_call)
  35. efi_status_t xen_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  36. {
  37. struct xen_platform_op op = INIT_EFI_OP(get_time);
  38. if (HYPERVISOR_platform_op(&op) < 0)
  39. return EFI_UNSUPPORTED;
  40. if (tm) {
  41. BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.get_time.time));
  42. memcpy(tm, &efi_data(op).u.get_time.time, sizeof(*tm));
  43. }
  44. if (tc) {
  45. tc->resolution = efi_data(op).u.get_time.resolution;
  46. tc->accuracy = efi_data(op).u.get_time.accuracy;
  47. tc->sets_to_zero = !!(efi_data(op).misc &
  48. XEN_EFI_GET_TIME_SET_CLEARS_NS);
  49. }
  50. return efi_data(op).status;
  51. }
  52. EXPORT_SYMBOL_GPL(xen_efi_get_time);
  53. efi_status_t xen_efi_set_time(efi_time_t *tm)
  54. {
  55. struct xen_platform_op op = INIT_EFI_OP(set_time);
  56. BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.set_time));
  57. memcpy(&efi_data(op).u.set_time, tm, sizeof(*tm));
  58. if (HYPERVISOR_platform_op(&op) < 0)
  59. return EFI_UNSUPPORTED;
  60. return efi_data(op).status;
  61. }
  62. EXPORT_SYMBOL_GPL(xen_efi_set_time);
  63. efi_status_t xen_efi_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
  64. efi_time_t *tm)
  65. {
  66. struct xen_platform_op op = INIT_EFI_OP(get_wakeup_time);
  67. if (HYPERVISOR_platform_op(&op) < 0)
  68. return EFI_UNSUPPORTED;
  69. if (tm) {
  70. BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.get_wakeup_time));
  71. memcpy(tm, &efi_data(op).u.get_wakeup_time, sizeof(*tm));
  72. }
  73. if (enabled)
  74. *enabled = !!(efi_data(op).misc & XEN_EFI_GET_WAKEUP_TIME_ENABLED);
  75. if (pending)
  76. *pending = !!(efi_data(op).misc & XEN_EFI_GET_WAKEUP_TIME_PENDING);
  77. return efi_data(op).status;
  78. }
  79. EXPORT_SYMBOL_GPL(xen_efi_get_wakeup_time);
  80. efi_status_t xen_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  81. {
  82. struct xen_platform_op op = INIT_EFI_OP(set_wakeup_time);
  83. BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.set_wakeup_time));
  84. if (enabled)
  85. efi_data(op).misc = XEN_EFI_SET_WAKEUP_TIME_ENABLE;
  86. if (tm)
  87. memcpy(&efi_data(op).u.set_wakeup_time, tm, sizeof(*tm));
  88. else
  89. efi_data(op).misc |= XEN_EFI_SET_WAKEUP_TIME_ENABLE_ONLY;
  90. if (HYPERVISOR_platform_op(&op) < 0)
  91. return EFI_UNSUPPORTED;
  92. return efi_data(op).status;
  93. }
  94. EXPORT_SYMBOL_GPL(xen_efi_set_wakeup_time);
  95. efi_status_t xen_efi_get_variable(efi_char16_t *name, efi_guid_t *vendor,
  96. u32 *attr, unsigned long *data_size,
  97. void *data)
  98. {
  99. struct xen_platform_op op = INIT_EFI_OP(get_variable);
  100. set_xen_guest_handle(efi_data(op).u.get_variable.name, name);
  101. BUILD_BUG_ON(sizeof(*vendor) !=
  102. sizeof(efi_data(op).u.get_variable.vendor_guid));
  103. memcpy(&efi_data(op).u.get_variable.vendor_guid, vendor, sizeof(*vendor));
  104. efi_data(op).u.get_variable.size = *data_size;
  105. set_xen_guest_handle(efi_data(op).u.get_variable.data, data);
  106. if (HYPERVISOR_platform_op(&op) < 0)
  107. return EFI_UNSUPPORTED;
  108. *data_size = efi_data(op).u.get_variable.size;
  109. if (attr)
  110. *attr = efi_data(op).misc;
  111. return efi_data(op).status;
  112. }
  113. EXPORT_SYMBOL_GPL(xen_efi_get_variable);
  114. efi_status_t xen_efi_get_next_variable(unsigned long *name_size,
  115. efi_char16_t *name,
  116. efi_guid_t *vendor)
  117. {
  118. struct xen_platform_op op = INIT_EFI_OP(get_next_variable_name);
  119. efi_data(op).u.get_next_variable_name.size = *name_size;
  120. set_xen_guest_handle(efi_data(op).u.get_next_variable_name.name, name);
  121. BUILD_BUG_ON(sizeof(*vendor) !=
  122. sizeof(efi_data(op).u.get_next_variable_name.vendor_guid));
  123. memcpy(&efi_data(op).u.get_next_variable_name.vendor_guid, vendor,
  124. sizeof(*vendor));
  125. if (HYPERVISOR_platform_op(&op) < 0)
  126. return EFI_UNSUPPORTED;
  127. *name_size = efi_data(op).u.get_next_variable_name.size;
  128. memcpy(vendor, &efi_data(op).u.get_next_variable_name.vendor_guid,
  129. sizeof(*vendor));
  130. return efi_data(op).status;
  131. }
  132. EXPORT_SYMBOL_GPL(xen_efi_get_next_variable);
  133. efi_status_t xen_efi_set_variable(efi_char16_t *name, efi_guid_t *vendor,
  134. u32 attr, unsigned long data_size,
  135. void *data)
  136. {
  137. struct xen_platform_op op = INIT_EFI_OP(set_variable);
  138. set_xen_guest_handle(efi_data(op).u.set_variable.name, name);
  139. efi_data(op).misc = attr;
  140. BUILD_BUG_ON(sizeof(*vendor) !=
  141. sizeof(efi_data(op).u.set_variable.vendor_guid));
  142. memcpy(&efi_data(op).u.set_variable.vendor_guid, vendor, sizeof(*vendor));
  143. efi_data(op).u.set_variable.size = data_size;
  144. set_xen_guest_handle(efi_data(op).u.set_variable.data, data);
  145. if (HYPERVISOR_platform_op(&op) < 0)
  146. return EFI_UNSUPPORTED;
  147. return efi_data(op).status;
  148. }
  149. EXPORT_SYMBOL_GPL(xen_efi_set_variable);
  150. efi_status_t xen_efi_query_variable_info(u32 attr, u64 *storage_space,
  151. u64 *remaining_space,
  152. u64 *max_variable_size)
  153. {
  154. struct xen_platform_op op = INIT_EFI_OP(query_variable_info);
  155. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  156. return EFI_UNSUPPORTED;
  157. efi_data(op).u.query_variable_info.attr = attr;
  158. if (HYPERVISOR_platform_op(&op) < 0)
  159. return EFI_UNSUPPORTED;
  160. *storage_space = efi_data(op).u.query_variable_info.max_store_size;
  161. *remaining_space = efi_data(op).u.query_variable_info.remain_store_size;
  162. *max_variable_size = efi_data(op).u.query_variable_info.max_size;
  163. return efi_data(op).status;
  164. }
  165. EXPORT_SYMBOL_GPL(xen_efi_query_variable_info);
  166. efi_status_t xen_efi_get_next_high_mono_count(u32 *count)
  167. {
  168. struct xen_platform_op op = INIT_EFI_OP(get_next_high_monotonic_count);
  169. if (HYPERVISOR_platform_op(&op) < 0)
  170. return EFI_UNSUPPORTED;
  171. *count = efi_data(op).misc;
  172. return efi_data(op).status;
  173. }
  174. EXPORT_SYMBOL_GPL(xen_efi_get_next_high_mono_count);
  175. efi_status_t xen_efi_update_capsule(efi_capsule_header_t **capsules,
  176. unsigned long count, unsigned long sg_list)
  177. {
  178. struct xen_platform_op op = INIT_EFI_OP(update_capsule);
  179. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  180. return EFI_UNSUPPORTED;
  181. set_xen_guest_handle(efi_data(op).u.update_capsule.capsule_header_array,
  182. capsules);
  183. efi_data(op).u.update_capsule.capsule_count = count;
  184. efi_data(op).u.update_capsule.sg_list = sg_list;
  185. if (HYPERVISOR_platform_op(&op) < 0)
  186. return EFI_UNSUPPORTED;
  187. return efi_data(op).status;
  188. }
  189. EXPORT_SYMBOL_GPL(xen_efi_update_capsule);
  190. efi_status_t xen_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  191. unsigned long count, u64 *max_size,
  192. int *reset_type)
  193. {
  194. struct xen_platform_op op = INIT_EFI_OP(query_capsule_capabilities);
  195. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  196. return EFI_UNSUPPORTED;
  197. set_xen_guest_handle(efi_data(op).u.query_capsule_capabilities.capsule_header_array,
  198. capsules);
  199. efi_data(op).u.query_capsule_capabilities.capsule_count = count;
  200. if (HYPERVISOR_platform_op(&op) < 0)
  201. return EFI_UNSUPPORTED;
  202. *max_size = efi_data(op).u.query_capsule_capabilities.max_capsule_size;
  203. *reset_type = efi_data(op).u.query_capsule_capabilities.reset_type;
  204. return efi_data(op).status;
  205. }
  206. EXPORT_SYMBOL_GPL(xen_efi_query_capsule_caps);
  207. void xen_efi_reset_system(int reset_type, efi_status_t status,
  208. unsigned long data_size, efi_char16_t *data)
  209. {
  210. switch (reset_type) {
  211. case EFI_RESET_COLD:
  212. case EFI_RESET_WARM:
  213. xen_reboot(SHUTDOWN_reboot);
  214. break;
  215. case EFI_RESET_SHUTDOWN:
  216. xen_reboot(SHUTDOWN_poweroff);
  217. break;
  218. default:
  219. BUG();
  220. }
  221. }
  222. EXPORT_SYMBOL_GPL(xen_efi_reset_system);