diff --git a/ac97/ac97.c b/ac97/ac97.c index 70239a5..aa9dfde 100644 --- a/ac97/ac97.c +++ b/ac97/ac97.c @@ -24,82 +24,97 @@ #include #include "clib.h" - -uint8_t first = 1, silent = 0, bus, dev, func; +uint8_t first = 1, silent = 0, bus, dev, func; uint16_t i, io_base, alt_io_base; - static void codec_probe(uint16_t (*codec_read)(uint8_t reg), - void (*codec_write)(uint8_t reg, uint16_t val)) + void (*codec_write)(uint8_t reg, uint16_t val)) { - uint8_t cur_reg = 0; + uint8_t cur_reg = 0; uint16_t regs[64], *regp = regs; - char buf[13]; - FILE *f; + char buf[13]; + FILE *f; /* Reset codec. */ - if (!silent) printf("\nResetting codec..."); + if (!silent) + printf("\nResetting codec..."); codec_write(0x00, 0xffff); - if (!silent) printf(" done.\n"); + if (!silent) + printf(" done.\n"); /* Set all optional registers/bits. */ - if (!silent) printf("Setting optional bits [Master"); + if (!silent) + printf("Setting optional bits [Master"); codec_write(0x02, 0xbf3f); - if (!silent) printf(" AuxOut"); + if (!silent) + printf(" AuxOut"); codec_write(0x04, 0xbf3f); - if (!silent) printf(" MonoOut"); + if (!silent) + printf(" MonoOut"); codec_write(0x06, 0x803f); - if (!silent) printf(" PCBeep"); + if (!silent) + printf(" PCBeep"); codec_write(0x0a, 0x9ffe); - if (!silent) printf(" Phone"); + if (!silent) + printf(" Phone"); codec_write(0x0c, 0x801f); - if (!silent) printf(" Video"); + if (!silent) + printf(" Video"); codec_write(0x14, 0x9f1f); - if (!silent) printf(" AuxIn"); + if (!silent) + printf(" AuxIn"); codec_write(0x16, 0x9f1f); - if (!silent) printf(" GP"); + if (!silent) + printf(" GP"); codec_write(0x20, 0xf380); - if (!silent) printf(" 3D"); + if (!silent) + printf(" 3D"); codec_write(0x22, 0xffff); - if (!silent) printf(" EAID"); + if (!silent) + printf(" EAID"); codec_write(0x28, codec_read(0x28) | 0x0030); - if (!silent) printf(" C/LFE"); + if (!silent) + printf(" C/LFE"); codec_write(0x36, 0xbfbf); - if (!silent) printf(" Surround"); + if (!silent) + printf(" Surround"); codec_write(0x38, 0xbfbf); /* Dump registers. */ if (!silent) { - printf("] then dumping registers:\n "); - for (i = 0x0; i <= 0xf; i += 2) { - if (i == 0x8) - putchar(' '); - printf(" %X", i); - } - putchar('\n'); + printf("] then dumping registers:\n "); + for (i = 0x0; i <= 0xf; i += 2) { + if (i == 0x8) + putchar(' '); + printf(" %X", i); + } + putchar('\n'); } do { - /* Print row header. */ - if (!silent) printf("%02X:", cur_reg); + /* Print row header. */ + if (!silent) + printf("%02X:", cur_reg); - /* Iterate through register words on this range. */ - do { - /* Add spacing at the halfway point. */ - if (!silent && ((cur_reg & 0x0f) == 0x08)) - putchar(' '); + /* Iterate through register words on this range. */ + do { + /* Add spacing at the halfway point. */ + if (!silent && ((cur_reg & 0x0f) == 0x08)) + putchar(' '); - /* Read and print the word value. */ - *regp = codec_read(cur_reg); - if (!silent) printf(" %04X", *regp); - regp++; + /* Read and print the word value. */ + *regp = codec_read(cur_reg); + if (!silent) + printf(" %04X", *regp); + regp++; - cur_reg += 2; - } while (cur_reg & 0x0f); + cur_reg += 2; + } while (cur_reg & 0x0f); - /* Move on to the next line. */ - if (!silent) putchar('\n'); + /* Move on to the next line. */ + if (!silent) + putchar('\n'); } while (cur_reg < 0x80); /* Generate and print dump file name. */ @@ -109,32 +124,30 @@ codec_probe(uint16_t (*codec_read)(uint8_t reg), /* Write dump file. */ f = fopen(buf, "wb"); if (!f) { - printf("File creation failed\n"); - return; + printf("File creation failed\n"); + return; } if (fwrite(regs, 1, sizeof(regs), f) < sizeof(regs)) { - fclose(f); - printf("File write failed\n"); - return; + fclose(f); + printf("File write failed\n"); + return; } fclose(f); } - static void audiopci_codec_wait(multi_t *regval) { /* Wait for WIP to be cleared. */ for (i = 1; i; i++) { - regval->u32 = inl(io_base | 0x14); - if (!(regval->u16[1] & 0x4000)) - break; + regval->u32 = inl(io_base | 0x14); + if (!(regval->u16[1] & 0x4000)) + break; } if (!i) - printf("[!]"); + printf("[!]"); } - static uint16_t audiopci_codec_read(uint8_t reg) { @@ -156,7 +169,6 @@ audiopci_codec_read(uint8_t reg) return regval.u16[0]; } - static void audiopci_codec_write(uint8_t reg, uint16_t val) { @@ -166,7 +178,7 @@ audiopci_codec_write(uint8_t reg, uint16_t val) audiopci_codec_wait(®val); /* Set address and data. */ - regval.u8[2] = reg; + regval.u8[2] = reg; regval.u16[0] = val; /* Perform write. */ @@ -176,7 +188,6 @@ audiopci_codec_write(uint8_t reg, uint16_t val) audiopci_codec_wait(®val); } - static void audiopci_probe(uint16_t dev_id) { @@ -192,7 +203,7 @@ audiopci_probe(uint16_t dev_id) /* Get I/O BAR. */ io_base = pci_get_io_bar(bus, dev, func, 0x10, 64, "Main"); if (!io_base) - return; + return; printf("GPIO readout [%02X]\n", inb(io_base | 0x02)); @@ -200,7 +211,6 @@ audiopci_probe(uint16_t dev_id) codec_probe(audiopci_codec_read, audiopci_codec_write); } - static void via_codec_wait(multi_t *regval, uint16_t additional) { @@ -209,15 +219,14 @@ via_codec_wait(multi_t *regval, uint16_t additional) /* Wait for Controller Busy to be cleared and additional bits to be set. */ mask |= additional; for (i = 1; i; i++) { - regval->u32 = inl(io_base | 0x80); - if ((regval->u16[1] & mask) == additional) - break; + regval->u32 = inl(io_base | 0x80); + if ((regval->u16[1] & mask) == additional) + break; } if (!i) - printf("[!]"); + printf("[!]"); } - static uint16_t via_codec_read(uint8_t reg) { @@ -242,7 +251,6 @@ via_codec_read(uint8_t reg) return regval.u16[0]; } - static void via_codec_write(uint8_t reg, uint16_t val) { @@ -255,7 +263,7 @@ via_codec_write(uint8_t reg, uint16_t val) regval.u8[3] = 0x02; /* Set address and data. */ - regval.u8[2] = reg; + regval.u8[2] = reg; regval.u16[0] = val; /* Perform write. */ @@ -265,83 +273,80 @@ via_codec_write(uint8_t reg, uint16_t val) via_codec_wait(®val, 0); } - static void via_probe(uint16_t dev_id) { uint8_t rev, base_rev; /* Print controller information. */ - rev = pci_readb(bus, dev, func, 0x08); + rev = pci_readb(bus, dev, func, 0x08); base_rev = pci_readb(bus, dev, 0, 0x08); printf("Found VIA %04X revision %02X (base %02X) at bus %02X device %02X function %d\n", dev_id, rev, base_rev, bus, dev, func); /* Get SGD I/O BAR. */ io_base = pci_get_io_bar(bus, dev, func, 0x10, 256, "SGD"); if (!io_base) - return; + return; /* Set up AC-Link interface. */ printf("Waking codec up... "); pci_writeb(bus, dev, func, 0x41, (dev_id == 0x3058) ? 0xc4 : 0xc0); for (i = 1; i; i++) { - if (pci_readb(bus, dev, func, 0x40) & 0x01) - break; + if (pci_readb(bus, dev, func, 0x40) & 0x01) + break; } if (!i) { - printf("timed out!\n"); - return; + printf("timed out!\n"); + return; } printf("done.\n"); /* Test Codec Shadow I/O BAR on 686. */ if (dev_id == 0x3058) { - alt_io_base = pci_get_io_bar(bus, dev, func, 0x1c, 256, "Codec Shadow"); + alt_io_base = pci_get_io_bar(bus, dev, func, 0x1c, 256, "Codec Shadow"); - /* Perform tests if the BAR is set. */ - if (alt_io_base) { - /* Reset codec. */ - via_codec_write(0x00, 0xffff); + /* Perform tests if the BAR is set. */ + if (alt_io_base) { + /* Reset codec. */ + via_codec_write(0x00, 0xffff); - /* Test shadow behavior. */ - printf("Testing Codec Shadow:"); + /* Test shadow behavior. */ + printf("Testing Codec Shadow:"); - i = via_codec_read(0x02); - printf(" Read[%04X %04X]", i, inw(alt_io_base | 0x02)); + i = via_codec_read(0x02); + printf(" Read[%04X %04X]", i, inw(alt_io_base | 0x02)); - via_codec_write(0x02, 0xffff); - printf(" Write[%04X %04X]", 0xffff, inw(alt_io_base | 0x02)); + via_codec_write(0x02, 0xffff); + printf(" Write[%04X %04X]", 0xffff, inw(alt_io_base | 0x02)); - via_codec_write(0x02, 0xffff); - i = via_codec_read(0x02); - printf(" WriteRead[%04X %04X]", i, inw(alt_io_base | 0x02)); + via_codec_write(0x02, 0xffff); + i = via_codec_read(0x02); + printf(" WriteRead[%04X %04X]", i, inw(alt_io_base | 0x02)); - outw(alt_io_base | 0x02, 0x0000); - i = inw(alt_io_base | 0x02); - printf("\n DirectWrite[%04X %04X]", i, via_codec_read(0x02)); - } + outw(alt_io_base | 0x02, 0x0000); + i = inw(alt_io_base | 0x02); + printf("\n DirectWrite[%04X %04X]", i, via_codec_read(0x02)); + } - printf("\n"); + printf("\n"); } /* Perform codec probe. */ codec_probe(via_codec_read, via_codec_write); } - static void intel_codec_wait() { /* Wait for CAS to be cleared. */ for (i = 1; i; i++) { - if (!(inb(alt_io_base | 0x34) & 0x01)) - break; + if (!(inb(alt_io_base | 0x34) & 0x01)) + break; } if (!i) - printf("[!]"); + printf("[!]"); } - static uint16_t intel_codec_read(uint8_t reg) { @@ -352,7 +357,6 @@ intel_codec_read(uint8_t reg) return inw(io_base | reg); } - static void intel_codec_write(uint8_t reg, uint16_t val) { @@ -363,7 +367,6 @@ intel_codec_write(uint8_t reg, uint16_t val) outw(io_base | reg, val); } - static void intel_probe(uint16_t dev_id) { @@ -376,26 +379,26 @@ intel_probe(uint16_t dev_id) /* Get Mixer I/O BAR. */ io_base = pci_get_io_bar(bus, dev, func, 0x10, 256, "Mixer"); if (!io_base) - return; + return; /* Get Bus Master I/O BAR. */ alt_io_base = pci_get_io_bar(bus, dev, func, 0x14, 256, "Bus Master"); if (!alt_io_base) - return; + return; /* Set up AC-Link interface. */ printf("Waking codec up... "); outl(alt_io_base | 0x2c, inl(alt_io_base | 0x2c) & ~0x00000002); for (i = 1; i; i++) /* unknown delay required */ - inb(0xeb); + inb(0xeb); outl(alt_io_base | 0x2c, inl(alt_io_base | 0x2c) | 0x00000002); for (i = 1; i; i++) { - if (inl(alt_io_base | 0x30) & 0x00000100) - break; + if (inl(alt_io_base | 0x30) & 0x00000100) + break; } if (!i) { - printf("timed out!\n"); - return; + printf("timed out!\n"); + return; } printf("done.\n"); @@ -403,40 +406,37 @@ intel_probe(uint16_t dev_id) codec_probe(intel_codec_read, intel_codec_write); } - static void print_spacer() { if (first) - first = 0; + first = 0; else if (silent) - printf("\n"); + printf("\n"); else - printf("\n\n"); + printf("\n\n"); } - static void pci_scan_callback(uint8_t this_bus, uint8_t this_dev, uint8_t this_func, - uint16_t ven_id, uint16_t dev_id) + uint16_t ven_id, uint16_t dev_id) { - bus = this_bus; - dev = this_dev; + bus = this_bus; + dev = this_dev; func = this_func; if ((ven_id == 0x1274) && (dev_id != 0x5000)) { - print_spacer(); - audiopci_probe(dev_id); + print_spacer(); + audiopci_probe(dev_id); } else if ((ven_id == 0x1106) && ((dev_id == 0x3058) || (dev_id == 0x3059))) { - print_spacer(); - via_probe(dev_id); + print_spacer(); + via_probe(dev_id); } else if ((ven_id == 0x8086) && ((dev_id == 0x2415) || (dev_id == 0x2425) || (dev_id == 0x2445) || (dev_id == 0x2485) || (dev_id == 0x24c5) || (dev_id == 0x24d5) || (dev_id == 0x25a6) || (dev_id == 0x266e) || (dev_id == 0x27de) || (dev_id == 0x7195))) { - print_spacer(); - intel_probe(dev_id); + print_spacer(); + intel_probe(dev_id); } } - int main(int argc, char **argv) { @@ -447,11 +447,11 @@ main(int argc, char **argv) /* Initialize PCI, and exit in case of failure. */ if (!pci_init()) - return 1; + return 1; /* Set silent mode. */ if ((argc >= 1) && !strcmp(argv[1], "-s")) - silent = 1; + silent = 1; /* Scan PCI bus 0. */ pci_scan_bus(0, pci_scan_callback); diff --git a/acpi/acpi.c b/acpi/acpi.c index 37a6290..8157de9 100644 --- a/acpi/acpi.c +++ b/acpi/acpi.c @@ -23,12 +23,11 @@ #include #include "clib.h" - static void try_sleep() { unsigned int port, type; - char dummy_buf[256]; + char dummy_buf[256]; /* Prompt for port address. */ printf("Enter port address to try: "); @@ -45,11 +44,10 @@ try_sleep() printf("Nothing?\n"); } - static void probe_intel(uint8_t dev, uint8_t func) { - uint8_t status; + uint8_t status; uint16_t acpi_base, port, devctl; /* Read and print ACPI I/O base and PMCNTRL value. */ @@ -60,80 +58,78 @@ probe_intel(uint8_t dev, uint8_t func) /* Read and print SMI trap status, ports and values. */ devctl = inl(acpi_base + 0x2e); - port = pci_readw(0, dev, func, 0x60); + port = pci_readw(0, dev, func, 0x60); status = pci_readb(0, dev, func, 0x62); printf("SMI traps: Dev9 = %04X+%X Decode[%c] Trap[%c] Val[%04X]\n", port, status & 0x0f, - (status & 0x20) ? '√' : ' ', (devctl & 0x0008) ? '√' : ' ', inw(port)); - port = pci_readw(0, dev, func, 0x64); + (status & 0x20) ? '√' : ' ', (devctl & 0x0008) ? '√' : ' ', inw(port)); + port = pci_readw(0, dev, func, 0x64); status = pci_readb(0, dev, func, 0x66); printf(" Dev10 = %04X+%X Decode[%c] Trap[%c] Val[%04X]\n", port, status & 0x0f, - (status & 0x20) ? '√' : ' ', (devctl & 0x0020) ? '√' : ' ', inw(port)); - port = pci_readw(0, dev, func, 0x68); + (status & 0x20) ? '√' : ' ', (devctl & 0x0020) ? '√' : ' ', inw(port)); + port = pci_readw(0, dev, func, 0x68); status = pci_readb(0, dev, func, 0x6a); printf(" Dev12 = %04X+%X Decode[%c] Trap[%c] Val[%04X]\n", port, status & 0x0f, - (status & 0x10) ? '√' : ' ', (devctl & 0x0100) ? '√' : ' ', inw(port)); - port = pci_readw(0, dev, func, 0x70); + (status & 0x10) ? '√' : ' ', (devctl & 0x0100) ? '√' : ' ', inw(port)); + port = pci_readw(0, dev, func, 0x70); status = pci_readb(0, dev, func, 0x72); printf(" Dev13 = %04X+%X Decode[%c] Trap[%c] Val[%04X]\n", port, status & 0x0f, - (status & 0x10) ? '√' : ' ', (devctl & 0x0200) ? '√' : ' ', inw(port)); + (status & 0x10) ? '√' : ' ', (devctl & 0x0200) ? '√' : ' ', inw(port)); /* Run interactive sequence. */ try_sleep(); } - static void probe_via(uint8_t dev, uint8_t func, uint16_t dev_id) { - uint8_t mask; + uint8_t mask; uint16_t acpi_base, port, glben, status; /* Read and print ACPI I/O base and PMCNTRL value. */ acpi_base = pci_readw(0, dev, func, (dev_id == 0x3040) ? 0x20 : 0x48); - port = acpi_base & ((dev_id == 0x3040) ? 0xff00 : 0xff80); + port = acpi_base & ((dev_id == 0x3040) ? 0xff00 : 0xff80); printf("ACPI base register = %04X PMCNTRL[%04X] Val[%04X]\n", acpi_base, port | 0x04, inw(port | 0x04)); acpi_base = port; /* Read and print SMI trap status, ports and values. */ if (dev_id >= 0x3050) { - glben = inw(acpi_base + 0x2a); - mask = pci_readb(0, dev, 0, 0x80); - if (dev_id == 0x3050) { - status = pci_readw(0, dev, 0, 0x76); - port = pci_readw(0, dev, 0, 0x78); - printf("SMI traps: PCS0 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, mask & 0x0f, - (status & 0x0010) ? '√' : ' ', (status & 0x1000) ? '√' : ' ', (glben & 0x4000) ? '√' : ' ', inw(port)); - port = pci_readw(0, dev, 0, 0x7a); - printf(" PCS1 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, (mask >> 4) & 0x0f, - (status & 0x0020) ? '√' : ' ', (status & 0x2000) ? '√' : ' ', (glben & 0x8000) ? '√' : ' ', inw(port)); - } else { - status = pci_readw(0, dev, 0, 0x8b); - port = pci_readw(0, dev, 0, 0x78); - printf("SMI traps: PCS0 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, mask & 0x0f, - (status & 0x0100) ? '√' : ' ', (status & 0x1000) ? '√' : ' ', (glben & 0x4000) ? '√' : ' ', inw(port)); - port = pci_readw(0, dev, 0, 0x7a); - printf(" PCS1 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, (mask >> 4) & 0x0f, - (status & 0x0200) ? '√' : ' ', (status & 0x2000) ? '√' : ' ', (glben & 0x8000) ? '√' : ' ', inw(port)); - glben = inw(acpi_base + 0x42); /* extended I/O traps */ - mask = pci_readb(0, dev, 0, 0x8a); - port = pci_readw(0, dev, 0, 0x8c); - printf(" PCS2 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, mask & 0x0f, - (status & 0x0400) ? '√' : ' ', (status & 0x4000) ? '√' : ' ', (glben & 0x0001) ? '√' : ' ', inw(port)); - port = pci_readw(0, dev, 0, 0x8e); - printf(" PCS3 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, (mask >> 4) & 0x0f, - (status & 0x0800) ? '√' : ' ', (status & 0x8000) ? '√' : ' ', (glben & 0x0002) ? '√' : ' ', inw(port)); - } + glben = inw(acpi_base + 0x2a); + mask = pci_readb(0, dev, 0, 0x80); + if (dev_id == 0x3050) { + status = pci_readw(0, dev, 0, 0x76); + port = pci_readw(0, dev, 0, 0x78); + printf("SMI traps: PCS0 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, mask & 0x0f, + (status & 0x0010) ? '√' : ' ', (status & 0x1000) ? '√' : ' ', (glben & 0x4000) ? '√' : ' ', inw(port)); + port = pci_readw(0, dev, 0, 0x7a); + printf(" PCS1 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, (mask >> 4) & 0x0f, + (status & 0x0020) ? '√' : ' ', (status & 0x2000) ? '√' : ' ', (glben & 0x8000) ? '√' : ' ', inw(port)); + } else { + status = pci_readw(0, dev, 0, 0x8b); + port = pci_readw(0, dev, 0, 0x78); + printf("SMI traps: PCS0 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, mask & 0x0f, + (status & 0x0100) ? '√' : ' ', (status & 0x1000) ? '√' : ' ', (glben & 0x4000) ? '√' : ' ', inw(port)); + port = pci_readw(0, dev, 0, 0x7a); + printf(" PCS1 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, (mask >> 4) & 0x0f, + (status & 0x0200) ? '√' : ' ', (status & 0x2000) ? '√' : ' ', (glben & 0x8000) ? '√' : ' ', inw(port)); + glben = inw(acpi_base + 0x42); /* extended I/O traps */ + mask = pci_readb(0, dev, 0, 0x8a); + port = pci_readw(0, dev, 0, 0x8c); + printf(" PCS2 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, mask & 0x0f, + (status & 0x0400) ? '√' : ' ', (status & 0x4000) ? '√' : ' ', (glben & 0x0001) ? '√' : ' ', inw(port)); + port = pci_readw(0, dev, 0, 0x8e); + printf(" PCS3 = %04X+%X Decode[%c] IntIO[%c] Trap[%c] Val[%04X]\n", port, (mask >> 4) & 0x0f, + (status & 0x0800) ? '√' : ' ', (status & 0x8000) ? '√' : ' ', (glben & 0x0002) ? '√' : ' ', inw(port)); + } } /* Run interactive sequence. */ try_sleep(); } - int main(int argc, char **argv) { - uint8_t dev, func; + uint8_t dev, func; uint16_t ven_id, dev_id; uint32_t cf8; @@ -142,42 +138,42 @@ main(int argc, char **argv) /* Initialize PCI, and exit in case of failure. */ if (!pci_init()) - return 1; + return 1; /* Scan PCI bus 0. */ for (dev = 0; dev < 32; dev++) { - /* Single-function devices are definitely not the southbridge. */ - if (!(pci_readb(0, dev, 0, 0x0e) & 0x80)) - continue; + /* Single-function devices are definitely not the southbridge. */ + if (!(pci_readb(0, dev, 0, 0x0e) & 0x80)) + continue; - /* Determine and execute southbridge-specific function. */ - ven_id = pci_readw(0, dev, 3, 0x00); - dev_id = pci_readw(0, dev, 3, 0x02); - if ((ven_id == 0x8086) && (dev_id == 0x7113)) { - printf("Found PIIX4 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 3, 0x08), dev, 3); - probe_intel(dev, 3); - return 0; - } else if ((ven_id == 0x1055) && (dev_id == 0x9463)) { - printf("Found SLC90E66 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 3, 0x08), dev, 3); - probe_intel(dev, 3); - return 0; - } else if ((ven_id == 0x1106) && (dev_id == 0x3040)) { - printf("Found VT82C586 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 3, 0x08), dev, 3); - probe_via(dev, 3, dev_id); - return 0; - } else if ((ven_id == 0x1106) && (dev_id == 0x3050)) { - printf("Found VT82C596 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 3, 0x08), dev, 3); - probe_via(dev, 3, dev_id); - return 0; - } else { - ven_id = pci_readw(0, dev, 4, 0); - dev_id = pci_readw(0, dev, 4, 2); - if ((ven_id == 0x1106) && (dev_id == 0x3057)) { - printf("Found VT82C686 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 4, 0x08), dev, 4); - probe_via(dev, 4, dev_id); - return 0; - } - } + /* Determine and execute southbridge-specific function. */ + ven_id = pci_readw(0, dev, 3, 0x00); + dev_id = pci_readw(0, dev, 3, 0x02); + if ((ven_id == 0x8086) && (dev_id == 0x7113)) { + printf("Found PIIX4 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 3, 0x08), dev, 3); + probe_intel(dev, 3); + return 0; + } else if ((ven_id == 0x1055) && (dev_id == 0x9463)) { + printf("Found SLC90E66 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 3, 0x08), dev, 3); + probe_intel(dev, 3); + return 0; + } else if ((ven_id == 0x1106) && (dev_id == 0x3040)) { + printf("Found VT82C586 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 3, 0x08), dev, 3); + probe_via(dev, 3, dev_id); + return 0; + } else if ((ven_id == 0x1106) && (dev_id == 0x3050)) { + printf("Found VT82C596 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 3, 0x08), dev, 3); + probe_via(dev, 3, dev_id); + return 0; + } else { + ven_id = pci_readw(0, dev, 4, 0); + dev_id = pci_readw(0, dev, 4, 2); + if ((ven_id == 0x1106) && (dev_id == 0x3057)) { + printf("Found VT82C686 ACPI revision %02X at device %02X function %d\n", pci_readb(0, dev, 4, 0x08), dev, 4); + probe_via(dev, 4, dev_id); + return 0; + } + } } /* Nothing of interest found. */ diff --git a/biosdump/biosdump.c b/biosdump/biosdump.c index 8b318d9..b27ee3a 100644 --- a/biosdump/biosdump.c +++ b/biosdump/biosdump.c @@ -18,12 +18,11 @@ #include #include "clib.h" - /* Dummy comment. */ static int dump_range(uint32_t base, uint32_t size, const char *id) { - char fn[13]; + char fn[13]; FILE *f; /* Output the range being dumped. */ @@ -35,15 +34,15 @@ dump_range(uint32_t base, uint32_t size, const char *id) /* Open the dump file. */ f = fopen(fn, "wb"); if (!f) { - printf("FAILURE\n"); - return 1; + printf("FAILURE\n"); + return 1; } /* Write the dump. */ if (fwrite((char *) base, 1, size, f) != size) { - printf("FAILURE\n"); - fclose(f); - return 2; + printf("FAILURE\n"); + fclose(f); + return 2; } /* Finish the dump. */ @@ -53,7 +52,6 @@ dump_range(uint32_t base, uint32_t size, const char *id) return 0; } - int #ifdef STANDARD_MAIN main(int argc, char *argv[]) @@ -68,11 +66,11 @@ main(void) /* Dump ranges. */ if ((ret = dump_range(0x000c0000, 0x00040000, "low"))) - return ret; + return ret; if ((ret = dump_range(0x00fe0000, 0x00020000, "mid"))) - return 2 + ret; + return 2 + ret; if ((ret = dump_range(0xfff80000, 0x00080000, "high"))) - return 4 + ret; + return 4 + ret; return 0; } diff --git a/clib/clib.c b/clib/clib.c index fea8650..ff0cc9a 100644 --- a/clib/clib.c +++ b/clib/clib.c @@ -16,71 +16,66 @@ * */ #ifdef __POSIX_UEFI__ -# include +# include #else -# include -# include -# include -# include -# ifdef __WATCOMC__ -# include -# include -# endif +# include +# include +# include +# include +# ifdef __WATCOMC__ +# include +# include +# endif #endif #include "clib.h" - -uint8_t pci_mechanism = 0, pci_device_count = 0; +uint8_t pci_mechanism = 0, pci_device_count = 0; #ifdef __WATCOMC__ static union REGPACK rp; /* things break if this is not a global variable... */ #endif - /* String functions. */ int parse_hex_u8(char *val, uint8_t *dest) { uint32_t dest32; - int ret = parse_hex_u32(val, &dest32); - *dest = dest32; + int ret = parse_hex_u32(val, &dest32); + *dest = dest32; return ret; } - int parse_hex_u16(char *val, uint16_t *dest) { uint32_t dest32; - int ret = parse_hex_u32(val, &dest32); - *dest = dest32; + int ret = parse_hex_u32(val, &dest32); + *dest = dest32; return ret; } - int parse_hex_u32(char *val, uint32_t *dest) { - int i, len = strlen(val); + int i, len = strlen(val); uint8_t digit; *dest = 0; for (i = 0; i < len; i++) { - if ((val[i] >= 0x30) && (val[i] <= 0x39)) - digit = val[i] - 0x30; - else if ((val[i] >= 0x41) && (val[i] <= 0x46)) - digit = val[i] - 0x37; - else if ((val[i] >= 0x61) && (val[i] <= 0x66)) - digit = val[i] - 0x57; - else - return 0; - *dest = (*dest << 4) | digit; + if ((val[i] >= 0x30) && (val[i] <= 0x39)) + digit = val[i] - 0x30; + else if ((val[i] >= 0x41) && (val[i] <= 0x46)) + digit = val[i] - 0x37; + else if ((val[i] >= 0x61) && (val[i] <= 0x66)) + digit = val[i] - 0x57; + else + return 0; + *dest = (*dest << 4) | digit; } return 1; } - /* Comparator functions. */ int comp_ui8(const void *elem1, const void *elem2) @@ -90,7 +85,6 @@ comp_ui8(const void *elem1, const void *elem2) return ((a < b) ? -1 : ((a > b) ? 1 : 0)); } - /* System functions. */ #ifdef __WATCOMC__ /* Defined in header. */ @@ -101,7 +95,6 @@ cli() __asm__("cli"); } - void sti() { @@ -113,14 +106,12 @@ cli() { } - void sti() { } #endif - /* Terminal functions. */ #ifdef __WATCOMC__ int @@ -131,7 +122,6 @@ term_get_size_x() return vc.numtextcols; } - int term_get_size_y() { @@ -140,7 +130,6 @@ term_get_size_y() return vc.numtextrows; } - int term_get_cursor_pos(uint8_t *x, uint8_t *y) { @@ -152,7 +141,6 @@ term_get_cursor_pos(uint8_t *x, uint8_t *y) return 1; } - int term_set_cursor_pos(uint8_t x, uint8_t y) { @@ -163,14 +151,12 @@ term_set_cursor_pos(uint8_t x, uint8_t y) return 1; } - void term_unbuffer_stdout() { setbuf(stdout, NULL); } - void term_final_linebreak() { @@ -183,34 +169,29 @@ term_get_size_x() return 80; } - int term_get_size_y() { return 25; } - int term_get_cursor_pos(uint8_t *x, uint8_t *y) { return 0; } - int term_set_cursor_pos(uint8_t x, uint8_t y) { return 0; } - void term_unbuffer_stdout() { } - void term_final_linebreak() { @@ -218,7 +199,6 @@ term_final_linebreak() } #endif - /* Port I/O functions. */ #ifdef __WATCOMC__ /* Defined in header. */ @@ -227,47 +207,54 @@ uint8_t inb(uint16_t port) { uint8_t ret; - __asm__ __volatile__("inb %1, %0" : "=a" (ret) : "Nd" (port)); + __asm__ __volatile__("inb %1, %0" + : "=a"(ret) + : "Nd"(port)); return ret; } - void outb(uint16_t port, uint8_t val) { - __asm__ __volatile__("outb %0, %1" : : "a" (val), "Nd" (port)); + __asm__ __volatile__("outb %0, %1" + : + : "a"(val), "Nd"(port)); } - uint16_t inw(uint16_t port) { uint16_t ret; - __asm__ __volatile__("inw %1, %0" : "=a" (ret) : "Nd" (port)); + __asm__ __volatile__("inw %1, %0" + : "=a"(ret) + : "Nd"(port)); return ret; } - void outw(uint16_t port, uint16_t val) { - __asm__ __volatile__("outw %0, %1" : : "a" (val), "Nd" (port)); + __asm__ __volatile__("outw %0, %1" + : + : "a"(val), "Nd"(port)); } - uint32_t inl(uint16_t port) { uint32_t ret; - __asm__ __volatile__("inl %1, %0" : "=a" (ret) : "Nd" (port)); + __asm__ __volatile__("inl %1, %0" + : "=a"(ret) + : "Nd"(port)); return ret; } - void outl(uint16_t port, uint32_t val) { - __asm__ __volatile__("outl %0, %1" : : "a" (val), "Nd" (port)); + __asm__ __volatile__("outl %0, %1" + : + : "a"(val), "Nd"(port)); } #else uint8_t @@ -276,71 +263,63 @@ inb(uint16_t port) return 0xff; } - void outb(uint16_t port, uint8_t val) { } - uint16_t inw(uint16_t port) { return 0xffff; } - void outw(uint16_t port, uint16_t val) { } - uint32_t inl(uint16_t port) { return 0xffffffff; } - void outl(uint16_t port, uint32_t val) { } #endif - uint16_t io_find_range(uint16_t size) { uint16_t base; for (base = 0x1000; base >= 0x1000; base += size) { - /* Test first and last words only, as poking through the entire space - can lead to trouble (VIA ACPI has magic reads that hang the CPU). */ - if ((inw(base) == 0xffff) && (inw(base + size - 2) == 0xffff)) - return base; + /* Test first and last words only, as poking through the entire space + can lead to trouble (VIA ACPI has magic reads that hang the CPU). */ + if ((inw(base) == 0xffff) && (inw(base + size - 2) == 0xffff)) + return base; } return 0; } - /* PCI functions. */ uint32_t pci_cf8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) { /* Generate a PCI port CF8h dword. */ multi_t ret; - ret.u8[3] = 0x80; - ret.u8[2] = bus; - ret.u8[1] = dev << 3; + ret.u8[3] = 0x80; + ret.u8[2] = bus; + ret.u8[1] = dev << 3; ret.u8[1] |= func & 7; - ret.u8[0] = reg & 0xfc; + ret.u8[0] = reg & 0xfc; return ret.u32; } - uint16_t pci_get_io_bar(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint16_t size, const char *name) { @@ -351,41 +330,40 @@ pci_get_io_bar(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint16_t siz /* Read BAR register. */ ret = pci_readw(bus, dev, func, reg); if (!(ret & 0x0001) || (ret == 0xffff)) { - temp = pci_readw(bus, dev, func, reg | 0x2); - printf("invalid! (%04X%04X)", temp, ret); - ret = 0; + temp = pci_readw(bus, dev, func, reg | 0x2); + printf("invalid! (%04X%04X)", temp, ret); + ret = 0; } else { - /* Assign BAR if unassigned. */ - ret &= ~(size - 1); - if (ret) { - printf("assigned to %04X", ret); - } else { - printf("unassigned "); + /* Assign BAR if unassigned. */ + ret &= ~(size - 1); + if (ret) { + printf("assigned to %04X", ret); + } else { + printf("unassigned "); - /* Find I/O range for the BAR. */ - ret = io_find_range(size); - if (ret) { - /* Assign and check value. */ - pci_writew(bus, dev, func, reg, ret | 0x0001); - temp = pci_readw(bus, dev, func, reg); - if ((temp & ~(size - 1)) == ret) { - printf("(assigning to %04X)", ret); - } else { - ret = pci_readw(bus, dev, func, reg | 0x2); - printf("and not responding! (%04X%04X)", ret, temp); - ret = 0; - } - } else { - printf("and no suitable range was found!"); - } - } + /* Find I/O range for the BAR. */ + ret = io_find_range(size); + if (ret) { + /* Assign and check value. */ + pci_writew(bus, dev, func, reg, ret | 0x0001); + temp = pci_readw(bus, dev, func, reg); + if ((temp & ~(size - 1)) == ret) { + printf("(assigning to %04X)", ret); + } else { + ret = pci_readw(bus, dev, func, reg | 0x2); + printf("and not responding! (%04X%04X)", ret, temp); + ret = 0; + } + } else { + printf("and no suitable range was found!"); + } + } } printf("\n"); return ret; } - #ifdef IS_32BIT uint32_t pci_get_mem_bar(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint32_t size, const char *name) @@ -397,15 +375,15 @@ pci_get_mem_bar(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint32_t si /* Read BAR register. */ ret = pci_readl(bus, dev, func, reg); if ((ret & 0x00000001) || (ret == 0xffffffff)) { - printf("invalid! (%08X)", ret); - ret = 0; + printf("invalid! (%08X)", ret); + ret = 0; } else { - /* Don't even try to find a valid memory range if the BAR is unassigned. */ - ret &= ~(size - 1); - if (ret) - printf("assigned to %08X", ret); - else - printf("unassigned!"); + /* Don't even try to find a valid memory range if the BAR is unassigned. */ + ret &= ~(size - 1); + if (ret) + printf("assigned to %08X", ret); + else + printf("unassigned!"); } printf("\n"); @@ -413,7 +391,6 @@ pci_get_mem_bar(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint32_t si } #endif - int pci_init() { @@ -425,55 +402,53 @@ pci_init() outl(0xcf8, cf8.u32); cf8.u32 = inl(0xcf8); if (cf8.u32 == 0x80001234) { - pci_mechanism = 1; - pci_device_count = 32; + pci_mechanism = 1; + pci_device_count = 32; } else { - outb(0xcf8, 0x00); - outb(0xcfa, 0x00); - if ((inb(0xcf8) == 0x00) && (inb(0xcfa) == 0x00)) { - pci_mechanism = 2; - pci_device_count = 16; - } + outb(0xcf8, 0x00); + outb(0xcfa, 0x00); + if ((inb(0xcf8) == 0x00) && (inb(0xcfa) == 0x00)) { + pci_mechanism = 2; + pci_device_count = 16; + } } sti(); if (pci_mechanism == 0) - printf("Failed to probe PCI configuration mechanism (%04X%04X). Is this a PCI system?\n", cf8.u16[1], cf8.u16[0]); + printf("Failed to probe PCI configuration mechanism (%04X%04X). Is this a PCI system?\n", cf8.u16[1], cf8.u16[0]); return pci_mechanism; } - uint8_t pci_readb(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) { - uint8_t ret; + uint8_t ret; uint16_t data_port; uint32_t cf8; switch (pci_mechanism) { - case 1: - data_port = 0xcfc | (reg & 0x03); - cf8 = pci_cf8(bus, dev, func, reg); - cli(); - outl(0xcf8, cf8); - ret = inb(data_port); - sti(); - break; + case 1: + data_port = 0xcfc | (reg & 0x03); + cf8 = pci_cf8(bus, dev, func, reg); + cli(); + outl(0xcf8, cf8); + ret = inb(data_port); + sti(); + break; - case 2: - cf8 = pci_readl(bus, dev, func, reg); - ret = cf8 >> ((reg & 0x03) << 3); - break; + case 2: + cf8 = pci_readl(bus, dev, func, reg); + ret = cf8 >> ((reg & 0x03) << 3); + break; - default: - ret = 0xff; - break; + default: + ret = 0xff; + break; } return ret; } - uint16_t pci_readw(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) { @@ -481,29 +456,28 @@ pci_readw(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) uint32_t cf8; switch (pci_mechanism) { - case 1: - data_port = 0xcfc | (reg & 0x02); - cf8 = pci_cf8(bus, dev, func, reg); - cli(); - outl(0xcf8, cf8); - ret = inw(data_port); - sti(); - break; + case 1: + data_port = 0xcfc | (reg & 0x02); + cf8 = pci_cf8(bus, dev, func, reg); + cli(); + outl(0xcf8, cf8); + ret = inw(data_port); + sti(); + break; - case 2: - cf8 = pci_readl(bus, dev, func, reg); - ret = cf8 >> ((reg & 0x02) << 3); - break; + case 2: + cf8 = pci_readl(bus, dev, func, reg); + ret = cf8 >> ((reg & 0x02) << 3); + break; - default: - ret = 0xffff; - break; + default: + ret = 0xffff; + break; } return ret; } - uint32_t pci_readl(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) { @@ -511,89 +485,86 @@ pci_readl(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) uint32_t ret, cf8; switch (pci_mechanism) { - case 1: - cf8 = pci_cf8(bus, dev, func, reg); - cli(); - outl(0xcf8, cf8); - ret = inl(0xcfc); - sti(); - break; + case 1: + cf8 = pci_cf8(bus, dev, func, reg); + cli(); + outl(0xcf8, cf8); + ret = inl(0xcfc); + sti(); + break; - case 2: - func = 0x80 | (func << 1); - data_port = 0xc000 | (dev << 8) | (reg & 0xfc); - cli(); - outb(0xcf8, func); - outb(0xcfa, bus); - ret = inl(data_port); - sti(); - break; + case 2: + func = 0x80 | (func << 1); + data_port = 0xc000 | (dev << 8) | (reg & 0xfc); + cli(); + outb(0xcf8, func); + outb(0xcfa, bus); + ret = inl(data_port); + sti(); + break; - default: - ret = 0xffffffff; - break; + default: + ret = 0xffffffff; + break; } return ret; } - void pci_writeb(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint8_t val) { - uint8_t shift; + uint8_t shift; uint16_t data_port; uint32_t cf8; switch (pci_mechanism) { - case 1: - data_port = 0xcfc | (reg & 0x03); - cf8 = pci_cf8(bus, dev, func, reg); - cli(); - outl(0xcf8, cf8); - outb(data_port, val); - sti(); - break; + case 1: + data_port = 0xcfc | (reg & 0x03); + cf8 = pci_cf8(bus, dev, func, reg); + cli(); + outl(0xcf8, cf8); + outb(data_port, val); + sti(); + break; - case 2: - cf8 = pci_readl(bus, dev, func, reg); - shift = (reg & 0x03) << 3; - cf8 &= ~(0x000000ff << shift); - cf8 |= val << shift; - pci_writel(bus, dev, func, reg, cf8); - break; + case 2: + cf8 = pci_readl(bus, dev, func, reg); + shift = (reg & 0x03) << 3; + cf8 &= ~(0x000000ff << shift); + cf8 |= val << shift; + pci_writel(bus, dev, func, reg, cf8); + break; } } - void pci_writew(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint16_t val) { - uint8_t shift; + uint8_t shift; uint16_t data_port; uint32_t cf8; switch (pci_mechanism) { - case 1: - data_port = 0xcfc | (reg & 0x02); - cf8 = pci_cf8(bus, dev, func, reg); - cli(); - outl(0xcf8, cf8); - outw(data_port, val); - sti(); - break; + case 1: + data_port = 0xcfc | (reg & 0x02); + cf8 = pci_cf8(bus, dev, func, reg); + cli(); + outl(0xcf8, cf8); + outw(data_port, val); + sti(); + break; - case 2: - cf8 = pci_readl(bus, dev, func, reg); - shift = (reg & 0x02) << 3; - cf8 &= ~(0x0000ffff << shift); - cf8 |= val << shift; - pci_writel(bus, dev, func, reg, cf8); - break; + case 2: + cf8 = pci_readl(bus, dev, func, reg); + shift = (reg & 0x02) << 3; + cf8 &= ~(0x0000ffff << shift); + cf8 |= val << shift; + pci_writel(bus, dev, func, reg, cf8); + break; } } - void pci_writel(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint32_t val) { @@ -601,82 +572,81 @@ pci_writel(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint32_t val) uint32_t cf8; switch (pci_mechanism) { - case 1: - cf8 = pci_cf8(bus, dev, func, reg); - cli(); - outl(0xcf8, cf8); - outl(0xcfc, val); - sti(); - break; + case 1: + cf8 = pci_cf8(bus, dev, func, reg); + cli(); + outl(0xcf8, cf8); + outl(0xcfc, val); + sti(); + break; - case 2: - func = 0x80 | (func << 1); - data_port = 0xc000 | (dev << 8) | (reg & 0xfc); - cli(); - outb(0xcf8, func); - outb(0xcfa, bus); - outl(data_port, val); - sti(); - break; + case 2: + func = 0x80 | (func << 1); + data_port = 0xc000 | (dev << 8) | (reg & 0xfc); + cli(); + outb(0xcf8, func); + outb(0xcfa, bus); + outl(data_port, val); + sti(); + break; } } - void pci_scan_bus(uint8_t bus, - void (*callback)(uint8_t bus, uint8_t dev, uint8_t func, - uint16_t ven_id, uint16_t dev_id)) + void (*callback)(uint8_t bus, uint8_t dev, uint8_t func, + uint16_t ven_id, uint16_t dev_id)) { uint8_t dev, func, header_type; multi_t dev_id; /* Iterate through devices. */ for (dev = 0; dev < pci_device_count; dev++) { - /* Iterate through functions. */ - for (func = 0; func < 8; func++) { - /* Read vendor/device ID. */ + /* Iterate through functions. */ + for (func = 0; func < 8; func++) { + /* Read vendor/device ID. */ #ifdef DEBUG - if ((bus < DEBUG) && (dev <= bus) && (func == 0)) { - dev_id.u16[0] = rand(); - dev_id.u16[1] = rand(); - } else { - dev_id.u32 = 0xffffffff; - } + if ((bus < DEBUG) && (dev <= bus) && (func == 0)) { + dev_id.u16[0] = rand(); + dev_id.u16[1] = rand(); + } else { + dev_id.u32 = 0xffffffff; + } #else - dev_id.u32 = pci_readl(bus, dev, func, 0x00); + dev_id.u32 = pci_readl(bus, dev, func, 0x00); #endif - /* Callback if this is a valid ID. */ - if (dev_id.u32 && (dev_id.u32 != 0xffffffff)) { - callback(bus, dev, func, dev_id.u16[0], dev_id.u16[1]); - } else { - /* Stop or move on to the next function if there's nothing here. */ - if (func) - continue; - else - break; - } + /* Callback if this is a valid ID. */ + if (dev_id.u32 && (dev_id.u32 != 0xffffffff)) { + callback(bus, dev, func, dev_id.u16[0], dev_id.u16[1]); + } else { + /* Stop or move on to the next function if there's nothing here. */ + if (func) + continue; + else + break; + } - /* Read header type. */ + /* Read header type. */ #ifdef DEBUG - header_type = (bus < (DEBUG - 1)) ? 0x01 : 0x00; + header_type = (bus < (DEBUG - 1)) ? 0x01 : 0x00; #else - header_type = pci_readb(bus, dev, func, 0x0e); + header_type = pci_readb(bus, dev, func, 0x0e); #endif - /* If this is a bridge, mark that we should probe its bus. */ - if (header_type & 0x7f) { - /* Scan the secondary bus. */ + /* If this is a bridge, mark that we should probe its bus. */ + if (header_type & 0x7f) { + /* Scan the secondary bus. */ #ifdef DEBUG - pci_scan_bus(bus + 1, callback); + pci_scan_bus(bus + 1, callback); #else - pci_scan_bus(pci_readb(bus, dev, func, 0x19), callback); + pci_scan_bus(pci_readb(bus, dev, func, 0x19), callback); #endif - } + } - /* If we're at the first function, stop if this is not a multi-function device. */ - if ((func == 0) && !(header_type & 0x80)) - break; - } + /* If we're at the first function, stop if this is not a multi-function device. */ + if ((func == 0) && !(header_type & 0x80)) + break; + } } } diff --git a/clib/clib.h b/clib/clib.h index fab0d86..bec9f43 100644 --- a/clib/clib.h +++ b/clib/clib.h @@ -16,129 +16,125 @@ * */ #ifndef CLIB_H -# define CLIB_H +#define CLIB_H /* Common macros. */ -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define ABS(x) ((x) > 0 ? (x) : -(x)) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define ABS(x) ((x) > 0 ? (x) : -(x)) /* Compiler compatibility macros. */ #ifdef __GNUC__ -# define PACKED __attribute__((packed)) +# define PACKED __attribute__((packed)) #else -# define PACKED +# define PACKED #endif /* Platform-specific macros. */ #ifndef __POSIX_UEFI__ -# define FMT_FLOAT_SUPPORTED 1 +# define FMT_FLOAT_SUPPORTED 1 #endif #if !defined(__WATCOMC__) || defined(M_I386) -# define IS_32BIT 1 +# define IS_32BIT 1 #endif - #pragma pack(push, 0) /* Convenience type for breaking a dword value down into words and bytes. */ typedef union { - uint8_t u8[4]; + uint8_t u8[4]; uint16_t u16[2]; uint32_t u32; } multi_t; #pragma pack(pop) - /* Global variables. */ -extern uint8_t pci_mechanism, pci_device_count; - +extern uint8_t pci_mechanism, pci_device_count; /* String functions. */ -extern int parse_hex_u8(char *val, uint8_t *dest); -extern int parse_hex_u16(char *val, uint16_t *dest); -extern int parse_hex_u32(char *val, uint32_t *dest); +extern int parse_hex_u8(char *val, uint8_t *dest); +extern int parse_hex_u16(char *val, uint16_t *dest); +extern int parse_hex_u32(char *val, uint32_t *dest); /* Comparator functions. */ -extern int comp_ui8(const void *elem1, const void *elem2); +extern int comp_ui8(const void *elem1, const void *elem2); /* System functions. */ #ifdef __WATCOMC__ -void cli(); -# pragma aux cli = "cli"; -void sti(); -# pragma aux sti = "sti"; +void cli(); +# pragma aux cli = "cli"; +void sti(); +# pragma aux sti = "sti"; #else -extern void cli(); -extern void sti(); +extern void cli(); +extern void sti(); #endif /* Terminal functions. */ -extern int term_get_size_x(); -extern int term_get_size_y(); -extern int term_get_cursor_pos(uint8_t *x, uint8_t *y); -extern int term_set_cursor_pos(uint8_t x, uint8_t y); -extern void term_unbuffer_stdout(); -extern void term_final_linebreak(); +extern int term_get_size_x(); +extern int term_get_size_y(); +extern int term_get_cursor_pos(uint8_t *x, uint8_t *y); +extern int term_set_cursor_pos(uint8_t x, uint8_t y); +extern void term_unbuffer_stdout(); +extern void term_final_linebreak(); /* Port I/O functions. */ #ifdef __WATCOMC__ -uint8_t inb(uint16_t port); -# pragma aux inb = "in al, dx" parm [dx] value [al]; -void outb(uint16_t port, uint8_t data); -# pragma aux outb = "out dx, al" parm [dx] [al]; -uint16_t inw(uint16_t port); -# pragma aux inw = "in ax, dx" parm [dx] value [ax]; -void outw(uint16_t port, uint16_t data); -# pragma aux outw = "out dx, ax" parm [dx] [ax]; -# ifdef M_I386 -uint32_t inl(uint16_t port); -# pragma aux inl = "in eax, dx" parm [dx] value [eax]; -void outl(uint16_t port, uint32_t data); -# pragma aux outl = "out dx, eax" parm [dx] [eax]; -# else +uint8_t inb(uint16_t port); +# pragma aux inb = "in al, dx" parm[dx] value[al]; +void outb(uint16_t port, uint8_t data); +# pragma aux outb = "out dx, al" parm[dx][al]; +uint16_t inw(uint16_t port); +# pragma aux inw = "in ax, dx" parm[dx] value[ax]; +void outw(uint16_t port, uint16_t data); +# pragma aux outw = "out dx, ax" parm[dx][ax]; +# ifdef M_I386 +uint32_t inl(uint16_t port); +# pragma aux inl = "in eax, dx" parm[dx] value[eax]; +void outl(uint16_t port, uint32_t data); +# pragma aux outl = "out dx, eax" parm[dx][eax]; +# else /* Some manual prefixing trickery to perform 32-bit I/O and access the extended part of EAX in real mode. Exchanging is necessary due to Watcom ignoring the order registers are specified in... */ -uint32_t inl(uint16_t port); -# pragma aux inl = "db 0x66" "in ax, dx" /* in eax, dx */ \ - "mov cx, ax" \ - "db 0x66, 0xc1, 0xe8, 0x10" /* shr eax, 16 */ \ - "xchg ax, cx" \ - parm [dx] \ - value [ax cx]; -void outl(uint16_t port, uint32_t data); -# pragma aux outl = "xchg ax, cx" \ - "db 0x66, 0xc1, 0xe0, 0x10" /* shl eax, 16 */ \ - "mov ax, cx" \ - "db 0x66" "out dx, ax" /* out dx, eax */ \ - parm [dx] [ax cx] \ - modify [ax cx]; -# endif +uint32_t inl(uint16_t port); +# pragma aux inl = "db 0x66" \ + "in ax, dx" /* in eax, dx */ \ + "mov cx, ax" \ + "db 0x66, 0xc1, 0xe8, 0x10" /* shr eax, 16 */ \ + "xchg ax, cx" parm[dx] value[ax cx]; +void outl(uint16_t port, uint32_t data); +# pragma aux outl = "xchg ax, cx" \ + "db 0x66, 0xc1, 0xe0, 0x10" /* shl eax, 16 */ \ + "mov ax, cx" \ + "db 0x66" \ + "out dx, ax" /* out dx, eax */ \ + parm[dx][ax cx] modify[ax cx]; +# endif #else -extern uint8_t inb(uint16_t port); -extern void outb(uint16_t port, uint8_t data); -extern uint16_t inw(uint16_t port); -extern void outw(uint16_t port, uint16_t data); -extern uint32_t inl(uint16_t port); -extern void outl(uint16_t port, uint32_t data); +extern uint8_t inb(uint16_t port); +extern void outb(uint16_t port, uint8_t data); +extern uint16_t inw(uint16_t port); +extern void outw(uint16_t port, uint16_t data); +extern uint32_t inl(uint16_t port); +extern void outl(uint16_t port, uint32_t data); #endif -extern uint16_t io_find_range(uint16_t size); +extern uint16_t io_find_range(uint16_t size); /* PCI functions. */ -extern uint32_t pci_cf8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg); -extern uint16_t pci_get_io_bar(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint16_t size, const char *name); +extern uint32_t pci_cf8(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg); +extern uint16_t pci_get_io_bar(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint16_t size, const char *name); #ifdef IS_32BIT -extern uint32_t pci_get_mem_bar(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint32_t size, const char *name); +extern uint32_t pci_get_mem_bar(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint32_t size, const char *name); #endif -extern int pci_init(); -extern uint8_t pci_readb(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg); -extern uint16_t pci_readw(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg); -extern uint32_t pci_readl(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg); -extern void pci_writeb(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint8_t val); -extern void pci_writew(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint16_t val); -extern void pci_writel(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint32_t val); -extern void pci_scan_bus(uint8_t bus, - void (*callback)(uint8_t bus, uint8_t dev, uint8_t func, - uint16_t ven_id, uint16_t dev_id)); +extern int pci_init(); +extern uint8_t pci_readb(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg); +extern uint16_t pci_readw(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg); +extern uint32_t pci_readl(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg); +extern void pci_writeb(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint8_t val); +extern void pci_writew(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint16_t val); +extern void pci_writel(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, uint32_t val); +extern void pci_scan_bus(uint8_t bus, + void (*callback)(uint8_t bus, uint8_t dev, uint8_t func, + uint16_t ven_id, uint16_t dev_id)); #endif diff --git a/cp437/cp437.c b/cp437/cp437.c index c1cf91b..3f7b2ac 100644 --- a/cp437/cp437.c +++ b/cp437/cp437.c @@ -18,33 +18,31 @@ #include #include - /* This is the text renderer's CP437 character conversion table, with 09, 0A and 0D modified for text processing purposes. */ static const char *cp437[] = { - /* 00 */ " ", "\xE2\x98\xBA", "\xE2\x98\xBB", "\xE2\x99\xA5", "\xE2\x99\xA6", "\xE2\x99\xA3", "\xE2\x99\xA0", "\xE2\x80\xA2", "\xE2\x97\x98", "\t", "\n", "\xE2\x99\x82", "\xE2\x99\x80", "\r", "\xE2\x99\xAB", "\xE2\x98\xBC", - /* 10 */ "\xE2\x96\xBA", "\xE2\x97\x84", "\xE2\x86\x95", "\xE2\x80\xBC", "\xC2\xB6", "\xC2\xA7", "\xE2\x96\xAC", "\xE2\x86\xA8", "\xE2\x86\x91", "\xE2\x86\x93", "\xE2\x86\x92", "\xE2\x86\x90", "\xE2\x88\x9F", "\xE2\x86\x94", "\xE2\x96\xB2", "\xE2\x96\xBC", - /* 20 */ " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", - /* 30 */ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", - /* 40 */ "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", - /* 50 */ "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", - /* 60 */ "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", - /* 70 */ "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "\xE2\x8C\x82", - /* 80 */ "\xC3\x87", "\xC3\xBC", "\xC3\xA9", "\xC3\xA2", "\xC3\xA4", "\xC3\xA0", "\xC3\xA5", "\xC3\xA7", "\xC3\xAA", "\xC3\xAB", "\xC3\xA8", "\xC3\xAF", "\xC3\xAE", "\xC3\xAC", "\xC3\x84", "\xC3\x85", - /* 90 */ "\xC3\x89", "\xC3\xA6", "\xC3\x86", "\xC3\xB4", "\xC3\xB6", "\xC3\xB2", "\xC3\xBB", "\xC3\xB9", "\xC3\xBF", "\xC3\x96", "\xC3\x9C", "\xC2\xA2", "\xC2\xA3", "\xC2\xA5", "\xE2\x82\xA7", "\xC6\x92", - /* A0 */ "\xC3\xA1", "\xC3\xAD", "\xC3\xB3", "\xC3\xBA", "\xC3\xB1", "\xC3\x91", "\xC2\xAA", "\xC2\xBA", "\xC2\xBF", "\xE2\x8C\x90", "\xC2\xAC", "\xC2\xBD", "\xC2\xBC", "\xC2\xA1", "\xC2\xAB", "\xC2\xBB", + /* 00 */ " ", "\xE2\x98\xBA", "\xE2\x98\xBB", "\xE2\x99\xA5", "\xE2\x99\xA6", "\xE2\x99\xA3", "\xE2\x99\xA0", "\xE2\x80\xA2", "\xE2\x97\x98", "\t", "\n", "\xE2\x99\x82", "\xE2\x99\x80", "\r", "\xE2\x99\xAB", "\xE2\x98\xBC", + /* 10 */ "\xE2\x96\xBA", "\xE2\x97\x84", "\xE2\x86\x95", "\xE2\x80\xBC", "\xC2\xB6", "\xC2\xA7", "\xE2\x96\xAC", "\xE2\x86\xA8", "\xE2\x86\x91", "\xE2\x86\x93", "\xE2\x86\x92", "\xE2\x86\x90", "\xE2\x88\x9F", "\xE2\x86\x94", "\xE2\x96\xB2", "\xE2\x96\xBC", + /* 20 */ " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", + /* 30 */ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", + /* 40 */ "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", + /* 50 */ "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", + /* 60 */ "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", + /* 70 */ "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "\xE2\x8C\x82", + /* 80 */ "\xC3\x87", "\xC3\xBC", "\xC3\xA9", "\xC3\xA2", "\xC3\xA4", "\xC3\xA0", "\xC3\xA5", "\xC3\xA7", "\xC3\xAA", "\xC3\xAB", "\xC3\xA8", "\xC3\xAF", "\xC3\xAE", "\xC3\xAC", "\xC3\x84", "\xC3\x85", + /* 90 */ "\xC3\x89", "\xC3\xA6", "\xC3\x86", "\xC3\xB4", "\xC3\xB6", "\xC3\xB2", "\xC3\xBB", "\xC3\xB9", "\xC3\xBF", "\xC3\x96", "\xC3\x9C", "\xC2\xA2", "\xC2\xA3", "\xC2\xA5", "\xE2\x82\xA7", "\xC6\x92", + /* A0 */ "\xC3\xA1", "\xC3\xAD", "\xC3\xB3", "\xC3\xBA", "\xC3\xB1", "\xC3\x91", "\xC2\xAA", "\xC2\xBA", "\xC2\xBF", "\xE2\x8C\x90", "\xC2\xAC", "\xC2\xBD", "\xC2\xBC", "\xC2\xA1", "\xC2\xAB", "\xC2\xBB", /* B0 */ "\xE2\x96\x91", "\xE2\x96\x92", "\xE2\x96\x93", "\xE2\x94\x82", "\xE2\x94\xA4", "\xE2\x95\xA1", "\xE2\x95\xA2", "\xE2\x95\x96", "\xE2\x95\x95", "\xE2\x95\xA3", "\xE2\x95\x91", "\xE2\x95\x97", "\xE2\x95\x9D", "\xE2\x95\x9C", "\xE2\x95\x9B", "\xE2\x94\x90", /* C0 */ "\xE2\x94\x94", "\xE2\x94\xB4", "\xE2\x94\xAC", "\xE2\x94\x9C", "\xE2\x94\x80", "\xE2\x94\xBC", "\xE2\x95\x9E", "\xE2\x95\x9F", "\xE2\x95\x9A", "\xE2\x95\x94", "\xE2\x95\xA9", "\xE2\x95\xA6", "\xE2\x95\xA0", "\xE2\x95\x90", "\xE2\x95\xAC", "\xE2\x95\xA7", /* D0 */ "\xE2\x95\xA8", "\xE2\x95\xA4", "\xE2\x95\xA5", "\xE2\x95\x99", "\xE2\x95\x98", "\xE2\x95\x92", "\xE2\x95\x93", "\xE2\x95\xAB", "\xE2\x95\xAA", "\xE2\x94\x98", "\xE2\x94\x8C", "\xE2\x96\x88", "\xE2\x96\x84", "\xE2\x96\x8C", "\xE2\x96\x90", "\xE2\x96\x80", - /* E0 */ "\xCE\xB1", "\xC3\x9F", "\xCE\x93", "\xCF\x80", "\xCE\xA3", "\xCF\x83", "\xC2\xB5", "\xCF\x84", "\xCE\xA6", "\xCE\x98", "\xCE\xA9", "\xCE\xB4", "\xE2\x88\x9E", "\xCF\x86", "\xCE\xB5", "\xE2\x88\xA9", - /* F0 */ "\xE2\x89\xA1", "\xC2\xB1", "\xE2\x89\xA5", "\xE2\x89\xA4", "\xE2\x8C\xA0", "\xE2\x8C\xA1", "\xC3\xB7", "\xE2\x89\x88", "\xC2\xB0", "\xE2\x88\x99", "\xC2\xB7", "\xE2\x88\x9A", "\xE2\x81\xBF", "\xC2\xB2", "\xE2\x96\xA0", "\xC2\xA0" + /* E0 */ "\xCE\xB1", "\xC3\x9F", "\xCE\x93", "\xCF\x80", "\xCE\xA3", "\xCF\x83", "\xC2\xB5", "\xCF\x84", "\xCE\xA6", "\xCE\x98", "\xCE\xA9", "\xCE\xB4", "\xE2\x88\x9E", "\xCF\x86", "\xCE\xB5", "\xE2\x88\xA9", + /* F0 */ "\xE2\x89\xA1", "\xC2\xB1", "\xE2\x89\xA5", "\xE2\x89\xA4", "\xE2\x8C\xA0", "\xE2\x8C\xA1", "\xC3\xB7", "\xE2\x89\x88", "\xC2\xB0", "\xE2\x88\x99", "\xC2\xB7", "\xE2\x88\x9A", "\xE2\x81\xBF", "\xC2\xB2", "\xE2\x96\xA0", "\xC2\xA0" }; - int main(int argc, char **argv) { - int i, j, success; + int i, j, success; char *buf; FILE *fin, *fout; @@ -54,71 +52,71 @@ main(int argc, char **argv) /* Print usage if no input files were specified. */ if (argc < 2) { - printf("Usage:\n"); - printf("\n"); - printf("%s infile [infile...]\n", argv[0]); - printf("- Converts UTF-8 input file(s) to CP437 output file(s) with .cp437 appended.\n"); - printf(" The new file names are also printed to stdout.\n"); - return 1; + printf("Usage:\n"); + printf("\n"); + printf("%s infile [infile...]\n", argv[0]); + printf("- Converts UTF-8 input file(s) to CP437 output file(s) with .cp437 appended.\n"); + printf(" The new file names are also printed to stdout.\n"); + return 1; } /* Process input files. */ success = 0; for (i = 1; i < argc; i++) { - /* Open input file. */ - fin = fopen(argv[i], "rb"); - if (!fin) { - fprintf(stderr, "Could not open input file \"%s\"\n", argv[i]); - continue; - } + /* Open input file. */ + fin = fopen(argv[i], "rb"); + if (!fin) { + fprintf(stderr, "Could not open input file \"%s\"\n", argv[i]); + continue; + } - /* Generate output file name. */ - buf = malloc(strlen(argv[i]) + 7); - sprintf(buf, "%s_cp437", argv[i]); + /* Generate output file name. */ + buf = malloc(strlen(argv[i]) + 7); + sprintf(buf, "%s_cp437", argv[i]); - /* Open output file. */ - fout = fopen(buf, "wb"); - if (!fout) { - fclose(fin); - fprintf(stderr, "Could not open output file \"%s\"\n", buf); - continue; - } + /* Open output file. */ + fout = fopen(buf, "wb"); + if (!fout) { + fclose(fin); + fprintf(stderr, "Could not open output file \"%s\"\n", buf); + continue; + } - printf("%s\n", buf); + printf("%s\n", buf); - /* Perform the conversion. */ - while (!feof(fin)) { - /* Read UTF-8 codepoint. */ - memset(buf, 0, 5); - fread(buf, 1, 1, fin); - if ((buf[0] & 0xe0) == 0xc0) - fread(&buf[1], 1, 1, fin); - else if ((buf[0] & 0xf0) == 0xe0) - fread(&buf[1], 2, 1, fin); - else if ((buf[0] & 0xf8) == 0xf0) - fread(&buf[1], 3, 1, fin); + /* Perform the conversion. */ + while (!feof(fin)) { + /* Read UTF-8 codepoint. */ + memset(buf, 0, 5); + fread(buf, 1, 1, fin); + if ((buf[0] & 0xe0) == 0xc0) + fread(&buf[1], 1, 1, fin); + else if ((buf[0] & 0xf0) == 0xe0) + fread(&buf[1], 2, 1, fin); + else if ((buf[0] & 0xf8) == 0xf0) + fread(&buf[1], 3, 1, fin); - for (j = 1; j < (sizeof(cp437) / sizeof(cp437[0])); j++) { - if (!strcmp(buf, cp437[j])) { - fputc(j, fout); - break; - } - } - } + for (j = 1; j < (sizeof(cp437) / sizeof(cp437[0])); j++) { + if (!strcmp(buf, cp437[j])) { + fputc(j, fout); + break; + } + } + } - /* Clean up. */ - free(buf); - fclose(fin); - fclose(fout); + /* Clean up. */ + free(buf); + fclose(fin); + fclose(fout); - /* Increase success counter. */ - success++; + /* Increase success counter. */ + success++; } if (--i == success) - return 0; + return 0; else if (success > 0) - return 1; + return 1; else - return 2; + return 2; } diff --git a/isapnp/isapnp.c b/isapnp/isapnp.c index 47724f3..c9d6971 100644 --- a/isapnp/isapnp.c +++ b/isapnp/isapnp.c @@ -35,18 +35,15 @@ #include #include "clib.h" - typedef struct _card_ { - char id[8]; + char id[8]; struct _card_ *next; } card_t; - -uint8_t buf[256], buf_pos; +uint8_t buf[256], buf_pos; uint16_t rd_data; -FILE *f; -card_t *first_card = NULL; - +FILE *f; +card_t *first_card = NULL; static inline void io_delay() @@ -54,16 +51,15 @@ io_delay() delay(1); } - static uint8_t -lfsr_advance(int lfsr, int bit) { - register uint8_t next; - next = lfsr >> 1; - next |= (((lfsr ^ next) ^ bit)) << 7; - return next; +lfsr_advance(int lfsr, int bit) +{ + register uint8_t next; + next = lfsr >> 1; + next |= (((lfsr ^ next) ^ bit)) << 7; + return next; } - static void unlock() { @@ -81,12 +77,11 @@ unlock() /* Send key. */ lfsr = 0x6a; for (i = 0; i < 32; i++) { - outb(0x279, lfsr); - lfsr = lfsr_advance(lfsr, 0); + outb(0x279, lfsr); + lfsr = lfsr_advance(lfsr, 0); } } - static int checksum(uint8_t *identifier) { @@ -94,16 +89,15 @@ checksum(uint8_t *identifier) lfsr = 0x6a; for (i = 0; i < 8; i++) { - byte = identifier[i]; - for (j = 0; j < 8; j++) { - lfsr = lfsr_advance(lfsr, byte); - byte >>= 1; - } + byte = identifier[i]; + for (j = 0; j < 8; j++) { + lfsr = lfsr_advance(lfsr, byte); + byte >>= 1; + } } return lfsr; } - static void parse_id(uint8_t *identifier, char *id) { @@ -111,13 +105,12 @@ parse_id(uint8_t *identifier, char *id) vendor = (identifier[0] << 8) | identifier[1]; sprintf(id, "%c%c%c%02X%02X", - '@' + ((vendor >> 10) & 0x1f), - '@' + ((vendor >> 5) & 0x1f), - '@' + (vendor & 0x1f), - identifier[2], identifier[3]); + '@' + ((vendor >> 10) & 0x1f), + '@' + ((vendor >> 5) & 0x1f), + '@' + (vendor & 0x1f), + identifier[2], identifier[3]); } - static int read_resource_data(uint8_t *byte) { @@ -125,21 +118,21 @@ read_resource_data(uint8_t *byte) /* Read byte. */ for (i = 0; i < 20; i++) { - /* Read only if ready. */ - outb(0x279, 0x05); - if (inb(rd_data) & 0x01) { - outb(0x279, 0x04); - *byte = inb(rd_data); - break; - } - io_delay(); + /* Read only if ready. */ + outb(0x279, 0x05); + if (inb(rd_data) & 0x01) { + outb(0x279, 0x04); + *byte = inb(rd_data); + break; + } + io_delay(); } /* Return failure if the read timed out. */ if (i == 20) { - printf("\n> Read timed out at byte %d", ftell(f) + buf_pos + 1); - *byte = 0xff; - return 0; + printf("\n> Read timed out at byte %d", ftell(f) + buf_pos + 1); + *byte = 0xff; + return 0; } /* Add byte to buffer. */ @@ -147,48 +140,47 @@ read_resource_data(uint8_t *byte) /* Flush buffer if full. */ if (!buf_pos && (fwrite(buf, sizeof(buf), 1, f) < 1)) { - printf("\n> File write failed"); - return 0; + printf("\n> File write failed"); + return 0; } /* Return success. */ return 1; } - static char dump_resource_data(char *id) { - uint8_t identifier[9], byte, i, j; + uint8_t identifier[9], byte, i, j; uint16_t data; - card_t *card, *new_card; + card_t *card, *new_card; /* Sanitize parsed PnP ID for filename purposes. */ for (i = 0; i < 3; i++) { - if (id[i] > 'Z') - id[i] = '_'; + if (id[i] > 'Z') + id[i] = '_'; } /* Determine this card's unique identifier. */ - i = 0; + i = 0; card = first_card; while (card) { - /* Increment unique identifier for each card - already found with this sanitized parsed ID. */ - if (!strcmp(card->id, id)) - i++; - card = card->next; + /* Increment unique identifier for each card + already found with this sanitized parsed ID. */ + if (!strcmp(card->id, id)) + i++; + card = card->next; } new_card = malloc(sizeof(card_t)); strcpy(new_card->id, id); new_card->next = NULL; if (!first_card) { - first_card = new_card; + first_card = new_card; } else { - card = first_card; - while (card->next) - card = card->next; - card->next = new_card; + card = first_card; + while (card->next) + card = card->next; + card->next = new_card; } /* Generate a dump file name. */ @@ -199,105 +191,104 @@ dump_resource_data(char *id) /* Open dump file. */ f = fopen(id, "wb"); if (f) { - /* Dump resource data, starting with the header. */ - buf_pos = 0; - for (j = 0; j < 9; j++) - read_resource_data(&byte); + /* Dump resource data, starting with the header. */ + buf_pos = 0; + for (j = 0; j < 9; j++) + read_resource_data(&byte); - /* Flush buffer. */ - if (fwrite(buf, buf_pos, 1, f) < 1) - printf("\n> File write failed"); - buf_pos = 0; + /* Flush buffer. */ + if (fwrite(buf, buf_pos, 1, f) < 1) + printf("\n> File write failed"); + buf_pos = 0; - /* Check for invalid resource data. */ - if (!memcmp(buf, "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9) || - !memcmp(buf, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9)) { - printf("\n> Invalid header (all %02X)", buf[0]); - goto done; - } else { - printf("\n> DEBUG: header: %02X %02X %02X %02X %02X %02X %02X %02X %02X", - buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8]); - } + /* Check for invalid resource data. */ + if (!memcmp(buf, "\x00\x00\x00\x00\x00\x00\x00\x00\x00", 9) || !memcmp(buf, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9)) { + printf("\n> Invalid header (all %02X)", buf[0]); + goto done; + } else { + printf("\n> DEBUG: header: %02X %02X %02X %02X %02X %02X %02X %02X %02X", + buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8]); + } - /* Now dump the resources. */ - j = 0; - while (read_resource_data(&byte)) { - if ((byte == 0x00) || (byte == 0xff)) { - printf("\n> DEBUG: %02X resource", byte); - break; - } + /* Now dump the resources. */ + j = 0; + while (read_resource_data(&byte)) { + if ((byte == 0x00) || (byte == 0xff)) { + printf("\n> DEBUG: %02X resource", byte); + break; + } - /* Determine the amount of bytes to skip depending on resource type. */ - if (byte & 0x80) { /* large resource */ - read_resource_data((uint8_t *) &data); - read_resource_data(((uint8_t *) &data) + 1); + /* Determine the amount of bytes to skip depending on resource type. */ + if (byte & 0x80) { /* large resource */ + read_resource_data((uint8_t *) &data); + read_resource_data(((uint8_t *) &data) + 1); - /* Handle ANSI strings. */ - byte &= 0x7f; - if (byte == 0x02) { - /* Output string. */ - if (!j) - printf("\n>"); - printf(" \""); - while (data--) { - read_resource_data(&byte); - if ((byte != 0x00) && (byte != '\r') && (byte != '\n')) - putchar(byte); - } - printf("\""); - data = 0; - } - } else { /* small resource */ - data = byte & 0x07; + /* Handle ANSI strings. */ + byte &= 0x7f; + if (byte == 0x02) { + /* Output string. */ + if (!j) + printf("\n>"); + printf(" \""); + while (data--) { + read_resource_data(&byte); + if ((byte != 0x00) && (byte != '\r') && (byte != '\n')) + putchar(byte); + } + printf("\""); + data = 0; + } + } else { /* small resource */ + data = byte & 0x07; - /* Handle some resource types. */ - byte = (byte >> 3) & 0x0f; - if ((byte == 0x02) && (data >= 4)) { /* logical device */ - /* Read logical device ID. */ - for (j = 0; j < 4; j++) - read_resource_data(&identifier[j]); - data -= j; + /* Handle some resource types. */ + byte = (byte >> 3) & 0x0f; + if ((byte == 0x02) && (data >= 4)) { /* logical device */ + /* Read logical device ID. */ + for (j = 0; j < 4; j++) + read_resource_data(&identifier[j]); + data -= j; - /* Output logical device ID. */ - parse_id(identifier, id); - printf("\n> %s", id); - } else if (byte == 0x0f) { /* end tag */ - /* Read the rest of this resource (including the checksum), then stop. */ - if (data == 0) /* just in case the checksum isn't covered */ - data++; - while (data--) - read_resource_data(&byte); - break; - } - } + /* Output logical device ID. */ + parse_id(identifier, id); + printf("\n> %s", id); + } else if (byte == 0x0f) { /* end tag */ + /* Read the rest of this resource (including the checksum), then stop. */ + if (data == 0) /* just in case the checksum isn't covered */ + data++; + while (data--) + read_resource_data(&byte); + break; + } + } - /* Skip bytes. */ - while (data--) - read_resource_data(&byte); - } + /* Skip bytes. */ + while (data--) + read_resource_data(&byte); + } - /* Flush buffer if not empty. */ - if (buf_pos && (fwrite(buf, buf_pos, 1, f) < 1)) - printf("\n> File write failed"); + /* Flush buffer if not empty. */ + if (buf_pos && (fwrite(buf, buf_pos, 1, f) < 1)) + printf("\n> File write failed"); - /* Finish the dump. */ -done: fclose(f); - printf("\n"); + /* Finish the dump. */ +done: + fclose(f); + printf("\n"); } else { - printf("\n> File creation failed\n"); + printf("\n> File creation failed\n"); } return i; } - static int try_isolate() { - char id[13]; - uint8_t identifier[9], byte, i, j, seen_55aa, seen_life; + char id[13]; + uint8_t identifier[9], byte, i, j, seen_55aa, seen_life; uint16_t data; - int csn; + int csn; printf("\rScanning on Read Data Port %04X...", rd_data); id[0] = 0; @@ -324,49 +315,49 @@ try_isolate() /* Check for and dump UMC Super I/O. */ outb(0x108, 0xaa); if (inb(0x108) != 0xff) { - outb(0x108, 0xc2); - byte = inb(0x109); - outb(0x108, 0x55); - outb(0x108, 0xc2); - printf("\nUMC Super I/O (C2U=%02X C2L=%02X)", byte, inb(0x109)); + outb(0x108, 0xc2); + byte = inb(0x109); + outb(0x108, 0x55); + outb(0x108, 0xc2); + printf("\nUMC Super I/O (C2U=%02X C2L=%02X)", byte, inb(0x109)); - /* Enable ISAPnP mode. */ - outb(0x108, 0xaa); - outb(0x108, 0xc1); - byte = inb(0x109); - outb(0x108, 0xaa); - outb(0x108, 0xc1); - outb(0x109, byte | 0x80); + /* Enable ISAPnP mode. */ + outb(0x108, 0xaa); + outb(0x108, 0xc1); + byte = inb(0x109); + outb(0x108, 0xaa); + outb(0x108, 0xc1); + outb(0x109, byte | 0x80); - /* Dump resource data. */ - sprintf(id, "UMCSIOP"); - i = dump_resource_data(id); + /* Dump resource data. */ + sprintf(id, "UMCSIOP"); + i = dump_resource_data(id); - /* Dump registers. */ - sprintf(id, "UMCSIOR%c.BIN", i); - printf("> Dumping registers to %s", id); - f = fopen(id, "wb"); - if (f) { - /* Dump registers to the buffer. */ - i = 0xc0; - do { - outb(0x108, 0xaa); - outb(0x108, i); - buf[i] = inb(0x109); - } while (++i != 0xc0); + /* Dump registers. */ + sprintf(id, "UMCSIOR%c.BIN", i); + printf("> Dumping registers to %s", id); + f = fopen(id, "wb"); + if (f) { + /* Dump registers to the buffer. */ + i = 0xc0; + do { + outb(0x108, 0xaa); + outb(0x108, i); + buf[i] = inb(0x109); + } while (++i != 0xc0); - /* Flush buffer. */ - if (fwrite(buf, sizeof(buf), 1, f) < 1) - printf("\n> File write failed"); + /* Flush buffer. */ + if (fwrite(buf, sizeof(buf), 1, f) < 1) + printf("\n> File write failed"); - /* Finish the dump. */ - fclose(f); - printf("\n"); - } else { - printf("\n> File creation failed\n"); - } + /* Finish the dump. */ + fclose(f); + printf("\n"); + } else { + printf("\n> File creation failed\n"); + } - /* Disable ISAPnP mode. */ + /* Disable ISAPnP mode. */ #if 0 outb(0x108, 0xaa); outb(0x108, 0xc1); @@ -375,77 +366,77 @@ try_isolate() outb(0x108, 0xc1); outb(0x109, byte & ~0x80); #endif - outb(0x108, 0x55); + outb(0x108, 0x55); } /* Isolate cards. */ csn = 0; while (1) { - /* Put sleeping cards to Isolation. */ - outb(0x279, 0x01); - io_delay(); + /* Put sleeping cards to Isolation. */ + outb(0x279, 0x01); + io_delay(); - /* Read serial identifier. */ - memset(identifier, 0, sizeof(identifier)); - seen_55aa = seen_life = 0; - for (i = 0; i < sizeof(identifier); i++) { - byte = 0; - for (j = 0; j < 8; j++) { - data = inb(rd_data); - io_delay(); - data = (data << 8) | inb(rd_data); - io_delay(); - byte >>= 1; - if (data != 0xffff) { - seen_life++; - if (data == 0x55aa) { - byte |= 0x80; - seen_55aa++; - } - } - } - identifier[i] = byte; - } + /* Read serial identifier. */ + memset(identifier, 0, sizeof(identifier)); + seen_55aa = seen_life = 0; + for (i = 0; i < sizeof(identifier); i++) { + byte = 0; + for (j = 0; j < 8; j++) { + data = inb(rd_data); + io_delay(); + data = (data << 8) | inb(rd_data); + io_delay(); + byte >>= 1; + if (data != 0xffff) { + seen_life++; + if (data == 0x55aa) { + byte |= 0x80; + seen_55aa++; + } + } + } + identifier[i] = byte; + } - /* Stop if we didn't see any 55AA patterns. */ - if (!seen_55aa) { - if (!csn && seen_life) - csn = -1; - break; - } + /* Stop if we didn't see any 55AA patterns. */ + if (!seen_55aa) { + if (!csn && seen_life) + csn = -1; + break; + } - /* Output ID. */ - if (!id[0]) - printf("\n"); - parse_id(identifier, id); - printf("%s (%02X%02X%02X%02X)", id, identifier[7], identifier[6], identifier[5], identifier[4]); + /* Output ID. */ + if (!id[0]) + printf("\n"); + parse_id(identifier, id); + printf("%s (%02X%02X%02X%02X)", id, identifier[7], identifier[6], identifier[5], identifier[4]); - /* Stop if the checksum is invalid. */ - if (identifier[8] != checksum(identifier)) { - printf(" - bad checksum (expected %02X got %02X), trying another port\n", - checksum(identifier), identifier[8]); - csn = -1; - break; - } + /* Stop if the checksum is invalid. */ + if (identifier[8] != checksum(identifier)) { + printf(" - bad checksum (expected %02X got %02X), trying another port\n", + checksum(identifier), identifier[8]); + csn = -1; + break; + } - /* Assign a CSN. */ - csn++; - outb(0x279, 0x06); - outb(0xa79, csn); - io_delay(); + /* Assign a CSN. */ + csn++; + outb(0x279, 0x06); + outb(0xa79, csn); + io_delay(); - /* Wake this card by its CSN to reset the resource data pointer. */ - outb(0x279, 0x03); - outb(0xa79, csn); - io_delay(); + /* Wake this card by its CSN to reset the resource data pointer. */ + outb(0x279, 0x03); + outb(0xa79, csn); + io_delay(); - /* Dump resource data. */ - dump_resource_data(id); + /* Dump resource data. */ + dump_resource_data(id); - /* Put this card to Sleep and other cards to Isolation. */ - outb(0x279, 0x03); - outb(0xa79, 0x00); - io_delay(); + /* Put this card to Sleep and other cards to Isolation. */ + outb(0x279, 0x03); + outb(0xa79, 0x00); + io_delay(); } /* Put all cards to Wait for Key. */ @@ -456,11 +447,10 @@ try_isolate() return csn; } - int main(int argc, char **argv) { - int max_csn; + int max_csn; card_t *card; /* Disable stdout buffering. */ @@ -470,28 +460,28 @@ main(int argc, char **argv) [213:3FF] with a hole at [280:380] for whatever safety reason. */ max_csn = -1; for (rd_data = 0x213; rd_data <= 0x3ff; rd_data += 16) { - if ((rd_data >= 0x280) && (rd_data <= 0x380)) - continue; + if ((rd_data >= 0x280) && (rd_data <= 0x380)) + continue; - max_csn = try_isolate(); - if (max_csn >= 0) - break; + max_csn = try_isolate(); + if (max_csn >= 0) + break; } /* Free the card list. */ while (first_card) { - card = first_card->next; - free(first_card); - first_card = card; + card = first_card->next; + free(first_card); + first_card = card; } /* Nothing returned. */ if (max_csn <= 0) { - if (max_csn < 0) - printf("\nNo good Read Data Ports found!\n"); - else - printf("\nNo devices found!\n"); - return 1; + if (max_csn < 0) + printf("\nNo good Read Data Ports found!\n"); + else + printf("\nNo devices found!\n"); + return 1; } return 0; diff --git a/pcireg/pcireg.c b/pcireg/pcireg.c index daadc2d..c932280 100644 --- a/pcireg/pcireg.c +++ b/pcireg/pcireg.c @@ -20,21 +20,20 @@ * └──────────────────────────────────────────────────────────────┘ */ #ifdef __POSIX_UEFI__ -# include +# include #else -# include -# include -# include -# include -# include -# include -# ifdef __WATCOMC__ -# include -# endif +# include +# include +# include +# include +# include +# include +# ifdef __WATCOMC__ +# include +# endif #endif #include "clib.h" - static const char *command_flags[] = { "I/O", "Mem", @@ -78,8 +77,7 @@ static const char *devsel[] = { "Invalid" }; - -static int term_width; +static int term_width; static FILE *pciids_f = NULL; #if defined(__WATCOMC__) && !defined(M_I386) static union REGPACK rp; @@ -87,98 +85,95 @@ static union REGPACK rp; #pragma pack(push, 0) static struct PACKED { - uint32_t device_db_offset, - subdevice_db_offset, - class_db_offset, - subclass_db_offset, - progif_db_offset, - string_db_offset; + uint32_t device_db_offset, + subdevice_db_offset, + class_db_offset, + subclass_db_offset, + progif_db_offset, + string_db_offset; } pciids_header; static struct PACKED { - uint16_t vendor_id; - uint32_t devices_offset, - string_offset; + uint16_t vendor_id; + uint32_t devices_offset, + string_offset; } pciids_vendor; static struct PACKED { - uint16_t device_id; - uint32_t subdevices_offset, - string_offset; + uint16_t device_id; + uint32_t subdevices_offset, + string_offset; } pciids_device; static struct PACKED { - uint16_t subvendor_id, - subdevice_id; - uint32_t string_offset; + uint16_t subvendor_id, + subdevice_id; + uint32_t string_offset; } pciids_subdevice; static struct PACKED { - uint8_t class_id; - uint32_t string_offset; + uint8_t class_id; + uint32_t string_offset; } pciids_class; static struct PACKED { - uint8_t class_id, - subclass_id; - uint32_t string_offset; + uint8_t class_id, + subclass_id; + uint32_t string_offset; } pciids_subclass; static struct PACKED { - uint8_t class_id, - subclass_id, - progif_id; - uint32_t string_offset; + uint8_t class_id, + subclass_id, + progif_id; + uint32_t string_offset; } pciids_progif; - -# if defined(__WATCOMC__) && !defined(M_I386) +#if defined(__WATCOMC__) && !defined(M_I386) typedef struct { uint8_t bus, dev; struct { - uint8_t link; - uint16_t bitmap; + uint8_t link; + uint16_t bitmap; } ints[4]; uint8_t slot, reserved; } irq_routing_entry_t; typedef struct { uint16_t len; union { - void far *data_ptr; - irq_routing_entry_t entry[1]; + void far *data_ptr; + irq_routing_entry_t entry[1]; }; } irq_routing_table_t; -# endif +#endif #pragma pack(pop) - static int pciids_open_database() { /* No action is required if the file is already open. */ if (pciids_f) - return 0; + return 0; /* Open file, and stop if the open failed or the header couldn't be read. */ pciids_f = fopen("PCIIDS.BIN", "rb"); if (pciids_f && (fread(&pciids_header, sizeof(pciids_header), 1, pciids_f) < 1)) { - fclose(pciids_f); - pciids_f = NULL; + fclose(pciids_f); + pciids_f = NULL; } /* Return 0 if the file was opened successfully, 1 otherwise. */ return !pciids_f; } - static char * pciids_read_string(uint32_t offset) { - uint8_t length, *buf; + uint8_t length, *buf; uint32_t sum; - int i; + int i; /* Return nothing if the string offset is invalid. */ if (offset == 0xffffffff) - return NULL; + return NULL; /* Open database if required. */ if (pciids_open_database()) - return NULL; + return NULL; /* Seek to string offset. */ fseek(pciids_f, pciids_header.string_db_offset + offset, SEEK_SET); @@ -186,54 +181,51 @@ pciids_read_string(uint32_t offset) /* Read string length, and return nothing if it's an empty string. */ fread(&length, sizeof(length), 1, pciids_f); if (length == 0) - return NULL; + return NULL; /* Read string into buffer. */ buf = malloc(length + 1); if (fread(buf, length, 1, pciids_f) < 1) { - /* Clean up and return nothing if the read failed. */ - free(buf); - return NULL; + /* Clean up and return nothing if the read failed. */ + free(buf); + return NULL; } buf[length] = '\0'; return buf; } - static int pciids_find_vendor(uint16_t vendor_id) { /* Open database if required. */ if (pciids_open_database()) - return 0; + return 0; /* Seek to vendor database. */ fseek(pciids_f, sizeof(pciids_header), SEEK_SET); /* Read vendor entries until the ID is matched or overtaken. */ do { - if (fread(&pciids_vendor, sizeof(pciids_vendor), 1, pciids_f) < 1) - break; + if (fread(&pciids_vendor, sizeof(pciids_vendor), 1, pciids_f) < 1) + break; } while (pciids_vendor.vendor_id < vendor_id); /* Return 1 if the ID was matched, 0 otherwise. */ return pciids_vendor.vendor_id == vendor_id; } - static char * pciids_get_vendor(uint16_t vendor_id) { /* Find vendor ID in the database, and return its name if found. */ if (pciids_find_vendor(vendor_id)) - return pciids_read_string(pciids_vendor.string_offset); + return pciids_read_string(pciids_vendor.string_offset); /* Return nothing if the vendor ID was not found. */ return NULL; } - static char * pciids_get_device(uint16_t device_id) { @@ -241,26 +233,25 @@ pciids_get_device(uint16_t device_id) /* Open database if required. */ if (pciids_open_database()) - return NULL; + return NULL; /* Seek to device database entries for the established vendor. */ fseek(pciids_f, pciids_header.device_db_offset + pciids_vendor.devices_offset, SEEK_SET); /* Read device entries until the ID is matched or overtaken. */ do { - if (fread(&pciids_device, sizeof(pciids_device), 1, pciids_f) < 1) - break; + if (fread(&pciids_device, sizeof(pciids_device), 1, pciids_f) < 1) + break; } while (pciids_device.device_id < device_id); /* Return the device name if found. */ if (pciids_device.device_id == device_id) - return pciids_read_string(pciids_device.string_offset); + return pciids_read_string(pciids_device.string_offset); /* Return nothing if the device ID was not found. */ return NULL; } - static char * pciids_get_subdevice(uint16_t subvendor_id, uint16_t subdevice_id) { @@ -268,109 +259,105 @@ pciids_get_subdevice(uint16_t subvendor_id, uint16_t subdevice_id) /* Open database if required. */ if (pciids_open_database()) - return NULL; + return NULL; /* Seek to subdevice database entries for the established subvendor. */ fseek(pciids_f, pciids_header.subdevice_db_offset + pciids_device.subdevices_offset, SEEK_SET); /* Read subdevice entries until the ID is matched or overtaken. */ do { - if (fread(&pciids_subdevice, sizeof(pciids_subdevice), 1, pciids_f) < 1) - break; + if (fread(&pciids_subdevice, sizeof(pciids_subdevice), 1, pciids_f) < 1) + break; } while ((pciids_subdevice.subvendor_id < subvendor_id) || (pciids_subdevice.subdevice_id < subdevice_id)); /* Return the subdevice name if found. */ if ((pciids_subdevice.subvendor_id == subvendor_id) && (pciids_subdevice.subdevice_id == subdevice_id)) - return pciids_read_string(pciids_subdevice.string_offset); + return pciids_read_string(pciids_subdevice.string_offset); /* Return nothing if the subdevice ID was not found. */ return NULL; } - static char * pciids_get_class(uint8_t class_id) { /* Open database if required. */ if (pciids_open_database()) - return NULL; + return NULL; /* Seek to class database. */ fseek(pciids_f, pciids_header.class_db_offset, SEEK_SET); /* Read class entries until the ID is matched or overtaken. */ do { - if (fread(&pciids_class, sizeof(pciids_class), 1, pciids_f) < 1) - break; + if (fread(&pciids_class, sizeof(pciids_class), 1, pciids_f) < 1) + break; } while (pciids_class.class_id < class_id); /* Return the class name if found. */ if (pciids_class.class_id == class_id) - return pciids_read_string(pciids_class.string_offset); + return pciids_read_string(pciids_class.string_offset); /* Return nothing if the class ID was not found. */ return NULL; } - static char * pciids_get_subclass(uint8_t class_id, uint8_t subclass_id) { /* Open database if required. */ if (pciids_open_database()) - return NULL; + return NULL; /* Seek to subclass database. */ fseek(pciids_f, pciids_header.subclass_db_offset, SEEK_SET); /* Read subclass entries until the ID is matched or overtaken. */ do { - if (fread(&pciids_subclass, sizeof(pciids_subclass), 1, pciids_f) < 1) - break; + if (fread(&pciids_subclass, sizeof(pciids_subclass), 1, pciids_f) < 1) + break; } while ((pciids_subclass.class_id < class_id) || (pciids_subclass.subclass_id < subclass_id)); /* Return the subclass name if found. */ if ((pciids_subclass.class_id == class_id) && (pciids_subclass.subclass_id == subclass_id)) - return pciids_read_string(pciids_subclass.string_offset); + return pciids_read_string(pciids_subclass.string_offset); /* Return nothing if the subclass ID was not found. */ return NULL; } - static char * pciids_get_progif(uint8_t class_id, uint8_t subclass_id, uint8_t progif_id) { /* Open database if required. */ if (pciids_open_database()) - return NULL; + return NULL; /* Seek to programming interface database. */ fseek(pciids_f, pciids_header.progif_db_offset, SEEK_SET); /* Read programming interface entries until the ID is matched or overtaken. */ do { - if (fread(&pciids_progif, sizeof(pciids_progif), 1, pciids_f) < 1) - break; + if (fread(&pciids_progif, sizeof(pciids_progif), 1, pciids_f) < 1) + break; } while ((pciids_progif.class_id < class_id) || (pciids_progif.subclass_id < subclass_id) || (pciids_progif.progif_id < progif_id)); /* Return the programming interface name if found. */ if ((pciids_progif.class_id == class_id) && (pciids_progif.subclass_id == subclass_id) && (pciids_progif.progif_id == progif_id)) - return pciids_read_string(pciids_progif.string_offset); + return pciids_read_string(pciids_progif.string_offset); /* Return nothing if the programming interface ID was not found. */ return NULL; } - static int dump_regs(uint8_t bus, uint8_t dev, uint8_t func, uint8_t start_reg, char sz) { - int i, width, flags, bar_id; - char buf[13]; + int i, width, flags, bar_id; + char buf[13]; uint8_t cur_reg, regs[256], dev_type, bar_reg; multi_t reg_val; - FILE *f; + FILE *f; /* Align the starting register. */ start_reg &= 0xfc; @@ -383,335 +370,333 @@ dump_regs(uint8_t bus, uint8_t dev, uint8_t func, uint8_t start_reg, char sz) /* Size character '.' indicates a quiet dump for scan_bus. */ if (sz != '.') { - /* Print banner message. */ - printf("Dumping registers [%02X:FF] from PCI bus %02X device %02X function %d\n\n", start_reg, - bus, dev, func); + /* Print banner message. */ + printf("Dumping registers [%02X:FF] from PCI bus %02X device %02X function %d\n\n", start_reg, + bus, dev, func); - /* Print column headers. */ - printf(" "); - switch (sz) { - case 'd': - case 'l': - width = 40; - for (i = 0x0; i <= 0xf; i += 4) { - /* Add spacing at the halfway point. */ - if (i == 0x8) - putchar(' '); - printf(" %X", i); - } - break; + /* Print column headers. */ + printf(" "); + switch (sz) { + case 'd': + case 'l': + width = 40; + for (i = 0x0; i <= 0xf; i += 4) { + /* Add spacing at the halfway point. */ + if (i == 0x8) + putchar(' '); + printf(" %X", i); + } + break; - case 'w': - width = 44; - for (i = 0x0; i <= 0xf; i += 2) { - if (i == 0x8) - putchar(' '); - printf(" %X", i); - } - break; + case 'w': + width = 44; + for (i = 0x0; i <= 0xf; i += 2) { + if (i == 0x8) + putchar(' '); + printf(" %X", i); + } + break; - default: - width = 52; - for (i = 0x0; i <= 0xf; i++) { - if (i == 0x8) - putchar(' '); - printf(" %X", i); - } - break; - } + default: + width = 52; + for (i = 0x0; i <= 0xf; i++) { + if (i == 0x8) + putchar(' '); + printf(" %X", i); + } + break; + } - /* Move on to the next line if the terminal didn't already do that for us. */ - if (width < term_width) - putchar('\n'); + /* Move on to the next line if the terminal didn't already do that for us. */ + if (width < term_width) + putchar('\n'); } else { - /* Print dump file name now. */ - printf("Dumping registers to %s", buf); + /* Print dump file name now. */ + printf("Dumping registers to %s", buf); } cur_reg = 0; do { - /* Print row header. */ - if (sz != '.') - printf("%02X:", cur_reg); + /* Print row header. */ + if (sz != '.') + printf("%02X:", cur_reg); - /* Iterate through register dwords on this range. */ - do { - /* Add spacing at the halfway point. */ - if ((sz != '.') && ((cur_reg & 0x0f) == 0x08)) - putchar(' '); + /* Iterate through register dwords on this range. */ + do { + /* Add spacing at the halfway point. */ + if ((sz != '.') && ((cur_reg & 0x0f) == 0x08)) + putchar(' '); - /* Are we supposed to read this register? */ - if (cur_reg < start_reg) { - /* No, read it as 0 for the dump. */ - reg_val.u32 = 0; + /* Are we supposed to read this register? */ + if (cur_reg < start_reg) { + /* No, read it as 0 for the dump. */ + reg_val.u32 = 0; - /* Print dashes. */ - switch (sz) { - case '.': - break; + /* Print dashes. */ + switch (sz) { + case '.': + break; - case 'd': - case 'l': - printf(" --------"); - break; + case 'd': + case 'l': + printf(" --------"); + break; - case 'w': - printf(" ---- ----"); - break; + case 'w': + printf(" ---- ----"); + break; - default: - printf(" -- -- -- --"); - break; - } - } else { - /* Yes, read dword value. */ + default: + printf(" -- -- -- --"); + break; + } + } else { + /* Yes, read dword value. */ #ifdef DEBUG - reg_val.u32 = pci_cf8(bus, dev, func, cur_reg); + reg_val.u32 = pci_cf8(bus, dev, func, cur_reg); #else - reg_val.u32 = pci_readl(bus, dev, func, cur_reg); + reg_val.u32 = pci_readl(bus, dev, func, cur_reg); #endif - /* Print the value as bytes/words/dword. */ - switch (sz) { - case '.': - break; + /* Print the value as bytes/words/dword. */ + switch (sz) { + case '.': + break; - case 'd': - case 'l': - printf(" %04X%04X", reg_val.u16[1], reg_val.u16[0]); - break; + case 'd': + case 'l': + printf(" %04X%04X", reg_val.u16[1], reg_val.u16[0]); + break; - case 'w': - printf(" %04X %04X", reg_val.u16[0], reg_val.u16[1]); - break; + case 'w': + printf(" %04X %04X", reg_val.u16[0], reg_val.u16[1]); + break; - default: - printf(" %02X %02X %02X %02X", reg_val.u8[0], reg_val.u8[1], reg_val.u8[2], reg_val.u8[3]); - break; - } - } + default: + printf(" %02X %02X %02X %02X", reg_val.u8[0], reg_val.u8[1], reg_val.u8[2], reg_val.u8[3]); + break; + } + } - /* Save value to dump array. */ - *((uint32_t *) ®s[cur_reg]) = reg_val.u32; + /* Save value to dump array. */ + *((uint32_t *) ®s[cur_reg]) = reg_val.u32; - /* Move on to the next dword. */ - cur_reg += 4; - } while (cur_reg & 0x0f); + /* Move on to the next dword. */ + cur_reg += 4; + } while (cur_reg & 0x0f); - if (sz != '.') { - /* Move on to the next line if the terminal didn't already do that for us. */ - if (width < term_width) - putchar('\n'); - } + if (sz != '.') { + /* Move on to the next line if the terminal didn't already do that for us. */ + if (width < term_width) + putchar('\n'); + } } while (cur_reg); /* Print dump file name. */ if (sz != '.') - printf("\nSaving dump to %s\n", buf); + printf("\nSaving dump to %s\n", buf); /* Write dump file. */ f = fopen(buf, "wb"); if (!f) { - if (sz != '.') - printf("File creation failed\n"); - return 1; + if (sz != '.') + printf("File creation failed\n"); + return 1; } if (fwrite(regs, sizeof(regs), 1, f) < 1) { - fclose(f); - if (sz != '.') - printf("File write failed\n"); - return 1; + fclose(f); + if (sz != '.') + printf("File write failed\n"); + return 1; } fclose(f); if (sz == '.') { - /* Clear the dump file name printed earlier. */ - width = strlen(buf) + 21; - for (i = 0; i < width; i++) - putchar('\b'); - for (i = 0; i < width; i++) - putchar(' '); - for (i = 0; i < width; i++) - putchar('\b'); + /* Clear the dump file name printed earlier. */ + width = strlen(buf) + 21; + for (i = 0; i < width; i++) + putchar('\b'); + for (i = 0; i < width; i++) + putchar(' '); + for (i = 0; i < width; i++) + putchar('\b'); } return 0; } - static int scan_bus(uint8_t bus, int nesting, char dump, char *buf) { - int i, j, count, last_count, children; - char *temp; + int i, j, count, last_count, children; + char *temp; uint8_t dev, func, header_type, new_bus, row, column; multi_t dev_id, dev_rev_class; /* Iterate through devices. */ count = 0; for (dev = 0; dev < pci_device_count; dev++) { - /* Iterate through functions. */ - for (func = 0; func < 8; func++) { - /* Read vendor/device ID. */ + /* Iterate through functions. */ + for (func = 0; func < 8; func++) { + /* Read vendor/device ID. */ #ifdef DEBUG - if ((bus < DEBUG) && (dev <= bus) && (func == 0)) { - dev_id.u16[0] = rand(); - dev_id.u16[1] = rand(); - } else { - dev_id.u32 = 0xffffffff; - } + if ((bus < DEBUG) && (dev <= bus) && (func == 0)) { + dev_id.u16[0] = rand(); + dev_id.u16[1] = rand(); + } else { + dev_id.u32 = 0xffffffff; + } #else - dev_id.u32 = pci_readl(bus, dev, func, 0x00); + dev_id.u32 = pci_readl(bus, dev, func, 0x00); #endif - /* Report a valid ID. */ - if (dev_id.u32 && (dev_id.u32 != 0xffffffff)) { - /* Clear vendor/device name buffer while adding nested bus spacing if required. */ - if (nesting) { - j = (nesting << 1) - 2; - for (i = 0; i < j; i++) - buf[i] = ' '; - buf[i] = '\0'; - sprintf(&buf[strlen(buf)], "└─"); - } else { - buf[0] = '\0'; - } + /* Report a valid ID. */ + if (dev_id.u32 && (dev_id.u32 != 0xffffffff)) { + /* Clear vendor/device name buffer while adding nested bus spacing if required. */ + if (nesting) { + j = (nesting << 1) - 2; + for (i = 0; i < j; i++) + buf[i] = ' '; + buf[i] = '\0'; + sprintf(&buf[strlen(buf)], "└─"); + } else { + buf[0] = '\0'; + } - /* Print device address and IDs. */ - printf(" %02X %02X %d [%04X:%04X] ", bus, dev, func, - dev_id.u16[0], dev_id.u16[1]); + /* Print device address and IDs. */ + printf(" %02X %02X %d [%04X:%04X] ", bus, dev, func, + dev_id.u16[0], dev_id.u16[1]); - /* Read revision and class ID. */ + /* Read revision and class ID. */ #ifdef DEBUG - dev_rev_class.u16[0] = rand(); - dev_rev_class.u16[1] = rand(); + dev_rev_class.u16[0] = rand(); + dev_rev_class.u16[1] = rand(); #else - dev_rev_class.u32 = pci_readl(bus, dev, func, 0x08); + dev_rev_class.u32 = pci_readl(bus, dev, func, 0x08); #endif - /* Look up vendor name in the PCI ID database. */ - temp = pciids_get_vendor(dev_id.u16[0]); - if (temp) { - /* Add vendor name to buffer. */ - strcat(buf, temp); - strcat(buf, " "); - free(temp); + /* Look up vendor name in the PCI ID database. */ + temp = pciids_get_vendor(dev_id.u16[0]); + if (temp) { + /* Add vendor name to buffer. */ + strcat(buf, temp); + strcat(buf, " "); + free(temp); - /* Look up device name. */ - temp = pciids_get_device(dev_id.u16[1]); - if (temp) { - /* Add device name to buffer. */ - strcat(buf, temp); - free(temp); - } else { - /* Device name not found. */ - goto unknown_device; - } - } else { - /* Vendor name not found. */ - strcat(buf, "[Unknown] "); + /* Look up device name. */ + temp = pciids_get_device(dev_id.u16[1]); + if (temp) { + /* Add device name to buffer. */ + strcat(buf, temp); + free(temp); + } else { + /* Device name not found. */ + goto unknown_device; + } + } else { + /* Vendor name not found. */ + strcat(buf, "[Unknown] "); -unknown_device: /* Look up class ID. */ - temp = pciids_get_subclass(dev_rev_class.u8[3], dev_rev_class.u8[2]); - if (temp) { - /* Add class name to buffer. */ - sprintf(&buf[strlen(buf)], "[%s]", temp); - free(temp); - } else { - /* Class name not found. */ - sprintf(&buf[strlen(buf)], "[Class %02X:%02X:%02X]", dev_rev_class.u8[3], dev_rev_class.u8[2], dev_rev_class.u8[1]); - } - } +unknown_device: /* Look up class ID. */ + temp = pciids_get_subclass(dev_rev_class.u8[3], dev_rev_class.u8[2]); + if (temp) { + /* Add class name to buffer. */ + sprintf(&buf[strlen(buf)], "[%s]", temp); + free(temp); + } else { + /* Class name not found. */ + sprintf(&buf[strlen(buf)], "[Class %02X:%02X:%02X]", dev_rev_class.u8[3], dev_rev_class.u8[2], dev_rev_class.u8[1]); + } + } - /* Limit buffer to screen width, then print it with the revision ID. */ - i = term_width - strlen(buf) - 24; - if (i >= 9) { - sprintf(&buf[strlen(buf)], " (rev %02X)", dev_rev_class.u8[0]); - } else { - if (i >= 5) - strcat(buf, " "); - else if (i < 4) - strcpy(&buf[term_width - 32], "... "); - sprintf(&buf[strlen(buf)], "(%02X)", dev_rev_class.u8[0]); - } - printf(buf); + /* Limit buffer to screen width, then print it with the revision ID. */ + i = term_width - strlen(buf) - 24; + if (i >= 9) { + sprintf(&buf[strlen(buf)], " (rev %02X)", dev_rev_class.u8[0]); + } else { + if (i >= 5) + strcat(buf, " "); + else if (i < 4) + strcpy(&buf[term_width - 32], "... "); + sprintf(&buf[strlen(buf)], "(%02X)", dev_rev_class.u8[0]); + } + printf(buf); - /* Move on to the next line if the terminal didn't already do that for us. */ - if (term_width > (strlen(buf) + 24)) - putchar('\n'); + /* Move on to the next line if the terminal didn't already do that for us. */ + if (term_width > (strlen(buf) + 24)) + putchar('\n'); - /* Rectify previous nesting indicator when nesting buses. */ - if (nesting && count && term_get_cursor_pos(&column, &row)) { - i = 22 + (nesting << 1); - j = row - 2; - if (children > j) - children = j; - while (children) { - term_set_cursor_pos(i, j); - printf("│"); - children--; - j--; - } - if (j != 0xff) { - term_set_cursor_pos(i, j); - printf("├"); - } + /* Rectify previous nesting indicator when nesting buses. */ + if (nesting && count && term_get_cursor_pos(&column, &row)) { + i = 22 + (nesting << 1); + j = row - 2; + if (children > j) + children = j; + while (children) { + term_set_cursor_pos(i, j); + printf("│"); + children--; + j--; + } + if (j != 0xff) { + term_set_cursor_pos(i, j); + printf("├"); + } - /* Restore cursor position. */ - term_set_cursor_pos(column, row); - } + /* Restore cursor position. */ + term_set_cursor_pos(column, row); + } - /* Dump registers if requested. */ - if (dump) - dump_regs(bus, dev, func, 0, '.'); + /* Dump registers if requested. */ + if (dump) + dump_regs(bus, dev, func, 0, '.'); - /* Increment device count. */ - count++; - } else { - /* Stop or move on to the next function if there's nothing here. */ - if (func) - continue; - else - break; - } + /* Increment device count. */ + count++; + } else { + /* Stop or move on to the next function if there's nothing here. */ + if (func) + continue; + else + break; + } - /* Read header type. */ + /* Read header type. */ #ifdef DEBUG - header_type = (bus < (DEBUG - 1)) ? 0x01 : 0x00; + header_type = (bus < (DEBUG - 1)) ? 0x01 : 0x00; #else - header_type = pci_readb(bus, dev, func, 0x0e); + header_type = pci_readb(bus, dev, func, 0x0e); #endif - /* If this is a bridge, mark that we should probe its bus. */ - if (header_type & 0x7f) { - /* Read bus numbers. */ + /* If this is a bridge, mark that we should probe its bus. */ + if (header_type & 0x7f) { + /* Read bus numbers. */ #ifdef DEBUG - new_bus = bus + 1; + new_bus = bus + 1; #else - new_bus = pci_readb(bus, dev, func, 0x19); + new_bus = pci_readb(bus, dev, func, 0x19); #endif - /* Scan the secondary bus. */ - children = scan_bus(new_bus, nesting + 1, dump, buf); - count += children; - } else { - children = 0; - } + /* Scan the secondary bus. */ + children = scan_bus(new_bus, nesting + 1, dump, buf); + count += children; + } else { + children = 0; + } - /* If we're at the first function, stop if this is not a multi-function device. */ - if ((func == 0) && !(header_type & 0x80)) - break; - } + /* If we're at the first function, stop if this is not a multi-function device. */ + if ((func == 0) && !(header_type & 0x80)) + break; + } } return count; } - static int scan_buses(char dump) { - int i; + int i; char *buf; /* Initialize buffers. */ @@ -724,7 +709,7 @@ scan_buses(char dump) /* Print header. */ printf("Bus Dev Fun [VeID:DeID] Device\n"); for (i = 0; i < term_width; i++) - printf("─"); + printf("─"); /* Scan the root bus. */ scan_bus(0, 0, dump, buf); @@ -735,7 +720,6 @@ scan_buses(char dump) return 0; } - static void info_flags_helper(uint16_t bitfield, const char **table) { @@ -745,36 +729,35 @@ info_flags_helper(uint16_t bitfield, const char **table) /* Check each bit. */ j = 0; for (i = 0; i < 16; i++) { - /* Ignore if there is no table entry. */ - if (!table[i]) - continue; + /* Ignore if there is no table entry. */ + if (!table[i]) + continue; - /* Print flag name in [brackets] if set. */ - if (bitfield & (1 << i)) - printf(" [%s]", table[i]); - else - printf(" %s ", table[i]); + /* Print flag name in [brackets] if set. */ + if (bitfield & (1 << i)) + printf(" [%s]", table[i]); + else + printf(" %s ", table[i]); - /* Add line break and indentation after the 7th item. */ - if (++j == 7) { - printf("\n "); - j = 0; - } + /* Add line break and indentation after the 7th item. */ + if (++j == 7) { + printf("\n "); + j = 0; + } } } - static int dump_info(uint8_t bus, uint8_t dev, uint8_t func) { - char *temp; - int i; + char *temp; + int i; uint8_t header_type, subsys_reg, num_bars; multi_t reg_val; /* Print banner message. */ printf("Displaying information for PCI bus %02X device %02X function %d\n", - bus, dev, func); + bus, dev, func); /* Read vendor/device ID, and stop if it's invalid. */ #ifdef DEBUG @@ -782,9 +765,9 @@ dump_info(uint8_t bus, uint8_t dev, uint8_t func) #else reg_val.u32 = pci_readl(bus, dev, func, 0x00); if (!reg_val.u32 || (reg_val.u32 == 0xffffffff)) { - printf("\nNo device appears to exist here. (vendor:device %04X:%04X)\n", - reg_val.u16[0], reg_val.u16[1]); - return 1; + printf("\nNo device appears to exist here. (vendor:device %04X:%04X)\n", + reg_val.u16[0], reg_val.u16[1]); + return 1; } #endif @@ -794,10 +777,10 @@ dump_info(uint8_t bus, uint8_t dev, uint8_t func) /* Print vendor name if found. */ temp = pciids_get_vendor(reg_val.u16[0]); if (temp) { - printf(temp); - free(temp); + printf(temp); + free(temp); } else { - printf("[Unknown]"); + printf("[Unknown]"); } /* Print device ID. */ @@ -806,10 +789,10 @@ dump_info(uint8_t bus, uint8_t dev, uint8_t func) /* Print device name if found. */ temp = pciids_get_device(reg_val.u16[1]); if (temp) { - printf(temp); - free(temp); + printf(temp); + free(temp); } else { - printf("[Unknown]"); + printf("[Unknown]"); } /* Read header type. We'll be using it a lot. */ @@ -817,54 +800,54 @@ dump_info(uint8_t bus, uint8_t dev, uint8_t func) /* Determine the locations of common registers for this header type. */ switch (header_type & 0x7f) { - case 0x00: /* standard */ - subsys_reg = 0x2c; - num_bars = 6; - break; + case 0x00: /* standard */ + subsys_reg = 0x2c; + num_bars = 6; + break; - case 0x02: /* CardBus bridge */ - subsys_reg = 0x40; - num_bars = 0; - break; + case 0x02: /* CardBus bridge */ + subsys_reg = 0x40; + num_bars = 0; + break; - case 0x03: /* PCI bridge and others */ - subsys_reg = 0xff; - num_bars = 2; - break; + case 0x03: /* PCI bridge and others */ + subsys_reg = 0xff; + num_bars = 2; + break; - default: /* others */ - subsys_reg = 0xff; - num_bars = 0; - break; + default: /* others */ + subsys_reg = 0xff; + num_bars = 0; + break; } if (subsys_reg != 0xff) { - /* Read subsystem ID and print it if valid. */ - reg_val.u32 = pci_readl(bus, dev, func, subsys_reg); - if (reg_val.u32 && (reg_val.u32 != 0xffffffff)) { - /* Print subvendor ID. */ - printf("\nSubvendor: [%04X] ", reg_val.u16[0]); + /* Read subsystem ID and print it if valid. */ + reg_val.u32 = pci_readl(bus, dev, func, subsys_reg); + if (reg_val.u32 && (reg_val.u32 != 0xffffffff)) { + /* Print subvendor ID. */ + printf("\nSubvendor: [%04X] ", reg_val.u16[0]); - /* Print subvendor name if found. */ - temp = pciids_get_vendor(reg_val.u16[0]); - if (temp) { - printf(temp); - free(temp); - } else { - printf("[Unknown]"); - } + /* Print subvendor name if found. */ + temp = pciids_get_vendor(reg_val.u16[0]); + if (temp) { + printf(temp); + free(temp); + } else { + printf("[Unknown]"); + } - /* Print subdevice ID. */ - printf("\nSubdevice: [%04X] ", reg_val.u16[1]); + /* Print subdevice ID. */ + printf("\nSubdevice: [%04X] ", reg_val.u16[1]); - /* Print subdevice ID if found. */ - temp = pciids_get_subdevice(reg_val.u16[0], reg_val.u16[1]); - if (temp) { - printf(temp); - free(temp); - } else { - printf("[Unknown]"); - } - } + /* Print subdevice ID if found. */ + temp = pciids_get_subdevice(reg_val.u16[0], reg_val.u16[1]); + if (temp) { + printf(temp); + free(temp); + } else { + printf("[Unknown]"); + } + } } /* Read command and status. */ @@ -894,10 +877,10 @@ dump_info(uint8_t bus, uint8_t dev, uint8_t func) /* Print class name if found. */ temp = pciids_get_class(reg_val.u8[3]); if (temp) { - printf(temp); - free(temp); + printf(temp); + free(temp); } else { - printf("[Unknown]"); + printf("[Unknown]"); } /* Print subclass ID. */ @@ -906,10 +889,10 @@ dump_info(uint8_t bus, uint8_t dev, uint8_t func) /* Print subclass name if found. */ temp = pciids_get_subclass(reg_val.u8[3], reg_val.u8[2]); if (temp) { - printf(temp); - free(temp); + printf(temp); + free(temp); } else { - printf("[Unknown]"); + printf("[Unknown]"); } /* Print programming interface ID. */ @@ -918,10 +901,10 @@ dump_info(uint8_t bus, uint8_t dev, uint8_t func) /* Print programming interface name if found. */ temp = pciids_get_progif(reg_val.u8[3], reg_val.u8[2], reg_val.u8[1]); if (temp) { - printf(temp); - free(temp); + printf(temp); + free(temp); } else { - printf("[Unknown]"); + printf("[Unknown]"); } /* Read latency, grant and interrupt line. */ @@ -929,61 +912,61 @@ dump_info(uint8_t bus, uint8_t dev, uint8_t func) /* Print interrupt if present. */ if (reg_val.u16[0] && (reg_val.u8[0] != 0xff)) - printf("\nInterrupt: INT%c (IRQ %d)", '@' + (reg_val.u8[1] & 15), reg_val.u8[0] & 15); + printf("\nInterrupt: INT%c (IRQ %d)", '@' + (reg_val.u8[1] & 15), reg_val.u8[0] & 15); /* Print latency and grant if available. */ if ((header_type & 0x7f) == 0x00) { #ifdef FMT_FLOAT_SUPPORTED - printf("\nMin Grant: %.0f us at 33 MHz", reg_val.u8[2] * 0.25); - printf("\nMax Latency: %.0f us at 33 MHz", reg_val.u8[3] * 0.25); + printf("\nMin Grant: %.0f us at 33 MHz", reg_val.u8[2] * 0.25); + printf("\nMax Latency: %.0f us at 33 MHz", reg_val.u8[3] * 0.25); #else - printf("\nMin Grant: %d.%d us at 33 MHz", reg_val.u8[2] >> 2, 25 * (reg_val.u8[2] & 3)); - printf("\nMax Latency: %d.%d us at 33 MHz", reg_val.u8[3] >> 2, 25 * (reg_val.u8[2] & 3)); + printf("\nMin Grant: %d.%d us at 33 MHz", reg_val.u8[2] >> 2, 25 * (reg_val.u8[2] & 3)); + printf("\nMax Latency: %d.%d us at 33 MHz", reg_val.u8[3] >> 2, 25 * (reg_val.u8[2] & 3)); #endif } /* Read and print BARs. */ for (i = 0; i < num_bars; i++) { - if (i == 0) - putchar('\n'); + if (i == 0) + putchar('\n'); - /* Read BAR. */ - reg_val.u32 = pci_readl(bus, dev, func, 0x10 + (i << 2)); + /* Read BAR. */ + reg_val.u32 = pci_readl(bus, dev, func, 0x10 + (i << 2)); - /* Move on to the next BAR if this one doesn't appear to be valid. */ - if (!reg_val.u32 || (reg_val.u32 == 0xffffffff)) - continue; + /* Move on to the next BAR if this one doesn't appear to be valid. */ + if (!reg_val.u32 || (reg_val.u32 == 0xffffffff)) + continue; - /* Print BAR index. */ - printf("\nBAR %d: ", i); + /* Print BAR index. */ + printf("\nBAR %d: ", i); - /* Print BAR type, address and properties. */ - if (reg_val.u8[0] & 1) { - printf("I/O at %04X", reg_val.u16[0] & 0xfffc); - } else { - printf("Memory at %04X%04X (", reg_val.u16[1], reg_val.u16[0] & 0xfff0); - switch (reg_val.u8[0] & 0x06) { - case 0x00: - printf("32-bit"); - break; + /* Print BAR type, address and properties. */ + if (reg_val.u8[0] & 1) { + printf("I/O at %04X", reg_val.u16[0] & 0xfffc); + } else { + printf("Memory at %04X%04X (", reg_val.u16[1], reg_val.u16[0] & 0xfff0); + switch (reg_val.u8[0] & 0x06) { + case 0x00: + printf("32-bit"); + break; - case 0x02: - printf("below 1 MB"); - break; + case 0x02: + printf("below 1 MB"); + break; - case 0x04: - printf("64-bit"); - break; + case 0x04: + printf("64-bit"); + break; - case 0x06: - printf("invalid location"); - break; - } - printf(", "); - if (!(reg_val.u8[0] & 0x08)) - printf("not "); - printf("prefetchable)"); - } + case 0x06: + printf("invalid location"); + break; + } + printf(", "); + if (!(reg_val.u8[0] & 0x08)) + printf("not "); + printf("prefetchable)"); + } } printf("\n"); @@ -991,7 +974,6 @@ dump_info(uint8_t bus, uint8_t dev, uint8_t func) return 0; } - #if defined(__WATCOMC__) && !defined(M_I386) static int comp_irq_routing_entry(const void *elem1, const void *elem2) @@ -1001,13 +983,12 @@ comp_irq_routing_entry(const void *elem1, const void *elem2) return comp_ui8(&a->dev, &b->dev); } - static int dump_steering_table(char mode) { - int i, j, entries; - uint8_t irq_bitmap[256], temp[4]; - uint16_t buf_size = 1024, dev_class; + int i, j, entries; + uint8_t irq_bitmap[256], temp[4]; + uint16_t buf_size = 1024, dev_class; irq_routing_table_t *table; irq_routing_entry_t *entry; @@ -1015,40 +996,40 @@ dump_steering_table(char mode) retry_buf: table = malloc(buf_size); if (!table) { - printf("Failed to allocate %d bytes.\n", buf_size); - return 1; + printf("Failed to allocate %d bytes.\n", buf_size); + return 1; } /* Specify where the IRQ routing information will be placed. */ - table->len = buf_size; + table->len = buf_size; table->data_ptr = (void far *) table; /* Call PCI BIOS to fetch IRQ routing information. */ - rp.w.ax = 0xb10e; - rp.w.bx = 0x0000; - rp.w.es = FP_SEG(table->data_ptr); - rp.w.di = FP_OFF(table->data_ptr); + rp.w.ax = 0xb10e; + rp.w.bx = 0x0000; + rp.w.es = FP_SEG(table->data_ptr); + rp.w.di = FP_OFF(table->data_ptr); table->data_ptr = &table->entry[0]; - rp.w.ds = 0xf000; + rp.w.ds = 0xf000; intr(0x1a, &rp); /* Check for any returned error. */ if ((rp.h.ah == 0x59) && (table->len > buf_size)) { - /* Re-allocate buffer with the requested size. */ - buf_size = table->len; - printf("PCI BIOS claims %d bytes for table entries, ", buf_size); - if (buf_size >= 65530) { - printf("which looks invalid.\n"); - return 1; - } - printf("retrying...\n"); - buf_size += 2; - free(table); - goto retry_buf; + /* Re-allocate buffer with the requested size. */ + buf_size = table->len; + printf("PCI BIOS claims %d bytes for table entries, ", buf_size); + if (buf_size >= 65530) { + printf("which looks invalid.\n"); + return 1; + } + printf("retrying...\n"); + buf_size += 2; + free(table); + goto retry_buf; } else if (rp.h.ah) { - /* Something else went wrong. */ - printf("PCI BIOS call failed. (AH=%02X)\n", rp.h.ah); - return 1; + /* Something else went wrong. */ + printf("PCI BIOS call failed. (AH=%02X)\n", rp.h.ah); + return 1; } /* Get terminal size. */ @@ -1056,179 +1037,179 @@ retry_buf: /* Start output according to the selected mode. */ if (mode == '8') { - /* Stop if no entries were found. */ - entries = table->len / sizeof(table->entry[0]); - if (!entries) { - printf("/* No entries found! */\n"); - return 1; - } + /* Stop if no entries were found. */ + entries = table->len / sizeof(table->entry[0]); + if (!entries) { + printf("/* No entries found! */\n"); + return 1; + } - /* Sort table entries by device number for a tidier generated code. */ - entry = &table->entry[0]; - qsort(entry, entries, sizeof(table->entry[0]), comp_irq_routing_entry); + /* Sort table entries by device number for a tidier generated code. */ + entry = &table->entry[0]; + qsort(entry, entries, sizeof(table->entry[0]), comp_irq_routing_entry); - /* Check for and add missing northbridge steering entries. */ - while (entries > 0) { - /* Make sure we are on the first entry for the root bus. */ - if (entry->bus == 0) - break; - entries--; - entry++; - } - if (entries > 0) { - /* Assume device 00 is the northbridge if it has a host bridge class. */ - if (entry->dev > 0x00) { - dev_class = pci_readw(0x00, 0x00, 0, 0x0a); - if (dev_class == 0x0600) - printf("pci_register_slot(0x00, PCI_CARD_NORTHBRIDGE, 1, 2, 3, 4);\n"); - } - /* Assume device 01 is the AGP bridge if it has a PCI bridge class. */ - if (entry->dev > 0x01) { - dev_class = pci_readw(0x00, 0x01, 0, 0x0a); - if (dev_class == 0x0604) - printf("pci_register_slot(0x01, PCI_CARD_AGPBRIDGE, 1, 2, 3, 4);\n"); - } - } + /* Check for and add missing northbridge steering entries. */ + while (entries > 0) { + /* Make sure we are on the first entry for the root bus. */ + if (entry->bus == 0) + break; + entries--; + entry++; + } + if (entries > 0) { + /* Assume device 00 is the northbridge if it has a host bridge class. */ + if (entry->dev > 0x00) { + dev_class = pci_readw(0x00, 0x00, 0, 0x0a); + if (dev_class == 0x0600) + printf("pci_register_slot(0x00, PCI_CARD_NORTHBRIDGE, 1, 2, 3, 4);\n"); + } + /* Assume device 01 is the AGP bridge if it has a PCI bridge class. */ + if (entry->dev > 0x01) { + dev_class = pci_readw(0x00, 0x01, 0, 0x0a); + if (dev_class == 0x0604) + printf("pci_register_slot(0x01, PCI_CARD_AGPBRIDGE, 1, 2, 3, 4);\n"); + } + } - /* Identify INTx# link value mapping by gathering all known values. */ - memset(irq_bitmap, 0, sizeof(irq_bitmap)); - entry = &table->entry[0]; - entries = table->len / sizeof(table->entry[0]); - while (entries-- > 0) { - /* Ignore non-root buses. */ - if (!entry->bus) { - /* Populate each IRQ link value. */ - for (j = 0; j < 4; j++) - irq_bitmap[entry->ints[j].link] = entry->ints[j].link; - } + /* Identify INTx# link value mapping by gathering all known values. */ + memset(irq_bitmap, 0, sizeof(irq_bitmap)); + entry = &table->entry[0]; + entries = table->len / sizeof(table->entry[0]); + while (entries-- > 0) { + /* Ignore non-root buses. */ + if (!entry->bus) { + /* Populate each IRQ link value. */ + for (j = 0; j < 4; j++) + irq_bitmap[entry->ints[j].link] = entry->ints[j].link; + } - /* Move on to the next entry. */ - entry++; - } + /* Move on to the next entry. */ + entry++; + } - /* Sort link value entries. */ - qsort(irq_bitmap, sizeof(irq_bitmap), sizeof(irq_bitmap[0]), comp_ui8); + /* Sort link value entries. */ + qsort(irq_bitmap, sizeof(irq_bitmap), sizeof(irq_bitmap[0]), comp_ui8); - /* Jump to the first non-zero link value entry. */ - for (i = 0; (i < sizeof(irq_bitmap)) && (!irq_bitmap[i]); i++); + /* Jump to the first non-zero link value entry. */ + for (i = 0; (i < sizeof(irq_bitmap)) && (!irq_bitmap[i]); i++) + ; - /* Warn if the mapping might not be completely valid. */ - j = sizeof(irq_bitmap) - i; - if (j % 4) - printf("/* WARNING: %d IRQ link mappings found */\n", j); + /* Warn if the mapping might not be completely valid. */ + j = sizeof(irq_bitmap) - i; + if (j % 4) + printf("/* WARNING: %d IRQ link mappings found */\n", j); - /* Copy link value entries to temporary array. */ - if (j > sizeof(temp)) - j = sizeof(temp); - memcpy(temp, &irq_bitmap[i], j); + /* Copy link value entries to temporary array. */ + if (j > sizeof(temp)) + j = sizeof(temp); + memcpy(temp, &irq_bitmap[i], j); - /* Fill in mapping entries. */ - memset(irq_bitmap, 0, sizeof(irq_bitmap)); - for (i = 0; i < j; i++) { - if (temp[i]) - irq_bitmap[temp[i]] = i + 1; - } + /* Fill in mapping entries. */ + memset(irq_bitmap, 0, sizeof(irq_bitmap)); + for (i = 0; i < j; i++) { + if (temp[i]) + irq_bitmap[temp[i]] = i + 1; + } } else { - /* Print header. */ - printf("┌ Location ─┐┌ INT Lines ┐┌ Steerable IRQs ───────── (INTA=1 B=2 C=4 D=8) ┐\n"); - printf("│Slt Bus Dev││A1 B2 C3 D4││ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15│\n"); - printf("┴───────────┴┴───────────┴┴───────────────────────────────────────────────┴"); - for (i = 75; i < term_width; i++) - putchar('─'); + /* Print header. */ + printf("┌ Location ─┐┌ INT Lines ┐┌ Steerable IRQs ───────── (INTA=1 B=2 C=4 D=8) ┐\n"); + printf("│Slt Bus Dev││A1 B2 C3 D4││ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15│\n"); + printf("┴───────────┴┴───────────┴┴───────────────────────────────────────────────┴"); + for (i = 75; i < term_width; i++) + putchar('─'); } /* Print entries until the end of the table. */ entries = table->len / sizeof(table->entry[0]); - entry = &table->entry[0]; + entry = &table->entry[0]; while (entries-- > 0) { - /* Correct device number. */ - entry->dev >>= 3; + /* Correct device number. */ + entry->dev >>= 3; - /* Print entry according to the selected mode. */ - if (mode == '8') { - /* Ignore non-root buses. */ - if (entry->bus) - goto next_entry; + /* Print entry according to the selected mode. */ + if (mode == '8') { + /* Ignore non-root buses. */ + if (entry->bus) + goto next_entry; - /* Print device ID. */ - printf("pci_register_slot(0x%02X, PCI_CARD_", entry->dev); + /* Print device ID. */ + printf("pci_register_slot(0x%02X, PCI_CARD_", entry->dev); - /* Determine and print slot type. */ - if (entry->dev == 0) { - printf("NORTHBRIDGE,"); - } else { - /* Read device class. */ - dev_class = pci_readw(0x00, 0x00, 0, 0x0a); + /* Determine and print slot type. */ + if (entry->dev == 0) { + printf("NORTHBRIDGE,"); + } else { + /* Read device class. */ + dev_class = pci_readw(0x00, 0x00, 0, 0x0a); - /* Determine slot type by location and class. */ - if ((entry->dev == 1) && (dev_class == 0x0604)) { - printf("AGPBRIDGE, "); - } else if (dev_class == 0x0601) { - printf("SOUTHBRIDGE,"); - } else if (entry->slot == 0) { - /* Very likely to be an onboard device. */ - if ((dev_class & 0xff00) == 0x0300) - printf("VIDEO, "); - else if ((dev_class == 0x0401) || (dev_class == 0x0403)) - printf("SOUND, "); - else if (dev_class == 0x0100) - printf("SCSI, "); - else if (dev_class == 0x0101) - printf("IDE, "); - else - goto normal; - } else { + /* Determine slot type by location and class. */ + if ((entry->dev == 1) && (dev_class == 0x0604)) { + printf("AGPBRIDGE, "); + } else if (dev_class == 0x0601) { + printf("SOUTHBRIDGE,"); + } else if (entry->slot == 0) { + /* Very likely to be an onboard device. */ + if ((dev_class & 0xff00) == 0x0300) + printf("VIDEO, "); + else if ((dev_class == 0x0401) || (dev_class == 0x0403)) + printf("SOUND, "); + else if (dev_class == 0x0100) + printf("SCSI, "); + else if (dev_class == 0x0101) + printf("IDE, "); + else + goto normal; + } else { normal: - printf("NORMAL, "); - } - } + printf("NORMAL, "); + } + } - /* Print INTx# IRQ routing entries. */ - for (i = 0; i < 4; i++) { - if (i) - putchar(','); - printf(" %d", irq_bitmap[entry->ints[i].link]); - } + /* Print INTx# IRQ routing entries. */ + for (i = 0; i < 4; i++) { + if (i) + putchar(','); + printf(" %d", irq_bitmap[entry->ints[i].link]); + } - /* Finish line with a comment containing the slot number if present. */ - printf(");"); - if (entry->slot) - printf(" /* Slot %02X */", entry->slot); - else - printf(" /* Onboard */"); - } else { - /* Print slot, bus and device. */ - printf(" %02X %02X %02X ", entry->slot, entry->bus, entry->dev); + /* Finish line with a comment containing the slot number if present. */ + printf(");"); + if (entry->slot) + printf(" /* Slot %02X */", entry->slot); + else + printf(" /* Onboard */"); + } else { + /* Print slot, bus and device. */ + printf(" %02X %02X %02X ", entry->slot, entry->bus, entry->dev); - /* Clear IRQ bitmap. */ - memset(irq_bitmap, 0, 16); + /* Clear IRQ bitmap. */ + memset(irq_bitmap, 0, 16); - /* Print INTx# line values, while populating the IRQ bitmap. */ - for (i = 0; i < (sizeof(entry->ints) / sizeof(entry->ints[0])); i++) { - printf(" %02X", entry->ints[i].link); + /* Print INTx# line values, while populating the IRQ bitmap. */ + for (i = 0; i < (sizeof(entry->ints) / sizeof(entry->ints[0])); i++) { + printf(" %02X", entry->ints[i].link); - /* Set this INTx# line's bit on the bitmap entries for IRQs where it can be placed. */ - for (j = 0; j < 16; j++) - irq_bitmap[j] |= ((entry->ints[i].bitmap >> j) & 1) << i; - } + /* Set this INTx# line's bit on the bitmap entries for IRQs where it can be placed. */ + for (j = 0; j < 16; j++) + irq_bitmap[j] |= ((entry->ints[i].bitmap >> j) & 1) << i; + } - /* Print IRQ bitmap. */ - putchar(' '); - for (i = 0; i < 16; i++) - printf(" %X", irq_bitmap[i]); - } + /* Print IRQ bitmap. */ + putchar(' '); + for (i = 0; i < 16; i++) + printf(" %X", irq_bitmap[i]); + } - /* Move on to the next entry. */ - putchar('\n'); + /* Move on to the next entry. */ + putchar('\n'); next_entry: - entry++; + entry++; } return 0; } #endif - static int read_reg(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) { @@ -1236,7 +1217,7 @@ read_reg(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) /* Print banner message. */ printf("Reading from PCI bus %02X device %02X function %d registers [%02X:%02X]\n", - bus, dev, func, reg | 3, reg & 0xfc); + bus, dev, func, reg | 3, reg & 0xfc); /* Read dword value from register. */ #ifdef DEBUG @@ -1247,112 +1228,110 @@ read_reg(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg) /* Print value as a dword and bytes. */ printf("Value: %04X%04X / %04X %04X / %02X %02X %02X %02X\n", - reg_val.u16[1], reg_val.u16[0], - reg_val.u16[0], reg_val.u16[1], - reg_val.u8[0], reg_val.u8[1], reg_val.u8[2], reg_val.u8[3]); + reg_val.u16[1], reg_val.u16[0], + reg_val.u16[0], reg_val.u16[1], + reg_val.u8[0], reg_val.u8[1], reg_val.u8[2], reg_val.u8[3]); return 0; } - static int write_reg(uint8_t bus, uint8_t dev, uint8_t func, uint8_t reg, char *val) { uint16_t data_port; - multi_t reg_val; + multi_t reg_val; /* Write a byte, word or dword depending on the input value's length. */ data_port = 0xcfc; switch (strlen(val)) { - case 1: - case 2: - /* Byte input value. */ - parse_hex_u8(val, ®_val.u8[0]); + case 1: + case 2: + /* Byte input value. */ + parse_hex_u8(val, ®_val.u8[0]); - /* Print banner message. */ - printf("Writing %02X to PCI bus %02X device %02X function %d register [%02X]\n", - reg_val.u8[0], - bus, dev, func, reg); + /* Print banner message. */ + printf("Writing %02X to PCI bus %02X device %02X function %d register [%02X]\n", + reg_val.u8[0], + bus, dev, func, reg); - /* Write byte value to register. */ - pci_writeb(bus, dev, func, reg, reg_val.u8[0]); - printf("Written!\n"); + /* Write byte value to register. */ + pci_writeb(bus, dev, func, reg, reg_val.u8[0]); + printf("Written!\n"); - /* Read the register's byte value back. */ + /* Read the register's byte value back. */ #ifdef DEBUG - reg_val.u32 = pci_cf8(bus, dev, func, reg); - reg_val.u8[0] = reg_val.u8[reg & 3]; + reg_val.u32 = pci_cf8(bus, dev, func, reg); + reg_val.u8[0] = reg_val.u8[reg & 3]; #else - reg_val.u8[0] = pci_readb(bus, dev, func, reg); + reg_val.u8[0] = pci_readb(bus, dev, func, reg); #endif - printf("Readback: %02X\n", reg_val.u8[0]); + printf("Readback: %02X\n", reg_val.u8[0]); - break; + break; - case 3: - case 4: - /* Word input value. */ - parse_hex_u16(val, ®_val.u16[0]); + case 3: + case 4: + /* Word input value. */ + parse_hex_u16(val, ®_val.u16[0]); - /* Print banner message. */ - printf("Writing %04X to PCI bus %02X device %02X function %d registers [%02X:%02X]\n", - reg_val.u16[0], - bus, dev, func, reg | 1, reg & 0xfe); + /* Print banner message. */ + printf("Writing %04X to PCI bus %02X device %02X function %d registers [%02X:%02X]\n", + reg_val.u16[0], + bus, dev, func, reg | 1, reg & 0xfe); - /* Write word value to register. */ - pci_writew(bus, dev, func, reg, reg_val.u16[0]); - printf("Written!\n"); + /* Write word value to register. */ + pci_writew(bus, dev, func, reg, reg_val.u16[0]); + printf("Written!\n"); - /* Read the register's word value back. */ + /* Read the register's word value back. */ #ifdef DEBUG - reg_val.u32 = pci_cf8(bus, dev, func, reg); - reg_val.u16[0] = reg_val.u16[(reg >> 1) & 1]; + reg_val.u32 = pci_cf8(bus, dev, func, reg); + reg_val.u16[0] = reg_val.u16[(reg >> 1) & 1]; #else - reg_val.u16[0] = pci_readw(bus, dev, func, reg); + reg_val.u16[0] = pci_readw(bus, dev, func, reg); #endif - printf("Readback: %04X / %02X %02X\n", - reg_val.u16[0], - reg_val.u8[0], reg_val.u8[1]); + printf("Readback: %04X / %02X %02X\n", + reg_val.u16[0], + reg_val.u8[0], reg_val.u8[1]); - break; + break; - default: - /* Dword input value. */ - parse_hex_u32(val, ®_val.u32); + default: + /* Dword input value. */ + parse_hex_u32(val, ®_val.u32); - /* Print banner message. */ - printf("Writing %04X%04X to PCI bus %02X device %02X function %d registers [%02X:%02X]\n", - reg_val.u16[1], reg_val.u16[0], - bus, dev, func, reg | 3, reg & 0xfc); + /* Print banner message. */ + printf("Writing %04X%04X to PCI bus %02X device %02X function %d registers [%02X:%02X]\n", + reg_val.u16[1], reg_val.u16[0], + bus, dev, func, reg | 3, reg & 0xfc); - /* Write dword value to register. */ - pci_writel(bus, dev, func, reg, reg_val.u32); - printf("Written!\n"); + /* Write dword value to register. */ + pci_writel(bus, dev, func, reg, reg_val.u32); + printf("Written!\n"); - /* Read the register's dword value back. */ + /* Read the register's dword value back. */ #ifdef DEBUG - reg_val.u32 = pci_cf8(bus, dev, func, reg); + reg_val.u32 = pci_cf8(bus, dev, func, reg); #else - reg_val.u32 = pci_readl(bus, dev, func, reg); + reg_val.u32 = pci_readl(bus, dev, func, reg); #endif - printf("Readback: %04X%04X / %04X %04X / %02X %02X %02X %02X\n", - reg_val.u16[1], reg_val.u16[0], - reg_val.u16[0], reg_val.u16[1], - reg_val.u8[0], reg_val.u8[1], reg_val.u8[2], reg_val.u8[3]); + printf("Readback: %04X%04X / %04X %04X / %02X %02X %02X %02X\n", + reg_val.u16[1], reg_val.u16[0], + reg_val.u16[0], reg_val.u16[1], + reg_val.u8[0], reg_val.u8[1], reg_val.u8[2], reg_val.u8[3]); - break; + break; } return 0; } - int main(int argc, char **argv) { - int hexargc, i; - char *ch; - uint8_t hexargv[8], bus, dev, func, reg; + int hexargc, i; + char *ch; + uint8_t hexargv[8], bus, dev, func, reg; uint32_t cf8; /* Disable stdout buffering. */ @@ -1361,34 +1340,34 @@ main(int argc, char **argv) /* Print usage if there are too few parameters or if the first one looks invalid. */ if ((argc <= 1) || (strlen(argv[1]) < 2) || ((argv[1][0] != '-') && (argv[1][0] != '/'))) { usage: - printf("Usage:\n"); - printf("\n"); - printf("%s -s [-d]\n", argv[0]); - printf("∟ Display all devices on the PCI bus. Specify -d to dump registers as well.\n"); + printf("Usage:\n"); + printf("\n"); + printf("%s -s [-d]\n", argv[0]); + printf("∟ Display all devices on the PCI bus. Specify -d to dump registers as well.\n"); #if defined(__WATCOMC__) && !defined(M_I386) - printf("\n"); - printf("%s -t [-8]\n", argv[0]); - printf("∟ Display BIOS IRQ steering table. Specify -8 to display as 86Box code.\n"); + printf("\n"); + printf("%s -t [-8]\n", argv[0]); + printf("∟ Display BIOS IRQ steering table. Specify -8 to display as 86Box code.\n"); #endif - printf("\n"); - printf("%s -i [bus] device [function]\n", argv[0]); - printf("∟ Show information about the specified device.\n"); - printf("\n"); - printf("%s -r [bus] device [function] register\n", argv[0]); - printf("∟ Read the specified register.\n"); - printf("\n"); - printf("%s -w [bus] device [function] register value\n", argv[0]); - printf("∟ Write byte, word or dword to the specified register.\n"); - printf("\n"); - printf("%s {-d|-dw|-dl} [bus] device [function [register]]\n", argv[0]); - printf("∟ Dump registers as bytes (-d), words (-dw) or dwords (-dl). Optionally\n"); - printf(" specify the register to start from (requires bus to be specified as well).\n"); - printf("\n"); - printf("All numeric parameters should be specified in hexadecimal (without 0x prefix).\n"); - printf("{bus device function register} can be substituted for a single port CF8h dword.\n"); - printf("Register dumps are saved to PCIbbddf.BIN where bb=bus, dd=device, f=function."); - term_final_linebreak(); - return 1; + printf("\n"); + printf("%s -i [bus] device [function]\n", argv[0]); + printf("∟ Show information about the specified device.\n"); + printf("\n"); + printf("%s -r [bus] device [function] register\n", argv[0]); + printf("∟ Read the specified register.\n"); + printf("\n"); + printf("%s -w [bus] device [function] register value\n", argv[0]); + printf("∟ Write byte, word or dword to the specified register.\n"); + printf("\n"); + printf("%s {-d|-dw|-dl} [bus] device [function [register]]\n", argv[0]); + printf("∟ Dump registers as bytes (-d), words (-dw) or dwords (-dl). Optionally\n"); + printf(" specify the register to start from (requires bus to be specified as well).\n"); + printf("\n"); + printf("All numeric parameters should be specified in hexadecimal (without 0x prefix).\n"); + printf("{bus device function register} can be substituted for a single port CF8h dword.\n"); + printf("Register dumps are saved to PCIbbddf.BIN where bb=bus, dd=device, f=function."); + term_final_linebreak(); + return 1; } #ifdef DEBUG @@ -1396,153 +1375,153 @@ usage: #else /* Initialize PCI, and exit in case of failure. */ if (!pci_init()) - return 1; + return 1; #endif /* Convert the first parameter to lowercase. */ ch = argv[1]; while (*ch) { - if ((*ch >= 'A') && (*ch <= 'Z')) - *ch += 32; - ch++; + if ((*ch >= 'A') && (*ch <= 'Z')) + *ch += 32; + ch++; } /* Interpret parameters. */ if (argv[1][1] == 's') { - /* Bus scan only require a single optional parameter. */ - if ((argc >= 3) && (strlen(argv[2]) > 1)) - return scan_buses(argv[2][1]); - else - return scan_buses('\0'); + /* Bus scan only require a single optional parameter. */ + if ((argc >= 3) && (strlen(argv[2]) > 1)) + return scan_buses(argv[2][1]); + else + return scan_buses('\0'); } #if defined(__WATCOMC__) && !defined(M_I386) else if (argv[1][1] == 't') { - /* Steering table display only requires a single optional parameter. */ - if ((argc >= 3) && (strlen(argv[2]) > 1)) - return dump_steering_table(argv[2][1]); - else - return dump_steering_table('\0'); + /* Steering table display only requires a single optional parameter. */ + if ((argc >= 3) && (strlen(argv[2]) > 1)) + return dump_steering_table(argv[2][1]); + else + return dump_steering_table('\0'); } #endif else if ((argc >= 3) && (strlen(argv[1]) > 1)) { - /* Read second parameter as a dword. */ - if (parse_hex_u32(argv[2], &cf8)) { - /* Initialize default bus/device/function/register values. */ - bus = dev = func = reg = 0; + /* Read second parameter as a dword. */ + if (parse_hex_u32(argv[2], &cf8)) { + /* Initialize default bus/device/function/register values. */ + bus = dev = func = reg = 0; - /* Determine if the second parameter's value is a port CF8h dword. */ - hexargc = 0; - if (cf8 & 0xffffff00) { - /* Break the CF8h dword's MSBs down into individual parameters. */ - hexargv[hexargc++] = (cf8 >> 16) & 0xff; - hexargv[hexargc++] = (cf8 >> 11) & 31; - hexargv[hexargc++] = (cf8 >> 8) & 7; - } - hexargv[hexargc++] = cf8; + /* Determine if the second parameter's value is a port CF8h dword. */ + hexargc = 0; + if (cf8 & 0xffffff00) { + /* Break the CF8h dword's MSBs down into individual parameters. */ + hexargv[hexargc++] = (cf8 >> 16) & 0xff; + hexargv[hexargc++] = (cf8 >> 11) & 31; + hexargv[hexargc++] = (cf8 >> 8) & 7; + } + hexargv[hexargc++] = cf8; - /* Read parameters until the end is reached or an invalid hex value is found. */ - for (i = 3; (i < argc) && (i < (sizeof(hexargv) - 1)); i++) { - if (!parse_hex_u8(argv[i], &hexargv[hexargc++])) - break; - } - } else { - /* Print usage if the second parameter is an invalid hex value. */ - goto usage; - } + /* Read parameters until the end is reached or an invalid hex value is found. */ + for (i = 3; (i < argc) && (i < (sizeof(hexargv) - 1)); i++) { + if (!parse_hex_u8(argv[i], &hexargv[hexargc++])) + break; + } + } else { + /* Print usage if the second parameter is an invalid hex value. */ + goto usage; + } - if ((argv[1][1] == 'd') || (argv[1][1] == 'i')) { - /* Process parameters for a register or information dump. */ - switch (hexargc) { - case 4: - /* Specifying a register is not valid on an information dump. */ - if (argv[1][1] == 'i') - goto usage; - reg = hexargv[3]; - /* fall-through */ + if ((argv[1][1] == 'd') || (argv[1][1] == 'i')) { + /* Process parameters for a register or information dump. */ + switch (hexargc) { + case 4: + /* Specifying a register is not valid on an information dump. */ + if (argv[1][1] == 'i') + goto usage; + reg = hexargv[3]; + /* fall-through */ - case 3: - func = hexargv[2]; - dev = hexargv[1]; - bus = hexargv[0]; - break; + case 3: + func = hexargv[2]; + dev = hexargv[1]; + bus = hexargv[0]; + break; - case 2: - func = hexargv[1]; - /* fall-through */ + case 2: + func = hexargv[1]; + /* fall-through */ - case 1: - dev = hexargv[0]; - break; + case 1: + dev = hexargv[0]; + break; - case -1: - break; + case -1: + break; - default: - goto usage; - } + default: + goto usage; + } - /* Start the operation. */ - switch (argv[1][1]) { - case 'd': - /* Start register dump. */ - return dump_regs(bus, dev, func, reg, argv[1][2]); + /* Start the operation. */ + switch (argv[1][1]) { + case 'd': + /* Start register dump. */ + return dump_regs(bus, dev, func, reg, argv[1][2]); - case 'i': - /* Start information dump. */ - return dump_info(bus, dev, func); - } - } else { - /* Subtract value parameter from a write operation. */ - if (argv[1][1] == 'w') - hexargc -= 1; + case 'i': + /* Start information dump. */ + return dump_info(bus, dev, func); + } + } else { + /* Subtract value parameter from a write operation. */ + if (argv[1][1] == 'w') + hexargc -= 1; - /* Process parameters for read/write operations. */ - switch (hexargc) { - case 4: - reg = hexargv[3]; - func = hexargv[2]; - dev = hexargv[1]; - bus = hexargv[0]; - break; + /* Process parameters for read/write operations. */ + switch (hexargc) { + case 4: + reg = hexargv[3]; + func = hexargv[2]; + dev = hexargv[1]; + bus = hexargv[0]; + break; - case 3: - reg = hexargv[2]; - /* fall-through */ + case 3: + reg = hexargv[2]; + /* fall-through */ - case 2: - func = hexargv[1]; - /* fall-through */ + case 2: + func = hexargv[1]; + /* fall-through */ - case 1: - dev = hexargv[0]; - break; + case 1: + dev = hexargv[0]; + break; - case -1: - break; + case -1: + break; - default: - printf("unknown hexargc %d\n", hexargc); - goto usage; - } + default: + printf("unknown hexargc %d\n", hexargc); + goto usage; + } - /* Start the operation. */ - switch (argv[1][1]) { - case 'r': - /* Start read. */ - return read_reg(bus, dev, func, reg); + /* Start the operation. */ + switch (argv[1][1]) { + case 'r': + /* Start read. */ + return read_reg(bus, dev, func, reg); - case 'w': - /* Start write. */ - return write_reg(bus, dev, func, reg, argv[2 + hexargc]); + case 'w': + /* Start write. */ + return write_reg(bus, dev, func, reg, argv[2 + hexargc]); - default: - /* Print usage if an unknown parameter was specified. */ - goto usage; - } - } + default: + /* Print usage if an unknown parameter was specified. */ + goto usage; + } + } } else { - /* Print usage if a single parameter was specified. */ - goto usage; + /* Print usage if a single parameter was specified. */ + goto usage; } return 1; diff --git a/usblgoff/usblgoff.c b/usblgoff/usblgoff.c index c3973db..30fdf76 100644 --- a/usblgoff/usblgoff.c +++ b/usblgoff/usblgoff.c @@ -24,12 +24,11 @@ #include #include "clib.h" - static void pci_scan_callback(uint8_t bus, uint8_t dev, uint8_t func, - uint16_t ven_id, uint16_t dev_id) + uint16_t ven_id, uint16_t dev_id) { - uint8_t i; + uint8_t i; uint16_t j, start, end; #ifdef IS_32BIT uint32_t mmio_base, *mmio; @@ -37,102 +36,96 @@ pci_scan_callback(uint8_t bus, uint8_t dev, uint8_t func, #ifdef IS_32BIT /* Disable southbridge I/O traps. */ - if ((ven_id == 0x8086) && (((dev_id >= 0x2640) && (dev_id <= 0x2642)) /* ICH6 */ || - ((dev_id >= 0x27b0) && (dev_id <= 0x27bd)) /* ICH7 */ || - ((dev_id >= 0x2810) && (dev_id <= 0x2815)) /* ICH8 */ || - ((dev_id >= 0x2912) && (dev_id <= 0x2919)) /* ICH9 */ || - ((dev_id >= 0x3a14) && (dev_id <= 0x3a1a)) /* ICH10 */)) { - printf("Found ICH6-10 LPC bridge %04X at bus %02X device %02X function %d\n", dev_id, bus, dev, func); + if ((ven_id == 0x8086) && (((dev_id >= 0x2640) && (dev_id <= 0x2642)) /* ICH6 */ || ((dev_id >= 0x27b0) && (dev_id <= 0x27bd)) /* ICH7 */ || ((dev_id >= 0x2810) && (dev_id <= 0x2815)) /* ICH8 */ || ((dev_id >= 0x2912) && (dev_id <= 0x2919)) /* ICH9 */ || ((dev_id >= 0x3a14) && (dev_id <= 0x3a1a)) /* ICH10 */)) { + printf("Found ICH6-10 LPC bridge %04X at bus %02X device %02X function %d\n", dev_id, bus, dev, func); - /* Get RCBA. */ - printf("> RCBA is "); - mmio_base = pci_readl(bus, dev, func, 0xf0); - if ((mmio_base & 0x00000001) && (mmio_base != 0xffffffff)) { - printf("assigned to %08X\n", mmio_base); - mmio = (uint32_t *) (mmio_base & ~0x00003fff); + /* Get RCBA. */ + printf("> RCBA is "); + mmio_base = pci_readl(bus, dev, func, 0xf0); + if ((mmio_base & 0x00000001) && (mmio_base != 0xffffffff)) { + printf("assigned to %08X\n", mmio_base); + mmio = (uint32_t *) (mmio_base & ~0x00003fff); - /* Disable all relevant I/O traps. */ - for (i = 0; i < 4; i++) { - /* Check if the trap is enabled. */ - j = (0x1e80 | (i << 3)) >> 2; - if (mmio[j] & 0x00000001) { - start = mmio[j] & 0xfffc; - end = (mmio[j] >> 16) & 0xfc; /* temporarily just the mask */ - printf("> Trap %d (%04X+%02X)", i, start, end); - end += start; /* now the actual end */ + /* Disable all relevant I/O traps. */ + for (i = 0; i < 4; i++) { + /* Check if the trap is enabled. */ + j = (0x1e80 | (i << 3)) >> 2; + if (mmio[j] & 0x00000001) { + start = mmio[j] & 0xfffc; + end = (mmio[j] >> 16) & 0xfc; /* temporarily just the mask */ + printf("> Trap %d (%04X+%02X)", i, start, end); + end += start; /* now the actual end */ - /* Check if the trap covers the KBC ports. */ - if (((start <= 0x60) && (end >= 0x60)) || - ((start <= 0x64) && (end >= 0x64))) { - /* Clear TRSE bit. */ - mmio[j] &= ~0x00000001; + /* Check if the trap covers the KBC ports. */ + if (((start <= 0x60) && (end >= 0x60)) || ((start <= 0x64) && (end >= 0x64))) { + /* Clear TRSE bit. */ + mmio[j] &= ~0x00000001; - /* Check if the bit was actually cleared. */ - if (mmio[j] & 0x00000001) - printf("enable bit stuck! (%08X%08X)\n", mmio[j | 1], mmio[j]); - else - printf("disabled\n"); - } else { - printf("not relevant\n"); - } - } - } - } else { - printf("unassigned! (%08X)\n", mmio_base); - } + /* Check if the bit was actually cleared. */ + if (mmio[j] & 0x00000001) + printf("enable bit stuck! (%08X%08X)\n", mmio[j | 1], mmio[j]); + else + printf("disabled\n"); + } else { + printf("not relevant\n"); + } + } + } + } else { + printf("unassigned! (%08X)\n", mmio_base); + } - return; + return; } #endif /* Skip non-USB devices. */ if (pci_readw(bus, dev, func, 0x0a) != 0x0c03) - return; + return; /* Read progif code. */ i = pci_readb(bus, dev, func, 0x09); /* Act according to the device class. */ if (i == 0x00) { /* UHCI */ - printf("Found UHCI USB controller at bus %02X device %02X function %d\n", bus, dev, func); + printf("Found UHCI USB controller at bus %02X device %02X function %d\n", bus, dev, func); - /* Clear 60/64h trap/SMI R/W bits. */ - j = pci_readw(bus, dev, func, 0xc0); - pci_writew(bus, dev, func, 0xc0, j & ~0x000f); + /* Clear 60/64h trap/SMI R/W bits. */ + j = pci_readw(bus, dev, func, 0xc0); + pci_writew(bus, dev, func, 0xc0, j & ~0x000f); - /* Check if the bits were actually cleared. */ - j = pci_readw(bus, dev, func, 0xc0); - if (j & 0x000f) - printf("> I/O trap bits stuck! (%04X)\n", j); - else - printf("> I/O traps disabled\n"); + /* Check if the bits were actually cleared. */ + j = pci_readw(bus, dev, func, 0xc0); + if (j & 0x000f) + printf("> I/O trap bits stuck! (%04X)\n", j); + else + printf("> I/O traps disabled\n"); } else if (i == 0x10) { /* OHCI */ - printf("Found OHCI USB controller at bus %02X device %02X function %d\n", bus, dev, func); + printf("Found OHCI USB controller at bus %02X device %02X function %d\n", bus, dev, func); #ifdef IS_32BIT - /* Get MMIO base address. */ - mmio_base = pci_get_mem_bar(bus, dev, func, 0x10, 4096, "> MMIO"); - if (mmio_base) { - mmio = (uint32_t *) mmio_base; + /* Get MMIO base address. */ + mmio_base = pci_get_mem_bar(bus, dev, func, 0x10, 4096, "> MMIO"); + if (mmio_base) { + mmio = (uint32_t *) mmio_base; - /* Clear EmulationEnable bit. */ - mmio[0x100 >> 2] &= ~0x00000001; + /* Clear EmulationEnable bit. */ + mmio[0x100 >> 2] &= ~0x00000001; - /* Check if the bit was actually cleared. */ - if (mmio[0x100 >> 2] & 0x00000001) - printf("> Emulation bit stuck! (%08X)\n", mmio[0x100 >> 2]); - else - printf("> Emulation disabled\n"); - } + /* Check if the bit was actually cleared. */ + if (mmio[0x100 >> 2] & 0x00000001) + printf("> Emulation bit stuck! (%08X)\n", mmio[0x100 >> 2]); + else + printf("> Emulation disabled\n"); + } #else - printf("> OHCI not supported on 16-bit build!\n"); + printf("> OHCI not supported on 16-bit build!\n"); #endif } else if ((i != 0x20) && (i != 0x30)) { /* not EHCI or XHCI */ - printf("Found unknown USB controller type %02X at bus %02X device %02X function %d\n", i, bus, dev, func); + printf("Found unknown USB controller type %02X at bus %02X device %02X function %d\n", i, bus, dev, func); } } - int main(int argc, char **argv) { @@ -143,7 +136,7 @@ main(int argc, char **argv) /* Initialize PCI, and exit in case of failure. */ if (!pci_init()) - return 1; + return 1; /* Scan PCI bus 0. */ pci_scan_bus(0, pci_scan_callback);