-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.c
1773 lines (1574 loc) · 45.5 KB
/
main.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
/*
* main.c
* gfxutil
*
* Created by mcmatrix on 07.01.08.
* Copyright 2008 mcmatrix. All rights reserved.
*
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <CoreFoundation/CoreFoundation.h> // (CFDictionary, ...)
#include <IOKit/IOCFSerialize.h> // (IOCFSerialize, ...)
#include <IOKit/IOKitLib.h> // (IOMasterPort, ...)
#include "edk2misc.h"
#include "efidevp.h"
#include "utils.h"
#include "main.h"
/*
* Get the number of bytes necessary to represent a Unicode string.
*/
static int unilen(const char *str, int len)
{
unsigned long ch;
int posn = 0;
int nbytes = 2;
while(posn < len)
{
ch = UTF8ReadChar(str, len, &posn);
if(ch < (unsigned long)0x10000)
{
nbytes += 2;
}
else if(ch < (((unsigned long)1) << 20))
{
nbytes += 4;
}
}
return nbytes;
}
/*
* Write a length-specified unicode string to a binary output stream.
*/
unsigned char *str2uni(const char *str, int len)
{
int posn = 0;
char buf[4];
unsigned char *bout, *binaryout;
int templen;
int blen,i;
blen = unilen(str, len);
bout = (unsigned char *)calloc(blen, sizeof(unsigned char));
binaryout = bout;
if (!bout)
{
fprintf(stderr, "str2unicode: out of memory\n");
return NULL;
}
/* Write out the contents of the string */
while(posn < len)
{
templen = UTF16WriteCharAsBytes(buf, UTF8ReadChar(str, len, &posn));
for(i = 0; i< templen; i++)
{
*bout++ = buf[i];
}
}
return binaryout;
}
static int readbin(unsigned char **data, unsigned int *size, unsigned char **dat, unsigned int len)
{
unsigned char *d = *data;
unsigned int s = *size;
if( s != 0 )
{
*dat = (unsigned char *)calloc(len, sizeof(unsigned char));
if (!dat)
{
fprintf(stderr, "read_binary: out of memory\n");
return 0;
}
if(((unsigned int)(len)) <= s)
{
memcpy((*dat),d,len);
*data = d + len;
*size = s - len;
return 1;
}
}
fprintf(stderr, "read_binary: invalid binary data\n");
return 0;
}
/*
* Returns zero if the data is badly formatted.
*/
static int uni2str(unsigned char *d, unsigned int length, char **str, unsigned int *len)
{
unsigned unich;
if(length != 0)
{
/* Allocate space for the converted string */
// Two unicode characters make up 1 buffer byte. Round up
if((*str = (char *)calloc(length*2 + 1, sizeof(char))) == 0)
{
fprintf(stderr, "unicode2str: out of memory\n");
return 0;
}
/* Convert the string from Unicode into UTF-8 */
*len = 0;
while(length >= 2)
{
unich = READ_UINT16(d);
d += 2;
if(unich < 0x80)
{
(*str)[*len] = (char)unich;
++(*len);
}
else if(unich < (1 << 11))
{
(*str)[*len] = (char)(0xC0 | (unich >> 6));
++(*len);
(*str)[*len] = (char)(0x80 | (unich & 0x3F));
++(*len);
}
else
{
(*str)[*len] = (char)(0xE0 | (unich >> 12));
++(*len);
(*str)[*len] = (char)(0x80 | ((unich >> 6) & 0x3F));
++(*len);
(*str)[*len] = (char)(0x80 | (unich & 0x3F));
++(*len);
}
length -= 2;
}
(*str)[*len] = '\0';
return 1;
}
fprintf(stderr, "unicode2str: invalid binary unicode data\n");
return 0;
}
unsigned char _nibbleValue(CHAR16 hexchar)
{
unsigned char val;
if(hexchar >= '0' && hexchar <= '9')
val = hexchar - '0';
else if(hexchar >= 'A' && hexchar <= 'F')
val = hexchar - 'A' + 10;
else if(hexchar >= 'a' && hexchar <= 'f')
val = hexchar - 'a' + 10;
else
val = 0xff;
return(val);
}
int isHexString16(CHAR16 * buffer, unsigned int Size)
{
int HexCnt;
// Find out how many hex characters the string has.
for (HexCnt = 0; IS_HEX( buffer[HexCnt] ); HexCnt++);
if( (HexCnt == Size) && (HexCnt != 0) ) return 1;
return 0;
}
const unsigned char _HexTabLC[16]= "0123456789abcdef";
/* binary to HEX conversion function -- for Unicode support */
char *bin2hex(const unsigned char *data, unsigned long size)
{
long i;
unsigned char *pin = (unsigned char *)data;
char *pout, *p;
unsigned char c;
// one binary byte make up 2 hex characters
pout = p = (char *)calloc((size * 2) + 1, sizeof(char));
if (!p)
{
fprintf(stderr, "bin2hex: out of memory\n");
return 0;
}
for(i=0; i < size; i++)
{
c = *pin++;
*p++ = _HexTabLC[c >> 4];
*p++ = _HexTabLC[c & 0xf];
}
*p = '\0';
return(pout);
}
/* After the call, length will contain the byte length of binary data. */
unsigned char *hex2bin(const char *data, unsigned long *size)
{
long bcount = 0;
char *pin = (char *)data;
unsigned char *pout, *p;
unsigned char ch;
int HighNibble = 1, HexCnt = 0;
// Find out how many hex characters the string has.
for (HexCnt = 0; IS_HEX(data[HexCnt]); HexCnt++);
if (HexCnt == 0)
{
*size = 0;
return NULL;
}
// two hex characters make up 1 binary byte. Round up.
pout = p = (unsigned char *)calloc((HexCnt + 1) / 2, sizeof(unsigned char));
if (!p)
{
fprintf(stderr, "hex2bin: out of memory\n");
return NULL;
}
// read until string end
for( ;*pin != '\0'; pin++)
{
ch = _nibbleValue(*pin);
if(ch > 15)
continue; // not a HEX character
if(HighNibble)
{
*p = ch << 4;
HighNibble = 0;
}
else
{
*p++ |= ch;
HighNibble = 1;
bcount++;
}
}
*size = bcount; /* byte count */
return(pout);
}
/* After the call, length will contain the byte length of binary data. */
unsigned char *hex2bin16(CHAR16 *data, unsigned long *size)
{
long bcount = 0;
CHAR16 *pin = data;
unsigned char *pout, *p;
unsigned char ch;
int HighNibble = 1, HexCnt = 0;
// Find out how many hex characters the string has.
for (HexCnt = 0; IS_HEX(data[HexCnt]); HexCnt++);
if (HexCnt == 0)
{
*size = 0;
return NULL;
}
// two hex characters make up 1 binary byte. Round up.
pout = p = (unsigned char *)calloc((HexCnt + 1) / 2, sizeof(unsigned char));
if (!p)
{
fprintf(stderr, "hex2bin16: out of memory\n");
return NULL;
}
// read until string end
for( ;*pin != '\0'; pin++)
{
ch = _nibbleValue(*pin);
if(ch > 15)
continue; // not a HEX character
if(HighNibble)
{
*p = ch << 4;
HighNibble = 0;
}
else
{
*p++ |= ch;
HighNibble = 1;
bcount++;
}
}
*size = bcount; /* byte count */
return(pout);
}
int is_string(void * buffer, int size)
{
int i;
for(i=0;i < size; i++)
{
if(!IS_ALPHANUMMARK( ((unsigned char *)buffer)[i]) ) return 0;
}
return size > 0; // && ((unsigned char *)buffer)[size-1] == '\0';
}
void dump_buffer(void * buffer, int size)
{
int i;
printf("Buffer size: %d, data = <",size);
for(i=0;i < size; ++i)
{
printf("%c",((char *)buffer)[i]);
}
printf(">\n");
}
// this writes gfx data to binary file
unsigned char *gfx2bin(GFX_HEADER *gfx)
{
GFX_BLOCKHEADER *gfx_blockheader_tmp;
GFX_ENTRY *gfx_entry_tmp;
unsigned char *buffer, *buffer_head;
if(gfx->filesize > 0)
{
buffer_head = buffer = (unsigned char *)calloc(gfx->filesize, sizeof(unsigned char));
if (!buffer)
{
fprintf(stderr, "gfx2bin: out of memory\n");
return NULL;
}
WRITE_UINT32(buffer, gfx->filesize);
buffer +=4;
WRITE_UINT32(buffer, gfx->var1);
buffer +=4;
WRITE_UINT32(buffer, gfx->countofblocks);
buffer +=4;
gfx_blockheader_tmp = gfx->blocks;
while(gfx_blockheader_tmp)
{
WRITE_UINT32(buffer, gfx_blockheader_tmp->blocksize);
buffer +=4;
WRITE_UINT32(buffer, gfx_blockheader_tmp->records);
buffer +=4;
//write header
memcpy(buffer,gfx_blockheader_tmp->devpath,gfx_blockheader_tmp->devpath_len);
buffer += gfx_blockheader_tmp->devpath_len;
gfx_entry_tmp = gfx_blockheader_tmp->entries;
while(gfx_entry_tmp)
{
WRITE_UINT32(buffer, gfx_entry_tmp->bkey_len + 4); // 4bytes - include length record too
buffer +=4;
memcpy(buffer, gfx_entry_tmp->bkey, gfx_entry_tmp->bkey_len);
buffer += gfx_entry_tmp->bkey_len;
WRITE_UINT32(buffer, gfx_entry_tmp->val_len +4); // 4bytes - include length record too
buffer +=4;
memcpy(buffer, gfx_entry_tmp->val, gfx_entry_tmp->val_len);
buffer += gfx_entry_tmp->val_len;
gfx_entry_tmp = gfx_entry_tmp->next;
}
gfx_blockheader_tmp = gfx_blockheader_tmp->next;
}
return buffer_head;
}
fprintf(stderr, "gfx2bin: invalid binary data\n");
return NULL;
}
static void free_gfx_blockheader_list(GFX_BLOCKHEADER *head, GFX_BLOCKHEADER *end)
{
GFX_BLOCKHEADER *tmp;
do {
if (head) {
tmp = head;
head = head->next;
free(tmp);
}
} while (head != end);
}
static void free_gfx_entry_list(GFX_ENTRY *head, GFX_ENTRY *end)
{
GFX_ENTRY *tmp;
do {
if (head) {
tmp = head;
head = head->next;
free(tmp);
}
} while (head != end);
}
// this reads gfx binary info and parses it
GFX_HEADER *parse_binary(unsigned char * bp, unsigned char * bpend, SETTINGS *settings)
{
GFX_HEADER *gfx_header = NULL;
// head points to the first node in list, end points to the last node in list
GFX_BLOCKHEADER *gfx_blockheader = NULL;
GFX_BLOCKHEADER *gfx_blockheader_head = NULL;
GFX_BLOCKHEADER *gfx_blockheader_end = NULL;
GFX_ENTRY *gfx_entry = NULL;
GFX_ENTRY *gfx_entry_head = NULL;
GFX_ENTRY *gfx_entry_end = NULL;
unsigned char *data = NULL, *bin = NULL, *tmp = NULL, *dpathtmp = NULL;
char * str = NULL;
unsigned int str_len, data_len, size, length;
int i,j;
//read header data
gfx_header = (GFX_HEADER *)calloc(1, sizeof(GFX_HEADER));
if(!gfx_header)
{
fprintf(stderr, "parse_binary: out of memory\n");
return NULL;
}
gfx_header->filesize = READ_UINT32(bp);
bp+=4;
gfx_header->var1 = READ_UINT32(bp);
bp+=4;
gfx_header->countofblocks = READ_UINT32(bp);
bp+=4;
//read blocks
gfx_blockheader_head = NULL;
gfx_blockheader_end = NULL;
for(i=0;i<gfx_header->countofblocks;i++)
{
//create new block
gfx_blockheader = (GFX_BLOCKHEADER *)calloc(1, sizeof(GFX_BLOCKHEADER));
if(!gfx_blockheader)
{
fprintf(stderr, "parse_binary: out of memory\n");
goto error;
}
//read block data
gfx_blockheader->blocksize = READ_UINT32(bp);
bp+=4;
gfx_blockheader->records = READ_UINT32(bp);
bp+=4;
size = gfx_blockheader->blocksize;
Boolean foundend = false;
// read device path data until devpath end node 0x0004FF7F
for (tmp = bp; tmp+4 <= bpend && !foundend; tmp+=READ_UINT16(tmp+2))
{
if( READ_UINT32(tmp) == 0x0004ff7f || READ_UINT32(tmp) == 0x0004ffff )
foundend = true;
}
if (!foundend)
{
// BugBug: Code to catch bogus device path
fprintf(stderr, "parse_binary: Cannot find device path end! Probably a bogus device path.\n");
goto error;
}
// read device path data
gfx_blockheader->devpath_len = (unsigned int)abs((int)(tmp-bp));
assert(readbin(&bp, &size, &dpathtmp,gfx_blockheader->devpath_len));
gfx_blockheader->devpath = (EFI_DEVICE_PATH_PROTOCOL *)dpathtmp;
gfx_entry_head = NULL;
gfx_entry_end = NULL;
for(j=1;j <= gfx_blockheader->records;j++)
{
length = READ_UINT32(bp);
length -= 4; bp += 4; size -=4;
if(readbin(&bp, &size, &bin, length))
{
if(!uni2str(bin, length, &str, &str_len))
{
goto error;
}
}
else
{
goto error;
}
data_len = READ_UINT32(bp);
data_len -= 4; bp += 4; size -=4;
if(!readbin(&bp, &size, &data, data_len))
{
goto error;
}
gfx_entry = (GFX_ENTRY *)calloc(1, sizeof(GFX_ENTRY));
if(!gfx_entry)
{
fprintf(stderr, "parse_binary: out of memory\n");
goto error;
}
//read entries
gfx_entry->bkey = bin;
gfx_entry->bkey_len = length;
gfx_entry->key = str;
gfx_entry->key_len = str_len;
gfx_entry->val_type = DATA_BINARY; // set default data type
gfx_entry->val = data;
gfx_entry->val_len = data_len;
if(settings->detect_numbers) // detect numbers
{
switch(data_len)
{
case (unsigned int)sizeof(UINT8): // int8
gfx_entry->val_type = DATA_INT8;
break;
case (unsigned int)sizeof(UINT16): //int16
gfx_entry->val_type = DATA_INT16;
break;
case (unsigned int)sizeof(UINT32): //int32
gfx_entry->val_type = DATA_INT32;
break;
default:
gfx_entry->val_type = DATA_BINARY;
break;
}
}
// detect strings
if(settings->detect_strings && is_string(data, data_len) && gfx_entry->val_type == DATA_BINARY)
{
gfx_entry->val_type = DATA_STRING;
}
if(!gfx_entry_head) // if there are no nodes in list then
gfx_entry_head = gfx_entry; // set head to this new node
if(gfx_entry_end)
gfx_entry_end->next = gfx_entry; // link in new node to the end of the list
gfx_entry->next = NULL; // set next field to signify the end of list
gfx_entry_end = gfx_entry; // adjust end to point to the last node
}
gfx_blockheader->entries = gfx_entry_head;
if(!gfx_blockheader_head) // if there are no nodes in list then
gfx_blockheader_head = gfx_blockheader; // set head to this new node
if(gfx_blockheader_end)
gfx_blockheader_end->next = gfx_blockheader;// link in new node to the end of the list
gfx_blockheader->next = NULL; // set next field to signify the end of list
gfx_blockheader_end = gfx_blockheader; // adjust end to point to the last node
}
gfx_header->blocks = gfx_blockheader_head;
return (gfx_header);
error:
free(str);
free(data);
free(bin);
free_gfx_blockheader_list(gfx_blockheader_head, gfx_blockheader_end);
free_gfx_entry_list(gfx_entry_head, gfx_entry_end);
free(gfx_blockheader);
free(gfx_header);
return NULL;
}
CFDictionaryRef CreateGFXDictionary(GFX_HEADER * gfx)
{
CFMutableDictionaryRef dict, items;
CFDataRef data = NULL;
//CFNumberRef number = NULL;
CFStringRef string = NULL;
CFStringRef key = NULL;
GFX_BLOCKHEADER *gfx_blockheader_tmp;
GFX_ENTRY *gfx_entry_tmp;
uint64_t bigint;
char hexstr[32];
CHAR16 *dpath;
// Create dictionary that will hold gfx data
dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0 ,&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
gfx_blockheader_tmp = gfx->blocks;
while(gfx_blockheader_tmp)
{
items = CFDictionaryCreateMutable(kCFAllocatorDefault, 0 ,&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
gfx_entry_tmp = gfx_blockheader_tmp->entries;
while(gfx_entry_tmp)
{
key = CFStringCreateWithCString(kCFAllocatorDefault, gfx_entry_tmp->key, kCFStringEncodingUTF8);
switch(gfx_entry_tmp->val_type)
{
case DATA_STRING:
string = CFStringCreateWithBytes(kCFAllocatorDefault,gfx_entry_tmp->val, gfx_entry_tmp->val_len, kCFStringEncodingASCII, false);
CFDictionarySetValue(items, key, string);
CFRelease(string);
CFRelease(key);
break;
case DATA_INT8:
bigint = READ_UINT8(gfx_entry_tmp->val);
sprintf(hexstr,"0x%02llx",bigint);
string = CFStringCreateWithCString(kCFAllocatorDefault,hexstr, kCFStringEncodingASCII);
CFDictionarySetValue(items, key, string);
CFRelease(string);
CFRelease(key);
break;
case DATA_INT16:
bigint = READ_UINT16(gfx_entry_tmp->val);
sprintf(hexstr,"0x%04llx",bigint);
string = CFStringCreateWithCString(kCFAllocatorDefault,hexstr, kCFStringEncodingASCII);
CFDictionarySetValue(items, key, string);
CFRelease(string);
CFRelease(key);
break;
case DATA_INT32:
bigint = READ_UINT32(gfx_entry_tmp->val);
sprintf(hexstr,"0x%08llx",bigint);
string = CFStringCreateWithCString(kCFAllocatorDefault,hexstr, kCFStringEncodingASCII);
CFDictionarySetValue(items, key, string);
CFRelease(string);
CFRelease(key);
break;
default:
case DATA_BINARY:
data = CFDataCreate(kCFAllocatorDefault,gfx_entry_tmp->val, gfx_entry_tmp->val_len);
CFDictionarySetValue(items, key, data);
CFRelease(data);
CFRelease(key);
break;
}
gfx_entry_tmp = gfx_entry_tmp->next;
}
VerifyDevicePathNodeSizes(gfx_blockheader_tmp->devpath);
dpath = ConvertDevicePathToText (gfx_blockheader_tmp->devpath, 1, 1);
if(dpath != NULL)
{
key = CFStringCreateWithCharacters(kCFAllocatorDefault, dpath, StrLen(dpath));
}
else
{
printf("CreateGFXDictionary: error converting device path to text shorthand notation\n");
return NULL;
}
CFDictionarySetValue(dict, key, items);
free(dpath);
CFRelease(key);
CFRelease(items);
gfx_blockheader_tmp = gfx_blockheader_tmp->next;
}
return dict;
}
GFX_HEADER *CreateGFXFromPlist(CFPropertyListRef plist)
{
int num_blocks, num_rec;
int block_size, gfx_size;
int i, IsHex;
unsigned int HexBytes = 0;
unsigned long len;
char *HexStr = NULL;
unsigned char *bytes = NULL;
CHAR16* bytes16 = NULL;
unsigned char *data = NULL;
CFStringRef *block_keys = NULL;
CFTypeRef *block_vals = NULL;
CFStringRef *dict_keys = NULL;
CFDictionaryRef *dict_vals = NULL;
CFMutableDictionaryRef this_block;
GFX_HEADER *gfx_header;
// head points to the first node in list, end points to the last node in list
GFX_BLOCKHEADER *gfx_blockheader = (GFX_BLOCKHEADER *) NULL;
GFX_BLOCKHEADER *gfx_blockheader_head = (GFX_BLOCKHEADER *) NULL;
GFX_BLOCKHEADER *gfx_blockheader_end = (GFX_BLOCKHEADER *) NULL;
GFX_ENTRY *gfx_entry = (GFX_ENTRY *) NULL;
GFX_ENTRY *gfx_entry_head = (GFX_ENTRY *) NULL;
GFX_ENTRY *gfx_entry_end = (GFX_ENTRY *) NULL;
CFIndex ret, count;
CFIndex needed;
uint64_t bigint;
num_blocks = (int)CFDictionaryGetCount(plist);
if(!num_blocks)
{
printf("CreateGFXFromPlist: no dictionaries found in property list\n");
return NULL;
}
dict_keys = CFAllocatorAllocate(NULL, num_blocks * sizeof(CFStringRef), 0);
dict_vals = CFAllocatorAllocate(NULL, num_blocks * sizeof(CFDictionaryRef), 0);
if(!dict_keys || !dict_vals)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
return NULL;
}
//create header data
gfx_header = (GFX_HEADER *)calloc(1,sizeof(GFX_HEADER));
gfx_header->filesize = 0; // we dont know yet
gfx_header->var1 = 0x1;
gfx_header->countofblocks = num_blocks;
CFDictionaryGetKeysAndValues(plist, (const void **)dict_keys, (const void **)dict_vals);
gfx_size = 12; // set first 12 bytes
gfx_blockheader_head = NULL;
gfx_blockheader_end = NULL;
for(i=0; i<num_blocks; i++)
{
this_block = (CFMutableDictionaryRef) CFDictionaryGetValue(plist,dict_keys[i]);
num_rec = (int)CFDictionaryGetCount(this_block);
if(!num_rec)
{
printf("CreateGFXFromPlist: empty dictionary block found in property list\n");
goto error;
}
block_size=0;
block_size+=4; // size record itself
block_size+=4; // entries count record
block_keys = CFAllocatorAllocate(NULL, num_rec * sizeof(CFStringRef), 0);
block_vals = CFAllocatorAllocate(NULL, num_rec * sizeof(CFTypeRef), 0);
gfx_blockheader = (GFX_BLOCKHEADER *)calloc(1,sizeof(GFX_BLOCKHEADER));
gfx_blockheader->blocksize = 0; // dont know yet
gfx_blockheader->records = num_rec;
count = CFStringGetLength(dict_keys[i]) + 1;
bytes16 = calloc(count, sizeof(CHAR16));
if (!bytes16)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
goto error;
}
ret = CFStringGetBytes(dict_keys[i], CFRangeMake(0, count-1), kCFStringEncodingUTF16, 0, false, (void*) bytes16, count * sizeof(CHAR16), &needed);
if(ret != count-1) // not utf16 string
{
fprintf(stderr, "CreateGFXFromPlist: string conversion error occured, not UTF16 string!\n");
goto error;
}
// add at end string terminator
bytes16[ret] = '\0';
// is hex or text notation
if(isHexString16(bytes16,(unsigned int)ret))
{
// hexadecimal devicepath
gfx_blockheader->devpath = (EFI_DEVICE_PATH_PROTOCOL *)hex2bin16(bytes16, &len);
}
else
{
// try convert from text notation
gfx_blockheader->devpath = ConvertTextToDevicePath (bytes16);
}
free(bytes16);
bytes16 = NULL;
if(gfx_blockheader->devpath == NULL)
{
fprintf(stderr, "CreateGFXFromPlist: device path conversion error occured, not correct syntax!\n");
goto error;
}
gfx_blockheader->devpath_len = (unsigned int)UefiDevicePathLibGetDevicePathSize (gfx_blockheader->devpath);
block_size+= gfx_blockheader->devpath_len; // header bytes count
CFDictionaryGetKeysAndValues(this_block, (const void **)block_keys, (const void **)block_vals);
gfx_entry_head = NULL;
gfx_entry_end = NULL;
while(--num_rec >= 0)
{
gfx_entry = (GFX_ENTRY *)calloc(1,sizeof(GFX_ENTRY));
count = CFStringGetLength(block_keys[num_rec]) + 1;
bytes = (unsigned char *)calloc(count, sizeof(unsigned char));
if (!bytes)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
goto error;
}
ret = CFStringGetBytes(block_keys[num_rec], CFRangeMake(0, count-1), kCFStringEncodingASCII, 0, false, bytes, count, &needed);
if(ret != count-1) // not ascii string
{
fprintf(stderr, "CreateGFXFromPlist: string conversion error occured, not ascii string!\n");
goto error;
}
// add at end string terminator
bytes[ret] = '\0';
gfx_entry->key = (char *)bytes;
gfx_entry->key_len = (unsigned int)ret;
gfx_entry->bkey = str2uni((char *)bytes, (int)ret);
gfx_entry->bkey_len = unilen((char *)bytes, (int)ret);
block_size+=4; // key len
block_size+=gfx_entry->bkey_len;
if(CFGetTypeID(block_vals[num_rec]) == CFStringGetTypeID())
{
count = CFStringGetLength(block_vals[num_rec]) + 1;
bytes = (unsigned char *)calloc(count, sizeof(unsigned char));
if (!bytes)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
goto error;
}
ret = CFStringGetBytes(block_vals[num_rec], CFRangeMake(0, count-1), kCFStringEncodingASCII, 0, false, bytes, count, &needed);
if(ret != count-1) // not ascii string
{
fprintf(stderr, "CreateGFXFromPlist: string conversion error occured, not ascii string!\n");
goto error;
}
// add at end string terminator
bytes[ret] = '\0';
// if is 0xXX or 0xXXXX or 0xXXXXXXXX hex string
HexStr = TrimHexStr ((char *)bytes, &IsHex);
if(IsHex)
{
bigint = Xtoi (HexStr, &HexBytes);
switch(HexBytes)
{
case 2:
needed = sizeof(unsigned char);
data = (unsigned char *)calloc(needed, sizeof(unsigned char));
if (!data)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
return NULL;
}
WRITE_UINT8(data, bigint);
gfx_entry->val = data;
gfx_entry->val_len = (unsigned int)needed;
gfx_entry->val_type = DATA_INT8;
break;
case 4:
needed = sizeof(unsigned short);
data = (unsigned char *)calloc(needed, sizeof(unsigned char));
if (!data)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
return NULL;
}
WRITE_UINT16(data, bigint);
gfx_entry->val = data;
gfx_entry->val_len = (unsigned int)needed;
gfx_entry->val_type = DATA_INT16;
break;
case 8:
needed = sizeof(unsigned int);
data = (unsigned char *)calloc(needed, sizeof(unsigned char));
if (!data)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
return NULL;
}
WRITE_UINT32(data, bigint);
gfx_entry->val = data;
gfx_entry->val_len = (unsigned int)needed;
gfx_entry->val_type = DATA_INT32;
break;
default:
fprintf(stderr, "CreateGFXFromPlist: incompatible hex string size, (only 0xXX or 0xXXXX or 0xXXXXXXXX hex strings accepted)\n");
return NULL;
break;
}
}
else
{
gfx_entry->val = bytes;
// strings in device properties don't end with null so ignore the fact that strings in IORegExplorer do end with \0.
gfx_entry->val_len = (unsigned int)(ret);
gfx_entry->val_type = DATA_STRING;
}
}
else if(CFGetTypeID(block_vals[num_rec]) == CFNumberGetTypeID())
{
needed = sizeof(unsigned int);
bytes = (unsigned char *)calloc(needed, sizeof(unsigned char));
if (!bytes)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
goto error;
}
CFNumberGetValue(block_vals[num_rec], kCFNumberSInt64Type, &bigint);
WRITE_UINT32(bytes, bigint);
gfx_entry->val = bytes;
gfx_entry->val_len = (unsigned int)needed;
gfx_entry->val_type = DATA_INT32; //only known number type from plist
}
else if(CFGetTypeID(block_vals[num_rec]) == CFBooleanGetTypeID())
{
needed = sizeof(unsigned char);
bytes = (unsigned char *)calloc(needed, sizeof(unsigned char));
if (!bytes)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
goto error;
}
bigint = CFBooleanGetValue(block_vals[num_rec]);
WRITE_UINT8(bytes, bigint);
gfx_entry->val = bytes;
gfx_entry->val_len = (unsigned int)needed;
gfx_entry->val_type = DATA_INT8;
}
else // data type
{
needed = CFDataGetLength(block_vals[num_rec]);
bytes = (unsigned char *)calloc(needed, sizeof(unsigned char));
if (!bytes)
{
fprintf(stderr, "CreateGFXFromPlist: out of memory\n");
goto error;
}
CFDataGetBytes(block_vals[num_rec], CFRangeMake(0,needed), bytes);
gfx_entry->val = bytes;
gfx_entry->val_len = (unsigned int)needed;
gfx_entry->val_type = DATA_BINARY;
}
block_size+=4; // value len
block_size+=gfx_entry->val_len;
if(!gfx_entry_head) // if there are no nodes in list then
gfx_entry_head = gfx_entry; // set head to this new node
if(gfx_entry_end)
gfx_entry_end->next = gfx_entry; // link in new node to the end of the list
gfx_entry->next = NULL; // set next field to signify the end of list
gfx_entry_end = gfx_entry; // adjust end to point to the last node
}
gfx_size+=block_size;
gfx_blockheader->blocksize = block_size;
gfx_blockheader->entries = gfx_entry_head;
if(!gfx_blockheader_head) // if there are no nodes in list then
gfx_blockheader_head = gfx_blockheader; // set head to this new node
if(gfx_blockheader_end)
gfx_blockheader_end->next = gfx_blockheader;// link in new node to the end of the list
gfx_blockheader->next = NULL; // set next field to signify the end of list
gfx_blockheader_end = gfx_blockheader; // adjust end to point to the last node
CFAllocatorDeallocate(NULL, block_keys);
CFAllocatorDeallocate(NULL, block_vals);
}
gfx_header->filesize = gfx_size;
gfx_header->blocks = gfx_blockheader_head;
CFAllocatorDeallocate(NULL, dict_keys);
CFAllocatorDeallocate(NULL, dict_vals);
return gfx_header;
error:
free(bytes16);
free(bytes);
free_gfx_entry_list(gfx_entry_head, gfx_entry_end);
free(gfx_entry);
free(gfx_header);
free_gfx_blockheader_list(gfx_blockheader_head, gfx_blockheader_end);
free(gfx_blockheader);
return NULL;
}
#ifndef CFErrorRef
#define CFErrorRef CFStringRef
#define CFPropertyListCreateWithStream CFPropertyListCreateFromStream