1 /* intelmetool Dump interesting things about Management Engine even if hidden */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
14 #include <machine/sysarch.h>
17 #include "intelmetool.h"
26 static uint32_t fd2
= 0;
27 static int ME_major_ver
= 0;
28 static int ME_minor_ver
= 0;
30 static void dumpmem(uint8_t *phys
, uint32_t size
)
33 printf("Dumping cloned ME memory:\n");
34 for (i
= 0; i
< size
; i
++) {
35 printf("%02X",*((uint8_t *) (phys
+ i
)));
40 static void zeroit(uint8_t *phys
, uint32_t size
)
43 for (i
= 0; i
< size
; i
++) {
44 *((uint8_t *) (phys
+ i
)) = 0x00;
48 static void dumpmemfile(uint8_t *phys
, uint32_t size
)
50 FILE *fp
= fopen("medump.bin", "w");
52 for (i
= 0; i
< size
; i
++) {
53 fprintf(fp
, "%c", *((uint8_t *) (phys
+ i
)));
58 static int isCPUGenuineIntel(void)
61 unsigned int level
= 0;
64 __get_cpuid(level
, &eax
, ®s
.ebx
, ®s
.ecx
, ®s
.edx
);
66 return !strncmp((char *)®s
, "GenuineIntel", CPU_ID_SIZE
-1);
69 /* You need >4GB total ram, in kernel cmdline, use 'mem=1000m'
70 * then this code will clone to absolute memory address 0xe0000000
71 * which can be read using a mmap tool at that offset.
72 * Real ME memory is located around top of memory minus 64MB. (I think)
73 * so we avoid cloning to this part.
75 static void dump_me_memory(void)
77 uintptr_t me_clone
= 0x60000000;
80 dump
= map_physical_exact((off_t
)me_clone
, (void *)me_clone
, 0x2000000);
82 printf("Could not map ME memory\n");
85 zeroit(dump
, 0x2000000);
86 printf("Send magic command for memory clone\n");
89 usleep(ME_COMMAND_DELAY
);
90 void* ptr
= &me_clone
;
91 int err
= mkhi_debug_me_memory(ptr
);
94 printf("Wait a second...");
95 usleep(ME_COMMAND_DELAY
);
96 printf("done\n\nHere are the first bytes:\n");
97 dumpmemfile(dump
, 0x2000000);
98 //printf("Try reading 0x%zx with other mmap tool...\n"
99 // "Press enter to quit, you only get one chance to run"
100 // "this tool before reboot required for some reason\n",
102 while (getc(stdin
) != '\n') {};
103 unmap_physical(dump
, 0x2000000);
107 static int pci_platform_scan(void)
109 struct pci_access
*pacc
;
115 pacc
->method
= PCI_ACCESS_I386_TYPE1
;
120 for (dev
=pacc
->devices
; dev
; dev
=dev
->next
) {
121 pci_fill_info(dev
, PCI_FILL_IDENT
| PCI_FILL_BASES
|
122 PCI_FILL_SIZES
| PCI_FILL_CLASS
);
123 name
= pci_lookup_name(pacc
, namebuf
, sizeof(namebuf
),
124 PCI_LOOKUP_DEVICE
, dev
->vendor_id
, dev
->device_id
);
127 if (dev
->vendor_id
!= PCI_VENDOR_ID_INTEL
)
130 if (PCI_DEV_NO_ME(dev
->device_id
)) {
131 printf(CGRN
"Good news, you have a `%s` so you have "
132 "no ME present at all, continuing...\n\n"
135 } else if (PCI_DEV_HAS_ME_DISABLE(dev
->device_id
)) {
136 printf(CGRN
"Good news, you have a `%s` so ME is "
137 "present but can be disabled, continuing...\n\n"
140 } else if (PCI_DEV_HAS_ME_DIFFICULT(dev
->device_id
)) {
141 printf(CRED
"Bad news, you have a `%s` so you have ME "
142 "hardware on board and you can't control or "
143 "disable it, continuing...\n\n" RESET
, name
);
145 } else if (PCI_DEV_CAN_DISABLE_ME_IF_PRESENT(dev
->device_id
)) {
146 printf(CYEL
"Not sure if ME hardware is present "
147 "because you have a `%s`, but it is possible to "
148 "disable it if you do, continuing...\n\n" RESET
,
151 } else if (PCI_DEV_ME_NOT_SURE(dev
->device_id
)) {
152 printf(CYEL
"Found `%s`. Not sure whether you have ME "
153 "hardware, exiting\n\n" RESET
, name
);
160 !PCI_DEV_HAS_ME_DISABLE(dev
->device_id
) &&
161 !PCI_DEV_HAS_ME_DIFFICULT(dev
->device_id
) &&
162 !PCI_DEV_CAN_DISABLE_ME_IF_PRESENT(dev
->device_id
) &&
163 !PCI_DEV_ME_NOT_SURE(dev
->device_id
)) {
164 printf(CCYN
"ME is not present on your board or unknown\n\n"
175 static int activate_me(void)
177 const uint32_t rcba
= get_rcba_phys();
179 printf("RCBA addr: 0x%08x\n", rcba
);
181 if (read_rcba32(FD2
, &fd2
)) {
182 printf("Error reading RCBA\n");
185 if (write_rcba32(FD2
, fd2
& ~0x2)) {
186 printf("Error writing RCBA\n");
189 if (debug
&& (fd2
& 0x2))
190 printf("MEI was hidden on PCI, now unlocked\n");
192 printf("MEI not hidden on PCI, checking if visible\n");
198 static void rehide_me(void)
200 const uint32_t rcba
= get_rcba_phys();
204 printf("Re-hiding MEI device...");
205 if (read_rcba32(FD2
, &fd2
)) {
206 printf("Error reading RCBA\n");
209 if (write_rcba32(FD2
, fd2
| 0x2)) {
210 printf("Error writing RCBA\n");
219 static struct pci_dev
*pci_me_interface_scan(const char **name
, char *namebuf
,
222 struct pci_access
*pacc
;
227 pacc
->method
= PCI_ACCESS_I386_TYPE1
;
232 for (dev
=pacc
->devices
; dev
; dev
=dev
->next
) {
233 pci_fill_info(dev
, PCI_FILL_IDENT
| PCI_FILL_BASES
|
234 PCI_FILL_SIZES
| PCI_FILL_CLASS
);
235 *name
= pci_lookup_name(pacc
, namebuf
, namebuf_size
,
236 PCI_LOOKUP_DEVICE
, dev
->vendor_id
, dev
->device_id
);
237 if (dev
->vendor_id
!= PCI_VENDOR_ID_INTEL
)
240 if (PCI_DEV_HAS_SUPPORTED_ME(dev
->device_id
)) {
256 static void dump_me_info(void)
259 uint32_t stat
, stat2
;
261 const char *name
= NULL
;
263 if (pci_platform_scan())
266 dev
= pci_me_interface_scan(&name
, namebuf
, sizeof(namebuf
));
269 printf("ME PCI device is hidden\n");
273 dev
= pci_me_interface_scan(&name
, namebuf
, sizeof(namebuf
));
275 printf("Can't find ME PCI device\n");
283 printf("MEI found: [%x:%x] %s\n\n",
284 dev
->vendor_id
, dev
->device_id
, name
);
285 stat
= pci_read_long(dev
, 0x40);
286 printf("ME Status : 0x%x\n", stat
);
287 stat2
= pci_read_long(dev
, 0x48);
288 printf("ME Status 2 : 0x%x\n\n", stat2
);
290 intel_me_status(stat
, stat2
);
292 intel_me_extend_valid(dev
);
296 printf("ME: has a broken implementation on your board with "
299 if (intel_mei_setup(dev
))
301 usleep(ME_COMMAND_DELAY
);
303 usleep(ME_COMMAND_DELAY
);
304 if (mkhi_get_fw_version(&ME_major_ver
, &ME_minor_ver
))
306 usleep(ME_COMMAND_DELAY
);
308 usleep(ME_COMMAND_DELAY
);
309 if (mkhi_get_fwcaps())
311 usleep(ME_COMMAND_DELAY
);
317 static void print_btg_bool_param(const char *name
, u8 state
)
319 printf("%-20s : %s\n", name
, state
? "ON" : "OFF");
322 static void dump_bootguard_info(void)
326 const char *name
= NULL
;
328 if (pci_platform_scan())
331 dev
= pci_me_interface_scan(&name
, namebuf
, sizeof(namebuf
));
334 printf("ME PCI device is hidden\n");
338 dev
= pci_me_interface_scan(&name
, namebuf
, sizeof(namebuf
));
340 printf("Can't find ME PCI device\n");
345 /* ME_major_ver is zero on some platforms (Mac) */
348 (ME_major_ver
== 9 && ME_minor_ver
< 5))) {
349 printf(CGRN
"Your system isn't Boot Guard ready.\n"
350 "You can flash other firmware!\n" RESET
);
355 if (pci_read_long(dev
, 0x40) & 0x10)
356 printf(CYEL
"Your southbridge configuration is insecure!!\n"
357 "Boot Guard keys can be overwritten or wiped, or you are "
358 "in developer mode.\n"
364 u8 nem_enabled
: 1; /* [ 0.. 0] */
365 u8 tpm_type
: 2; /* [ 2.. 1] */
366 u8 tpm_success
: 1; /* [ 3.. 3] */
367 u8 facb_fpf
: 1; /* [ 4.. 4] */
368 u8 measured_boot
: 1; /* [ 5.. 5] */
369 u8 verified_boot
: 1; /* [ 6.. 6] */
370 u8 module_revoked
: 1; /* [ 7.. 7] */
372 u8 btg_capability
: 1; /* [32..32] */
378 if (msr_bootguard(&btg
.raw
) < 0) {
379 printf("Could not read the BOOTGUARD_SACM_INFO MSR.\n");
383 printf("Boot Guard MSR Output : 0x%" PRIx64
"\n", btg
.raw
);
385 if (!btg
.btg_capability
) {
386 printf(CGRN
"Your system isn't Boot Guard ready.\n"
387 "You can flash other firmware!\n" RESET
);
391 print_btg_bool_param("Measured boot", btg
.measured_boot
);
392 print_btg_bool_param("Verified boot", btg
.verified_boot
);
393 print_btg_bool_param("FACB in FPFs", btg
.facb_fpf
);
394 print_btg_bool_param("Module revoked", btg
.module_revoked
);
395 if (btg
.measured_boot
) {
396 const char *const tpm_type_strs
[] = {
402 printf("%-20s : %s\n", "TPM type", tpm_type_strs
[btg
.tpm_type
]);
403 print_btg_bool_param("TPM success", btg
.tpm_success
);
405 if (btg
.verified_boot
) {
406 print_btg_bool_param("NEM enabled", btg
.nem_enabled
);
408 printf(CRED
"Verified boot is enabled and ACM has enabled "
409 "Cache-As-RAM.\nYou can't flash other firmware!\n" RESET
);
411 printf(CYEL
"Verified boot is enabled, but ACM did not enable "
412 "Cache-As-RAM.\nIt might be possible to flash other firmware.\n"
415 printf(CGRN
"Your system is Boot Guard ready but verified boot is disabled.\n"
416 "You can flash other firmware!\n" RESET
);
420 static void print_version(void)
422 printf("intelmetool v%s -- ", INTELMETOOL_VERSION
);
423 printf("Copyright (C) 2015 Damien Zammit\n");
424 printf("Copyright (C) 2017 Philipp Deppenwiese\n");
425 printf("Copyright (C) 2017 Patrick Rudolph\n\n");
426 printf(GPLV2COPYRIGHT
);
429 static void print_usage(const char *name
)
431 printf("usage: %s [-vh?smdb]\n", name
);
433 " -v | --version: print the version\n"
434 " -h | --help: print this help\n\n"
435 " -d | --debug: enable debug output\n"
436 " -m | --me dump all me information on console\n"
437 " -b | --bootguard dump bootguard state of the platform\n"
442 int main(int argc
, char *argv
[])
444 int opt
, option_index
= 0;
445 unsigned cmd_exec
= 0;
447 static struct option long_options
[] = {
448 {"version", 0, 0, 'v'},
451 {"bootguard", 0, 0, 'b'},
452 {"debug", 0, 0, 'd'},
456 while ((opt
= getopt_long(argc
, argv
, "vh?smdb",
457 long_options
, &option_index
)) != EOF
) {
463 case 's': /* Legacy fallthrough */
476 print_usage(argv
[0]);
483 print_usage(argv
[0]);
485 #if defined(__FreeBSD__)
486 if (open("/dev/io", O_RDWR
) < 0) {
488 #elif defined(__NetBSD__)
493 if (x86_64_iopl(3)) {
500 printf("You need to be root.\n");
505 fd_mem
= open("/dev/mem", O_RDWR
);
507 perror("Can not open /dev/mem. Do you have disabled "
512 if (!isCPUGenuineIntel()) {
513 perror("Error CPU is not from Intel.");
521 dump_bootguard_info();