]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFspWrapperPkg/FspWrapperSecCore/Vtf0/Tools/FixupForRawSection.py
Add IntelFspWrapper to support boot EDKII on FSP bin.
[mirror_edk2.git] / IntelFspWrapperPkg / FspWrapperSecCore / Vtf0 / Tools / FixupForRawSection.py
CommitLineData
a33a2f62
JY
1## @file\r
2# Apply fixup to VTF binary image for FFS Raw section\r
3#\r
4# Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
5#\r
6# This program and the accompanying materials\r
7# are licensed and made available under the terms and conditions of the BSD License\r
8# which accompanies this distribution. The full text of the license may be found at\r
9# http://opensource.org/licenses/bsd-license.php\r
10#\r
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13#\r
14\r
15import sys\r
16\r
17filename = sys.argv[1]\r
18\r
19if filename.lower().find('ia32') >= 0:\r
20 d = open(sys.argv[1], 'rb').read()\r
21 c = ((len(d) + 4 + 7) & ~7) - 4\r
22 if c > len(d):\r
23 c -= len(d)\r
24 f = open(sys.argv[1], 'wb')\r
25 f.write('\x90' * c)\r
26 f.write(d)\r
27 f.close()\r
28else:\r
29 from struct import pack\r
30\r
31 PAGE_PRESENT = 0x01\r
32 PAGE_READ_WRITE = 0x02\r
33 PAGE_USER_SUPERVISOR = 0x04\r
34 PAGE_WRITE_THROUGH = 0x08\r
35 PAGE_CACHE_DISABLE = 0x010\r
36 PAGE_ACCESSED = 0x020\r
37 PAGE_DIRTY = 0x040\r
38 PAGE_PAT = 0x080\r
39 PAGE_GLOBAL = 0x0100\r
40 PAGE_2M_MBO = 0x080\r
41 PAGE_2M_PAT = 0x01000\r
42\r
43 def NopAlign4k(s):\r
44 c = ((len(s) + 0xfff) & ~0xfff) - len(s)\r
45 return ('\x90' * c) + s\r
46\r
47 def PageDirectoryEntries4GbOf2MbPages(baseAddress):\r
48\r
49 s = ''\r
50 for i in range(0x800):\r
51 i = (\r
52 baseAddress + long(i << 21) +\r
53 PAGE_2M_MBO +\r
54 PAGE_CACHE_DISABLE +\r
55 PAGE_ACCESSED +\r
56 PAGE_DIRTY +\r
57 PAGE_READ_WRITE +\r
58 PAGE_PRESENT\r
59 )\r
60 s += pack('Q', i)\r
61 return s\r
62\r
63 def PageDirectoryPointerTable4GbOf2MbPages(pdeBase):\r
64 s = ''\r
65 for i in range(0x200):\r
66 i = (\r
67 pdeBase +\r
68 (min(i, 3) << 12) +\r
69 PAGE_CACHE_DISABLE +\r
70 PAGE_ACCESSED +\r
71 PAGE_READ_WRITE +\r
72 PAGE_PRESENT\r
73 )\r
74 s += pack('Q', i)\r
75 return s\r
76\r
77 def PageMapLevel4Table4GbOf2MbPages(pdptBase):\r
78 s = ''\r
79 for i in range(0x200):\r
80 i = (\r
81 pdptBase +\r
82 (min(i, 0) << 12) +\r
83 PAGE_CACHE_DISABLE +\r
84 PAGE_ACCESSED +\r
85 PAGE_READ_WRITE +\r
86 PAGE_PRESENT\r
87 )\r
88 s += pack('Q', i)\r
89 return s\r
90\r
91 def First4GbPageEntries(topAddress):\r
92 PDE = PageDirectoryEntries4GbOf2MbPages(0L)\r
93 pml4tBase = topAddress - 0x1000\r
94 pdptBase = pml4tBase - 0x1000\r
95 pdeBase = pdptBase - len(PDE)\r
96 PDPT = PageDirectoryPointerTable4GbOf2MbPages(pdeBase)\r
97 PML4T = PageMapLevel4Table4GbOf2MbPages(pdptBase)\r
98 return PDE + PDPT + PML4T\r
99\r
100 def AlignAndAddPageTables():\r
101 d = open(sys.argv[1], 'rb').read()\r
102 code = NopAlign4k(d)\r
103 topAddress = 0x100000000 - len(code)\r
104 d = ('\x90' * 4) + First4GbPageEntries(topAddress) + code\r
105 f = open(sys.argv[1], 'wb')\r
106 f.write(d)\r
107 f.close()\r
108\r
109 AlignAndAddPageTables()\r
110\r