Exemple #1
0
 static void Remove(BcdObject o, uint t)
 {
     if (!o.DeleteElement(t))
     {
         throw new InvalidOperationException();
     }
 }
        public static bool ChangeOSEntryDescription(string guid, string newDescription, string storePath)
        {
            BcdObject nonEmbeddedObjRef = new BcdObject(BcdStore_API.ImpersonationScope, guid, storePath);

            bool setDescripStatus = nonEmbeddedObjRef.SetStringElement(newDescription, Constants.BCDE_LIBRARY_TYPE_DESCRIPTION);

            return(setDescripStatus);
        }
Exemple #3
0
        public void AddElement_WrongType()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Assert.Throws <ArgumentException>(() => obj.AddElement(WellKnownElement.LibraryApplicationDevice, ElementValue.ForString(@"\a\path\to\nowhere")));
        }
Exemple #4
0
        public void FriendlyName()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Assert.Equal(obj.Identity.ToString("B"), obj.FriendlyName);
        }
Exemple #5
0
        public void RemoveElement_NonExistent()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            obj.RemoveElement(WellKnownElement.LibraryApplicationPath);
        }
Exemple #6
0
        public void RemoveObject()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);

            BcdObject obj = s.CreateInherit(InheritType.AnyObject);

            s.RemoveObject(obj.Identity);
        }
        /// <summary>
        /// Adds the passed in (valid store) GUID to the end of the current OS entry display list for store.
        /// </summary>
        /// <param name="newGuid"></param>
        /// <param name="storePath"></param>
        /// <param name="errorDetails"></param>
        /// <returns></returns>
        public static bool AddGuidToOSDisplayList(string newGuid, string storePath, out string errorDetails)
        {
            // optional additional info on any errors encountered, sent back as output variable
            errorDetails = string.Empty;             // default to empty string ( no errors encountered)

            BcdObject bcdBootMgr = new BcdObject(BcdStore_API.ImpersonationScope, Constants.GUID_WINDOWS_BOOTMGR, storePath);

            ManagementBaseObject mboListElements;
            bool successGetDisplayOrderObj = bcdBootMgr.GetElement(Constants.BCDE_BOOTMGR_TYPE_DISPLAY_ORDER,
                                                                   out mboListElements);

            if (!successGetDisplayOrderObj)
            {
                errorDetails = "Trouble getting boot manager display order object";
                return(false);
            }
            else
            {
                BcdObjectListElement listElement   = new BcdObjectListElement(mboListElements);
                string[]             osDisplayList = listElement.Ids;

                int      len = osDisplayList.Length;
                string[] newOsDisplayList = new string[len + 1];
                for (int i = 0; i < len; i++)
                {
                    newOsDisplayList[i] = osDisplayList[i];
                }

                newOsDisplayList[len] = newGuid;

                //if (_updateGuidListViaObject)
                //{
                //    // this code needs research to figure out why it's not working:

                //    //listElement.AutoCommit = true;
                //    //listElement.Ids = newOsDisplayList;
                //    //listElement.CommitObject();

                //    // DOES NOT WORK, error in WMI classes?
                //}
                //else
                //{

                // save new display list via bcdbootmgr object's SetObjectListElement method.
                bool setElementStatus = bcdBootMgr.SetObjectListElement(newOsDisplayList, Constants.BCDE_BOOTMGR_TYPE_DISPLAY_ORDER);
                if (!setElementStatus)
                {
                    errorDetails = "Trouble setting new display order list via SetObjectListElement call";
                    return(false);
                }

                //}
            }

            return(true);
        }
Exemple #8
0
        public void FriendlyName()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Element el = obj.AddElement(WellKnownElement.LibraryApplicationPath, ElementValue.ForString(@"a\path\to\nowhere"));

            Assert.Equal("{path}", el.FriendlyName);
        }
Exemple #9
0
        public void RemoveElement()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            obj.AddElement(WellKnownElement.LibraryApplicationPath, ElementValue.ForString(@"\a\path\to\nowhere"));
            obj.RemoveElement(WellKnownElement.LibraryApplicationPath);

            Assert.False(obj.HasElement(WellKnownElement.LibraryApplicationPath));
        }
        public void BooleanValue()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Element el = obj.AddElement(WellKnownElement.LibraryAutoRecoveryEnabled, ElementValue.ForBoolean(true));

            el = obj.GetElement(WellKnownElement.LibraryAutoRecoveryEnabled);

            Assert.Equal(true.ToString(), el.Value.ToString());
        }
        public void IntegerValue()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Element el = obj.AddElement(WellKnownElement.LibraryTruncatePhysicalMemory, ElementValue.ForInteger(1234));

            el = obj.GetElement(WellKnownElement.LibraryTruncatePhysicalMemory);

            Assert.Equal("1234", el.Value.ToString());
        }
Exemple #12
0
 public BCDComboBoxItem(BcdObject obj)
 {
     if (obj != null)
     {
         bcdObj = obj;
         BcdElement element = bcdObj.elementsDict[(uint)BuiltinElementType.BcdLibraryString_Description];
         if (element is BcdStringElement)
         {
             ItemName = ((BcdStringElement)element).StringValue;
         }
     }
 }
        public void IntegerListValue()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Element el = obj.AddElement(WellKnownElement.LibraryBadMemoryList, ElementValue.ForIntegerList(new long[] { 1234, 4132 }));

            el = obj.GetElement(WellKnownElement.LibraryBadMemoryList);

            Assert.NotNull(el.Value.ToString());
            Assert.NotEmpty(el.Value.ToString());
        }
        public void DeviceValue_BootDevice()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Element el = obj.AddElement(WellKnownElement.LibraryApplicationDevice, ElementValue.ForBootDevice());

            el = obj.GetElement(WellKnownElement.LibraryApplicationDevice);

            Assert.NotNull(el.Value.ToString());
            Assert.NotEmpty(el.Value.ToString());
        }
        public void GuidValue()
        {
            Guid testGuid = Guid.NewGuid();

            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Element el = obj.AddElement(WellKnownElement.BootMgrDefaultObject, ElementValue.ForGuid(testGuid));

            el = obj.GetElement(WellKnownElement.BootMgrDefaultObject);

            Assert.Equal(testGuid.ToString("B"), el.Value.ToString());
        }
Exemple #16
0
        public void CreateDevice()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);

            BcdObject obj = s.CreateDevice();

            Assert.NotEqual(Guid.Empty, obj.Identity);

            Assert.Equal(ObjectType.Device, obj.ObjectType);

            BcdObject reGet = s.GetObject(obj.Identity);

            Assert.Equal(obj.Identity, reGet.Identity);
        }
        public void GuidListValue()
        {
            Guid testGuid1 = Guid.NewGuid();
            Guid testGuid2 = Guid.NewGuid();

            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Element el = obj.AddElement(WellKnownElement.BootMgrDisplayOrder, ElementValue.ForGuidList(new Guid[] { testGuid1, testGuid2 }));

            el = obj.GetElement(WellKnownElement.BootMgrDisplayOrder);

            Assert.Equal(testGuid1.ToString("B") + "," + testGuid2.ToString("B"), el.Value.ToString());
        }
Exemple #18
0
        public void CreateApplication()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);

            BcdObject obj = s.CreateApplication(ApplicationImageType.WindowsBoot, ApplicationType.BootManager);

            Assert.NotEqual(Guid.Empty, obj.Identity);

            Assert.Equal(ObjectType.Application, obj.ObjectType);

            BcdObject reGet = s.GetObject(obj.Identity);

            Assert.Equal(obj.Identity, reGet.Identity);
        }
        public static string GetDescriptionForGuid(string guid, string storePath)
        {
            string descripFound = null;

            BcdObject nonEmbeddedObjRef = new BcdObject(BcdStore_API.ImpersonationScope, guid, storePath);

            System.Management.ManagementBaseObject Element;

            bool getDescripStatus = nonEmbeddedObjRef.GetElement(Constants.BCDE_LIBRARY_TYPE_DESCRIPTION, out Element);

            if (getDescripStatus)
            {
                descripFound = Element.GetPropertyValue("String").ToString();
            }

            return(descripFound);
        }
Exemple #20
0
        public void CreateInherit()
        {
            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);

            BcdObject obj = s.CreateInherit(InheritType.ApplicationObjects);

            Assert.NotEqual(Guid.Empty, obj.Identity);

            Assert.Equal(ObjectType.Inherit, obj.ObjectType);

            Assert.True(obj.IsInheritableBy(ObjectType.Application));
            Assert.False(obj.IsInheritableBy(ObjectType.Device));

            BcdObject reGet = s.GetObject(obj.Identity);

            Assert.Equal(obj.Identity, reGet.Identity);
        }
        public static List <string> GetBootManagerGUIDEntriesDisplayListAsList(string storePath)
        {
            List <string> guidList = null;

            BcdObject bcdObject = new BcdObject(BcdStore_API.ImpersonationScope, Constants.GUID_WINDOWS_BOOTMGR, storePath);

            ManagementBaseObject mboOut;
            bool success = bcdObject.GetElement(Constants.BCDE_BOOTMGR_TYPE_DISPLAY_ORDER, out mboOut);

            if (success)
            {
                string[] oSList = (string[])mboOut.GetPropertyValue("Ids");
                guidList = new List <string>();
                foreach (string s in oSList)
                {
                    guidList.Add(s);
                }
            }
            return(guidList);
        }
        public void DeviceValue_Mbr()
        {
            SparseMemoryStream ms = new SparseMemoryStream();

            ms.SetLength(80 * 1024 * 1024);
            BiosPartitionTable pt = BiosPartitionTable.Initialize(ms, Geometry.FromCapacity(ms.Length));

            pt.Create(WellKnownPartitionType.WindowsNtfs, true);
            VolumeManager volMgr = new VolumeManager(ms);

            RegistryHive hive = RegistryHive.Create(new MemoryStream());
            Store        s    = Store.Initialize(hive.Root);
            BcdObject    obj  = s.CreateInherit(InheritType.AnyObject);

            Element el = obj.AddElement(WellKnownElement.LibraryApplicationDevice, ElementValue.ForDevice(Guid.Empty, volMgr.GetPhysicalVolumes()[0]));

            el = obj.GetElement(WellKnownElement.LibraryApplicationDevice);

            Assert.NotNull(el.Value.ToString());
            Assert.NotEmpty(el.Value.ToString());
        }
        public static bool CloneExistingOSEntry(string osEntryToCloneGuid, string newOSEntryDescriptionName, string storePath, out string errorDetails)
        {
            errorDetails = string.Empty;             // default error string to empty string/no errors
            ROOT.WMI.BcdStore bcdStore = new ROOT.WMI.BcdStore(BcdStore_API.ImpersonationScope, storePath);
            string            newGuid  = "{" + Guid.NewGuid().ToString() + "}";

            ManagementBaseObject newObject;

            bool success = bcdStore.CopyObject(Constants.BCD_COPY_CREATE_NEW_OBJECT_IDENTIFIER, osEntryToCloneGuid, storePath, out newObject);

            if (!success)
            {
                errorDetails = "failure with bcdstore copy object call";
                return(false);
            }

            BcdObject newBcdObject = new BcdObject(newObject);
            string    newGuid2     = newBcdObject.Id.ToString();

            if (success)
            {
                string errorString;
                bool   successfulAddToOsList = AddGuidToOSDisplayList(newGuid2, storePath, out errorString);
                if (!successfulAddToOsList)
                {
                    errorDetails = errorString;
                    return(false);
                }

                bool setDescripStatus = ChangeOSEntryDescription(newBcdObject.Id, newOSEntryDescriptionName, storePath);

                if (!setDescripStatus)
                {
                    errorDetails = "failure with setting new OS entry description, otherwise success";
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// Overwrite the given bcd store's OS Display List (what displays on system load) with the given string array of guids.
        /// Note this is very powerful make sure you know what you are doing.
        /// </summary>
        /// <param name="arrNewGuidArray"></param>
        /// <param name="storePath"></param>
        /// <param name="errorDetails"></param>
        /// <returns></returns>
        public static bool SetOSDisplayListGuids(string[] arrNewGuidArray, string storePath, out string errorDetails)
        {
            // optional additional info on any errors encountered, sent back as output variable
            errorDetails = string.Empty;             // default to empty string ( no errors encountered)

            if (arrNewGuidArray.Length == 0)
            {
                errorDetails = "This will delete ALL OS displayed list items, effectively making system boot NON-FUNCTIONAL.  Disabling this functionality per this version of library...";
                return(false);
            }

            BcdObject bcdBootMgr = new BcdObject(BcdStore_API.ImpersonationScope, Constants.GUID_WINDOWS_BOOTMGR, storePath);

            ManagementBaseObject mboListElements;
            bool successGetDisplayOrderObj = bcdBootMgr.GetElement(Constants.BCDE_BOOTMGR_TYPE_DISPLAY_ORDER,
                                                                   out mboListElements);

            if (!successGetDisplayOrderObj)
            {
                errorDetails = "Trouble getting boot manager display order object";
                return(false);
            }
            else
            {
                BcdObjectListElement listElement = new BcdObjectListElement(mboListElements);

                // save new display list via bcdbootmgr object's SetObjectListElement method.
                bool setElementStatus = bcdBootMgr.SetObjectListElement(arrNewGuidArray, Constants.BCDE_BOOTMGR_TYPE_DISPLAY_ORDER);
                if (!setElementStatus)
                {
                    errorDetails = "Trouble setting new display order list via SetObjectListElement call";
                    return(false);
                }
            }

            return(true);
        }
Exemple #25
0
        static int Main(string[] args)
        {
            Console.WriteLine("TestsigningOff Copyright (C) 2010-2012  Jeffrey Bush <*****@*****.**>");
            Console.WriteLine("This program comes with ABSOLUTELY NO WARRANTY;");
            Console.WriteLine("This is free software, and you are welcome to redistribute it");
            Console.WriteLine("under certain conditions;");
            Console.WriteLine("See http://www.gnu.org/licenses/gpl.html for more details.");
            Console.WriteLine();

            ConnectionOptions opts = new ConnectionOptions();

            opts.Impersonation    = ImpersonationLevel.Impersonate;
            opts.EnablePrivileges = true;

            ManagementScope scope = new ManagementScope("root\\WMI", opts);

            // Open the system store
            BcdObject store = new BcdObject(scope, Guids.SystemStore, "");

            // Get the Bootmgr information
            string bm_name = GetString(store, BcdLibraryElementTypes.String_Description);
            bool   bm_ts   = Has(store, ID);

            // Get the default GUID
            string def = null;

            try
            {
                def = GetString(store, BcdBootMgrElementTypes.Object_DefaultObject);
            }
            catch (Exception) {}

            // Get the current GUID
            BcdObject current = new BcdObject(scope, Guids.Current, "");
            string    cur     = current.Id;

            // Loop over all OS GUIDs
            string[]    guids = GetIds(store, BcdBootMgrElementTypes.ObjectList_DisplayOrder);
            BcdObject[] objs = new BcdObject[guids.Length];
            string[]    names = new string[guids.Length];
            bool[]      testsignings = new bool[guids.Length];
            bool        any_ts = bm_ts;
            int         def_i = -1, cur_i = -1;

            for (int i = 0; i < guids.Length; i++)
            {
                objs[i]         = new BcdObject(scope, guids[i], "");
                names[i]        = GetString(objs[i], BcdLibraryElementTypes.String_Description);
                testsignings[i] = Has(objs[i], ID);
                any_ts         |= testsignings[i];
                if (guids[i].Equals(def))
                {
                    def_i = i;
                }
                if (guids[i].Equals(cur))
                {
                    cur_i = i;
                }
            }

            // Final message
            if (any_ts)
            {
                string msg = "You have " + NAME + " entries for the following:\n";
                if (bm_ts)
                {
                    msg += "  {bootmgr} " + bm_name + "\n";
                }
                if (cur_i >= 0 && testsignings[cur_i])
                {
                    msg += "  {current} " + names[cur_i] + "\n";
                }
                if (def_i >= 0 && cur_i != def_i && testsignings[def_i])
                {
                    msg += "  {default} " + names[def_i] + "\n";
                }
                for (int i = 0; i < guids.Length; i++)
                {
                    if (i != cur_i && i != def_i && testsignings[i])
                    {
                        msg += "  " + guids[i] + " " + names[i] + "\n";
                    }
                }
                msg += "If these were added with a previous version of this program you should remove them. Would you like to remove " + NAME + "? [Y/n] ";
                Console.WriteLine(msg);

                // y/n input
                string line;
                while ((line = Console.ReadLine()) != null)
                {
                    line = line.Trim();
                    if (line.Length == 0 || line[0] == 'Y' || line[0] == 'y')
                    {
                        break;
                    }
                    else if (line[0] == 'n' || line[0] == 'N')
                    {
                        Console.WriteLine("Nothing was done.");
                        return(0);
                    }
                }

                // Set the data
                if (bm_ts)
                {
                    Remove(store, ID);
                }
                for (int i = 0; i < guids.Length; i++)
                {
                    if (testsignings[i])
                    {
                        Remove(objs[i], ID);
                    }
                }
                Console.WriteLine("Removed " + NAME + ".");
            }
            else
            {
                Console.WriteLine("Nothing has " + NAME + ".");
            }

            return(0);
        }
Exemple #26
0
 static void Remove <E>(BcdObject o, E t) where E : IConvertible
 {
     Remove(o, Convert.ToUInt32(t));
 }
Exemple #27
0
        static bool Has(BcdObject o, uint t)
        {
            ManagementBaseObject mbo;

            return(o.GetElement(t, out mbo));
        }
Exemple #28
0
 static bool Has <E>(BcdObject o, E t) where E : IConvertible
 {
     return(Has(o, Convert.ToUInt32(t)));
 }
Exemple #29
0
        static string GetString(BcdObject o, uint t)
        {
            ManagementBaseObject mbo;

            return(o.GetElement(t, out mbo) ? (string)mbo.GetPropertyValue("String") : null);
        }
Exemple #30
0
 static string GetString <E>(BcdObject o, E t) where E : IConvertible
 {
     return(GetString(o, Convert.ToUInt32(t)));
 }