1 /** @file
2   Common header file.
3 
4 Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
6 
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8 
9 **/
10 
11 #ifndef _CAPSULE_COMMON_HEADER_
12 #define _CAPSULE_COMMON_HEADER_
13 
14 //
15 // 8 extra pages for PF handler.
16 //
17 #define EXTRA_PAGE_TABLE_PAGES      8
18 
19 #define PAGING_1G_ADDRESS_MASK_64   0x000FFFFFC0000000ull
20 
21 //
22 // This capsule PEIM puts its private data at the start of the
23 // coalesced capsule. Here's the structure definition.
24 //
25 #define EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('C', 'a', 'p', 'P')
26 
27 #pragma pack(1)
28 typedef struct {
29   UINT64  Signature;
30   UINT64  CapsuleAllImageSize;
31   UINT64  CapsuleNumber;
32   UINT64  CapsuleOffset[1];
33 } EFI_CAPSULE_PEIM_PRIVATE_DATA;
34 #pragma pack()
35 
36 typedef struct {
37   ///
38   /// The physical start address of the resource region.
39   ///
40   EFI_PHYSICAL_ADDRESS        PhysicalStart;
41   ///
42   /// The number of bytes of the resource region.
43   ///
44   UINT64                      ResourceLength;
45 } MEMORY_RESOURCE_DESCRIPTOR;
46 
47 #define CAPSULE_TEST_SIGNATURE SIGNATURE_32('T', 'E', 'S', 'T')
48 
49 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
50 #pragma pack(1)
51 typedef struct {
52   EFI_PHYSICAL_ADDRESS  EntryPoint;
53   EFI_PHYSICAL_ADDRESS  StackBufferBase;
54   UINT64                StackBufferLength;
55   EFI_PHYSICAL_ADDRESS  JumpBuffer;
56   EFI_PHYSICAL_ADDRESS  BlockListAddr;
57   EFI_PHYSICAL_ADDRESS  MemoryResource;
58   EFI_PHYSICAL_ADDRESS  MemoryBase64Ptr;
59   EFI_PHYSICAL_ADDRESS  MemorySize64Ptr;
60   BOOLEAN               Page1GSupport;
61   UINT64                AddressEncMask;
62 } SWITCH_32_TO_64_CONTEXT;
63 
64 typedef struct {
65   UINT16                ReturnCs;
66   EFI_PHYSICAL_ADDRESS  ReturnEntryPoint;
67   UINT64                ReturnStatus;
68   //
69   // NOTICE:
70   // Be careful about the Base field of IA32_DESCRIPTOR
71   // that is UINTN type.
72   // To extend new field for this structure, add it to
73   // right before this Gdtr field.
74   //
75   IA32_DESCRIPTOR       Gdtr;
76 } SWITCH_64_TO_32_CONTEXT;
77 #pragma pack()
78 #endif
79 
80 /**
81   The function to coalesce a fragmented capsule in memory.
82 
83   @param PeiServices        General purpose services available to every PEIM.
84   @param BlockListBuffer    Point to the buffer of Capsule Descriptor Variables.
85   @param MemoryResource     Pointer to the buffer of memory resource descriptor.
86   @param MemoryBase         Pointer to the base of a block of memory that we can walk
87                             all over while trying to coalesce our buffers.
88                             On output, this variable will hold the base address of
89                             a coalesced capsule.
90   @param MemorySize         Size of the memory region pointed to by MemoryBase.
91                             On output, this variable will contain the size of the
92                             coalesced capsule.
93 
94   @retval EFI_NOT_FOUND     if we can't determine the boot mode
95                             if the boot mode is not flash-update
96                             if we could not find the capsule descriptors
97 
98   @retval EFI_BUFFER_TOO_SMALL
99                             if we could not coalesce the capsule in the memory
100                             region provided to us
101 
102   @retval EFI_SUCCESS       if there's no capsule, or if we processed the
103                             capsule successfully.
104 **/
105 EFI_STATUS
106 EFIAPI
107 CapsuleDataCoalesce (
108   IN EFI_PEI_SERVICES                **PeiServices,
109   IN EFI_PHYSICAL_ADDRESS            *BlockListBuffer,
110   IN MEMORY_RESOURCE_DESCRIPTOR      *MemoryResource,
111   IN OUT VOID                        **MemoryBase,
112   IN OUT UINTN                       *MemorySize
113   );
114 
115 #endif
116