2 efivars_proc.[ch] - Manipulates EFI variables as exported in /proc/efi/vars
4 Copyright (C) 2001,2003 Dell Computer Corporation <Matt_Domsch@dell.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <linux/sockios.h>
38 #include <linux/ethtool.h>
41 #include "scsi_ioctls.h"
43 #include "efibootmgr.h"
44 #include "efivars_procfs.h"
45 #include "efivars_sysfs.h"
48 static struct efivar_kernel_calls *fs_kernel_calls;
51 load_option_path(EFI_LOAD_OPTION *option)
53 char *p = (char *) option;
54 return (EFI_DEVICE_PATH *)
55 (p + sizeof(uint32_t) /* Attributes */
56 + sizeof(uint16_t) /* FilePathListLength*/
57 + efichar_strsize(option->description)); /* Description */
61 efi_guid_unparse(efi_guid_t *guid, char *out)
63 sprintf(out, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
64 guid->b[3], guid->b[2], guid->b[1], guid->b[0],
65 guid->b[5], guid->b[4], guid->b[7], guid->b[6],
66 guid->b[8], guid->b[9], guid->b[10], guid->b[11],
67 guid->b[12], guid->b[13], guid->b[14], guid->b[15]);
76 snprintf(name, PATH_MAX, "%s", SYSFS_DIR_EFI_VARS);
80 fs_kernel_calls = &sysfs_kernel_calls;
84 snprintf(name, PATH_MAX, "%s", PROCFS_DIR_EFI_VARS);
88 fs_kernel_calls = &procfs_kernel_calls;
91 fprintf(stderr, "Fatal: Couldn't open either sysfs or procfs directories for accessing EFI variables.\n");
92 fprintf(stderr, "Try 'modprobe efivars' as root.\n");
99 write_variable_to_file(efi_variable_t *var)
101 int fd, byteswritten;
102 if (!var || !opts.testfile) return EFI_INVALID_PARAMETER;
104 printf("Test mode: Writing to %s\n", opts.testfile);
105 fd = creat(opts.testfile, S_IRWXU);
107 perror("Couldn't write to testfile");
108 return EFI_INVALID_PARAMETER;
111 byteswritten = write(fd, var, sizeof(*var));
112 if (byteswritten == -1) {
113 perror("Writing to testfile");
121 read_variable(const char *name, efi_variable_t *var)
123 if (!name || !var) return EFI_INVALID_PARAMETER;
124 return fs_kernel_calls->read(name, var);
128 create_variable(efi_variable_t *var)
130 if (!var) return EFI_INVALID_PARAMETER;
131 if (opts.testfile) return write_variable_to_file(var);
132 return fs_kernel_calls->create(var);
136 delete_variable(efi_variable_t *var)
138 if (!var) return EFI_INVALID_PARAMETER;
139 if (opts.testfile) return write_variable_to_file(var);
140 return fs_kernel_calls->delete(var);
145 edit_variable(efi_variable_t *var)
148 if (!var) return EFI_INVALID_PARAMETER;
149 if (opts.testfile) return write_variable_to_file(var);
151 variable_to_name(var, name);
152 return fs_kernel_calls->edit(name, var);
156 create_or_edit_variable(efi_variable_t *var)
158 efi_variable_t testvar;
161 memcpy(&testvar, var, sizeof(*var));
162 variable_to_name(var, name);
164 if (read_variable(name, &testvar) == EFI_SUCCESS)
165 return edit_variable(var);
167 return create_variable(var);
171 select_boot_var_names(const struct dirent *d)
173 if (!strncmp(d->d_name, "Boot", 4) &&
174 isxdigit(d->d_name[4]) && isxdigit(d->d_name[5]) &&
175 isxdigit(d->d_name[6]) && isxdigit(d->d_name[7]) &&
182 read_boot_var_names(struct dirent ***namelist)
184 if (!fs_kernel_calls || !namelist) return -1;
185 return scandir(fs_kernel_calls->path,
186 namelist, select_boot_var_names,
196 efi_guid_t guid = BLKX_UNKNOWN_GUID;
197 char name[80], text_guid[40];
198 ACPI_DEVICE_PATH *path = (ACPI_DEVICE_PATH *)&(var.Data);
201 /* Allow global user option override */
203 switch (opts.edd_version)
205 case 0: /* No EDD information */
208 case 1: /* EDD 1.0 */
211 case 3: /* EDD 3.0 */
219 memset(&var, 0, sizeof(efi_variable_t));
220 efi_guid_unparse(&guid, text_guid);
221 sprintf(name, "blk0-%s", text_guid);
223 status = read_variable(name, &var);
224 if (status != EFI_SUCCESS) {
227 if (path->type == 2 && path->subtype == 1) rc = 3;
233 EFI_DEVICE_PATH, 0x01 (Hardware), 0x04 (Vendor), length 0x0018
234 This needs to know what EFI device has the boot device.
237 make_edd10_device_path(void *dest, uint32_t hardware_device)
239 VENDOR_DEVICE_PATH *hw;
240 char buffer[EDD10_HARDWARE_VENDOR_PATH_LENGTH];
241 efi_guid_t guid = EDD10_HARDWARE_VENDOR_PATH_GUID;
243 memset(buffer, 0, sizeof(buffer));
244 hw = (VENDOR_DEVICE_PATH *)buffer;
245 data = (uint32_t *)hw->data;
246 hw->type = 0x01; /* Hardware Device Path */
247 hw->subtype = 0x04; /* Vendor */
248 hw->length = EDD10_HARDWARE_VENDOR_PATH_LENGTH;
249 memcpy(&(hw->vendor_guid), &guid, sizeof(guid));
250 *data = hardware_device;
251 memcpy(dest, buffer, hw->length);
256 make_end_device_path(void *dest)
259 memset(&p, 0, sizeof(p));
260 p.type = 0x7F; /* End of Hardware Device Path */
261 p.subtype = 0xFF; /* End Entire Device Path */
262 p.length = sizeof(p);
263 memcpy(dest, &p, p.length);
268 make_acpi_device_path(void *dest, uint32_t _HID, uint32_t _UID)
271 memset(&p, 0, sizeof(p));
274 p.length = sizeof(p);
277 memcpy(dest, &p, p.length);
282 make_mac_addr_device_path(void *dest, char *mac, uint8_t iftype)
286 MAC_ADDR_DEVICE_PATH p;
287 memset(&p, 0, sizeof(p));
290 p.length = sizeof(p);
291 for (i=0; i < 14; i++) {
292 p.macaddr[i] = mac[i];
295 memcpy(dest, &p, p.length);
301 struct pci_dev *pci_dev;
302 struct list_head node;
305 static struct device *
306 is_parent_bridge(struct pci_dev *p, unsigned int target_bus)
309 unsigned int primary, secondary;
311 if ( (pci_read_word(p, PCI_HEADER_TYPE) & 0x7f) != PCI_HEADER_TYPE_BRIDGE)
314 primary=pci_read_byte(p, PCI_PRIMARY_BUS);
315 secondary=pci_read_byte(p, PCI_SECONDARY_BUS);
318 if (secondary != target_bus)
321 d = malloc(sizeof(struct device));
324 memset(d, 0, sizeof(*d));
325 INIT_LIST_HEAD(&d->node);
332 static struct device *
333 find_parent(struct pci_access *pacc, unsigned int target_bus)
338 for (p=pacc->devices; p; p=p->next) {
339 dev = is_parent_bridge(p, target_bus);
347 make_one_pci_device_path(void *dest, uint8_t device, uint8_t function)
350 memset(&p, 0, sizeof(p));
353 p.length = sizeof(p);
355 p.function = function;
356 memcpy(dest, &p, p.length);
361 make_pci_device_path(void *dest, uint8_t bus, uint8_t device, uint8_t function)
364 struct pci_access *pacc;
365 struct list_head *pos, *n;
366 LIST_HEAD(pci_parent_list);
377 dev = find_parent(pacc, bus);
379 list_add(&pci_parent_list, &dev->node);
380 bus = dev->pci_dev->bus;
382 } while (dev && bus);
385 list_for_each_safe(pos, n, &pci_parent_list) {
386 dev = list_entry(pos, struct device, node);
387 p += make_one_pci_device_path(p,
390 list_del(&dev->node);
394 p += make_one_pci_device_path(p, device, function);
398 return ((void *)p - dest);
402 make_scsi_device_path(void *dest, uint16_t id, uint16_t lun)
405 memset(&p, 0, sizeof(p));
408 p.length = sizeof(p);
411 memcpy(dest, &p, p.length);
416 make_harddrive_device_path(void *dest, uint32_t num, uint64_t start, uint64_t size,
418 uint8_t mbr_type, uint8_t signature_type)
420 HARDDRIVE_DEVICE_PATH p;
421 memset(&p, 0, sizeof(p));
424 p.length = sizeof(p);
428 if (signature) memcpy(p.signature, signature, 16);
429 p.mbr_type = mbr_type;
430 p.signature_type = signature_type;
431 memcpy(dest, &p, p.length);
436 make_file_path_device_path(void *dest, efi_char16_t *name)
438 FILE_PATH_DEVICE_PATH *p;
440 int namelen = efichar_strlen(name, -1);
441 int namesize = efichar_strsize(name);
443 memset(buffer, 0, sizeof(buffer));
444 p = (FILE_PATH_DEVICE_PATH *)buffer;
447 p->length = 4 + namesize;
448 efichar_strncpy(p->path_name,
451 memcpy(dest, buffer, p->length);
459 make_edd30_device_path(int fd, void *buffer)
462 unsigned char bus=0, device=0, function=0;
464 unsigned char host=0, channel=0, id=0, lun=0;
468 rc = disk_get_pci(fd, &bus, &device, &function);
471 memset(&idlun, 0, sizeof(idlun));
472 rc = get_scsi_idlun(fd, &idlun);
474 idlun_to_components(&idlun, &host, &channel, &id, &lun);
476 p += make_acpi_device_path (p, EISAID_PNP0A03, bus);
477 p += make_pci_device_path (p, bus, device, function);
478 p += make_scsi_device_path (p, id, lun);
479 return ((void *)p - buffer);
483 * make_disk_load_option()
486 * Returns 0 on error, length of load option created on success.
488 char *make_disk_load_option(char *p, char *disk)
493 int rc, edd_version=0;
494 uint8_t mbr_type=0, signature_type=0;
495 uint64_t start=0, size=0;
496 efi_char16_t os_loader_path[40];
498 memset(signature, 0, sizeof(signature));
500 disk_fd = open(opts.disk, O_RDWR);
502 sprintf(buffer, "Could not open disk %s", opts.disk);
507 if (opts.edd_version) {
508 edd_version = get_edd_version();
510 if (edd_version == 3) {
511 p += make_edd30_device_path(disk_fd, p);
513 else if (edd_version == 1) {
514 p += make_edd10_device_path(p, opts.edd10_devicenum);
518 rc = disk_get_partition_info (disk_fd, opts.part,
519 &start, &size, signature,
520 &mbr_type, &signature_type);
525 fprintf(stderr, "Error: no partition information on disk %s.\n"
526 " Cowardly refusing to create a boot option.\n",
531 p += make_harddrive_device_path (p, opts.part,
534 mbr_type, signature_type);
536 efichar_from_char(os_loader_path, opts.loader, sizeof(os_loader_path));
537 p += make_file_path_device_path (p, os_loader_path);
538 p += make_end_device_path (p);
544 * make_net_load_option()
545 * @data - load option returned
547 * Returns NULL on error, or p advanced by length of load option
548 * created on success.
550 char *make_net_load_option(char *p, char *iface)
552 /* copied pretty much verbatim from the ethtool source */
556 struct ethtool_drvinfo drvinfo;
558 memset(&ifr, 0, sizeof(ifr));
559 strcpy(ifr.ifr_name, iface);
560 drvinfo.cmd = ETHTOOL_GDRVINFO;
561 ifr.ifr_data = (caddr_t)&drvinfo;
562 /* Open control socket */
563 fd = socket(AF_INET, SOCK_DGRAM, 0);
565 perror("Cannot get control socket");
568 err = ioctl(fd, SIOCETHTOOL, &ifr);
570 perror("Cannot get driver information");
574 /* The domain part was added in 2.6 kernels. Test for that first. */
575 err = sscanf(drvinfo.bus_info, "%*x:%2x:%2x.%x", &bus, &slot, &func);
577 err = sscanf(drvinfo.bus_info, "%2x:%2x.%x", &bus, &slot, &func);
579 perror("Couldn't parse device location string.");
584 err = ioctl(fd, SIOCGIFHWADDR, &ifr);
586 perror("Cannot get hardware address.");
590 p += make_acpi_device_path(p, opts.acpi_hid, opts.acpi_uid);
591 p += make_pci_device_path(p, bus, (uint8_t)slot, (uint8_t)func);
592 p += make_mac_addr_device_path(p, ifr.ifr_ifru.ifru_hwaddr.sa_data, 0);
593 p += make_end_device_path (p);
600 * make_linux_load_option()
601 * @data - load option returned
603 * Returns 0 on error, length of load option created on success.
606 make_linux_load_option(void *data)
608 EFI_LOAD_OPTION *load_option = data;
610 efi_char16_t description[64];
611 unsigned long datasize=0;
613 /* Write Attributes */
614 if (opts.active) load_option->attributes = LOAD_OPTION_ACTIVE;
615 else load_option->attributes = 0;
617 p += sizeof(uint32_t);
618 /* skip writing file_path_list_length */
619 p += sizeof(uint16_t);
620 /* Write description. This is the text that appears on the screen for the load option. */
621 memset(description, 0, sizeof(description));
622 efichar_from_char(description, opts.label, sizeof(description));
623 efichar_strncpy(load_option->description, description, sizeof(description));
624 p += efichar_strsize(load_option->description);
629 p = (char *)make_net_load_option(p, opts.iface);
632 p = (char *)make_disk_load_option(p, opts.iface);
637 load_option->file_path_list_length = p - q;
639 datasize = (uint8_t *)p - (uint8_t *)data;
644 * append_extra_args()
645 * appends all arguments from argv[] not snarfed by getopt
646 * as one long string onto data, up to maxchars. allow for nulls
650 append_extra_args_ascii(void *data, unsigned long maxchars)
654 unsigned long usedchars=0;
658 for (i=opts.optind; i < opts.argc && usedchars < maxchars; i++) {
659 p = strncpy(p, opts.argv[i], maxchars-usedchars-1);
663 usedchars = p - (char *)data;
665 /* Put a space between args */
666 if (i < (opts.argc-1)) {
668 p = strncpy(p, " ", maxchars-usedchars-1);
670 usedchars = p - (char *)data;
674 /* Remember the NULL */
675 if (appended) return strlen(data) + 1;
680 append_extra_args_unicode(void *data, unsigned long maxchars)
684 unsigned long usedchars=0;
688 for (i=opts.optind; i < opts.argc && usedchars < maxchars; i++) {
689 p += efichar_from_char((efi_char16_t *)p, opts.argv[i],
691 usedchars = efichar_strsize(data) - sizeof(efi_char16_t);
694 /* Put a space between args */
695 if (i < (opts.argc-1)) {
696 p += efichar_from_char((efi_char16_t *)p, " ",
698 usedchars = efichar_strsize(data) -
699 sizeof(efi_char16_t);
703 if (appended) return efichar_strsize( (efi_char16_t *)data );
709 append_extra_args(void *data, unsigned long maxchars)
712 return append_extra_args_unicode(data, maxchars);
714 return append_extra_args_ascii(data, maxchars);
720 make_linux_efi_variable(efi_variable_t *var,
721 unsigned int free_number)
723 efi_guid_t guid = EFI_GLOBAL_VARIABLE;
725 unsigned char *optional_data=NULL;
726 unsigned long load_option_size = 0, opt_data_size=0;
728 memset(buffer, 0, sizeof(buffer));
730 /* VariableName needs to be BootXXXX */
731 sprintf(buffer, "Boot%04X", free_number);
733 efichar_from_char(var->VariableName, buffer, 1024);
735 memcpy(&(var->VendorGuid), &guid, sizeof(guid));
737 EFI_VARIABLE_NON_VOLATILE |
738 EFI_VARIABLE_BOOTSERVICE_ACCESS |
739 EFI_VARIABLE_RUNTIME_ACCESS;
741 /* Set Data[] and DataSize */
743 load_option_size = make_linux_load_option(var->Data);
745 if (!load_option_size) return 0;
747 /* Set OptionalData (passed as binary to the called app) */
748 optional_data = var->Data + load_option_size;
749 opt_data_size = append_extra_args(optional_data,
750 sizeof(var->Data) - load_option_size);
751 var->DataSize = load_option_size + opt_data_size;
752 return var->DataSize;
757 variable_to_name(efi_variable_t *var, char *name)
760 efichar_to_char(p, var->VariableName, PATH_MAX);
762 p += sprintf(p, "-");
763 efi_guid_unparse(&var->VendorGuid, p);