Source code of Windows XP (NT5)
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.

1283 lines
36 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;
  15. SYSTEM_POWER_POLICY Ac, Dc;
  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. Dest += sprintf (Dest, "%s", DefBits[i].String);
  215. c = '|';
  216. }
  217. }
  218. }
  219. VOID
  220. PrintPowerStatus (
  221. VOID
  222. )
  223. {
  224. ULONG i;
  225. PUCHAR Ls;
  226. UCHAR Bf[200];
  227. switch (PS.ACLineStatus) {
  228. case AC_LINE_OFFLINE: Ls = "off line"; break;
  229. case AC_LINE_ONLINE: Ls = "on line"; break;
  230. case AC_LINE_BACKUP_POWER: Ls = "backup power"; break;
  231. case AC_LINE_UNKNOWN: Ls = "unknown"; break;
  232. default: Ls = "**invalid**"; break;
  233. }
  234. strcpy (Bf, "unkown");
  235. if (PS.BatteryFlag != BATTERY_FLAG_UNKNOWN) {
  236. GetBits(Bf, PS.BatteryFlag, PsBatteryFlagBits);
  237. }
  238. printf ("Win32 System power status\n");
  239. printf ("AC line status..........: %s\n", Ls);
  240. printf ("Battery flag............: %s\n", Bf);
  241. printf ("Battery life percent....: %d\n", PS.BatteryLifePercent);
  242. printf ("Battery full life time..: %d\n", PS.BatteryFullLifeTime);
  243. printf ("\n");
  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",Batt.DefaultAlert1, Batt.DefaultAlert2);
  267. printf ("\n");
  268. }
  269. BOOLEAN
  270. streql (
  271. PUCHAR p1,
  272. PUCHAR p2
  273. )
  274. {
  275. return strcmp(p1, p2) == 0;
  276. }
  277. PUCHAR
  278. _strtok (
  279. PUCHAR Start
  280. )
  281. {
  282. static PUCHAR Location;
  283. PUCHAR p;
  284. if (Start) {
  285. Location = Start;
  286. }
  287. for (p = Location; *p && *p <'a' && *p >'z'; p++) ;
  288. if (!*p) {
  289. return NULL;
  290. }
  291. Start = p;
  292. for (; *p && *p >= 'a' && *p <= 'z'; p++) ;
  293. if (*p) {
  294. *p++ = 0;
  295. }
  296. Location = p;
  297. return Start;
  298. }
  299. VOID
  300. PrintCap (
  301. PVOID Context
  302. )
  303. {
  304. ULONG i;
  305. printf ("System power capabilties\n");
  306. printf ("Power Button Present....: %s\n", szBool[Cap.PowerButtonPresent]);
  307. printf ("Sleep Button Present....: %s\n", szBool[Cap.SleepButtonPresent]);
  308. printf ("Lid Present.............: %s\n", szBool[Cap.LidPresent]);
  309. printf ("System states supported.: %s%s%s%s%s\n",
  310. Cap.SystemS1 ? "S1 " : "",
  311. Cap.SystemS2 ? "S2 " : "",
  312. Cap.SystemS3 ? "S3 " : "",
  313. Cap.SystemS4 ? "S4 " : "",
  314. Cap.SystemS5 ? "S5 " : ""
  315. );
  316. printf ("Hiber file reserved.....: %s\n", szBool[Cap.HiberFilePresent]);
  317. printf ("Thermal control.........: %s\n", szBool[Cap.ThermalControl]);
  318. printf ("CPU Throttle control....: %s\n", szBool[Cap.ProcessorThrottle]);
  319. printf ("Processor min throttle..: %d\n", Cap.ProcessorMinThrottle);
  320. printf ("Processor max throttle..: %d\n", Cap.ProcessorMaxThrottle);
  321. printf ("Some disk will spindown.: %s\n", szBool[Cap.DiskSpinDown]);
  322. printf ("System batteries present: %s %s\n",
  323. szBool[Cap.SystemBatteriesPresent],
  324. Cap.BatteriesAreShortTerm ? "- short term" : ""
  325. );
  326. printf ("System batteries scale..: ");
  327. for (i=0; i<3; i++) {
  328. printf ("(G:%d C:%d) ",
  329. Cap.BatteryScale[i].Granularity,
  330. Cap.BatteryScale[i].Capacity
  331. );
  332. }
  333. printf ("\n");
  334. printf ("Ac on line wake ability.: %s\n", SysPower(Cap.AcOnLineWake));
  335. printf ("Lid wake ability........: %s\n", SysPower(Cap.SoftLidWake));
  336. printf ("RTC wake ability........: %s\n", SysPower(Cap.RtcWake));
  337. printf ("Min device wake.........: %s\n", SysPower(Cap.MinDeviceWakeState));
  338. printf ("Default low latency wake: %s\n", SysPower(Cap.DefaultLowLatencyWake));
  339. printf ("\n");
  340. }
  341. PUCHAR
  342. Action (
  343. IN PBOOLEAN CapFlag,
  344. IN PPOWER_ACTION_POLICY Act
  345. )
  346. {
  347. static UCHAR text[200];
  348. PUCHAR p;
  349. UCHAR c;
  350. ULONG i;
  351. p = text;
  352. if (CapFlag && !*CapFlag) {
  353. p += sprintf(p, "Disabled ");
  354. }
  355. p += sprintf (p, "%s", 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 (!p) {
  382. printf ("Unknown power action %s.\n", CurValue);
  383. printf ("use: doze, sleep, shutdown, shutdownreset, shutdownoff\n");
  384. exit (1);
  385. }
  386. if (streql(p, "none")) { Act = PowerActionNone;
  387. } else if (streql(p, "sleep")) { Act = PowerActionSleep;
  388. } else if (streql(p, "hiber")) { Act = PowerActionHibernate;
  389. } else if (streql(p, "shutdown")) { Act = PowerActionShutdown;
  390. } else if (streql(p, "shutdownreset")) { Act = PowerActionShutdownReset;
  391. } else if (streql(p, "shutdownoff")) { Act = PowerActionShutdownOff;
  392. } else {
  393. printf ("Unknown power action '%s'.\n", p);
  394. printf ("use: doze, sleep, shutdown, shutdownreset, shutdownoff\n");
  395. exit (1);
  396. }
  397. while (p = _strtok(NULL)) {
  398. if (streql(p, "qapp")) { Flags |= POWER_ACTION_QUERY_ALLOWED;
  399. } else if (streql(p, "ui")) { Flags |= POWER_ACTION_UI_ALLOWED;
  400. } else if (streql(p, "override")) { Flags |= POWER_ACTION_OVERRIDE_APPS;
  401. } else if (streql(p, "disablewake")) { Flags |= POWER_ACTION_DISABLE_WAKES;
  402. } else if (streql(p, "critical")) { Flags |= POWER_ACTION_CRITICAL;
  403. } else {
  404. printf ("Unknown power action '%s'.\n", p);
  405. printf ("use: qapp, io, override, disablewake, critical\n");
  406. exit (1);
  407. }
  408. }
  409. Action->Action = Act;
  410. Action->Flags = Flags;
  411. }
  412. VOID
  413. SetSysPower (
  414. IN PSYSTEM_POWER_STATE SysPower
  415. )
  416. {
  417. PUCHAR p;
  418. p = CurValue;
  419. if (streql(p, "s0")) { *SysPower = PowerSystemWorking;
  420. } else if (streql(p, "s1")) { *SysPower = PowerSystemSleeping1;
  421. } else if (streql(p, "s2")) { *SysPower = PowerSystemSleeping2;
  422. } else if (streql(p, "s3")) { *SysPower = PowerSystemSleeping3;
  423. } else if (streql(p, "s4")) { *SysPower = PowerSystemHibernate;
  424. } else {
  425. printf ("Unknown system power state '%s'. Use S0,S1,S2,S3 or S4\n", p);
  426. exit (1);
  427. }
  428. }
  429. VOID
  430. SetPolicyCount(
  431. IN PULONG Variable
  432. )
  433. {
  434. ULONG local;
  435. if (!CurValueIsNumeric) {
  436. printf("'%s' is not numeric\n", CurValue);
  437. exit(1);
  438. }
  439. local = atol(CurValue);
  440. if (local > 3) {
  441. printf("PolicyCount can only be in the range 0 to 3\n");
  442. exit(1);
  443. }
  444. *Variable = local;
  445. }
  446. VOID
  447. SetDynamicThrottle(
  448. IN PUCHAR Variable
  449. )
  450. {
  451. ULONG local;
  452. PUCHAR p;
  453. p = CurValue;
  454. if (streql(p,"none")) { *Variable = PO_THROTTLE_NONE;
  455. } else if (streql(p,"constant")) { *Variable = PO_THROTTLE_CONSTANT;
  456. } else if (streql(p,"degrade")) { *Variable = PO_THROTTLE_DEGRADE;
  457. } else if (streql(p,"degraded")) { *Variable = PO_THROTTLE_DEGRADE;
  458. } else if (streql(p,"adaptive")) { *Variable = PO_THROTTLE_ADAPTIVE;
  459. } else {
  460. printf("Unknown Dynamic Throttle state %s\n", CurValue);
  461. exit(1);
  462. }
  463. }
  464. VOID
  465. SetPercentage (
  466. IN PUCHAR Variable
  467. )
  468. {
  469. PUCHAR p;
  470. if (!CurValueIsNumeric) {
  471. for (p = CurValue; *p; p++) {
  472. if (*p == '%') {
  473. *p = '\0';
  474. }
  475. }
  476. CurValueIsNumeric = TRUE;
  477. for (p = CurValue; *p; p++) {
  478. if (*p < '0' || *p > '9') {
  479. CurValueIsNumeric = FALSE;
  480. }
  481. }
  482. if (!CurValueIsNumeric) {
  483. printf ("'%s' is not numeric\n", CurValue);
  484. exit (1);
  485. }
  486. }
  487. *Variable = (UCHAR) atol(CurValue);
  488. }
  489. VOID
  490. SetValue (
  491. IN PULONG Variable
  492. )
  493. {
  494. if (!CurValueIsNumeric) {
  495. printf ("'%s' is not numeric\n", CurValue);
  496. exit (1);
  497. }
  498. *Variable = atol(CurValue);
  499. }
  500. VOID
  501. SetBool (
  502. IN PBOOLEAN Variable
  503. )
  504. {
  505. BOOLEAN State;
  506. State = 99;
  507. if (CurValueIsNumeric) {
  508. State = (BOOLEAN)atol(CurValue);
  509. } else {
  510. if (streql(CurValue, "true")) { State = TRUE;
  511. } else if (streql(CurValue, "false")) { State = FALSE;
  512. }
  513. }
  514. if (State != FALSE && State != TRUE) {
  515. printf ("'%s' is not boolean\n");
  516. exit (1);
  517. }
  518. *Variable = State;
  519. }
  520. VOID
  521. SetField(
  522. IN PPROCESSOR_POWER_POLICY Pol,
  523. IN ULONG Index,
  524. IN PUCHAR What
  525. )
  526. {
  527. BOOLEAN State;
  528. State = 99;
  529. if (CurValueIsNumeric) {
  530. State = (BOOLEAN)atol(CurValue);
  531. } else {
  532. if (streql(CurValue, "true")) { State = TRUE;
  533. } else if (streql(CurValue, "false")) { State = FALSE;
  534. }
  535. }
  536. if (State != FALSE && State != TRUE) {
  537. printf ("'%s' is not boolean\n", CurValue);
  538. exit (1);
  539. }
  540. if (streql(What,"allowpromotion")) { Pol->Policy[Index].AllowPromotion = State;
  541. } else if (streql(What,"allowdemotion")) { Pol->Policy[Index].AllowDemotion = State;
  542. }
  543. }
  544. VOID
  545. PrintPol (
  546. IN PSYSTEM_POWER_POLICY Pol
  547. )
  548. {
  549. BOOLEAN AnySleep;
  550. ULONG i;
  551. UCHAR text[200];
  552. PUCHAR p;
  553. if (Pol->Revision != 1) {
  554. printf ("** revision not 1**\n");
  555. }
  556. AnySleep = Cap.SystemS1 || Cap.SystemS2 || Cap.SystemS3 || Cap.SystemS4;
  557. printf ("Power button.........: %s\n", Action(&Cap.PowerButtonPresent, &Pol->PowerButton));
  558. printf ("Sleep button.........: %s\n", Action(&Cap.SleepButtonPresent, &Pol->SleepButton));
  559. printf ("Lid close............: %s\n", Action(&Cap.LidPresent, &Pol->LidClose));
  560. printf ("Lid open wake........: %s\n", SysPower(Pol->LidOpenWake));
  561. printf ("Idle.................: %s\n", Action(&AnySleep, &Pol->Idle));
  562. printf ("Settings.............: Timeout=%d, Sensitivity=%d\n",
  563. Pol->IdleTimeout,
  564. Pol->IdleSensitivity
  565. );
  566. printf ("Min sleep state......: %s\n", SysPower(Pol->MinSleep));
  567. printf ("Max sleep state......: %s\n", SysPower(Pol->MaxSleep));
  568. printf ("Reduced latency.sleep: %s\n", SysPower(Pol->ReducedLatencySleep));
  569. printf ("WinLogonFlags........: %x\n", Pol->WinLogonFlags);
  570. printf ("DynamicThrottle......: %s\n", DynamicThrottle(Pol->DynamicThrottle));
  571. printf ("Doze S4 timeout......: %d\n", Pol->DozeS4Timeout);
  572. printf ("Battery broadcast res: %d\n", Pol->BroadcastCapacityResolution);
  573. for (i=0; i < NUM_DISCHARGE_POLICIES; i++) {
  574. printf ("Battery discharge %d..: ", i);
  575. p = "Disabled";
  576. if (!Cap.SystemBatteriesPresent && Pol->DischargePolicy[i].Enable) {
  577. p = "No battery, but enable is set";
  578. }
  579. if (Cap.SystemBatteriesPresent && Pol->DischargePolicy[i].Enable) {
  580. sprintf (text, "%d, %s, Min=%s",
  581. Pol->DischargePolicy[i].BatteryLevel,
  582. Action (NULL, &Pol->DischargePolicy[i].PowerPolicy),
  583. SysPower(Pol->DischargePolicy[i].MinSystemState)
  584. );
  585. p = text;
  586. }
  587. printf ("%s\n", p);
  588. }
  589. printf ("Video timeout.......: %d\n", Pol->VideoTimeout);
  590. printf ("Hard disk timeout....: %d\n", Pol->SpindownTimeout);
  591. printf ("Optimize for power...: %s\n", szBool[Pol->OptimizeForPower]);
  592. printf ("Fan throttle toler...: %d\n", Pol->FanThrottleTolerance);
  593. printf ("Forced throttle......: %d%% %s\n",
  594. Pol->ForcedThrottle,
  595. Pol->ForcedThrottle == 100 ? "(off)" : "(on)"
  596. );
  597. printf ("Min throttle.........: %d%%\n", Pol->MinThrottle);
  598. printf ("Over throttle act....: %s\n", Action (NULL, &Pol->OverThrottled));
  599. printf ("\n");
  600. }
  601. VOID
  602. PrintPPol (
  603. IN PPROCESSOR_POWER_POLICY Pol
  604. )
  605. {
  606. ULONG i;
  607. UCHAR text[200];
  608. PUCHAR p;
  609. if (Pol->Revision != 1) {
  610. printf ("** revision not 1**\n");
  611. }
  612. printf ("Dynamic Throttle.....: %s\n", DynamicThrottle(Pol->DynamicThrottle) );
  613. printf ("\n");
  614. for (i = 0; i < Pol->PolicyCount; i++) {
  615. printf ("C%x Processor Power Policy\n", (i+1) );
  616. printf (" Allow Demotion....: %s\n", szBool[Pol->Policy[i].AllowDemotion]);
  617. printf (" Allow Promotion...: %s\n", szBool[Pol->Policy[i].AllowPromotion]);
  618. printf (" Demote Percent....: %d%%\n", Pol->Policy[i].DemotePercent);
  619. printf (" Promote Percent...: %d%%\n", Pol->Policy[i].PromotePercent);
  620. printf (" Demote Limit......: %s\n", MicroSeconds(Pol->Policy[i].DemoteLimit) );
  621. printf (" Promote Limit.....: %s\n", MicroSeconds(Pol->Policy[i].PromoteLimit) );
  622. printf (" Time Check........: %s\n", MicroSeconds(Pol->Policy[i].TimeCheck) );
  623. printf ("\n");
  624. }
  625. printf ("\n");
  626. }
  627. VOID
  628. PrintAdmin (
  629. PVOID Context
  630. )
  631. {
  632. printf ("Min sleep state......: %s\n", SysPower(Admin.MinSleep));
  633. printf ("Max sleep state......: %s\n", SysPower(Admin.MaxSleep));
  634. printf ("Min video timeout....: %d\n", Admin.MinVideoTimeout);
  635. printf ("Max video timeout....: %d\n", Admin.MaxVideoTimeout);
  636. printf ("Min spindown timeout.: %d\n", Admin.MinSpindownTimeout);
  637. printf ("Max spindown timeout.: %d\n", Admin.MaxSpindownTimeout);
  638. }
  639. VOID
  640. AssignSetting (
  641. PUCHAR Variable,
  642. PUCHAR Value
  643. )
  644. {
  645. PSYSTEM_POWER_POLICY Pol;
  646. PUCHAR p;
  647. if (!CurContext) {
  648. printf ("must select ac, dc, pac, pdc, cap, admin to make setting in\n");
  649. exit (1);
  650. }
  651. CurValue = Value;
  652. if (!*CurValue) {
  653. printf ("null settings not allowed\n");
  654. exit (1);
  655. }
  656. CurValueIsNumeric = TRUE;
  657. for (p = Value; *p; p++) {
  658. if (*p < '0' || *p > '9') {
  659. CurValueIsNumeric = FALSE;
  660. }
  661. }
  662. ItemUpdated = TRUE;
  663. CurAssign (Variable, Value);
  664. }
  665. VOID
  666. AssignPolicySetting (
  667. PUCHAR Variable,
  668. PUCHAR Value
  669. )
  670. {
  671. PSYSTEM_POWER_POLICY Pol;
  672. Pol = (PSYSTEM_POWER_POLICY) CurContext;
  673. // set policy
  674. if (streql(Variable, "pbutt")) { SetAction(&Pol->PowerButton);
  675. } else if (streql(Variable, "sbutt")) { SetAction(&Pol->SleepButton);
  676. } else if (streql(Variable, "lidclose")) { SetAction(&Pol->LidClose);
  677. } else if (streql(Variable, "idle")) { SetAction(&Pol->Idle);
  678. } else if (streql(Variable, "idlesense")) { SetValue((PULONG)&Pol->IdleSensitivity);
  679. } else if (streql(Variable, "idletimeout")) { SetValue(&Pol->IdleTimeout);
  680. } else if (streql(Variable, "minsleep")) { SetSysPower(&Pol->MinSleep);
  681. } else if (streql(Variable, "maxsleep")) { SetSysPower(&Pol->MaxSleep);
  682. } else if (streql(Variable, "reducedsleep")) { SetSysPower(&Pol->ReducedLatencySleep);
  683. } else if (streql(Variable, "s4timeout")) { SetValue(&Pol->DozeS4Timeout);
  684. } else if (streql(Variable, "videotimeout")) { SetValue(&Pol->VideoTimeout);
  685. } else if (streql(Variable, "disktimeout")) { SetValue(&Pol->SpindownTimeout);
  686. } else if (streql(Variable, "optpower")) { SetBool(&Pol->OptimizeForPower);
  687. } else if (streql(Variable, "fantol")) { SetValue((PULONG)&Pol->FanThrottleTolerance);
  688. } else if (streql(Variable, "minthrot")) { SetValue((PULONG)&Pol->MinThrottle);
  689. } else if (streql(Variable, "forcethrot")) { SetValue((PULONG)&Pol->ForcedThrottle);
  690. } else if (streql(Variable, "overthrot")) { SetAction(&Pol->OverThrottled);
  691. } else {
  692. printf ("Variable not:\n");
  693. printf (" pbutt, sbutt, lidclose\n");
  694. printf (" idle, idlesense, idletime\n");
  695. printf (" minsleep, maxsleep, reducedsleep, s4timeout\n");
  696. printf (" videotimeout, disktimeout, optpower\n");
  697. printf (" optpower, fantol, minthrot, forcethrot, overthrot\n");
  698. exit (1);
  699. }
  700. }
  701. VOID
  702. AssignPPolicySetting (
  703. PUCHAR Variable,
  704. PUCHAR Value
  705. )
  706. {
  707. PPROCESSOR_POWER_POLICY Pol;
  708. Pol = (PPROCESSOR_POWER_POLICY) CurContext;
  709. if (streql(Variable,"policycount")) { SetPolicyCount(&(Pol->PolicyCount));
  710. } else if (streql(Variable,"dynamicthrottle")) { SetDynamicThrottle(&(Pol->DynamicThrottle));
  711. } else if (streql(Variable,"c1allowpromotion")) { SetField(Pol,0,"allowpromotion");
  712. } else if (streql(Variable,"c1allowdemotion")) { SetField(Pol,0,"allowdemotion");
  713. } else if (streql(Variable,"c1demotepercent")) { SetPercentage(&(Pol->Policy[0].DemotePercent));
  714. } else if (streql(Variable,"c1promotepercent")) { SetPercentage(&(Pol->Policy[0].PromotePercent));
  715. } else if (streql(Variable,"c1demotelimit")) { SetValue(&(Pol->Policy[0].DemoteLimit));
  716. } else if (streql(Variable,"c1promotelimit")) { SetValue(&(Pol->Policy[0].PromoteLimit));
  717. } else if (streql(Variable,"c1timecheck")) { SetValue(&(Pol->Policy[0].TimeCheck));
  718. } else if (streql(Variable,"c2allowpromotion")) { SetField(Pol,1,"allowpromotion");
  719. } else if (streql(Variable,"c2allowdemotion")) { SetField(Pol,1,"allowdemotion");
  720. } else if (streql(Variable,"c2demotepercent")) { SetPercentage(&(Pol->Policy[1].DemotePercent));
  721. } else if (streql(Variable,"c2promotepercent")) { SetPercentage(&(Pol->Policy[1].PromotePercent));
  722. } else if (streql(Variable,"c2demotelimit")) { SetValue(&(Pol->Policy[1].DemoteLimit));
  723. } else if (streql(Variable,"c2promotelimit")) { SetValue(&(Pol->Policy[1].PromoteLimit));
  724. } else if (streql(Variable,"c2timecheck")) { SetValue(&(Pol->Policy[1].TimeCheck));
  725. } else if (streql(Variable,"c3allowpromotion")) { SetField(Pol,2,"allowpromotion");
  726. } else if (streql(Variable,"c3allowdemotion")) { SetField(Pol,2,"allowdemotion");
  727. } else if (streql(Variable,"c3demotepercent")) { SetPercentage(&(Pol->Policy[2].DemotePercent));
  728. } else if (streql(Variable,"c3promotepercent")) { SetPercentage(&(Pol->Policy[2].PromotePercent));
  729. } else if (streql(Variable,"c3demotelimit")) { SetValue(&(Pol->Policy[2].DemoteLimit));
  730. } else if (streql(Variable,"c3promotelimit")) { SetValue(&(Pol->Policy[2].PromoteLimit));
  731. } else if (streql(Variable,"c3timecheck")) { SetValue(&(Pol->Policy[2].TimeCheck));
  732. } else {
  733. printf("Variable not:\n");
  734. printf(" policycount - number of elements in policy\n");
  735. printf(" dynamicthrottle - which throttle policy to use\n");
  736. printf(" cXallowpromotion - allow promotion from state X\n");
  737. printf(" cXallowdemotion - allow demotion from state X\n");
  738. printf(" cXdemotepercent - set demote percent for state X\n");
  739. printf(" cXpromotepercent - set promote percent for state X\n");
  740. printf(" cXdemotelimit - set demote limit for state X\n");
  741. printf(" cXpromotelimit - set promote limit for state X\n");
  742. printf(" cXtimecheck - set time check interval for state X\n");
  743. printf(" where X = <1,2,3>\n");
  744. exit(1);
  745. }
  746. }
  747. VOID
  748. AssignAdminSetting (
  749. PUCHAR Variable,
  750. PUCHAR Value
  751. )
  752. {
  753. if (streql(Variable, "minsleep")) { SetSysPower(&Admin.MinSleep);
  754. } else if (streql(Variable, "maxsleep")) { SetSysPower(&Admin.MaxSleep);
  755. } else if (streql(Variable, "minvideo")) { SetValue(&Admin.MinVideoTimeout);
  756. } else if (streql(Variable, "maxvideo")) { SetValue(&Admin.MaxVideoTimeout);
  757. } else if (streql(Variable, "mindisk")) { SetValue(&Admin.MinSpindownTimeout);
  758. } else if (streql(Variable, "maxdisk")) { SetValue(&Admin.MaxSpindownTimeout);
  759. } else {
  760. printf ("Variable not:\n");
  761. printf (" minsleep, maxsleep\n");
  762. printf (" minvideo, maxvideo, mindisk, maxdisk\n");
  763. exit (1);
  764. }
  765. }
  766. VOID
  767. AssignCapSetting (
  768. PUCHAR Variable,
  769. PUCHAR Value
  770. )
  771. {
  772. // set capability
  773. if (streql(Variable, "pbutt")) { SetBool(&Cap.PowerButtonPresent);
  774. } else if (streql(Variable, "sbutt")) { SetBool(&Cap.SleepButtonPresent);
  775. } else if (streql(Variable, "lid")) { SetBool(&Cap.LidPresent);
  776. } else if (streql(Variable, "s1")) { SetBool(&Cap.SystemS1);
  777. } else if (streql(Variable, "s2")) { SetBool(&Cap.SystemS2);
  778. } else if (streql(Variable, "s3")) { SetBool(&Cap.SystemS3);
  779. } else if (streql(Variable, "s4")) { SetBool(&Cap.SystemS4);
  780. } else if (streql(Variable, "s5")) { SetBool(&Cap.SystemS5);
  781. } else if (streql(Variable, "batt")) { SetBool(&Cap.SystemBatteriesPresent);
  782. } else if (streql(Variable, "shortterm")) { SetBool(&Cap.BatteriesAreShortTerm);
  783. } else if (streql(Variable, "acwake")) { SetSysPower(&Cap.AcOnLineWake);
  784. } else if (streql(Variable, "lidwake")) { SetSysPower(&Cap.SoftLidWake);
  785. } else if (streql(Variable, "rtcwake")) { SetSysPower(&Cap.RtcWake);
  786. } else if (streql(Variable, "lowlat")) { SetSysPower(&Cap.DefaultLowLatencyWake);
  787. } else {
  788. printf ("Variable not:\n");
  789. printf (" pbutt, sbutt, lid, s1, s2, s3, s4, s5\n");
  790. printf (" batt, shortterm, acwake, lidwake, rtcwak, lowlat\n");
  791. exit (1);
  792. }
  793. }
  794. #define SEL_NONE 0
  795. #define SEL_AC 1
  796. #define SEL_DC 2
  797. #define SEL_CAP 3
  798. #define SEL_ADMIN 4
  799. #define SEL_PAC 5
  800. #define SEL_PDC 6
  801. VOID
  802. SelectItem(
  803. IN ULONG Type
  804. )
  805. {
  806. NTSTATUS Status;
  807. if (CurType) {
  808. if (ItemUpdated) {
  809. if (Verbose2) {
  810. printf ("%s being set as:\n", CurDesc);
  811. CurPrint (CurContext);
  812. }
  813. Status = NtPowerInformation (
  814. CurInfo,
  815. CurContext,
  816. CurContextSize,
  817. CurContext,
  818. CurContextSize
  819. );
  820. if (!NT_SUCCESS(Status)) {
  821. printf ("NtPowerInformation failed with %x\n", Status);
  822. }
  823. }
  824. printf ("%s\n", CurDesc);
  825. CurPrint (CurContext);
  826. }
  827. switch (Type) {
  828. case SEL_NONE:
  829. CurPrint = NULL;
  830. CurAssign = NULL;
  831. CurContext = NULL;
  832. break;
  833. case SEL_AC:
  834. CurPrint = PrintPol;
  835. CurInfo = SystemPowerPolicyAc;
  836. CurContext = &Ac;
  837. CurContextSize = sizeof(SYSTEM_POWER_POLICY);
  838. CurAssign = AssignPolicySetting;
  839. CurDesc = "AC power policy";
  840. break;
  841. case SEL_DC:
  842. CurPrint = PrintPol;
  843. CurInfo = SystemPowerPolicyDc;
  844. CurContext = &Dc;
  845. CurContextSize = sizeof(SYSTEM_POWER_POLICY);
  846. CurAssign = AssignPolicySetting;
  847. CurDesc = "DC power policy";
  848. break;
  849. case SEL_PAC:
  850. CurPrint = PrintPPol;
  851. CurInfo = ProcessorPowerPolicyAc;
  852. CurContext = &PAc;
  853. CurContextSize = sizeof(PROCESSOR_POWER_POLICY);
  854. CurAssign = AssignPPolicySetting;
  855. CurDesc = "AC processor power policy";
  856. break;
  857. case SEL_PDC:
  858. CurPrint = PrintPPol;
  859. CurInfo = ProcessorPowerPolicyDc;
  860. CurContext = &PDc;
  861. CurContextSize = sizeof(PROCESSOR_POWER_POLICY);
  862. CurAssign = AssignPPolicySetting;
  863. CurDesc = "DC processor power policy";
  864. break;
  865. case SEL_CAP:
  866. CurPrint = PrintCap;
  867. CurInfo = SystemPowerCapabilities,
  868. CurContext = &Cap;
  869. CurContextSize = sizeof(SYSTEM_POWER_CAPABILITIES);
  870. CurAssign = AssignCapSetting;
  871. CurDesc = "power capabilties";
  872. break;
  873. case SEL_ADMIN:
  874. CurPrint = PrintAdmin;
  875. CurInfo = AdministratorPowerPolicy;
  876. CurContext = &Admin;
  877. CurContextSize = sizeof(ADMINISTRATOR_POWER_POLICY);
  878. CurAssign = AssignAdminSetting;
  879. CurDesc = "Admin policy overrides";
  880. break;
  881. }
  882. CurType = Type;
  883. ItemUpdated = FALSE;
  884. }
  885. VOID
  886. UpdateAdmin (
  887. VOID
  888. )
  889. {
  890. ADMINISTRATOR_POWER_POLICY AdminPolicy;
  891. NTSTATUS Status;
  892. Status = NtPowerInformation (
  893. AdministratorPowerPolicy,
  894. NULL,
  895. 0,
  896. &AdminPolicy,
  897. sizeof (AdminPolicy)
  898. );
  899. if (!NT_SUCCESS(Status)) {
  900. printf ("Error reading admin policy %x\n", Status);
  901. exit (1);
  902. }
  903. Status = NtPowerInformation (
  904. AdministratorPowerPolicy,
  905. &AdminPolicy,
  906. sizeof (AdminPolicy),
  907. NULL,
  908. 0
  909. );
  910. if (!NT_SUCCESS(Status)) {
  911. printf ("Error writing admin policy %x\n", Status);
  912. exit (1);
  913. }
  914. }
  915. VOID __cdecl
  916. main (argc, argv)
  917. int argc;
  918. char *argv[];
  919. {
  920. NTSTATUS Status;
  921. PUCHAR p, p1;
  922. BOOLEAN Result;
  923. HANDLE hToken;
  924. TOKEN_PRIVILEGES tkp;
  925. if (argc < 2) {
  926. printf ("dumppo: cap ps bs admin ac dc pac pdc\n");
  927. exit (1);
  928. }
  929. Status = NtPowerInformation (
  930. SystemPowerCapabilities,
  931. NULL,
  932. 0,
  933. &Cap,
  934. sizeof (Cap)
  935. );
  936. if (!NT_SUCCESS(Status)) {
  937. printf ("Error reading power capabilities %x\n", Status);
  938. exit (1);
  939. }
  940. Status = NtPowerInformation (
  941. AdministratorPowerPolicy,
  942. NULL,
  943. 0,
  944. &Admin,
  945. sizeof (Admin)
  946. );
  947. if (!NT_SUCCESS(Status)) {
  948. printf ("Error reading admin power overrides %x\n", Status);
  949. exit (1);
  950. }
  951. Status = NtPowerInformation (
  952. SystemPowerPolicyAc,
  953. NULL,
  954. 0,
  955. &Ac,
  956. sizeof (Ac)
  957. );
  958. if (!NT_SUCCESS(Status)) {
  959. printf ("Error reading AC policies %x\n", Status);
  960. exit (1);
  961. }
  962. Status = NtPowerInformation (
  963. SystemPowerPolicyDc,
  964. NULL,
  965. 0,
  966. &Dc,
  967. sizeof (Dc)
  968. );
  969. if (!NT_SUCCESS(Status)) {
  970. printf ("Error reading DC policies %x\n", Status);
  971. exit (1);
  972. }
  973. Result = (BOOLEAN)GetSystemPowerStatus (&PS);
  974. if (!Result) {
  975. printf ("False returned from GetSystemPowerStatus %x\n", GetLastError());
  976. exit (1);
  977. }
  978. Status = NtPowerInformation (
  979. SystemBatteryState,
  980. NULL,
  981. 0,
  982. &Batt,
  983. sizeof (Batt)
  984. );
  985. if (!NT_SUCCESS(Status)) {
  986. printf ("Error reading system battery status %x\n", Status);
  987. exit (1);
  988. }
  989. Status = NtPowerInformation (
  990. ProcessorPowerPolicyAc,
  991. NULL,
  992. 0,
  993. &PAc,
  994. sizeof (PAc)
  995. );
  996. if (!NT_SUCCESS(Status)) {
  997. printf ("Error reading processor AC policy %x\n", Status);
  998. exit (1);
  999. }
  1000. Status = NtPowerInformation(
  1001. ProcessorPowerPolicyDc,
  1002. NULL,
  1003. 0,
  1004. &PDc,
  1005. sizeof (PDc)
  1006. );
  1007. if (!NT_SUCCESS(Status)) {
  1008. printf("Error reading processor DC policy %x\n", Status);
  1009. exit (1);
  1010. }
  1011. //
  1012. // Upgrade premissions
  1013. //
  1014. OpenProcessToken (
  1015. GetCurrentProcess(),
  1016. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  1017. &hToken
  1018. );
  1019. LookupPrivilegeValue (
  1020. NULL,
  1021. SE_SHUTDOWN_NAME,
  1022. &tkp.Privileges[0].Luid
  1023. );
  1024. tkp.PrivilegeCount = 1;
  1025. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1026. AdjustTokenPrivileges (
  1027. hToken,
  1028. FALSE,
  1029. &tkp,
  1030. 0,
  1031. NULL,
  1032. 0
  1033. );
  1034. //
  1035. // Process args
  1036. //
  1037. while (argc) {
  1038. argc--;
  1039. if (streql(*argv, "-v")) {
  1040. Verbose1 = TRUE;
  1041. }
  1042. if (streql(*argv, "-V")) {
  1043. Verbose1 = TRUE;
  1044. Verbose2 = TRUE;
  1045. }
  1046. for (p = *argv; *p; p++) {
  1047. if (*p >= 'A' && *p <= 'Z') {
  1048. *p += 'a' - 'A';
  1049. }
  1050. }
  1051. p = *argv;
  1052. argv += 1;
  1053. if (streql(p, "ac")) {
  1054. SelectItem(SEL_AC);
  1055. }
  1056. if (streql(p, "dc")) {
  1057. SelectItem(SEL_DC);
  1058. }
  1059. if (streql(p, "cap")) {
  1060. SelectItem(SEL_CAP);
  1061. }
  1062. if (streql(p, "ps")) {
  1063. SelectItem(SEL_NONE);
  1064. PrintPowerStatus ();
  1065. }
  1066. if (streql(p, "bs")) {
  1067. SelectItem(SEL_NONE);
  1068. PrintBattStatus ();
  1069. }
  1070. if (streql(p, "admin")) {
  1071. SelectItem(SEL_ADMIN);
  1072. }
  1073. if (streql(p, "pdc")) {
  1074. SelectItem(SEL_PDC);
  1075. }
  1076. if (streql(p, "pac")) {
  1077. SelectItem(SEL_PAC);
  1078. }
  1079. //
  1080. // Check if this is a assignment
  1081. //
  1082. for (p1=p; *p1; p1++) {
  1083. if (*p1 == '=') {
  1084. *p1 = 0;
  1085. AssignSetting (p, p1+1);
  1086. }
  1087. }
  1088. }
  1089. // flush
  1090. SelectItem(SEL_NONE);
  1091. }