VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/Include/Library/CommonLib.h
blob: d64ae41803b31c464519a7fd7866b55dc97d0c34 (plain)
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
/** @file
EFI common library (helpers)

Copyright (c) 2016. Disk Cryptography Services for EFI (DCS), Alex Kolotnikov
Copyright (c) 2016. VeraCrypt, Mounir IDRASSI 

This program and the accompanying materials are licensed and made available
under the terms and conditions of the GNU Lesser General Public License, version 3.0 (LGPL-3.0).

The full text of the license may be found at
https://opensource.org/licenses/LGPL-3.0
**/

#ifndef __COMMONLIB_H__
#define __COMMONLIB_H__

#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Protocol/BlockIo.h>
#include <Protocol/DcsBmlProto.h>
#include <Protocol/SimpleFileSystem.h>
#include <Protocol/UsbIo.h>
#include <Protocol/AbsolutePointer.h>
#include <Guid/FileInfo.h>
#include <Uefi/UefiGpt.h>

//////////////////////////////////////////////////////////////////////////
// Check error 
//////////////////////////////////////////////////////////////////////////
extern UINTN gCELine;
#define CE(ex) gCELine = __LINE__; if(EFI_ERROR(res = ex)) goto err

#ifndef CSTATIC_ASSERT
#define CSTATIC_ASSERT(b, name) typedef int StaticAssertFailed##name[b ? 1 : -1];
#endif

//////////////////////////////////////////////////////////////////////////
// defines
//////////////////////////////////////////////////////////////////////////
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
#define FIELD_OFFSET(t, f) ((UINTN)(&((t*)0)->f))

//////////////////////////////////////////////////////////////////////////
// Memory procedures wrappers
//////////////////////////////////////////////////////////////////////////

#define MEM_ALLOC MemAlloc
#define MEM_FREE MemFree
#define MEM_REALLOC MemRealloc
#define MEM_BURN(ptr,count) do { volatile char *burnPtr = (volatile char *)(ptr); UINTN burnCount = (UINTN) count; while (burnCount--) *burnPtr++ = 0; } while (0)

VOID*
MemAlloc(
   IN UINTN size
   );

VOID
MemFree(
   IN VOID* ptr
   );

VOID*
MemRealloc(
	IN UINTN  OldSize,
	IN UINTN  NewSize,
	IN VOID   *OldBuffer  OPTIONAL
	);

EFI_STATUS
PrepareMemory(
   IN UINTN    address,
   IN UINTN    len,
   OUT VOID**  mem
   );

//////////////////////////////////////////////////////////////////////////
// handles
//////////////////////////////////////////////////////////////////////////

EFI_STATUS
EfiGetHandles(
   IN  EFI_LOCATE_SEARCH_TYPE   SearchType,
   IN  EFI_GUID                 *Protocol, OPTIONAL
   IN  VOID                     *SearchKey, OPTIONAL
   OUT EFI_HANDLE               **Buffer,
   OUT UINTN                    *Count
   );

EFI_STATUS
EfiGetStartDevice(
   OUT EFI_HANDLE* handle
   );
//////////////////////////////////////////////////////////////////////////
// Print handle info
//////////////////////////////////////////////////////////////////////////

VOID EfiPrintDevicePath(
   IN EFI_HANDLE handle
   );

VOID
EfiPrintProtocols(
   IN EFI_HANDLE handle
   );

//////////////////////////////////////////////////////////////////////////
// Block I/O
//////////////////////////////////////////////////////////////////////////

EFI_BLOCK_IO_PROTOCOL*
EfiGetBlockIO(
   IN EFI_HANDLE handle
   );

extern EFI_HANDLE* gBIOHandles;
extern UINTN       gBIOCount;

EFI_STATUS
InitBio();

BOOLEAN
EfiIsPartition(
	IN    EFI_HANDLE              h
	);

EFI_STATUS
EfiGetPartDetails(
	IN    EFI_HANDLE              h,
	OUT   HARDDRIVE_DEVICE_PATH*  dpVolme,
	OUT   EFI_HANDLE*             hDisk
	);

EFI_STATUS
EfiGetPartGUID(
	IN    EFI_HANDLE              h,
	OUT   EFI_GUID*               guid
	);

EFI_STATUS
EfiFindPartByGUID(
	IN   EFI_GUID*               guid,
	OUT  EFI_HANDLE*             h
	);

//////////////////////////////////////////////////////////////////////////
// GPT
//////////////////////////////////////////////////////////////////////////

BOOLEAN
GptHeaderCheckCrc(
	IN UINTN                 MaxSize,
	IN OUT EFI_TABLE_HEADER  *Hdr
	);

EFI_STATUS
GptCheckEntryArray(
	IN  EFI_PARTITION_TABLE_HEADER  *PartHeader,
	IN  EFI_PARTITION_ENTRY         *Entrys
	);

EFI_STATUS
GptUpdateCRC(
	IN  EFI_PARTITION_TABLE_HEADER  *PartHeader,
	IN  EFI_PARTITION_ENTRY         *Entrys
	);

EFI_STATUS
GptReadEntryArray(
	IN  EFI_BLOCK_IO_PROTOCOL*      BlockIo,
	IN  EFI_PARTITION_TABLE_HEADER  *PartHeader,
	OUT EFI_PARTITION_ENTRY         **Entrys
	);

EFI_STATUS
GptReadHeader(
	IN  EFI_BLOCK_IO_PROTOCOL*      BlockIo,
	IN  EFI_LBA                     HeaderLba,
	OUT EFI_PARTITION_TABLE_HEADER  **PartHeader
	);

//////////////////////////////////////////////////////////////////////////
// General EFI tables
//////////////////////////////////////////////////////////////////////////
#define EFITABLE_HEADER_SIGN SIGNATURE_64('E','F','I','T','A','B','L','E')

BOOLEAN
TablesVerify(
	IN UINTN maxSize,
	IN VOID* tables);

BOOLEAN
TablesGetData(
	IN  VOID*   tables,
	IN  UINT64  sign,
	OUT VOID**  data,
	OUT UINTN*  size);

BOOLEAN
TablesDelete(
	IN  VOID*   tables,
	IN  UINT64  sign
	);

BOOLEAN
TablesAppend(
	IN OUT VOID**  tables,
	IN     UINT64  sign,
	IN     VOID*   data,
	IN     UINTN   size);

//////////////////////////////////////////////////////////////////////////
// Bluetooth
//////////////////////////////////////////////////////////////////////////
extern EFI_HANDLE* gBluetoothIoHandles;
extern UINTN       gBluetoothIoCount;

extern EFI_HANDLE* gBluetoothHcHandles;
extern UINTN       gBluetoothHcCount;

extern EFI_HANDLE* gBluetoothConfigHandles;
extern UINTN       gBluetoothConfigCount;

EFI_STATUS
InitBluetooth();

//////////////////////////////////////////////////////////////////////////
// TCG
//////////////////////////////////////////////////////////////////////////
extern EFI_HANDLE* gTcgHandles;
extern UINTN       gTcgCount;

extern EFI_HANDLE* gTcg2Handles;
extern UINTN       gTcg2Count;

EFI_STATUS
InitTcg();

//////////////////////////////////////////////////////////////////////////
// USB
//////////////////////////////////////////////////////////////////////////
extern EFI_HANDLE* gUSBHandles;
extern UINTN       gUSBCount;

EFI_STATUS
InitUsb();

EFI_STATUS
UsbGetIO(
	IN    EFI_HANDLE					Handle,
	OUT   EFI_USB_IO_PROTOCOL**	UsbIo
	);

EFI_STATUS
UsbGetIOwithDescriptor(
	IN    EFI_HANDLE					Handle,
	OUT   EFI_USB_IO_PROTOCOL**	UsbIo,
	OUT   EFI_USB_DEVICE_DESCRIPTOR* UsbDescriptor
	);

EFI_STATUS
UsbGetId(
	IN    EFI_HANDLE		Handle,
	OUT   CHAR8**			id
	);

#define PC_to_RDR_XfrBlock_Message 0x6F

#pragma pack(1)
typedef struct _CCID_HEADER_OUT {
	UINT8    bMessageType;     // Type of the message
	UINT32   dwLength;         // Length of data
	UINT8    bSlot;            // 
	UINT8    bSeq;             // 
	UINT8    bReserved;        // 
	UINT16   wLevelParameter;  // 0000h Short APDU level 
} CCID_HEADER_OUT;

typedef struct _CCID_HEADER_IN {
	UINT8    bMessageType;     // Type of the message
	UINT32   dwLength;         // Length of data
	UINT8    bSlot;            // 
	UINT8    bSeq;             // 
	UINT8    bStatus;          // 
	UINT8    bError;           // 
	UINT8    bChainParameter;
} CCID_HEADER_IN;
#pragma pack()

EFI_STATUS
UsbScTransmit(
	IN    EFI_USB_IO_PROTOCOL    *UsbIO,
	IN    UINT8*                 cmd,
	IN    UINTN                  cmdLen,
	OUT   UINT8*                 resp,
	OUT   UINTN*                 respLen,
	OUT   UINT16*                statusSc
	);

//////////////////////////////////////////////////////////////////////////
// Touch
//////////////////////////////////////////////////////////////////////////

extern EFI_HANDLE* gTouchHandles;
extern UINTN       gTouchCount;
extern int         gTouchSimulate;
extern EFI_ABSOLUTE_POINTER_PROTOCOL*	gTouchPointer;
extern UINT32      gTouchSimulateStep;

EFI_STATUS
InitTouch();

EFI_STATUS
TouchGetIO(
	IN    EFI_HANDLE								Handle,
	OUT   EFI_ABSOLUTE_POINTER_PROTOCOL**	io
	);


//////////////////////////////////////////////////////////////////////////
// Console I/O
//////////////////////////////////////////////////////////////////////////

#define OUT_PRINT(format, ...) AttrPrintEx(-1,-1, format, ##__VA_ARGS__)
#define ERR_PRINT(format, ...) AttrPrintEx(-1,-1, L"%E" format L"%N" , ##__VA_ARGS__)

VOID
PrintBytes(
	IN UINT8* Data,
	IN UINTN Size);

EFI_STATUS
ConsoleGetOutput(
	IN EFI_HANDLE handle,
	OUT   EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL**	io
	);

VOID 
FlushInput();

VOID
FlushInputDelay(
	IN UINTN delay
	);

EFI_INPUT_KEY
KeyWait(
   CHAR16* Prompt,
   UINTN mDelay,
   UINT16 scanCode,
   UINT16 unicodeChar);

EFI_INPUT_KEY
GetKey(void);

VOID
ConsoleShowTip(
	IN CHAR16* tip,
	IN UINTN   delay);

VOID
GetLine(
   UINTN    *length,
   CHAR16   *line,
   CHAR8    *asciiLine,
   UINTN    line_max,
   UINT8    show);

int
AskAsciiString(
   CHAR8* prompt,
   CHAR8* str,
   UINTN max_len,
   UINT8 visible,
	CHAR8* defStr);

int
AskInt(
   CHAR8* prompt,
   UINT8 visible);


UINT8
AskConfirm(
   CHAR8* prompt,
   UINT8 visible);

UINT64
AskUINT64(
	IN char* prompt,
	IN UINT64 def);

UINT64
AskHexUINT64(
	IN char* prompt,
	IN UINT64 def);

UINTN
AskUINTN(
	IN char* prompt,
	IN UINTN def);

BOOLEAN
AsciiHexToDigit(
	OUT UINT8  *b, 
	IN  CHAR8  *str
	);

BOOLEAN
AsciiHexToByte(
	OUT UINT8  *b,
	IN  CHAR8  *str
	);

BOOLEAN
AsciiStrToGuid(
	OUT EFI_GUID  *guid, 
	IN  CHAR8     *str
	);

BOOLEAN
AsciiHexToBytes(
	OUT UINT8  *b,
	IN  UINTN  *bytesLen,
	IN  CHAR8  *str
	);

BOOLEAN
StrHexToBytes(
	OUT UINT8  *b,
	IN  UINTN  *bytesLen,
	IN  CHAR16  *str
	);

//////////////////////////////////////////////////////////////////////////
// Menu
//////////////////////////////////////////////////////////////////////////
typedef EFI_STATUS(*MENU_ACTION)(IN VOID *ctx);

typedef struct _MENU_ITEM MENU_ITEM;
typedef struct _MENU_ITEM {
	CHAR16         Text[128];
	CHAR16         Select;
	MENU_ACTION    Action;
	VOID*          Context;
	MENU_ITEM      *Next;
} MENU_ITEM, *PMENU_ITEM;

PMENU_ITEM
DcsMenuAppend(
	IN PMENU_ITEM  menu,
	IN CHAR16     *text,
	IN CHAR16     select,
	IN MENU_ACTION action,
	IN VOID*       actionContext
	);

VOID
DcsMenuPrint(
	IN  PMENU_ITEM head
	);

//////////////////////////////////////////////////////////////////////////
// Attribute print
//////////////////////////////////////////////////////////////////////////

extern BOOLEAN	gShellReady;

VOID
SetShellAPI(
	IN VOID* shellProtocol,
	IN VOID* shellParametersProtocol
	);

/**
Print at a specific location on the screen.

This function will move the cursor to a given screen location and print the specified string.

If -1 is specified for either the Row or Col the current screen location for BOTH
will be used.

If either Row or Col is out of range for the current console, then ASSERT.
If Format is NULL, then ASSERT.

In addition to the standard %-based flags as supported by UefiLib Print() this supports
the following additional flags:
%N       -   Set output attribute to normal
%H       -   Set output attribute to highlight
%E       -   Set output attribute to error
%B       -   Set output attribute to blue color
%V       -   Set output attribute to green color

Note: The background color is controlled by the shell command cls.

@param[in] Col        the column to print at
@param[in] Row        the row to print at
@param[in] Format     the format string
@param[in] ...        The variable argument list.

@return EFI_SUCCESS           The printing was successful.
@return EFI_DEVICE_ERROR      The console device reported an error.
**/
EFI_STATUS
EFIAPI
AttrPrintEx(
	IN INT32                Col OPTIONAL,
	IN INT32                Row OPTIONAL,
	IN CONST CHAR16         *Format,
	...
	);

//////////////////////////////////////////////////////////////////////////
// Console control
//////////////////////////////////////////////////////////////////////////

extern EFI_HANDLE* gConsoleControlHandles;
extern UINTN       gConsoleControlCount;

EFI_STATUS
InitConsoleControl();

//////////////////////////////////////////////////////////////////////////
// Beep
//////////////////////////////////////////////////////////////////////////
extern EFI_HANDLE*                gSpeakerHandles;
extern UINTN                      gSpeakerCount;
extern EFI_GUID                   gSpeakerGuid;

extern int gBeepEnabled;
extern BOOLEAN	gBeepControlEnabled;
extern int gBeepDevice;
extern int gBeepNumberDefault;
extern int gBeepDurationDefault;
extern int gBeepIntervalDefault;
extern int gBeepToneDefault;


EFI_STATUS
InitSpeaker();

EFI_STATUS
SpeakerBeep(
	IN UINT16  Tone,
	IN UINTN   NumberOfBeeps,
	IN UINTN   Duration,
	IN UINTN   Interval
	);

EFI_STATUS
SpeakerSelect(
	IN UINTN index
	);

//////////////////////////////////////////////////////////////////////////
// BML
//////////////////////////////////////////////////////////////////////////
extern EFI_HANDLE*                gBmlHandles;
extern UINTN                      gBmlCount;
extern EFI_DCSBML_PROTOCOL*	      gBml;
extern EFI_GUID                   gBmlGuid;

EFI_STATUS
InitBml();

EFI_STATUS
BmlLock(
    IN BOOLEAN lock
    );


//////////////////////////////////////////////////////////////////////////
// Efi variables
//////////////////////////////////////////////////////////////////////////

#define DCS_BOOT_STR L"DcsBoot"

extern EFI_GUID gEfiDcsVariableGuid;

EFI_STATUS
EfiGetVar(
   IN  CONST CHAR16*    varName,
   IN  EFI_GUID*        varGuid,
   OUT VOID**           varValue,
   OUT UINTN*           varSize,
   OUT UINT32*          varAttr
   );

EFI_STATUS
EfiSetVar(
   IN  CONST CHAR16*    varName,
   IN  EFI_GUID*        varGuid,
   IN  VOID*            varValue,
   IN  UINTN            varSize,
   IN  UINT32           varAttr
   );

EFI_STATUS
BootOrderInsert(
	IN CHAR16 *OrderVarName,
	IN UINTN index,
	UINT16   value);

EFI_STATUS
BootOrderRemove(
	IN CHAR16 *OrderVarName,
	UINT16   value
	);

EFI_STATUS
BootOrderPresent(
    IN CHAR16 *OrderVarName,
    UINT16   value);

EFI_STATUS
BootMenuItemCreate(
	IN CHAR16     *VarName,
	IN CHAR16     *Desc,
	IN EFI_HANDLE volumeHandle,
	IN CHAR16     *Path,
	IN BOOLEAN    Reduced
	);

EFI_STATUS
BootMenuItemRemove(
	IN CHAR16     *VarName
	);



//////////////////////////////////////////////////////////////////////////
// File
//////////////////////////////////////////////////////////////////////////


extern EFI_FILE*      gFileRoot;
extern EFI_HANDLE     gFileRootHandle;

extern EFI_HANDLE* gFSHandles;
extern UINTN       gFSCount;

EFI_STATUS
InitFS();

EFI_STATUS
FileOpenRoot(
   IN    EFI_HANDLE rootHandle,
   OUT   EFI_FILE** rootFile);

EFI_STATUS
FileOpen(
   IN    EFI_FILE*   root,
   IN    CHAR16*     name,
   OUT   EFI_FILE**  file,
   IN    UINT64      mode,
   IN    UINT64      attributes
   );

EFI_STATUS
FileClose(
   IN EFI_FILE* f);

EFI_STATUS
FileDelete(
   IN    EFI_FILE*   root,
   IN    CHAR16*     name
   );

EFI_STATUS
FileRead(
   IN       EFI_FILE*   f,
   OUT      VOID*       data,
   IN OUT   UINTN*      bytes,
   IN OUT   UINT64*     position);

EFI_STATUS
FileWrite(
   IN       EFI_FILE*   f,
   IN       VOID*       data,
   IN OUT   UINTN*      bytes,
   IN OUT   UINT64*     position);

UINTN
FileAsciiPrint(
	IN EFI_FILE            *f,
	IN CONST CHAR8         *format,
	...
	);

EFI_STATUS
FileGetInfo(
   IN    EFI_FILE*         f,
   OUT   EFI_FILE_INFO**   info,
   OUT   UINTN*            size
   );

EFI_STATUS
FileGetSize(
   IN    EFI_FILE*   f,
   OUT   UINTN*     size
   );

EFI_STATUS
FileLoad(
   IN    EFI_FILE*   root,
   IN    CHAR16*     name,
   OUT   VOID**      data,
   OUT   UINTN*      size
   );

EFI_STATUS
FileSave(
   IN    EFI_FILE*   root,
   IN    CHAR16*     name,
   IN    VOID*       data,
   IN    UINTN      size
   );

EFI_STATUS
FileExist(
	IN    EFI_FILE*   root,
	IN    CHAR16*     name
	);

EFI_STATUS
FileRename(
	IN    EFI_FILE*   root,
	IN    CHAR16*     src,
	IN    CHAR16*     dst
	);

EFI_STATUS
FileCopy(
	IN    EFI_FILE*   srcroot,
	IN    CHAR16*     src,
	IN    EFI_FILE*   dstroot,
	IN    CHAR16*     dst,
	IN    UINTN       bufSz
	);

//////////////////////////////////////////////////////////////////////////
// Exec
//////////////////////////////////////////////////////////////////////////

EFI_STATUS
EfiExec(
   IN    EFI_HANDLE  deviceHandle,
   IN    CHAR16*     path
   );

EFI_STATUS
ConnectAllEfi(
   VOID
   );

VOID
EfiCpuHalt();

#endif