Skip to content

Commit 1dac9ef

Browse files
committed
Use internal libefivar for FreeBSD
1 parent ac006ff commit 1dac9ef

File tree

2 files changed

+120
-27
lines changed

2 files changed

+120
-27
lines changed

CMakeLists.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,11 @@ elseif(WIN32)
4242
message("Use Windows API for EFI operations")
4343
# TODO: Add include and lib from Windows API
4444
else()
45-
message("Use my libefivar for EFI operations")
46-
endif()
47-
48-
if(PATCH_FREEBSD_EFIVAR)
49-
# Patch efivar 0.15 build for FreeBSD
50-
# TODO(Inoki): check 0.15 build for FreeBSD
51-
add_definitions(-DEFIVAR_FREEBSD_PATCH)
52-
target_link_libraries(QEFI PRIVATE geom)
45+
if(${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
46+
# Link with FreeBSD system-level libefivar
47+
target_link_libraries(QEFI PUBLIC efivar)
48+
endif()
49+
message("Use qefivar implementations for EFI operations")
5350
endif()
5451

5552
install(

qefi.cpp

Lines changed: 115 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -678,16 +678,6 @@ void qefi_set_variable(QUuid uuid, QString name, QByteArray value)
678678

679679
#else
680680
/* Implementation based on libefivar */
681-
extern "C" {
682-
#include <fcntl.h>
683-
#include <stdio.h>
684-
#include <unistd.h>
685-
#include <stdlib.h>
686-
687-
#include <sys/ioctl.h>
688-
#include <sys/stat.h>
689-
#include <sys/types.h>
690-
691681
#define EFI_VARIABLE_NON_VOLATILE ((uint64_t)0x0000000000000001)
692682
#define EFI_VARIABLE_BOOTSERVICE_ACCESS ((uint64_t)0x0000000000000002)
693683
#define EFI_VARIABLE_RUNTIME_ACCESS ((uint64_t)0x0000000000000004)
@@ -696,13 +686,101 @@ extern "C" {
696686
#define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ((uint64_t)0x0000000000000020)
697687
#define EFI_VARIABLE_APPEND_WRITE ((uint64_t)0x0000000000000040)
698688
#define EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS ((uint64_t)0x0000000000000080)
699-
}
700689

701690
#include <QByteArray>
702691
#include <QFile>
703692
#include <QFileInfo>
704693

705694
/* Get rid of efivar */
695+
#if defined(Q_OS_FREEBSD)
696+
697+
extern "C" {
698+
// Use FreeBSD system-level libefivar
699+
#include <efivar.h>
700+
}
701+
702+
int qefivar_variables_supported(void)
703+
{
704+
return efi_variables_supported();
705+
}
706+
707+
static int qefivar_get_variable_size(const QUuid &uuid, const QString &name, size_t *size)
708+
{
709+
int return_code;
710+
711+
std::string std_name = name.toStdString();
712+
const char *c_name = std_name.c_str();
713+
std::string std_uuid = uuid.toString(QUuid::WithoutBraces).toStdString();
714+
const char *c_uuid = std_uuid.c_str();
715+
716+
efi_guid_t guid;
717+
return_code = efi_str_to_guid(c_uuid, &guid);
718+
if (return_code != 0)
719+
{
720+
return return_code;
721+
}
722+
723+
return efi_get_variable_size(guid, c_name, size);
724+
}
725+
726+
static int qefivar_get_variable(QUuid &uuid, QString &name, uint8_t **data, size_t *size, uint32_t *attributes)
727+
{
728+
int return_code;
729+
730+
std::string std_name = name.toStdString();
731+
const char *c_name = std_name.c_str();
732+
std::string std_uuid = uuid.toString(QUuid::WithoutBraces).toStdString();
733+
const char *c_uuid = std_uuid.c_str();
734+
735+
efi_guid_t guid;
736+
return_code = efi_str_to_guid(c_uuid, &guid);
737+
if (return_code != 0)
738+
{
739+
return return_code;
740+
}
741+
742+
return_code = efi_get_variable_size(guid, c_name, size);
743+
if (size == 0 || return_code != 0)
744+
{
745+
return return_code;
746+
}
747+
748+
return efi_get_variable(guid, c_name, data, size, attributes);
749+
}
750+
751+
static int qefi_set_variable(const QUuid &uuid, const QString &name, const uint8_t *data,
752+
size_t data_size, uint32_t attributes, mode_t mode)
753+
{
754+
int return_code;
755+
756+
std::string std_name = name.toStdString();
757+
const char *c_name = std_name.c_str();
758+
std::string std_uuid = uuid.toString(QUuid::WithoutBraces).toStdString();
759+
const char *c_uuid = std_uuid.c_str();
760+
761+
efi_guid_t guid;
762+
return_code = efi_str_to_guid(c_uuid, &guid);
763+
if (return_code != 0)
764+
{
765+
return;
766+
}
767+
768+
// Arg "mode" is not supported here
769+
return efi_set_variable(guid, c_name, data, data_size, attributes);
770+
}
771+
772+
#else
773+
extern "C" {
774+
#include <fcntl.h>
775+
#include <stdio.h>
776+
#include <unistd.h>
777+
#include <stdlib.h>
778+
779+
#include <sys/ioctl.h>
780+
#include <sys/stat.h>
781+
#include <sys/types.h>
782+
}
783+
706784
static QString const default_efivarfs_path = QStringLiteral("/sys/firmware/efi/efivars/");
707785
static QString efivarfs_path;
708786

@@ -727,7 +805,7 @@ static QString get_efivarfs_path(void)
727805
return efivarfs_path;
728806
}
729807

730-
int efi_variables_supported(void)
808+
int qefivar_variables_supported(void)
731809
{
732810
QFileInfo fileInfo(get_efivarfs_path());
733811
if (!fileInfo.exists() || !fileInfo.isDir())
@@ -758,6 +836,11 @@ static int qefivar_efivarfs_get_variable_size(const QUuid &guid, const QString &
758836
return ret;
759837
}
760838

839+
static int inline qefivar_get_variable_size(const QUuid &guid, const QString &name, size_t *size)
840+
{
841+
return qefivar_efivarfs_get_variable_size(guid, name, size);
842+
}
843+
761844
static int qefivar_efivarfs_get_variable(QUuid &guid, QString &name, uint8_t **data, size_t *size, uint32_t *attributes)
762845
{
763846
int ret = -1;
@@ -799,6 +882,11 @@ static int qefivar_efivarfs_get_variable(QUuid &guid, QString &name, uint8_t **d
799882
return ret;
800883
}
801884

885+
static int inline qefivar_get_variable(QUuid &guid, QString &name, uint8_t **data, size_t *size, uint32_t *attributes)
886+
{
887+
return qefivar_efivarfs_get_variable(guid, name, data, size, attributes);
888+
}
889+
802890
static int
803891
qefi_efivarfs_del_variable(const QUuid &guid, const QString &name)
804892
{
@@ -867,11 +955,19 @@ qefi_efivarfs_set_variable(const QUuid &guid, const QString &name, const uint8_t
867955
errno = errno_value;
868956
return ret;
869957
}
958+
959+
static inline int
960+
qefi_set_variable(const QUuid &guid, const QString &name, const uint8_t *data,
961+
size_t data_size, uint32_t attributes, mode_t mode)
962+
{
963+
return qefi_efivarfs_set_variable(guid, name, data, data_size, attributes, mode);
964+
}
965+
#endif
870966
/* End: Get rid of efivar */
871967

872968
bool qefi_is_available()
873969
{
874-
return efi_variables_supported();
970+
return qefivar_variables_supported();
875971
}
876972

877973
bool qefi_has_privilege()
@@ -884,15 +980,15 @@ quint16 qefi_get_variable_uint16(QUuid uuid, QString name)
884980
{
885981
int return_code;
886982
size_t var_size;
887-
return_code = qefivar_efivarfs_get_variable_size(uuid, name, &var_size);
983+
return_code = qefivar_get_variable_size(uuid, name, &var_size);
888984
if (var_size == 0 || return_code != 0)
889985
{
890986
return 0;
891987
}
892988

893989
uint8_t *data;
894990
uint32_t attributes;
895-
return_code = qefivar_efivarfs_get_variable(uuid, name, &data, &var_size, &attributes);
991+
return_code = qefivar_get_variable(uuid, name, &data, &var_size, &attributes);
896992

897993
quint16 value;
898994
if (return_code != 0)
@@ -914,15 +1010,15 @@ QByteArray qefi_get_variable(QUuid uuid, QString name)
9141010
int return_code;
9151011

9161012
size_t var_size;
917-
return_code = qefivar_efivarfs_get_variable_size(uuid, name, &var_size);
1013+
return_code = qefivar_get_variable_size(uuid, name, &var_size);
9181014
if (var_size == 0 || return_code != 0)
9191015
{
9201016
return QByteArray();
9211017
}
9221018

9231019
uint8_t *data;
9241020
uint32_t attributes;
925-
return_code = qefivar_efivarfs_get_variable(uuid, name, &data, &var_size, &attributes);
1021+
return_code = qefivar_get_variable(uuid, name, &data, &var_size, &attributes);
9261022

9271023
QByteArray value;
9281024
if (return_code != 0)
@@ -951,7 +1047,7 @@ void qefi_set_variable_uint16(QUuid uuid, QString name, quint16 value)
9511047

9521048
uint8_t buffer[2];
9531049
*((uint16_t *)buffer) = qToLittleEndian<quint16>(value);
954-
return_code = qefi_efivarfs_set_variable(uuid, name, buffer, 2,
1050+
return_code = qefi_set_variable(uuid, name, buffer, 2,
9551051
default_write_attribute,
9561052
0644);
9571053

@@ -962,7 +1058,7 @@ void qefi_set_variable(QUuid uuid, QString name, QByteArray value)
9621058
{
9631059
int return_code;
9641060

965-
return_code = qefi_efivarfs_set_variable(uuid, name, (uint8_t *)value.data(), value.size(),
1061+
return_code = qefi_set_variable(uuid, name, (uint8_t *)value.data(), value.size(),
9661062
default_write_attribute,
9671063
0644);
9681064

0 commit comments

Comments
 (0)