Skip to content

Add support for Lenovo Legion Slim 5 16AHP9 (NRCN) and fix legiond segfault#377

Merged
st0nie merged 2 commits intojohnfanv2:mainfrom
Hashim-K:add-legion-slim-5-16ahp9-nrcn
Dec 19, 2025
Merged

Add support for Lenovo Legion Slim 5 16AHP9 (NRCN) and fix legiond segfault#377
st0nie merged 2 commits intojohnfanv2:mainfrom
Hashim-K:add-legion-slim-5-16ahp9-nrcn

Conversation

@Hashim-K
Copy link
Contributor

Summary

This PR adds support for the Lenovo Legion Slim 5 16AHP9 (BIOS: NRCN) and fixes a critical segmentation fault in the legiond daemon caused by NULL pointer dereference when reading power supply status.

Changes

1. Add Legion Slim 5 16AHP9 Support

Commit: 46939a7
File: kernel_module/legion-laptop.c

Adds model_nrcn configuration for Legion Slim 5 16AHP9 (Model 83DH).

Hardware Details:

  • Model: 83DH
  • BIOS: NRCN17WW
  • CPU: AMD Ryzen 7 8845HS
  • EC Chip ID: 0x8227
  • EC Version: 0x2a4

Features Verified Working:

  • ✅ Temperature monitoring (CPU, GPU via WMI3)
  • ✅ Fan speed monitoring (1900-3500+ RPM range observed)
  • ✅ Automatic BIOS fan control
  • ✅ Power mode switching (quiet/balanced/performance/balanced-performance)
  • ✅ Toggle features (G-Sync, Overdrive, keyboard backlight)
  • ✅ Fn+Q hardware button integration
  • ✅ Platform profile support

Known Hardware Limitations:

  • Manual fan control not supported (BIOS-locked by design on this model)
  • Fan curve reading/writing not available
  • These are expected hardware behaviors, not software issues

Closes: #218

2. Fix legiond Segmentation Fault

Commit: 66c1ba3
File: extra/service/legiond/modules/powerstate.c

Fixed NULL pointer dereference in get_powerstate() causing immediate daemon crash on startup.

Problem:
The function called fscanf() on NULL file pointers when AC power supply paths failed to open. This occurred when both /sys/class/power_supply/ADP0/online and /sys/class/power_supply/ACAD/online don't exist on the system.

Stack trace of crash:
Process legiond (PID 101198) dumped core
#0 0x00007fad7a4423fb __vfscanf_internal (libc.so.6 + 0x423fb)
#1 0x00007fad7a435d3f __isoc99_fscanf (libc.so.6 + 0x35d3f)
#2 0x0000000000401f86 get_powerstate (/usr/bin/legiond + 0x1f86)
#3 0x00000000004025eb timer_handler (/usr/bin/legiond + 0x25eb)

Solution:
// Added NULL check after fopen attempts
fp = fopen(ac_path, "r");
if (fp == NULL)
fp = fopen(ac_path_alt, "r");

+if (fp == NULL) {

printf("failed to open AC power status file\n");

return P_ERROR_AC;
+}

// Similar checks added for platform_profile path

- Added model_nrcn configuration for Legion Slim 5 16AHP9
- Model: 83DH with BIOS NRCN17WW
- CPU: AMD Ryzen 7 8845HS
- EC Chip ID: 0x8227, Version: 0x2a4
- Tested and confirmed working:
  * Temperature monitoring (CPU, GPU via WMI3)
  * Fan speed monitoring (1900-3500+ RPM observed)
  * Automatic BIOS fan control
  * Power mode switching
  * All toggle features (G-Sync, Overdrive, keyboard backlight)
- Known limitations:
  * Manual fan control not supported (BIOS-locked by design)
  * Fan curve reading/writing not available
  * This is expected hardware behavior for this model

Closes johnfanv2#218
- Added NULL check after fopen() for AC power status paths
- Added NULL check after fopen() for power profile path
- Prevents segfault when sysfs files don't exist or can't be opened
- Fixes crash on startup when timer_handler calls get_powerstate()

The daemon was crashing because fscanf() was being called on NULL
file pointers when both /sys/class/power_supply/ADP0/online and
/sys/class/power_supply/ACAD/online didn't exist.
@st0nie st0nie merged commit 1d9450f into johnfanv2:main Dec 19, 2025
4 of 5 checks passed
@awsms
Copy link

awsms commented Dec 19, 2025

Hi Hashim, and thanks for the patch!
Are you sure it’s a BIOS limitation regarding setting custom RPMs for the fans?
Could we reach out to Lenovo about it, or is this an intentional restriction that they won’t change?
I don’t know if this is also restricted on Windows, but for reference, I’ve had my fan replaced on this laptop (16AHP9) after a failure, so maybe it’s a known defect on their side?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants