optimize bcd processing

This commit is contained in:
a1ive 2025-02-16 13:45:52 +09:00
parent 9ff52bf73a
commit ffa2dc1469
No known key found for this signature in database
GPG Key ID: DA9BACF4F462B55D
6 changed files with 116 additions and 54 deletions

57
bcd.c
View File

@ -69,6 +69,24 @@ bcd_find_hive (hive_t *hive, HKEY objects,
return data;
}
static void
bcd_delete_key (hive_t *hive, HKEY objects,
const wchar_t *guid, const wchar_t *keyname)
{
HKEY entry, elements, key;
if (reg_find_key (hive, objects, guid, &entry) != REG_ERR_NONE)
die ("Can't find HKEY %ls\n", guid);
if (reg_find_key (hive, entry, BCD_REG_HKEY, &elements)
!= REG_ERR_NONE)
die ("Can't find HKEY %ls\n", BCD_REG_HKEY);
if (reg_find_key (hive, elements, keyname, &key)
!= REG_ERR_NONE)
die ("Can't find HKEY %ls\n", keyname);
if (reg_delete_key (hive, elements, key)
!= REG_ERR_NONE)
die ("Can't delete HKEY %ls\n", keyname);
}
static inline void
bcd_patch_bool (hive_t *hive, HKEY objects,
const wchar_t *guid, const wchar_t *keyname,
@ -134,7 +152,6 @@ bcd_patch_dp (hive_t *hive, HKEY objects, uint32_t boottype,
uint8_t *data;
uint32_t len, ofs;
uint8_t sdi[] = GUID_BIN_RAMDISK;
uint8_t rdi[] = GUID_BIN_RDIMAGE;
data = bcd_find_hive (hive, objects, guid, keyname, &len);
memset (data, 0, len);
switch (boottype)
@ -145,10 +162,7 @@ bcd_patch_dp (hive_t *hive, HKEY objects, uint32_t boottype,
if (len < 0x028a)
die ("WIM device path (%ls->%ls) length error (%x)\n",
guid, keyname, len);
if (boottype == NTBOOT_RAM)
memcpy (data + 0x0000, rdi, sizeof (rdi));
else
memcpy (data + 0x0000, sdi, sizeof (sdi));
memcpy (data + 0x0000, sdi, sizeof (sdi));
data[0x0014] = 0x01;
data[0x0018] = 0x7a; data[0x0019] = 0x02; // len - 0x10
data[0x0020] = 0x03;
@ -262,10 +276,18 @@ bcd_patch_data (void)
BCDOPT_SYSROOT, BCD_DEFAULT_HIBERFIL); // hiberfil
/* Patch Objects->{Ramdisk} */
bcd_patch_bool (&hive, objects, GUID_RAMD,
BCDOPT_EXPORTCD, nt_cmdline->exportcd);
bcd_patch_u64 (&hive, objects, GUID_RAMD,
BCDOPT_IMGOFS, nt_cmdline->imgofs);
if (nt_cmdline->boottype == NTBOOT_RAM)
{
bcd_patch_bool (&hive, objects, GUID_RAMDISK,
BCDOPT_EXPORTCD, nt_cmdline->exportcd);
bcd_patch_u64 (&hive, objects, GUID_RAMDISK,
BCDOPT_IMGOFS, nt_cmdline->imgofs);
}
else
{
bcd_delete_key (&hive, objects, GUID_RAMDISK, BCDOPT_EXPORTCD);
bcd_delete_key (&hive, objects, GUID_RAMDISK, BCDOPT_IMGOFS);
}
/* Patch Objects->{Options} */
bcd_patch_sz (&hive, objects, GUID_OPTN,
@ -288,29 +310,28 @@ bcd_patch_data (void)
/* Patch Objects->{Resolution} */
if (nt_cmdline->hires == NTARG_BOOL_NA)
{
bcd_patch_szw (&hive, objects, GUID_OPTN,
BCDOPT_INHERIT, GUID_LRES);
bcd_patch_u64 (&hive, objects, GUID_LRES,
bcd_delete_key (&hive, objects, GUID_OPTN, BCDOPT_HIGHRES);
bcd_patch_u64 (&hive, objects, GUID_OPTN,
BCDOPT_GFXMODE, nt_cmdline->gfxmode);
}
else
{
bcd_patch_bool (&hive, objects, GUID_HRES,
bcd_delete_key (&hive, objects, GUID_OPTN, BCDOPT_GFXMODE);
bcd_patch_bool (&hive, objects, GUID_OPTN,
BCDOPT_HIGHRES, nt_cmdline->hires);
}
/* Patch Objects->{SafeBoot} */
if (nt_cmdline->safemode)
{
bcd_patch_u64 (&hive, objects, GUID_SAFE,
bcd_patch_u64 (&hive, objects, GUID_OPTN,
BCDOPT_SAFEMODE, nt_cmdline->safeboot);
bcd_patch_bool (&hive, objects, GUID_SAFE,
bcd_patch_bool (&hive, objects, GUID_OPTN,
BCDOPT_ALTSHELL, nt_cmdline->altshell);
}
else
{
bcd_patch_szw (&hive, objects, entry_guid,
BCDOPT_INHERIT, GUID_OPTN);
bcd_delete_key (&hive, objects, GUID_OPTN, BCDOPT_SAFEMODE);
bcd_delete_key (&hive, objects, GUID_OPTN, BCDOPT_ALTSHELL);
}
/* Patch Objects->{Entry} */

View File

@ -28,12 +28,6 @@
#define GUID_HIBR L"{19260817-6666-8888-abcd-000000000003}"
#define GUID_OPTN L"{19260817-6666-8888-abcd-100000000000}"
#define GUID_SAFE L"{19260817-6666-8888-abcd-200000000000}"
#define GUID_HRES L"{19260817-6666-8888-abcd-110000000000}"
#define GUID_LRES L"{19260817-6666-8888-abcd-120000000000}"
#define GUID_RAMD L"{19260817-6666-8888-abcd-101000000000}"
#define GUID_BOOTMGR L"{9dea862c-5cdd-4e70-acc1-f32b344d4795}"
#define GUID_RAMDISK L"{ae5534e0-a924-466c-b836-758539a3ee3a}"
@ -48,12 +42,6 @@
0xb8, 0x36, 0x75, 0x85, 0x39, 0xa3, 0xee, 0x3a \
}
#define GUID_BIN_RDIMAGE \
{ \
0x17, 0x08, 0x26, 0x19, 0x66, 0x66, 0x88, 0x88, \
0xab, 0xcd, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00 \
}
#define BCD_REG_ROOT L"Objects"
#define BCD_REG_HKEY L"Elements"
#define BCD_REG_HVAL L"Element"

View File

@ -179,6 +179,9 @@ reg_err_t
reg_enum_keys(hive_t *h, HKEY Key, uint32_t Index,
wchar_t *Name, uint32_t NameLength);
reg_err_t
reg_delete_key(hive_t *h, HKEY parent, HKEY key);
reg_err_t
reg_find_key(hive_t *h, HKEY Parent, const wchar_t *Path, HKEY *Key);

65
reg.c
View File

@ -445,6 +445,71 @@ reg_find_key(hive_t *h, HKEY Parent, const wchar_t *Path, HKEY *Key)
} while (true);
}
reg_err_t
reg_delete_key(hive_t *h, HKEY parent, HKEY key)
{
int32_t size;
CM_KEY_NODE *nk;
CM_KEY_FAST_INDEX *lh;
// find parent key node
size = get_int32_size (h->data, parent);
if (size < 0)
return REG_ERR_FILE_NOT_FOUND;
if ((uint32_t)size < sizeof(int32_t) + offsetof(CM_KEY_NODE, Name[0]))
return REG_ERR_BAD_ARGUMENT;
nk = (CM_KEY_NODE *)((uint8_t *)h->data + parent + sizeof(int32_t));
if (nk->Signature != CM_KEY_NODE_SIGNATURE)
return REG_ERR_BAD_ARGUMENT;
if ((uint32_t)size < sizeof(int32_t) + offsetof(CM_KEY_NODE,
Name[0]) + nk->NameLength)
return REG_ERR_BAD_ARGUMENT;
if (nk->SubKeyCount == 0 || nk->SubKeyList == 0xffffffff)
return REG_ERR_FILE_NOT_FOUND;
// go to key index
size = get_int32_size (h->data, 0x1000 + nk->SubKeyList);
if (size < 0)
return REG_ERR_FILE_NOT_FOUND;
if ((uint32_t)size < sizeof(int32_t) + offsetof(CM_KEY_FAST_INDEX, List[0]))
return REG_ERR_BAD_ARGUMENT;
lh = (CM_KEY_FAST_INDEX *)((uint8_t *)h->data + 0x1000
+ nk->SubKeyList + sizeof(int32_t));
if (lh->Signature != CM_KEY_HASH_LEAF && lh->Signature != CM_KEY_FAST_LEAF)
return REG_ERR_BAD_ARGUMENT;
if ((uint32_t)size < sizeof(int32_t) + offsetof(CM_KEY_FAST_INDEX,
List[0]) + (lh->Count * sizeof(CM_INDEX)))
return REG_ERR_BAD_ARGUMENT;
for (unsigned int i = 0; i < lh->Count; i++)
{
// key = 0x1000 + lh->List[i].Cell;
if (key != 0x1000 + lh->List[i].Cell)
continue;
lh->Count--;
for (unsigned j = i; j < lh->Count; j++)
memcpy (&lh->List[j].Cell, &lh->List[j + 1].Cell,
sizeof (CM_INDEX));
nk->SubKeyCount--;
return REG_ERR_NONE;
}
return REG_ERR_FILE_NOT_FOUND;
}
reg_err_t
reg_enum_values(hive_t *h, HKEY Key,
uint32_t Index, wchar_t *Name,

View File

@ -10,12 +10,6 @@ set guidwin={19260817-6666-8888-abcd-000000000002}
set guidhib={19260817-6666-8888-abcd-000000000003}
set guidopt={19260817-6666-8888-abcd-100000000000}
set guidsbt={19260817-6666-8888-abcd-200000000000}
set guidhir={19260817-6666-8888-abcd-110000000000}
set guidlor={19260817-6666-8888-abcd-120000000000}
set guidram={19260817-6666-8888-abcd-101000000000}
set winload=\WINLOAD0000000000000000000000000000000000000000000000000000000
set windir=\WINDIR000000000000000000000000
@ -42,12 +36,6 @@ bcdedit %bcd% /set {globalsettings} locale en-us
bcdedit %bcd% /create {bootloadersettings}
bcdedit %bcd% /create %guidlor% /inherit OSLOADER
bcdedit %bcd% /set %guidlor% graphicsresolution 1024x768
bcdedit %bcd% /create %guidhir% /inherit OSLOADER
bcdedit %bcd% /set %guidhir% highestmode true
bcdedit %bcd% /create %guidopt% /inherit OSLOADER
bcdedit %bcd% /set %guidopt% detecthal true
bcdedit %bcd% /set %guidopt% winpe true
@ -62,33 +50,30 @@ REM bcdedit %bcd% /set %guidopt% integrityservices disable
REM bcdedit %bcd% /set %guidopt% isolatedcontext true
REM bcdedit %bcd% /set %guidopt% bootmenupolicy Legacy
bcdedit %bcd% /set %guidopt% loadoptions %wincmd%
bcdedit %bcd% /set %guidopt% inherit %guidhir%
bcdedit %bcd% /create %guidsbt% /inherit OSLOADER
bcdedit %bcd% /set %guidsbt% safeboot minimal
bcdedit %bcd% /set %guidsbt% safebootalternateshell yes
bcdedit %bcd% /set %guidopt% safeboot minimal
bcdedit %bcd% /set %guidopt% safebootalternateshell yes
bcdedit %bcd% /set %guidopt% graphicsresolution 1024x768
bcdedit %bcd% /set %guidopt% highestmode true
bcdedit %bcd% /create {ramdiskoptions}
bcdedit %bcd% /set {ramdiskoptions} ramdisksdidevice "boot"
bcdedit %bcd% /set {ramdiskoptions} ramdisksdipath \boot\boot.sdi
bcdedit %bcd% /create %guidram% /device
bcdedit %bcd% /set %guidram% ramdiskimageoffset 65536
bcdedit %bcd% /set %guidram% exportascd yes
bcdedit %bcd% /set {ramdiskoptions} ramdiskimageoffset 65536
bcdedit %bcd% /set {ramdiskoptions} exportascd yes
bcdedit %bcd% /create %guidwim% /d "NT6+ WIM" /application OSLOADER
bcdedit %bcd% /set %guidwim% device ramdisk=[C:]%wimfile%,{ramdiskoptions}
bcdedit %bcd% /set %guidwim% osdevice ramdisk=[C:]%wimfile%,{ramdiskoptions}
bcdedit %bcd% /set %guidwim% systemroot %windir%
bcdedit %bcd% /set %guidwim% path %winload%
bcdedit %bcd% /set %guidwim% inherit %guidopt% %guidsbt%
bcdedit %bcd% /set %guidwim% inherit %guidopt%
bcdedit %bcd% /create %guidvhd% /d "NT6+ VHD" /application OSLOADER
bcdedit %bcd% /set %guidvhd% device vhd=[C:]%vhdfile%
bcdedit %bcd% /set %guidvhd% osdevice vhd=[C:]%vhdfile%
bcdedit %bcd% /set %guidvhd% systemroot %windir%
bcdedit %bcd% /set %guidvhd% path %winload%
bcdedit %bcd% /set %guidvhd% inherit %guidopt% %guidsbt%
bcdedit %bcd% /set %guidvhd% inherit %guidopt%
bcdedit %bcd% /create {resumeloadersettings}
@ -104,7 +89,7 @@ bcdedit %bcd% /set %guidwin% device partition=C:
bcdedit %bcd% /set %guidwin% osdevice partition=C:
bcdedit %bcd% /set %guidwin% systemroot %windir%
bcdedit %bcd% /set %guidwin% path %winload%
bcdedit %bcd% /set %guidwin% inherit %guidopt% %guidsbt%
bcdedit %bcd% /set %guidwin% inherit %guidopt%
bcdedit %bcd% /set %guidwin% resumeobject %guidhib%
bcdedit %bcd% /set {bootmgr} displayorder %guidwim% /addlast

Binary file not shown.