Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1399 lines
40 KiB

  1. /*****************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1988-1991 **/
  4. /*****************************************************************/
  5. #include <stdio.h>
  6. #include <process.h>
  7. #include <setjmp.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include <windows.h>
  14. PROCESSOR_POWER_POLICY PAc, PDc, PCurrent;
  15. SYSTEM_POWER_POLICY Ac, Dc, Current;
  16. SYSTEM_POWER_CAPABILITIES Cap;
  17. SYSTEM_POWER_STATUS PS;
  18. ADMINISTRATOR_POWER_POLICY Admin;
  19. SYSTEM_BATTERY_STATE Batt;
  20. typedef
  21. VOID
  22. (*PCUR_PRINT)(
  23. IN PVOID Context
  24. );
  25. typedef
  26. VOID
  27. (*PCUR_ASSIGN)(
  28. PUCHAR Variable,
  29. PUCHAR Value
  30. );
  31. ULONG CurType;
  32. POWER_INFORMATION_LEVEL CurInfo;
  33. PVOID CurContext;
  34. ULONG CurContextSize;
  35. PCUR_PRINT CurPrint;
  36. PCUR_ASSIGN CurAssign;
  37. PUCHAR CurDesc;
  38. PUCHAR CurValue;
  39. BOOLEAN ItemUpdated;
  40. BOOLEAN CurValueIsNumeric;
  41. BOOLEAN Verbose1;
  42. BOOLEAN Verbose2;
  43. PUCHAR szBool[] = { "FALSE", "TRUE" };
  44. typedef struct {
  45. ULONG Flags;
  46. PUCHAR String;
  47. } DEFBITS, *PDEFBITS;
  48. DEFBITS ActFlags[] = {
  49. POWER_ACTION_QUERY_ALLOWED, "QueryApps",
  50. POWER_ACTION_UI_ALLOWED, "UIAllowed",
  51. POWER_ACTION_OVERRIDE_APPS, "OverrideApps",
  52. POWER_ACTION_DISABLE_WAKES, "DisableWakes",
  53. POWER_ACTION_CRITICAL, "Critical",
  54. 0, NULL
  55. };
  56. DEFBITS PsBatteryFlagBits[] = {
  57. BATTERY_FLAG_HIGH, "high",
  58. BATTERY_FLAG_LOW, "low",
  59. BATTERY_FLAG_CRITICAL, "critical",
  60. BATTERY_FLAG_CHARGING, "charing",
  61. 0, NULL
  62. };
  63. PUCHAR
  64. ActionS(
  65. IN POWER_ACTION Act
  66. )
  67. {
  68. static char line[50];
  69. PCHAR p;
  70. switch (Act) {
  71. case PowerActionNone: p = "None"; break;
  72. case PowerActionSleep: p = "Sleep"; break;
  73. case PowerActionHibernate: p = "Hibernate"; break;
  74. case PowerActionShutdown: p = "Shutdown"; break;
  75. case PowerActionShutdownReset: p = "ShutdownReset"; break;
  76. case PowerActionShutdownOff: p = "ShutdownOff"; break;
  77. default:
  78. sprintf(line, "Unknown action %x", Act);
  79. p = line;
  80. break;
  81. }
  82. return p;
  83. }
  84. PUCHAR
  85. SysPower(
  86. IN SYSTEM_POWER_STATE State
  87. )
  88. {
  89. static char line[50];
  90. PCHAR p;
  91. switch (State) {
  92. case PowerSystemUnspecified: p = "Unspecified"; break;
  93. case PowerSystemWorking: p = "Working"; break;
  94. case PowerSystemSleeping1: p = "S1"; break;
  95. case PowerSystemSleeping2: p = "S2"; break;
  96. case PowerSystemSleeping3: p = "S3"; break;
  97. case PowerSystemHibernate: p = "S4 - hibernate"; break;
  98. case PowerSystemShutdown: p = "Shutdown"; break;
  99. default:
  100. sprintf(line, "Unknown power state %x", State);
  101. p = line;
  102. break;
  103. }
  104. return p;
  105. }
  106. PUCHAR
  107. DynamicThrottle(
  108. IN UCHAR Throttle
  109. )
  110. {
  111. static char line[50];
  112. PCHAR p;
  113. switch (Throttle) {
  114. case PO_THROTTLE_NONE: p = "None"; break;
  115. case PO_THROTTLE_CONSTANT: p = "Constant"; break;
  116. case PO_THROTTLE_DEGRADE: p = "Degrade"; break;
  117. case PO_THROTTLE_ADAPTIVE: p = "Adaptive"; break;
  118. default:
  119. sprintf(line,"Unknown Dynamic Throttle state %x", Throttle);
  120. p = line;
  121. break;
  122. }
  123. return p;
  124. }
  125. PUCHAR
  126. MicroSeconds(
  127. IN ULONG Time
  128. )
  129. {
  130. static char line[256];
  131. PCHAR p;
  132. ULONG MicroSeconds;
  133. ULONG MilliSeconds;
  134. ULONG Seconds;
  135. ULONG Minutes;
  136. ULONG Hours;
  137. ULONG Days;
  138. MicroSeconds = Time % 1000;
  139. MilliSeconds = Time / 1000;
  140. Seconds = MilliSeconds / 1000;
  141. MilliSeconds = MilliSeconds % 1000;
  142. Minutes = Seconds / 60;
  143. Seconds = Seconds % 60;
  144. Hours = Minutes / 60;
  145. Minutes = Minutes % 60;
  146. Days = Hours / 24;
  147. Hours = Hours % 24;
  148. if (Hours) {
  149. sprintf(
  150. line,
  151. "%d [%2d:%2d %ds %dms %dus]",
  152. Time,
  153. Hours,
  154. Minutes,
  155. Seconds,
  156. MilliSeconds,
  157. MicroSeconds
  158. );
  159. } else if (Minutes) {
  160. sprintf(
  161. line,
  162. "%d [0:%2d %ds %dms %dus]",
  163. Time,
  164. Minutes,
  165. Seconds,
  166. MilliSeconds,
  167. MicroSeconds
  168. );
  169. } else if (Seconds) {
  170. sprintf(
  171. line,
  172. "%d [%ds %dms %dus]",
  173. Time,
  174. Seconds,
  175. MilliSeconds,
  176. MicroSeconds
  177. );
  178. } else if (MilliSeconds) {
  179. sprintf(
  180. line,
  181. "%d [%dms %dus]",
  182. Time,
  183. MilliSeconds,
  184. MicroSeconds
  185. );
  186. } else {
  187. sprintf(
  188. line,
  189. "%d [%dus]",
  190. Time,
  191. MicroSeconds
  192. );
  193. }
  194. p = line;
  195. return p;
  196. }
  197. VOID
  198. GetBits (
  199. IN PUCHAR Dest,
  200. IN ULONG Flags,
  201. IN PDEFBITS DefBits
  202. )
  203. {
  204. UCHAR c;
  205. ULONG i;
  206. c = 0;
  207. *Dest = 0;
  208. for (i=0; DefBits[i].Flags; i++) {
  209. if (Flags & DefBits[i].Flags) {
  210. if (c) {
  211. *Dest = c;
  212. Dest += 1;
  213. }
  214. strcpy(Dest, DefBits[i].String);
  215. Dest += strlen(DefBits[i].String);
  216. c = '|';
  217. }
  218. }
  219. }
  220. VOID
  221. PrintPowerStatus (
  222. VOID
  223. )
  224. {
  225. ULONG i;
  226. PUCHAR Ls;
  227. UCHAR Bf[200];
  228. switch (PS.ACLineStatus) {
  229. case AC_LINE_OFFLINE: Ls = "off line"; break;
  230. case AC_LINE_ONLINE: Ls = "on line"; break;
  231. case AC_LINE_BACKUP_POWER: Ls = "backup power"; break;
  232. case AC_LINE_UNKNOWN: Ls = "unknown"; break;
  233. default: Ls = "**invalid**"; break;
  234. }
  235. strcpy (Bf, "unkown");
  236. if (PS.BatteryFlag != BATTERY_FLAG_UNKNOWN) {
  237. GetBits(Bf, PS.BatteryFlag, PsBatteryFlagBits);
  238. }
  239. printf ("Win32 System power status\n"
  240. "AC line status..........: %s\n", Ls);
  241. printf ("Battery flag............: %s\n", Bf);
  242. printf ("Battery life percent....: %d\n", PS.BatteryLifePercent);
  243. printf ("Battery full life time..: %d\n\n", PS.BatteryFullLifeTime);
  244. }
  245. VOID
  246. PrintBattStatus (
  247. VOID
  248. )
  249. {
  250. ULONG i;
  251. PUCHAR Ls;
  252. UCHAR Bf[200];
  253. printf ("AC on line..............: %s\n", szBool[Batt.AcOnLine]);
  254. printf ("Battery present ........: %s\n", szBool[Batt.BatteryPresent]);
  255. printf ("Charging................: %s\n", szBool[Batt.Charging]);
  256. printf ("Discharging.............: %s\n", szBool[Batt.Discharging]);
  257. printf ("Max Capacity............: %d\n", Batt.MaxCapacity);
  258. printf ("Remaining Capacity......: %d", Batt.RemainingCapacity);
  259. if (Batt.MaxCapacity) {
  260. printf (" %d%%\n", Batt.RemainingCapacity * 100 / Batt.MaxCapacity);
  261. } else {
  262. printf (" (divide by zero)%%\n");
  263. }
  264. printf ("Rate....................: %d\n", Batt.Rate);
  265. printf ("Estimated time..........: %d\n", Batt.EstimatedTime);
  266. printf ("Default alert 1 & 2.....: %d %d\n\n",Batt.DefaultAlert1, Batt.DefaultAlert2);
  267. }
  268. BOOLEAN
  269. streql (
  270. PUCHAR p1,
  271. PUCHAR p2
  272. )
  273. {
  274. return strcmp(p1, p2) == 0;
  275. }
  276. PUCHAR
  277. _strtok (
  278. PUCHAR Start
  279. )
  280. {
  281. static PUCHAR Location;
  282. PUCHAR p;
  283. if (Start) {
  284. Location = Start;
  285. }
  286. for (p = Location; *p && *p <'a' && *p >'z'; p++) ;
  287. if (!*p) {
  288. return NULL;
  289. }
  290. Start = p;
  291. for (; *p && *p >= 'a' && *p <= 'z'; p++) ;
  292. if (*p) {
  293. *p++ = 0;
  294. }
  295. Location = p;
  296. return Start;
  297. }
  298. VOID
  299. PrintCap (
  300. PVOID Context
  301. )
  302. {
  303. ULONG i;
  304. puts ("System power capabilties");
  305. printf ("Power Button Present....: %s\n", szBool[Cap.PowerButtonPresent]);
  306. printf ("Sleep Button Present....: %s\n", szBool[Cap.SleepButtonPresent]);
  307. printf ("Lid Present.............: %s\n", szBool[Cap.LidPresent]);
  308. printf ("System states supported.: %s%s%s%s%s\n",
  309. Cap.SystemS1 ? "S1 " : "",
  310. Cap.SystemS2 ? "S2 " : "",
  311. Cap.SystemS3 ? "S3 " : "",
  312. Cap.SystemS4 ? "S4 " : "",
  313. Cap.SystemS5 ? "S5 " : ""
  314. );
  315. printf ("Hiber file reserved.....: %s\n", szBool[Cap.HiberFilePresent]);
  316. printf ("Thermal control.........: %s\n", szBool[Cap.ThermalControl]);
  317. printf ("CPU Throttle control....: %s\n", szBool[Cap.ProcessorThrottle]);
  318. printf ("Processor min throttle..: %d\n", Cap.ProcessorMinThrottle);
  319. printf ("Processor max throttle..: %d\n", Cap.ProcessorMaxThrottle);
  320. printf ("Some disk will spindown.: %s\n", szBool[Cap.DiskSpinDown]);
  321. printf ("System batteries present: %s %s\n",
  322. szBool[Cap.SystemBatteriesPresent],
  323. Cap.BatteriesAreShortTerm ? "- short term" : ""
  324. );
  325. printf ("System batteries scale..: ");
  326. for (i=0; i<3; i++) {
  327. printf ("(G:%d C:%d) ",
  328. Cap.BatteryScale[i].Granularity,
  329. Cap.BatteryScale[i].Capacity
  330. );
  331. }
  332. printf ("\n"
  333. "Ac on line wake ability.: %s\n", SysPower(Cap.AcOnLineWake));
  334. printf ("Lid wake ability........: %s\n", SysPower(Cap.SoftLidWake));
  335. printf ("RTC wake ability........: %s\n", SysPower(Cap.RtcWake));
  336. printf ("Min device wake.........: %s\n", SysPower(Cap.MinDeviceWakeState));
  337. printf ("Default low latency wake: %s\n\n", SysPower(Cap.DefaultLowLatencyWake));
  338. }
  339. PUCHAR
  340. Action (
  341. IN PBOOLEAN CapFlag,
  342. IN PPOWER_ACTION_POLICY Act
  343. )
  344. {
  345. static UCHAR text[200];
  346. PUCHAR p;
  347. UCHAR c;
  348. ULONG i;
  349. p = text;
  350. if (CapFlag && !*CapFlag) {
  351. strcpy(p, "Disabled ");
  352. p += strlen("Disabled ");
  353. }
  354. strcpy(p, ActionS(Act->Action));
  355. p+=strlen(ActionS(Act->Action));
  356. if (Act->Action != PowerActionNone && Act->Flags) {
  357. c = '(';
  358. for (i=0; ActFlags[i].Flags; i++) {
  359. if (Act->Flags & ActFlags[i].Flags) {
  360. p += sprintf (p, "%c%s", c, ActFlags[i].String);
  361. c = '|';
  362. }
  363. }
  364. p += sprintf (p, ")");
  365. }
  366. if (Act->EventCode) {
  367. p += sprintf (p, "-Code=%x", Act->EventCode);
  368. }
  369. return text;
  370. }
  371. VOID
  372. SetAction (
  373. IN PPOWER_ACTION_POLICY Action
  374. )
  375. {
  376. PUCHAR p;
  377. POWER_ACTION Act;
  378. ULONG Flags;
  379. Flags = 0;
  380. p = _strtok (CurValue);
  381. if (streql(p, "none")) { Act = PowerActionNone;
  382. } else if (streql(p, "sleep")) { Act = PowerActionSleep;
  383. } else if (streql(p, "hiber")) { Act = PowerActionHibernate;
  384. } else if (streql(p, "shutdown")) { Act = PowerActionShutdown;
  385. } else if (streql(p, "shutdownreset")) { Act = PowerActionShutdownReset;
  386. } else if (streql(p, "shutdownoff")) { Act = PowerActionShutdownOff;
  387. } else {
  388. printf ("Unknown power action '%s'.\n", p);
  389. puts ("use: doze, sleep, shutdown, shutdownreset, shutdownoff");
  390. exit (1);
  391. }
  392. while (p = _strtok(NULL)) {
  393. if (streql(p, "qapp")) { Flags |= POWER_ACTION_QUERY_ALLOWED;
  394. } else if (streql(p, "ui")) { Flags |= POWER_ACTION_UI_ALLOWED;
  395. } else if (streql(p, "override")) { Flags |= POWER_ACTION_OVERRIDE_APPS;
  396. } else if (streql(p, "disablewake")) { Flags |= POWER_ACTION_DISABLE_WAKES;
  397. } else if (streql(p, "critical")) { Flags |= POWER_ACTION_CRITICAL;
  398. } else {
  399. printf ("Unknown power action '%s'.\n", p);
  400. puts ("use: qapp, io, override, disablewake, critical");
  401. exit (1);
  402. }
  403. }
  404. Action->Action = Act;
  405. Action->Flags = Flags;
  406. }
  407. VOID
  408. SetSysPower (
  409. IN PSYSTEM_POWER_STATE SysPower
  410. )
  411. {
  412. PUCHAR p;
  413. p = CurValue;
  414. if (streql(p, "s0")) { *SysPower = PowerSystemWorking;
  415. } else if (streql(p, "s1")) { *SysPower = PowerSystemSleeping1;
  416. } else if (streql(p, "s2")) { *SysPower = PowerSystemSleeping2;
  417. } else if (streql(p, "s3")) { *SysPower = PowerSystemSleeping3;
  418. } else if (streql(p, "s4")) { *SysPower = PowerSystemHibernate;
  419. } else {
  420. printf ("Unknown system power state '%s'. Use S0,S1,S2,S3 or S4\n", p);
  421. exit (1);
  422. }
  423. }
  424. VOID
  425. SetPolicyCount(
  426. IN PULONG Variable
  427. )
  428. {
  429. ULONG local;
  430. if (!CurValueIsNumeric) {
  431. printf("'%s' is not numeric\n", CurValue);
  432. exit(1);
  433. }
  434. local = atol(CurValue);
  435. if (local > 3) {
  436. puts("PolicyCount can only be in the range 0 to 3\n");
  437. exit(1);
  438. }
  439. *Variable = local;
  440. }
  441. VOID
  442. SetDynamicThrottle(
  443. IN PUCHAR Variable
  444. )
  445. {
  446. ULONG local;
  447. PUCHAR p;
  448. p = CurValue;
  449. if (streql(p,"none")) { *Variable = PO_THROTTLE_NONE;
  450. } else if (streql(p,"constant")) { *Variable = PO_THROTTLE_CONSTANT;
  451. } else if (streql(p,"degrade")) { *Variable = PO_THROTTLE_DEGRADE;
  452. } else if (streql(p,"degraded")) { *Variable = PO_THROTTLE_DEGRADE;
  453. } else if (streql(p,"adaptive")) { *Variable = PO_THROTTLE_ADAPTIVE;
  454. } else {
  455. printf("Unknown Dynamic Throttle state %s\n", CurValue);
  456. exit(1);
  457. }
  458. }
  459. VOID
  460. SetCStates(
  461. IN PPROCESSOR_POWER_POLICY Pol
  462. )
  463. {
  464. PUCHAR p;
  465. p = CurValue;
  466. if (streql(p,"true")) {
  467. Pol->DisableCStates = 1;
  468. } else {
  469. Pol->DisableCStates = 0;
  470. }
  471. }
  472. VOID
  473. SetPercentage (
  474. IN PUCHAR Variable
  475. )
  476. {
  477. PUCHAR p;
  478. if (!CurValueIsNumeric) {
  479. for (p = CurValue; *p; p++) {
  480. if (*p == '%') {
  481. *p = '\0';
  482. }
  483. }
  484. CurValueIsNumeric = TRUE;
  485. for (p = CurValue; *p; p++) {
  486. if (*p < '0' || *p > '9') {
  487. CurValueIsNumeric = FALSE;
  488. }
  489. }
  490. if (!CurValueIsNumeric) {
  491. printf ("'%s' is not numeric\n", CurValue);
  492. exit (1);
  493. }
  494. }
  495. *Variable = (UCHAR) atol(CurValue);
  496. }
  497. VOID
  498. SetValue (
  499. IN PULONG Variable
  500. )
  501. {
  502. if (!CurValueIsNumeric) {
  503. printf ("'%s' is not numeric\n", CurValue);
  504. exit (1);
  505. }
  506. *Variable = atol(CurValue);
  507. }
  508. VOID
  509. SetBool (
  510. IN PBOOLEAN Variable
  511. )
  512. {
  513. BOOLEAN State;
  514. State = 99;
  515. if (CurValueIsNumeric) {
  516. State = (BOOLEAN)atol(CurValue);
  517. } else {
  518. if (streql(CurValue, "true")) { State = TRUE;
  519. } else if (streql(CurValue, "false")) { State = FALSE;
  520. }
  521. }
  522. if (State != FALSE && State != TRUE) {
  523. printf ("'%s' is not boolean\n", CurValue);
  524. exit (1);
  525. }
  526. *Variable = State;
  527. }
  528. VOID
  529. SetField(
  530. IN PPROCESSOR_POWER_POLICY Pol,
  531. IN ULONG Index,
  532. IN PUCHAR What
  533. )
  534. {
  535. BOOLEAN State;
  536. State = 99;
  537. if (CurValueIsNumeric) {
  538. State = (BOOLEAN)atol(CurValue);
  539. } else {
  540. if (streql(CurValue, "true")) { State = TRUE;
  541. } else if (streql(CurValue, "false")) { State = FALSE;
  542. }
  543. }
  544. if (State != FALSE && State != TRUE) {
  545. printf ("'%s' is not boolean\n", CurValue);
  546. exit (1);
  547. }
  548. if (streql(What,"allowpromotion")) { Pol->Policy[Index].AllowPromotion = State;
  549. } else if (streql(What,"allowdemotion")) { Pol->Policy[Index].AllowDemotion = State;
  550. }
  551. }
  552. VOID
  553. PrintPol (
  554. IN PSYSTEM_POWER_POLICY Pol
  555. )
  556. {
  557. BOOLEAN AnySleep;
  558. ULONG i;
  559. UCHAR text[200];
  560. PUCHAR p;
  561. if (Pol->Revision != 1) {
  562. puts ("** revision not 1**");
  563. }
  564. AnySleep = Cap.SystemS1 || Cap.SystemS2 || Cap.SystemS3 || Cap.SystemS4;
  565. printf ("Power button.........: %s\n", Action(&Cap.PowerButtonPresent, &Pol->PowerButton));
  566. printf ("Sleep button.........: %s\n", Action(&Cap.SleepButtonPresent, &Pol->SleepButton));
  567. printf ("Lid close............: %s\n", Action(&Cap.LidPresent, &Pol->LidClose));
  568. printf ("Lid open wake........: %s\n", SysPower(Pol->LidOpenWake));
  569. printf ("Idle.................: %s\n", Action(&AnySleep, &Pol->Idle));
  570. printf ("Settings.............: Timeout=%d, Sensitivity=%d\n",
  571. Pol->IdleTimeout,
  572. Pol->IdleSensitivity
  573. );
  574. printf ("Min sleep state......: %s\n", SysPower(Pol->MinSleep));
  575. printf ("Max sleep state......: %s\n", SysPower(Pol->MaxSleep));
  576. printf ("Reduced latency.sleep: %s\n", SysPower(Pol->ReducedLatencySleep));
  577. printf ("WinLogonFlags........: %x\n", Pol->WinLogonFlags);
  578. printf ("DynamicThrottle......: %s\n", DynamicThrottle(Pol->DynamicThrottle));
  579. printf ("Doze S4 timeout......: %d\n", Pol->DozeS4Timeout);
  580. printf ("Battery broadcast res: %d\n", Pol->BroadcastCapacityResolution);
  581. for (i=0; i < NUM_DISCHARGE_POLICIES; i++) {
  582. printf ("Battery discharge %d..: ", i);
  583. p = "Disabled";
  584. if (!Cap.SystemBatteriesPresent && Pol->DischargePolicy[i].Enable) {
  585. p = "No battery, but enable is set";
  586. }
  587. if (Cap.SystemBatteriesPresent && Pol->DischargePolicy[i].Enable) {
  588. sprintf (text, "%d, %s, Min=%s",
  589. Pol->DischargePolicy[i].BatteryLevel,
  590. Action (NULL, &Pol->DischargePolicy[i].PowerPolicy),
  591. SysPower(Pol->DischargePolicy[i].MinSystemState)
  592. );
  593. p = text;
  594. }
  595. printf ("%s\n", p);
  596. }
  597. printf ("Video timeout.......: %d\n", Pol->VideoTimeout);
  598. printf ("Hard disk timeout....: %d\n", Pol->SpindownTimeout);
  599. printf ("Optimize for power...: %s\n", szBool[Pol->OptimizeForPower]);
  600. printf ("Fan throttle toler...: %d\n", Pol->FanThrottleTolerance);
  601. printf ("Forced throttle......: %d%% %s\n",
  602. Pol->ForcedThrottle,
  603. Pol->ForcedThrottle == 100 ? "(off)" : "(on)"
  604. );
  605. printf ("Min throttle.........: %d%%\n", Pol->MinThrottle);
  606. printf ("Over throttle act....: %s\n", Action (NULL, &Pol->OverThrottled));
  607. printf ("\n");
  608. }
  609. VOID
  610. PrintPPol (
  611. IN PPROCESSOR_POWER_POLICY Pol
  612. )
  613. {
  614. ULONG i;
  615. UCHAR text[200];
  616. PUCHAR p;
  617. if (Pol->Revision != 1) {
  618. puts ("** revision not 1**");
  619. }
  620. printf ("Dynamic Throttle.....: %s\n", DynamicThrottle(Pol->DynamicThrottle) );
  621. printf ("Disable CStates......: %s\n", szBool[Pol->DisableCStates] );
  622. printf ("\n");
  623. for (i = 0; i < Pol->PolicyCount; i++) {
  624. printf ("C%x Processor Power Policy\n", (i+1) );
  625. printf (" Allow Demotion....: %s\n", szBool[Pol->Policy[i].AllowDemotion]);
  626. printf (" Allow Promotion...: %s\n", szBool[Pol->Policy[i].AllowPromotion]);
  627. printf (" Demote Percent....: %d%%\n", Pol->Policy[i].DemotePercent);
  628. printf (" Promote Percent...: %d%%\n", Pol->Policy[i].PromotePercent);
  629. printf (" Demote Limit......: %s\n", MicroSeconds(Pol->Policy[i].DemoteLimit) );
  630. printf (" Promote Limit.....: %s\n", MicroSeconds(Pol->Policy[i].PromoteLimit) );
  631. printf (" Time Check........: %s\n", MicroSeconds(Pol->Policy[i].TimeCheck) );
  632. printf ("\n");
  633. }
  634. printf ("\n");
  635. }
  636. VOID
  637. PrintAdmin (
  638. PVOID Context
  639. )
  640. {
  641. printf ("Min sleep state......: %s\n", SysPower(Admin.MinSleep));
  642. printf ("Max sleep state......: %s\n", SysPower(Admin.MaxSleep));
  643. printf ("Min video timeout....: %d\n", Admin.MinVideoTimeout);
  644. printf ("Max video timeout....: %d\n", Admin.MaxVideoTimeout);
  645. printf ("Min spindown timeout.: %d\n", Admin.MinSpindownTimeout);
  646. printf ("Max spindown timeout.: %d\n", Admin.MaxSpindownTimeout);
  647. }
  648. VOID
  649. AssignSetting (
  650. PUCHAR Variable,
  651. PUCHAR Value
  652. )
  653. {
  654. PSYSTEM_POWER_POLICY Pol;
  655. PUCHAR p;
  656. if (!CurContext) {
  657. puts(
  658. "must select one of:\n"
  659. " ac - the system power policy while on AC\n"
  660. " dc - the system power policy while on DC\n"
  661. " pac - the processor power policy while on AC\n"
  662. " pdc - the processor power policy while on DC\n"
  663. " cap - the system capabilities\n"
  664. " admin - the admin policy\n"
  665. "before assigning settings"
  666. );
  667. exit (1);
  668. }
  669. CurValue = Value;
  670. if (!*CurValue) {
  671. printf ("null settings not allowed\n");
  672. exit (1);
  673. }
  674. CurValueIsNumeric = TRUE;
  675. for (p = Value; *p; p++) {
  676. if (*p < '0' || *p > '9') {
  677. CurValueIsNumeric = FALSE;
  678. }
  679. }
  680. ItemUpdated = TRUE;
  681. CurAssign (Variable, Value);
  682. }
  683. VOID
  684. AssignPolicySetting (
  685. PUCHAR Variable,
  686. PUCHAR Value
  687. )
  688. {
  689. PSYSTEM_POWER_POLICY Pol;
  690. Pol = (PSYSTEM_POWER_POLICY) CurContext;
  691. // set policy
  692. if (streql(Variable, "pbutt")) { SetAction(&Pol->PowerButton);
  693. } else if (streql(Variable, "sbutt")) { SetAction(&Pol->SleepButton);
  694. } else if (streql(Variable, "lidclose")) { SetAction(&Pol->LidClose);
  695. } else if (streql(Variable, "idle")) { SetAction(&Pol->Idle);
  696. } else if (streql(Variable, "idlesense")) { SetValue((PULONG)&Pol->IdleSensitivity);
  697. } else if (streql(Variable, "idletimeout")) { SetValue(&Pol->IdleTimeout);
  698. } else if (streql(Variable, "minsleep")) { SetSysPower(&Pol->MinSleep);
  699. } else if (streql(Variable, "maxsleep")) { SetSysPower(&Pol->MaxSleep);
  700. } else if (streql(Variable, "reducedsleep")) { SetSysPower(&Pol->ReducedLatencySleep);
  701. } else if (streql(Variable, "s4timeout")) { SetValue(&Pol->DozeS4Timeout);
  702. } else if (streql(Variable, "videotimeout")) { SetValue(&Pol->VideoTimeout);
  703. } else if (streql(Variable, "disktimeout")) { SetValue(&Pol->SpindownTimeout);
  704. } else if (streql(Variable, "optpower")) { SetBool(&Pol->OptimizeForPower);
  705. } else if (streql(Variable, "fantol")) { SetValue((PULONG)&Pol->FanThrottleTolerance);
  706. } else if (streql(Variable, "minthrot")) { SetValue((PULONG)&Pol->MinThrottle);
  707. } else if (streql(Variable, "forcethrot")) { SetValue((PULONG)&Pol->ForcedThrottle);
  708. } else if (streql(Variable, "overthrot")) { SetAction(&Pol->OverThrottled);
  709. } else {
  710. puts (
  711. "Variable not:\n"
  712. " pbutt, sbutt, lidclose\n"
  713. " idle, idlesense, idletime\n"
  714. " minsleep, maxsleep, reducedsleep, s4timeout\n"
  715. " videotimeout, disktimeout, optpower\n"
  716. " optpower, fantol, minthrot, forcethrot, overthrot"
  717. );
  718. exit (1);
  719. }
  720. }
  721. VOID
  722. AssignPPolicySetting (
  723. PUCHAR Variable,
  724. PUCHAR Value
  725. )
  726. {
  727. PPROCESSOR_POWER_POLICY Pol;
  728. Pol = (PPROCESSOR_POWER_POLICY) CurContext;
  729. if (streql(Variable,"policycount")) { SetPolicyCount(&(Pol->PolicyCount));
  730. } else if (streql(Variable,"dynamicthrottle")) { SetDynamicThrottle(&(Pol->DynamicThrottle));
  731. } else if (streql(Variable,"disablecstates")) { SetCStates(Pol);
  732. } else if (streql(Variable,"c1allowpromotion")) { SetField(Pol,0,"allowpromotion");
  733. } else if (streql(Variable,"c1allowdemotion")) { SetField(Pol,0,"allowdemotion");
  734. } else if (streql(Variable,"c1demotepercent")) { SetPercentage(&(Pol->Policy[0].DemotePercent));
  735. } else if (streql(Variable,"c1promotepercent")) { SetPercentage(&(Pol->Policy[0].PromotePercent));
  736. } else if (streql(Variable,"c1demotelimit")) { SetValue(&(Pol->Policy[0].DemoteLimit));
  737. } else if (streql(Variable,"c1promotelimit")) { SetValue(&(Pol->Policy[0].PromoteLimit));
  738. } else if (streql(Variable,"c1timecheck")) { SetValue(&(Pol->Policy[0].TimeCheck));
  739. } else if (streql(Variable,"c2allowpromotion")) { SetField(Pol,1,"allowpromotion");
  740. } else if (streql(Variable,"c2allowdemotion")) { SetField(Pol,1,"allowdemotion");
  741. } else if (streql(Variable,"c2demotepercent")) { SetPercentage(&(Pol->Policy[1].DemotePercent));
  742. } else if (streql(Variable,"c2promotepercent")) { SetPercentage(&(Pol->Policy[1].PromotePercent));
  743. } else if (streql(Variable,"c2demotelimit")) { SetValue(&(Pol->Policy[1].DemoteLimit));
  744. } else if (streql(Variable,"c2promotelimit")) { SetValue(&(Pol->Policy[1].PromoteLimit));
  745. } else if (streql(Variable,"c2timecheck")) { SetValue(&(Pol->Policy[1].TimeCheck));
  746. } else if (streql(Variable,"c3allowpromotion")) { SetField(Pol,2,"allowpromotion");
  747. } else if (streql(Variable,"c3allowdemotion")) { SetField(Pol,2,"allowdemotion");
  748. } else if (streql(Variable,"c3demotepercent")) { SetPercentage(&(Pol->Policy[2].DemotePercent));
  749. } else if (streql(Variable,"c3promotepercent")) { SetPercentage(&(Pol->Policy[2].PromotePercent));
  750. } else if (streql(Variable,"c3demotelimit")) { SetValue(&(Pol->Policy[2].DemoteLimit));
  751. } else if (streql(Variable,"c3promotelimit")) { SetValue(&(Pol->Policy[2].PromoteLimit));
  752. } else if (streql(Variable,"c3timecheck")) { SetValue(&(Pol->Policy[2].TimeCheck));
  753. } else {
  754. puts(
  755. "Variable not:\n"
  756. " policycount - number of elements in policy\n"
  757. " dynamicthrottle - which throttle policy to use\n"
  758. " disablecstates - disable cstates if true\n"
  759. " cXallowpromotion - allow promotion from state X\n"
  760. " cXallowdemotion - allow demotion from state X\n"
  761. " cXdemotepercent - set demote percent for state X\n"
  762. " cXpromotepercent - set promote percent for state X\n"
  763. " cXdemotelimit - set demote limit for state X\n"
  764. " cXpromotelimit - set promote limit for state X\n"
  765. " cXtimecheck - set time check interval for state X"
  766. " where X = <1,2,3>\n"
  767. );
  768. exit(1);
  769. }
  770. }
  771. VOID
  772. AssignCurrentSetting(
  773. PUCHAR Variable,
  774. PUCHAR Value
  775. )
  776. {
  777. if (CurInfo == ProcessorPowerPolicyCurrent) {
  778. puts(
  779. "PCurrent is only intended for use in displaying the current processor\n"
  780. "power policy. You must use 'pac' or 'pdc' to actually set policy."
  781. );
  782. } else if (CurInfo == SystemPowerPolicyCurrent) {
  783. puts(
  784. "Current is only intended for use in displaying the current system\n"
  785. "power policy. You muse use 'ac' or 'dc' to actually set policy."
  786. );
  787. } else {
  788. puts(
  789. "Unknown setting."
  790. );
  791. }
  792. exit(1);
  793. }
  794. VOID
  795. AssignAdminSetting (
  796. PUCHAR Variable,
  797. PUCHAR Value
  798. )
  799. {
  800. if (streql(Variable, "minsleep")) { SetSysPower(&Admin.MinSleep);
  801. } else if (streql(Variable, "maxsleep")) { SetSysPower(&Admin.MaxSleep);
  802. } else if (streql(Variable, "minvideo")) { SetValue(&Admin.MinVideoTimeout);
  803. } else if (streql(Variable, "maxvideo")) { SetValue(&Admin.MaxVideoTimeout);
  804. } else if (streql(Variable, "mindisk")) { SetValue(&Admin.MinSpindownTimeout);
  805. } else if (streql(Variable, "maxdisk")) { SetValue(&Admin.MaxSpindownTimeout);
  806. } else {
  807. puts(
  808. "Variable not:\n"
  809. " minsleep, maxsleep\n"
  810. " minvideo, maxvideo, mindisk, maxdisk"
  811. );
  812. exit (1);
  813. }
  814. }
  815. VOID
  816. AssignCapSetting (
  817. PUCHAR Variable,
  818. PUCHAR Value
  819. )
  820. {
  821. // set capability
  822. if (streql(Variable, "pbutt")) { SetBool(&Cap.PowerButtonPresent);
  823. } else if (streql(Variable, "sbutt")) { SetBool(&Cap.SleepButtonPresent);
  824. } else if (streql(Variable, "lid")) { SetBool(&Cap.LidPresent);
  825. } else if (streql(Variable, "s1")) { SetBool(&Cap.SystemS1);
  826. } else if (streql(Variable, "s2")) { SetBool(&Cap.SystemS2);
  827. } else if (streql(Variable, "s3")) { SetBool(&Cap.SystemS3);
  828. } else if (streql(Variable, "s4")) { SetBool(&Cap.SystemS4);
  829. } else if (streql(Variable, "s5")) { SetBool(&Cap.SystemS5);
  830. } else if (streql(Variable, "batt")) { SetBool(&Cap.SystemBatteriesPresent);
  831. } else if (streql(Variable, "shortterm")) { SetBool(&Cap.BatteriesAreShortTerm);
  832. } else if (streql(Variable, "acwake")) { SetSysPower(&Cap.AcOnLineWake);
  833. } else if (streql(Variable, "lidwake")) { SetSysPower(&Cap.SoftLidWake);
  834. } else if (streql(Variable, "rtcwake")) { SetSysPower(&Cap.RtcWake);
  835. } else if (streql(Variable, "lowlat")) { SetSysPower(&Cap.DefaultLowLatencyWake);
  836. } else {
  837. puts (
  838. "Variable not:\n"
  839. " pbutt, sbutt, lid, s1, s2, s3, s4, s5\n"
  840. " batt, shortterm, acwake, lidwake, rtcwak, lowlat"
  841. );
  842. exit (1);
  843. }
  844. }
  845. #define SEL_NONE 0
  846. #define SEL_AC 1
  847. #define SEL_DC 2
  848. #define SEL_CAP 3
  849. #define SEL_ADMIN 4
  850. #define SEL_PAC 5
  851. #define SEL_PDC 6
  852. #define SEL_CURRENT 7
  853. #define SEL_PCURRENT 8
  854. VOID
  855. SelectItem(
  856. IN ULONG Type
  857. )
  858. {
  859. NTSTATUS Status;
  860. if (CurType) {
  861. if (ItemUpdated) {
  862. if (Verbose2) {
  863. printf ("%s being set as:\n", CurDesc);
  864. CurPrint (CurContext);
  865. }
  866. Status = NtPowerInformation (
  867. CurInfo,
  868. CurContext,
  869. CurContextSize,
  870. CurContext,
  871. CurContextSize
  872. );
  873. if (!NT_SUCCESS(Status)) {
  874. printf ("NtPowerInformation failed with %x\n", Status);
  875. }
  876. }
  877. puts (CurDesc);
  878. CurPrint (CurContext);
  879. }
  880. switch (Type) {
  881. case SEL_NONE:
  882. CurPrint = NULL;
  883. CurAssign = NULL;
  884. CurContext = NULL;
  885. break;
  886. case SEL_AC:
  887. CurPrint = PrintPol;
  888. CurInfo = SystemPowerPolicyAc;
  889. CurContext = &Ac;
  890. CurContextSize = sizeof(SYSTEM_POWER_POLICY);
  891. CurAssign = AssignPolicySetting;
  892. CurDesc = "AC power policy";
  893. break;
  894. case SEL_DC:
  895. CurPrint = PrintPol;
  896. CurInfo = SystemPowerPolicyDc;
  897. CurContext = &Dc;
  898. CurContextSize = sizeof(SYSTEM_POWER_POLICY);
  899. CurAssign = AssignPolicySetting;
  900. CurDesc = "DC power policy";
  901. break;
  902. case SEL_CURRENT:
  903. CurPrint = PrintPol;
  904. CurInfo = SystemPowerPolicyCurrent;
  905. CurContext = &Current;
  906. CurContextSize = sizeof(SYSTEM_POWER_POLICY);
  907. CurAssign = AssignCurrentSetting;
  908. CurDesc = "Current power policy";
  909. break;
  910. case SEL_PAC:
  911. CurPrint = PrintPPol;
  912. CurInfo = ProcessorPowerPolicyAc;
  913. CurContext = &PAc;
  914. CurContextSize = sizeof(PROCESSOR_POWER_POLICY);
  915. CurAssign = AssignPPolicySetting;
  916. CurDesc = "AC processor power policy";
  917. break;
  918. case SEL_PDC:
  919. CurPrint = PrintPPol;
  920. CurInfo = ProcessorPowerPolicyDc;
  921. CurContext = &PDc;
  922. CurContextSize = sizeof(PROCESSOR_POWER_POLICY);
  923. CurAssign = AssignPPolicySetting;
  924. CurDesc = "DC processor power policy";
  925. break;
  926. case SEL_PCURRENT:
  927. CurPrint = PrintPPol;
  928. CurInfo = ProcessorPowerPolicyCurrent;
  929. CurContext = &PCurrent;
  930. CurContextSize = sizeof(PROCESSOR_POWER_POLICY);
  931. CurAssign = AssignCurrentSetting;
  932. CurDesc = "Current processor power policy";
  933. break;
  934. case SEL_CAP:
  935. CurPrint = PrintCap;
  936. CurInfo = SystemPowerCapabilities,
  937. CurContext = &Cap;
  938. CurContextSize = sizeof(SYSTEM_POWER_CAPABILITIES);
  939. CurAssign = AssignCapSetting;
  940. CurDesc = "power capabilties";
  941. break;
  942. case SEL_ADMIN:
  943. CurPrint = PrintAdmin;
  944. CurInfo = AdministratorPowerPolicy;
  945. CurContext = &Admin;
  946. CurContextSize = sizeof(ADMINISTRATOR_POWER_POLICY);
  947. CurAssign = AssignAdminSetting;
  948. CurDesc = "Admin policy overrides";
  949. break;
  950. }
  951. CurType = Type;
  952. ItemUpdated = FALSE;
  953. }
  954. VOID
  955. UpdateAdmin (
  956. VOID
  957. )
  958. {
  959. ADMINISTRATOR_POWER_POLICY AdminPolicy;
  960. NTSTATUS Status;
  961. Status = NtPowerInformation (
  962. AdministratorPowerPolicy,
  963. NULL,
  964. 0,
  965. &AdminPolicy,
  966. sizeof (AdminPolicy)
  967. );
  968. if (!NT_SUCCESS(Status)) {
  969. printf ("Error reading admin policy %x\n", Status);
  970. exit (1);
  971. }
  972. Status = NtPowerInformation (
  973. AdministratorPowerPolicy,
  974. &AdminPolicy,
  975. sizeof (AdminPolicy),
  976. NULL,
  977. 0
  978. );
  979. if (!NT_SUCCESS(Status)) {
  980. printf ("Error writing admin policy %x\n", Status);
  981. exit (1);
  982. }
  983. }
  984. VOID __cdecl
  985. main (argc, argv)
  986. int argc;
  987. char *argv[];
  988. {
  989. NTSTATUS Status;
  990. PUCHAR p, p1;
  991. BOOLEAN Result;
  992. HANDLE hToken;
  993. TOKEN_PRIVILEGES tkp;
  994. if (argc < 2) {
  995. puts ("dumppo: cap ps bs admin ac dc current pac pdc pcurrent");
  996. exit (1);
  997. }
  998. Status = NtPowerInformation (
  999. SystemPowerCapabilities,
  1000. NULL,
  1001. 0,
  1002. &Cap,
  1003. sizeof (Cap)
  1004. );
  1005. if (!NT_SUCCESS(Status)) {
  1006. printf ("Error reading power capabilities %x\n", Status);
  1007. exit (1);
  1008. }
  1009. Status = NtPowerInformation (
  1010. AdministratorPowerPolicy,
  1011. NULL,
  1012. 0,
  1013. &Admin,
  1014. sizeof (Admin)
  1015. );
  1016. if (!NT_SUCCESS(Status)) {
  1017. printf ("Error reading admin power overrides %x\n", Status);
  1018. exit (1);
  1019. }
  1020. Status = NtPowerInformation (
  1021. SystemPowerPolicyAc,
  1022. NULL,
  1023. 0,
  1024. &Ac,
  1025. sizeof (Ac)
  1026. );
  1027. if (!NT_SUCCESS(Status)) {
  1028. printf ("Error reading AC policies %x\n", Status);
  1029. exit (1);
  1030. }
  1031. Status = NtPowerInformation (
  1032. SystemPowerPolicyDc,
  1033. NULL,
  1034. 0,
  1035. &Dc,
  1036. sizeof (Dc)
  1037. );
  1038. if (!NT_SUCCESS(Status)) {
  1039. printf ("Error reading DC policies %x\n", Status);
  1040. exit (1);
  1041. }
  1042. Status = NtPowerInformation (
  1043. SystemPowerPolicyCurrent,
  1044. NULL,
  1045. 0,
  1046. &Current,
  1047. sizeof (Current)
  1048. );
  1049. if (!NT_SUCCESS(Status)) {
  1050. printf ("Error reading Current policies %x\n", Status);
  1051. exit (1);
  1052. }
  1053. Result = (BOOLEAN)GetSystemPowerStatus (&PS);
  1054. if (!Result) {
  1055. printf ("False returned from GetSystemPowerStatus %x\n", GetLastError());
  1056. exit (1);
  1057. }
  1058. Status = NtPowerInformation (
  1059. SystemBatteryState,
  1060. NULL,
  1061. 0,
  1062. &Batt,
  1063. sizeof (Batt)
  1064. );
  1065. if (!NT_SUCCESS(Status)) {
  1066. printf ("Error reading system battery status %x\n", Status);
  1067. exit (1);
  1068. }
  1069. Status = NtPowerInformation (
  1070. ProcessorPowerPolicyAc,
  1071. NULL,
  1072. 0,
  1073. &PAc,
  1074. sizeof (PAc)
  1075. );
  1076. if (!NT_SUCCESS(Status)) {
  1077. printf ("Error reading processor AC policy %x\n", Status);
  1078. exit (1);
  1079. }
  1080. Status = NtPowerInformation(
  1081. ProcessorPowerPolicyDc,
  1082. NULL,
  1083. 0,
  1084. &PDc,
  1085. sizeof (PDc)
  1086. );
  1087. if (!NT_SUCCESS(Status)) {
  1088. printf("Error reading processor DC policy %x\n", Status);
  1089. exit (1);
  1090. }
  1091. Status = NtPowerInformation(
  1092. ProcessorPowerPolicyCurrent,
  1093. NULL,
  1094. 0,
  1095. &PCurrent,
  1096. sizeof (PCurrent)
  1097. );
  1098. if (!NT_SUCCESS(Status)) {
  1099. printf("Error reading processor Current policy %x\n", Status);
  1100. exit (1);
  1101. }
  1102. //
  1103. // Upgrade premissions
  1104. //
  1105. OpenProcessToken (
  1106. GetCurrentProcess(),
  1107. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  1108. &hToken
  1109. );
  1110. LookupPrivilegeValue (
  1111. NULL,
  1112. SE_SHUTDOWN_NAME,
  1113. &tkp.Privileges[0].Luid
  1114. );
  1115. tkp.PrivilegeCount = 1;
  1116. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1117. AdjustTokenPrivileges (
  1118. hToken,
  1119. FALSE,
  1120. &tkp,
  1121. 0,
  1122. NULL,
  1123. 0
  1124. );
  1125. //
  1126. // Process args
  1127. //
  1128. while (argc) {
  1129. argc--;
  1130. if (streql(*argv, "-v")) {
  1131. Verbose1 = TRUE;
  1132. }
  1133. if (streql(*argv, "-V")) {
  1134. Verbose1 = TRUE;
  1135. Verbose2 = TRUE;
  1136. }
  1137. for (p = *argv; *p; p++) {
  1138. if (*p >= 'A' && *p <= 'Z') {
  1139. *p += 'a' - 'A';
  1140. }
  1141. }
  1142. p = *argv;
  1143. argv += 1;
  1144. if (streql(p, "ac")) {
  1145. SelectItem(SEL_AC);
  1146. }
  1147. if (streql(p, "dc")) {
  1148. SelectItem(SEL_DC);
  1149. }
  1150. if (streql(p, "current")) {
  1151. SelectItem(SEL_CURRENT);
  1152. }
  1153. if (streql(p, "cap")) {
  1154. SelectItem(SEL_CAP);
  1155. }
  1156. if (streql(p, "ps")) {
  1157. SelectItem(SEL_NONE);
  1158. PrintPowerStatus ();
  1159. }
  1160. if (streql(p, "bs")) {
  1161. SelectItem(SEL_NONE);
  1162. PrintBattStatus();
  1163. }
  1164. if (streql(p, "admin")) {
  1165. SelectItem(SEL_ADMIN);
  1166. }
  1167. if (streql(p, "pdc")) {
  1168. SelectItem(SEL_PDC);
  1169. }
  1170. if (streql(p, "pac")) {
  1171. SelectItem(SEL_PAC);
  1172. }
  1173. if (streql(p, "pcurrent")) {
  1174. SelectItem(SEL_PCURRENT);
  1175. }
  1176. //
  1177. // Check if this is a assignment
  1178. //
  1179. for (p1=p; *p1; p1++) {
  1180. if (*p1 == '=') {
  1181. *p1 = 0;
  1182. AssignSetting (p, p1+1);
  1183. }
  1184. }
  1185. }
  1186. // flush
  1187. SelectItem(SEL_NONE);
  1188. }