315 lines
15 KiB
C#
315 lines
15 KiB
C#
//----------------------------------------------------------------------------
|
|
//
|
|
// Copyright (c) Intel Corporation, 2011 - 2014 All Rights Reserved.
|
|
//
|
|
// File: RemoteControlSample.cs
|
|
//
|
|
// Contents: Sample code for Intel(R) Active Management Technology
|
|
// (Intel® AMT) RemoteControl Sample.
|
|
//
|
|
// Notes: This sample demonstrates how to use various commands of
|
|
// the RemoteControl service.
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
using Utils;
|
|
|
|
namespace RemoteControl
|
|
{
|
|
class RemoteControlSample
|
|
{
|
|
#region CONSTANTS
|
|
|
|
//Command line Flags
|
|
// *** Do GET_CONFIG_TO_NEXT_BOOT
|
|
private const string ALL = "api";
|
|
private const string GET_SYSTEM_POWER_STATE = "getpower";
|
|
private const string GET_POWER_CAPABILITIES = "powercap";
|
|
private const string CHANGE_POWER_STATE = "changepower";
|
|
private const string GET_BOOT_DETAILS = "bootdetails";
|
|
private const string GET_BOOT_CAPABILITIES = "bootcap";
|
|
private const string SET_RSE_FLAG = "setrseflag";
|
|
private const string SET_RSE_PASSWORD = "setrsepass";
|
|
private const string CLEAR_BOOT_OPTIONS = "clearoptions";
|
|
private const string SET_BOOT_SOURCE = "setbootsource";
|
|
private const string GET_CONFIG_TO_NEXT_BOOT = "getbootconfig";
|
|
private const string SET_RSE_NEXT_BOOT = "setrsenextboot";
|
|
|
|
private const string CPU_STATE = "cpu";
|
|
|
|
//Command line Flags's descriptions
|
|
private const string CHANGE_POWER_STATE_DESCRIPTION = "To activate an Intel AMT power change.\n\t NOTE: This option can cause a remote reboot to the Intel AMT machine.\n\t Value can be one of the following:\n \t on | off | wakefromconnectedstandby | powercycle | busreset | \n \t sleep | hibernate | gracefulOff | gracefulReset | nmi \n";
|
|
private const string GET_BOOT_DETAILS_DESCRIPTION = "Get and display Boot details";
|
|
private const string GET_BOOT_CAPABILITIES_DESCRIPTION = "Get and display the “Managed System” Boot Capabilities";
|
|
private const string SET_RSE_FLAG_DESCRIPTION = "Set RSE boot flag for the next Intel AMT power change. (When True, the BIOS performs secure erase operation)";
|
|
private const string SET_RSE_PASSWORD_DESCRIPTION = "Set RSE password";
|
|
private const string SET_RSE_NEXT_BOOT_DESCRIPTION = "Set RSE boot for the next Intel AMT power change.\n WARNING: This option erases the content of the primary or boot drive of the machine after performing reboot";
|
|
private const string CLEAR_BOOT_OPTIONS_DESCRIPTION = "clear all boot options for the next Intel AMT power change. (When True, the BIOS performs secure erase operation)";
|
|
private const string GET_POWER_CAPABILITIES_DESCRIPTION = "Get and display Power Management Capabilities";
|
|
private const string CPU_STATE_DESCRIPTION = "Check if the Host CPU has halted unexpectedly";
|
|
private const string GET_SYSTEM_POWER_STATE_DESCRIPTION = "Get system power state and power saving state";
|
|
private const string ALL_DESCRIPTION = "Perform all Remote Control options";
|
|
private const string SET_BOOT_SOURCE_DESCRIPTION = "Set a boot device for next Intel AMT power change.\n\t Value can be one of the following:\n\t none, harddisk, cd, pxe, idercd, iderfloppy\n";
|
|
private const string DEACTIVATE = "deactivate";
|
|
private const string ACTIVATE = "activate";
|
|
private const string POWER_ON = "on";
|
|
private const string WAKE_FROM_POWER_SAVING = "wakefromconnectedstandby";
|
|
private const string POWER_CYCLE = "powercycle";
|
|
private const string POWER_OFF = "off";
|
|
private const string MASTER_BUS_RESET = "busreset";
|
|
private const string SLEEP = "sleep";
|
|
private const string HIBERNATE = "hibernate";
|
|
private const string GRACEFUL_OFF = "gracefuloff";
|
|
private const string GRACEFUL_RESET = "gracefulreset";
|
|
private const string NMI = "nmi";
|
|
|
|
// Exit Codes Types
|
|
private enum exitCodes
|
|
{
|
|
EXIT_SUCCESS = 0,
|
|
EXIT_FAILURE,
|
|
EXIT_USAGE,
|
|
EXIT_COMMUNICATION_ERROR,
|
|
EXIT_ARGUMENT_ERROR,
|
|
}
|
|
|
|
#endregion CONSTANTS
|
|
|
|
#region MAIN
|
|
|
|
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
public static extern bool SetDefaultDllDirectories(int directoryFlags);
|
|
|
|
/// <summary>
|
|
/// This is the main entry point for the function.
|
|
/// </summary>
|
|
static int Main(string[] args)
|
|
{
|
|
// set default dll lookup directory to system
|
|
SetDefaultDllDirectories(0x00000800); //LOAD_LIBRARY_SEARCH_SYSTEM32
|
|
|
|
exitCodes exitCode = exitCodes.EXIT_SUCCESS;
|
|
CmdLineArguments Params = new CmdLineArguments();
|
|
RemoteControlApi api = null;
|
|
|
|
// Add command line argument options.
|
|
Params.init_functions();
|
|
|
|
// Add options to activate
|
|
Params.AddArg(ALL, false, false, ALL_DESCRIPTION);
|
|
Params.AddArg(GET_SYSTEM_POWER_STATE, false, false, GET_SYSTEM_POWER_STATE_DESCRIPTION);
|
|
Params.AddArg(GET_POWER_CAPABILITIES, false, false, GET_POWER_CAPABILITIES_DESCRIPTION);
|
|
Params.AddArg(CHANGE_POWER_STATE, true, false, CHANGE_POWER_STATE_DESCRIPTION);
|
|
Params.AddArg(GET_BOOT_DETAILS, false, false, GET_BOOT_DETAILS_DESCRIPTION);
|
|
Params.AddArg(GET_BOOT_CAPABILITIES, false, false, GET_BOOT_CAPABILITIES_DESCRIPTION);
|
|
Params.AddArg(SET_RSE_FLAG, true, false, SET_RSE_FLAG_DESCRIPTION);
|
|
Params.AddArg(SET_RSE_PASSWORD, true, false, SET_RSE_PASSWORD);
|
|
Params.AddArg(SET_BOOT_SOURCE, true, false, SET_BOOT_SOURCE_DESCRIPTION);
|
|
Params.AddArg(CPU_STATE, false, false, CPU_STATE_DESCRIPTION);
|
|
Params.AddArg(CLEAR_BOOT_OPTIONS, false, false, CLEAR_BOOT_OPTIONS_DESCRIPTION);
|
|
Params.AddArg(SET_RSE_NEXT_BOOT, false, false, SET_RSE_NEXT_BOOT_DESCRIPTION);
|
|
|
|
string usage = string.Empty;
|
|
string assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
|
|
|
|
// If no params were given, just print the usage
|
|
if (args.Length == 0)
|
|
{
|
|
// creates usage string
|
|
usage = Params.CreateUsage(assembly, false);
|
|
Console.WriteLine("\n" + usage);
|
|
return (int)exitCodes.EXIT_SUCCESS;//as Exit_Success = 0 ;
|
|
}
|
|
|
|
try
|
|
{
|
|
try
|
|
{
|
|
// Verify command line arguments.
|
|
Params.Parse(args);
|
|
|
|
// If Set RSE Password was inserted, validate password.
|
|
if (args[0].Trim('-').Equals(SET_RSE_PASSWORD))
|
|
{
|
|
string setRsePasswordParam = Params[SET_RSE_PASSWORD];
|
|
if (setRsePasswordParam == null || setRsePasswordParam.Length < 8 || setRsePasswordParam.Length > 32)
|
|
{
|
|
throw new Exception("Invalid Argument: RSE Password must contain between 8 to 32 characters.");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
usage = Params.CreateUsage(assembly, false);
|
|
Console.WriteLine(usage);
|
|
return 0;
|
|
|
|
}
|
|
|
|
bool verbose = Params.Selected(CmdLineArguments.OPT_VERBOSE);
|
|
|
|
//Create the WSMan Connection.
|
|
//Check if TLS option is selected or not.
|
|
if (Params.Selected(CmdLineArguments.OPT_SECURE) == false)
|
|
{
|
|
api = new RemoteControlApi(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER],
|
|
Params[CmdLineArguments.OPT_PASS], Params.Selected(CmdLineArguments.OPT_KRB), Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
|
|
}
|
|
else
|
|
{
|
|
api = new RemoteControlApi(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER],
|
|
Params[CmdLineArguments.OPT_PASS], Params[CmdLineArguments.OPT_CERT],
|
|
Params.Selected(CmdLineArguments.OPT_KRB), Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
|
|
}
|
|
|
|
// Main flow
|
|
|
|
//check for only 1 parameter
|
|
switch (args[0].Trim('-'))
|
|
{
|
|
case ALL:
|
|
api.GetSystemPowerState();
|
|
api.GetSystemPowerManagementCapabilities();
|
|
api.ChangeSystemPowerState(PowerState.On, verbose);
|
|
api.RetrieveTheBootConfigurationSettings(verbose);
|
|
api.GetBootDetails(verbose);
|
|
api.GetBootCapabilities(verbose);
|
|
api.ConfigBootSource("none", verbose);
|
|
api.GetCPUState();
|
|
break;
|
|
|
|
case GET_POWER_CAPABILITIES:
|
|
api.GetSystemPowerManagementCapabilities();
|
|
break;
|
|
|
|
case CPU_STATE:
|
|
api.GetCPUState();
|
|
break;
|
|
|
|
//call DisplayStatus option
|
|
case GET_BOOT_DETAILS:
|
|
api.GetBootDetails(verbose);
|
|
break;
|
|
|
|
//set boot source
|
|
case SET_BOOT_SOURCE:
|
|
Console.WriteLine("\nSet the boot source for the next boot");
|
|
string bootSource = Params[SET_BOOT_SOURCE];
|
|
if (bootSource == null)
|
|
throw new Exception();
|
|
api.ConfigBootSource(bootSource.ToLower(), verbose);
|
|
Console.WriteLine("Success");
|
|
break;
|
|
|
|
//display BootCapabilities Option
|
|
case GET_BOOT_CAPABILITIES:
|
|
api.GetBootCapabilities(verbose);
|
|
break;
|
|
|
|
//change power state
|
|
case CHANGE_POWER_STATE:
|
|
Console.WriteLine("\nChange the power state of the Intel AMT machine");
|
|
string requestedPowerState = Params[CHANGE_POWER_STATE];
|
|
if (requestedPowerState == null)
|
|
throw new Exception();
|
|
requestedPowerState = requestedPowerState.ToLower();
|
|
PowerState powerState;
|
|
switch (requestedPowerState)
|
|
{
|
|
case POWER_ON:
|
|
powerState = PowerState.On;
|
|
break;
|
|
case WAKE_FROM_POWER_SAVING:
|
|
powerState = PowerState.WakeFromPowerSaving;
|
|
break;
|
|
case POWER_OFF:
|
|
powerState = PowerState.SoftOff;
|
|
break;
|
|
case POWER_CYCLE:
|
|
powerState = PowerState.SoftPowerCycle;
|
|
break;
|
|
case MASTER_BUS_RESET:
|
|
powerState = PowerState.MasterBusReset;
|
|
break;
|
|
case SLEEP:
|
|
powerState = PowerState.DeepSleep;
|
|
break;
|
|
case HIBERNATE:
|
|
powerState = PowerState.Hibernate;
|
|
break;
|
|
case GRACEFUL_RESET:
|
|
powerState = PowerState.GracefulReset;
|
|
break;
|
|
case GRACEFUL_OFF:
|
|
powerState = PowerState.GracefulOff;
|
|
break;
|
|
case NMI:
|
|
powerState = PowerState.DiagnosticInterrupt;
|
|
break;
|
|
default:
|
|
throw new ArgumentException("Power state is not a valid power state. " +
|
|
"Please check the usage for valid options");
|
|
}
|
|
uint result = api.ChangeSystemPowerState(powerState, verbose);
|
|
if (result == 0)
|
|
Console.WriteLine("\n Success - Power state change performed.. ");
|
|
break;
|
|
|
|
case GET_SYSTEM_POWER_STATE:
|
|
api.GetSystemPowerState();
|
|
break;
|
|
|
|
case SET_RSE_FLAG:
|
|
Console.WriteLine("\nSet the RSE for the next boot");
|
|
bool rse;
|
|
string set_rse_flag = Params[SET_RSE_FLAG];
|
|
if (set_rse_flag == null)
|
|
throw new Exception();
|
|
bool succ = Boolean.TryParse(set_rse_flag.ToLower(), out rse);
|
|
if (!succ)
|
|
Params.MessageDisplay_Color("RSE boot flag value need to be Boolean.", ConsoleColor.Red);
|
|
else
|
|
api.SetRSE(rse, verbose);
|
|
break;
|
|
|
|
case CLEAR_BOOT_OPTIONS:
|
|
Console.WriteLine("\nClearing all boot options");
|
|
api.ClearBootOptions(verbose);
|
|
break;
|
|
|
|
case SET_RSE_PASSWORD:
|
|
Console.WriteLine("\nSetting RSE password");
|
|
api.SetRSEPassword(Params[SET_RSE_PASSWORD], verbose);
|
|
break;
|
|
|
|
case SET_RSE_NEXT_BOOT:
|
|
Console.WriteLine("\nSetting RSE for the next boot");
|
|
api.setRSENextBoot(verbose);
|
|
break;
|
|
default:
|
|
Console.WriteLine("No option was selected");
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Params.MessageDisplay_Color("\nFailed!!!", ConsoleColor.Red);
|
|
//Check for the Type of Exception created during execution.
|
|
exitCode = (exitCodes)Params.catchType(e, null);
|
|
}
|
|
finally
|
|
{
|
|
api?.Dispose();
|
|
}
|
|
|
|
return (int)exitCode;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|