ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/pbcdedit/pbcdedit
(Generate patch)

Comparing pbcdedit/pbcdedit (file contents):
Revision 1.9 by root, Wed Aug 14 22:54:28 2019 UTC vs.
Revision 1.66 by root, Sun Sep 1 15:55:44 2019 UTC

17# 17#
18# You should have received a copy of the GNU General Public License 18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <https://www.gnu.org/licenses/>. 19# along with this program. If not, see <https://www.gnu.org/licenses/>.
20# 20#
21 21
22use 5.014; # numerous features needed 22use 5.016; # numerous features need 5.14, __SUB__ needs 5.16
23 23
24our $VERSION = '1.0'; 24our $VERSION = '1.4';
25our $JSON_VERSION = 1; # the versiobn of the json objects generated by this program 25our $JSON_VERSION = 3; # the version of the json objects generated by this program
26
27our $CHANGELOG = <<EOF;
28
29 - work around lsblk bug sometimes giving dos pttype for gpt partitions.
30 - bootmenupolicy in synopsis must be set to 0 for text menu.
31
321.4 Thu Aug 22 10:48:22 CEST 2019
33 - new "create" subcommand.
34 - "create" and "edit" try to save and restore ownership/permissions
35 of bcd hives when writing the new file.
36 - editorial fixes to the documentation.
37 - add mininmal hive creation example.
38
391.3 Sat Aug 17 07:04:15 CEST 2019
40 - output of pbcdedit elements --json has changed, as it didn't
41 take the reorganisation by classes fully into account.
42 - json schema bumped to 3.
43 - new "bcd-device" and "bcd-legacy-device" subcommands.
44 - implement --json option for lsblk.
45
461.2 Fri Aug 16 00:20:41 CEST 2019
47 - bcd element names now depend on the bcd object type they are in,
48 also affects "elements" output.
49 - json schema bumped to 2.
50 - new version command.
51 - numerous minor bugfixes.
52
53EOF
26 54
27=head1 NAME 55=head1 NAME
28 56
29 pbcdedit - portable boot configuration data (BCD) store editor 57 pbcdedit - portable boot configuration data (BCD) store editor
30 58
31=head1 SYNOPSIS 59=head1 SYNOPSIS
32 60
33 pbcdedit help # output manual page 61 pbcdedit help # output manual page
62 pbcdedit version # output version and changelog
63
34 pbcdedit export path/to/BCD # output BCD hive as JSON 64 pbcdedit export path/to/BCD # output BCD hive as JSON
35 pbcdedit import path/to/bcd # convert standard input to BCD hive 65 pbcdedit import path/to/BCD # convert standard input to BCD hive
36 pbcdedit edit path/to/BCD edit-instructions... 66 pbcdedit edit path/to/BCD edit-instructions...
37 67
38 pbcdedit objects # list all supported object aliases and types 68 pbcdedit objects # list all supported object aliases and types
39 pbcdedit elements # list all supported bcd element aliases 69 pbcdedit elements # list all supported bcd element aliases
40 70
71 # Example: enable text-based boot menu.
72 pbcdedit edit /my/BCD set '{default}' bootmenupolicy 0
73
74 # Example change system device to first partition containing winload.
75 pbcdedit edit /my/BCD \
76 set '{default}' device 'locate=<null>,element,path' \
77 set '{default}' osdevice 'locate=<null>,element,path'
78
79
41=head1 DESCRIPTION 80=head1 DESCRIPTION
42 81
43This program allows you to create, read and modify Boot Configuration Data 82This program allows you to create, read and modify Boot Configuration Data
44(BCD) stores used by Windows Vista and newer versions of Windows. 83(BCD) stores used by Windows Vista and newer versions of Windows.
45 84
58 97
59=item Does not rely on Windows 98=item Does not rely on Windows
60 99
61As the "portable" in the name implies, this program does not rely on 100As the "portable" in the name implies, this program does not rely on
62C<bcdedit> or other windows programs or libraries, it works on any system 101C<bcdedit> or other windows programs or libraries, it works on any system
63that supports at least perl version 5.14. 102that supports at least perl version 5.16.
64 103
65=item Decodes and encodes BCD device elements 104=item Decodes and encodes BCD device elements
66 105
67PBCDEDIT can concisely decode and encode BCD device element contents. This 106PBCDEDIT can concisely decode and encode BCD device element contents. This
68is pretty unique, and offers a lot of potential that can't be realised 107is pretty unique, and offers a lot of potential that can't be realised
75sensitive data. 114sensitive data.
76 115
77=back 116=back
78 117
79The target audience for this program is professionals and tinkerers who 118The target audience for this program is professionals and tinkerers who
80are rewady to invest time into learning how it works. It is not an easy 119are ready to invest time into learning how it works. It is not an easy
81program to use and requires patience and a good understanding of BCD data 120program to use and requires patience and a good understanding of BCD
82stores. 121stores.
83 122
84 123
85=head1 SUBCOMMANDS 124=head1 SUBCOMMANDS
86 125
87PCBEDIT expects a subcommand as first argument that tells it what to 126PBCDEDIT expects a subcommand as first argument that tells it what to
88do. The following subcommands exist: 127do. The following subcommands exist:
89 128
90=over 129=over
91 130
92=item help 131=item C<help>
93 132
94Displays the whole manuale page (this document). 133Displays the whole manual page (this document).
95 134
135=item C<version>
136
137This outputs the PBCDEDIT version, the JSON schema version it uses and the
138full log of changes.
139
96=item export F<path> 140=item C<export> F<path>
97 141
98Reads a BCD data store and writes a JSON representation of it to standard 142Reads a BCD data store and writes a JSON representation of it to standard
99output. 143output.
100 144
101The format of the data is explained later in this document. 145The format of the data is explained later in this document.
102 146
103Example: read a BCD store, modify it wiht an extenral program, write it again. 147Example: read a BCD store, modify it with an external program, write it
148again.
104 149
105 pbcdedit export BCD | modify-json-somehow | pbcdedit import BCD 150 pbcdedit export BCD | modify-json-somehow | pbcdedit import BCD
106 151
107=item import F<path> 152=item C<import> F<path>
108 153
109The reverse of C<export>: Reads a JSON representation of a BCD data store 154The reverse of C<export>: Reads a JSON representation of a BCD data store
110from standard input, and creates or replaces the given BCD data store. 155from standard input, and creates or replaces the given BCD data store.
111 156
112=item edit F<path> instructions... 157=item C<edit> F<path> I<instructions...>
113 158
114Load a BCD data store, apply some instructions to it, and save it again. 159Load a BCD data store, apply some instructions to it, and save it again.
115 160
116See the section L<EDITING BCD DATA STORES>, below, for more info. 161See the section L<EDITING BCD STORES>, below, for more info.
117 162
118=item parse F<path> instructions... 163=item C<parse> F<path> I<instructions...>
119 164
120Same as C<edit>, above, except it doesn't save the data store again. Can 165Same as C<edit>, above, except it doesn't save the data store again. Can
121be useful to extract some data from it. 166be useful to extract some data from it.
122 167
123=item lsblk 168=item C<create> F<path> I<instructions...>
169
170Same as C<edit>, above, except it creates a new data store from scratch if
171needed. An existing store will be emptied completely.
172
173=item C<lsblk> [C<--json>]
124 174
125On a GNU/Linux system, you can get a list of partition device descriptors 175On a GNU/Linux system, you can get a list of partition device descriptors
126using this command - the external C<lsblk> command is required, as well as 176using this command - the external C<lsblk> command is required, as well as
127a mounted C</sys> file system. 177a mounted C</sys> file system.
128 178
129The output will be a list of all partitions in the system and C<partition> 179The output will be a list of all partitions in the system and C<partition>
130descriptors for GPT and both C<legacypartition> and C<partition> 180descriptors for GPT and both C<legacypartition> and C<partition>
131descritpors for MBR partitions. 181descriptors for MBR partitions.
132 182
183With C<--json> it will print similar information as C<lsblk --json>, but
184with extra C<bcd_device> and C<bcd_legacy_device> attributes.
185
186=item C<bcd-device> F<path>
187
188Tries to find the BCD device element for the given device, which currently
189must be a a partition of some kind. Prints the C<partition=> descriptor as
190a result, or nothing. Exit status will be true on success, and false on
191failure.
192
193Like C<lsblk>, above, this likely only works on GNU/Linux systems.
194
195Example: print the partition descriptor of tghe partition with label DATA.
196
197 $ pbcdedit bcd-device /dev/disk/by-label/DATA
198 partition=<null>,harddisk,mbr,47cbc08a,213579202560
199
200=item C<bcd-legacy-device> F<path>
201
202Like above, but uses a C<legacypartition> descriptor instead.
203
133=item objects [--json] 204=item C<objects> [C<--json>]
134 205
135Outputs two tables: a table listing all type aliases with their hex bcd 206Outputs two tables: a table listing all type aliases with their hex BCD
136element ID, and all object name aliases with their GUID and default type 207element ID, and all object name aliases with their GUID and default type
137(if any). 208(if any).
138 209
139With C<--json> it prints similar information as a JSON object, for easier parsing. 210With C<--json> it prints similar information as a JSON object, for easier parsing.
140 211
141=item elements [--json] 212=item C<elements> [C<--json>]
142 213
143Outputs a table of known element aliases with their hex ID and the format 214Outputs a table of known element aliases with their hex ID and the format
144type. 215type.
145 216
146With C<--json> it prints similar information as a JSON object, for easier parsing. 217With C<--json> it prints similar information as a JSON object, for easier parsing.
147 218
148=item export-regf F<path> 219=item C<export-regf> F<path>
149 220
150This has nothing to do with BCD data stores - it takes a registry hive 221This has nothing to do with BCD stores, but simply exposes PCBEDIT's
222internal registry hive reader - it takes a registry hive file as argument
151file as argument and outputs a JSON representation of it to standard 223and outputs a JSON representation of it to standard output.
152output.
153 224
154Hive versions 1.2 till 1.6 are supported. 225Hive versions 1.2 till 1.6 are supported.
155 226
156=item import-regf F<path> 227=item C<import-regf> F<path>
157 228
158The reverse of C<export-regf>: reads a JSON representation of a registry 229The reverse of C<export-regf>: reads a JSON representation of a registry
159hive from standard input and creates or replaces the registry hive file given as 230hive from standard input and creates or replaces the registry hive file
160argument. 231given as argument.
161 232
162The written hive will always be in a slightly modified version 1.3 233The written hive will always be in a slightly modified version 1.3
163format. It's not the format windows would generate, but it should be 234format. It's not the format windows would generate, but it should be
164understood by any conformant hive reader. 235understood by any conformant hive reader.
165 236
166Note that the representation chosen by PBCDEDIT currently throws away 237Note that the representation chosen by PBCDEDIT currently throws away
167clasname data (often used for feeble attemtps at hiding stuff by 238classname data (often used for feeble attempts at hiding stuff by
168Microsoft) and security descriptors, so if you write anything other than 239Microsoft) and security descriptors, so if you write anything other than
169a BCD hive you will most likely destroy it. 240a BCD hive you will most likely destroy it.
170 241
171=back 242=back
172 243
173 244
174=head1 BCD DATA STORE REPRESENTATION FORMAT 245=head1 BCD STORE REPRESENTATION FORMAT
175 246
176A BCD data store is represented as a JSON object with one special key, 247A BCD data store is represented as a JSON object with one special key,
177C<meta>, and one key per BCD object. That is, each BCD object becomes 248C<meta>, and one key per BCD object. That is, each BCD object becomes
178one key-value pair in the object, and an additional key called C<meta> 249one key-value pair in the object, and an additional key called C<meta>
179contains meta information. 250contains meta information.
215 } 286 }
216 287
217=head2 Minimal BCD to boot windows 288=head2 Minimal BCD to boot windows
218 289
219Experimentally I found the following BCD is the minimum required to 290Experimentally I found the following BCD is the minimum required to
220successfully boot any post-XP version of Windows (suitable C<device> and 291successfully boot any post-XP version of Windows (assuming suitable
221C<osdevice> values, of course): 292C<device> and C<osdevice> values, of course, and assuming a BIOS boot -
293for UEFI, you should use F<winload.efi> instead of F<winload.exe>):
222 294
223 { 295 {
224 "{bootmgr}" : { 296 "{bootmgr}" : {
225 "resumeobject" : "{45b547a7-8ca6-4417-9eb0-a257b61f35b4}" 297 "default" : "{45b547a7-8ca6-4417-9eb0-a257b61f35b4}"
226 }, 298 },
227 299
228 "{45b547a7-8ca6-4417-9eb0-a257b61f35b1}" : { 300 "{45b547a7-8ca6-4417-9eb0-a257b61f35b1}" : {
229 "type" : "application::osloader", 301 "type" : "application::osloader",
230 "description" : "Windows Boot", 302 "description" : "Windows Boot",
237 309
238Note that minimal doesn't mean recommended - Windows itself will add stuff 310Note that minimal doesn't mean recommended - Windows itself will add stuff
239to this during or after boot, and you might or might not run into issues 311to this during or after boot, and you might or might not run into issues
240when installing updates as it might not be able to find the F<bootmgr>. 312when installing updates as it might not be able to find the F<bootmgr>.
241 313
314This is how you would create a minimal hive with PBCDEDIT from within
315GNU/Linux, assuming F</dev/sdc3> is the windows partition, using
316a random GUID for the osloader and using C<partition> instead of
317C<legacypartition>:
318
319 osldr="{$(uuidgen)}"
320 part=$(pbcdedit bcd-device /dev/sdc3)
321 pbcdedit create minimal.bcd \
322 set '{bootmgr}' default "$osldr" \
323 set "$osldr" type application::osloader \
324 set "$osldr" description 'Windows Boot' \
325 set "$osldr" device "$part" \
326 set "$osldr" osdevice "$part" \
327 set "$osldr" path '\Windows\system32\winload.exe' \
328 set "$osldr" systemroot '\Windows'
329
242=head2 The C<meta> key 330=head2 The C<meta> key
243 331
244The C<meta> key is not stored in the BCD data store but is used only 332The C<meta> key is not stored in the BCD data store but is used only
245by PBCDEDIT. It is always generated when exporting, and importing will 333by PBCDEDIT. It is always generated when exporting, and importing will
246be refused when it exists and the version stored inside doesn't store 334be refused when it exists and the version stored inside doesn't store
247the JSON schema version of PBCDEDIT. This ensures that differemt and 335the JSON schema version of PBCDEDIT. This ensures that different and
248incompatible versions of PBCDEDIT will not read and misinterΓΌret each 336incompatible versions of PBCDEDIT will not read and misinterpret each
249others data. 337others data.
250 338
251=head2 The object keys 339=head2 The object keys
252 340
253Every other key is a BCD object. There is usually a BCD object for the 341Every other key is a BCD object. There is usually a BCD object for the
254boot manager, one for every boot option and a few others that store common 342boot manager, one for every boot option and a few others that store common
255settings inherited by these. 343settings inherited by these.
256 344
257Each BCD object is represented by a GUID wrapped in curly braces. These 345Each BCD object is represented by a GUID wrapped in curly braces. These
258are usually random GUIDs used only to distinguish bCD objects from each 346are usually random GUIDs used only to distinguish BCD objects from each
259other. When adding a new boot option, you can simply generate a new GUID. 347other. When adding a new boot option, you can simply generate a new GUID.
260 348
261Some of these GUIDs are fixed well known GUIDs which PBCDEDIT will decode 349Some of these GUIDs are fixed well known GUIDs which PBCDEDIT will decode
262into human-readable strings such as C<{globalsettings}>, which is the same 350into human-readable strings such as C<{globalsettings}>, which is the same
263as C<{7ea2e1ac-2e61-4728-aaa3-896d9d0a9f0e}>. 351as C<{7ea2e1ac-2e61-4728-aaa3-896d9d0a9f0e}>.
297get a list of all BCD elements known to PBCDEDIT by running F<pbcdedit 385get a list of all BCD elements known to PBCDEDIT by running F<pbcdedit
298elements>. 386elements>.
299 387
300What was said about duplicate keys mapping to the same object is true for 388What was said about duplicate keys mapping to the same object is true for
301elements as well, so, again, you should always use the canonical name, 389elements as well, so, again, you should always use the canonical name,
302whcih is the human radable alias, if known. 390which is the human readable alias, if known.
303 391
304=head3 BCD element types 392=head3 BCD element types
305 393
306Each BCD element has a type such as I<string> or I<boolean>. This type 394Each BCD element has a type such as I<string> or I<boolean>. This type
307determines how the value is interpreted, and most of them are pretty easy 395determines how the value is interpreted, and most of them are pretty easy
319 "description" : "Windows 10", 407 "description" : "Windows 10",
320 "systemroot" : "\\Windows", 408 "systemroot" : "\\Windows",
321 409
322=item boolean 410=item boolean
323 411
324Almost as simnple are booleans, which represent I<true>/I<false>, 412Almost as simple are booleans, which represent I<true>/I<false>,
325I<on>/I<off> and similar values. In the JSON form, true is represented 413I<on>/I<off> and similar values. In the JSON form, true is represented
326by the number C<1>, and false is represented by the number C<0>. Other 414by the number C<1>, and false is represented by the number C<0>. Other
327values will be accepted, but PBCDEDIT doesn't guarantee how these are 415values will be accepted, but PBCDEDIT doesn't guarantee how these are
328interpreted. 416interpreted.
329 417
333 421
334 "displaybootmenu" : 0, 422 "displaybootmenu" : 0,
335 423
336=item integer 424=item integer
337 425
338Again, very simple, this is a 64 bit integer. IT can be either specified 426Again, very simple, this is a 64 bit integer. It can be either specified
339as a decimal number, as a hex number (by prefixing it with C<0x>) or as a 427as a decimal number, as a hex number (by prefixing it with C<0x>) or as a
340binatry number (prefix C<0b>). 428binary number (prefix C<0b>).
341 429
342For example, the boot C<timeout> is an integer, specifying the automatic 430For example, the boot C<timeout> is an integer, specifying the automatic
343boot delay in seconds: 431boot delay in seconds:
344 432
345 "timeout" : 30, 433 "timeout" : 30,
346 434
347=item integer list 435=item integer list
348 436
349This is a list of 64 bit integers separated by whitespace. It is not used 437This is a list of 64 bit integers separated by whitespace. It is not used
350much, so here is a somewhat artificial an untested exanmple of using 438much, so here is a somewhat artificial and untested example of using
351C<customactions> to specify a certain custom, eh, action to be executed 439C<customactions> to specify a certain custom, eh, action to be executed
352when pressing C<F10> at boot: 440when pressing C<F10> at boot:
353 441
354 "customactions" : "0x1000044000001 0x54000001", 442 "customactions" : "0x1000044000001 0x54000001",
355 443
356=item guid 444=item guid
357 445
358This represents a single GUID value wrqapped in curly braces. It is used a 446This represents a single GUID value wrapped in curly braces. It is used a
359lot to refer from one BCD object to other one. 447lot to refer from one BCD object to other one.
360 448
361For example, The C<{bootmgr}> object might refer to a resume boot option 449For example, The C<{bootmgr}> object might refer to a resume boot option
362using C<resumeobject>: 450using C<default>:
363 451
364 "resumeobject" : "{7ae02178-821d-11e7-8813-1c872c5f5ab0}", 452 "default" : "{7ae02178-821d-11e7-8813-1c872c5f5ab0}",
365 453
366Human readable aliases are used and allowed. 454Human readable aliases are used and allowed.
367 455
368=item guid list 456=item guid list
369 457
370Similar to te guid type, this represents a list of such GUIDs, separated 458Similar to the GUID type, this represents a list of such GUIDs, separated
371by whitespace from each other. 459by whitespace from each other.
372 460
373For example, many BCD objects can I<inherit> elements from other BCD 461For example, many BCD objects can I<inherit> elements from other BCD
374objects by specifying the GUIDs of those other objects ina GUID list 462objects by specifying the GUIDs of those other objects in a GUID list
375called surprisingly called C<inherit>: 463called surprisingly called C<inherit>:
376 464
377 "inherit" : "{dbgsettings} {emssettings} {badmemory}", 465 "inherit" : "{dbgsettings} {emssettings} {badmemory}",
378 466
379This example also shows how human readable aliases can be used. 467This example also shows how human readable aliases can be used.
383This type is why I write I<most> are easy to explain earlier: This type 471This type is why I write I<most> are easy to explain earlier: This type
384is the pinnacle of Microsoft-typical hacks layered on top of other 472is the pinnacle of Microsoft-typical hacks layered on top of other
385hacks. Understanding this type took more time than writing all the rest of 473hacks. Understanding this type took more time than writing all the rest of
386PBCDEDIT, and because it is so complex, this type has its own subsection 474PBCDEDIT, and because it is so complex, this type has its own subsection
387below. 475below.
476
388=back 477=back
389 478
390=head4 The BCD "device" element type 479=head3 The BCD "device" element type
391 480
392Device elements specify, well, devices. They are used for such diverse 481Device elements specify, well, devices. They are used for such diverse
393purposes such as finding a TFTP network boot imagem serial ports or VMBUS 482purposes such as finding a TFTP network boot image, serial ports or VMBUS
394devices, but most commonly they are used to specify the disk (harddisk, 483devices, but most commonly they are used to specify the disk (harddisk,
395cdrom ramdisk, vhd...) to boot from. 484cdrom, ramdisk, vhd...) to boot from.
396 485
397The device element is kind of a mini-language in its own which is much 486The device element is kind of a mini-language in its own which is much
398more versatile then the limited windows interface to it - BCDEDIT - 487more versatile then the limited windows interface to it - BCDEDIT -
399reveals. 488reveals.
400 489
403element, so almost everything known about it had to be researched first 492element, so almost everything known about it had to be researched first
404in the process of writing this script, and consequently, support for BCD 493in the process of writing this script, and consequently, support for BCD
405device elements is partial only. 494device elements is partial only.
406 495
407On the other hand, the expressive power of PBCDEDIT in specifying devices 496On the other hand, the expressive power of PBCDEDIT in specifying devices
408is much bigger than BCDEDIT and therefore more cna be don with it. The 497is much greater than BCDEDIT and therefore more can be done with it. The
409downside is that BCD device elements are much more complicated than what 498downside is that BCD device elements are much more complicated than what
410you might think from reading the BCDEDIT documentation. 499you might think from reading the BCDEDIT documentation.
411 500
412In other words, simple things are complicated, and complicated things are 501In other words, simple things are complicated, and complicated things are
413possible. 502possible.
414 503
415Anyway, the general syntax of device elements is an optional GUID, 504Anyway, the general syntax of device elements is an optional GUID,
416followed by a device type, optionally followed by hexdecimal flags in 505followed by a device type, optionally followed by hexadecimal flags in
417angle brackets, optionally followed by C<=> and a comma-separated list of 506angle brackets, optionally followed by C<=> and a comma-separated list of
418arguments, some of which can be (and often are) in turn devices again. 507arguments, some of which can be (and often are) in turn devices again.
419 508
420 [{GUID}]type[<flags>][=arg,arg...] 509 [{GUID}]type[<flags>][=arg,arg...]
421 510
447The types understood and used by PBCDEDIT are as follows (keep in mind 536The types understood and used by PBCDEDIT are as follows (keep in mind
448that not of all the following is necessarily supported in PBCDEDIT): 537that not of all the following is necessarily supported in PBCDEDIT):
449 538
450=over 539=over
451 540
452=item binary=hex... 541=item C<binary=>I<hex...>
453 542
454This type isn't actually a real BCD element type, but a fallback for those 543This type isn't actually a real BCD element type, but a fallback for those
455cases where PBCDEDIT can't perfectly decode a device element (except for 544cases where PBCDEDIT can't perfectly decode a device element (except for
456the leading GUID, which it can always decode). In such cases, it will 545the leading GUID, which it can always decode). In such cases, it will
457convert the device into this type with a hexdump of the element data. 546convert the device into this type with a hexdump of the element data.
458 547
459=item null 548=item C<null>
460 549
461This is another special type - sometimes, a device all zero-filled, which 550This is another special type - sometimes, a device is all zero-filled,
462is not valid. This can mark the absence of a device or something PBCDEDIT 551which is not valid. This can mark the absence of a device or something
463does not understand, so it decodes it into this special "all zero" type 552PBCDEDIT does not understand, so it decodes it into this special "all
464called C<null>. 553zero" type called C<null>.
465 554
466It's most commonly found in devices that can use an optional parent 555It's most commonly found in devices that can use an optional parent
467device, when no parent device is used. 556device, when no parent device is used.
468 557
469=item boot 558=item C<boot>
470 559
471Another type without parameters, this refers to the device that was booted 560Another type without parameters, this refers to the device that was booted
472from (nowadays typically the EFI system partition). 561from (nowadays typically the EFI system partition).
473 562
474=item vmbus=interfacetype,interfaceinstance 563=item C<vmbus=>I<interfacetype>,I<interfaceinstance>
475 564
476This specifies a VMBUS device with the given interface type and interface 565This specifies a VMBUS device with the given interface type and interface
477instance, both of which are "naked" (no curly braces) GUIDs. 566instance, both of which are "naked" (no curly braces) GUIDs.
478 567
479Made-up example (couldn't find a single example on the web): 568Made-up example (couldn't find a single example on the web):
480 569
481 vmbus=c376c1c3-d276-48d2-90a9-c04748072c60,12345678-a234-b234-c234-d2345678abcd 570 vmbus=c376c1c3-d276-48d2-90a9-c04748072c60,12345678-a234-b234-c234-d2345678abcd
482 571
483=item partition=<parent>,devicetype,partitiontype,diskid,partitionid 572=item C<partition=><I<parent>>,I<devicetype>,I<partitiontype>,I<diskid>,I<partitionid>
484 573
485This designates a specific partition on a block device. C<< <parent> 574This designates a specific partition on a block device. I<parent> is an
486>> is an optional parent device on which to search on, and is often 575optional parent device on which to search on, and is often C<null>. Note
487C<null>. Note that the anfgle brackets are part of the syntax. 576that the angle brackets around I<parent> are part of the syntax.
488 577
489C<devicetypes> is one of C<harddisk>, C<floppy>, C<cdrom>, C<ramdisk>, 578I<devicetypes> is one of C<harddisk>, C<floppy>, C<cdrom>, C<ramdisk>,
490C<file> or C<vhd>, where the first three should be self-explaining, 579C<file> or C<vhd>, where the first three should be self-explaining,
491C<file> is usually used to locate a device by finding a magic file, and 580C<file> is usually used to locate a file to be used as a disk image,
492C<vhd> is used for virtual harddisks - F<.vhd> and F<-vhdx> files. 581and C<vhd> is used to treat files as virtual harddisks, i.e. F<vhd> and
582F<vhdx> files.
493 583
494The C<partitiontype> is either C<mbr>, C<gpt> or C<raw>, the latter being 584The I<partitiontype> is either C<mbr>, C<gpt> or C<raw>, the latter being
495used for devices without partitions, such as cdroms, where the "partition" 585used for devices without partitions, such as cdroms, where the "partition"
496is usually the whole device. 586is usually the whole device.
497 587
498The C<diskid> identifies the disk or device using a unique signature, and 588The I<diskid> identifies the disk or device using a unique signature, and
499the same is true for the C<partitionid>. How these are interpreted depends 589the same is true for the I<partitionid>. How these are interpreted depends
500on the C<partitiontype>: 590on the I<partitiontype>:
501 591
502=over 592=over
503 593
504=item mbr 594=item C<mbr>
505 595
506The C<diskid> is the 32 bit disk signature stored at offset 0x1b8 in the 596The C<diskid> is the 32 bit disk signature stored at offset 0x1b8 in the
507MBR, interpreted as a 32 bit unsigned little endian integer and written as 597MBR, interpreted as a 32 bit unsigned little endian integer and written as
508hex number. That is, the bytes C<01 02 03 04> would become C<04030201>. 598hex number. That is, the bytes C<01 02 03 04> would become C<04030201>.
509 599
510Diskpart (using the C<DETAIL> command) and the C<lsblk> comamnd typically 600Diskpart (using the C<DETAIL> command) and the C<lsblk> command typically
511found on GNU/Linux systems (using e.g. C<lsblk -o NAME,PARTUUID>) can 601found on GNU/Linux systems (using e.g. C<lsblk -o NAME,PARTUUID>) can
512display the disk id. 602display the I<diskid>.
513 603
514The C<partitionid> is the byte offset(!) of the partition counting from 604The I<partitionid> is the byte offset(!) of the partition counting from
515the beginning of the MBR. 605the beginning of the MBR.
516 606
517Example, use the partition on the harddisk with C<diskid> C<47cbc08a> 607Example, use the partition on the harddisk with I<diskid> C<47cbc08a>
518starting at sector C<2048> (= 1048576 / 512). 608starting at sector C<2048> (= 1048576 / 512).
519 609
520 partition=<null>,harddisk,mbr,47cbc08a,1048576 610 partition=<null>,harddisk,mbr,47cbc08a,1048576
521 611
522=item gpt 612=item C<gpt>
523 613
524The C<diskid> is the disk UUID/disk identifier GUID from the partition 614The I<diskid> is the disk GUID/disk identifier GUID from the partition
525table (as displayed e.g. by C<gdisk>), and the C<partitionid> is the 615table (as displayed e.g. by F<gdisk>), and the I<partitionid> is the
526partition unique GUID (displayed using e.g. the C<gdisk> C<i> command). 616partition unique GUID (displayed using e.g. the F<gdisk> F<i> command).
527 617
528Example: use the partition C<76d39e5f-ad1b-407e-9c05-c81eb83b57dd> on GPT 618Example: use the partition C<76d39e5f-ad1b-407e-9c05-c81eb83b57dd> on GPT
529disk C<9742e468-9206-48a0-b4e4-c4e9745a356a>. 619disk C<9742e468-9206-48a0-b4e4-c4e9745a356a>.
530 620
531 partition=<null>,harddisk,gpt,9742e468-9206-48a0-b4e4-c4e9745a356a,76d39e5f-ad1b-407e-9c05-c81eb83b57dd 621 partition=<null>,harddisk,gpt,9742e468-9206-48a0-b4e4-c4e9745a356a,76d39e5f-ad1b-407e-9c05-c81eb83b57dd
532 622
533=item raw 623=item C<raw>
534 624
535Instead of diskid and partitionid, this type only accepts a decimal disk 625Instead of I<diskid> and I<partitionid>, this type only accepts a decimal
536number and signifies the whole disk. BCDEDIT cannot display the resulting 626disk number and signifies the whole disk. BCDEDIT cannot display the
537device, and I am doubtful whether it has a useful effect. 627resulting device, and I am doubtful whether it has a useful effect.
538 628
539=back 629=back
540 630
541=item legacypartition=<parent>,devicetype,partitiontype,diskid,partitionid 631=item C<legacypartition=><I<parent>>,I<devicetype>,I<partitiontype>,I<diskid>,I<partitionid>
542 632
543This is exactly the same as the C<partition> type, except for a tiny 633This is exactly the same as the C<partition> type, except for a tiny
544detail: instead of using the partition start offset, this type uses the 634detail: instead of using the partition start offset, this type uses the
545partition number for MBR disks. Behaviour other partition types should be 635partition number for MBR disks. Behaviour other partition types should be
546the same. 636the same.
547 637
548The partition number starts at C<1> and skips unused partition, so if 638The partition number starts at C<1> and skips unused partition, so if
549there are two primary partitions and another partition inside the extended 639there are two primary partitions and another partition inside the extended
550partition, the primary partitions are number C<1> and C<2> and the 640partition, the primary partitions are number C<1> and C<2> and the
551partition inside the extended partition is number C<3>, rwegardless of any 641partition inside the extended partition is number C<3>, regardless of any
552gaps. 642gaps.
553 643
554=item locate=<parent>,locatetype,locatearg 644=item C<locate=><I<parent>>,I<locatetype>,I<locatearg>
555 645
556This device description will make the bootloader search for a partition 646This device description will make the bootloader search for a partition
557with a given path. 647with a given path.
558 648
559The C<< <parent> >> device is the device to search on (angle brackets are 649The I<parent> device is the device to search on (angle brackets are
560still part of the syntax!) If it is C<< <null> >>, then C<locate> will 650still part of the syntax!) If it is C<null>, then C<locate> will
561search all disks it can find. 651search all disks it can find.
562 652
563C<locatetype> is either C<element> or C<path>, and merely distinguishes 653I<locatetype> is either C<element> or C<path>, and merely distinguishes
564between two different ways to specify the path to search for: C<element> 654between two different ways to specify the path to search for: C<element>
565uses an element ID (either as hex or as name) as C<locatearg> and C<path> 655uses an element ID (either as hex or as name) as I<locatearg> and C<path>
566uses a relative path as C<locatearg>. 656uses a relative path as I<locatearg>.
567 657
568Example: find any partition which has the C<magicfile.xxx> path in the 658Example: find any partition which has the F<magicfile.xxx> path in the
569root. 659root.
570 660
571 locate=<null>,path,\magicfile.xxx 661 locate=<null>,path,\magicfile.xxx
572 662
573Example: find any partition which has the path specified in the 663Example: find any partition which has the path specified in the
574C<systemroot> element (typically C<\Windows>). 664C<systemroot> element (typically F<\Windows>).
575 665
576 locate=<null>,element,systemroot 666 locate=<null>,element,systemroot
577 667
578=item block=devicetype,args... 668=item C<block=>I<devicetype>,I<args...>
579 669
580Last not least, the most complex type, C<block>, which... specifies block 670Last not least, the most complex type, C<block>, which... specifies block
581devices (which could be inside a F<vhdx> file for example). 671devices (which could be inside a F<vhdx> file for example).
582 672
583C<devicetypes> is one of C<harddisk>, C<floppy>, C<cdrom>, C<ramdisk>, 673I<devicetypes> is one of C<harddisk>, C<floppy>, C<cdrom>, C<ramdisk>,
584C<file> or C<vhd> - the same as for C<partiion=>. 674C<file> or C<vhd> - the same as for C<partition=>.
585 675
586The remaining arguments change depending on the C<devicetype>: 676The remaining arguments change depending on the I<devicetype>:
587 677
588=over 678=over
589 679
590=item block=file,<parent>,path 680=item C<block=file>,<I<parent>>,I<path>
591 681
592Interprets the C<< <parent> >> device (typically a partition) as a 682Interprets the I<parent> device (typically a partition) as a
593filesystem and specifies a file path inside. 683filesystem and specifies a file path inside.
594 684
595=item block=vhd,<parent> 685=item C<block=vhd>,<I<parent>>
596 686
597Pretty much just changes the interpretation of C<< <parent> >>, which is 687Pretty much just changes the interpretation of I<parent>, which is
598usually a disk image (C<block=file,...)>) to be a F<vhd> or F<vhdx> file. 688usually a disk image (C<block=file,...)>) to be a F<vhd> or F<vhdx> file.
599 689
600=item block=ramdisk,<parent>,base,size,offset,path 690=item C<block=ramdisk>,<I<parent>>,I<base>,I<size>,I<offset>,I<path>
601 691
602Interprets the C<< <parent> >> device as RAM disk, using the (decimal) 692Interprets the I<parent> device as RAM disk, using the (decimal)
603base address, byte size and byte offset inside a file specified by 693base address, byte size and byte offset inside a file specified by
604C<path>. The numbers are usually all C<0> because they cna be extracted 694I<path>. The numbers are usually all C<0> because they can be extracted
605from the RAM disk image or other parameters. 695from the RAM disk image or other parameters.
606 696
607This is most commonly used to boot C<wim> images. 697This is most commonly used to boot C<wim> images.
608 698
609=item block=floppy,drivenum 699=item C<block=floppy>,I<drivenum>
610 700
611Refers to a removable drive identified by a number. BCDEDIT cannot display 701Refers to a removable drive identified by a number. BCDEDIT cannot display
612the resultinfg device, and it is not clear what effect it will have. 702the resulting device, and it is not clear what effect it will have.
613 703
614=item block=cdrom,drivenum 704=item C<block=cdrom>,I<drivenum>
615 705
616Pretty much the same as C<floppy> but for CD-ROMs. 706Pretty much the same as C<floppy> but for CD-ROMs.
617 707
618=item anything else 708=item anything else
619 709
620Probably not yet implemented. Tell me of your needs... 710Probably not yet implemented. Tell me of your needs...
621 711
622=back 712=back
623 713
624=back5 Examples 714=head4 Examples
625 715
626This concludes the syntax overview for device elements, but probably 716This concludes the syntax overview for device elements, but probably
627leaves many questions open. I can't help with most of them, as I also ave 717leaves many questions open. I can't help with most of them, as I also have
628many questions, but I can walk you through some actual examples using mroe 718many questions, but I can walk you through some actual examples using more
629complex aspects. 719complex aspects.
630 720
631=item locate=<block=vhd,<block=file,<locate=<null>,path,\disk.vhdx>,\disk.vhdx>>,element,path 721=item C<< locate=<block=vhd,<block=file,<locate=<null>,path,\disk.vhdx>,\disk.vhdx>>,element,path >>
632 722
633Just like with C declarations, you best treat device descriptors as 723Just like with C declarations, you best treat device descriptors as
634instructions to find your device and work your way from the inside out: 724instructions to find your device and work your way from the inside out:
635 725
636 locate=<null>,path,\disk.vhdx 726 locate=<null>,path,\disk.vhdx
643Next, this takes the device locate has found and finds a file called 733Next, this takes the device locate has found and finds a file called
644F<\disk.vhdx> on it. This is the same file locate was using, but that is 734F<\disk.vhdx> on it. This is the same file locate was using, but that is
645only because we find the device using the same path as finding the disk 735only because we find the device using the same path as finding the disk
646image, so this is purely incidental, although quite common. 736image, so this is purely incidental, although quite common.
647 737
648Bext, this file will be opened as a virtual disk: 738Next, this file will be opened as a virtual disk:
649 739
650 block=vhd,<see above> 740 block=vhd,<see above>
651 741
652And finally, inside this disk, another C<locate> will look for a partition 742And finally, inside this disk, another C<locate> will look for a partition
653with a path as specified in the C<path> element, which most likely will be 743with a path as specified in the C<path> element, which most likely will be
656 locate=<see above>,element,path 746 locate=<see above>,element,path
657 747
658As a result, this will boot the first Windows it finds on the first 748As a result, this will boot the first Windows it finds on the first
659F<disk.vhdx> disk image it can find anywhere. 749F<disk.vhdx> disk image it can find anywhere.
660 750
661=item locate=<block=vhd,<block=file,<partition=<null>,harddisk,mbr,47cbc08a,242643632128>,\win10.vhdx>>,element,path 751=item C<< locate=<block=vhd,<block=file,<partition=<null>,harddisk,mbr,47cbc08a,242643632128>,\win10.vhdx>>,element,path >>
662 752
663Pretty much the same as the previous case, but witzh a bit of variance. First, look for a specific partition on 753Pretty much the same as the previous case, but with a bit of
664an MBR-partitioned disk: 754variance. First, look for a specific partition on an MBR-partitioned disk:
665 755
666 partition=<null>,harddisk,mbr,47cbc08a,242643632128 756 partition=<null>,harddisk,mbr,47cbc08a,242643632128
667 757
668Then open the file F<\win10.vhdx> on that partition: 758Then open the file F<\win10.vhdx> on that partition:
669 759
675 765
676And again the windows loader (or whatever is in C<path>) will be searched: 766And again the windows loader (or whatever is in C<path>) will be searched:
677 767
678 locate=<see above>,element,path 768 locate=<see above>,element,path
679 769
680=item {b097d2b2-bc00-11e9-8a9a-525400123456}block<1>=ramdisk,<partition=<null>,harddisk,mbr,47cbc08a,242643632128>,0,0,0,\boot.wim 770=item C<< {b097d2b2-bc00-11e9-8a9a-525400123456}block<1>=ramdisk,<partition=<null>,harddisk,mbr,47cbc08a,242643632128>,0,0,0,\boot.wim >>
681 771
682This is quite different. First, it starts with a GUID. This GUID belongs 772This is quite different. First, it starts with a GUID. This GUID belongs
683to a BCD object of type C<device>, which has additional parameters: 773to a BCD object of type C<device>, which has additional parameters:
684 774
685 "{b097d2b2-bc00-11e9-8a9a-525400123456}" : { 775 "{b097d2b2-bc00-11e9-8a9a-525400123456}" : {
688 "ramdisksdidevice" : "partition=<null>,harddisk,mbr,47cbc08a,1048576", 778 "ramdisksdidevice" : "partition=<null>,harddisk,mbr,47cbc08a,1048576",
689 "ramdisksdipath" : "\boot.sdi" 779 "ramdisksdipath" : "\boot.sdi"
690 }, 780 },
691 781
692I will not go into many details, but this specifies a (presumably empty) 782I will not go into many details, but this specifies a (presumably empty)
693template ramdisk image (F<\boot.sdi>) that is used to initiaolize the 783template ramdisk image (F<\boot.sdi>) that is used to initialize the
694ramdisk. The F<\boot.wim> file is then extracted into it. As you cna also 784ramdisk. The F<\boot.wim> file is then extracted into it. As you can also
695see, this F<.sdi> file resides on a different C<partition>. 785see, this F<.sdi> file resides on a different C<partition>.
696 786
697Continuitn, as always, form the inside out, first this device descriptor 787Continuing, as always, from the inside out, first this device descriptor
698finds a specific partition: 788finds a specific partition:
699 789
700 partition=<null>,harddisk,mbr,47cbc08a,242643632128 790 partition=<null>,harddisk,mbr,47cbc08a,242643632128
701 791
702And then specifies a C<ramdisk> image on this partition: 792And then specifies a C<ramdisk> image on this partition:
707seems to be always there on this kind of entry. 797seems to be always there on this kind of entry.
708 798
709If you have some good examples to add here, feel free to mail me. 799If you have some good examples to add here, feel free to mail me.
710 800
711 801
712=head1 EDITING BCD DATA STORES 802=head1 EDITING BCD STORES
713 803
714The C<edit> and C<parse> subcommands allow you to read a BCD data store 804The C<edit> and C<parse> subcommands allow you to read a BCD data store
715and modify it or extract data from it. This is done by exyecuting a series 805and modify it or extract data from it. This is done by executing a series
716of "editing instructions" which are explained here. 806of "editing instructions" which are explained here.
717 807
718=over 808=over
719 809
720=item get I<object> I<element> 810=item C<get> I<object> I<element>
721 811
722Reads the BCD element I<element> from the BCD object I<object> and writes 812Reads the BCD element I<element> from the BCD object I<object> and writes
723it to standard output, followed by a newline. The I<object> can be a GUID 813it to standard output, followed by a newline. The I<object> can be a GUID
724or a human-readable alias, or the special string C<{default}>, which will 814or a human-readable alias, or the special string C<{default}>, which will
725refer to the default BCD object. 815refer to the default BCD object.
726 816
727Example: find description of the default BCD object. 817Example: find description of the default BCD object.
728 818
729 pbcdedit parse BCD get "{default}" description 819 pbcdedit parse BCD get "{default}" description
730 820
731=item set I<object> I<element> I<value> 821=item C<set> I<object> I<element> I<value>
732 822
733Similar to C<get>, but sets the element to the given I<value> instead. 823Similar to C<get>, but sets the element to the given I<value> instead.
734 824
735Example: change bootmgr default too 825Example: change the bootmgr default too
736C<{b097d2ad-bc00-11e9-8a9a-525400123456}>: 826C<{b097d2ad-bc00-11e9-8a9a-525400123456}>:
737 827
738 pbcdedit edit BCD set "{bootmgr}" resumeobject "{b097d2ad-bc00-11e9-8a9a-525400123456}" 828 pbcdedit edit BCD set "{bootmgr}" default "{b097d2ad-bc00-11e9-8a9a-525400123456}"
739 829
740=item eval I<perlcode> 830=item C<eval> I<perlcode>
741 831
742This takes the next argument, interprets it as Perl code and 832This takes the next argument, interprets it as Perl code and
743evaluates it. This allows you to do more complicated modifications or 833evaluates it. This allows you to do more complicated modifications or
744extractions. 834extractions.
745 835
764The example given for C<get>, above, could be expressed like this with 854The example given for C<get>, above, could be expressed like this with
765C<eval>: 855C<eval>:
766 856
767 pbcdedit edit BCD eval 'say $BCD->{$DEFAULT}{description}' 857 pbcdedit edit BCD eval 'say $BCD->{$DEFAULT}{description}'
768 858
769The example given for C<set> could be expresed like this: 859The example given for C<set> could be expressed like this:
770 860
771 pbcdedit edit BCD eval '$BCD->{$DEFAULT}{resumeobject} = "{b097d2ad-bc00-11e9-8a9a-525400123456}"' 861 pbcdedit edit BCD eval '$BCD->{"{bootmgr}"{default} = "{b097d2ad-bc00-11e9-8a9a-525400123456}"'
772 862
773=item do I<path> 863=item C<do> I<path>
774 864
775Similar to C<eval>, above, but instead of using the argument as perl code, 865Similar to C<eval>, above, but instead of using the argument as perl code,
776it loads the perl code from the given file and executes it. This makes it 866it loads the perl code from the given file and executes it. This makes it
777easier to write more complicated or larger programs. 867easier to write more complicated or larger programs.
778 868
779=back 869=back
780 870
871
781=head1 SEE ALSO 872=head1 SEE ALSO
782 873
783For ideas on what you can do, and some introductory material, try 874For ideas on what you can do with BCD stores in
875general, and some introductory material, try
784L<http://www.mistyprojects.co.uk/documents/BCDEdit/index.html>. 876L<http://www.mistyprojects.co.uk/documents/BCDEdit/index.html>.
785 877
786For good reference on BCD objects and elements, see Geoff Chappels pages 878For good reference on which BCD objects and
879elements exist, see Geoff Chappell's pages at
787at L<http://www.geoffchappell.com/notes/windows/boot/bcd/index.htm>. 880L<http://www.geoffchappell.com/notes/windows/boot/bcd/index.htm>.
788 881
789=head1 AUTHOR 882=head1 AUTHOR
790 883
791Written by Marc A. Lehmann <pbcdedit@schmorp.de>. 884Written by Marc A. Lehmann L<pbcdedit@schmorp.de>.
792 885
793=head1 REPORTING BUGS 886=head1 REPORTING BUGS
794 887
795Bugs can be reported dorectly tt he author at L<pcbedit@schmorp.de>. 888Bugs can be reported directly the author at L<pcbedit@schmorp.de>.
796 889
797=head1 BUGS AND SHORTCOMINGS 890=head1 BUGS AND SHORTCOMINGS
798 891
799This should be a module. Of a series of modules, even. 892This should be a module. Of a series of modules, even.
800 893
801Registry code should preserve classname and security descriptor data, and 894Registry code should preserve classname and security descriptor data, and
802whatever else is necessary to read and write any registry hive file. 895whatever else is necessary to read and write any registry hive file.
803 896
804I am also not happy with device descriptors being strings rather than a 897I am also not happy with device descriptors being strings rather than a
805data structure, but strings are probably better for command line usage. In 898data structure, but strings are probably better for command line usage. In
806any case,. device descriptors could be converted by simply "splitting" at 899any case, device descriptors could be converted by simply "splitting" at
807"=" and "," into an array reference, recursively. 900"=" and "," into an array reference, recursively.
808 901
809=head1 HOMEPAGE 902=head1 HOMEPAGE
810 903
811Original versions of this program can be found at 904Original versions of this program can be found at
818free to change and redistribute it. There is NO WARRANTY, to the extent 911free to change and redistribute it. There is NO WARRANTY, to the extent
819permitted by law. 912permitted by law.
820 913
821=cut 914=cut
822 915
823BEGIN { require "common/sense.pm"; common::sense->import } # common sense is optional, but recommended 916# common sense is optional, but recommended
917BEGIN { eval { require "common/sense.pm"; } && common::sense->import }
824 918
825use Data::Dump; 919no warnings 'portable'; # avoid 32 bit integer warnings
920
826use Encode (); 921use Encode ();
827use List::Util (); 922use List::Util ();
828use IO::Handle (); 923use IO::Handle ();
829use Time::HiRes (); 924use Time::HiRes ();
830 925
839 934
840# hack used for debugging 935# hack used for debugging
841sub xxd($$) { 936sub xxd($$) {
842 open my $xxd, "| xxd | sed -e 's/^/\Q$_[0]\E: /'"; 937 open my $xxd, "| xxd | sed -e 's/^/\Q$_[0]\E: /'";
843 syswrite $xxd, $_[1]; 938 syswrite $xxd, $_[1];
939}
940
941# get some meta info on a file (uid, gid, perms)
942sub stat_get($) {
943 [(stat shift)[4, 5, 2]]
944}
945
946# set stat info on a file
947sub stat_set($$) {
948 my ($fh_or_path, $stat) = @_;
949
950 return unless $stat;
951 chown $stat->[0], $stat->[1], $fh_or_path;
952 chmod +($stat->[2] & 07777), $fh_or_path;
844} 953}
845 954
846sub file_load($) { 955sub file_load($) {
847 my ($path) = @_; 956 my ($path) = @_;
848 957
853 or die "$path: short read\n"; 962 or die "$path: short read\n";
854 963
855 $buf 964 $buf
856} 965}
857 966
967sub file_save($$;$) {
968 my ($path, $data, $stat) = @_;
969
970 open my $fh, ">:raw", "$path~"
971 or die "$path~: $!\n";
972 print $fh $data
973 or die "$path~: short write\n";
974 stat_set $fh, $stat;
975 $fh->sync;
976 close $fh;
977
978 rename "$path~", $path;
979}
980
858# sources and resources used for this: 981# sources and resources used for writing pbcdedit
982#
859# registry: 983# registry:
860# https://github.com/msuhanov/regf/blob/master/Windows%20registry%20file%20format%20specification.md 984# https://github.com/msuhanov/regf/blob/master/Windows%20registry%20file%20format%20specification.md
861# http://amnesia.gtisc.gatech.edu/~moyix/suzibandit.ltd.uk/MSc/ 985# http://amnesia.gtisc.gatech.edu/~moyix/suzibandit.ltd.uk/MSc/
862# bcd: 986# bcd:
863# http://www.geoffchappell.com/notes/windows/boot/bcd/index.htm 987# http://www.geoffchappell.com/notes/windows/boot/bcd/index.htm
1045 my ($rname, $root) = $decode_key->($rootcell); 1169 my ($rname, $root) = $decode_key->($rootcell);
1046 1170
1047 [$rname, $root] 1171 [$rname, $root]
1048} 1172}
1049 1173
1050# return a binary windows fILETIME struct 1174# return a binary windows FILETIME struct
1051sub filetime_now { 1175sub filetime_now {
1052 my ($s, $ms) = Time::HiRes::gettimeofday; 1176 my ($s, $ms) = Time::HiRes::gettimeofday;
1053 1177
1054 pack "Q<", $s = ($s * 1_000_000 + $ms) * 10 + 116_444_736_000_000_000 1178 pack "Q<", ($s * 1_000_000 + $ms) * 10
1179 + 116_444_736_000_000_000 # 1970-01-01 00:00:00
1055} 1180}
1056 1181
1057# encode a registry hive 1182# encode a registry hive
1058sub regf_encode($) { 1183sub regf_encode($) {
1059 my ($hive) = @_; 1184 my ($hive) = @_;
1062 1187
1063 # the filetime is apparently used to verify log file validity, 1188 # the filetime is apparently used to verify log file validity,
1064 # so by generating a new timestamp the log files *should* automatically 1189 # so by generating a new timestamp the log files *should* automatically
1065 # become invalidated and windows would "self-heal" them. 1190 # become invalidated and windows would "self-heal" them.
1066 # (update: has been verified by reverse engineering) 1191 # (update: has been verified by reverse engineering)
1067 # possibly the fact that the two sequence numbes match might also 1192 # possibly the fact that the two sequence numbers match might also
1068 # make windows think that the hive is not dirty and ignore logs. 1193 # make windows think that the hive is not dirty and ignore logs.
1069 # (update: has been verified by reverse engineering) 1194 # (update: has been verified by reverse engineering)
1070 1195
1071 my $now = filetime_now; 1196 my $now = filetime_now;
1072 1197
1216 1341
1217 regf_decode file_load $path 1342 regf_decode file_load $path
1218} 1343}
1219 1344
1220# encode and save registry to file 1345# encode and save registry to file
1221sub regf_save { 1346sub regf_save($$;$) {
1222 my ($path, $hive) = @_; 1347 my ($path, $hive, $stat) = @_;
1223 1348
1224 $hive = regf_encode $hive; 1349 $hive = regf_encode $hive;
1225 1350
1226 open my $regf, ">:raw", "$path~" 1351 file_save $path, $hive, $stat;
1227 or die "$path~: $!\n";
1228 print $regf $hive
1229 or die "$path~: short write\n";
1230 $regf->sync;
1231 close $regf;
1232
1233 rename "$path~", $path;
1234} 1352}
1235 1353
1236############################################################################# 1354#############################################################################
1237# bcd stuff 1355# bcd stuff
1238 1356
1239# human-readable alises for GUID object identifiers 1357# human-readable aliases for GUID object identifiers
1240our %bcd_objects = ( 1358our %bcd_objects = (
1241 '{0ce4991b-e6b3-4b16-b23c-5e0d9250e5d9}' => '{emssettings}', 1359 '{0ce4991b-e6b3-4b16-b23c-5e0d9250e5d9}' => '{emssettings}',
1242 '{1afa9c49-16ab-4a5c-4a90-212802da9460}' => '{resumeloadersettings}', 1360 '{1afa9c49-16ab-4a5c-4a90-212802da9460}' => '{resumeloadersettings}',
1243 '{1cae1eb7-a0df-4d4d-9851-4860e34ef535}' => '{default}', 1361 '{1cae1eb7-a0df-4d4d-9851-4860e34ef535}' => '{default}',
1244 '{313e8eed-7098-4586-a9bf-309c61f8d449}' => '{kerneldbgsettings}', 1362 '{313e8eed-7098-4586-a9bf-309c61f8d449}' => '{kerneldbgsettings}',
1351sub BCDE_FORMAT_GUID_LIST () { 0x04000000 } 1469sub BCDE_FORMAT_GUID_LIST () { 0x04000000 }
1352sub BCDE_FORMAT_INTEGER () { 0x05000000 } 1470sub BCDE_FORMAT_INTEGER () { 0x05000000 }
1353sub BCDE_FORMAT_BOOLEAN () { 0x06000000 } 1471sub BCDE_FORMAT_BOOLEAN () { 0x06000000 }
1354sub BCDE_FORMAT_INTEGER_LIST () { 0x07000000 } 1472sub BCDE_FORMAT_INTEGER_LIST () { 0x07000000 }
1355 1473
1356sub dec_device;
1357sub enc_device;
1358
1359sub enc_integer($) { 1474sub enc_integer($) {
1360 no warnings 'portable'; # ugh
1361 my $value = shift; 1475 my $value = shift;
1362 $value = oct $value if $value =~ /^0[bBxX]/; 1476 $value = oct $value if $value =~ /^0[bBxX]/;
1363 unpack "H*", pack "Q<", $value 1477 unpack "H*", pack "Q<", $value
1364} 1478}
1479
1480sub enc_device($$);
1481sub dec_device($$);
1365 1482
1366our %bcde_dec = ( 1483our %bcde_dec = (
1367 BCDE_FORMAT_DEVICE , \&dec_device, 1484 BCDE_FORMAT_DEVICE , \&dec_device,
1368# # for round-trip verification 1485# # for round-trip verification
1369# BCDE_FORMAT_DEVICE , sub { 1486# BCDE_FORMAT_DEVICE , sub {
1379 BCDE_FORMAT_BOOLEAN , sub { shift eq "00" ? 0 : 1 }, 1496 BCDE_FORMAT_BOOLEAN , sub { shift eq "00" ? 0 : 1 },
1380 BCDE_FORMAT_INTEGER_LIST, sub { join " ", unpack "Q*", pack "H*", shift }, # not sure if this cna be 4 bytes 1497 BCDE_FORMAT_INTEGER_LIST, sub { join " ", unpack "Q*", pack "H*", shift }, # not sure if this cna be 4 bytes
1381); 1498);
1382 1499
1383our %bcde_enc = ( 1500our %bcde_enc = (
1384 BCDE_FORMAT_DEVICE , sub { binary => enc_device shift }, 1501 BCDE_FORMAT_DEVICE , sub { binary => enc_device $_[0], $_[1] },
1385 BCDE_FORMAT_STRING , sub { sz => shift }, 1502 BCDE_FORMAT_STRING , sub { sz => shift },
1386 BCDE_FORMAT_GUID , sub { sz => "{" . (dec_guid enc_wguid shift) . "}" }, 1503 BCDE_FORMAT_GUID , sub { sz => "{" . (dec_guid enc_wguid shift) . "}" },
1387 BCDE_FORMAT_GUID_LIST , sub { multi_sz => [map "{" . (dec_guid enc_wguid $_) . "}", split /\s+/, shift ] }, 1504 BCDE_FORMAT_GUID_LIST , sub { multi_sz => [map "{" . (dec_guid enc_wguid $_) . "}", split /\s+/, shift ] },
1388 BCDE_FORMAT_INTEGER , sub { binary => enc_integer shift }, 1505 BCDE_FORMAT_INTEGER , sub { binary => enc_integer shift },
1389 BCDE_FORMAT_BOOLEAN , sub { binary => shift ? "01" : "00" }, 1506 BCDE_FORMAT_BOOLEAN , sub { binary => shift ? "01" : "00" },
1390 BCDE_FORMAT_INTEGER_LIST, sub { binary => join "", map enc_integer $_, split /\s+/, shift }, 1507 BCDE_FORMAT_INTEGER_LIST, sub { binary => join "", map enc_integer $_, split /\s+/, shift },
1391); 1508);
1392 1509
1393# BCD Elements 1510# BCD Elements
1394our %bcde = ( 1511our %bcde_byclass = (
1512 any => {
1395 0x11000001 => 'device', 1513 0x11000001 => 'device',
1396 0x12000002 => 'path', 1514 0x12000002 => 'path',
1397 0x12000004 => 'description', 1515 0x12000004 => 'description',
1398 0x12000005 => 'locale', 1516 0x12000005 => 'locale',
1399 0x14000006 => 'inherit', 1517 0x14000006 => 'inherit',
1400 0x15000007 => 'truncatememory', 1518 0x15000007 => 'truncatememory',
1401 0x14000008 => 'recoverysequence', 1519 0x14000008 => 'recoverysequence',
1402 0x16000009 => 'recoveryenabled', 1520 0x16000009 => 'recoveryenabled',
1403 0x1700000a => 'badmemorylist', 1521 0x1700000a => 'badmemorylist',
1404 0x1600000b => 'badmemoryaccess', 1522 0x1600000b => 'badmemoryaccess',
1405 0x1500000c => 'firstmegabytepolicy', 1523 0x1500000c => 'firstmegabytepolicy',
1406 0x1500000d => 'relocatephysical', 1524 0x1500000d => 'relocatephysical',
1407 0x1500000e => 'avoidlowmemory', 1525 0x1500000e => 'avoidlowmemory',
1408 0x1600000f => 'traditionalkseg', 1526 0x1600000f => 'traditionalkseg',
1409 0x16000010 => 'bootdebug', 1527 0x16000010 => 'bootdebug',
1410 0x15000011 => 'debugtype', 1528 0x15000011 => 'debugtype',
1411 0x15000012 => 'debugaddress', 1529 0x15000012 => 'debugaddress',
1412 0x15000013 => 'debugport', 1530 0x15000013 => 'debugport',
1413 0x15000014 => 'baudrate', 1531 0x15000014 => 'baudrate',
1414 0x15000015 => 'channel', 1532 0x15000015 => 'channel',
1415 0x12000016 => 'targetname', 1533 0x12000016 => 'targetname',
1416 0x16000017 => 'noumex', 1534 0x16000017 => 'noumex',
1417 0x15000018 => 'debugstart', 1535 0x15000018 => 'debugstart',
1418 0x12000019 => 'busparams', 1536 0x12000019 => 'busparams',
1419 0x1500001a => 'hostip', 1537 0x1500001a => 'hostip',
1420 0x1500001b => 'port', 1538 0x1500001b => 'port',
1421 0x1600001c => 'dhcp', 1539 0x1600001c => 'dhcp',
1422 0x1200001d => 'key', 1540 0x1200001d => 'key',
1423 0x1600001e => 'vm', 1541 0x1600001e => 'vm',
1424 0x16000020 => 'bootems', 1542 0x16000020 => 'bootems',
1425 0x15000022 => 'emsport', 1543 0x15000022 => 'emsport',
1426 0x15000023 => 'emsbaudrate', 1544 0x15000023 => 'emsbaudrate',
1427 0x12000030 => 'loadoptions', 1545 0x12000030 => 'loadoptions',
1428 0x16000040 => 'advancedoptions', 1546 0x16000040 => 'advancedoptions',
1429 0x16000041 => 'optionsedit', 1547 0x16000041 => 'optionsedit',
1430 0x15000042 => 'keyringaddress', 1548 0x15000042 => 'keyringaddress',
1431 0x11000043 => 'bootstatdevice', 1549 0x11000043 => 'bootstatdevice',
1432 0x12000044 => 'bootstatfilepath', 1550 0x12000044 => 'bootstatfilepath',
1433 0x16000045 => 'preservebootstat', 1551 0x16000045 => 'preservebootstat',
1434 0x16000046 => 'graphicsmodedisabled', 1552 0x16000046 => 'graphicsmodedisabled',
1435 0x15000047 => 'configaccesspolicy', 1553 0x15000047 => 'configaccesspolicy',
1436 0x16000048 => 'nointegritychecks', 1554 0x16000048 => 'nointegritychecks',
1437 0x16000049 => 'testsigning', 1555 0x16000049 => 'testsigning',
1438 0x1200004a => 'fontpath', 1556 0x1200004a => 'fontpath',
1439 0x1500004b => 'integrityservices', 1557 0x1500004b => 'integrityservices',
1440 0x1500004c => 'volumebandid', 1558 0x1500004c => 'volumebandid',
1441 0x16000050 => 'extendedinput', 1559 0x16000050 => 'extendedinput',
1442 0x15000051 => 'initialconsoleinput', 1560 0x15000051 => 'initialconsoleinput',
1443 0x15000052 => 'graphicsresolution', 1561 0x15000052 => 'graphicsresolution',
1444 0x16000053 => 'restartonfailure', 1562 0x16000053 => 'restartonfailure',
1445 0x16000054 => 'highestmode', 1563 0x16000054 => 'highestmode',
1446 0x16000060 => 'isolatedcontext', 1564 0x16000060 => 'isolatedcontext',
1447 0x15000065 => 'displaymessage', 1565 0x15000065 => 'displaymessage',
1448 0x15000066 => 'displaymessageoverride', 1566 0x15000066 => 'displaymessageoverride',
1449 0x16000068 => 'nobootuxtext', 1567 0x16000068 => 'nobootuxtext',
1450 0x16000069 => 'nobootuxprogress', 1568 0x16000069 => 'nobootuxprogress',
1451 0x1600006a => 'nobootuxfade', 1569 0x1600006a => 'nobootuxfade',
1452 0x1600006b => 'bootuxreservepooldebug', 1570 0x1600006b => 'bootuxreservepooldebug',
1453 0x1600006c => 'bootuxdisabled', 1571 0x1600006c => 'bootuxdisabled',
1454 0x1500006d => 'bootuxfadeframes', 1572 0x1500006d => 'bootuxfadeframes',
1455 0x1600006e => 'bootuxdumpstats', 1573 0x1600006e => 'bootuxdumpstats',
1456 0x1600006f => 'bootuxshowstats', 1574 0x1600006f => 'bootuxshowstats',
1457 0x16000071 => 'multibootsystem', 1575 0x16000071 => 'multibootsystem',
1458 0x16000072 => 'nokeyboard', 1576 0x16000072 => 'nokeyboard',
1459 0x15000073 => 'aliaswindowskey', 1577 0x15000073 => 'aliaswindowskey',
1460 0x16000074 => 'bootshutdowndisabled', 1578 0x16000074 => 'bootshutdowndisabled',
1461 0x15000075 => 'performancefrequency', 1579 0x15000075 => 'performancefrequency',
1462 0x15000076 => 'securebootrawpolicy', 1580 0x15000076 => 'securebootrawpolicy',
1463 0x17000077 => 'allowedinmemorysettings', 1581 0x17000077 => 'allowedinmemorysettings',
1464 0x15000079 => 'bootuxtransitiontime', 1582 0x15000079 => 'bootuxtransitiontime',
1465 0x1600007a => 'mobilegraphics', 1583 0x1600007a => 'mobilegraphics',
1466 0x1600007b => 'forcefipscrypto', 1584 0x1600007b => 'forcefipscrypto',
1467 0x1500007d => 'booterrorux', 1585 0x1500007d => 'booterrorux',
1468 0x1600007e => 'flightsigning', 1586 0x1600007e => 'flightsigning',
1469 0x1500007f => 'measuredbootlogformat', 1587 0x1500007f => 'measuredbootlogformat',
1470 0x15000080 => 'displayrotation', 1588 0x15000080 => 'displayrotation',
1471 0x15000081 => 'logcontrol', 1589 0x15000081 => 'logcontrol',
1472 0x16000082 => 'nofirmwaresync', 1590 0x16000082 => 'nofirmwaresync',
1473 0x11000084 => 'windowssyspart', 1591 0x11000084 => 'windowssyspart',
1474 0x16000087 => 'numlock', 1592 0x16000087 => 'numlock',
1475 0x22000001 => 'bpbstring', 1593 0x26000202 => 'skipffumode',
1594 0x26000203 => 'forceffumode',
1595 0x25000510 => 'chargethreshold',
1596 0x26000512 => 'offmodecharging',
1597 0x25000aaa => 'bootflow',
1598 0x45000001 => 'devicetype',
1599 0x42000002 => 'applicationrelativepath',
1600 0x42000003 => 'ramdiskdevicerelativepath',
1601 0x46000004 => 'omitosloaderelements',
1602 0x47000006 => 'elementstomigrate',
1603 0x46000010 => 'recoveryos',
1604 },
1605 bootapp => {
1606 0x26000145 => 'enablebootdebugpolicy',
1607 0x26000146 => 'enablebootorderclean',
1608 0x26000147 => 'enabledeviceid',
1609 0x26000148 => 'enableffuloader',
1610 0x26000149 => 'enableiuloader',
1611 0x2600014a => 'enablemassstorage',
1612 0x2600014b => 'enablerpmbprovisioning',
1613 0x2600014c => 'enablesecurebootpolicy',
1614 0x2600014d => 'enablestartcharge',
1615 0x2600014e => 'enableresettpm',
1616 },
1617 bootmgr => {
1476 0x24000001 => 'displayorder', 1618 0x24000001 => 'displayorder',
1477 0x21000001 => 'filedevice',
1478 0x21000001 => 'osdevice',
1479 0x25000001 => 'passcount',
1480 0x26000001 => 'pxesoftreboot',
1481 0x22000002 => 'applicationname',
1482 0x24000002 => 'bootsequence', 1619 0x24000002 => 'bootsequence',
1483 0x22000002 => 'filepath',
1484 0x22000002 => 'systemroot',
1485 0x25000002 => 'testmix',
1486 0x26000003 => 'cacheenable',
1487 0x26000003 => 'customsettings',
1488 0x23000003 => 'default', 1620 0x23000003 => 'default',
1489 0x25000003 => 'failurecount',
1490 0x23000003 => 'resumeobject',
1491 0x26000004 => 'failuresenabled',
1492 0x26000004 => 'pae',
1493 0x26000004 => 'stampdisks',
1494 0x25000004 => 'testtofail',
1495 0x25000004 => 'timeout', 1621 0x25000004 => 'timeout',
1496 0x21000005 => 'associatedosdevice',
1497 0x26000005 => 'cacheenable',
1498 0x26000005 => 'resume', 1622 0x26000005 => 'resume',
1499 0x25000005 => 'stridefailcount',
1500 0x26000006 => 'debugoptionenabled',
1501 0x25000006 => 'invcfailcount',
1502 0x23000006 => 'resumeobject', 1623 0x23000006 => 'resumeobject',
1503 0x25000007 => 'bootux',
1504 0x25000007 => 'matsfailcount',
1505 0x24000007 => 'startupsequence', 1624 0x24000007 => 'startupsequence',
1506 0x25000008 => 'bootmenupolicy',
1507 0x25000008 => 'randfailcount',
1508 0x25000009 => 'chckrfailcount',
1509 0x26000010 => 'detecthal',
1510 0x24000010 => 'toolsdisplayorder', 1625 0x24000010 => 'toolsdisplayorder',
1511 0x22000011 => 'kernel',
1512 0x22000012 => 'hal',
1513 0x22000013 => 'dbgtransport',
1514 0x26000020 => 'displaybootmenu', 1626 0x26000020 => 'displaybootmenu',
1515 0x25000020 => 'nx',
1516 0x26000021 => 'noerrordisplay', 1627 0x26000021 => 'noerrordisplay',
1517 0x25000021 => 'pae',
1518 0x21000022 => 'bcddevice', 1628 0x21000022 => 'bcddevice',
1519 0x26000022 => 'winpe',
1520 0x22000023 => 'bcdfilepath', 1629 0x22000023 => 'bcdfilepath',
1521 0x26000024 => 'hormenabled', 1630 0x26000024 => 'hormenabled',
1522 0x26000024 => 'hormenabled',
1523 0x26000024 => 'nocrashautoreboot',
1524 0x26000025 => 'hiberboot', 1631 0x26000025 => 'hiberboot',
1525 0x26000025 => 'lastknowngood',
1526 0x26000026 => 'oslnointegritychecks',
1527 0x22000026 => 'passwordoverride', 1632 0x22000026 => 'passwordoverride',
1528 0x26000027 => 'osltestsigning',
1529 0x22000027 => 'pinpassphraseoverride', 1633 0x22000027 => 'pinpassphraseoverride',
1530 0x26000028 => 'processcustomactionsfirst', 1634 0x26000028 => 'processcustomactionsfirst',
1531 0x27000030 => 'customactions', 1635 0x27000030 => 'customactions',
1532 0x26000030 => 'nolowmem',
1533 0x26000031 => 'persistbootsequence', 1636 0x26000031 => 'persistbootsequence',
1534 0x25000031 => 'removememory',
1535 0x25000032 => 'increaseuserva',
1536 0x26000032 => 'skipstartupsequence', 1637 0x26000032 => 'skipstartupsequence',
1537 0x25000033 => 'perfmem',
1538 0x22000040 => 'fverecoveryurl', 1638 0x22000040 => 'fverecoveryurl',
1539 0x26000040 => 'vga',
1540 0x22000041 => 'fverecoverymessage', 1639 0x22000041 => 'fverecoverymessage',
1640 },
1641 device => {
1642 0x35000001 => 'ramdiskimageoffset',
1643 0x35000002 => 'ramdisktftpclientport',
1644 0x31000003 => 'ramdisksdidevice',
1645 0x32000004 => 'ramdisksdipath',
1646 0x35000005 => 'ramdiskimagelength',
1647 0x36000006 => 'exportascd',
1648 0x35000007 => 'ramdisktftpblocksize',
1649 0x35000008 => 'ramdisktftpwindowsize',
1650 0x36000009 => 'ramdiskmcenabled',
1651 0x3600000a => 'ramdiskmctftpfallback',
1652 0x3600000b => 'ramdisktftpvarwindow',
1653 },
1654 memdiag => {
1655 0x25000001 => 'passcount',
1656 0x25000002 => 'testmix',
1657 0x25000003 => 'failurecount',
1658 0x26000003 => 'cacheenable',
1659 0x25000004 => 'testtofail',
1660 0x26000004 => 'failuresenabled',
1661 0x25000005 => 'stridefailcount',
1662 0x26000005 => 'cacheenable',
1663 0x25000006 => 'invcfailcount',
1664 0x25000007 => 'matsfailcount',
1665 0x25000008 => 'randfailcount',
1666 0x25000009 => 'chckrfailcount',
1667 },
1668 ntldr => {
1669 0x22000001 => 'bpbstring',
1670 },
1671 osloader => {
1672 0x21000001 => 'osdevice',
1673 0x22000002 => 'systemroot',
1674 0x23000003 => 'resumeobject',
1675 0x26000004 => 'stampdisks',
1676 0x26000010 => 'detecthal',
1677 0x22000011 => 'kernel',
1678 0x22000012 => 'hal',
1679 0x22000013 => 'dbgtransport',
1680 0x25000020 => 'nx',
1681 0x25000021 => 'pae',
1682 0x26000022 => 'winpe',
1683 0x26000024 => 'nocrashautoreboot',
1684 0x26000025 => 'lastknowngood',
1685 0x26000026 => 'oslnointegritychecks',
1686 0x26000027 => 'osltestsigning',
1687 0x26000030 => 'nolowmem',
1688 0x25000031 => 'removememory',
1689 0x25000032 => 'increaseuserva',
1690 0x25000033 => 'perfmem',
1691 0x26000040 => 'vga',
1541 0x26000041 => 'quietboot', 1692 0x26000041 => 'quietboot',
1542 0x26000042 => 'novesa', 1693 0x26000042 => 'novesa',
1543 0x26000043 => 'novga', 1694 0x26000043 => 'novga',
1544 0x25000050 => 'clustermodeaddressing', 1695 0x25000050 => 'clustermodeaddressing',
1545 0x26000051 => 'usephysicaldestination', 1696 0x26000051 => 'usephysicaldestination',
1546 0x25000052 => 'restrictapiccluster', 1697 0x25000052 => 'restrictapiccluster',
1547 0x22000053 => 'evstore', 1698 0x22000053 => 'evstore',
1548 0x26000054 => 'uselegacyapicmode', 1699 0x26000054 => 'uselegacyapicmode',
1549 0x26000060 => 'onecpu', 1700 0x26000060 => 'onecpu',
1550 0x25000061 => 'numproc', 1701 0x25000061 => 'numproc',
1551 0x26000062 => 'maxproc', 1702 0x26000062 => 'maxproc',
1552 0x25000063 => 'configflags', 1703 0x25000063 => 'configflags',
1553 0x26000064 => 'maxgroup', 1704 0x26000064 => 'maxgroup',
1554 0x26000065 => 'groupaware', 1705 0x26000065 => 'groupaware',
1555 0x25000066 => 'groupsize', 1706 0x25000066 => 'groupsize',
1556 0x26000070 => 'usefirmwarepcisettings', 1707 0x26000070 => 'usefirmwarepcisettings',
1557 0x25000071 => 'msi', 1708 0x25000071 => 'msi',
1558 0x25000072 => 'pciexpress', 1709 0x25000072 => 'pciexpress',
1559 0x25000080 => 'safeboot', 1710 0x25000080 => 'safeboot',
1560 0x26000081 => 'safebootalternateshell', 1711 0x26000081 => 'safebootalternateshell',
1561 0x26000090 => 'bootlog', 1712 0x26000090 => 'bootlog',
1562 0x26000091 => 'sos', 1713 0x26000091 => 'sos',
1563 0x260000a0 => 'debug', 1714 0x260000a0 => 'debug',
1564 0x260000a1 => 'halbreakpoint', 1715 0x260000a1 => 'halbreakpoint',
1565 0x260000a2 => 'useplatformclock', 1716 0x260000a2 => 'useplatformclock',
1566 0x260000a3 => 'forcelegacyplatform', 1717 0x260000a3 => 'forcelegacyplatform',
1567 0x260000a4 => 'useplatformtick', 1718 0x260000a4 => 'useplatformtick',
1568 0x260000a5 => 'disabledynamictick', 1719 0x260000a5 => 'disabledynamictick',
1569 0x250000a6 => 'tscsyncpolicy', 1720 0x250000a6 => 'tscsyncpolicy',
1570 0x260000b0 => 'ems', 1721 0x260000b0 => 'ems',
1571 0x250000c0 => 'forcefailure', 1722 0x250000c0 => 'forcefailure',
1572 0x250000c1 => 'driverloadfailurepolicy', 1723 0x250000c1 => 'driverloadfailurepolicy',
1573 0x250000c2 => 'bootmenupolicy', 1724 0x250000c2 => 'bootmenupolicy',
1574 0x260000c3 => 'onetimeadvancedoptions', 1725 0x260000c3 => 'onetimeadvancedoptions',
1575 0x260000c4 => 'onetimeoptionsedit', 1726 0x260000c4 => 'onetimeoptionsedit',
1576 0x250000e0 => 'bootstatuspolicy', 1727 0x250000e0 => 'bootstatuspolicy',
1577 0x260000e1 => 'disableelamdrivers', 1728 0x260000e1 => 'disableelamdrivers',
1578 0x250000f0 => 'hypervisorlaunchtype', 1729 0x250000f0 => 'hypervisorlaunchtype',
1579 0x220000f1 => 'hypervisorpath', 1730 0x220000f1 => 'hypervisorpath',
1580 0x260000f2 => 'hypervisordebug', 1731 0x260000f2 => 'hypervisordebug',
1581 0x250000f3 => 'hypervisordebugtype', 1732 0x250000f3 => 'hypervisordebugtype',
1582 0x250000f4 => 'hypervisordebugport', 1733 0x250000f4 => 'hypervisordebugport',
1583 0x250000f5 => 'hypervisorbaudrate', 1734 0x250000f5 => 'hypervisorbaudrate',
1584 0x250000f6 => 'hypervisorchannel', 1735 0x250000f6 => 'hypervisorchannel',
1585 0x250000f7 => 'bootux', 1736 0x250000f7 => 'bootux',
1586 0x260000f8 => 'hypervisordisableslat', 1737 0x260000f8 => 'hypervisordisableslat',
1587 0x220000f9 => 'hypervisorbusparams', 1738 0x220000f9 => 'hypervisorbusparams',
1588 0x250000fa => 'hypervisornumproc', 1739 0x250000fa => 'hypervisornumproc',
1589 0x250000fb => 'hypervisorrootprocpernode', 1740 0x250000fb => 'hypervisorrootprocpernode',
1590 0x260000fc => 'hypervisoruselargevtlb', 1741 0x260000fc => 'hypervisoruselargevtlb',
1591 0x250000fd => 'hypervisorhostip', 1742 0x250000fd => 'hypervisorhostip',
1592 0x250000fe => 'hypervisorhostport', 1743 0x250000fe => 'hypervisorhostport',
1593 0x250000ff => 'hypervisordebugpages', 1744 0x250000ff => 'hypervisordebugpages',
1594 0x25000100 => 'tpmbootentropy', 1745 0x25000100 => 'tpmbootentropy',
1595 0x22000110 => 'hypervisorusekey', 1746 0x22000110 => 'hypervisorusekey',
1596 0x22000112 => 'hypervisorproductskutype', 1747 0x22000112 => 'hypervisorproductskutype',
1597 0x25000113 => 'hypervisorrootproc', 1748 0x25000113 => 'hypervisorrootproc',
1598 0x26000114 => 'hypervisordhcp', 1749 0x26000114 => 'hypervisordhcp',
1599 0x25000115 => 'hypervisoriommupolicy', 1750 0x25000115 => 'hypervisoriommupolicy',
1600 0x26000116 => 'hypervisorusevapic', 1751 0x26000116 => 'hypervisorusevapic',
1601 0x22000117 => 'hypervisorloadoptions', 1752 0x22000117 => 'hypervisorloadoptions',
1602 0x25000118 => 'hypervisormsrfilterpolicy', 1753 0x25000118 => 'hypervisormsrfilterpolicy',
1603 0x25000119 => 'hypervisormmionxpolicy', 1754 0x25000119 => 'hypervisormmionxpolicy',
1604 0x2500011a => 'hypervisorschedulertype', 1755 0x2500011a => 'hypervisorschedulertype',
1605 0x25000120 => 'xsavepolicy', 1756 0x25000120 => 'xsavepolicy',
1606 0x25000121 => 'xsaveaddfeature0', 1757 0x25000121 => 'xsaveaddfeature0',
1607 0x25000122 => 'xsaveaddfeature1', 1758 0x25000122 => 'xsaveaddfeature1',
1608 0x25000123 => 'xsaveaddfeature2', 1759 0x25000123 => 'xsaveaddfeature2',
1609 0x25000124 => 'xsaveaddfeature3', 1760 0x25000124 => 'xsaveaddfeature3',
1610 0x25000125 => 'xsaveaddfeature4', 1761 0x25000125 => 'xsaveaddfeature4',
1611 0x25000126 => 'xsaveaddfeature5', 1762 0x25000126 => 'xsaveaddfeature5',
1612 0x25000127 => 'xsaveaddfeature6', 1763 0x25000127 => 'xsaveaddfeature6',
1613 0x25000128 => 'xsaveaddfeature7', 1764 0x25000128 => 'xsaveaddfeature7',
1614 0x25000129 => 'xsaveremovefeature', 1765 0x25000129 => 'xsaveremovefeature',
1615 0x2500012a => 'xsaveprocessorsmask', 1766 0x2500012a => 'xsaveprocessorsmask',
1616 0x2500012b => 'xsavedisable', 1767 0x2500012b => 'xsavedisable',
1617 0x2500012c => 'kerneldebugtype', 1768 0x2500012c => 'kerneldebugtype',
1618 0x2200012d => 'kernelbusparams', 1769 0x2200012d => 'kernelbusparams',
1619 0x2500012e => 'kerneldebugaddress', 1770 0x2500012e => 'kerneldebugaddress',
1620 0x2500012f => 'kerneldebugport', 1771 0x2500012f => 'kerneldebugport',
1621 0x25000130 => 'claimedtpmcounter', 1772 0x25000130 => 'claimedtpmcounter',
1622 0x25000131 => 'kernelchannel', 1773 0x25000131 => 'kernelchannel',
1623 0x22000132 => 'kerneltargetname', 1774 0x22000132 => 'kerneltargetname',
1624 0x25000133 => 'kernelhostip', 1775 0x25000133 => 'kernelhostip',
1625 0x25000134 => 'kernelport', 1776 0x25000134 => 'kernelport',
1626 0x26000135 => 'kerneldhcp', 1777 0x26000135 => 'kerneldhcp',
1627 0x22000136 => 'kernelkey', 1778 0x22000136 => 'kernelkey',
1628 0x22000137 => 'imchivename', 1779 0x22000137 => 'imchivename',
1629 0x21000138 => 'imcdevice', 1780 0x21000138 => 'imcdevice',
1630 0x25000139 => 'kernelbaudrate', 1781 0x25000139 => 'kernelbaudrate',
1631 0x22000140 => 'mfgmode', 1782 0x22000140 => 'mfgmode',
1632 0x26000141 => 'event', 1783 0x26000141 => 'event',
1633 0x25000142 => 'vsmlaunchtype', 1784 0x25000142 => 'vsmlaunchtype',
1634 0x25000144 => 'hypervisorenforcedcodeintegrity', 1785 0x25000144 => 'hypervisorenforcedcodeintegrity',
1635 0x26000145 => 'enablebootdebugpolicy',
1636 0x26000146 => 'enablebootorderclean',
1637 0x26000147 => 'enabledeviceid',
1638 0x26000148 => 'enableffuloader',
1639 0x26000149 => 'enableiuloader',
1640 0x2600014a => 'enablemassstorage',
1641 0x2600014b => 'enablerpmbprovisioning',
1642 0x2600014c => 'enablesecurebootpolicy',
1643 0x2600014d => 'enablestartcharge',
1644 0x2600014e => 'enableresettpm',
1645 0x21000150 => 'systemdatadevice', 1786 0x21000150 => 'systemdatadevice',
1646 0x21000151 => 'osarcdevice', 1787 0x21000151 => 'osarcdevice',
1647 0x21000153 => 'osdatadevice', 1788 0x21000153 => 'osdatadevice',
1648 0x21000154 => 'bspdevice', 1789 0x21000154 => 'bspdevice',
1649 0x21000155 => 'bspfilepath', 1790 0x21000155 => 'bspfilepath',
1650 0x26000202 => 'skipffumode', 1791 },
1651 0x26000203 => 'forceffumode', 1792 resume => {
1652 0x25000510 => 'chargethreshold', 1793 0x21000001 => 'filedevice',
1653 0x26000512 => 'offmodecharging', 1794 0x22000002 => 'filepath',
1654 0x25000aaa => 'bootflow', 1795 0x26000003 => 'customsettings',
1655 0x35000001 => 'ramdiskimageoffset', 1796 0x26000004 => 'pae',
1656 0x35000002 => 'ramdisktftpclientport', 1797 0x21000005 => 'associatedosdevice',
1657 0x31000003 => 'ramdisksdidevice', 1798 0x26000006 => 'debugoptionenabled',
1658 0x32000004 => 'ramdisksdipath', 1799 0x25000007 => 'bootux',
1659 0x35000005 => 'ramdiskimagelength', 1800 0x25000008 => 'bootmenupolicy',
1660 0x36000006 => 'exportascd', 1801 0x26000024 => 'hormenabled',
1661 0x35000007 => 'ramdisktftpblocksize', 1802 },
1662 0x35000008 => 'ramdisktftpwindowsize', 1803 startup => {
1663 0x36000009 => 'ramdiskmcenabled', 1804 0x26000001 => 'pxesoftreboot',
1664 0x3600000a => 'ramdiskmctftpfallback', 1805 0x22000002 => 'applicationname',
1665 0x3600000b => 'ramdisktftpvarwindow', 1806 },
1666 0x45000001 => 'devicetype',
1667 0x42000002 => 'applicationrelativepath',
1668 0x42000003 => 'ramdiskdevicerelativepath',
1669 0x46000004 => 'omitosloaderelements',
1670 0x47000006 => 'elementstomigrate',
1671 0x46000010 => 'recoveryos',
1672); 1807);
1673 1808
1674our %rbcde = reverse %bcde; 1809# mask, value => class
1810our @bcde_typeclass = (
1811 [0x00000000, 0x00000000, 'any'],
1812 [0xf00fffff, 0x1000000a, 'bootapp'],
1813 [0xf0ffffff, 0x2020000a, 'bootapp'],
1814 [0xf00fffff, 0x10000001, 'bootmgr'],
1815 [0xf00fffff, 0x10000002, 'bootmgr'],
1816 [0xf0ffffff, 0x20200001, 'bootmgr'],
1817 [0xf0ffffff, 0x20200002, 'bootmgr'],
1818 [0xf0f00000, 0x20300000, 'device'],
1819 [0xf0000000, 0x30000000, 'device'],
1820 [0xf00fffff, 0x10000005, 'memdiag'],
1821 [0xf0ffffff, 0x20200005, 'memdiag'],
1822 [0xf00fffff, 0x10000006, 'ntldr'],
1823 [0xf00fffff, 0x10000007, 'ntldr'],
1824 [0xf0ffffff, 0x20200006, 'ntldr'],
1825 [0xf0ffffff, 0x20200007, 'ntldr'],
1826 [0xf00fffff, 0x10000003, 'osloader'],
1827 [0xf0ffffff, 0x20200003, 'osloader'],
1828 [0xf00fffff, 0x10000004, 'resume'],
1829 [0xf0ffffff, 0x20200004, 'resume'],
1830 [0xf00fffff, 0x10000009, 'startup'],
1831 [0xf0ffffff, 0x20200009, 'startup'],
1832);
1675 1833
1834our %rbcde_byclass;
1835
1836while (my ($k, $v) = each %bcde_byclass) {
1837 $rbcde_byclass{$k} = { reverse %$v };
1838}
1839
1840# decodes (numerical elem, type) to name
1676sub dec_bcde_id($) { 1841sub dec_bcde_id($$) {
1842 for my $class (@bcde_typeclass) {
1843 if (($_[1] & $class->[0]) == $class->[1]) {
1844 if (my $id = $bcde_byclass{$class->[2]}{$_[0]}) {
1845 return $id;
1846 }
1847 }
1848 }
1849
1677 $bcde{$_[0]} // sprintf "custom:%08x", $_[0] 1850 sprintf "custom:%08x", $_[0]
1678} 1851}
1679 1852
1853# encodes (elem as name, type)
1680sub enc_bcde_id($) { 1854sub enc_bcde_id($$) {
1681 $_[0] =~ /^custom:([0-9a-fA-F]{8}$)/ 1855 $_[0] =~ /^custom:(?:0x)?([0-9a-fA-F]{8}$)/
1682 ? hex $1 1856 and return hex $1;
1683 : $rbcde{$_[0]} 1857
1858 for my $class (@bcde_typeclass) {
1859 if (($_[1] & $class->[0]) == $class->[1]) {
1860 if (my $value = $rbcde_byclass{$class->[2]}{$_[0]}) {
1861 return $value;
1862 }
1863 }
1864 }
1865
1866 undef
1684} 1867}
1685 1868
1686# decode/encode bcd device element - the horror, no documentaion 1869# decode/encode bcd device element - the horror, no documentaion
1687# whatsoever, supercomplex, superinconsistent. 1870# whatsoever, supercomplex, superinconsistent.
1688 1871
1692 1875
1693our $NULL_DEVICE = "\x00" x 16; 1876our $NULL_DEVICE = "\x00" x 16;
1694 1877
1695# biggest bitch to decode, ever 1878# biggest bitch to decode, ever
1696# this decoded a device portion after the GUID 1879# this decoded a device portion after the GUID
1697sub dec_device_($); 1880sub dec_device_($$);
1698sub dec_device_($) { 1881sub dec_device_($$) {
1699 my ($device) = @_; 1882 my ($device, $type) = @_;
1700 1883
1701 my $res; 1884 my $res;
1702 1885
1703 my ($type, $flags, $length, $pad) = unpack "VVVV", substr $device, 0, 4 * 4, ""; 1886 my ($type, $flags, $length, $pad) = unpack "VVVV", substr $device, 0, 4 * 4, "";
1704 1887
1749 1932
1750 my $partid = $parttype eq "gpt" ? dec_guid $partdata 1933 my $partid = $parttype eq "gpt" ? dec_guid $partdata
1751 : $type eq "partition" ? unpack "Q<", $partdata # byte offset to partition start 1934 : $type eq "partition" ? unpack "Q<", $partdata # byte offset to partition start
1752 : unpack "L<", $partdata; # partition number, one-based 1935 : unpack "L<", $partdata; # partition number, one-based
1753 1936
1754 (my $parent, $device) = dec_device_ $device; 1937 (my $parent, $device) = dec_device_ $device, $type;
1755 1938
1756 $res .= "="; 1939 $res .= "=";
1757 $res .= "<$parent>"; 1940 $res .= "<$parent>";
1758 $res .= ",$blocktype,$parttype,$diskid,$partid"; 1941 $res .= ",$blocktype,$parttype,$diskid,$partid";
1759 1942
1779 or die "unsupported file descriptor version '$fver'\n"; 1962 or die "unsupported file descriptor version '$fver'\n";
1780 1963
1781 $ftype == 5 1964 $ftype == 5
1782 or die "unsupported file descriptor path type '$type'\n"; 1965 or die "unsupported file descriptor path type '$type'\n";
1783 1966
1784 (my $parent, $path) = dec_device_ $path; 1967 (my $parent, $path) = dec_device_ $path, $type;
1785 1968
1786 $path = $dec_path->($path, "file device without path"); 1969 $path = $dec_path->($path, "file device without path");
1787 1970
1788 ($parent, $path) 1971 ($parent, $path)
1789 }; 1972 };
1795 1978
1796 } elsif ($blocktype eq "vhd") { 1979 } elsif ($blocktype eq "vhd") {
1797 $device =~ s/^\x00{20}//s 1980 $device =~ s/^\x00{20}//s
1798 or die "virtualdisk has non-zero fields I don't understand\n"; 1981 or die "virtualdisk has non-zero fields I don't understand\n";
1799 1982
1800 (my $parent, $device) = dec_device_ $device; 1983 (my $parent, $device) = dec_device_ $device, $type;
1801 1984
1802 $res .= "=vhd,<$parent>"; 1985 $res .= "=vhd,<$parent>";
1803 1986
1804 } elsif ($blocktype eq "ramdisk") { 1987 } elsif ($blocktype eq "ramdisk") {
1805 my ($base, $size, $offset) = unpack "Q< Q< L<", substr $device, 0, 8 + 8 + 4, ""; 1988 my ($base, $size, $offset) = unpack "Q< Q< L<", substr $device, 0, 8 + 8 + 4, "";
1818 my ($mode, $elem, $parent) = unpack "VVV", substr $device, 0, 4 * 3, ""; 2001 my ($mode, $elem, $parent) = unpack "VVV", substr $device, 0, 4 * 3, "";
1819 2002
1820 if ($parent) { 2003 if ($parent) {
1821 # not sure why this is an offset - it must come after the path 2004 # not sure why this is an offset - it must come after the path
1822 $parent = substr $device, $parent - 4 * 3 - 4 * 4, 1e9, ""; 2005 $parent = substr $device, $parent - 4 * 3 - 4 * 4, 1e9, "";
1823 ($parent, my $tail) = dec_device_ $parent; 2006 ($parent, my $tail) = dec_device_ $parent, $type;
1824 0 == length $tail 2007 0 == length $tail
1825 or die "trailing data after locate device parent\n"; 2008 or die "trailing data after locate device parent\n";
1826 } else { 2009 } else {
1827 $parent = "null"; 2010 $parent = "null";
1828 } 2011 }
1834 2017
1835 if ($mode == 0) { # "Element" 2018 if ($mode == 0) { # "Element"
1836 !length $path 2019 !length $path
1837 or die "device locate mode 0 having non-empty path ($mode, $elem, $path)\n"; 2020 or die "device locate mode 0 having non-empty path ($mode, $elem, $path)\n";
1838 2021
1839 $elem = dec_bcde_id $elem; 2022 $elem = dec_bcde_id $elem, $type;
1840 $res .= "element,$elem"; 2023 $res .= "element,$elem";
1841 2024
1842 } elsif ($mode == 1) { # "String" 2025 } elsif ($mode == 1) { # "String"
1843 !$elem 2026 !$elem
1844 or die "device locate mode 1 having non-zero element\n"; 2027 or die "device locate mode 1 having non-zero element\n";
1869 2052
1870 ($res, $tail) 2053 ($res, $tail)
1871} 2054}
1872 2055
1873# decode a full binary BCD device descriptor 2056# decode a full binary BCD device descriptor
1874sub dec_device($) { 2057sub dec_device($$) {
1875 my ($device) = @_; 2058 my ($device, $type) = @_;
1876 2059
1877 $device = pack "H*", $device; 2060 $device = pack "H*", $device;
1878 2061
1879 my $guid = dec_guid substr $device, 0, 16, ""; 2062 my $guid = dec_guid substr $device, 0, 16, "";
1880 $guid = $guid eq "00000000-0000-0000-0000-000000000000" 2063 $guid = $guid eq "00000000-0000-0000-0000-000000000000"
1881 ? "" : "{$guid}"; 2064 ? "" : "{$guid}";
1882 2065
1883 eval { 2066 eval {
1884 my ($dev, $tail) = dec_device_ $device; 2067 my ($dev, $tail) = dec_device_ $device, $type;
1885 2068
1886 $tail eq "" 2069 $tail eq ""
1887 or die "unsupported trailing data after device descriptor\n"; 2070 or die "unsupported trailing data after device descriptor\n";
1888 2071
1889 "$guid$dev" 2072 "$guid$dev"
1901 2084
1902 undef 2085 undef
1903} 2086}
1904 2087
1905# encode the device portion after the GUID 2088# encode the device portion after the GUID
1906sub enc_device_; 2089sub enc_device_($$);
1907sub enc_device_ { 2090sub enc_device_($$) {
1908 my ($device) = @_; 2091 my ($device, $type) = @_;
1909 2092
1910 my $enc_path = sub { 2093 my $enc_path = sub {
1911 my $path = shift; 2094 my $path = shift;
1912 $path =~ s/\//\\/g; 2095 $path =~ s/\//\\/g;
1913 (Encode::encode "UTF-16LE", $path) . "\x00\x00" 2096 (Encode::encode "UTF-16LE", $path) . "\x00\x00"
1931 2114
1932 my $parse_parent = sub { 2115 my $parse_parent = sub {
1933 my $parent; 2116 my $parent;
1934 2117
1935 if (s/^<//) { 2118 if (s/^<//) {
1936 ($parent, $_) = enc_device_ $_; 2119 ($parent, $_) = enc_device_ $_, $type;
1937 s/^>// 2120 s/^>//
1938 or die "$device: syntax error: parent device not followed by '>'\n"; 2121 or die "$device: syntax error: parent device not followed by '>'\n";
1939 } else { 2122 } else {
1940 $parent = $NULL_DEVICE; 2123 $parent = $NULL_DEVICE;
1941 } 2124 }
2029 2212
2030 s/^,// 2213 s/^,//
2031 or die "$_: missing comma after locate parent device\n"; 2214 or die "$_: missing comma after locate parent device\n";
2032 2215
2033 if (s/^element,//) { 2216 if (s/^element,//) {
2034 s/^([0-9a-z]+)//i 2217 s/^([0-9a-z:]+)//i
2035 or die "$_ locate element must be either name or 8-digit hex id\n"; 2218 or die "$_ locate element must be either name or 8-digit hex id\n";
2036 $elem = enc_bcde_id $1; 2219 $elem = enc_bcde_id $1, $type;
2037 $mode = 0; 2220 $mode = 0;
2038 $path = $enc_path->(""); 2221 $path = $enc_path->("");
2039 2222
2040 } elsif (s/^path,//) { 2223 } elsif (s/^path,//) {
2041 $mode = 1; 2224 $mode = 1;
2108 or die "$_: malformed or missing vmbus interface instance guid\n"; 2291 or die "$_: malformed or missing vmbus interface instance guid\n";
2109 my $instance = enc_guid $1; 2292 my $instance = enc_guid $1;
2110 2293
2111 $payload = pack "a16a16x24", $type, $instance; 2294 $payload = pack "a16a16x24", $type, $instance;
2112 2295
2296# } elsif ($type eq "udp") {
2297# $payload = pack "Va16", 1, "12345678";
2298
2113 } else { 2299 } else {
2114 die "$type: not a supported device type (binary, null, boot, legacypartition, partition, block, locate)\n"; 2300 die "$type: not a supported device type (binary, null, boot, legacypartition, partition, block, locate)\n";
2115 } 2301 }
2116 2302
2117 return ( 2303 return (
2120 ); 2306 );
2121 } 2307 }
2122} 2308}
2123 2309
2124# encode a full binary BCD device descriptor 2310# encode a full binary BCD device descriptor
2125sub enc_device { 2311sub enc_device($$) {
2126 my ($device) = @_; 2312 my ($device, $type) = @_;
2127 2313
2128 my $guid = "\x00" x 16; 2314 my $guid = "\x00" x 16;
2129 2315
2130 if ($device =~ s/^\{([A-Za-z0-9\-]+)\}//) { 2316 if ($device =~ s/^\{([A-Za-z0-9\-]+)\}//) {
2131 $guid = enc_guid $1 2317 $guid = enc_guid $1
2132 or die "$device: does not start with valid guid\n"; 2318 or die "$device: does not start with valid guid\n";
2133 } 2319 }
2134 2320
2135 my ($descriptor, $tail) = enc_device_ $device; 2321 my ($descriptor, $tail) = enc_device_ $device, $type;
2136 2322
2137 length $tail 2323 length $tail
2138 and die "$device: garbage after device descriptor\n"; 2324 and die "$device: garbage after device descriptor\n";
2139 2325
2140 unpack "H*", $guid . $descriptor 2326 unpack "H*", $guid . $descriptor
2155 $k = $bcd_objects{$k} // $k; 2341 $k = $bcd_objects{$k} // $k;
2156 2342
2157 my $type = $v->{Description}[0]{Type}[1]; 2343 my $type = $v->{Description}[0]{Type}[1];
2158 2344
2159 if ($type != $bcd_object_types{$k}) { 2345 if ($type != $bcd_object_types{$k}) {
2160 $type = $bcd_types{$type} // sprintf "0x%08x", $type; 2346 $kv{type} = $bcd_types{$type} // sprintf "0x%08x", $type;
2161 $kv{type} = $type;
2162 } 2347 }
2163 2348
2164 my $elems = $v->{Elements}[1]; 2349 my $elems = $v->{Elements}[1];
2165 2350
2166 while (my ($k, $v) = each %$elems) { 2351 while (my ($k, $v) = each %$elems) {
2167 my $k = hex $k; 2352 my $k = hex $k;
2168 2353
2169 my $v = $bcde_dec{$k & BCDE_FORMAT}->($v->[0]{Element}[1]); 2354 my $v = $bcde_dec{$k & BCDE_FORMAT}->($v->[0]{Element}[1], $type);
2170 my $k = dec_bcde_id $k; 2355 my $k = dec_bcde_id $k, $type;
2171 2356
2172 $kv{$k} = $v; 2357 $kv{$k} = $v;
2173 } 2358 }
2174 2359
2175 $bcd{$k} = \%kv; 2360 $bcd{$k} = \%kv;
2216 my %elem; 2401 my %elem;
2217 2402
2218 while (my ($k, $v) = each %$v) { 2403 while (my ($k, $v) = each %$v) {
2219 next if $k eq "type"; 2404 next if $k eq "type";
2220 2405
2221 $k = (enc_bcde_id $k) // die "$k: invalid bcde element name or id\n"; 2406 $k = (enc_bcde_id $k, $type) // die "$k: invalid bcde element name or id\n";
2222 $elem{sprintf "%08x", $k} = [{ 2407 $elem{sprintf "%08x", $k} = [{
2223 Element => [ ($bcde_enc{$k & BCDE_FORMAT} // die "$k: unable to encode unknown bcd element type}")->($v)] 2408 Element => [ ($bcde_enc{$k & BCDE_FORMAT} // die "$k: unable to encode unknown bcd element type}")->($v)]
2224 }]; 2409 }];
2225 } 2410 }
2226 2411
2242 Objects => [undef, \%objects], 2427 Objects => [undef, \%objects],
2243 }]] 2428 }]]
2244} 2429}
2245 2430
2246############################################################################# 2431#############################################################################
2432# edit instructions
2247 2433
2248sub bcd_edit_eval { 2434sub bcd_edit_eval {
2249 package pbcdedit; 2435 package pbcdedit;
2250 2436
2251 our ($PATH, $BCD, $DEFAULT); 2437 our ($PATH, $BCD, $DEFAULT);
2255} 2441}
2256 2442
2257sub bcd_edit { 2443sub bcd_edit {
2258 my ($path, $bcd, @insns) = @_; 2444 my ($path, $bcd, @insns) = @_;
2259 2445
2260 my $default = $bcd->{"{bootmgr}"}{resumeobject}; 2446 my $default = $bcd->{"{bootmgr}"}{default};
2261 2447
2262 # prepare "officially visible" variables 2448 # prepare "officially visible" variables
2263 local $pbcdedit::PATH = $path; 2449 local $pbcdedit::PATH = $path;
2264 local $pbcdedit::BCD = $bcd; 2450 local $pbcdedit::BCD = $bcd;
2265 local $pbcdedit::DEFAULT = $default; 2451 local $pbcdedit::DEFAULT = $default;
2269 2455
2270 if ($insn eq "get") { 2456 if ($insn eq "get") {
2271 my $object = shift @insns; 2457 my $object = shift @insns;
2272 my $elem = shift @insns; 2458 my $elem = shift @insns;
2273 2459
2274 $object = $default if $object eq "{default}"; 2460 $object = $object eq "{default}" ? $default : dec_wguid enc_wguid $object;
2275 2461
2276 print $bcd->{$object}{$elem}, "\n"; 2462 print $bcd->{$object}{$elem}, "\n";
2277 2463
2278 } elsif ($insn eq "set") { 2464 } elsif ($insn eq "set") {
2279 my $object = shift @insns; 2465 my $object = shift @insns;
2280 my $elem = shift @insns; 2466 my $elem = shift @insns;
2281 my $value = shift @insns; 2467 my $value = shift @insns;
2282 2468
2283 $object = $default if $object eq "{default}"; 2469 $object = $object eq "{default}" ? $default : dec_wguid enc_wguid $object;
2284 2470
2285 $bcd->{$object}{$elem} = $value; 2471 $bcd->{$object}{$elem} = $value;
2286 2472
2287 } elsif ($insn eq "eval") { 2473 } elsif ($insn eq "eval") {
2288 bcd_edit_eval shift @insns; 2474 my $perl = shift @insns;
2475 bcd_edit_eval "#line 1 'eval'\n$perl";
2289 2476
2290 } elsif ($insn eq "do") { 2477 } elsif ($insn eq "do") {
2291 my $path = shift @insns; 2478 my $path = shift @insns;
2292 my $file = file_load $path; 2479 my $file = file_load $path;
2293 bcd_edit_eval "#line 1 '$path'\n$file"; 2480 bcd_edit_eval "#line 1 '$path'\n$file";
2298 } 2485 }
2299 2486
2300} 2487}
2301 2488
2302############################################################################# 2489#############################################################################
2490# other utilities
2303 2491
2304# json to stdout 2492# json to stdout
2305sub prjson($) { 2493sub prjson($) {
2306 print $json_coder->encode ($_[0]); 2494 print $json_coder->encode ($_[0]);
2307} 2495}
2311 my $json; 2499 my $json;
2312 1 while read STDIN, $json, 65536, length $json; 2500 1 while read STDIN, $json, 65536, length $json;
2313 $json_coder->decode ($json) 2501 $json_coder->decode ($json)
2314} 2502}
2315 2503
2316# all subcommands 2504sub lsblk() {
2505 my $lsblk = $json_coder->decode (scalar qx<lsblk --json -o PATH,KNAME,MAJ:MIN,TYPE,PTTYPE,PTUUID,PARTUUID,LABEL,FSTYPE>);
2506
2507 for my $dev (@{ $lsblk->{blockdevices} }) {
2508 if ($dev->{type} eq "part") {
2509
2510 # lsblk sometimes gives a bogus pttype, so we recreate it here
2511 $dev->{pttype} = $dev->{ptuuid} =~ /^$RE_GUID\z/
2512 ? "gpt" : "dos";
2513
2514 if ($dev->{pttype} eq "gpt") {
2515 $dev->{bcd_device} = "partition=<null>,harddisk,gpt,$dev->{ptuuid},$dev->{partuuid}";
2516 } elsif ($dev->{pttype} eq "dos") { # why not "mbr" :(
2517 if ($dev->{partuuid} =~ /^([0-9a-f]{8})-([0-9a-f]{2})\z/i) {
2518 my ($diskid, $partno) = ($1, hex $2);
2519 $dev->{bcd_legacy_device} = "legacypartition=<null>,harddisk,mbr,$diskid,$partno";
2520 if (open my $fh, "/sys/class/block/$dev->{kname}/start") {
2521 my $start = 512 * readline $fh;
2522 $dev->{bcd_device} = "partition=<null>,harddisk,mbr,$diskid,$start";
2523 }
2524 }
2525 }
2526 }
2527 }
2528
2529 $lsblk->{blockdevices}
2530}
2531
2532sub prdev($$) {
2533 my ($path, $attribute) = @_;
2534
2535 # rather than stat'ing and guessing how devices are encoded, we use lsblk for this
2536 my $mm = $json_coder->decode (scalar qx<lsblk -d -o MAJ:MIN -J \Q$path\E>)->{blockdevices}[0]{"maj:min"};
2537
2538 my $lsblk = lsblk;
2539
2540 for my $dev (@$lsblk) {
2541 if ($dev->{"maj:min"} eq $mm && $dev->{$attribute}) {
2542 say $dev->{$attribute};
2543 exit 0;
2544 }
2545 }
2546
2547 exit 1;
2548}
2549
2550#############################################################################
2551# command line parser
2552
2317our %CMD = ( 2553our %CMD = (
2318 help => sub { 2554 help => sub {
2319 require Pod::Usage; 2555 require Pod::Usage;
2320 Pod::Usage::pod2usage (-verbose => 2); 2556 Pod::Usage::pod2usage (-verbose => 2);
2321 }, 2557 },
2346 2582
2347 print "\n"; 2583 print "\n";
2348 2584
2349 printf "%-39s %-23s %s\n", "Object GUID", "Alias", "(Hex) Default Type"; 2585 printf "%-39s %-23s %s\n", "Object GUID", "Alias", "(Hex) Default Type";
2350 for my $name (sort keys %rbcd_objects) { 2586 for my $name (sort keys %rbcd_objects) {
2351 my $guid = $rbcd_objects{$name}; 2587 my $guid = $rbcd_objects{$name};
2352 my $type = $bcd_object_types{$name}; 2588 my $type = $bcd_object_types{$name};
2353 my $tname = $bcd_types{$type}; 2589 my $tname = $bcd_types{$type};
2354 2590
2355 $type = $type ? sprintf "(%08x) %s", $type, $tname : "-"; 2591 $type = $type ? sprintf "(%08x) %s", $type, $tname : "-";
2356 2592
2357 printf "%-39s %-23s %s\n", $guid, $name, $type; 2593 printf "%-39s %-23s %s\n", $guid, $name, $type;
2371 BCDE_FORMAT_GUID_LIST , "guid list", 2607 BCDE_FORMAT_GUID_LIST , "guid list",
2372 BCDE_FORMAT_INTEGER , "integer", 2608 BCDE_FORMAT_INTEGER , "integer",
2373 BCDE_FORMAT_BOOLEAN , "boolean", 2609 BCDE_FORMAT_BOOLEAN , "boolean",
2374 BCDE_FORMAT_INTEGER_LIST, "integer list", 2610 BCDE_FORMAT_INTEGER_LIST, "integer list",
2375 ); 2611 );
2376 my %rbcde = reverse %bcde;
2377 $_ = sprintf "%08x", $_ for values %rbcde;
2378 2612
2379 my %element; 2613 my @element;
2380 2614
2615 for my $class (sort keys %rbcde_byclass) {
2616 my $rbcde = $rbcde_byclass{$class};
2617
2381 unless ($json) { 2618 unless ($json) {
2382 print "\n"; 2619 print "\n";
2620 printf "Elements applicable to class(es): $class\n";
2383 printf "%-9s %-12s %s\n", "Element", "Format", "Name Alias"; 2621 printf "%-9s %-12s %s\n", "Element", "Format", "Name Alias";
2384 } 2622 }
2385 for my $name (sort keys %rbcde) { 2623 for my $name (sort keys %$rbcde) {
2386 my $id = $rbcde{$name}; 2624 my $id = $rbcde->{$name};
2387 my $format = $format_name{(hex $id) & BCDE_FORMAT}; 2625 my $format = $format_name{$id & BCDE_FORMAT};
2388 2626
2389 if ($json) { 2627 if ($json) {
2390 $element{$id} = [$format, $name]; 2628 push @element, [$class, $id * 1, $format, $name];
2391 } else { 2629 } else {
2630 $id = sprintf "%08x", $id;
2392 printf "%-9s %-12s %s\n", $id, $format, $name; 2631 printf "%-9s %-12s %s\n", $id, $format, $name;
2632 }
2393 } 2633 }
2394 } 2634 }
2395 print "\n" unless $json; 2635 print "\n" unless $json;
2396 2636
2397 prjson { 2637 prjson {
2398 version => $JSON_VERSION, 2638 version => $JSON_VERSION,
2399 element => \%element, 2639 element => \@element,
2640 class => \@bcde_typeclass,
2400 } if $json; 2641 } if $json;
2401 2642
2402 }, 2643 },
2403 2644
2404 export => sub { 2645 export => sub {
2407 2648
2408 import => sub { 2649 import => sub {
2409 regf_save shift, bcd_encode rdjson; 2650 regf_save shift, bcd_encode rdjson;
2410 }, 2651 },
2411 2652
2653 create => sub {
2654 my $path = shift;
2655 my $stat = stat_get $path; # should actually be done at file load time
2656 my $bcd = { };
2657 bcd_edit $path, $bcd, @_;
2658 regf_save $path, bcd_encode $bcd;
2659 stat_set $path, $stat;
2660 },
2661
2412 edit => sub { 2662 edit => sub {
2413 my $path = shift; 2663 my $path = shift;
2664 my $stat = stat_get $path; # should actually be done at file load time
2414 my $bcd = bcd_decode regf_load $path; 2665 my $bcd = bcd_decode regf_load $path;
2415 bcd_edit $path, $bcd, @_; 2666 bcd_edit $path, $bcd, @_;
2416 regf_save $path, bcd_encode $bcd; 2667 regf_save $path, bcd_encode $bcd;
2668 stat_set $path, $stat;
2417 }, 2669 },
2418 2670
2419 parse => sub { 2671 parse => sub {
2420 my $path = shift; 2672 my $path = shift;
2421 my $bcd = bcd_decode regf_load $path; 2673 my $bcd = bcd_decode regf_load $path;
2430 "import-regf" => sub { 2682 "import-regf" => sub {
2431 regf_save shift, rdjson; 2683 regf_save shift, rdjson;
2432 }, 2684 },
2433 2685
2434 lsblk => sub { 2686 lsblk => sub {
2687 my $json = $_[0] eq "--json";
2688
2689 my $lsblk = lsblk;
2690
2691 if ($json) {
2692 prjson $lsblk;
2693 } else {
2435 printf "%-10s %-8.8s %-6.6s %-3s %s\n", "DEVICE", "LABEL", "FSTYPE", "PT", "DEVICE DESCRIPTOR"; 2694 printf "%-10s %-8.8s %-6.6s %-3s %s\n", "DEVICE", "LABEL", "FSTYPE", "PT", "DEVICE DESCRIPTOR";
2436 2695 for my $dev (@$lsblk) {
2437 my $lsblk = $json_coder->decode (scalar qx<lsblk --json -o PATH,KNAME,TYPE,PTTYPE,PTUUID,PARTUUID,LABEL,FSTYPE>); 2696 for my $bcd ($dev->{bcd_device}, $dev->{bcd_legacy_device}) {
2438
2439 for my $dev (@{ $lsblk->{blockdevices} }) {
2440 my $pr = sub {
2441 printf "%-10s %-8.8s %-6.6s %-3s %s\n", 2697 printf "%-10s %-8.8s %-6.6s %-3s %s\n",
2442 $dev->{path}, $dev->{label}, $dev->{fstype}, $dev->{pttype}, $_[0]; 2698 $dev->{path}, $dev->{label}, $dev->{fstype}, $dev->{pttype}, $bcd
2443 };
2444
2445 if ($dev->{type} eq "part") {
2446 if ($dev->{pttype} eq "gpt") {
2447 $pr->("partition=<null>,harddisk,gpt,$dev->{ptuuid},$dev->{partuuid}");
2448 } elsif ($dev->{pttype} eq "dos") { # why not "mbr" :(
2449 if ($dev->{partuuid} =~ /^([0-9a-f]{8})-([0-9a-f]{2})\z/i) {
2450 my ($diskid, $partno) = ($1, hex $2);
2451 $pr->("legacypartition=<null>,harddisk,mbr,$diskid,$partno");
2452 if (open my $fh, "/sys/class/block/$dev->{kname}/start") {
2453 my $start = 512 * readline $fh;
2454 $pr->("partition=<null>,harddisk,mbr,$diskid,$start");
2455 } 2699 if $bcd;
2456 }
2457 } 2700 }
2458 } 2701 }
2459 } 2702 }
2460 }, 2703 },
2704
2705 "bcd-device" => sub {
2706 prdev shift, "bcd_device";
2707 },
2708
2709 "bcd-legacy-device" => sub {
2710 prdev shift, "bcd_legacy_device";
2711 },
2712
2713 version => sub {
2714 print "\n",
2715 "PBCDEDIT version $VERSION, copyright 2019 Marc A. Lehmann <pbcdedit\@schmorp.de>.\n",
2716 "JSON schema version: $JSON_VERSION\n",
2717 "Licensed under the GNU General Public License Version 3.0, or any later version.\n",
2718 "\n",
2719 $CHANGELOG,
2720 "\n";
2721 },
2461); 2722);
2462 2723
2463my $cmd = shift; 2724my $cmd = shift;
2464 2725
2465unless (exists $CMD{$cmd}) { 2726unless (exists $CMD{$cmd}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines