iPXE
apm.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * Advanced Power Management
30  *
31  */
32 
33 #include <errno.h>
34 #include <realmode.h>
35 #include <ipxe/apm.h>
36 
37 /**
38  * Power off the computer using APM
39  *
40  * @ret rc Return status code
41  */
42 int apm_poweroff ( void ) {
43  uint16_t apm_version;
44  uint16_t apm_signature;
45  uint16_t apm_flags;
46  uint16_t carry;
47 
48  /* APM check */
49  __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
50  "adc %%edx,0\n\t" )
51  : "=a" ( apm_version ), "=b" ( apm_signature ),
52  "=c" ( apm_flags ), "=d" ( carry )
53  : "a" ( 0x5300 ), "b" ( 0x0000 ),
54  "d" ( 0x0000 ) );
55  if ( carry ) {
56  DBG ( "APM not present\n" );
57  return -ENOTSUP;
58  }
59  if ( apm_signature != 0x504d ) { /* signature 'PM' */
60  DBG ( "APM not present\n" );
61  return -ENOTSUP;
62  }
63  if ( apm_version < 0x0101 ) { /* Need version 1.1+ */
64  DBG ( "APM 1.1+ not supported\n" );
65  return -ENOTSUP;
66  }
67  if ( ( apm_flags & 0x8 ) == 0x8 ) {
68  DBG ( "APM power management disabled\n" );
69  return -EPERM;
70  }
71  DBG2 ( "APM check completed\n" );
72 
73  /* APM initialisation */
74  __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
75  "adc %%edx,0\n\t" )
76  : "=d" ( carry )
77  : "a" ( 0x5301 ), "b" ( 0x0000 ),
78  "d" ( 0x0000 ) );
79  if ( carry ) {
80  DBG ( "APM initialisation failed\n" );
81  return -EIO;
82  }
83  DBG2 ( "APM initialisation completed\n" );
84 
85  /* Set APM driver version */
86  __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
87  "adc %%edx,0\n\t" )
88  : "=d" ( carry )
89  : "a" ( 0x530e ), "b" ( 0x0000 ),
90  "c" ( 0x0101 ), "d" ( 0x0000 ) );
91  if ( carry ) {
92  DBG ( "APM setting driver version failed\n" );
93  return -EIO;
94  }
95  DBG2 ( "APM driver version set\n" );
96 
97  /* Setting power state to off */
98  __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
99  "adc %%edx,0\n\t" )
100  : "=d" ( carry )
101  : "a" ( 0x5307 ), "b" ( 0x0001 ),
102  "c" ( 0x0003 ), "d" ( 0x0000) );
103  if ( carry ) {
104  DBG ( "APM setting power state failed\n" );
105  return -ENOTTY;
106  }
107 
108  /* Should never happen */
109  return -ECANCELED;
110 }
unsigned short uint16_t
Definition: stdint.h:11
Error codes.
#define ECANCELED
Operation canceled.
Definition: errno.h:343
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
__asm__ __volatile__("\n1:\n\t" "movb -1(%3,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %4, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c), "+m"(*value) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define EPERM
Operation not permitted.
Definition: errno.h:614
int apm_poweroff(void)
Power off the computer using APM.
Definition: apm.c:42
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:594
Advanced Power Management.
#define EIO
Input/output error.
Definition: errno.h:433
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226
#define DBG2(...)
Definition: compiler.h:515