@@ -677,17 +677,10 @@ void qefi_set_variable(QUuid uuid, QString name, QByteArray value)
677
677
}
678
678
679
679
#else
680
- /* Implementation based on libefivar */
681
680
extern " C" {
682
- #include < fcntl.h>
683
- #include < stdio.h>
684
681
#include < unistd.h>
685
- #include < stdlib.h>
686
-
687
- #include < sys/ioctl.h>
688
- #include < sys/stat.h>
689
- #include < sys/types.h>
690
-
682
+ }
683
+ /* Implementation based on libefivar */
691
684
#define EFI_VARIABLE_NON_VOLATILE ((uint64_t )0x0000000000000001 )
692
685
#define EFI_VARIABLE_BOOTSERVICE_ACCESS ((uint64_t )0x0000000000000002 )
693
686
#define EFI_VARIABLE_RUNTIME_ACCESS ((uint64_t )0x0000000000000004 )
@@ -696,13 +689,123 @@ extern "C" {
696
689
#define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS ((uint64_t )0x0000000000000020 )
697
690
#define EFI_VARIABLE_APPEND_WRITE ((uint64_t )0x0000000000000040 )
698
691
#define EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS ((uint64_t )0x0000000000000080 )
699
- }
700
692
701
693
#include < QByteArray>
702
694
#include < QFile>
703
695
#include < QFileInfo>
704
696
705
697
/* Get rid of efivar */
698
+ #if defined(Q_OS_FREEBSD)
699
+
700
+ extern " C" {
701
+ // Use FreeBSD system-level libefivar
702
+ #include < efivar.h>
703
+ }
704
+ #include < iostream>
705
+
706
+ int qefivar_variables_supported (void )
707
+ {
708
+ return efi_variables_supported ();
709
+ }
710
+
711
+ static int qefivar_get_variable_size (const QUuid &uuid, const QString &name, size_t *size)
712
+ {
713
+ int return_code;
714
+
715
+ std::string std_name = name.toStdString ();
716
+ const char *c_name = std_name.c_str ();
717
+ std::string std_uuid = uuid.toString (QUuid::WithoutBraces).toStdString ();
718
+ const char *c_uuid = std_uuid.c_str ();
719
+
720
+ efi_guid_t guid;
721
+ return_code = efi_str_to_guid (c_uuid, &guid);
722
+ if (return_code < 0 )
723
+ {
724
+ return return_code;
725
+ }
726
+ return_code = efi_get_variable_size (guid, c_name, size);
727
+
728
+ return 0 ;
729
+ }
730
+
731
+ static int qefivar_get_variable (QUuid &uuid, QString &name, uint8_t **data, size_t *size, uint32_t *attributes)
732
+ {
733
+ int return_code;
734
+
735
+ std::string std_name = name.toStdString ();
736
+ const char *c_name = std_name.c_str ();
737
+ std::string std_uuid = uuid.toString (QUuid::WithoutBraces).toStdString ();
738
+ const char *c_uuid = std_uuid.c_str ();
739
+
740
+ efi_guid_t guid;
741
+ return_code = efi_str_to_guid (c_uuid, &guid);
742
+ if (return_code < 0 )
743
+ {
744
+ return return_code;
745
+ }
746
+
747
+ return_code = efi_get_variable_size (guid, c_name, size);
748
+ if (*size == 0 || return_code < 0 )
749
+ {
750
+ return return_code;
751
+ }
752
+
753
+ uint8_t *temp_data;
754
+ return_code = efi_get_variable (guid, c_name, &temp_data, size, attributes);
755
+ if (*size == 0 || return_code < 0 )
756
+ {
757
+ return return_code;
758
+ }
759
+ // Allocate to have the same behaviour with Linux efivar
760
+ *data = (uint8_t *)malloc (*size);
761
+ std::memcpy (*data, temp_data, *size);
762
+
763
+ if (return_code < 0 )
764
+ {
765
+ return return_code;
766
+ }
767
+ return 0 ;
768
+ }
769
+
770
+ static int qefivar_set_variable (const QUuid &uuid, const QString &name, uint8_t *data,
771
+ size_t data_size, uint32_t attributes, mode_t mode)
772
+ {
773
+ int return_code;
774
+
775
+ std::string std_name = name.toStdString ();
776
+ const char *c_name = std_name.c_str ();
777
+ std::string std_uuid = uuid.toString (QUuid::WithoutBraces).toStdString ();
778
+ const char *c_uuid = std_uuid.c_str ();
779
+
780
+ efi_guid_t guid;
781
+ return_code = efi_str_to_guid (c_uuid, &guid);
782
+ if (return_code < 0 )
783
+ {
784
+ return return_code;
785
+ }
786
+
787
+ // Arg "mode" is not supported here
788
+ return_code = efi_set_variable (guid, c_name, data, data_size, attributes);
789
+
790
+ if (return_code < 0 )
791
+ {
792
+ return return_code;
793
+ }
794
+
795
+ return 0 ;
796
+ }
797
+
798
+ #else
799
+ extern " C" {
800
+ #include < fcntl.h>
801
+ #include < stdio.h>
802
+ #include < stdlib.h>
803
+
804
+ #include < sys/ioctl.h>
805
+ #include < sys/stat.h>
806
+ #include < sys/types.h>
807
+ }
808
+
706
809
static QString const default_efivarfs_path = QStringLiteral(" /sys/firmware/efi/efivars/" );
707
810
static QString efivarfs_path;
708
811
@@ -727,7 +830,7 @@ static QString get_efivarfs_path(void)
727
830
return efivarfs_path;
728
831
}
729
832
730
- int efi_variables_supported (void )
833
+ int qefivar_variables_supported (void )
731
834
{
732
835
QFileInfo fileInfo (get_efivarfs_path ());
733
836
if (!fileInfo.exists () || !fileInfo.isDir ())
@@ -758,6 +861,11 @@ static int qefivar_efivarfs_get_variable_size(const QUuid &guid, const QString &
758
861
return ret;
759
862
}
760
863
864
+ static int inline qefivar_get_variable_size (const QUuid &guid, const QString &name, size_t *size)
865
+ {
866
+ return qefivar_efivarfs_get_variable_size (guid, name, size);
867
+ }
868
+
761
869
static int qefivar_efivarfs_get_variable (QUuid &guid, QString &name, uint8_t **data, size_t *size, uint32_t *attributes)
762
870
{
763
871
int ret = -1 ;
@@ -799,8 +907,13 @@ static int qefivar_efivarfs_get_variable(QUuid &guid, QString &name, uint8_t **d
799
907
return ret;
800
908
}
801
909
910
+ static int inline qefivar_get_variable (QUuid &guid, QString &name, uint8_t **data, size_t *size, uint32_t *attributes)
911
+ {
912
+ return qefivar_efivarfs_get_variable (guid, name, data, size, attributes);
913
+ }
914
+
802
915
static int
803
- qefi_efivarfs_del_variable (const QUuid &guid, const QString &name)
916
+ qefivar_efivarfs_del_variable (const QUuid &guid, const QString &name)
804
917
{
805
918
const QString &rawPath = make_efivarfs_path (guid, name);
806
919
const char *path = rawPath.toLocal8Bit ().constData ();
@@ -814,7 +927,7 @@ qefi_efivarfs_del_variable(const QUuid &guid, const QString &name)
814
927
}
815
928
816
929
static int
817
- qefi_efivarfs_set_variable (const QUuid &guid, const QString &name, const uint8_t *data,
930
+ qefivar_efivarfs_set_variable (const QUuid &guid, const QString &name, uint8_t *data,
818
931
size_t data_size, uint32_t attributes, mode_t mode)
819
932
{
820
933
QByteArray buf ((qsizetype)(sizeof (attributes) + data_size), (char )0 );
@@ -841,7 +954,7 @@ qefi_efivarfs_set_variable(const QUuid &guid, const QString &name, const uint8_t
841
954
const char *path = rawPath.toLocal8Bit ().constData ();
842
955
843
956
if (!access (path, F_OK) && !(attributes & EFI_VARIABLE_APPEND_WRITE)) {
844
- rc = qefi_efivarfs_del_variable (guid, name);
957
+ rc = qefivar_efivarfs_del_variable (guid, name);
845
958
if (rc < 0 )
846
959
goto err;
847
960
}
@@ -867,11 +980,19 @@ qefi_efivarfs_set_variable(const QUuid &guid, const QString &name, const uint8_t
867
980
errno = errno_value;
868
981
return ret;
869
982
}
983
+
984
+ static inline int
985
+ qefivar_set_variable (const QUuid &guid, const QString &name, uint8_t *data,
986
+ size_t data_size, uint32_t attributes, mode_t mode)
987
+ {
988
+ return qefivar_efivarfs_set_variable (guid, name, data, data_size, attributes, mode);
989
+ }
990
+ #endif
870
991
/* End: Get rid of efivar */
871
992
872
993
bool qefi_is_available ()
873
994
{
874
- return efi_variables_supported ();
995
+ return qefivar_variables_supported ();
875
996
}
876
997
877
998
bool qefi_has_privilege ()
@@ -884,15 +1005,15 @@ quint16 qefi_get_variable_uint16(QUuid uuid, QString name)
884
1005
{
885
1006
int return_code;
886
1007
size_t var_size;
887
- return_code = qefivar_efivarfs_get_variable_size (uuid, name, &var_size);
1008
+ return_code = qefivar_get_variable_size (uuid, name, &var_size);
888
1009
if (var_size == 0 || return_code != 0 )
889
1010
{
890
1011
return 0 ;
891
1012
}
892
1013
893
1014
uint8_t *data;
894
1015
uint32_t attributes;
895
- return_code = qefivar_efivarfs_get_variable (uuid, name, &data, &var_size, &attributes);
1016
+ return_code = qefivar_get_variable (uuid, name, &data, &var_size, &attributes);
896
1017
897
1018
quint16 value;
898
1019
if (return_code != 0 )
@@ -914,15 +1035,15 @@ QByteArray qefi_get_variable(QUuid uuid, QString name)
914
1035
int return_code;
915
1036
916
1037
size_t var_size;
917
- return_code = qefivar_efivarfs_get_variable_size (uuid, name, &var_size);
1038
+ return_code = qefivar_get_variable_size (uuid, name, &var_size);
918
1039
if (var_size == 0 || return_code != 0 )
919
1040
{
920
1041
return QByteArray ();
921
1042
}
922
1043
923
1044
uint8_t *data;
924
1045
uint32_t attributes;
925
- return_code = qefivar_efivarfs_get_variable (uuid, name, &data, &var_size, &attributes);
1046
+ return_code = qefivar_get_variable (uuid, name, &data, &var_size, &attributes);
926
1047
927
1048
QByteArray value;
928
1049
if (return_code != 0 )
@@ -951,7 +1072,7 @@ void qefi_set_variable_uint16(QUuid uuid, QString name, quint16 value)
951
1072
952
1073
uint8_t buffer[2 ];
953
1074
*((uint16_t *)buffer) = qToLittleEndian<quint16>(value);
954
- return_code = qefi_efivarfs_set_variable (uuid, name, buffer, 2 ,
1075
+ return_code = qefivar_set_variable (uuid, name, buffer, 2 ,
955
1076
default_write_attribute,
956
1077
0644 );
957
1078
@@ -962,7 +1083,7 @@ void qefi_set_variable(QUuid uuid, QString name, QByteArray value)
962
1083
{
963
1084
int return_code;
964
1085
965
- return_code = qefi_efivarfs_set_variable (uuid, name, (uint8_t *)value.data (), value.size (),
1086
+ return_code = qefivar_set_variable (uuid, name, (uint8_t *)value.data (), value.size (),
966
1087
default_write_attribute,
967
1088
0644 );
968
1089
0 commit comments