iPXE
Functions
apm.c File Reference

Advanced Power Management. More...

#include <errno.h>
#include <realmode.h>
#include <ipxe/apm.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int apm_poweroff (void)
 Power off the computer using APM.

Detailed Description

Advanced Power Management.

Definition in file apm.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
int apm_poweroff ( void  )

Power off the computer using APM.

Return values:
rcReturn status code

Definition at line 42 of file apm.c.

References __asm__(), DBG, DBG2, ECANCELED, EIO, ENOTSUP, ENOTTY, EPERM, and REAL_CODE.

Referenced by bios_poweroff().

                          {
        uint16_t apm_version;
        uint16_t apm_signature;
        uint16_t apm_flags;
        uint16_t carry;

        /* APM check */
        __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
                                           "adc %%edx,0\n\t" )
                               : "=a" ( apm_version ), "=b" ( apm_signature ),
                                 "=c" ( apm_flags ), "=d" ( carry )
                               : "a" ( 0x5300 ), "b" ( 0x0000 ),
                                 "d" ( 0x0000 ) );
        if ( carry ) {
                DBG ( "APM not present\n" );
                return -ENOTSUP;
        }
        if ( apm_signature != 0x504d ) { /* signature 'PM' */
                DBG ( "APM not present\n" );
                return -ENOTSUP;
        }
        if ( apm_version < 0x0101 ) { /* Need version 1.1+ */
                DBG ( "APM 1.1+ not supported\n" );
                return -ENOTSUP;
        }
        if ( ( apm_flags & 0x8 ) == 0x8 ) {
                DBG ( "APM power management disabled\n" );
                return -EPERM;
        }
        DBG2 ( "APM check completed\n" );

        /* APM initialisation */
        __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
                                           "adc %%edx,0\n\t" )
                               : "=d" ( carry )
                               : "a" ( 0x5301 ), "b" ( 0x0000 ),
                                 "d" ( 0x0000 ) );
        if ( carry ) {
                DBG ( "APM initialisation failed\n" );
                return -EIO;
        }
        DBG2 ( "APM initialisation completed\n" );

        /* Set APM driver version */
        __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
                                           "adc %%edx,0\n\t" )
                               : "=d" ( carry )
                               : "a" ( 0x530e ), "b" ( 0x0000 ),
                                 "c" ( 0x0101 ), "d" ( 0x0000 ) );
        if ( carry ) {
                DBG ( "APM setting driver version failed\n" );
                return -EIO;
        }
        DBG2 ( "APM driver version set\n" );

        /* Setting power state to off */
        __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
                                           "adc %%edx,0\n\t" )
                               : "=d" ( carry )
                               : "a" ( 0x5307 ), "b" ( 0x0001 ),
                                 "c" ( 0x0003 ), "d" ( 0x0000) );
        if ( carry ) {
                DBG ( "APM setting power state failed\n" );
                return -ENOTTY;
        }

        /* Should never happen */
        return -ECANCELED;
}