Loading...
  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
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Extensible Firmware Interface
 * Based on 'Extensible Firmware Interface Specification' version 0.9,
 * April 30, 1999
 *
 * Copyright (C) 1999 VA Linux Systems
 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
 * Copyright (C) 1999, 2002-2003 Hewlett-Packard Co.
 *	David Mosberger-Tang <davidm@hpl.hp.com>
 *	Stephane Eranian <eranian@hpl.hp.com>
 *
 * From include/linux/efi.h in kernel 4.1 with some additions/subtractions
 */

#ifndef _EFI_H
#define _EFI_H

#include <linux/linkage.h>
#include <linux/string.h>
#include <linux/types.h>
#if !defined(USE_HOSTCC) && CONFIG_EFI_APP
#include <net.h>
#endif

struct abuf;
struct efi_input_key;
struct efi_key_data;
struct udevice;

/* Type INTN in UEFI specification */
#define efi_intn_t ssize_t
/* Type UINTN in UEFI specification*/
#define efi_uintn_t size_t

/*
 * EFI on x86_64 uses the Microsoft ABI which is not the default for GCC.
 *
 * There are two scenarios for EFI on x86_64: building a 64-bit EFI stub
 * codes (CONFIG_EFI_STUB_64BIT) and building a 64-bit U-Boot (CONFIG_X86_64).
 * Either needs to be properly built with the '-m64' compiler flag, and hence
 * it is enough to only check the compiler provided define __x86_64__ here.
 */
#ifdef __x86_64__
#define EFIAPI __attribute__((ms_abi))
#define efi_va_list __builtin_ms_va_list
#define efi_va_start __builtin_ms_va_start
#define efi_va_copy __builtin_ms_va_copy
#define efi_va_arg __builtin_va_arg
#define efi_va_end __builtin_ms_va_end
#else
#define EFIAPI asmlinkage
#define efi_va_list va_list
#define efi_va_start va_start
#define efi_va_copy va_copy
#define efi_va_arg va_arg
#define efi_va_end va_end
#endif /* __x86_64__ */

#define EFI32_LOADER_SIGNATURE	"EL32"
#define EFI64_LOADER_SIGNATURE	"EL64"

#ifdef CONFIG_EFI_APP
/* add an empty value for this so that efi_variables.h can be included */
#define __efi_runtime
#endif

/**
 * struct efi_device_path - device path protocol
 *
 * @type:	device path type
 * @sub_type:	device path sub-type
 * @length:	length of the device path node including the header
 */
struct efi_device_path {
	u8 type;
	u8 sub_type;
	u16 length;
} __packed;

/*
 * The EFI spec defines the EFI_GUID as
 * "128-bit buffer containing a unique identifier value. Unless otherwise specified,
 * aligned on a 64-bit boundary".
 * Page 163 of the UEFI specification v2.10 and
 * EDK2 reference implementation both define EFI_GUID as
 * struct { u32 a; u16; b; u16 c; u8 d[8]; }; which is 4-byte
 * aligned.
 */
typedef struct efi_guid {
	u8 b[16];
} efi_guid_t __attribute__((aligned(4)));

static inline int guidcmp(const void *g1, const void *g2)
{
	return memcmp(g1, g2, sizeof(efi_guid_t));
}

static inline void *guidcpy(void *dst, const void *src)
{
	return memcpy(dst, src, sizeof(efi_guid_t));
}

#define EFI_BITS_PER_LONG	(sizeof(long) * 8)

/* Bit mask for EFI status code with error */
#define EFI_ERROR_MASK (1UL << (EFI_BITS_PER_LONG - 1))
/* Status codes returned by EFI protocols */
#define EFI_SUCCESS			0
#define EFI_LOAD_ERROR			(EFI_ERROR_MASK | 1)
#define EFI_INVALID_PARAMETER		(EFI_ERROR_MASK | 2)
#define EFI_UNSUPPORTED			(EFI_ERROR_MASK | 3)
#define EFI_BAD_BUFFER_SIZE		(EFI_ERROR_MASK | 4)
#define EFI_BUFFER_TOO_SMALL		(EFI_ERROR_MASK | 5)
#define EFI_NOT_READY			(EFI_ERROR_MASK | 6)
#define EFI_DEVICE_ERROR		(EFI_ERROR_MASK | 7)
#define EFI_WRITE_PROTECTED		(EFI_ERROR_MASK | 8)
#define EFI_OUT_OF_RESOURCES		(EFI_ERROR_MASK | 9)
#define EFI_VOLUME_CORRUPTED		(EFI_ERROR_MASK | 10)
#define EFI_VOLUME_FULL			(EFI_ERROR_MASK | 11)
#define EFI_NO_MEDIA			(EFI_ERROR_MASK | 12)
#define EFI_MEDIA_CHANGED		(EFI_ERROR_MASK | 13)
#define EFI_NOT_FOUND			(EFI_ERROR_MASK | 14)
#define EFI_ACCESS_DENIED		(EFI_ERROR_MASK | 15)
#define EFI_NO_RESPONSE			(EFI_ERROR_MASK | 16)
#define EFI_NO_MAPPING			(EFI_ERROR_MASK | 17)
#define EFI_TIMEOUT			(EFI_ERROR_MASK | 18)
#define EFI_NOT_STARTED			(EFI_ERROR_MASK | 19)
#define EFI_ALREADY_STARTED		(EFI_ERROR_MASK | 20)
#define EFI_ABORTED			(EFI_ERROR_MASK | 21)
#define EFI_ICMP_ERROR			(EFI_ERROR_MASK | 22)
#define EFI_TFTP_ERROR			(EFI_ERROR_MASK | 23)
#define EFI_PROTOCOL_ERROR		(EFI_ERROR_MASK | 24)
#define EFI_INCOMPATIBLE_VERSION	(EFI_ERROR_MASK | 25)
#define EFI_SECURITY_VIOLATION		(EFI_ERROR_MASK | 26)
#define EFI_CRC_ERROR			(EFI_ERROR_MASK | 27)
#define EFI_END_OF_MEDIA		(EFI_ERROR_MASK | 28)
#define EFI_END_OF_FILE			(EFI_ERROR_MASK | 31)
#define EFI_INVALID_LANGUAGE		(EFI_ERROR_MASK | 32)
#define EFI_COMPROMISED_DATA		(EFI_ERROR_MASK | 33)
#define EFI_IP_ADDRESS_CONFLICT		(EFI_ERROR_MASK | 34)
#define EFI_HTTP_ERROR			(EFI_ERROR_MASK | 35)
#define EFI_ERROR_COUNT			36

#define EFI_WARN_UNKNOWN_GLYPH		1
#define EFI_WARN_DELETE_FAILURE		2
#define EFI_WARN_WRITE_FAILURE		3
#define EFI_WARN_BUFFER_TOO_SMALL	4
#define EFI_WARN_STALE_DATA		5
#define EFI_WARN_FILE_SYSTEM		6
#define EFI_WARN_RESET_REQUIRED		7

typedef unsigned long efi_status_t;
typedef u64 efi_physical_addr_t;
typedef u64 efi_virtual_addr_t;
typedef struct efi_object *efi_handle_t;

#define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
	{{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, \
		((a) >> 24) & 0xff, \
		(b) & 0xff, ((b) >> 8) & 0xff, \
		(c) & 0xff, ((c) >> 8) & 0xff, \
		(d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) } }

extern const efi_guid_t efi_global_variable_guid;
extern const efi_guid_t efi_guid_fdt;
/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
extern const efi_guid_t efi_guid_driver_binding_protocol;
extern const efi_guid_t efi_guid_component_name2;
/* GUIDs for authentication - most of these are still in efi_loader.h */
extern const efi_guid_t efi_guid_image_security_database;

/* Access to Shim variables */
extern const efi_guid_t efi_shim_lock;

extern const efi_guid_t efi_guid_loaded_image;
extern const efi_guid_t efi_guid_loaded_image_device_path;
extern const efi_guid_t efi_guid_device_path;

extern const efi_guid_t efi_simple_file_system_protocol_guid;
extern const efi_guid_t efi_guid_simple_pointer;
extern const efi_guid_t efi_guid_absolute_pointer;

/* Generic EFI table header */
struct efi_table_hdr {
	u64 signature;
	u32 revision;
	u32 headersize;
	u32 crc32;
	u32 reserved;
};

/* Allocation types for calls to boottime->allocate_pages*/
/**
 * enum efi_allocate_type - address restriction for memory allocation
 */
enum efi_allocate_type {
	/**
	 * @EFI_ALLOCATE_ANY_PAGES:
	 * Allocate any block of sufficient size. Ignore memory address.
	 */
	EFI_ALLOCATE_ANY_PAGES,
	/**
	 * @EFI_ALLOCATE_MAX_ADDRESS:
	 * Allocate a memory block with an uppermost address less or equal
	 * to the indicated address.
	 */
	EFI_ALLOCATE_MAX_ADDRESS,
	/**
	 * @EFI_ALLOCATE_ADDRESS:
	 * Allocate a memory block starting at the indicated address.
	 */
	EFI_ALLOCATE_ADDRESS,
	/**
	 * @EFI_MAX_ALLOCATE_TYPE:
	 * Value use for range checking.
	 */
	EFI_MAX_ALLOCATE_TYPE,
};

/* Enumeration of memory types introduced in UEFI */
enum efi_memory_type {
	EFI_RESERVED_MEMORY_TYPE,
	/*
	 * The code portions of a loaded application.
	 * (Note that UEFI OS loaders are UEFI applications.)
	 */
	EFI_LOADER_CODE,
	/*
	 * The data portions of a loaded application and
	 * the default data allocation type used by an application
	 * to allocate pool memory.
	 */
	EFI_LOADER_DATA,
	/* The code portions of a loaded Boot Services Driver */
	EFI_BOOT_SERVICES_CODE,
	/*
	 * The data portions of a loaded Boot Services Driver and
	 * the default data allocation type used by a Boot Services
	 * Driver to allocate pool memory.
	 */
	EFI_BOOT_SERVICES_DATA,
	/* The code portions of a loaded Runtime Services Driver */
	EFI_RUNTIME_SERVICES_CODE,
	/*
	 * The data portions of a loaded Runtime Services Driver and
	 * the default data allocation type used by a Runtime Services
	 * Driver to allocate pool memory.
	 */
	EFI_RUNTIME_SERVICES_DATA,
	/* Free (unallocated) memory */
	EFI_CONVENTIONAL_MEMORY,
	/* Memory in which errors have been detected */
	EFI_UNUSABLE_MEMORY,
	/* Memory that holds the ACPI tables */
	EFI_ACPI_RECLAIM_MEMORY,
	/* Address space reserved for use by the firmware */
	EFI_ACPI_MEMORY_NVS,
	/*
	 * Used by system firmware to request that a memory-mapped IO region
	 * be mapped by the OS to a virtual address so it can be accessed by
	 * EFI runtime services.
	 */
	EFI_MMAP_IO,
	/*
	 * System memory-mapped IO region that is used to translate
	 * memory cycles to IO cycles by the processor.
	 */
	EFI_MMAP_IO_PORT,
	/*
	 * Address space reserved by the firmware for code that is
	 * part of the processor.
	 */
	EFI_PAL_CODE,
	/*
	 * Byte addressable non-volatile memory.
	 */
	EFI_PERSISTENT_MEMORY_TYPE,
	/*
	 * Unaccepted memory must be accepted by boot target before usage.
	 */
	EFI_UNACCEPTED_MEMORY_TYPE,

	EFI_MAX_MEMORY_TYPE,
};

/* Attribute values */
#define EFI_MEMORY_UC		((u64)0x0000000000000001ULL)	/* uncached */
#define EFI_MEMORY_WC		((u64)0x0000000000000002ULL)	/* write-coalescing */
#define EFI_MEMORY_WT		((u64)0x0000000000000004ULL)	/* write-through */
#define EFI_MEMORY_WB		((u64)0x0000000000000008ULL)	/* write-back */
#define EFI_MEMORY_UCE		((u64)0x0000000000000010ULL)	/* uncached, exported */
#define EFI_MEMORY_WP		((u64)0x0000000000001000ULL)	/* write-protect */
#define EFI_MEMORY_RP		((u64)0x0000000000002000ULL)	/* read-protect */
#define EFI_MEMORY_XP		((u64)0x0000000000004000ULL)	/* execute-protect */
#define EFI_MEMORY_NV		((u64)0x0000000000008000ULL)	/* non-volatile */
#define EFI_MEMORY_MORE_RELIABLE \
				((u64)0x0000000000010000ULL)	/* higher reliability */
#define EFI_MEMORY_RO		((u64)0x0000000000020000ULL)	/* read-only */
#define EFI_MEMORY_SP		((u64)0x0000000000040000ULL)	/* specific-purpose memory (SPM) */
#define EFI_MEMORY_CPU_CRYPTO	((u64)0x0000000000080000ULL)	/* cryptographically protectable */
#define EFI_MEMORY_HOT_PLUGGABLE \
				((u64)0x0000000000100000ULL)	/* hot pluggable */
#define EFI_MEMORY_RUNTIME	((u64)0x8000000000000000ULL)	/* range requires runtime mapping */
#define EFI_MEM_DESC_VERSION	1

#define EFI_PAGE_SHIFT		12
#define EFI_PAGE_SIZE		(1ULL << EFI_PAGE_SHIFT)
#define EFI_PAGE_MASK		(EFI_PAGE_SIZE - 1)

/**
 * struct efi_mem_desc - defines an EFI memory record
 *
 * This field implements the EFI_MEMORY_DESCRIPTOR type of the UEFI
 * specification.
 *
 * Note that this struct is for use outside U-Boot, so the two 'start' fields
 * are pointers, not addresses. Use map_to_sysmem() to convert to an address, so
 * that sandbox operates correctly.
 *
 * @type (enum efi_memory_type): EFI memory-type
 * @reserved: unused
 * @physical_start: Start address of region in physical memory
 * @virtual_start: Start address of region in virtual memory, which will be the
 *	same as @physical_start before where both addresses will always be the
 *	same before SetVirtualMemoryMap() is called as the UEFI specification
 *	requires identity mapping.
 * @num_pages: Number of EFI pages this record covers (each is EFI_PAGE_SIZE
 *	bytes)
 * @attribute: Memory attributes (see EFI_MEMORY_...)
 */
struct efi_mem_desc {
	u32 type;
	u32 reserved;
	efi_physical_addr_t physical_start;
	efi_virtual_addr_t virtual_start;
	u64 num_pages;
	u64 attribute;
};

#define EFI_MEMORY_DESCRIPTOR_VERSION 1

/* Types and defines for Time Services */
#define EFI_TIME_ADJUST_DAYLIGHT 0x1
#define EFI_TIME_IN_DAYLIGHT     0x2
#define EFI_UNSPECIFIED_TIMEZONE 0x07ff

struct efi_time {
	u16 year;
	u8 month;
	u8 day;
	u8 hour;
	u8 minute;
	u8 second;
	u8 pad1;
	u32 nanosecond;
	s16 timezone;
	u8 daylight;
	u8 pad2;
};

struct efi_time_cap {
	u32 resolution;
	u32 accuracy;
	u8 sets_to_zero;
};

enum efi_locate_search_type {
	ALL_HANDLES,
	BY_REGISTER_NOTIFY,
	BY_PROTOCOL
};

struct efi_open_protocol_info_entry {
	efi_handle_t agent_handle;
	efi_handle_t controller_handle;
	u32 attributes;
	u32 open_count;
};

#define EFI_TABLE_VERSION	1

/**
 * struct efi_info_hdr - Header for the EFI info table
 *
 * @version:	EFI_TABLE_VERSION
 * @hdr_size:	Size of this struct in bytes
 * @total_size:	Total size of this header plus following data
 * @spare:	Spare space for expansion
 */
struct efi_info_hdr {
	u32 version;
	u32 hdr_size;
	u32 total_size;
	u32 spare[5];
};

/**
 * struct efi_entry_hdr - Header for a table entry
 *
 * @type:	enum eft_entry_t
 * @size:	size of entry bytes excluding header and padding
 * @addr:	address of this entry (0 if it follows the header )
 * @link:	size of entry including header and padding
 * @spare1:	Spare space for expansion
 * @spare2:	Spare space for expansion
 */
struct efi_entry_hdr {
	u32 type;
	u32 size;
	u64 addr;
	u32 link;
	u32 spare1;
	u64 spare2;
};

/**
 * struct efi_entry_memmap - a memory map table passed to U-Boot
 *
 * @version:	EFI's memory map table version
 * @desc_size:	EFI's size of each memory descriptor
 * @spare:	Spare space for expansion
 * @desc:	An array of descriptors, each @desc_size bytes apart
 */
struct efi_entry_memmap {
	u32 version;
	u32 desc_size;
	u64 spare;
	struct efi_mem_desc desc[];
};

/**
 * struct efi_entry_gopmode - a GOP mode table passed to U-Boot
 *
 * @fb_base:	EFI's framebuffer base address
 * @fb_size:	EFI's framebuffer size
 * @info_size:	GOP mode info structure size
 * @info:	Start address of the GOP mode info structure
 */
struct efi_entry_gopmode {
	efi_physical_addr_t fb_base;
	/*
	 * Not like the ones in 'struct efi_gop_mode' which are 'unsigned
	 * long', @fb_size and @info_size have to be 'u64' here. As the EFI
	 * stub codes may have different bit size from the U-Boot payload,
	 * using 'long' will cause mismatch between the producer (stub) and
	 * the consumer (payload).
	 */
	u64 fb_size;
	u64 info_size;
	/*
	 * We cannot directly use 'struct efi_gop_mode_info info[]' here as
	 * it causes compiler to complain: array type has incomplete element
	 * type 'struct efi_gop_mode_info'.
	 */
	struct /* efi_gop_mode_info */ {
		u32 version;
		u32 width;
		u32 height;
		u32 pixel_format;
		u32 pixel_bitmask[4];
		u32 pixels_per_scanline;
	} info[];
};

/**
 * struct efi_entry_systable - system table passed to U-Boot
 *
 * @sys_table:	EFI system table address
 */
struct efi_entry_systable {
	efi_physical_addr_t sys_table;
};

static inline struct efi_mem_desc *efi_get_next_mem_desc(
		struct efi_mem_desc *desc, int desc_size)
{
	return (struct efi_mem_desc *)((ulong)desc + desc_size);
}

/**
 * struct efi_priv - Information about the environment provided by EFI
 *
 * @parent_image: image passed into the EFI app or stub
 * @sys_table: Pointer to system table
 * @boot: Pointer to boot-services table
 * @run: Pointer to runtime-services table
 * @memmap_key: Key returned from get_memory_map()
 * @memmap_desc: List of memory-map records
 * @memmap_alloc: Amount of memory allocated for memory map list
 * @memmap_size Size of memory-map list in bytes
 * @memmap_desc_size: Size of an individual memory-map record, in bytes
 * @memmap_version: Memory-map version
 *
 * @use_pool_for_malloc: true if all allocation should go through the EFI 'pool'
 *	methods allocate_pool() and free_pool(); false to use 'pages' methods
 *	allocate_pages() and free_pages()
 * @ram_base: Base address of RAM (size CONFIG_EFI_RAM_SIZE)
 * @image_data_type: Type of the loaded image (e.g. EFI_LOADER_CODE)
 * @efi_dp_to_text: Pointer to the EFI_DEVICE_PATH protocol, or NULL if none
 *
 * @info: Header of the info list, holding info collected by the stub and passed
 *	to U-Boot
 * @info_size: Size of the info list @info in bytes
 * @next_hdr: Pointer to where to put the next header when adding to the list
 * @jump_addr: Address to jump to to start U-Boot
 * @x86_cs32: x86 code-segment to use for U-Boot
 */
struct efi_priv {
	efi_handle_t parent_image;
	struct efi_loaded_image *loaded_image;
	struct efi_system_table *sys_table;
	struct efi_boot_services *boot;
	struct efi_runtime_services *run;
	efi_uintn_t memmap_key;
	struct efi_mem_desc *memmap_desc;
	efi_uintn_t memmap_alloc;
	efi_uintn_t memmap_size;
	efi_uintn_t memmap_desc_size;
	u32 memmap_version;

	/* app: */
	bool use_pool_for_malloc;
	unsigned long ram_base;
	unsigned int image_data_type;
	struct efi_device_path_to_text_protocol *efi_dp_to_text;

	/* stub: */
	struct efi_info_hdr *info;
	unsigned int info_size;
	void *next_hdr;
	ulong jump_addr;
	int x86_cs32;
};

/*
 * EFI attributes of the udevice handled by efi_media driver
 *
 * @handle: handle of the controller on which this driver is installed
 * @blkio: block io protocol proxied by this driver
 * @device_path: EFI path to the device
 */
struct efi_media_plat {
	efi_handle_t handle;
	struct efi_block_io *blkio;
	struct efi_device_path *device_path;
};

#if !defined(USE_HOSTCC) && CONFIG_EFI_APP
/*
 * EFI attributes of the udevice handled by efi_net driver
 *
 * @handle: handle of the controller on which this driver is installed
 */
struct efi_net_plat {
	struct eth_pdata eth_pdata;
	efi_handle_t handle;
	struct efi_simple_network *snp;
};
#endif

/*
 * EFI attributes of the udevice handled by efi_tpm driver
 *
 * @handle: handle of the controller on which this driver is installed
 * @proto: pointer to the TCG2 EFI protocol
 */
struct efi_tpm_plat {
	efi_handle_t handle;
	struct efi_tcg2_protocol *proto;
};

/* Base address of the EFI image */
extern char image_base[];

/* Start and end of U-Boot image (for payload) */
extern char _binary_u_boot_bin_start[], _binary_u_boot_bin_end[], _binary_u_boot_bin_size[];

/*
 * Variable Attributes
 */
#define EFI_VARIABLE_NON_VOLATILE				0x00000001
#define EFI_VARIABLE_BOOTSERVICE_ACCESS				0x00000002
#define EFI_VARIABLE_RUNTIME_ACCESS				0x00000004
#define EFI_VARIABLE_HARDWARE_ERROR_RECORD			0x00000008
#define EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS			0x00000010
#define EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS	0x00000020
#define EFI_VARIABLE_APPEND_WRITE				0x00000040
#define EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS		0x00000080

#define EFI_VARIABLE_MASK	(EFI_VARIABLE_NON_VOLATILE | \
				EFI_VARIABLE_BOOTSERVICE_ACCESS | \
				EFI_VARIABLE_RUNTIME_ACCESS | \
				EFI_VARIABLE_HARDWARE_ERROR_RECORD | \
				EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS | \
				EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS | \
				EFI_VARIABLE_APPEND_WRITE | \
				EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS)

/* Use internal device tree when starting UEFI application */
#define EFI_FDT_USE_INTERNAL NULL

/**
 * efi_get_priv() - Get access to the EFI-private information
 *
 * This struct it used by both the stub and the app to record things about the
 * EFI environment. It is not available in U-Boot proper after the stub has
 * jumped there. Use efi_info_get() to obtain info in that case.
 *
 * Return: pointer to private info
 */
struct efi_priv *efi_get_priv(void);

/**
 * efi_set_priv() - Set up a pointer to the EFI-private information
 *
 * This is called in the stub and app to record the location of this
 * information.
 *
 * @priv: New location of private data
 */
void efi_set_priv(struct efi_priv *priv);

/**
 * efi_move_priv() - Move EFI-private information to a new location
 *
 * Copies the existing EFI-private data to a new location and updates the
 * global pointer. If there is no existing data, just sets the pointer.
 *
 * @new_priv: New location for private data
 */
void efi_move_priv(struct efi_priv *new_priv);

/**
 * efi_get_sys_table() - Get access to the main EFI system table
 *
 * Returns: pointer to EFI system table
 */
struct efi_system_table *efi_get_sys_table(void);

/**
 * efi_get_boot() - Get access to the EFI boot services table
 *
 * Returns: pointer to EFI boot services table
 */
struct efi_boot_services *efi_get_boot(void);

/**
 * efi_get_run() - Get access to the EFI runtime-services table
 *
 * Returns: pointer to EFI runtime-services table
 */
struct efi_runtime_services *efi_get_run(void);

/**
 * efi_get_parent_image() - Get the handle of the parent image
 *
 * Return: parent-image handle
 */
efi_handle_t efi_get_parent_image(void);

/**
 * efi_get_ram_base() - Find the base of RAM
 *
 * This is used when U-Boot is built as an EFI application.
 *
 * Returns: the base of RAM as known to U-Boot
 */
unsigned long efi_get_ram_base(void);

/**
 * efi_init() - Set up ready for use of EFI boot services
 *
 * @priv:	Pointer to our private EFI structure to fill in
 * @banner:	Banner to display when starting
 * @image:	The image handle passed to efi_main()
 * @sys_table:	The EFI system table pointer passed to efi_main()
 * @verbose:    true to show messages on startup
 * Return: 0 on succcess, EFI error code on failure
 */
int efi_init(struct efi_priv *priv, const char *banner, efi_handle_t image,
	     struct efi_system_table *sys_table, bool verbose);

/**
 * efi_malloc() - Allocate some memory from EFI
 *
 * @priv:	Pointer to private EFI structure
 * @size:	Number of bytes to allocate
 * @retp:	Return EFI status result
 * Returns: pointer to memory allocated, or NULL on error
 */
void *efi_malloc(struct efi_priv *priv, int size, efi_status_t *retp);

/**
 * efi_free() - Free memory allocated from EFI
 *
 * @priv:	Pointer to private EFI structure
 * @ptr:	Pointer to memory to free
 */
void efi_free(struct efi_priv *priv, void *ptr);

/**
 * efi_free_pool() - free memory from pool
 *
 * @buffer:	start of memory to be freed
 * Return:	status code
 */
efi_status_t efi_free_pool(void *buffer);

/**
 * efi_puts() - Write out a string to the EFI console
 *
 * @priv:	Pointer to private EFI structure
 * @str:	String to write (note this is a ASCII, not unicode)
 */
void efi_puts(struct efi_priv *priv, const char *str);

/**
 * efi_putc() - Write out a character to the EFI console
 *
 * @priv:	Pointer to private EFI structure
 * @ch:		Character to write (note this is not unicode)
 */
void efi_putc(struct efi_priv *priv, const char ch);

/**
 * efi_stub_exit_boot_services() - Handle the exit-boot-service procedure
 *
 * Tell EFI we don't want their boot services anymore
 *
 * Return: 0 if OK, non-zero on error
 */
int efi_stub_exit_boot_services(void);

/**
 * efi_app_exit_boot_services() - Handle the exit-boot-service procedure
 *
 * Tell EFI we don't want their boot services anymore
 *
 * This is only available in the app
 *
 * Return: 0 if OK, non-zero on error
 */
int efi_app_exit_boot_services(struct efi_priv *priv, uint key);

/**
 * efi_get_mmap() - Get the memory map from EFI
 *
 * This is used in the app. The caller must free *@descp when done
 *
 * @descp:	Returns allocated pointer to EFI memory map table
 * @sizep:	Returns size of table in bytes
 * @keyp:	Returns memory-map key
 * @desc_sizep:	Returns size of each @desc_base record
 * @versionp:	Returns version number of memory map
 * Returns: 0 on success, -ve on error
 */
int efi_get_mmap(struct efi_mem_desc **descp, int *sizep, uint *keyp,
		 int *desc_sizep, uint *versionp);

/**
 * efi_show_tables() - Show a list of available tables
 *
 * Shows the address, GUID (and name where known) for each table
 *
 * @systab: System table containing the list of tables
 */
void efi_show_tables(struct efi_system_table *systab);

/**
 * efi_get_basename() - Get the default filename to use when loading
 *
 * E.g. this function returns BOOTAA64.EFI for an aarch target
 *
 * Return: Default EFI filename
 */
const char *efi_get_basename(void);

#ifdef CONFIG_SANDBOX
#include <asm/state.h>
#endif

static inline bool efi_use_host_arch(void)
{
#ifdef CONFIG_SANDBOX
	struct sandbox_state *state = state_get_current();

	return state->native;
#else
	return false;
#endif
}

/**
 * efi_get_pxe_arch() - Get the architecture value for PXE
 *
 * See:
 * http://www.iana.org/assignments/dhcpv6-parameters/dhcpv6-parameters.xml
 *
 * Return: Architecture value
 */
int efi_get_pxe_arch(void);

/**
 * efi_mem_is_boot_services() - checks if the memory type relates to boot-time
 *
 * Return: true if loader code/data or boot-services code/data
 */
static inline bool efi_mem_is_boot_services(int type)
{
	return type == EFI_LOADER_CODE || type == EFI_LOADER_DATA ||
		type == EFI_BOOT_SERVICES_CODE ||
		type == EFI_BOOT_SERVICES_DATA;
}

/**
 * efi_mem_type_name() - Get the name of a memory type
 *
 * Return: Name, or "<invalid>" if the type is not known
 */
const char *efi_mem_type_name(enum efi_memory_type type);

/**
 * efi_mem_reserved_sync() - Sync EFI memory map with DT reserved-memory nodes
 *
 * Compares the EFI memory map with the device tree's reserved-memory nodes and
 * adds regions to the devicetree that are reserved in EFI but not mentioned in
 * the devicetree's '/reserved-memory' node. This ensures that memory regions
 * which EFI considers reserved are not used by the OS, e.g. because a
 * hypervisor may be in use..
 *
 * Note: This only works with #address-cells and #size-cells of 2
 *
 * @fdt: Pointer to the devicetree blob
 * @verbose: If true, show detailed output; if false, only show errors
 * Return: Number of regions synced, or -ve on error
 */
int efi_mem_reserved_sync(void *fdt, bool verbose);

/**
 * efi_dump_mem_table() - Dump out the EFI memory map
 *
 * @desc: List of descriptors to dump
 * @size: Size of desc array in bytes
 * @desc_size: Size of each description in @desc
 * @skip_bs: true to skip boot-services allocations
 */
void efi_dump_mem_table(struct efi_mem_desc *desc, int size, int desc_size,
			bool skip_bs);

/**
 * calculate_paths() - Calculate the device and image patch from strings
 *
 * @dev:		device, e.g. "MMC"
 * @devnr:		number of the device, e.g. "1:2"
 * @path:		path to file loaded
 * @device_pathp:	returns EFI device path
 * @image_pathp:	returns EFI image path
 * Return: EFI_SUCCESS on success, else error code
 */
efi_status_t calculate_paths(const char *dev, const char *devnr,
			     const char *path,
			     struct efi_device_path **device_pathp,
			     struct efi_device_path **image_pathp);

/**
 * efi_binary_run_dp() - run loaded UEFI image
 *
 * @image:	memory address of the UEFI image
 * @size:	size of the UEFI image
 * @fdt:	device-tree
 * @initrd:	initrd
 * @initrd_sz:	initrd size
 * @dp_dev:	EFI device-path
 * @dp_img:	EFI image-path
 *
 * Execute an EFI binary image loaded at @image.
 * @size may be zero if the binary is loaded with U-Boot load command.
 *
 * Return:	status code
 */
efi_status_t efi_binary_run_dp(void *image, size_t size, void *fdt,
			       void *initrd, size_t initrd_sz,
			       struct efi_device_path *dp_dev,
			       struct efi_device_path *dp_img);

/**
 * efi_run_image() - run loaded UEFI image
 *
 * @source_buffer:	memory address of the UEFI image
 * @source_size:	size of the UEFI image
 * @dp_dev:		EFI device-path
 * @dp_img:		EFI image-path
 * Return:		status code
 */
efi_status_t efi_run_image(void *source_buffer, efi_uintn_t source_size,
			   struct efi_device_path *dp_dev,
			   struct efi_device_path *dp_img);

/**
 * efi_dp_from_bootdev() - Get the device path from a bootdev
 *
 * This is only available in the app. It looks up the bootdev and returns the
 * assocated device path (attached to its sibling block device)
 *
 * @dev: UCLASS_BOOTDEV device to check
 * @dpp: Returns device path on success
 * Returns: 0 if OK, -ve on error
 */
int efi_dp_from_bootdev(const struct udevice *dev,
			const struct efi_device_path **dpp);

/**
 * efi_read_var() - Read an EFI variable
 *
 * @name: Name of variable to read
 * @guid: GUID for the variable
 * @attrp: Returns variable attributes if non-NULL, on success
 * @buf: Returns allocated buffer containing the value
 * @timep: Returns the timestamp for the variable if non_NULL
 * Return: 0 if OK, -ENOENT if the variable was not found, -EBADF if something
 * went wrong when reading
 */
int efi_read_var(const u16 *name, const efi_guid_t *guid, u32 *attrp,
		 struct abuf *buf, u64 *timep);

uint16_t *efi_dp_str(struct efi_device_path *dp);

/**
 * efi_decode_key() - Convert EFI input key to character
 *
 * Converts an EFI input key structure to a character code, handling
 * both unicode characters and scan codes for special keys like arrow keys
 * and backspace.
 *
 * Unicode characters are returned as-is, with the exception that carriage
 * return ('\r') is converted to newline ('\n') for consistency with U-Boot
 * conventions.
 *
 * @key: Pointer to EFI input key structure
 * Return: Character code (0-255), or 0 if no valid character
 */
int efi_decode_key(struct efi_input_key *key);

/**
 * efi_decode_key_ex() - Convert EFI extended input key to character
 *
 * Converts an EFI extended key data structure to a character code by
 * extracting the basic input key and calling efi_decode_key().
 *
 * This function provides a convenient wrapper for handling EFI Simple Text
 * Input EX Protocol key data, which includes modifier keys (currently ignored)
 *
 * @key_data: Pointer to EFI extended key data structure
 * Return: Character code (0-255), or 0 if no valid character
 */
int efi_decode_key_ex(struct efi_key_data *key_data);

/**
 * efi_startup() - Start up the app
 *
 * This is called from efi_main() to start up an image
 *
 * @image: Image handle for the app
 * @systab: EFI system table to use with the app
 * @is_ulib: True if using U-Boot library
 * Return: Error code
 */
efi_status_t EFIAPI efi_startup(efi_handle_t image, struct efi_system_table *systab,
				bool is_ulib);

/**
 * efi_shutdown() - Shut down the app
 *
 * Call this to shut down the app - freeing memory and exiting back to EFI
 */
void efi_shutdown(void);

#endif /* _LINUX_EFI_H */