poweroff.c 884 B

12345678910111213141516171819202122232425262728293031
  1. /* Poweroff command: ACPI S5 on real hardware, Bochs/QEMU ports as fallback.
  2. *
  3. * The ACPI path parses the FADT and the DSDT \_S5 package (acpi.c) and
  4. * sets SLP_TYP|SLP_EN in the PM1a/PM1b control blocks. The Bochs/QEMU
  5. * ports are kept for emulators without usable ACPI tables. If no path
  6. * works the command returns instead of halting, so the desktop survives a
  7. * failed poweroff and the netlog/kworker can still report the reason. */
  8. #include "acpi.h"
  9. #include "pci.h"
  10. #include "serial.h"
  11. #include "timer.h"
  12. static void wait_ms(unsigned int ms)
  13. {
  14. unsigned int t0 = get_ticks_ms();
  15. while (get_ticks_ms() - t0 < ms)
  16. __asm__ volatile ("hlt");
  17. }
  18. void program_poweroff_main(const char *arg)
  19. {
  20. (void)arg;
  21. if (acpi_poweroff() == 0)
  22. wait_ms(1000);
  23. port_outw(0x604, 0x2000);
  24. port_outw(0xB004, 0x2000);
  25. wait_ms(200);
  26. serial_puts(SERIAL_COM1, "poweroff: failed\n");
  27. }