1919static char * kernel_efi_path = NULL ;
2020
2121void kernel_variable_init (void ) {
22- char fname [] = "/tmp/efi.XXXXXX" ;
23- char cmdline [256 ];
24- int fd , ret ;
25- struct stat st ;
26- char * buf ;
22+ char fname [] = "/tmp/efi.XXXXXX" ;
23+ char cmdline [256 ];
24+ int fd , ret ;
25+ struct stat st ;
26+ char * buf ;
2727
28- if (kernel_efi_path )
29- return ;
30- mkstemp (fname );
31- snprintf (cmdline , sizeof (cmdline ), "mount -l > %s" , fname );
32- ret = system (cmdline );
33- if (WEXITSTATUS (ret ) != 0 )
34- /* hopefully stderr said what was wrong */
35- exit (1 );
36- fd = open (fname , O_RDONLY );
37- unlink (fname );
38- if (fd < 0 ) {
39- fprintf (stderr , "Failed to open output of %s\n" , cmdline );
40- exit (1 );
41- }
42- if (fstat (fd , & st ) < 0 ) {
43- perror ("stat failed" );
44- exit (1 );
45- }
46- if (st .st_size == 0 ) {
47- fprintf (stderr , "No efivarfs filesystem is mounted\n" );
48- exit (1 );
49- }
50- buf = malloc (st .st_size );
51- read (fd , buf , st .st_size );
52- close (fd );
28+ if (kernel_efi_path )
29+ return ;
30+ mkstemp (fname );
31+ snprintf (cmdline , sizeof (cmdline ), "mount -l > %s" , fname );
32+ ret = system (cmdline );
33+ if (WEXITSTATUS (ret ) != 0 )
34+ /* hopefully stderr said what was wrong */
35+ exit (1 );
36+ fd = open (fname , O_RDONLY );
37+ unlink (fname );
38+ if (fd < 0 ) {
39+ fprintf (stderr , "Failed to open output of %s\n" , cmdline );
40+ exit (1 );
41+ }
42+ if (fstat (fd , & st ) < 0 ) {
43+ perror ("stat failed" );
44+ exit (1 );
45+ }
46+ if (st .st_size == 0 ) {
47+ fprintf (stderr , "No efivarfs filesystem is mounted\n" );
48+ exit (1 );
49+ }
50+ buf = malloc (st .st_size );
51+ read (fd , buf , st .st_size );
52+ close (fd );
5353
54- char * ptr = buf ;
55- char path [512 ], type [512 ];
56- while (ptr < buf + st .st_size ) {
57- int count ;
54+ char * ptr = buf ;
55+ char path [512 ], type [512 ];
56+ while (ptr < buf + st .st_size ) {
57+ int count ;
5858
59- sscanf (ptr , "%*s on %s type %s %*[^\n]\n%n" , path , type , & count );
60- ptr += count ;
61- if (strcmp (type , "efivarfs" ) == 0 )
62- break ;
63- }
64- if (strcmp (type , "efivarfs" ) != 0 ) {
65- fprintf (stderr , "No efivarfs filesystem is mounted\n" );
66- exit (1 );
67- }
68- kernel_efi_path = malloc (strlen (path ) + 1 );
69- strcpy (kernel_efi_path , path );
59+ sscanf (ptr , "%*s on %s type %s %*[^\n]\n%n" , path , type , & count );
60+ ptr += count ;
61+ if (strcmp (type , "efivarfs" ) == 0 )
62+ break ;
63+ }
64+ if (strcmp (type , "efivarfs" ) != 0 ) {
65+ fprintf (stderr , "No efivarfs filesystem is mounted\n" );
66+ exit (1 );
67+ }
68+ kernel_efi_path = malloc (strlen (path ) + 1 );
69+ strcpy (kernel_efi_path , path );
7070}
7171
7272int set_variable (
@@ -75,24 +75,25 @@ int set_variable(
7575 uint32_t attributes ,
7676 uint32_t size ,
7777 void * buf ) {
78- if (!kernel_efi_path )
79- return - EINVAL ;
78+ if (!kernel_efi_path )
79+ return - EINVAL ;
8080
81- int varfs_len = strlen (var ) + 48 + strlen (kernel_efi_path );
82- char * varfs = malloc (varfs_len );
83- char * newbuf = malloc (size + sizeof (attributes ));
84- snprintf (varfs , varfs_len , "%s/%s-%s" , kernel_efi_path ,
85- var , guid_to_str (guid ));
86- int fd = open (varfs , O_RDWR |O_CREAT |O_TRUNC , 0644 );
87- free (varfs );
88- if (fd < 0 )
89- return errno ;
90- memcpy (newbuf , & attributes , sizeof (attributes ));
91- memcpy (newbuf + sizeof (attributes ), buf , size );
92-
93- if (write (fd , newbuf , size + sizeof (attributes )) != size + sizeof (attributes ))
94- return errno ;
81+ int varfs_len = strlen (var ) + 48 + strlen (kernel_efi_path );
82+ char * varfs = malloc (varfs_len );
83+ char * newbuf = malloc (size + sizeof (attributes ));
84+ snprintf (varfs , varfs_len , "%s/%s-%s" , kernel_efi_path ,
85+ var , guid_to_str (guid ));
86+ int fd = open (varfs , O_RDWR |O_CREAT |O_TRUNC , 0644 );
87+ free (varfs );
88+ if (fd < 0 )
89+ return errno ;
90+ memcpy (newbuf , & attributes , sizeof (attributes ));
91+ memcpy (newbuf + sizeof (attributes ), buf , size );
92+
93+ ssize_t result = write (fd , newbuf , size + sizeof (attributes ));
94+ if (result != size + sizeof (attributes ))
95+ return errno ;
9596
96- close (fd );
97- return 0 ;
97+ close (fd );
98+ return 0 ;
9899}
0 commit comments