This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
patch.c
1121 lines (990 loc) · 37.9 KB
/
patch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Patch Implementation
Copyright (c) 2012, Nikolaj Schlej. All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*/
#include "patch_int.h"
// Implementation of GNU memmem function using Boyer-Moore-Horspool algorithm
UINT8* find_pattern(UINT8* string, UINT32 slen, CONST UINT8* pattern, UINT32 plen)
{
UINT32 scan = 0;
UINT32 bad_char_skip[256];
UINT32 last;
if (plen == 0 || !string || !pattern)
return NULL;
for (scan = 0; scan <= 255; scan++)
bad_char_skip[scan] = plen;
last = plen - 1;
for (scan = 0; scan < last; scan++)
bad_char_skip[pattern[scan]] = last - scan;
while (slen >= plen)
{
for (scan = last; string[scan] == pattern[scan]; scan--)
if (scan == 0)
return string;
slen -= bad_char_skip[string[last]];
string += bad_char_skip[string[last]];
}
return NULL;
}
UINT8 calculate_checksum(UINT8* data, UINT32 length)
{
UINT8 counter = 0;
while(length--)
counter += data[length];
return ~counter + 1;
}
VOID int2size(UINT32 size, UINT8* module_size)
{
module_size[2] = (UINT8) ((size) >> 16);
module_size[1] = (UINT8) ((size) >> 8);
module_size[0] = (UINT8) ((size) );
}
UINT32 size2int(UINT8* module_size)
{
return (module_size[2] << 16) +
(module_size[1] << 8) +
module_size[0];
}
UINT8 correct_checksums(UINT8* module)
{
module_header *header;
if(!module)
return ERR_INVALID_ARGUMENT;
header = (module_header*) module;
// Calculating new module checksums
header->header_checksum = 0;
header->data_checksum = 0;
header->header_checksum = calculate_checksum(module, sizeof(module_header) - 1);
header->data_checksum = calculate_checksum(module + sizeof(module_header), size2int(header->size) - sizeof(module_header));
return ERR_PATCHED;
}
UINT8 insert_gap_after(UINT8* module, UINT8* end, UINT32 gap_size)
{
UINT8 *gap;
module_header *header;
module_header *gap_header;
UINT32 size;
UINT32 allignment;
if (!module || !end || end <= module)
return ERR_INVALID_ARGUMENT;
// Checking for existing GAP module
// Determining next module position
header = (module_header *) module;
gap = module + size2int(header->size);
size = gap - module;
if (size % MODULE_ALLIGNMENT)
allignment = MODULE_ALLIGNMENT - size % MODULE_ALLIGNMENT;
else
allignment = 0;
gap += allignment;
// Checking for next module to be GAP
if (find_pattern(gap, sizeof(GAP_UUID), GAP_UUID, sizeof(GAP_UUID)))
{
header = (module_header *) gap;
// Using found GAP module as free space
gap_size += size2int(header->size) + allignment;
}
size = end - module;
if (size % MODULE_ALLIGNMENT)
allignment = MODULE_ALLIGNMENT - size % MODULE_ALLIGNMENT;
else
allignment = 0;
gap_size -= allignment;
if (gap_size < sizeof(module_header))
return ERR_INVALID_ARGUMENT;
memset(end, 0xFF, allignment);
gap = end + allignment;
gap_header = (module_header*) gap;
// Constructing gap header
memcpy(gap_header->guid, GAP_UUID, sizeof(GAP_UUID));
gap_header->type = TYPE_GAP;
gap_header->attributes = ATTRIBUTES_GAP;
gap_header->state = STATE_STD;
int2size(gap_size, gap_header->size);
// Filling gap with 0xFF byte
memset(gap + sizeof(module_header), 0xFF, gap_size - sizeof(module_header));
// Calculating checksums
gap_header->header_checksum = 0;
gap_header->data_checksum = 0;
gap_header->header_checksum = calculate_checksum(gap, sizeof(module_header) - 1);
gap_header->data_checksum = 0xAA;
printf("Gap module inserted after repacked module.\n");
return ERR_PATCHED;
}
UINT8 patch_powermanagement_module(UINT8* module, UINT8 start_patch)
{
module_header *header;
common_section_header *common_header;
compressed_section_header *compressed_header;
guid_section_header *guid_header;
UINT8* data;
UINT32 data_size;
UINT8* compressed;
UINT32 compressed_size;
UINT8* decompressed;
UINT32 decompressed_size;
UINT8* scratch;
UINT32 scratch_size;
UINT8* string;
UINT8 current_patch;
BOOLEAN is_patched;
INT32 module_size_change;
UINT32 grow;
UINT32 freespace_length;
UINT8* end;
UINT8 allignment;
if(!module || start_patch >= PATCHED_PATTERNS_COUNT)
return ERR_INVALID_ARGUMENT;
header = (module_header*) module;
if (header->state != STATE_STD)
return ERR_NOT_MODULE;
data = module + sizeof(module_header);
common_header = (common_section_header*) data;
// PowerManagement and PowerMgmtDxe modules, continue execution
if (common_header->type == SECTION_DXE_DEPEX)
data += size2int(common_header->size);
// PowerManagement2.efi module special case
else if (common_header->type == SECTION_GUID_DEFINED)
{
guid_header = (guid_section_header*) data;
data += guid_header->data_offset;
// Searching for specific patch patterns first
string = find_pattern(data, size2int(guid_header->size), POWERMANAGEMENT_PATCH_PATTERN_80FB01, sizeof(POWERMANAGEMENT_PATCH_PATTERN_80FB01));
if(string)
{
// Patching first 3 bytes with 0x90
memset(string, 0x90, 3);
string += 3;
}
else
// Searching for generic patch pattern
string = find_pattern(data, size2int(guid_header->size), POWERMANAGEMENT_PATCH_PATTERN, sizeof(POWERMANAGEMENT_PATCH_PATTERN));
if (!string)
return ERR_PATCH_STRING_NOT_FOUND;
// Patching
memcpy(string, POWERMANAGEMENT_PATCHED_PATTERNS[start_patch], sizeof(POWERMANAGEMENT_PATCH_PATTERN));
// Correcting checksums
return correct_checksums(module);
}
else
return ERR_UNKNOWN_MODULE;
// Skipping section allignment bytes
allignment = (data - module) % SECTION_ALLIGNMENT;
if (allignment)
data += SECTION_ALLIGNMENT - allignment;
// Reading compressed header
compressed_header = (compressed_section_header*) data;
if (compressed_header->type == SECTION_COMPRESSED)
{
data += sizeof(compressed_section_header);
data_size = size2int(compressed_header->size) - sizeof(compressed_section_header);
}
else
return ERR_UNKNOWN_MODULE;
// Decompressing module data
compressed = NULL;
switch (compressed_header->compression_type)
{
case COMPRESSION_TIANO:
if (TianoGetInfo(data, data_size, &decompressed_size, &scratch_size) != ERR_SUCCESS
|| decompressed_size != compressed_header->decompressed_size)
return ERR_TIANO_DECOMPRESSION_FAILED;
decompressed = (UINT8*)malloc(decompressed_size);
scratch = (UINT8*)malloc(scratch_size);
if (TianoDecompress(data, data_size, decompressed, decompressed_size, scratch, scratch_size) != ERR_SUCCESS)
return ERR_TIANO_DECOMPRESSION_FAILED;
free(scratch);
break;
case COMPRESSION_LZMA:
if (LzmaGetInfo(data, data_size, &decompressed_size) != ERR_SUCCESS
|| decompressed_size != compressed_header->decompressed_size)
return ERR_LZMA_DECOMPRESSION_FAILED;
decompressed = (UINT8*)malloc(decompressed_size);
if (!decompressed)
return ERR_MEMORY_ALLOCATION_FAILED;
if (LzmaDecompress(data, data_size, decompressed) != ERR_SUCCESS)
return ERR_LZMA_DECOMPRESSION_FAILED;
break;
case COMPRESSION_NONE:
decompressed = data;
decompressed_size = data_size;
break;
default:
return ERR_UNKNOWN_COMPRESSION_TYPE;
}
// Searching for specific patch patterns first
string = find_pattern(decompressed, decompressed_size, POWERMANAGEMENT_PATCH_PATTERN_80FB01, sizeof(POWERMANAGEMENT_PATCH_PATTERN_80FB01));
if(string)
{
// Patching first 3 bytes with 0x90
memset(string, 0x90, 3);
string += 3;
}
else
// Searching for generic patch pattern
string = find_pattern(decompressed, decompressed_size, POWERMANAGEMENT_PATCH_PATTERN, sizeof(POWERMANAGEMENT_PATCH_PATTERN));
if (!string)
return ERR_PATCH_STRING_NOT_FOUND;
// Trying all patched strings beginning from start_patch
is_patched = FALSE;
for(current_patch = start_patch; current_patch < PATCHED_PATTERNS_COUNT; current_patch++)
{
// Patching found string with current patch
memcpy(string, POWERMANAGEMENT_PATCHED_PATTERNS[current_patch], sizeof(POWERMANAGEMENT_PATCH_PATTERN));
// Compressing patched module
switch(compressed_header->compression_type)
{
case COMPRESSION_TIANO:
compressed_size = 0;
if (TianoCompress(decompressed, decompressed_size, compressed, &compressed_size) != ERR_BUFFER_TOO_SMALL)
return ERR_TIANO_COMPRESSION_FAILED;
compressed = (UINT8*)malloc(compressed_size);
if (!compressed)
return ERR_MEMORY_ALLOCATION_FAILED;
if (TianoCompress(decompressed, decompressed_size, compressed, &compressed_size) != ERR_SUCCESS)
return ERR_TIANO_COMPRESSION_FAILED;
break;
case COMPRESSION_LZMA:
compressed_size = 0;
if(LzmaCompress(decompressed, decompressed_size, compressed, &compressed_size) != ERR_BUFFER_TOO_SMALL)
return ERR_LZMA_COMPRESSION_FAILED;
compressed = (UINT8*)malloc(compressed_size);
if (!compressed)
return ERR_MEMORY_ALLOCATION_FAILED;
if (LzmaCompress(decompressed, decompressed_size, compressed, &compressed_size) != ERR_SUCCESS)
return ERR_TIANO_COMPRESSION_FAILED;
break;
case COMPRESSION_NONE:
compressed = decompressed;
compressed_size = decompressed_size;
break;
default:
return ERR_UNKNOWN_COMPRESSION_TYPE;
}
// Checking compressed data size
if (data_size < compressed_size)
{
grow = compressed_size - data_size;
end = data + data_size;
for (freespace_length = 0; *end++ == 0xFF; freespace_length++);
if (grow > freespace_length)
continue;
}
else if (data_size > compressed_size)
{
grow = data_size - compressed_size;
end = data + data_size;
for (freespace_length = 0; *end++ == 0xFF; freespace_length++);
if (grow + freespace_length >= 8)
if (insert_gap_after(module, data + compressed_size, grow))
continue;
}
is_patched = TRUE;
break;
}
if (!is_patched)
return ERR_PATCHED_MODULE_INSERTION_FAILED;
// Writing new module
if (data_size > compressed_size)
memset(data + compressed_size, 0xFF, data_size - compressed_size);
if (compressed_header->compression_type != COMPRESSION_NONE)
{
memcpy(data, compressed, compressed_size);
// Writing new compressed section size
int2size(compressed_size + sizeof(compressed_section_header), compressed_header->size);
// Writing new module size
module_size_change = compressed_size - data_size;
int2size(size2int(header->size) + module_size_change, header->size);
}
// Correcting checksums
return correct_checksums(module);
}
UINT8 patch_smmplatform_module(UINT8* module)
{
module_header* header;
UINT8* string;
UINT8* data;
guid_section_header *guid_header;
common_section_header *depex_header;
UINT8 allignment;
if(!module)
return ERR_INVALID_ARGUMENT;
header = (module_header*) module;
if (header->state != STATE_STD)
return ERR_NOT_MODULE;
data = module + sizeof(module_header);
guid_header = (guid_section_header*) data;
// Skipping GUID definition section in the beginning of SmmPlatform module
if (guid_header->type == SECTION_GUID_DEFINED)
data += guid_header->data_offset;
else
return ERR_UNKNOWN_MODULE;
// Skipping section allignment bytes
allignment = (data - module) % SECTION_ALLIGNMENT;
if (allignment)
data += SECTION_ALLIGNMENT - allignment;
depex_header = (common_section_header*) data;
// Skipping DXE dependency section in the beginning of SmmPlatform module
if (depex_header->type == SECTION_DXE_DEPEX)
data += size2int(depex_header->size);
else
return ERR_UNKNOWN_MODULE;
// Searching for patch pattern
string = find_pattern(data, size2int(guid_header->size) - size2int(depex_header->size), SMMPLATFORM_PATCH_PATTERN, sizeof(SMMPLATFORM_PATCH_PATTERN));
if (!string)
return ERR_PATCH_STRING_NOT_FOUND;
// Patching
memcpy(string, SMMPLATFORM_PATCHED_PATTERN, sizeof(SMMPLATFORM_PATCHED_PATTERN));
// Correcting checksums
return correct_checksums(module);
}
UINT8 patch_platformsetupadvanced_module(UINT8* module)
{
module_header* header;
UINT8* string;
UINT8* data;
guid_section_header *guid_header;
BOOLEAN is_found;
if(!module)
return ERR_INVALID_ARGUMENT;
header = (module_header*) module;
if (header->state != STATE_STD)
return ERR_NOT_MODULE;
data = module + sizeof(module_header);
guid_header = (guid_section_header*) data;
// Skipping GUID definition section in the beginning of PlatformSetupAdvancedDxe.efi module
if (guid_header->type == SECTION_GUID_DEFINED)
data += guid_header->data_offset;
else
return ERR_UNKNOWN_MODULE;
// Searching for Unicode patch string
string = find_pattern(data, size2int(guid_header->size), PLATFORMSETUPADVANCED_UNICODE_PATCH_PATTERN, sizeof(PLATFORMSETUPADVANCED_UNICODE_PATCH_PATTERN));
if(string)
{
memcpy(string, PLATFORMSETUPADVANCED_UNICODE_PATCHED_PATTERN, sizeof(PLATFORMSETUPADVANCED_UNICODE_PATCH_PATTERN));
}
else
return ERR_PATCH_STRING_NOT_FOUND; // TIP: Remove this line to make more BIOSes patchable
// Searching for all patch strings
is_found = FALSE;
for (string = find_pattern(data, size2int(guid_header->size), PLATFORMSETUPADVANCED_PATCH_PATTERN, sizeof(PLATFORMSETUPADVANCED_PATCH_PATTERN));
string;
string = find_pattern(data, size2int(guid_header->size), PLATFORMSETUPADVANCED_PATCH_PATTERN, sizeof(PLATFORMSETUPADVANCED_PATCH_PATTERN)))
{
is_found = TRUE;
// Patching
memcpy(string, PLATFORMSETUPADVANCED_PATCHED_PATTERN, sizeof(PLATFORMSETUPADVANCED_PATCH_PATTERN));
}
if(!is_found)
return ERR_PATCH_STRING_NOT_FOUND;
// Correcting checksums
return correct_checksums(module);
}
UINT8 patch_cpupei_module(UINT8* module)
{
module_header* header;
UINT8* string;
if(!module)
return ERR_INVALID_ARGUMENT;
header = (module_header*) module;
if (header->state != STATE_STD)
return ERR_NOT_MODULE;
header = (module_header*) module;
// Searching for patch string
string = find_pattern(module, size2int(header->size), CPUPEI_PATCH_PATTERN, sizeof(CPUPEI_PATCH_PATTERN));
if(!string)
return ERR_PATCH_STRING_NOT_FOUND;
// Patching
memcpy(string, CPUPEI_PATCHED_PATTERN, sizeof(CPUPEI_PATCH_PATTERN));
// Correcting checksums
return correct_checksums(module);
}
UINT8 patch_nested_module(UINT8* module)
{
module_header *header;
compressed_section_header *compressed_header;
UINT8* data;
UINT32 data_size;
UINT8* compressed;
UINT32 compressed_size;
UINT8* decompressed;
UINT32 decompressed_size;
UINT8* scratch;
UINT32 scratch_size;
UINT8* string;
INT32 module_size_change;
UINT8 current_patch;
UINT8 result;
BOOLEAN is_patched;
BOOLEAN is_module_patched;
if(!module)
return ERR_INVALID_ARGUMENT;
header = (module_header*) module;
if (header->state != STATE_STD)
return ERR_NOT_MODULE;
data = module + sizeof(module_header);
compressed_header = (compressed_section_header*) data;
if(compressed_header->type != SECTION_COMPRESSED)
return ERR_UNKNOWN_MODULE;
data += sizeof(compressed_section_header);
data_size = size2int(compressed_header->size) - sizeof(compressed_section_header);
// Decompressing module data
switch (compressed_header->compression_type)
{
case COMPRESSION_TIANO:
if (TianoGetInfo(data, data_size, &decompressed_size, &scratch_size) != ERR_SUCCESS
|| decompressed_size != compressed_header->decompressed_size)
return ERR_TIANO_DECOMPRESSION_FAILED;
decompressed = (UINT8*)malloc(decompressed_size);
scratch = (UINT8*)malloc(scratch_size);
if (!decompressed || !scratch)
return ERR_MEMORY_ALLOCATION_FAILED;
if (TianoDecompress(data, data_size, decompressed, decompressed_size, scratch, scratch_size) != ERR_SUCCESS)
return ERR_TIANO_DECOMPRESSION_FAILED;
free(scratch);
break;
case COMPRESSION_LZMA:
if (LzmaGetInfo(data, data_size, &decompressed_size) != ERR_SUCCESS
|| decompressed_size != compressed_header->decompressed_size)
return ERR_LZMA_DECOMPRESSION_FAILED;
decompressed = (UINT8*)malloc(decompressed_size);
if (!decompressed)
return ERR_MEMORY_ALLOCATION_FAILED;
if (LzmaDecompress(data, data_size, decompressed) != ERR_SUCCESS)
return ERR_LZMA_DECOMPRESSION_FAILED;
break;
case COMPRESSION_NONE:
decompressed = data;
decompressed_size = data_size;
break;
default:
return ERR_UNKNOWN_COMPRESSION_TYPE;
}
// Searching for PlatformSetupAdvancedDxe.efi module
string = find_pattern(decompressed, decompressed_size, PLATFORMSETUPADVANCED_UUID, UUID_LENGTH);
if (string)
{
result = patch_platformsetupadvanced_module(string);
if (!result)
printf("Nested PlatformSetupAdvancedDxe.efi at %08X patched.\n", (UINT32)(string - module));
}
// Trying to patch PowerManagement modules with all patch patterns
is_patched = FALSE;
scratch = (UINT8*)malloc(decompressed_size);
if (!scratch)
return ERR_MEMORY_ALLOCATION_FAILED;
for (current_patch = 0; current_patch < PATCHED_PATTERNS_COUNT; current_patch++)
{
printf("Trying to apply patch #%d\n", current_patch + 1);
// Making a copy of decompressed module
memcpy(scratch, decompressed, decompressed_size);
is_module_patched = FALSE;
// Searching for all PowerManagement modules
for (string = find_pattern(scratch, decompressed_size, POWERMANAGEMENT_UUID, UUID_LENGTH);
string;
string = find_pattern(string + UUID_LENGTH, decompressed_size - (string - scratch) - UUID_LENGTH, POWERMANAGEMENT_UUID, UUID_LENGTH))
{
// Patching PowerManagement module
result = patch_powermanagement_module(string, current_patch);
if (!result)
{
printf("Nested PowerManagement module at %08X patched.\n", (UINT32)(string - scratch));
is_module_patched = TRUE;
continue;
}
printf("Nested PowerManagement module at %08X not patched: ", (UINT32)(string - scratch));
switch (result)
{
case ERR_INVALID_ARGUMENT:
printf("Invalid parameter.\n");
break;
case ERR_NOT_MODULE:
printf("Unknown module state.\n");
break;
case ERR_UNKNOWN_MODULE:
printf("Unknown module structure.\n");
break;
case ERR_UNKNOWN_COMPRESSION_TYPE:
printf("Unknown compression type.\n");
break;
case ERR_TIANO_DECOMPRESSION_FAILED:
printf("Tiano decompression failed.\n");
break;
case ERR_LZMA_DECOMPRESSION_FAILED:
printf("LZMA decompression failed.\n");
break;
case ERR_PATCH_STRING_NOT_FOUND:
printf("Patch pattern not found.\n");
break;
case ERR_TIANO_COMPRESSION_FAILED:
printf("Tiano compression failed.\n");
break;
case ERR_LZMA_COMPRESSION_FAILED:
printf("LZMA compression failed.\n");
break;
case ERR_MEMORY_ALLOCATION_FAILED:
printf("Memory allocation failed.\n");
break;
default:
printf("Unknown error.\n");
break;
}
}
// Searching for all PowerMgmtDxe modules
for (string = find_pattern(scratch, decompressed_size, POWERMGMTDXE_UUID, UUID_LENGTH);
string;
string = find_pattern(string + UUID_LENGTH, decompressed_size - (string - scratch) - UUID_LENGTH, POWERMGMTDXE_UUID, UUID_LENGTH))
{
// Patching PowerMgmtDxe module
result = patch_powermanagement_module(string, current_patch);
if (!result)
{
printf("Nested PowerMgmtDxe/PowerManagement2.efi module at %08X patched.\n", (UINT32)(string - scratch));
is_module_patched = TRUE;
continue;
}
printf("Nested PowerMgmtDxe/PowerManagement2.efi module at %08X not patched: ", (UINT32)(string - scratch));
switch (result)
{
case ERR_INVALID_ARGUMENT:
printf("Invalid parameter.\n");
break;
case ERR_NOT_MODULE:
printf("Unknown module state.\n");
break;
case ERR_UNKNOWN_MODULE:
printf("Unknown module structure.\n");
break;
case ERR_UNKNOWN_COMPRESSION_TYPE:
printf("Unknown compression type.\n");
break;
case ERR_TIANO_DECOMPRESSION_FAILED:
printf("Tiano decompression failed.\n");
break;
case ERR_LZMA_DECOMPRESSION_FAILED:
printf("LZMA decompression failed.\n");
break;
case ERR_PATCH_STRING_NOT_FOUND:
printf("Patch pattern not found.\n");
break;
case ERR_TIANO_COMPRESSION_FAILED:
printf("Tiano compression failed.\n");
break;
case ERR_LZMA_COMPRESSION_FAILED:
printf("LZMA compression failed.\n");
break;
case ERR_MEMORY_ALLOCATION_FAILED:
printf("Memory allocation failed.\n");
break;
default:
printf("Unknown error.\n");
break;
}
}
// Searching for all SmmPlatform modules
for (string = find_pattern(scratch, decompressed_size, SMMPLATFORM_UUID, UUID_LENGTH);
string;
string = find_pattern(string + UUID_LENGTH, decompressed_size - (string - scratch) - UUID_LENGTH, SMMPLATFORM_UUID, UUID_LENGTH))
{
// Patching SmmPlatform module
result = patch_smmplatform_module(string);
if (!result)
{
printf("Nested SmmPlatform module at %08X patched.\n", (UINT32)(string - scratch));
is_module_patched = TRUE;
continue;
}
printf("Nested SmmPlatform module at %08X not patched: ", (UINT32)(string - scratch));
switch (result)
{
case ERR_INVALID_ARGUMENT:
printf("Invalid parameter.\n");
break;
case ERR_NOT_MODULE:
printf("Unknown module state.\n");
break;
case ERR_UNKNOWN_MODULE:
printf("Unknown module structure.\n");
break;
case ERR_UNKNOWN_COMPRESSION_TYPE:
printf("Unknown compression type.\n");
break;
case ERR_TIANO_DECOMPRESSION_FAILED:
printf("Tiano decompression failed.\n");
break;
case ERR_LZMA_DECOMPRESSION_FAILED:
printf("LZMA decompression failed.\n");
break;
case ERR_PATCH_STRING_NOT_FOUND:
printf("Patch pattern not found.\n");
break;
case ERR_TIANO_COMPRESSION_FAILED:
printf("Tiano compression failed.\n");
break;
case ERR_LZMA_COMPRESSION_FAILED:
printf("LZMA compression failed.\n");
break;
case ERR_MEMORY_ALLOCATION_FAILED:
printf("Memory allocation failed.\n");
break;
default:
printf("Unknown error.\n");
break;
}
}
if (!is_module_patched)
return ERR_MODULE_NOT_FOUND;
// Compressing patched module
switch(compressed_header->compression_type)
{
case COMPRESSION_TIANO:
compressed = 0;
compressed_size = 0;
if (TianoCompress(scratch, decompressed_size, compressed, &compressed_size) != ERR_BUFFER_TOO_SMALL)
return ERR_TIANO_COMPRESSION_FAILED;
compressed = (UINT8*)malloc(compressed_size);
if(!compressed)
return ERR_MEMORY_ALLOCATION_FAILED;
if (TianoCompress(scratch, decompressed_size, compressed, &compressed_size) != ERR_SUCCESS)
return ERR_TIANO_COMPRESSION_FAILED;
break;
case COMPRESSION_LZMA:
compressed = 0;
compressed_size = 0;
if(LzmaCompress(scratch, decompressed_size, compressed, &compressed_size) != ERR_BUFFER_TOO_SMALL)
return ERR_LZMA_COMPRESSION_FAILED;
compressed = (UINT8*)malloc(compressed_size);
if(!compressed)
return ERR_MEMORY_ALLOCATION_FAILED;
if (LzmaCompress(scratch, decompressed_size, compressed, &compressed_size) != ERR_SUCCESS)
return ERR_TIANO_COMPRESSION_FAILED;
break;
case COMPRESSION_NONE:
compressed = scratch;
compressed_size = decompressed_size;
break;
default:
return ERR_UNKNOWN_COMPRESSION_TYPE;
}
module_size_change = compressed_size - data_size;
// Checking that new compressed module can be inserted
if (module_size_change > 0) // Compressed module is bigger then original
{
INT32 pos;
for(pos = 0; data[data_size+pos] == 0xFF; pos++);
if(pos < module_size_change)
{
printf ("Patched module too big after compression.\n");
continue;
}
}
else if (module_size_change < 0) // Compressed module is smaller then original
{
// Checking if there is another module after this one
INT32 pos;
for(pos = 0; data[data_size+pos] == 0xFF; pos++);
if (pos < 8 && -module_size_change + pos > 7)
{
if (insert_gap_after(module, data + compressed_size, data_size - compressed_size))
{
printf ("Patched module is smaller then original after compression, but gap module can't be inserted.\n");
continue;
}
}
else
memset(data + compressed_size, 0xFF, data_size - compressed_size);
}
is_patched = TRUE;
break;
}
free(scratch);
if(!is_patched)
return ERR_PATCHED_MODULE_INSERTION_FAILED;
// Writing new module
if (compressed_header->compression_type != COMPRESSION_NONE)
{
memcpy(data, compressed, compressed_size);
// Writing new compressed section size
int2size(compressed_size + sizeof(compressed_section_header), compressed_header->size);
// Writing new module size
int2size(size2int(header->size) + module_size_change, header->size);
}
// Correcting checksums
return correct_checksums(module);
}
BOOLEAN patch_bios(UINT8* bios, UINT32 size)
{
UINT8* module;
UINT8* raw_file;
UINT8* bios_end;
UINT8 patch_result;
BOOLEAN is_found;
BOOLEAN is_patched;
if (!bios || !size)
return ERR_INVALID_ARGUMENT;
bios_end = bios + size;
is_patched = FALSE;
// Searching for all PowerManagement modules
is_found = FALSE;
for (module = find_pattern(bios, size, POWERMANAGEMENT_UUID, UUID_LENGTH);
module;
module = find_pattern(module+UUID_LENGTH, bios_end-module-UUID_LENGTH, POWERMANAGEMENT_UUID, UUID_LENGTH))
{
is_found = TRUE;
patch_result = patch_powermanagement_module(module, 0);
if (!patch_result)
{
printf("PowerManagement module at %08X patched.\n", (UINT32)(module - bios));
is_patched = TRUE;
continue;
}
printf("PowerManagement module at %08X not patched: ", (UINT32)(module - bios));
switch (patch_result)
{
case ERR_INVALID_ARGUMENT:
printf("Invalid parameter.\n");
break;
case ERR_UNKNOWN_MODULE:
printf("Unknown module structure.\n");
break;
case ERR_UNKNOWN_COMPRESSION_TYPE:
printf("Unknown compression type.\n");
break;
case ERR_TIANO_DECOMPRESSION_FAILED:
printf("Tiano decompression failed.\n");
break;
case ERR_LZMA_DECOMPRESSION_FAILED:
printf("LZMA decompression failed.\n");
break;
case ERR_PATCH_STRING_NOT_FOUND:
printf("Patch pattern not found.\n");
break;
case ERR_TIANO_COMPRESSION_FAILED:
printf("Tiano compression failed.\n");
break;
case ERR_LZMA_COMPRESSION_FAILED:
printf("LZMA compression failed.\n");
break;
case ERR_PATCHED_MODULE_INSERTION_FAILED:
printf("Repacked module can't be inserted.\n");
break;
case ERR_MEMORY_ALLOCATION_FAILED:
printf("Memory allocation failed.\n");
break;
default:
printf("Unknown error.\n");
break;
}
}
if (!is_found)
printf("PowerManagement modules not found.\n");
// Searching for all PowerManagement modules
is_found = FALSE;
for (module = find_pattern(bios, size, POWERMGMTDXE_UUID, UUID_LENGTH);
module;
module = find_pattern(module+UUID_LENGTH, bios_end-module-UUID_LENGTH, POWERMGMTDXE_UUID, UUID_LENGTH))
{
is_found = TRUE;
patch_result = patch_powermanagement_module(module, 0);
if (!patch_result)
{
printf("PowerMgmtDxe/PowerManagement2.efi module at %08X patched.\n", (UINT32)(module - bios));
is_patched = TRUE;
continue;
}
printf("PowerMgmtDxe/PowerManagement2.efi module at %08X not patched: ", (UINT32)(module - bios));
switch (patch_result)
{
case ERR_INVALID_ARGUMENT:
printf("Invalid parameter.\n");
break;
case ERR_UNKNOWN_MODULE:
printf("Unknown module structure.\n");
break;
case ERR_UNKNOWN_COMPRESSION_TYPE:
printf("Unknown compression type.\n");
break;
case ERR_TIANO_DECOMPRESSION_FAILED:
printf("Tiano decompression failed.\n");
break;
case ERR_LZMA_DECOMPRESSION_FAILED:
printf("LZMA decompression failed.\n");
break;
case ERR_PATCH_STRING_NOT_FOUND:
printf("Patch pattern not found.\n");
break;
case ERR_TIANO_COMPRESSION_FAILED:
printf("Tiano compression failed.\n");
break;
case ERR_LZMA_COMPRESSION_FAILED:
printf("LZMA compression failed.\n");
break;
case ERR_PATCHED_MODULE_INSERTION_FAILED:
printf("Repacked module can't be inserted.\n");
break;
case ERR_MEMORY_ALLOCATION_FAILED:
printf("Memory allocation failed.\n");
break;
default:
printf("Unknown error.\n");
break;
}
}
if (!is_found)
printf("PowerMgmtDxe/PowerManagement2.efi modules not found.\n");
// Searching for all AMI nest modules
is_found = FALSE;
for (module = find_pattern(bios, size, AMI_NEST_UUID, UUID_LENGTH);
module;
module = find_pattern(module+UUID_LENGTH, bios_end-module-UUID_LENGTH, AMI_NEST_UUID, UUID_LENGTH))
{
is_found = TRUE;
patch_result = patch_nested_module(module);
if (!patch_result)
{
printf("AMI nest module at %08X patched.\n", (UINT32)(module - bios));
is_patched = TRUE;
continue;
}
printf("AMI nest module at %08X not patched: ", (UINT32)(module - bios));
switch (patch_result)
{
case ERR_INVALID_ARGUMENT:
printf("Invalid argument.\n");
break;
case ERR_UNKNOWN_MODULE:
printf("Unknown module structure.\n");
break;
case ERR_UNKNOWN_COMPRESSION_TYPE:
printf("Unknown compression type.\n");
break;
case ERR_TIANO_DECOMPRESSION_FAILED:
printf("Tiano decompression failed.\n");
break;
case ERR_LZMA_DECOMPRESSION_FAILED:
printf("LZMA decompression failed.\n");
break;
case ERR_PATCH_STRING_NOT_FOUND:
printf("Patch pattern not found.\n");
break;
case ERR_TIANO_COMPRESSION_FAILED:
printf("Tiano compression failed.\n");
break;