]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFspWrapperPkg/FspWrapperSecCore/Vtf0/Tools/FixupForRawSection.py
IntelFspWrapperPkg: Replace BSD License with BSD+Patent License
[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
19486360 6# SPDX-License-Identifier: BSD-2-Clause-Patent\r
a33a2f62
JY
7#\r
8\r
9import sys\r
10\r
11filename = sys.argv[1]\r
12\r
13if filename.lower().find('ia32') >= 0:\r
14 d = open(sys.argv[1], 'rb').read()\r
15 c = ((len(d) + 4 + 7) & ~7) - 4\r
16 if c > len(d):\r
17 c -= len(d)\r
18 f = open(sys.argv[1], 'wb')\r
19 f.write('\x90' * c)\r
20 f.write(d)\r
21 f.close()\r
22else:\r
23 from struct import pack\r
24\r
25 PAGE_PRESENT = 0x01\r
26 PAGE_READ_WRITE = 0x02\r
27 PAGE_USER_SUPERVISOR = 0x04\r
28 PAGE_WRITE_THROUGH = 0x08\r
29 PAGE_CACHE_DISABLE = 0x010\r
30 PAGE_ACCESSED = 0x020\r
31 PAGE_DIRTY = 0x040\r
32 PAGE_PAT = 0x080\r
33 PAGE_GLOBAL = 0x0100\r
34 PAGE_2M_MBO = 0x080\r
35 PAGE_2M_PAT = 0x01000\r
36\r
37 def NopAlign4k(s):\r
38 c = ((len(s) + 0xfff) & ~0xfff) - len(s)\r
39 return ('\x90' * c) + s\r
40\r
41 def PageDirectoryEntries4GbOf2MbPages(baseAddress):\r
42\r
43 s = ''\r
44 for i in range(0x800):\r
45 i = (\r
46 baseAddress + long(i << 21) +\r
47 PAGE_2M_MBO +\r
48 PAGE_CACHE_DISABLE +\r
49 PAGE_ACCESSED +\r
50 PAGE_DIRTY +\r
51 PAGE_READ_WRITE +\r
52 PAGE_PRESENT\r
53 )\r
54 s += pack('Q', i)\r
55 return s\r
56\r
57 def PageDirectoryPointerTable4GbOf2MbPages(pdeBase):\r
58 s = ''\r
59 for i in range(0x200):\r
60 i = (\r
61 pdeBase +\r
62 (min(i, 3) << 12) +\r
63 PAGE_CACHE_DISABLE +\r
64 PAGE_ACCESSED +\r
65 PAGE_READ_WRITE +\r
66 PAGE_PRESENT\r
67 )\r
68 s += pack('Q', i)\r
69 return s\r
70\r
71 def PageMapLevel4Table4GbOf2MbPages(pdptBase):\r
72 s = ''\r
73 for i in range(0x200):\r
74 i = (\r
75 pdptBase +\r
76 (min(i, 0) << 12) +\r
77 PAGE_CACHE_DISABLE +\r
78 PAGE_ACCESSED +\r
79 PAGE_READ_WRITE +\r
80 PAGE_PRESENT\r
81 )\r
82 s += pack('Q', i)\r
83 return s\r
84\r
85 def First4GbPageEntries(topAddress):\r
86 PDE = PageDirectoryEntries4GbOf2MbPages(0L)\r
87 pml4tBase = topAddress - 0x1000\r
88 pdptBase = pml4tBase - 0x1000\r
89 pdeBase = pdptBase - len(PDE)\r
90 PDPT = PageDirectoryPointerTable4GbOf2MbPages(pdeBase)\r
91 PML4T = PageMapLevel4Table4GbOf2MbPages(pdptBase)\r
92 return PDE + PDPT + PML4T\r
93\r
94 def AlignAndAddPageTables():\r
95 d = open(sys.argv[1], 'rb').read()\r
96 code = NopAlign4k(d)\r
97 topAddress = 0x100000000 - len(code)\r
98 d = ('\x90' * 4) + First4GbPageEntries(topAddress) + code\r
99 f = open(sys.argv[1], 'wb')\r
100 f.write(d)\r
101 f.close()\r
102\r
103 AlignAndAddPageTables()\r
104\r