usr.sbin/acpi/acpidb/acpidb.c
static int
load_dsdt(const char *dsdtfile)
{
	char			filetmp[PATH_MAX];
	u_int8_t		*code, *amlptr;
	struct stat		 sb;
	int			 fd, fd2;
	int			 error;
	ACPI_TABLE_HEADER	*Table;

	fd = open(dsdtfile, O_RDONLY, 0);
	if (fd == -1) {
		perror("open");
		return (-1);
	}
	if (fstat(fd, &sb) == -1) {
		perror("fstat");
		return (-1);
	}
	code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
	if (code == NULL) {
		perror("mmap");
		return (-1);
	}
	mapped_rsdp = code;
	if ((error = AcpiInitializeSubsystem()) != AE_OK) {
		err(1, "AcpiInitializeSubsystem returned %d", error);
		return (-1);
	}

	/*
	 * make sure DSDT data contains table header or not.
	 */
	if (strncmp((char *)code, "DSDT", 4) == 0) {
		strncpy(filetmp, dsdtfile, sizeof(filetmp));
	} else {
		mode_t	mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
		dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
		snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
		fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
		if (fd2 == -1) {
			perror("open");
			return (-1);
		}
		write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));

		write(fd2, code, sb.st_size);
		close(fd2);
	}

	/*
	 * Install the virtual machine version of address space handlers.
	 */
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_SYSTEM_MEMORY,
			aml_vm_space_handler_system_memory,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_SYSTEM_IO,
			aml_vm_space_handler_system_io,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_PCI_CONFIG,
			aml_vm_space_handler_pci_config,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_EC,
			aml_vm_space_handler_ec,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise EC handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_SMBUS,
			aml_vm_space_handler_smbus,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_CMOS,
			aml_vm_space_handler_cmos,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_PCI_BAR_TARGET,
			aml_vm_space_handler_pci_bar_target,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
		return (-1);
	}

	AcpiGbl_GlobalLockPresent = TRUE;

	error = AcpiDbReadTableFromFile (filetmp, &Table);
	if (ACPI_FAILURE(error)) {
		fprintf(stderr, "AcpiDbReadTableFromFile failed: %s\n",
			AcpiFormatException(error));
		return (-1);
	}
	AeBuildLocalTables(Table);
	if (ACPI_FAILURE(error = AeInstallTables())) {
		fprintf(stderr, "AeInstallTables failed: %s\n",
			AcpiFormatException(error));
		return (-1);
	}

	AcpiUtSetIntegerWidth (Table->Revision);

	AcpiDbInitialize();
	AcpiGbl_DebuggerConfiguration = 0;
	AcpiDbUserCommands(':', NULL);

	if (strcmp(dsdtfile, filetmp) != 0) {
		unlink(filetmp);
	}

	return (0);
}
/*
 * Load DSDT data file and invoke debugger
 */
Load DSDT data file and invoke debugger
Load DSDT data file and invoke debugger
static int
load_dsdt(const char *dsdtfile)
{
	char			filetmp[PATH_MAX];
	u_int8_t		*code, *amlptr;
	struct stat		 sb;
	int			 fd, fd2;
	int			 error;
	ACPI_TABLE_HEADER	*Table;
	fd = open(dsdtfile, O_RDONLY, 0);
	if (fd == -1) {
		perror("open");
		return (-1);
	}
	if (fstat(fd, &sb) == -1) {
		perror("fstat");
		return (-1);
	}
	code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
	if (code == NULL) {
		perror("mmap");
		return (-1);
	}
	mapped_rsdp = code;
	if ((error = AcpiInitializeSubsystem()) != AE_OK) {
		err(1, "AcpiInitializeSubsystem returned %d", error);
		return (-1);
	}
	if (strncmp((char *)code, "DSDT", 4) == 0) {
		strncpy(filetmp, dsdtfile, sizeof(filetmp));
	} else {
		mode_t	mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
		dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size;
		snprintf(filetmp, sizeof(filetmp), "%s.tmp", dsdtfile);
		fd2 = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
		if (fd2 == -1) {
			perror("open");
			return (-1);
		}
		write(fd2, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
		write(fd2, code, sb.st_size);
		close(fd2);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_SYSTEM_MEMORY,
			aml_vm_space_handler_system_memory,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_SYSTEM_IO,
			aml_vm_space_handler_system_io,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise SystemIO handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_PCI_CONFIG,
			aml_vm_space_handler_pci_config,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise PciConfig handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_EC,
			aml_vm_space_handler_ec,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise EC handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_SMBUS,
			aml_vm_space_handler_smbus,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise SMBUS handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_CMOS,
			aml_vm_space_handler_cmos,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise CMOS handler: %d\n", error);
		return (-1);
	}
	if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
			ACPI_ADR_SPACE_PCI_BAR_TARGET,
			aml_vm_space_handler_pci_bar_target,
			NULL, NULL)) != AE_OK) {
		fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error);
		return (-1);
	}
	AcpiGbl_GlobalLockPresent = TRUE;
	error = AcpiDbReadTableFromFile (filetmp, &Table);
	if (ACPI_FAILURE(error)) {
		fprintf(stderr, "AcpiDbReadTableFromFile failed: %s\n",
			AcpiFormatException(error));
		return (-1);
	}
	AeBuildLocalTables(Table);
	if (ACPI_FAILURE(error = AeInstallTables())) {
		fprintf(stderr, "AeInstallTables failed: %s\n",
			AcpiFormatException(error));
		return (-1);
	}
	AcpiUtSetIntegerWidth (Table->Revision);
	AcpiDbInitialize();
	AcpiGbl_DebuggerConfiguration = 0;
	AcpiDbUserCommands(':', NULL);
	if (strcmp(dsdtfile, filetmp) != 0) {
		unlink(filetmp);
	}
	return (0);
}
static int load_dsdt ( const char * dsdtfile ) { char filetmp [ PATH_MAX ] ; u_int8_t * code , * amlptr ; struct stat sb ; int fd , fd2 ; int error ; ACPI_TABLE_HEADER * Table ; fd = open ( dsdtfile , O_RDONLY , 0 ) ; if ( fd == -1 ) { perror ( " " ) ; return ( -1 ) ; } if ( fstat ( fd , & sb ) == -1 ) { perror ( " " ) ; return ( -1 ) ; } code = mmap ( NULL , ( size_t ) sb . st_size , PROT_READ , MAP_PRIVATE , fd , ( off_t ) 0 ) ; if ( code == NULL ) { perror ( " " ) ; return ( -1 ) ; } mapped_rsdp = code ; if ( ( error = AcpiInitializeSubsystem ( ) ) != AE_OK ) { err ( 1 , " " , error ) ; return ( -1 ) ; } if ( strncmp ( ( char * ) code , " " , 4 ) == 0 ) { strncpy ( filetmp , dsdtfile , sizeof ( filetmp ) ) ; } else { mode_t mode = ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ; dummy_dsdt_table . Length = sizeof ( ACPI_TABLE_HEADER ) + sb . st_size ; snprintf ( filetmp , sizeof ( filetmp ) , " " , dsdtfile ) ; fd2 = open ( filetmp , O_WRONLY | O_CREAT | O_TRUNC , mode ) ; if ( fd2 == -1 ) { perror ( " " ) ; return ( -1 ) ; } write ( fd2 , & dummy_dsdt_table , sizeof ( ACPI_TABLE_HEADER ) ) ; write ( fd2 , code , sb . st_size ) ; close ( fd2 ) ; } if ( ( error = AcpiInstallAddressSpaceHandler ( ACPI_ROOT_OBJECT , ACPI_ADR_SPACE_SYSTEM_MEMORY , aml_vm_space_handler_system_memory , NULL , NULL ) ) != AE_OK ) { fprintf ( stderr , " \n " , error ) ; return ( -1 ) ; } if ( ( error = AcpiInstallAddressSpaceHandler ( ACPI_ROOT_OBJECT , ACPI_ADR_SPACE_SYSTEM_IO , aml_vm_space_handler_system_io , NULL , NULL ) ) != AE_OK ) { fprintf ( stderr , " \n " , error ) ; return ( -1 ) ; } if ( ( error = AcpiInstallAddressSpaceHandler ( ACPI_ROOT_OBJECT , ACPI_ADR_SPACE_PCI_CONFIG , aml_vm_space_handler_pci_config , NULL , NULL ) ) != AE_OK ) { fprintf ( stderr , " \n " , error ) ; return ( -1 ) ; } if ( ( error = AcpiInstallAddressSpaceHandler ( ACPI_ROOT_OBJECT , ACPI_ADR_SPACE_EC , aml_vm_space_handler_ec , NULL , NULL ) ) != AE_OK ) { fprintf ( stderr , " \n " , error ) ; return ( -1 ) ; } if ( ( error = AcpiInstallAddressSpaceHandler ( ACPI_ROOT_OBJECT , ACPI_ADR_SPACE_SMBUS , aml_vm_space_handler_smbus , NULL , NULL ) ) != AE_OK ) { fprintf ( stderr , " \n " , error ) ; return ( -1 ) ; } if ( ( error = AcpiInstallAddressSpaceHandler ( ACPI_ROOT_OBJECT , ACPI_ADR_SPACE_CMOS , aml_vm_space_handler_cmos , NULL , NULL ) ) != AE_OK ) { fprintf ( stderr , " \n " , error ) ; return ( -1 ) ; } if ( ( error = AcpiInstallAddressSpaceHandler ( ACPI_ROOT_OBJECT , ACPI_ADR_SPACE_PCI_BAR_TARGET , aml_vm_space_handler_pci_bar_target , NULL , NULL ) ) != AE_OK ) { fprintf ( stderr , " \n " , error ) ; return ( -1 ) ; } AcpiGbl_GlobalLockPresent = TRUE ; error = AcpiDbReadTableFromFile ( filetmp , & Table ) ; if ( ACPI_FAILURE ( error ) ) { fprintf ( stderr , " \n " , AcpiFormatException ( error ) ) ; return ( -1 ) ; } AeBuildLocalTables ( Table ) ; if ( ACPI_FAILURE ( error = AeInstallTables ( ) ) ) { fprintf ( stderr , " \n " , AcpiFormatException ( error ) ) ; return ( -1 ) ; } AcpiUtSetIntegerWidth ( Table -> Revision ) ; AcpiDbInitialize ( ) ; AcpiGbl_DebuggerConfiguration = 0 ; AcpiDbUserCommands ( ' ' , NULL ) ; if ( strcmp ( dsdtfile , filetmp ) != 0 ) { unlink ( filetmp ) ; } return ( 0 ) ; }
Load DSDT data file and invoke debugger
Load DSDT data file and invoke debugger
/*
	 * make sure DSDT data contains table header or not.
	 */ /*
	 * Install the virtual machine version of address space handlers.
	 */