Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 2 | |
| 3 | #include <getopt.h> |
| 4 | #include <stdio.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <stdint.h> |
| 10 | #include <string.h> |
| 11 | #include <errno.h> |
| 12 | #include <unistd.h> |
| 13 | #include <sys/mman.h> |
| 14 | #if defined(__GLIBC__) |
| 15 | #include <sys/io.h> |
| 16 | #endif |
| 17 | #include <pci/pci.h> |
| 18 | |
| 19 | #if defined(__sun) && (defined(__i386) || defined(__amd64)) |
| 20 | #define MEM_DEV "/dev/xsvc" |
| 21 | #else |
| 22 | #define MEM_DEV "/dev/mem" |
| 23 | #endif |
| 24 | |
| 25 | #define BUC_OFFSET 0x3414 |
| 26 | #define GCS_OFFSET 0x3410 |
| 27 | |
| 28 | #define SPIBAR 0x3800 |
| 29 | #define FDOC 0xb0 |
| 30 | #define FDOD 0xb4 |
| 31 | |
| 32 | enum { |
| 33 | DESCRIPTOR_MAP_SECTION = 0, |
| 34 | COMPONENT_SECTION, |
| 35 | REGION_SECTION, |
| 36 | MASTER_SECTION, |
| 37 | PCHSTRAPS_SECTION, |
| 38 | }; |
| 39 | |
| 40 | enum { |
| 41 | BUCTS_UNSET = 0, |
| 42 | BUCTS_SET |
| 43 | }; |
| 44 | |
| 45 | static int fd_mem = -1; |
| 46 | |
| 47 | static void print_usage(const char *name) |
| 48 | { |
| 49 | printf("usage: %s [-h?]\n", name); |
| 50 | printf("\n" |
| 51 | " -s | --set: Set the top swap bit to invert the bootblock region decoding\n" |
| 52 | " -u | --unset: Unset the top swap bit to have regular bootblock region decoding\n" |
| 53 | " -p | --print: print the current status of the hardware\n" |
| 54 | " -h | --help: print this help\n" |
| 55 | "\n"); |
| 56 | } |
| 57 | |
| 58 | static void *sys_physmap(unsigned long phys_addr, size_t len) |
| 59 | { |
| 60 | void *virt_addr = |
| 61 | mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED, fd_mem, |
| 62 | (off_t) phys_addr); |
| 63 | return virt_addr == MAP_FAILED ? NULL : virt_addr; |
| 64 | } |
| 65 | |
| 66 | static void physunmap(void *virt_addr, size_t len) |
| 67 | { |
| 68 | if (!len) { |
| 69 | printf("Not unmapping zero size at %p\n", virt_addr); |
| 70 | return; |
| 71 | } |
| 72 | munmap(virt_addr, len); |
| 73 | } |
| 74 | |
| 75 | static void *physmap(const char *descr, unsigned long phys_addr, size_t len) |
| 76 | { |
| 77 | void *virt_addr; |
| 78 | |
| 79 | if (!descr) |
| 80 | descr = "memory"; |
| 81 | |
| 82 | if (!len) { |
| 83 | printf("Not mapping %s, zero size at 0x%08lx.\n", descr, |
| 84 | phys_addr); |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | if ((getpagesize() - 1) & len) |
| 89 | fprintf(stderr, "Unaligned size 0x%lx for %s at 0x%08lx!\n", |
| 90 | (unsigned long)len, descr, phys_addr); |
| 91 | |
| 92 | if ((getpagesize() - 1) & phys_addr) |
| 93 | fprintf(stderr, "Unaligned address 0x%08lx for %s!\n", |
| 94 | phys_addr, descr); |
| 95 | |
| 96 | virt_addr = sys_physmap(phys_addr, len); |
| 97 | if (!virt_addr) { |
| 98 | fprintf(stderr, "Error accessing 0x%lx bytes %s at 0x%08lx!\n", |
| 99 | (unsigned long)len, descr, phys_addr); |
| 100 | perror("mmap(" MEM_DEV ")"); |
| 101 | if (errno == EINVAL) { |
| 102 | fprintf(stderr, "\n"); |
| 103 | fprintf(stderr, |
| 104 | "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); |
| 105 | fprintf(stderr, |
| 106 | "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); |
| 107 | fprintf(stderr, |
| 108 | "Please check if either is enabled in your kernel before reporting a failure.\n"); |
| 109 | fprintf(stderr, |
| 110 | "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); |
| 111 | fprintf(stderr, |
| 112 | "disabling the other option unfortunately requires a kernel recompile. Sorry!\n"); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return virt_addr; |
| 117 | } |
| 118 | |
| 119 | static int get_platform_info(struct pci_dev *sb, int *has_var_bb, |
| 120 | uint32_t *rcba_addr) |
| 121 | { |
| 122 | *has_var_bb = 0; |
| 123 | switch (sb->device_id) { |
| 124 | case 0x1c44: /* Z68 */ |
| 125 | case 0x1c46: /* P67 */ |
| 126 | case 0x1c47: /* UM67 */ |
| 127 | case 0x1c49: /* HM68 */ |
| 128 | case 0x1c4a: /* H67 */ |
| 129 | case 0x1c4b: /* HM67 */ |
| 130 | case 0x1c4c: /* Q65 */ |
| 131 | case 0x1c4d: /* QS67 */ |
| 132 | case 0x1c4e: /* Q67 */ |
| 133 | case 0x1c4f: /* QM67 */ |
| 134 | case 0x1c50: /* B65 */ |
| 135 | case 0x1c52: /* C202 */ |
| 136 | case 0x1c54: /* C204 */ |
| 137 | case 0x1c56: /* C026 */ |
| 138 | case 0x1c5c: /* H61 */ |
| 139 | case 0x1d40: /* C60x/X79 */ |
| 140 | case 0x1c41: /* C60x/X79 */ |
| 141 | case 0x1e44: /* Z77 */ |
| 142 | case 0x1e46: /* Z75 */ |
| 143 | case 0x1e47: /* Q77 */ |
| 144 | case 0x1e48: /* Q75 */ |
| 145 | case 0x1e49: /* B75 */ |
| 146 | case 0x1e4a: /* H77 */ |
| 147 | case 0x1e53: /* C216 */ |
| 148 | case 0x1e55: /* QM77 */ |
| 149 | case 0x1e56: /* QS77 */ |
| 150 | case 0x1e57: /* HM77 */ |
| 151 | case 0x1e58: /* UM77 */ |
| 152 | case 0x1e59: /* HM76 */ |
| 153 | case 0x1e5d: /* HM75 */ |
| 154 | case 0x1e5e: /* HM70 */ |
| 155 | case 0x1e5f: /* NM70 */ |
| 156 | case 0x3b02: /* P55 */ |
| 157 | case 0x3b03: /* PM55 */ |
| 158 | case 0x3b06: /* H55 */ |
| 159 | case 0x3b07: /* QM55 */ |
| 160 | case 0x3b08: /* H57 */ |
| 161 | case 0x3b09: /* HM55 */ |
| 162 | case 0x3b0a: /* Q57 */ |
| 163 | case 0x3b0b: /* HM57 */ |
| 164 | case 0x3b0d: /* PCH 3400 Mobile SFF */ |
| 165 | case 0x3b0e: /* B55 */ |
| 166 | case 0x3b0f: /* QS57 */ |
| 167 | case 0x3b12: /* PCH 3400 */ |
| 168 | case 0x3b14: /* PCH 3420 */ |
| 169 | case 0x3b16: /* PCH 3450 */ |
| 170 | case 0x3b1e: /* B55 */ |
| 171 | case 0x8c40: /* Lynx Point */ |
| 172 | case 0x8c41: /* Lynx Point Mobile Eng. Sample */ |
| 173 | case 0x8c42: /* Lynx Point Desktop Eng. Sample */ |
| 174 | case 0x8c43: /* Lynx Point */ |
| 175 | case 0x8c44: /* Z87 */ |
| 176 | case 0x8c45: /* Lynx Point */ |
| 177 | *has_var_bb = 1; |
| 178 | /* fallthrough */ |
| 179 | case 0x2640: /* ICH6/ICH6R */ |
| 180 | case 0x2641: /* ICH6-M */ |
| 181 | case 0x2642: /* ICH6W/ICH6RW */ |
| 182 | case 0x2670: /* 631xESB/632xESB/3100 */ |
| 183 | case 0x27b0: /* ICH7DH */ |
| 184 | case 0x27b8: /* ICH7/ICH7R */ |
| 185 | case 0x27b9: /* ICH7M */ |
| 186 | case 0x27bd: /* ICH7MDH */ |
| 187 | case 0x27bc: /* NM10 */ |
| 188 | case 0x2810: /* ICH8/ICH8R */ |
| 189 | case 0x2811: /* ICH8M-E */ |
| 190 | case 0x2812: /* ICH8DH */ |
| 191 | case 0x2814: /* ICH8DO */ |
| 192 | case 0x2815: /* ICH8M */ |
| 193 | case 0x2910: /* ICH9 Eng. Sample */ |
| 194 | case 0x2912: /* ICH9DH */ |
| 195 | case 0x2914: /* ICH9DO */ |
| 196 | case 0x2916: /* ICH9R */ |
| 197 | case 0x2917: /* ICH9M-E */ |
| 198 | case 0x2918: /* ICH9 */ |
| 199 | case 0x2919: /* ICH9M */ |
| 200 | case 0x3a10: /* ICH10R Eng. Sample */ |
| 201 | case 0x3a14: /* ICH10D0 */ |
| 202 | case 0x3a16: /* ICH10R */ |
| 203 | case 0x3a18: /* ICH10 */ |
| 204 | case 0x3a1a: /* ICH10D */ |
| 205 | case 0x3a1e: /* ICH10 Eng. Sample*/ |
| 206 | case 0x3b00: /* PCH 3400 Desktop */ |
| 207 | case 0x3b01: /* PCH 3400 Mobile */ |
| 208 | *rcba_addr = pci_read_long(sb, 0xf0) & ~1; |
| 209 | return 0; |
| 210 | default: |
| 211 | fprintf(stderr, "Unsupported LPC bridge. Sorry.\n"); |
| 212 | return 1; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | int print_status(uint32_t rcba_addr, int has_var_bb) |
| 217 | { |
| 218 | int ret = 0; |
| 219 | volatile uint8_t *rcba; |
| 220 | int bild, ts, ts_size = 64 << 10; |
| 221 | rcba = physmap("RCBA", rcba_addr, 0x4000); |
| 222 | if (!rcba) |
| 223 | return 1; |
| 224 | |
| 225 | bild = rcba[GCS_OFFSET] & 1; |
| 226 | ts = rcba[BUC_OFFSET] & 1; |
| 227 | int spi_is_bootdevice = |
| 228 | ((*(uint16_t *)(rcba + GCS_OFFSET) >> 10) & 3) == 3; |
| 229 | |
| 230 | if (has_var_bb && spi_is_bootdevice) { |
| 231 | *(uint32_t *)(rcba + SPIBAR + FDOC) = |
| 232 | (PCHSTRAPS_SECTION << 12) | 0; |
| 233 | uint32_t pchstrap0 = *(uint32_t *)(rcba + SPIBAR + FDOD); |
| 234 | switch (pchstrap0 >> 29) { |
| 235 | case 0: |
| 236 | ts_size = 64 << 10; |
| 237 | break; |
| 238 | case 1: |
| 239 | ts_size = 128 << 10; |
| 240 | break; |
| 241 | case 2: |
| 242 | ts_size = 256 << 10; |
| 243 | break; |
| 244 | default: |
| 245 | printf("Unknown BIOS Boot-Block size (BBBS)\n"); |
| 246 | ret = 1; |
| 247 | goto out; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | printf("\nSystem status\n=============\n"); |
| 252 | printf("BILD (BIOS Interface Lock-Down) : %s\n", bild |
| 253 | ? "set, changing top swap not possible" : "unset, changing top swap is possible"); |
| 254 | printf("Top Swap : %s\n", ts ? "Enabled" : "Disabled"); |
| 255 | printf("BIOS Boot-block Size (BBDS) : %dK\n", ts_size >> 10); |
| 256 | if (ts) { |
| 257 | printf("Ranges swapped : 0x%llx-0x%08llx <-> 0x%08llx-0x%08llx", |
| 258 | ((4ULL << 30) - ts_size), |
| 259 | (4ULL << 30) - 1, |
| 260 | (4ULL << 30) - 2 * ts_size, |
| 261 | (4ULL << 30) - ts_size - 1); |
| 262 | } |
| 263 | |
| 264 | out: |
| 265 | physunmap((void *)rcba, 0x4000); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static int set_bucts(uint32_t rcba_addr, int bucts_state) |
| 270 | { |
| 271 | int ret = 0; |
| 272 | volatile uint8_t *rcba; |
| 273 | int bild; |
| 274 | rcba = physmap("RCBA", rcba_addr, 0x4000); |
| 275 | if (!rcba) |
| 276 | return 1; |
| 277 | |
| 278 | bild = rcba[GCS_OFFSET] & 1; |
| 279 | if (bild) { |
| 280 | printf("BIOS Interface Lock-Down set, Changing Top Swap not possible.\n"); |
| 281 | ret = 1; |
| 282 | goto out; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | rcba[BUC_OFFSET] = bucts_state & 1; |
| 287 | if ((rcba[BUC_OFFSET] & 1) != bucts_state) { |
| 288 | fprintf(stderr, "Hu?! setting the top swap bit failed!\n"); |
| 289 | ret = 1; |
| 290 | } else { |
| 291 | printf("Setting top swap bit succeeded.\n"); |
| 292 | ret = 0; |
| 293 | } |
| 294 | |
| 295 | out: |
| 296 | physunmap((void *)rcba, 0x4000); |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | int main(int argc, char *argv[], const char *envp[]) |
| 301 | { |
| 302 | struct pci_access *pacc; |
| 303 | struct pci_dev *dev, *sb = NULL; |
| 304 | #if defined(__FreeBSD__) |
| 305 | int io_fd; |
| 306 | #endif |
| 307 | int opt, option_index = 0; |
| 308 | int print_state = 0; |
| 309 | int change_state = 0; |
| 310 | int bucts_state; |
| 311 | |
| 312 | static struct option long_options[] = { |
| 313 | {"unset", 0, 0, 'u'}, |
| 314 | {"set", 0, 0, 's'}, |
| 315 | {"print", 0, 0, 'p'}, |
| 316 | {"help", 0, 0, 'h'}, |
| 317 | {0, 0, 0, 0} |
| 318 | }; |
| 319 | |
| 320 | |
| 321 | printf("bucts utility version '" VERSION "'\n"); |
| 322 | |
| 323 | if (argv[1] == NULL) { |
| 324 | print_usage(argv[0]); |
| 325 | exit(1); |
| 326 | } |
| 327 | |
| 328 | while ((opt = getopt_long(argc, argv, "usph?:", |
| 329 | long_options, &option_index)) != EOF) { |
| 330 | switch (opt) { |
| 331 | case 'u': |
| 332 | if (change_state) { |
| 333 | printf("Invalid setting: multiple set/unset arguments\n"); |
| 334 | exit(1); |
| 335 | } |
| 336 | if (print_state) { |
| 337 | printf("Print and Set are mutually exclusive!\n"); |
| 338 | exit(1); |
| 339 | } |
| 340 | bucts_state = BUCTS_UNSET; |
| 341 | change_state = 1; |
| 342 | break; |
| 343 | case 's': |
| 344 | if (change_state) { |
| 345 | printf("Invalid setting: multiple set/unset arguments\n"); |
| 346 | exit(1); |
| 347 | } |
| 348 | if (print_state) { |
| 349 | printf("Print and Set are mutually exclusive!\n"); |
| 350 | exit(1); |
| 351 | } |
| 352 | bucts_state = BUCTS_SET; |
| 353 | change_state = 1; |
| 354 | break; |
| 355 | case 'p': |
| 356 | if (change_state) { |
| 357 | printf("Print and Set are mutually exclusive!\n"); |
| 358 | exit(1); |
| 359 | } |
| 360 | print_state = 1; |
| 361 | break; |
| 362 | case 'h': |
| 363 | print_usage(argv[0]); |
| 364 | exit(0); |
| 365 | case '?': |
| 366 | default: |
| 367 | print_usage(argv[0]); |
| 368 | exit(1); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (optind < argc) { |
| 373 | fprintf(stderr, "Error: Extra parameter found.\n"); |
| 374 | print_usage(argv[0]); |
| 375 | exit(1); |
| 376 | } |
| 377 | |
| 378 | |
| 379 | #if defined(__FreeBSD__) |
| 380 | if ((io_fd = open("/dev/io", O_RDWR)) < 0) { |
| 381 | perror("open(/dev/io)"); |
| 382 | #else |
| 383 | if (iopl(3)) { |
| 384 | perror("iopl"); |
| 385 | #endif |
| 386 | printf("You need to be root.\n"); |
| 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | if ((fd_mem = open(MEM_DEV, O_RDWR | O_SYNC)) == -1) { |
| 391 | perror("Error: open(" MEM_DEV ")"); |
| 392 | return 1; |
| 393 | } |
| 394 | |
| 395 | pacc = pci_alloc(); |
| 396 | pci_init(pacc); |
| 397 | pci_scan_bus(pacc); |
| 398 | for (dev = pacc->devices; dev && !sb; dev = dev->next) { |
| 399 | pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS); |
| 400 | if (dev->vendor_id != 0x8086 || dev->device_class != 0x0601) |
| 401 | continue; |
| 402 | sb = dev; |
| 403 | } |
| 404 | if (!sb) { |
| 405 | fprintf(stderr, "Error: LPC bridge not found!\n"); |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | printf("Using LPC bridge %04x:%04x at %02x%02x:%02x.%02x\n", |
| 410 | sb->vendor_id, sb->device_id, sb->domain, sb->bus, sb->dev, |
| 411 | sb->func); |
| 412 | |
| 413 | int has_var_bb; |
| 414 | uint32_t rcba_addr; |
| 415 | |
| 416 | if (get_platform_info(sb, &has_var_bb, &rcba_addr)) |
| 417 | exit(1); |
| 418 | |
| 419 | if (print_state) |
| 420 | print_status(rcba_addr, has_var_bb); |
| 421 | if (change_state) { |
| 422 | set_bucts(rcba_addr, bucts_state); |
| 423 | print_status(rcba_addr, has_var_bb); |
| 424 | } |
| 425 | |
| 426 | close(fd_mem); |
| 427 | return 0; |
| 428 | } |