Check in following modules,
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / Ia32 / ImageRead.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 ImageRead.c
15
16 Abstract:
17
18 --*/
19
20 //
21 // Include common header file for this module.
22 //
23 #include "CommonHeader.h"
24
25 #include "DxeIpl.h"
26
27 EFI_STATUS
28 EFIAPI
29 PeiImageRead (
30 IN VOID *FileHandle,
31 IN UINTN FileOffset,
32 IN OUT UINTN *ReadSize,
33 OUT VOID *Buffer
34 )
35 /*++
36
37 Routine Description:
38
39 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
40
41 Arguments:
42
43 FileHandle - The handle to the PE/COFF file
44
45 FileOffset - The offset, in bytes, into the file to read
46
47 ReadSize - The number of bytes to read from the file starting at FileOffset
48
49 Buffer - A pointer to the buffer to read the data into.
50
51 Returns:
52
53 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
54
55 --*/
56 {
57 UINT8 *Destination32;
58 UINT8 *Source32;
59 UINTN Length;
60
61
62 Destination32 = Buffer;
63 Source32 = (UINT8 *) ((UINTN) FileHandle + FileOffset);
64
65 //
66 // This function assumes 32-bit alignment to increase performance
67 //
68 // ASSERT (ALIGN_POINTER (Destination32, sizeof (UINT32)) == Destination32);
69 // ASSERT (ALIGN_POINTER (Source32, sizeof (UINT32)) == Source32);
70
71 Length = *ReadSize;
72 while (Length--) {
73 *(Destination32++) = *(Source32++);
74 }
75
76 return EFI_SUCCESS;
77 }
78
79 EFI_STATUS
80 GetImageReadFunction (
81 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
82 )
83 /*++
84
85 Routine Description:
86 Support routine to return the PE32 Image Reader.
87 If the PeiImageRead() function is less than a page
88 in legnth. If the function is more than a page the DXE IPL will crash!!!!
89
90 Arguments:
91 ImageContext - The context of the image being loaded
92
93 Returns:
94 EFI_SUCCESS - If Image function location is found
95
96 --*/
97 {
98 VOID *MemoryBuffer;
99
100 if (gInMemory) {
101 ImageContext->ImageRead = PeiImageRead;
102 return EFI_SUCCESS;
103 }
104
105 //
106 // BugBug; This code assumes PeiImageRead() is less than a page in size!
107 // Allocate a page so we can shaddow the read function from FLASH into
108 // memory to increase performance.
109 //
110
111 MemoryBuffer = AllocateCopyPool (0x400, (VOID *)(UINTN) PeiImageRead);
112 ASSERT (MemoryBuffer != NULL);
113
114 ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;
115
116 return EFI_SUCCESS;
117 }