Add boot device selection to MeshCmd AMTPower [PXE, HDD, CD]

Joko Sastriawan committed Mar 24, 2021 at 17:19 UTC 7744b46d639dbe32a567ba5c38b2a9c2f8fe59e0
1 file changed +92 -4
agents/meshcmd.js
+92 -4
@@ -307,8 +307,10 @@ function run(argv) {
307 console.log(' --pass [password] The Intel AMT login password.');
308 console.log('\r\nOptional arguments:\r\n');
309 console.log(' --reset, --poweron, --poweroff, --powercycle, --sleep, --hibernate');
310 - console.log(' --user [username] The Intel AMT login username, admin is default.');
311 - console.log(' --tls Specifies that TLS must be used.');
310 + console.log(' --user [username] The Intel AMT login username, admin is default.');
311 + console.log(' --tls Specifies that TLS must be used.');
312 + console.log(' --bootdevice [pxe|hdd|cd] Specifies the boot device to use after reset, poweron or powercycle.');
313 + console.log(' --bootindex [number] Specifies the index of boot device to use.');
314 } else if (action == 'amtnetwork') {
315 console.log('AmtNetwork is used to get/set Intel AMT network interface configuration. Example usage:\r\n\r\n meshcmd amtnetwork --host 1.2.3.4 --user admin --pass mypassword --dhcp');
316 console.log('\r\nRequired arguments:\r\n');
@@ -788,6 +790,23 @@ function run(argv) {
790 if (args.hibernate) { settings.poweraction = 7; }
791 if (args.reset) { settings.poweraction = 10; }
792 //if (settings.poweraction == 0) { console.log('No power action, specify --poweron, --sleep, --powercycle, --poweroff, --hibernate, --reset.'); exit(1); return; }
793 + // Accepted option for boot device are: pxe, hdd, cd
794 + var bootdevices = ['pxe','hdd','cd'];
795 + if (args.bootdevice) {
796 + console.log()
797 + if (bootdevices.indexOf(args.bootdevice.toLowerCase())>=0) {
798 + settings.bootdevice = args.bootdevice
799 + // set bootindex to 0 by default, unless overriden
800 + settings.bootindex = 0
801 + } else {
802 + console.log('Supported boot devices are pxe, hdd, cd'); exit(1); return;
803 + }
804 + }
805 + // boot index for cd and hdd
806 + if (args.bootindex && args.bootindex >=0) {
807 + settings.bootindex = args.bootindex;
808 + }
809 +
810 performAmtPowerAction();
811 } else {
812 console.log('Invalid \"action\" specified.'); exit(1); return;
@@ -2706,14 +2725,83 @@ function performAmtPowerAction() {
2725 wsstack = new wsman(transport, settings.hostname, settings.tls ? 16993 : 16992, settings.username, settings.password, settings.tls);
2726 amtstack = new amt(wsstack);
2727 if (settings.poweraction != 0) {
2709 - // Set the power state
2710 - amtstack.RequestPowerStateChange(settings.poweraction, performAmtPowerActionEx);
2728 + // check if there is bootdevice and the command is either poweron, powercycle or reset
2729 + if (settings.bootdevice && [2,5,10].indexOf(settings.poweraction)>=0) {
2730 + // change boot order
2731 + amtstack.Get('AMT_BootSettingData', powerActionResponse1, 0, 1);
2732 + } else {
2733 + // Set the power state
2734 + amtstack.RequestPowerStateChange(settings.poweraction, performAmtPowerActionEx);
2735 + }
2736 } else {
2737 // Get the power state
2738 amtstack.Get('CIM_AssociatedPowerManagementService', performAmtPowerActionEx2, 0, 1);
2739 }
2740 }
2741
2742 +function powerActionResponse1(stack, name, response, status) {
2743 + if (status !=200) { console.log("Unable to get AMT_BootSettingData"); exit(1); return;}
2744 + var r = response.Body;
2745 + r['ConfigurationDataReset'] = false;
2746 + r['BIOSPause'] = false;
2747 + r['EnforceSecureBoot'] = false;
2748 + r['BIOSSetup'] = false;
2749 + if (settings.bootdevice && settings.bootdevice!='pxe') {
2750 + r['BootMediaIndex'] = settings.bootindex;
2751 + } else {
2752 + r['BootMediaIndex'] = 0;
2753 + }
2754 + r['FirmwareVerbosity'] = 0;
2755 + r['ForcedProgressEvents'] = false;
2756 + r['IDERBootDevice'] = 0;
2757 + r['LockKeyboard'] = false;
2758 + r['LockPowerButton'] = false;
2759 + r['LockResetButton'] = false;
2760 + r['LockSleepButton'] = false;
2761 + r['ReflashBIOS'] = false;
2762 + r['UseIDER'] = false;
2763 + r['UseSOL'] = false;//
2764 + r['UseSafeMode'] = false;
2765 + r['UserPasswordBypass'] = false;
2766 + if (r['SecureErase'] != null) {
2767 + r['SecureErase'] = false; // no secure erase
2768 + }
2769 + if (r['PlatformErase'] != null) {
2770 + r['PlatformErase'] = false; //disable platform erase
2771 + }
2772 + delete r['WinREBootEnabled'];
2773 + delete r['UEFILocalPBABootEnabled'];
2774 + delete r['UEFIHTTPSBootEnabled'];
2775 + delete r['SecureBootControlEnabled'];
2776 + delete r['BootguardStatus'];
2777 + delete r['OptionsCleared'];
2778 + delete r['BIOSLastStatus'];
2779 + delete r['UefiBootParametersArray'];
2780 + if (r['UefiBootNumberOfParams'] != null) r['UefiBootNumberOfParams'] = 0;
2781 + // Set the boot order to null, this is needed for some AMT versions that don't clear this automatically.
2782 + amtstack.CIM_BootConfigSetting_ChangeBootOrder(null, function (stack, name, response, status) {
2783 + if (status != 200) { console.log('PUT CIM_BootConfigSetting_ChangeBootOrder failed'); exit(1); return; }
2784 + if (response.Body['ReturnValue'] != 0) { console.log('(1) Change Boot Order returns '+ response.Body.ReturnValueStr); exit(1); return; }
2785 + amtstack.Put('AMT_BootSettingData', r, powerActionResponse2, 0, 1);
2786 + }, 0, 1);
2787 +}
2788 +
2789 +function powerActionResponse2(stack, name, response, status, tag) {
2790 + if (status != 200) { console.log('PUT AMT_BootSettingData failed.'); exit(1); return; }
2791 + amtstack.SetBootConfigRole(1, powerActionResponse3, 0, 1);
2792 +}
2793 +
2794 +function powerActionResponse3(stack, name, response, status) {
2795 + if (status != 200) { console.log('SetBootConfigRole failed.'); exit(1); return; }
2796 + var bootsources = { 'pxe' : 'Force PXE Boot', 'hdd' : 'Force Hard-drive Boot', 'cd' : 'Force CD/DVD Boot'};
2797 + var cbparam='<Address xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing">http://schemas.xmlsoap.org/ws/2004/08/addressing</Address><ReferenceParameters xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing"><ResourceURI xmlns="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_BootSourceSetting</ResourceURI><SelectorSet xmlns="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"><Selector Name="InstanceID">Intel(r) AMT: ' + bootsources[settings.bootdevice] + '</Selector></SelectorSet></ReferenceParameters>';
2798 + amtstack.CIM_BootConfigSetting_ChangeBootOrder(cbparam, function(st, nm, resp, sts) {
2799 + if (resp.Body['ReturnValue'] != 0) { console.log('(2) Change Boot Order returns '+ response.Body.ReturnValueStr); exit(1); return; }
2800 + amtstack.RequestPowerStateChange(settings.poweraction, performAmtPowerActionEx);
2801 + });
2802 +}
2803 +
2804 +
2805 function performAmtPowerActionEx(stack, name, response, status) {
2806 if (status == 200) {
2807 console.log(response.Body.ReturnValueStr.split('_').join(' '));