efi_helper.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2020, Linaro Limited
  4. */
  5. #define LOG_CATEGORY LOGC_EFI
  6. #include <common.h>
  7. #include <env.h>
  8. #include <malloc.h>
  9. #include <dm.h>
  10. #include <fs.h>
  11. #include <efi_load_initrd.h>
  12. #include <efi_loader.h>
  13. #include <efi_variable.h>
  14. /**
  15. * efi_create_current_boot_var() - Return Boot#### name were #### is replaced by
  16. * the value of BootCurrent
  17. *
  18. * @var_name: variable name
  19. * @var_name_size: size of var_name
  20. *
  21. * Return: Status code
  22. */
  23. static efi_status_t efi_create_current_boot_var(u16 var_name[],
  24. size_t var_name_size)
  25. {
  26. efi_uintn_t boot_current_size;
  27. efi_status_t ret;
  28. u16 boot_current;
  29. u16 *pos;
  30. boot_current_size = sizeof(boot_current);
  31. ret = efi_get_variable_int(L"BootCurrent",
  32. &efi_global_variable_guid, NULL,
  33. &boot_current_size, &boot_current, NULL);
  34. if (ret != EFI_SUCCESS)
  35. goto out;
  36. pos = efi_create_indexed_name(var_name, var_name_size, "Boot",
  37. boot_current);
  38. if (!pos) {
  39. ret = EFI_OUT_OF_RESOURCES;
  40. goto out;
  41. }
  42. out:
  43. return ret;
  44. }
  45. /**
  46. * efi_get_dp_from_boot() - Retrieve and return a device path from an EFI
  47. * Boot### variable.
  48. * A boot option may contain an array of device paths.
  49. * We use a VenMedia() with a specific GUID to identify
  50. * the usage of the array members. This function is
  51. * used to extract a specific device path
  52. *
  53. * @guid: vendor GUID of the VenMedia() device path node identifying the
  54. * device path
  55. *
  56. * Return: device path or NULL. Caller must free the returned value
  57. */
  58. struct efi_device_path *efi_get_dp_from_boot(const efi_guid_t guid)
  59. {
  60. struct efi_device_path *file_path = NULL;
  61. struct efi_device_path *tmp = NULL;
  62. struct efi_load_option lo;
  63. void *var_value = NULL;
  64. efi_uintn_t size;
  65. efi_status_t ret;
  66. u16 var_name[16];
  67. ret = efi_create_current_boot_var(var_name, sizeof(var_name));
  68. if (ret != EFI_SUCCESS)
  69. return NULL;
  70. var_value = efi_get_var(var_name, &efi_global_variable_guid, &size);
  71. if (!var_value)
  72. return NULL;
  73. ret = efi_deserialize_load_option(&lo, var_value, &size);
  74. if (ret != EFI_SUCCESS)
  75. goto out;
  76. tmp = efi_dp_from_lo(&lo, &size, guid);
  77. if (!tmp)
  78. goto out;
  79. /* efi_dp_dup will just return NULL if efi_dp_next is NULL */
  80. file_path = efi_dp_dup(efi_dp_next(tmp));
  81. out:
  82. efi_free_pool(tmp);
  83. free(var_value);
  84. return file_path;
  85. }