efi_boottime.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * EFI application boot time services
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #include <malloc.h>
  11. #include <asm/global_data.h>
  12. #include <libfdt_env.h>
  13. #include <u-boot/crc.h>
  14. #include <bootm.h>
  15. #include <inttypes.h>
  16. #include <watchdog.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /* This list contains all the EFI objects our payload has access to */
  19. LIST_HEAD(efi_obj_list);
  20. /*
  21. * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
  22. * we need to do trickery with caches. Since we don't want to break the EFI
  23. * aware boot path, only apply hacks when loading exiting directly (breaking
  24. * direct Linux EFI booting along the way - oh well).
  25. */
  26. static bool efi_is_direct_boot = true;
  27. /*
  28. * EFI can pass arbitrary additional "tables" containing vendor specific
  29. * information to the payload. One such table is the FDT table which contains
  30. * a pointer to a flattened device tree blob.
  31. *
  32. * In most cases we want to pass an FDT to the payload, so reserve one slot of
  33. * config table space for it. The pointer gets populated by do_bootefi_exec().
  34. */
  35. static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
  36. #ifdef CONFIG_ARM
  37. /*
  38. * The "gd" pointer lives in a register on ARM and AArch64 that we declare
  39. * fixed when compiling U-Boot. However, the payload does not know about that
  40. * restriction so we need to manually swap its and our view of that register on
  41. * EFI callback entry/exit.
  42. */
  43. static volatile void *efi_gd, *app_gd;
  44. #endif
  45. /* Called from do_bootefi_exec() */
  46. void efi_save_gd(void)
  47. {
  48. #ifdef CONFIG_ARM
  49. efi_gd = gd;
  50. #endif
  51. }
  52. /* Called on every callback entry */
  53. void efi_restore_gd(void)
  54. {
  55. #ifdef CONFIG_ARM
  56. /* Only restore if we're already in EFI context */
  57. if (!efi_gd)
  58. return;
  59. if (gd != efi_gd)
  60. app_gd = gd;
  61. gd = efi_gd;
  62. #endif
  63. }
  64. /* Called on every callback exit */
  65. efi_status_t efi_exit_func(efi_status_t ret)
  66. {
  67. #ifdef CONFIG_ARM
  68. gd = app_gd;
  69. #endif
  70. return ret;
  71. }
  72. static efi_status_t efi_unsupported(const char *funcname)
  73. {
  74. debug("EFI: App called into unimplemented function %s\n", funcname);
  75. return EFI_EXIT(EFI_UNSUPPORTED);
  76. }
  77. static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2)
  78. {
  79. return memcmp(g1, g2, sizeof(efi_guid_t));
  80. }
  81. static unsigned long EFIAPI efi_raise_tpl(unsigned long new_tpl)
  82. {
  83. EFI_ENTRY("0x%lx", new_tpl);
  84. return EFI_EXIT(0);
  85. }
  86. static void EFIAPI efi_restore_tpl(unsigned long old_tpl)
  87. {
  88. EFI_ENTRY("0x%lx", old_tpl);
  89. EFI_EXIT(efi_unsupported(__func__));
  90. }
  91. static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
  92. unsigned long pages,
  93. uint64_t *memory)
  94. {
  95. efi_status_t r;
  96. EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
  97. r = efi_allocate_pages(type, memory_type, pages, memory);
  98. return EFI_EXIT(r);
  99. }
  100. static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
  101. unsigned long pages)
  102. {
  103. efi_status_t r;
  104. EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
  105. r = efi_free_pages(memory, pages);
  106. return EFI_EXIT(r);
  107. }
  108. static efi_status_t EFIAPI efi_get_memory_map_ext(
  109. unsigned long *memory_map_size,
  110. struct efi_mem_desc *memory_map,
  111. unsigned long *map_key,
  112. unsigned long *descriptor_size,
  113. uint32_t *descriptor_version)
  114. {
  115. efi_status_t r;
  116. EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
  117. map_key, descriptor_size, descriptor_version);
  118. r = efi_get_memory_map(memory_map_size, memory_map, map_key,
  119. descriptor_size, descriptor_version);
  120. return EFI_EXIT(r);
  121. }
  122. static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
  123. unsigned long size,
  124. void **buffer)
  125. {
  126. efi_status_t r;
  127. EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
  128. r = efi_allocate_pool(pool_type, size, buffer);
  129. return EFI_EXIT(r);
  130. }
  131. static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
  132. {
  133. efi_status_t r;
  134. EFI_ENTRY("%p", buffer);
  135. r = efi_free_pool(buffer);
  136. return EFI_EXIT(r);
  137. }
  138. /*
  139. * Our event capabilities are very limited. Only support a single
  140. * event to exist, so we don't need to maintain lists.
  141. */
  142. static struct {
  143. enum efi_event_type type;
  144. u32 trigger_type;
  145. u32 trigger_time;
  146. u64 trigger_next;
  147. unsigned long notify_tpl;
  148. void (EFIAPI *notify_function) (void *event, void *context);
  149. void *notify_context;
  150. } efi_event = {
  151. /* Disable timers on bootup */
  152. .trigger_next = -1ULL,
  153. };
  154. static efi_status_t EFIAPI efi_create_event(
  155. enum efi_event_type type, ulong notify_tpl,
  156. void (EFIAPI *notify_function) (void *event,
  157. void *context),
  158. void *notify_context, void **event)
  159. {
  160. EFI_ENTRY("%d, 0x%lx, %p, %p", type, notify_tpl, notify_function,
  161. notify_context);
  162. if (efi_event.notify_function) {
  163. /* We only support one event at a time */
  164. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  165. }
  166. if (event == NULL)
  167. return EFI_EXIT(EFI_INVALID_PARAMETER);
  168. if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
  169. return EFI_EXIT(EFI_INVALID_PARAMETER);
  170. if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
  171. notify_function == NULL)
  172. return EFI_EXIT(EFI_INVALID_PARAMETER);
  173. efi_event.type = type;
  174. efi_event.notify_tpl = notify_tpl;
  175. efi_event.notify_function = notify_function;
  176. efi_event.notify_context = notify_context;
  177. *event = &efi_event;
  178. return EFI_EXIT(EFI_SUCCESS);
  179. }
  180. /*
  181. * Our timers have to work without interrupts, so we check whenever keyboard
  182. * input or disk accesses happen if enough time elapsed for it to fire.
  183. */
  184. void efi_timer_check(void)
  185. {
  186. u64 now = timer_get_us();
  187. if (now >= efi_event.trigger_next) {
  188. /* Triggering! */
  189. if (efi_event.trigger_type == EFI_TIMER_PERIODIC)
  190. efi_event.trigger_next += efi_event.trigger_time / 10;
  191. if (efi_event.type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL))
  192. efi_event.notify_function(&efi_event,
  193. efi_event.notify_context);
  194. }
  195. WATCHDOG_RESET();
  196. }
  197. static efi_status_t EFIAPI efi_set_timer(void *event, int type,
  198. uint64_t trigger_time)
  199. {
  200. /* We don't have 64bit division available everywhere, so limit timer
  201. * distances to 32bit bits. */
  202. u32 trigger32 = trigger_time;
  203. EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
  204. if (trigger32 < trigger_time) {
  205. printf("WARNING: Truncating timer from %"PRIx64" to %x\n",
  206. trigger_time, trigger32);
  207. }
  208. if (event != &efi_event) {
  209. /* We only support one event at a time */
  210. return EFI_EXIT(EFI_INVALID_PARAMETER);
  211. }
  212. switch (type) {
  213. case EFI_TIMER_STOP:
  214. efi_event.trigger_next = -1ULL;
  215. break;
  216. case EFI_TIMER_PERIODIC:
  217. case EFI_TIMER_RELATIVE:
  218. efi_event.trigger_next = timer_get_us() + (trigger32 / 10);
  219. break;
  220. default:
  221. return EFI_EXIT(EFI_INVALID_PARAMETER);
  222. }
  223. efi_event.trigger_type = type;
  224. efi_event.trigger_time = trigger_time;
  225. return EFI_EXIT(EFI_SUCCESS);
  226. }
  227. static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
  228. void *event, unsigned long *index)
  229. {
  230. u64 now;
  231. EFI_ENTRY("%ld, %p, %p", num_events, event, index);
  232. now = timer_get_us();
  233. while (now < efi_event.trigger_next) { }
  234. efi_timer_check();
  235. return EFI_EXIT(EFI_SUCCESS);
  236. }
  237. static efi_status_t EFIAPI efi_signal_event(void *event)
  238. {
  239. EFI_ENTRY("%p", event);
  240. return EFI_EXIT(EFI_SUCCESS);
  241. }
  242. static efi_status_t EFIAPI efi_close_event(void *event)
  243. {
  244. EFI_ENTRY("%p", event);
  245. efi_event.trigger_next = -1ULL;
  246. return EFI_EXIT(EFI_SUCCESS);
  247. }
  248. static efi_status_t EFIAPI efi_check_event(void *event)
  249. {
  250. EFI_ENTRY("%p", event);
  251. return EFI_EXIT(EFI_NOT_READY);
  252. }
  253. static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
  254. efi_guid_t *protocol, int protocol_interface_type,
  255. void *protocol_interface)
  256. {
  257. struct list_head *lhandle;
  258. int i;
  259. efi_status_t r;
  260. EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type,
  261. protocol_interface);
  262. if (!handle || !protocol ||
  263. protocol_interface_type != EFI_NATIVE_INTERFACE) {
  264. r = EFI_INVALID_PARAMETER;
  265. goto out;
  266. }
  267. /* Create new handle if requested. */
  268. if (!*handle) {
  269. r = EFI_OUT_OF_RESOURCES;
  270. goto out;
  271. }
  272. /* Find object. */
  273. list_for_each(lhandle, &efi_obj_list) {
  274. struct efi_object *efiobj;
  275. efiobj = list_entry(lhandle, struct efi_object, link);
  276. if (efiobj->handle != *handle)
  277. continue;
  278. /* Check if protocol is already installed on the handle. */
  279. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  280. struct efi_handler *handler = &efiobj->protocols[i];
  281. if (!handler->guid)
  282. continue;
  283. if (!guidcmp(handler->guid, protocol)) {
  284. r = EFI_INVALID_PARAMETER;
  285. goto out;
  286. }
  287. }
  288. /* Install protocol in first empty slot. */
  289. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  290. struct efi_handler *handler = &efiobj->protocols[i];
  291. if (handler->guid)
  292. continue;
  293. handler->guid = protocol;
  294. handler->protocol_interface = protocol_interface;
  295. r = EFI_SUCCESS;
  296. goto out;
  297. }
  298. r = EFI_OUT_OF_RESOURCES;
  299. goto out;
  300. }
  301. r = EFI_INVALID_PARAMETER;
  302. out:
  303. return EFI_EXIT(r);
  304. }
  305. static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
  306. efi_guid_t *protocol, void *old_interface,
  307. void *new_interface)
  308. {
  309. EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface,
  310. new_interface);
  311. return EFI_EXIT(EFI_ACCESS_DENIED);
  312. }
  313. static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
  314. efi_guid_t *protocol, void *protocol_interface)
  315. {
  316. struct list_head *lhandle;
  317. int i;
  318. efi_status_t r = EFI_NOT_FOUND;
  319. EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface);
  320. if (!handle || !protocol) {
  321. r = EFI_INVALID_PARAMETER;
  322. goto out;
  323. }
  324. list_for_each(lhandle, &efi_obj_list) {
  325. struct efi_object *efiobj;
  326. efiobj = list_entry(lhandle, struct efi_object, link);
  327. if (efiobj->handle != handle)
  328. continue;
  329. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  330. struct efi_handler *handler = &efiobj->protocols[i];
  331. const efi_guid_t *hprotocol = handler->guid;
  332. if (!hprotocol)
  333. continue;
  334. if (!guidcmp(hprotocol, protocol)) {
  335. if (handler->protocol_interface) {
  336. r = EFI_ACCESS_DENIED;
  337. } else {
  338. handler->guid = 0;
  339. r = EFI_SUCCESS;
  340. }
  341. goto out;
  342. }
  343. }
  344. }
  345. out:
  346. return EFI_EXIT(r);
  347. }
  348. static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
  349. void *event,
  350. void **registration)
  351. {
  352. EFI_ENTRY("%p, %p, %p", protocol, event, registration);
  353. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  354. }
  355. static int efi_search(enum efi_locate_search_type search_type,
  356. efi_guid_t *protocol, void *search_key,
  357. struct efi_object *efiobj)
  358. {
  359. int i;
  360. switch (search_type) {
  361. case all_handles:
  362. return 0;
  363. case by_register_notify:
  364. return -1;
  365. case by_protocol:
  366. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  367. const efi_guid_t *guid = efiobj->protocols[i].guid;
  368. if (guid && !guidcmp(guid, protocol))
  369. return 0;
  370. }
  371. return -1;
  372. }
  373. return -1;
  374. }
  375. static efi_status_t EFIAPI efi_locate_handle(
  376. enum efi_locate_search_type search_type,
  377. efi_guid_t *protocol, void *search_key,
  378. unsigned long *buffer_size, efi_handle_t *buffer)
  379. {
  380. struct list_head *lhandle;
  381. unsigned long size = 0;
  382. EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
  383. buffer_size, buffer);
  384. /* Count how much space we need */
  385. list_for_each(lhandle, &efi_obj_list) {
  386. struct efi_object *efiobj;
  387. efiobj = list_entry(lhandle, struct efi_object, link);
  388. if (!efi_search(search_type, protocol, search_key, efiobj)) {
  389. size += sizeof(void*);
  390. }
  391. }
  392. if (*buffer_size < size) {
  393. *buffer_size = size;
  394. return EFI_EXIT(EFI_BUFFER_TOO_SMALL);
  395. }
  396. /* Then fill the array */
  397. list_for_each(lhandle, &efi_obj_list) {
  398. struct efi_object *efiobj;
  399. efiobj = list_entry(lhandle, struct efi_object, link);
  400. if (!efi_search(search_type, protocol, search_key, efiobj)) {
  401. *(buffer++) = efiobj->handle;
  402. }
  403. }
  404. *buffer_size = size;
  405. return EFI_EXIT(EFI_SUCCESS);
  406. }
  407. static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
  408. struct efi_device_path **device_path,
  409. efi_handle_t *device)
  410. {
  411. EFI_ENTRY("%p, %p, %p", protocol, device_path, device);
  412. return EFI_EXIT(EFI_NOT_FOUND);
  413. }
  414. efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
  415. {
  416. int i;
  417. /* Check for guid override */
  418. for (i = 0; i < systab.nr_tables; i++) {
  419. if (!guidcmp(guid, &efi_conf_table[i].guid)) {
  420. efi_conf_table[i].table = table;
  421. return EFI_SUCCESS;
  422. }
  423. }
  424. /* No override, check for overflow */
  425. if (i >= ARRAY_SIZE(efi_conf_table))
  426. return EFI_OUT_OF_RESOURCES;
  427. /* Add a new entry */
  428. memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
  429. efi_conf_table[i].table = table;
  430. systab.nr_tables = i + 1;
  431. return EFI_SUCCESS;
  432. }
  433. static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
  434. void *table)
  435. {
  436. EFI_ENTRY("%p, %p", guid, table);
  437. return EFI_EXIT(efi_install_configuration_table(guid, table));
  438. }
  439. static efi_status_t EFIAPI efi_load_image(bool boot_policy,
  440. efi_handle_t parent_image,
  441. struct efi_device_path *file_path,
  442. void *source_buffer,
  443. unsigned long source_size,
  444. efi_handle_t *image_handle)
  445. {
  446. static struct efi_object loaded_image_info_obj = {
  447. .protocols = {
  448. {
  449. .guid = &efi_guid_loaded_image,
  450. },
  451. },
  452. };
  453. struct efi_loaded_image *info;
  454. struct efi_object *obj;
  455. EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
  456. file_path, source_buffer, source_size, image_handle);
  457. info = malloc(sizeof(*info));
  458. loaded_image_info_obj.protocols[0].protocol_interface = info;
  459. obj = malloc(sizeof(loaded_image_info_obj));
  460. memset(info, 0, sizeof(*info));
  461. memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj));
  462. obj->handle = info;
  463. info->file_path = file_path;
  464. info->reserved = efi_load_pe(source_buffer, info);
  465. if (!info->reserved) {
  466. free(info);
  467. free(obj);
  468. return EFI_EXIT(EFI_UNSUPPORTED);
  469. }
  470. *image_handle = info;
  471. list_add_tail(&obj->link, &efi_obj_list);
  472. return EFI_EXIT(EFI_SUCCESS);
  473. }
  474. static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
  475. unsigned long *exit_data_size,
  476. s16 **exit_data)
  477. {
  478. ulong (*entry)(void *image_handle, struct efi_system_table *st);
  479. struct efi_loaded_image *info = image_handle;
  480. EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
  481. entry = info->reserved;
  482. efi_is_direct_boot = false;
  483. /* call the image! */
  484. if (setjmp(&info->exit_jmp)) {
  485. /* We returned from the child image */
  486. return EFI_EXIT(info->exit_status);
  487. }
  488. entry(image_handle, &systab);
  489. /* Should usually never get here */
  490. return EFI_EXIT(EFI_SUCCESS);
  491. }
  492. static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
  493. efi_status_t exit_status, unsigned long exit_data_size,
  494. int16_t *exit_data)
  495. {
  496. struct efi_loaded_image *loaded_image_info = (void*)image_handle;
  497. EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
  498. exit_data_size, exit_data);
  499. loaded_image_info->exit_status = exit_status;
  500. longjmp(&loaded_image_info->exit_jmp, 1);
  501. panic("EFI application exited");
  502. }
  503. static struct efi_object *efi_search_obj(void *handle)
  504. {
  505. struct list_head *lhandle;
  506. list_for_each(lhandle, &efi_obj_list) {
  507. struct efi_object *efiobj;
  508. efiobj = list_entry(lhandle, struct efi_object, link);
  509. if (efiobj->handle == handle)
  510. return efiobj;
  511. }
  512. return NULL;
  513. }
  514. static efi_status_t EFIAPI efi_unload_image(void *image_handle)
  515. {
  516. struct efi_object *efiobj;
  517. EFI_ENTRY("%p", image_handle);
  518. efiobj = efi_search_obj(image_handle);
  519. if (efiobj)
  520. list_del(&efiobj->link);
  521. return EFI_EXIT(EFI_SUCCESS);
  522. }
  523. static void efi_exit_caches(void)
  524. {
  525. #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  526. /*
  527. * Grub on 32bit ARM needs to have caches disabled before jumping into
  528. * a zImage, but does not know of all cache layers. Give it a hand.
  529. */
  530. if (efi_is_direct_boot)
  531. cleanup_before_linux();
  532. #endif
  533. }
  534. static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
  535. unsigned long map_key)
  536. {
  537. EFI_ENTRY("%p, %ld", image_handle, map_key);
  538. board_quiesce_devices();
  539. /* Fix up caches for EFI payloads if necessary */
  540. efi_exit_caches();
  541. /* This stops all lingering devices */
  542. bootm_disable_interrupts();
  543. /* Give the payload some time to boot */
  544. WATCHDOG_RESET();
  545. return EFI_EXIT(EFI_SUCCESS);
  546. }
  547. static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
  548. {
  549. static uint64_t mono = 0;
  550. EFI_ENTRY("%p", count);
  551. *count = mono++;
  552. return EFI_EXIT(EFI_SUCCESS);
  553. }
  554. static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
  555. {
  556. EFI_ENTRY("%ld", microseconds);
  557. udelay(microseconds);
  558. return EFI_EXIT(EFI_SUCCESS);
  559. }
  560. static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
  561. uint64_t watchdog_code,
  562. unsigned long data_size,
  563. uint16_t *watchdog_data)
  564. {
  565. EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
  566. data_size, watchdog_data);
  567. return EFI_EXIT(efi_unsupported(__func__));
  568. }
  569. static efi_status_t EFIAPI efi_connect_controller(
  570. efi_handle_t controller_handle,
  571. efi_handle_t *driver_image_handle,
  572. struct efi_device_path *remain_device_path,
  573. bool recursive)
  574. {
  575. EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
  576. remain_device_path, recursive);
  577. return EFI_EXIT(EFI_NOT_FOUND);
  578. }
  579. static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
  580. void *driver_image_handle,
  581. void *child_handle)
  582. {
  583. EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
  584. child_handle);
  585. return EFI_EXIT(EFI_INVALID_PARAMETER);
  586. }
  587. static efi_status_t EFIAPI efi_close_protocol(void *handle,
  588. efi_guid_t *protocol,
  589. void *agent_handle,
  590. void *controller_handle)
  591. {
  592. EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle,
  593. controller_handle);
  594. return EFI_EXIT(EFI_NOT_FOUND);
  595. }
  596. static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
  597. efi_guid_t *protocol,
  598. struct efi_open_protocol_info_entry **entry_buffer,
  599. unsigned long *entry_count)
  600. {
  601. EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer,
  602. entry_count);
  603. return EFI_EXIT(EFI_NOT_FOUND);
  604. }
  605. static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
  606. efi_guid_t ***protocol_buffer,
  607. unsigned long *protocol_buffer_count)
  608. {
  609. EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
  610. protocol_buffer_count);
  611. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  612. }
  613. static efi_status_t EFIAPI efi_locate_handle_buffer(
  614. enum efi_locate_search_type search_type,
  615. efi_guid_t *protocol, void *search_key,
  616. unsigned long *no_handles, efi_handle_t **buffer)
  617. {
  618. EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key,
  619. no_handles, buffer);
  620. return EFI_EXIT(EFI_NOT_FOUND);
  621. }
  622. static struct efi_class_map efi_class_maps[] = {
  623. {
  624. .guid = &efi_guid_console_control,
  625. .interface = &efi_console_control
  626. },
  627. };
  628. static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
  629. void *registration,
  630. void **protocol_interface)
  631. {
  632. int i;
  633. EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface);
  634. for (i = 0; i < ARRAY_SIZE(efi_class_maps); i++) {
  635. struct efi_class_map *curmap = &efi_class_maps[i];
  636. if (!guidcmp(protocol, curmap->guid)) {
  637. *protocol_interface = (void*)curmap->interface;
  638. return EFI_EXIT(EFI_SUCCESS);
  639. }
  640. }
  641. return EFI_EXIT(EFI_NOT_FOUND);
  642. }
  643. static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
  644. void **handle, ...)
  645. {
  646. EFI_ENTRY("%p", handle);
  647. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  648. }
  649. static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
  650. void *handle, ...)
  651. {
  652. EFI_ENTRY("%p", handle);
  653. return EFI_EXIT(EFI_INVALID_PARAMETER);
  654. }
  655. static efi_status_t EFIAPI efi_calculate_crc32(void *data,
  656. unsigned long data_size,
  657. uint32_t *crc32_p)
  658. {
  659. EFI_ENTRY("%p, %ld", data, data_size);
  660. *crc32_p = crc32(0, data, data_size);
  661. return EFI_EXIT(EFI_SUCCESS);
  662. }
  663. static void EFIAPI efi_copy_mem(void *destination, void *source,
  664. unsigned long length)
  665. {
  666. EFI_ENTRY("%p, %p, %ld", destination, source, length);
  667. memcpy(destination, source, length);
  668. }
  669. static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
  670. {
  671. EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
  672. memset(buffer, value, size);
  673. }
  674. static efi_status_t EFIAPI efi_open_protocol(
  675. void *handle, efi_guid_t *protocol,
  676. void **protocol_interface, void *agent_handle,
  677. void *controller_handle, uint32_t attributes)
  678. {
  679. struct list_head *lhandle;
  680. int i;
  681. efi_status_t r = EFI_INVALID_PARAMETER;
  682. EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol,
  683. protocol_interface, agent_handle, controller_handle,
  684. attributes);
  685. if (!handle || !protocol ||
  686. (!protocol_interface && attributes !=
  687. EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
  688. goto out;
  689. }
  690. switch (attributes) {
  691. case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
  692. case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
  693. case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
  694. break;
  695. case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
  696. if (controller_handle == handle)
  697. goto out;
  698. case EFI_OPEN_PROTOCOL_BY_DRIVER:
  699. case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
  700. if (controller_handle == NULL)
  701. goto out;
  702. case EFI_OPEN_PROTOCOL_EXCLUSIVE:
  703. if (agent_handle == NULL)
  704. goto out;
  705. break;
  706. default:
  707. goto out;
  708. }
  709. list_for_each(lhandle, &efi_obj_list) {
  710. struct efi_object *efiobj;
  711. efiobj = list_entry(lhandle, struct efi_object, link);
  712. if (efiobj->handle != handle)
  713. continue;
  714. for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
  715. struct efi_handler *handler = &efiobj->protocols[i];
  716. const efi_guid_t *hprotocol = handler->guid;
  717. if (!hprotocol)
  718. continue;
  719. if (!guidcmp(hprotocol, protocol)) {
  720. if (attributes !=
  721. EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
  722. *protocol_interface =
  723. handler->protocol_interface;
  724. }
  725. r = EFI_SUCCESS;
  726. goto out;
  727. }
  728. }
  729. goto unsupported;
  730. }
  731. unsupported:
  732. r = EFI_UNSUPPORTED;
  733. out:
  734. return EFI_EXIT(r);
  735. }
  736. static efi_status_t EFIAPI efi_handle_protocol(void *handle,
  737. efi_guid_t *protocol,
  738. void **protocol_interface)
  739. {
  740. return efi_open_protocol(handle, protocol, protocol_interface, NULL,
  741. NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
  742. }
  743. static const struct efi_boot_services efi_boot_services = {
  744. .hdr = {
  745. .headersize = sizeof(struct efi_table_hdr),
  746. },
  747. .raise_tpl = efi_raise_tpl,
  748. .restore_tpl = efi_restore_tpl,
  749. .allocate_pages = efi_allocate_pages_ext,
  750. .free_pages = efi_free_pages_ext,
  751. .get_memory_map = efi_get_memory_map_ext,
  752. .allocate_pool = efi_allocate_pool_ext,
  753. .free_pool = efi_free_pool_ext,
  754. .create_event = efi_create_event,
  755. .set_timer = efi_set_timer,
  756. .wait_for_event = efi_wait_for_event,
  757. .signal_event = efi_signal_event,
  758. .close_event = efi_close_event,
  759. .check_event = efi_check_event,
  760. .install_protocol_interface = efi_install_protocol_interface,
  761. .reinstall_protocol_interface = efi_reinstall_protocol_interface,
  762. .uninstall_protocol_interface = efi_uninstall_protocol_interface,
  763. .handle_protocol = efi_handle_protocol,
  764. .reserved = NULL,
  765. .register_protocol_notify = efi_register_protocol_notify,
  766. .locate_handle = efi_locate_handle,
  767. .locate_device_path = efi_locate_device_path,
  768. .install_configuration_table = efi_install_configuration_table_ext,
  769. .load_image = efi_load_image,
  770. .start_image = efi_start_image,
  771. .exit = efi_exit,
  772. .unload_image = efi_unload_image,
  773. .exit_boot_services = efi_exit_boot_services,
  774. .get_next_monotonic_count = efi_get_next_monotonic_count,
  775. .stall = efi_stall,
  776. .set_watchdog_timer = efi_set_watchdog_timer,
  777. .connect_controller = efi_connect_controller,
  778. .disconnect_controller = efi_disconnect_controller,
  779. .open_protocol = efi_open_protocol,
  780. .close_protocol = efi_close_protocol,
  781. .open_protocol_information = efi_open_protocol_information,
  782. .protocols_per_handle = efi_protocols_per_handle,
  783. .locate_handle_buffer = efi_locate_handle_buffer,
  784. .locate_protocol = efi_locate_protocol,
  785. .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
  786. .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
  787. .calculate_crc32 = efi_calculate_crc32,
  788. .copy_mem = efi_copy_mem,
  789. .set_mem = efi_set_mem,
  790. };
  791. static uint16_t __efi_runtime_data firmware_vendor[] =
  792. { 'D','a','s',' ','U','-','b','o','o','t',0 };
  793. struct efi_system_table __efi_runtime_data systab = {
  794. .hdr = {
  795. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  796. .revision = 0x20005, /* 2.5 */
  797. .headersize = sizeof(struct efi_table_hdr),
  798. },
  799. .fw_vendor = (long)firmware_vendor,
  800. .con_in = (void*)&efi_con_in,
  801. .con_out = (void*)&efi_con_out,
  802. .std_err = (void*)&efi_con_out,
  803. .runtime = (void*)&efi_runtime_services,
  804. .boottime = (void*)&efi_boot_services,
  805. .nr_tables = 0,
  806. .tables = (void*)efi_conf_table,
  807. };