Thomas Heijligen | a065520 | 2021-12-14 16:36:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <string.h> |
| 19 | #if !defined (__DJGPP__) && !defined(__LIBPAYLOAD__) |
| 20 | /* No file access needed/possible to get hardware access permissions. */ |
| 21 | #include <unistd.h> |
| 22 | #include <fcntl.h> |
| 23 | #endif |
| 24 | |
| 25 | #include "hwaccess_x86_io.h" |
| 26 | #include "flash.h" |
| 27 | |
| 28 | #if USE_IOPERM |
| 29 | #include <sys/io.h> |
| 30 | #endif |
| 31 | |
| 32 | #if USE_DEV_IO |
| 33 | int io_fd; |
| 34 | #endif |
| 35 | |
| 36 | #if !(defined(__DJGPP__) || defined(__LIBPAYLOAD__)) |
| 37 | static int release_io_perms(void *p) |
| 38 | { |
| 39 | #if defined (__sun) |
| 40 | sysi86(SI86V86, V86SC_IOPL, 0); |
| 41 | #elif USE_DEV_IO |
| 42 | close(io_fd); |
| 43 | #elif USE_IOPERM |
| 44 | ioperm(0, 65536, 0); |
| 45 | #elif USE_IOPL |
| 46 | iopl(0); |
| 47 | #endif |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | /* Get I/O permissions with automatic permission release on shutdown. */ |
| 52 | int rget_io_perms(void) |
| 53 | { |
| 54 | #if defined (__sun) |
| 55 | if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) { |
| 56 | #elif USE_DEV_IO |
| 57 | if ((io_fd = open("/dev/io", O_RDWR)) < 0) { |
| 58 | #elif USE_IOPERM |
| 59 | if (ioperm(0, 65536, 1) != 0) { |
| 60 | #elif USE_IOPL |
| 61 | if (iopl(3) != 0) { |
| 62 | #endif |
| 63 | msg_perr("ERROR: Could not get I/O privileges (%s).\n", strerror(errno)); |
| 64 | msg_perr("You need to be root.\n"); |
| 65 | #if defined (__OpenBSD__) |
| 66 | msg_perr("If you are root already please set securelevel=-1 in /etc/rc.securelevel and\n" |
| 67 | "reboot, or reboot into single user mode.\n"); |
| 68 | #elif defined(__NetBSD__) |
| 69 | msg_perr("If you are root already please reboot into single user mode or make sure\n" |
| 70 | "that your kernel configuration has the option INSECURE enabled.\n"); |
| 71 | #endif |
| 72 | return 1; |
| 73 | } else { |
| 74 | register_shutdown(release_io_perms, NULL); |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | #else |
| 80 | |
| 81 | /* DJGPP and libpayload environments have full PCI port I/O permissions by default. */ |
Thomas Heijligen | a065520 | 2021-12-14 16:36:05 +0100 | [diff] [blame] | 82 | int rget_io_perms(void) |
| 83 | { |
| 84 | return 0; |
| 85 | } |
| 86 | #endif |