toaruos/apps/qemu-fwcfg.c

169 lines
3.9 KiB
C
Raw Normal View History

2018-08-14 11:13:38 +03:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2018 K. Lange
*
* qemu-fwcfg - Read QEMU fwcfg values.
*
* Provides easy access to values and files set by QEMU's -fw_cfg
* flag. This is used by the QEMU harness, as well as the bootloader,
* and can be used to provide files directly to the guest.
2018-07-17 08:29:26 +03:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
2018-07-17 08:42:29 +03:00
#define FW_CFG_PORT_OUT 0x510
#define FW_CFG_PORT_IN 0x511
#define FW_CFG_SELECT_QEMU 0x0000
#define FW_CFG_SELECT_LIST 0x0019
2018-07-17 08:29:26 +03:00
2018-07-17 08:42:29 +03:00
/* outw / inb helper functions */
static void outports(unsigned short _port, unsigned short _data) {
asm volatile ("outw %1, %0" : : "dN" (_port), "a" (_data));
2018-07-17 08:29:26 +03:00
}
2018-07-17 08:42:29 +03:00
static unsigned char inportb(unsigned short _port) {
2018-07-17 08:29:26 +03:00
unsigned char rv;
asm volatile ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}
2018-07-17 08:42:29 +03:00
/* Despite primarily emulating x86, these are all big-endian */
static void swap_bytes(void * in, int count) {
2018-07-17 08:29:26 +03:00
char * bytes = in;
if (count == 4) {
uint32_t * t = in;
*t = (bytes[0] << 24) | (bytes[1] << 12) | (bytes[2] << 8) | bytes[3];
} else if (count == 2) {
uint16_t * t = in;
*t = (bytes[0] << 8) | bytes[1];
}
}
2018-07-17 08:42:29 +03:00
/* Layout of the information returned from the fw_cfg port */
2018-07-17 08:29:26 +03:00
struct fw_cfg_file {
uint32_t size;
uint16_t select;
uint16_t reserved;
char name[56];
};
2018-07-17 08:42:29 +03:00
static int usage(char * argv[]) {
2018-07-17 08:29:26 +03:00
printf(
2018-07-17 08:42:29 +03:00
"Obtain QEMU fw_cfg values\n"
"\n"
2018-07-17 08:29:26 +03:00
"usage: %s [-?ln] [config name]\n"
"\n"
" -l \033[3mlist available config entries\033[0m\n"
" -n \033[3mdon't print a new line after image\033[0m\n"
" -? \033[3mshow this help text\033[0m\n"
"\n", argv[0]);
return 1;
}
int main(int argc, char * argv[]) {
2018-07-17 08:42:29 +03:00
uint32_t count = 0;
uint8_t * bytes = (uint8_t *)&count;
int found = 0;
struct fw_cfg_file file;
uint8_t * tmp = (uint8_t *)&file;
2018-07-17 08:29:26 +03:00
int opt = 0;
int list = 0;
int no_newline = 0;
2018-07-18 10:20:01 +03:00
int query_quietly = 0;
2018-07-17 08:29:26 +03:00
2018-07-18 10:20:01 +03:00
while ((opt = getopt(argc, argv, "?lnq")) != -1) {
2018-07-17 08:29:26 +03:00
switch (opt) {
case '?':
return usage(argv);
case 'n':
no_newline = 1;
break;
2018-07-18 10:20:01 +03:00
case 'q':
query_quietly = 1;
break;
2018-07-17 08:29:26 +03:00
case 'l':
list = 1;
break;
}
}
if (optind >= argc && !list) {
return usage(argv);
}
2018-07-17 08:42:29 +03:00
/* First check for QEMU */
outports(FW_CFG_PORT_OUT, FW_CFG_SELECT_QEMU);
if (inportb(FW_CFG_PORT_IN) != 'Q' ||
inportb(FW_CFG_PORT_IN) != 'E' ||
inportb(FW_CFG_PORT_IN) != 'M' ||
inportb(FW_CFG_PORT_IN) != 'U') {
2018-07-17 08:29:26 +03:00
fprintf(stderr, "%s: this doesn't seem to be qemu\n", argv[0]);
2018-07-17 08:51:48 +03:00
return 1;
2018-07-17 08:29:26 +03:00
}
2018-07-17 08:42:29 +03:00
/* Then get the list of "files" so we can look at names */
outports(FW_CFG_PORT_OUT, FW_CFG_SELECT_LIST);
2018-07-17 08:29:26 +03:00
for (int i = 0; i < 4; ++i) {
2018-07-17 08:42:29 +03:00
bytes[i] = inportb(FW_CFG_PORT_IN);
2018-07-17 08:29:26 +03:00
}
2018-07-17 08:42:29 +03:00
swap_bytes(&count, sizeof(count));
2018-07-17 08:29:26 +03:00
for (unsigned int i = 0; i < count; ++i) {
2018-07-17 08:42:29 +03:00
/* read one file entry */
2018-07-17 08:29:26 +03:00
for (unsigned int j = 0; j < sizeof(struct fw_cfg_file); ++j) {
2018-07-17 08:42:29 +03:00
tmp[j] = inportb(FW_CFG_PORT_IN);
2018-07-17 08:29:26 +03:00
}
2018-07-17 08:42:29 +03:00
/* endian swap to get file size and selector ID */
swap_bytes(&file.size, sizeof(file.size));
swap_bytes(&file.select, sizeof(file.select));
2018-07-17 08:29:26 +03:00
if (list) {
2018-07-17 08:42:29 +03:00
/* 0x0020 org/whatever (1234 bytes) */
fprintf(stdout, "0x%04x %s (%d byte%s)\n", file.select, file.name, (int)file.size, file.size == 1 ? "" : "s");
2018-07-17 08:29:26 +03:00
} else {
if (!strcmp(file.name, argv[optind])) {
2018-07-17 08:42:29 +03:00
/* found the requested file */
2018-07-17 08:29:26 +03:00
found = 1;
break;
}
}
}
2018-07-18 10:20:01 +03:00
if (query_quietly) {
return !found;
}
2018-07-17 08:29:26 +03:00
if (found) {
2018-07-17 08:42:29 +03:00
/* if we found the requested file, read it from the port */
outports(FW_CFG_PORT_OUT, file.select);
2018-07-17 10:28:09 +03:00
for (unsigned int i = 0; i < file.size; ++i) {
2018-07-17 08:42:29 +03:00
fputc(inportb(FW_CFG_PORT_IN), stdout);
2018-07-17 08:29:26 +03:00
}
if (!no_newline) {
fprintf(stdout, "\n");
2018-07-17 08:42:29 +03:00
} else {
fflush(stdout);
2018-07-17 08:29:26 +03:00
}
2018-07-17 08:42:29 +03:00
2018-07-17 08:29:26 +03:00
} else if (!list) {
fprintf(stderr, "%s: config option not found\n", argv[0]);
return 1;
}
return 0;
}