2021-09-11 12:20:47 +03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-02 10:17:55 +03:00
|
|
|
#include <AK/Platform.h>
|
|
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
|
|
|
|
# include <Kernel/Arch/x86/common/I8042Reboot.h>
|
|
|
|
|
# include <Kernel/Arch/x86/common/Shutdown.h>
|
|
|
|
|
#endif
|
2021-09-11 12:20:47 +03:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
2022-04-22 09:44:31 +03:00
|
|
|
#include <Kernel/FileSystem/SysFS/Subsystems/Firmware/PowerStateSwitch.h>
|
2021-09-11 12:20:47 +03:00
|
|
|
#include <Kernel/Firmware/ACPI/Parser.h>
|
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
#include <Kernel/Sections.h>
|
|
|
|
|
#include <Kernel/TTY/ConsoleManagement.h>
|
|
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
|
|
mode_t PowerStateSwitchNode::permissions() const
|
|
|
|
|
{
|
|
|
|
|
return S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
UNMAP_AFTER_INIT NonnullLockRefPtr<PowerStateSwitchNode> PowerStateSwitchNode::must_create(FirmwareSysFSDirectory& firmware_directory)
|
2021-09-11 12:20:47 +03:00
|
|
|
{
|
2022-08-19 20:53:40 +02:00
|
|
|
return adopt_lock_ref_if_nonnull(new (nothrow) PowerStateSwitchNode(firmware_directory)).release_nonnull();
|
2021-09-11 12:20:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT PowerStateSwitchNode::PowerStateSwitchNode(FirmwareSysFSDirectory&)
|
2021-12-12 16:33:08 +02:00
|
|
|
: SysFSComponent()
|
2021-09-11 12:20:47 +03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> PowerStateSwitchNode::truncate(u64 size)
|
2021-09-17 11:52:51 +03:00
|
|
|
{
|
|
|
|
|
// Note: This node doesn't store any useful data anyway, so we can safely
|
|
|
|
|
// truncate this to zero (essentially ignoring the request without failing).
|
|
|
|
|
if (size != 0)
|
|
|
|
|
return EPERM;
|
2021-11-08 00:51:39 +01:00
|
|
|
return {};
|
2021-09-17 11:52:51 +03:00
|
|
|
}
|
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> PowerStateSwitchNode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& data, OpenFileDescription*)
|
2021-09-11 12:20:47 +03:00
|
|
|
{
|
|
|
|
|
if (Checked<off_t>::addition_would_overflow(offset, count))
|
|
|
|
|
return EOVERFLOW;
|
|
|
|
|
if (offset > 0)
|
|
|
|
|
return EINVAL;
|
|
|
|
|
if (count > 1)
|
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
|
|
char buf[1];
|
|
|
|
|
TRY(data.read(buf, 1));
|
|
|
|
|
switch (buf[0]) {
|
|
|
|
|
case '0':
|
|
|
|
|
return EINVAL;
|
|
|
|
|
case '1':
|
|
|
|
|
reboot();
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
case '2':
|
|
|
|
|
poweroff();
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
default:
|
|
|
|
|
return EINVAL;
|
|
|
|
|
}
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerStateSwitchNode::reboot()
|
|
|
|
|
{
|
|
|
|
|
MutexLocker locker(Process::current().big_lock());
|
|
|
|
|
|
|
|
|
|
dbgln("acquiring FS locks...");
|
|
|
|
|
FileSystem::lock_all();
|
|
|
|
|
dbgln("syncing mounted filesystems...");
|
|
|
|
|
FileSystem::sync();
|
|
|
|
|
dbgln("attempting reboot via ACPI");
|
|
|
|
|
if (ACPI::is_enabled())
|
|
|
|
|
ACPI::Parser::the()->try_acpi_reboot();
|
2022-09-02 10:17:55 +03:00
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
|
|
|
|
i8042_reboot();
|
|
|
|
|
#endif
|
2021-09-11 12:20:47 +03:00
|
|
|
dbgln("reboot attempts failed, applications will stop responding.");
|
|
|
|
|
dmesgln("Reboot can't be completed. It's safe to turn off the computer!");
|
|
|
|
|
Processor::halt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerStateSwitchNode::poweroff()
|
|
|
|
|
{
|
|
|
|
|
MutexLocker locker(Process::current().big_lock());
|
|
|
|
|
|
|
|
|
|
ConsoleManagement::the().switch_to_debug();
|
|
|
|
|
|
|
|
|
|
dbgln("acquiring FS locks...");
|
|
|
|
|
FileSystem::lock_all();
|
|
|
|
|
dbgln("syncing mounted filesystems...");
|
|
|
|
|
FileSystem::sync();
|
|
|
|
|
dbgln("attempting system shutdown...");
|
2022-09-02 10:17:55 +03:00
|
|
|
#if ARCH(I386) || ARCH(X86_64)
|
|
|
|
|
qemu_shutdown();
|
|
|
|
|
virtualbox_shutdown();
|
|
|
|
|
#endif
|
2021-09-11 12:20:47 +03:00
|
|
|
dbgln("shutdown attempts failed, applications will stop responding.");
|
|
|
|
|
dmesgln("Shutdown can't be completed. It's safe to turn off the computer!");
|
|
|
|
|
Processor::halt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|