| /* |
| * Copyright 2016 Google Inc. |
| * |
| * See file CREDITS for list of people who contributed to this |
| * project. |
| * |
| * This program is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU General Public License as |
| * published by the Free Software Foundation; either version 2 of |
| * the License, or (at your option) any later version. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but without any warranty; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| * MA 02111-1307 USA |
| */ |
| |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| |
| #include "base/container_of.h" |
| #include "base/fwdb.h" |
| #include "base/xalloc.h" |
| #include "drivers/storage/uefi_nv.h" |
| |
| static EFI_GUID uefi_nv_default_vendor_guid = |
| { |
| 0x52bcc60a, |
| 0x91c7, |
| 0x4602, |
| { 0xa8, 0x7c, 0xc3, 0xbc, 0x7f, 0x89, 0x59, 0x9a } |
| }; |
| |
| |
| static int uefi_nv_storage_write_variable(UefiNvStorage *storage) |
| { |
| EFI_SYSTEM_TABLE *st = uefi_system_table_ptr(); |
| if (!st) |
| return 1; |
| EFI_RUNTIME_SERVICES *rs = st->RuntimeServices; |
| |
| UINTN attributes = |
| EFI_VARIABLE_NON_VOLATILE | |
| EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| EFI_VARIABLE_RUNTIME_ACCESS; |
| |
| // Write with size 0 to erase any existing version of this variable. |
| EFI_STATUS status = rs->SetVariable(storage->name, storage->vendor, |
| 0, 0, (void *)storage->data); |
| if (status != EFI_SUCCESS && status != EFI_NOT_FOUND) { |
| printf("Failed to erase previous version of nvstorage.\n"); |
| return 1; |
| } |
| status = rs->SetVariable(storage->name, storage->vendor, attributes, |
| storage->size, (void *)storage->data); |
| if (status != EFI_SUCCESS) { |
| printf("Error writing NV storage.\n"); |
| return 1; |
| } |
| |
| return 0; |
| } |
| |
| static int uefi_nv_storage_prepare(UefiNvStorage *storage) |
| { |
| EFI_SYSTEM_TABLE *st = uefi_system_table_ptr(); |
| if (!st) |
| return 1; |
| EFI_RUNTIME_SERVICES *rs = st->RuntimeServices; |
| |
| UINTN old_size = 0; |
| EFI_STATUS status = rs->GetVariable(storage->name, storage->vendor, |
| NULL, &old_size, NULL); |
| |
| if (status == EFI_NOT_FOUND || old_size != storage->size) { |
| printf("UEFI NV variable not found or the wrong size.\n"); |
| storage->data = xzalloc(storage->size); |
| |
| if (uefi_nv_storage_write_variable(storage)) |
| return 1; |
| } else { |
| storage->data = xmalloc(storage->size); |
| status = rs->GetVariable(storage->name, storage->vendor, |
| NULL, &old_size, storage->data); |
| } |
| |
| return 0; |
| } |
| |
| |
| static int uefi_nv_storage_read(StorageOps *me, void *buffer, |
| uint64_t offset, size_t size) |
| { |
| UefiNvStorage *storage = container_of(me, UefiNvStorage, ops); |
| |
| if (!storage->data && uefi_nv_storage_prepare(storage)) |
| return 1; |
| |
| if (offset + size > storage->size) { |
| printf("Read beyond the bounds of UEFI NV variable.\n"); |
| return 1; |
| } |
| |
| memcpy(buffer, (uint8_t *)storage->data + offset, size); |
| return 0; |
| } |
| |
| static int uefi_nv_storage_write(StorageOps *me, const void *buffer, |
| uint64_t offset, size_t size) |
| { |
| UefiNvStorage *storage = container_of(me, UefiNvStorage, ops); |
| |
| if (!storage->data && uefi_nv_storage_prepare(storage)) |
| return 1; |
| |
| if (offset + size > storage->size) { |
| printf("Write beyond the bounds of UEFI NV variable.\n"); |
| return 1; |
| } |
| |
| memcpy((uint8_t *)storage->data + offset, buffer, size); |
| return uefi_nv_storage_write_variable(storage); |
| } |
| |
| static int uefi_nv_storage_size(StorageOps *me) |
| { |
| UefiNvStorage *storage = container_of(me, UefiNvStorage, ops); |
| |
| if (!storage->data && uefi_nv_storage_prepare(storage)) |
| return -1; |
| |
| return storage->size; |
| } |
| |
| UefiNvStorage *new_uefi_nv_storage(size_t size, CHAR16 *name, EFI_GUID *vendor) |
| { |
| UefiNvStorage *storage = xzalloc(sizeof(*storage)); |
| |
| storage->ops.read = &uefi_nv_storage_read; |
| storage->ops.write = &uefi_nv_storage_write; |
| storage->ops.size = &uefi_nv_storage_size; |
| |
| storage->size = size; |
| |
| storage->name = name; |
| storage->vendor = vendor ? vendor : &uefi_nv_default_vendor_guid; |
| |
| return storage; |
| } |