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