1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/*
* This file is part of the flashrom project.
*
* Copyright (C) 2009,2010 Carl-Daniel Hailfinger
* Copyright (C) 2022 secunet Security Networks AG
* (Written by Thomas Heijligen <thomas.heijligen@secunet.com)
*
* 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.
*/
/*
* This file implements x86 I/O port permission and access handling.
*
* The first part of the file defines the platform dependend implementations to
* use on each platform. For getting I/O permissions set IO_PORT_PERMISSION to
* one of:
* - USE_DUMMY
* - USE_SUN
* - USE_DEVICE
* - USE_IOPERM
* - USE_IOPL
* For the IN[B/W/L] and OUT[B/W/L] functions set IO_PORT_FUNCTION to one of:
* - USE_LIBC_TARGET_LAST
* - USE_LIBC_TARGET_FIRST
* - USE_DOS
* - USE_ASM
*
* The platform specific code for getting I/O permissions consists of two
* functions. `platform_get_io_perms()` is called to get
* permissions and `platform_release_io_perms()` is called for releasing those.
*/
#include <errno.h>
#include <string.h>
#include "flash.h"
#include "hwaccess_x86_io.h"
/* IO_PORT_FUNCTION */
#define USE_LIBC_TARGET_LAST 1
#define USE_LIBC_TARGET_FIRST 2
#define USE_ASM 3
#define USE_DOS 4
/* IO_PORT_PERMISSION */
#define USE_IOPL 5
#define USE_DEVICE 6
#define USE_DUMMY 7
#define USE_SUN 8
#define USE_IOPERM 9
#if defined(__ANDROID__)
#include <sys/io.h>
#include <unistd.h>
#define IO_PORT_PERMISSION USE_IOPERM
#define IO_PORT_FUNCTION USE_ASM
#endif
#if defined(__linux__) && !defined(__ANDROID__)
#include <sys/io.h>
#include <unistd.h>
#define IO_PORT_PERMISSION USE_IOPL
#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST
#endif
#if defined(__FreeBSD_kernel) && defined(__GLIBC__)
#include <sys/io.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#define IO_PORT_PERMISSION USE_DEVICE
#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/types.h>
#include <machine/cpufunc.h>
#include <fcntl.h>
#include <unistd.h>
#define IO_PORT_PERMISSION USE_DEVICE
#define IO_PORT_FUNCTION USE_LIBC_TARGET_FIRST
#endif
#if defined(__NetBSD__)
#include <sys/types.h>
#include <machine/sysarch.h>
#if defined(__i386__)
#define iopl i386_iopl
#elif defined(__x86_64__)
#define iopl x86_64_iopl
#endif
#define IO_PORT_PERMISSION USE_IOPL
#define IO_PORT_FUNCTION USE_ASM
#endif
#if defined(__OpenBSD__)
#include <sys/types.h>
#include <machine/sysarch.h>
#if defined(__i386__)
#define iopl i386_iopl
#elif defined(__amd64__)
#define iopl amd64_iopl
#endif
#define IO_PORT_PERMISSION USE_IOPL
#define IO_PORT_FUNCTION USE_ASM
#endif
#if defined(__MACH__) && defined(__APPLE__)
#include <DirectHW/DirectHW.h>
#define IO_PORT_PERMISSION USE_IOPL
#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST
#endif
#if defined(__DJGPP__)
#include <pc.h>
#define IO_PORT_PERMISSION USE_DUMMY
#define IO_PORT_FUNCTION USE_DOS
#endif
#if defined(__LIBPAYLOAD__)
#include <arch/io.h>
#define IO_PORT_PERMISSION USE_DUMMY
#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST
#endif
#if defined(__sun)
#include <sys/sysi86.h>
#include <sys/psw.h>
#include <asm/sunddi.h>
#define IO_PORT_PERMISSION USE_SUN
#define IO_PORT_FUNCTION USE_LIBC_TARGET_FIRST
#endif
#if defined(__gnu_hurd__)
#include <sys/io.h>
#define IO_PORT_PERMISSION USE_IOPERM
#define IO_PORT_FUNCTION USE_LIBC_TARGET_LAST
#endif
/* Make sure IO_PORT_PERMISSION and IO_PORT_FUNCTION are set */
#if IO_PORT_PERMISSION == 0 || IO_PORT_FUNCTION == 0
#error Unsupported or misconfigured platform.
#endif
/*
* USE_DUMMY
* This is for platforms which have no privilege levels.
*/
#if IO_PORT_PERMISSION == USE_DUMMY
static int platform_get_io_perms(void)
{
return 0;
}
static int platform_release_io_perms(void *p)
{
return 0;
}
#endif
/*
* USE_SUN
* This implementation uses SunOS system call sysi86 to handle I/O port permissions.
*/
#if IO_PORT_PERMISSION == USE_SUN
static int platform_get_io_perms(void)
{
return sysi86(SI86V86, V86SC_IOPL, PS_IOPL);
}
static int platform_release_io_perms(void *p)
{
sysi86(SI86V86, V86SC_IOPL, 0);
return 0;
}
#endif
/*
* USE_DEVICE
* This implementation uses the virtual /dev/io file to handle I/O Port permissions.
*/
#if IO_PORT_PERMISSION == USE_DEVICE
int io_fd;
static int platform_get_io_perms(void)
{
io_fd = open("/dev/io", O_RDWR);
return (io_fd >= 0 ? 0 : -1);
}
static int platform_release_io_perms(void *p)
{
close(io_fd);
return 0;
}
#endif
/*
* USE_IOPERM
* This implementation uses the ioperm system call to handle I/O port permissions.
*/
#if IO_PORT_PERMISSION == USE_IOPERM
static int platform_get_io_perms(void)
{
return ioperm(0, 65536, 1);
}
static int platform_release_io_perms(void *p)
{
ioperm(0, 65536, 0);
return 0;
}
#endif
/*
* USE_IOPL
* This implementation uses the iopl system call to handle I/O port permissions.
*/
#if IO_PORT_PERMISSION == USE_IOPL
static int platform_get_io_perms(void)
{
return iopl(3);
}
static int platform_release_io_perms(void *p)
{
iopl(0);
return 0;
}
#endif
/**
* @brief Get access to the x86 I/O ports
*
* This function abstracts the platform dependent function to get access to the
* x86 I/O ports and musst be called before using IN[B/W/L] or OUT[B/W/L].
* It registers a shutdown function to release those privileges.
*
* @return 0 on success, platform specific number on failure
*/
int rget_io_perms(void)
{
if (platform_get_io_perms() == 0) {
register_shutdown(platform_release_io_perms, NULL);
return 0;
}
msg_perr("ERROR: Could not get I/O privileges (%s).\n", strerror(errno));
#if defined(__linux__) && !defined(__ANDROID__)
if (getuid() != 0) {
msg_perr("Make sure you are running flashrom with root privileges.\n");
} else {
msg_perr("Your kernel may prevent access based on security policies.\n"
"Issue a 'dmesg | grep flashrom' for further information\n");
}
#elif defined(__OpenBSD__)
msg_perr("On OpenBSD set securelevel=-1 in /etc/rc.securelevel and\n"
"reboot, or reboot into single user mode.\n");
#elif defined(__NetBSD__)
msg_perr("On NetBSD reboot into single user mode or make sure\n"
"that your kernel configuration has the option INSECURE enabled.\n");
#else
msg_perr("Make sure you are running flashrom with root privileges.\n");
#endif
return 1;
}
/*
* USE_LIBC_LAST
* This implementation wraps the glibc functions with the port as 2nd argument.
*/
#if IO_PORT_FUNCTION == USE_LIBC_TARGET_LAST
void OUTB(uint8_t value, uint16_t port)
{
outb(value, port);
}
void OUTW(uint16_t value, uint16_t port)
{
outw(value, port);
}
void OUTL(uint32_t value, uint16_t port)
{
outl(value, port);
}
#endif
/*
* USE_LIBC_FIRST
* This implementation uses libc based functions with the port as first argument
* like on BSDs.
*/
#if IO_PORT_FUNCTION == USE_LIBC_TARGET_FIRST
void OUTB(uint8_t value, uint16_t port)
{
outb(port, value);
}
void OUTW(uint16_t value, uint16_t port)
{
outw(port, value);
}
void OUTL(uint32_t value, uint16_t port)
{
outl(port, value);
}
#endif
/*
* LIBC_xxx
* INx functions can be shared between _LAST and _FIRST
*/
#if IO_PORT_FUNCTION == USE_LIBC_TARGET_LAST || IO_PORT_FUNCTION == USE_LIBC_TARGET_FIRST
uint8_t INB(uint16_t port)
{
return inb(port);
}
uint16_t INW(uint16_t port)
{
return inw(port);
}
uint32_t INL(uint16_t port)
{
return inl(port);
}
#endif
/*
* USE_DOS
* DOS provides the functions under a differnt name.
*/
#if IO_PORT_FUNCTION == USE_DOS
void OUTB(uint8_t value, uint16_t port)
{
outportb(port, value);
}
void OUTW(uint16_t value, uint16_t port)
{
outportw(port, value);
}
void OUTL(uint32_t value, uint16_t port)
{
outportl(port, value);
}
uint8_t INB(uint16_t port)
{
return inportb(port);
}
uint16_t INW(uint16_t port)
{
return inportw(port);
}
uint32_t INL(uint16_t port)
{
return inportl(port);
}
#endif
/*
* USE_ASM
* Pure assembly implementation.
*/
#if IO_PORT_FUNCTION == USE_ASM
void OUTB(uint8_t value, uint16_t port)
{
__asm__ volatile ("outb %b0,%w1": :"a" (value), "Nd" (port));
}
void OUTW(uint16_t value, uint16_t port)
{
__asm__ volatile ("outw %w0,%w1": :"a" (value), "Nd" (port));
}
void OUTL(uint32_t value, uint16_t port)
{
__asm__ volatile ("outl %0,%w1": :"a" (value), "Nd" (port));
}
uint8_t INB(uint16_t port)
{
uint8_t value;
__asm__ volatile ("inb %w1,%0":"=a" (value):"Nd" (port));
return value;
}
uint16_t INW(uint16_t port)
{
uint16_t value;
__asm__ volatile ("inw %w1,%0":"=a" (value):"Nd" (port));
return value;
}
uint32_t INL(uint16_t port)
{
uint32_t value;
__asm__ volatile ("inl %1,%0":"=a" (value):"Nd" (port));
return value;
}
#endif
|