2N/A/* Export pnvram and some variables for runtime */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/file.h>
2N/A#include <grub/err.h>
2N/A#include <grub/normal.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/charset.h>
2N/A#include <grub/efiemu/efiemu.h>
2N/A#include <grub/efiemu/runtime.h>
2N/A#include <grub/extcmd.h>
2N/A
2N/A/* Place for final location of variables */
2N/Astatic int nvram_handle = 0;
2N/Astatic int nvramsize_handle = 0;
2N/Astatic int high_monotonic_count_handle = 0;
2N/Astatic int timezone_handle = 0;
2N/Astatic int accuracy_handle = 0;
2N/Astatic int daylight_handle = 0;
2N/A
2N/Astatic grub_size_t nvramsize;
2N/A
2N/A/* Parse signed value */
2N/Astatic int
2N/Agrub_strtosl (const char *arg, char **end, int base)
2N/A{
2N/A if (arg[0] == '-')
2N/A return -grub_strtoul (arg + 1, end, base);
2N/A return grub_strtoul (arg, end, base);
2N/A}
2N/A
2N/Astatic inline int
2N/Ahextoval (char c)
2N/A{
2N/A if (c >= '0' && c <= '9')
2N/A return c - '0';
2N/A if (c >= 'a' && c <= 'z')
2N/A return c - 'a' + 10;
2N/A if (c >= 'A' && c <= 'Z')
2N/A return c - 'A' + 10;
2N/A return 0;
2N/A}
2N/A
2N/Astatic inline grub_err_t
2N/Aunescape (char *in, char *out, char *outmax, int *len)
2N/A{
2N/A char *ptr, *dptr;
2N/A dptr = out;
2N/A for (ptr = in; *ptr && dptr < outmax; )
2N/A if (*ptr == '%' && ptr[1] && ptr[2])
2N/A {
2N/A *dptr = (hextoval (ptr[1]) << 4) | (hextoval (ptr[2]));
2N/A ptr += 3;
2N/A dptr++;
2N/A }
2N/A else
2N/A {
2N/A *dptr = *ptr;
2N/A ptr++;
2N/A dptr++;
2N/A }
2N/A if (dptr == outmax)
2N/A return grub_error (GRUB_ERR_OUT_OF_MEMORY,
2N/A "too many NVRAM variables for reserved variable space."
2N/A " Try increasing EfiEmu.pnvram.size");
2N/A *len = dptr - out;
2N/A return 0;
2N/A}
2N/A
2N/A/* Export stuff for efiemu */
2N/Astatic grub_err_t
2N/Anvram_set (void * data __attribute__ ((unused)))
2N/A{
2N/A const char *env;
2N/A /* Take definitive pointers */
2N/A char *nvram = grub_efiemu_mm_obtain_request (nvram_handle);
2N/A grub_uint32_t *nvramsize_def
2N/A = grub_efiemu_mm_obtain_request (nvramsize_handle);
2N/A grub_uint32_t *high_monotonic_count
2N/A = grub_efiemu_mm_obtain_request (high_monotonic_count_handle);
2N/A grub_int16_t *timezone
2N/A = grub_efiemu_mm_obtain_request (timezone_handle);
2N/A grub_uint8_t *daylight
2N/A = grub_efiemu_mm_obtain_request (daylight_handle);
2N/A grub_uint32_t *accuracy
2N/A = grub_efiemu_mm_obtain_request (accuracy_handle);
2N/A char *nvramptr;
2N/A
2N/A auto int iterate_env (struct grub_env_var *var);
2N/A int iterate_env (struct grub_env_var *var)
2N/A {
2N/A char *guid, *attr, *name, *varname;
2N/A struct efi_variable *efivar;
2N/A int len = 0;
2N/A int i;
2N/A grub_uint64_t guidcomp;
2N/A
2N/A if (grub_memcmp (var->name, "EfiEmu.pnvram.",
2N/A sizeof ("EfiEmu.pnvram.") - 1) != 0)
2N/A return 0;
2N/A
2N/A guid = var->name + sizeof ("EfiEmu.pnvram.") - 1;
2N/A
2N/A attr = grub_strchr (guid, '.');
2N/A if (!attr)
2N/A return 0;
2N/A attr++;
2N/A
2N/A name = grub_strchr (attr, '.');
2N/A if (!name)
2N/A return 0;
2N/A name++;
2N/A
2N/A efivar = (struct efi_variable *) nvramptr;
2N/A if (nvramptr - nvram + sizeof (struct efi_variable) > nvramsize)
2N/A {
2N/A grub_error (GRUB_ERR_OUT_OF_MEMORY,
2N/A "too many NVRAM variables for reserved variable space."
2N/A " Try increasing EfiEmu.pnvram.size");
2N/A return 1;
2N/A }
2N/A
2N/A nvramptr += sizeof (struct efi_variable);
2N/A
2N/A efivar->guid.data1 = grub_cpu_to_le32 (grub_strtoul (guid, &guid, 16));
2N/A if (*guid != '-')
2N/A return 0;
2N/A guid++;
2N/A
2N/A efivar->guid.data2 = grub_cpu_to_le16 (grub_strtoul (guid, &guid, 16));
2N/A if (*guid != '-')
2N/A return 0;
2N/A guid++;
2N/A
2N/A efivar->guid.data3 = grub_cpu_to_le16 (grub_strtoul (guid, &guid, 16));
2N/A if (*guid != '-')
2N/A return 0;
2N/A guid++;
2N/A
2N/A guidcomp = grub_strtoull (guid, 0, 16);
2N/A for (i = 0; i < 8; i++)
2N/A efivar->guid.data4[i] = (guidcomp >> (56 - 8 * i)) & 0xff;
2N/A
2N/A efivar->attributes = grub_strtoull (attr, 0, 16);
2N/A
2N/A varname = grub_malloc (grub_strlen (name) + 1);
2N/A if (! varname)
2N/A return 1;
2N/A
2N/A if (unescape (name, varname, varname + grub_strlen (name) + 1, &len))
2N/A return 1;
2N/A
2N/A len = grub_utf8_to_utf16 ((grub_uint16_t *) nvramptr,
2N/A (nvramsize - (nvramptr - nvram)) / 2,
2N/A (grub_uint8_t *) varname, len, NULL);
2N/A
2N/A if (len < 0)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_ARGUMENT, "broken UTF-8 in variable name");
2N/A return 1;
2N/A }
2N/A
2N/A nvramptr += 2 * len;
2N/A *((grub_uint16_t *) nvramptr) = 0;
2N/A nvramptr += 2;
2N/A efivar->namelen = 2 * len + 2;
2N/A
2N/A if (unescape (var->value, nvramptr, nvram + nvramsize, &len))
2N/A {
2N/A efivar->namelen = 0;
2N/A return 1;
2N/A }
2N/A
2N/A nvramptr += len;
2N/A
2N/A efivar->size = len;
2N/A
2N/A return 0;
2N/A }
2N/A
2N/A /* Copy to definitive loaction */
2N/A grub_dprintf ("efiemu", "preparing pnvram\n");
2N/A
2N/A env = grub_env_get ("EfiEmu.pnvram.high_monotonic_count");
2N/A *high_monotonic_count = env ? grub_strtoul (env, 0, 0) : 1;
2N/A env = grub_env_get ("EfiEmu.pnvram.timezone");
2N/A *timezone = env ? grub_strtosl (env, 0, 0) : GRUB_EFI_UNSPECIFIED_TIMEZONE;
2N/A env = grub_env_get ("EfiEmu.pnvram.accuracy");
2N/A *accuracy = env ? grub_strtoul (env, 0, 0) : 50000000;
2N/A env = grub_env_get ("EfiEmu.pnvram.daylight");
2N/A *daylight = env ? grub_strtoul (env, 0, 0) : 0;
2N/A
2N/A nvramptr = nvram;
2N/A grub_memset (nvram, 0, nvramsize);
2N/A grub_env_iterate (iterate_env);
2N/A if (grub_errno)
2N/A return grub_errno;
2N/A *nvramsize_def = nvramsize;
2N/A
2N/A /* Register symbols */
2N/A grub_efiemu_register_symbol ("efiemu_variables", nvram_handle, 0);
2N/A grub_efiemu_register_symbol ("efiemu_varsize", nvramsize_handle, 0);
2N/A grub_efiemu_register_symbol ("efiemu_high_monotonic_count",
2N/A high_monotonic_count_handle, 0);
2N/A grub_efiemu_register_symbol ("efiemu_time_zone", timezone_handle, 0);
2N/A grub_efiemu_register_symbol ("efiemu_time_daylight", daylight_handle, 0);
2N/A grub_efiemu_register_symbol ("efiemu_time_accuracy",
2N/A accuracy_handle, 0);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic void
2N/Anvram_unload (void * data __attribute__ ((unused)))
2N/A{
2N/A grub_efiemu_mm_return_request (nvram_handle);
2N/A grub_efiemu_mm_return_request (nvramsize_handle);
2N/A grub_efiemu_mm_return_request (high_monotonic_count_handle);
2N/A grub_efiemu_mm_return_request (timezone_handle);
2N/A grub_efiemu_mm_return_request (accuracy_handle);
2N/A grub_efiemu_mm_return_request (daylight_handle);
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_efiemu_pnvram (void)
2N/A{
2N/A const char *size;
2N/A grub_err_t err;
2N/A
2N/A nvramsize = 0;
2N/A
2N/A size = grub_env_get ("EfiEmu.pnvram.size");
2N/A if (size)
2N/A nvramsize = grub_strtoul (size, 0, 0);
2N/A
2N/A if (!nvramsize)
2N/A nvramsize = 2048;
2N/A
2N/A err = grub_efiemu_register_prepare_hook (nvram_set, nvram_unload, 0);
2N/A if (err)
2N/A return err;
2N/A
2N/A nvram_handle
2N/A = grub_efiemu_request_memalign (1, nvramsize,
2N/A GRUB_EFI_RUNTIME_SERVICES_DATA);
2N/A nvramsize_handle
2N/A = grub_efiemu_request_memalign (1, sizeof (grub_uint32_t),
2N/A GRUB_EFI_RUNTIME_SERVICES_DATA);
2N/A high_monotonic_count_handle
2N/A = grub_efiemu_request_memalign (1, sizeof (grub_uint32_t),
2N/A GRUB_EFI_RUNTIME_SERVICES_DATA);
2N/A timezone_handle
2N/A = grub_efiemu_request_memalign (1, sizeof (grub_uint16_t),
2N/A GRUB_EFI_RUNTIME_SERVICES_DATA);
2N/A daylight_handle
2N/A = grub_efiemu_request_memalign (1, sizeof (grub_uint8_t),
2N/A GRUB_EFI_RUNTIME_SERVICES_DATA);
2N/A accuracy_handle
2N/A = grub_efiemu_request_memalign (1, sizeof (grub_uint32_t),
2N/A GRUB_EFI_RUNTIME_SERVICES_DATA);
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}