vaseboot/VasEBoot-core/commands/fwconfig.c

123 lines
3.7 KiB
C

/* fwconfig.c - command to read config from qemu fwconfig */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2015 CoreOS, Inc.
*
* VasEBoot 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 3 of the License, or
* (at your option) any later version.
*
* VasEBoot 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 VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/dl.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/extcmd.h>
#include <VasEBoot/env.h>
#include <VasEBoot/cpu/io.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/mm.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
#define SELECTOR 0x510
#define DATA 0x511
#define SIGNATURE_INDEX 0x00
#define DIRECTORY_INDEX 0x19
static VasEBoot_extcmd_t cmd_read_fwconfig;
struct VasEBoot_qemu_fwcfgfile {
VasEBoot_uint32_t size;
VasEBoot_uint16_t select;
VasEBoot_uint16_t reserved;
char name[56];
};
static const struct VasEBoot_arg_option options[] =
{
{0, 'v', 0, N_("Save read value into variable VARNAME."),
N_("VARNAME"), ARG_TYPE_STRING},
{0, 0, 0, 0, 0, 0}
};
static VasEBoot_err_t
VasEBoot_cmd_fwconfig (VasEBoot_extcmd_context_t ctxt __attribute__ ((unused)),
int argc, char **argv)
{
VasEBoot_uint32_t i, j, value = 0;
struct VasEBoot_qemu_fwcfgfile file;
char fwsig[4], signature[4] = { 'Q', 'E', 'M', 'U' };
if (argc != 2)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("two arguments expected"));
/* Verify that we have meaningful hardware here */
VasEBoot_outw(SIGNATURE_INDEX, SELECTOR);
for (i=0; i<sizeof(fwsig); i++)
fwsig[i] = VasEBoot_inb(DATA);
if (VasEBoot_memcmp(fwsig, signature, sizeof(signature)) != 0)
return VasEBoot_error (VasEBoot_ERR_BAD_DEVICE, N_("invalid fwconfig hardware signature: got 0x%x%x%x%x"), fwsig[0], fwsig[1], fwsig[2], fwsig[3]);
/* Find out how many file entries we have */
VasEBoot_outw(DIRECTORY_INDEX, SELECTOR);
value = VasEBoot_inb(DATA) | VasEBoot_inb(DATA) << 8 | VasEBoot_inb(DATA) << 16 | VasEBoot_inb(DATA) << 24;
value = VasEBoot_be_to_cpu32(value);
/* Read the file description for each file */
for (i=0; i<value; i++)
{
for (j=0; j<sizeof(file); j++)
{
((char *)&file)[j] = VasEBoot_inb(DATA);
}
/* Check whether it matches what we're looking for, and if so read the file */
if (VasEBoot_strncmp(file.name, argv[0], sizeof(file.name)) == 0)
{
VasEBoot_uint32_t filesize = VasEBoot_be_to_cpu32(file.size);
VasEBoot_uint16_t location = VasEBoot_be_to_cpu16(file.select);
char *data = VasEBoot_malloc(filesize+1);
if (!data)
return VasEBoot_error (VasEBoot_ERR_OUT_OF_MEMORY, N_("can't allocate buffer for data"));
VasEBoot_outw(location, SELECTOR);
for (j=0; j<filesize; j++)
{
data[j] = VasEBoot_inb(DATA);
}
data[filesize] = '\0';
VasEBoot_env_set (argv[1], data);
VasEBoot_free(data);
return 0;
}
}
return VasEBoot_error (VasEBoot_ERR_FILE_NOT_FOUND, N_("couldn't find entry %s"), argv[0]);
}
VasEBoot_MOD_INIT(fwconfig)
{
cmd_read_fwconfig =
VasEBoot_register_extcmd ("fwconfig", VasEBoot_cmd_fwconfig, 0,
N_("PATH VAR"),
N_("Set VAR to the contents of fwconfig PATH"),
options);
}
VasEBoot_MOD_FINI(fwconfig)
{
VasEBoot_unregister_extcmd (cmd_read_fwconfig);
}