|
| 1 | +/* |
| 2 | + * |
| 3 | + * This file is provided under a dual BSD/GPLv2 license. When using or |
| 4 | + * redistributing this file, you may do so under either license. |
| 5 | + * |
| 6 | + * GPL LICENSE SUMMARY |
| 7 | + * |
| 8 | + * Copyright(c) 2015 Intel Corporation. |
| 9 | + * |
| 10 | + * This program is free software; you can redistribute it and/or modify |
| 11 | + * it under the terms of version 2 of the GNU General Public License as |
| 12 | + * published by the Free Software Foundation. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, but |
| 15 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + * General Public License for more details. |
| 18 | + * |
| 19 | + * BSD LICENSE |
| 20 | + * |
| 21 | + * Copyright(c) 2015 Intel Corporation. |
| 22 | + * |
| 23 | + * Redistribution and use in source and binary forms, with or without |
| 24 | + * modification, are permitted provided that the following conditions |
| 25 | + * are met: |
| 26 | + * |
| 27 | + * - Redistributions of source code must retain the above copyright |
| 28 | + * notice, this list of conditions and the following disclaimer. |
| 29 | + * - Redistributions in binary form must reproduce the above copyright |
| 30 | + * notice, this list of conditions and the following disclaimer in |
| 31 | + * the documentation and/or other materials provided with the |
| 32 | + * distribution. |
| 33 | + * - Neither the name of Intel Corporation nor the names of its |
| 34 | + * contributors may be used to endorse or promote products derived |
| 35 | + * from this software without specific prior written permission. |
| 36 | + * |
| 37 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 38 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 39 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 40 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 41 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 42 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 43 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 44 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 45 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 46 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 47 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 48 | + * |
| 49 | + */ |
| 50 | + |
| 51 | +#include "efivar.h" |
| 52 | + |
| 53 | +/* GUID for HFI1 variables in EFI */ |
| 54 | +#define HFI1_EFIVAR_GUID EFI_GUID(0xc50a953e, 0xa8b2, 0x42a6, \ |
| 55 | + 0xbf, 0x89, 0xd3, 0x33, 0xa6, 0xe9, 0xe6, 0xd4) |
| 56 | +/* largest EFI data size we expect */ |
| 57 | +#define EFI_DATA_SIZE 4096 |
| 58 | + |
| 59 | +/* |
| 60 | + * Read the named EFI variable. Return the size of the actual data in *size |
| 61 | + * and a kmalloc'ed buffer in *return_data. The caller must free the |
| 62 | + * data. It is guaranteed that *return_data will be NULL and *size = 0 |
| 63 | + * if this routine fails. |
| 64 | + * |
| 65 | + * Return 0 on success, -errno on failure. |
| 66 | + */ |
| 67 | +static int read_efi_var(const char *name, unsigned long *size, |
| 68 | + void **return_data) |
| 69 | +{ |
| 70 | + efi_status_t status; |
| 71 | + efi_char16_t *uni_name; |
| 72 | + efi_guid_t guid; |
| 73 | + unsigned long temp_size; |
| 74 | + void *temp_buffer; |
| 75 | + void *data; |
| 76 | + int i; |
| 77 | + int ret; |
| 78 | + |
| 79 | + /* set failure return values */ |
| 80 | + *size = 0; |
| 81 | + *return_data = NULL; |
| 82 | + |
| 83 | + if (!efi_enabled(EFI_RUNTIME_SERVICES)) |
| 84 | + return -EOPNOTSUPP; |
| 85 | + |
| 86 | + uni_name = kzalloc(sizeof(efi_char16_t) * (strlen(name) + 1), |
| 87 | + GFP_KERNEL); |
| 88 | + temp_buffer = kzalloc(EFI_DATA_SIZE, GFP_KERNEL); |
| 89 | + |
| 90 | + if (!uni_name || !temp_buffer) { |
| 91 | + ret = -ENOMEM; |
| 92 | + goto fail; |
| 93 | + } |
| 94 | + |
| 95 | + /* input: the size of the buffer */ |
| 96 | + temp_size = EFI_DATA_SIZE; |
| 97 | + |
| 98 | + /* convert ASCII to unicode - it is a 1:1 mapping */ |
| 99 | + for (i = 0; name[i]; i++) |
| 100 | + uni_name[i] = name[i]; |
| 101 | + |
| 102 | + /* need a variable for our GUID */ |
| 103 | + guid = HFI1_EFIVAR_GUID; |
| 104 | + |
| 105 | + /* call into EFI runtime services */ |
| 106 | + status = efi.get_variable( |
| 107 | + uni_name, |
| 108 | + &guid, |
| 109 | + NULL, |
| 110 | + &temp_size, |
| 111 | + temp_buffer); |
| 112 | + |
| 113 | + /* |
| 114 | + * It would be nice to call efi_status_to_err() here, but that |
| 115 | + * is in the EFIVAR_FS code and may not be compiled in. |
| 116 | + * However, even that is insufficient since it does not cover |
| 117 | + * EFI_BUFFER_TOO_SMALL which could be an important return. |
| 118 | + * For now, just split out succces or not found. |
| 119 | + */ |
| 120 | + ret = status == EFI_SUCCESS ? 0 : |
| 121 | + status == EFI_NOT_FOUND ? -ENOENT : |
| 122 | + -EINVAL; |
| 123 | + if (ret) |
| 124 | + goto fail; |
| 125 | + |
| 126 | + /* |
| 127 | + * We have successfully read the EFI variable into our |
| 128 | + * temporary buffer. Now allocate a correctly sized |
| 129 | + * buffer. |
| 130 | + */ |
| 131 | + data = kmalloc(temp_size, GFP_KERNEL); |
| 132 | + if (!data) { |
| 133 | + ret = -ENOMEM; |
| 134 | + goto fail; |
| 135 | + } |
| 136 | + |
| 137 | + memcpy(data, temp_buffer, temp_size); |
| 138 | + *size = temp_size; |
| 139 | + *return_data = data; |
| 140 | + |
| 141 | +fail: |
| 142 | + kfree(uni_name); |
| 143 | + kfree(temp_buffer); |
| 144 | + |
| 145 | + return ret; |
| 146 | +} |
| 147 | + |
| 148 | +/* |
| 149 | + * Read an HFI1 EFI variable of the form: |
| 150 | + * <PCIe address>-<kind> |
| 151 | + * Return an kalloc'ed array and size of the data. |
| 152 | + * |
| 153 | + * Returns 0 on success, -errno on failure. |
| 154 | + */ |
| 155 | +int read_hfi1_efi_var(struct hfi1_devdata *dd, const char *kind, |
| 156 | + unsigned long *size, void **return_data) |
| 157 | +{ |
| 158 | + char name[64]; |
| 159 | + |
| 160 | + /* create a common prefix */ |
| 161 | + snprintf(name, sizeof(name), "%04x:%02x:%02x.%x-%s", |
| 162 | + pci_domain_nr(dd->pcidev->bus), |
| 163 | + dd->pcidev->bus->number, |
| 164 | + PCI_SLOT(dd->pcidev->devfn), |
| 165 | + PCI_FUNC(dd->pcidev->devfn), |
| 166 | + kind); |
| 167 | + |
| 168 | + return read_efi_var(name, size, return_data); |
| 169 | +} |
0 commit comments