rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # |
| 4 | # Copyright (C) 2015 MediaTek Inc. All rights reserved. |
| 5 | # Tristan Shieh <tristan.shieh@mediatek.com> |
| 6 | # |
| 7 | # Redistribution and use in source and binary forms, with or without |
| 8 | # modification, are permitted provided that the following conditions |
| 9 | # are met: |
| 10 | # 1. Redistributions of source code must retain the above copyright |
| 11 | # notice, this list of conditions and the following disclaimer. |
| 12 | # 2. Redistributions in binary form must reproduce the above copyright |
| 13 | # notice, this list of conditions and the following disclaimer in the |
| 14 | # documentation and/or other materials provided with the distribution. |
| 15 | # |
| 16 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 17 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 20 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 22 | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 23 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | # SUCH DAMAGE. |
| 27 | # |
| 28 | |
| 29 | import os |
| 30 | import struct |
| 31 | import sys |
| 32 | |
| 33 | def read(path): |
| 34 | with open(path, "rb") as f: |
| 35 | return f.read() |
| 36 | |
| 37 | def write(path, data): |
| 38 | with open(path, "wb") as f: |
| 39 | f.write(data) |
| 40 | |
| 41 | def padding(data, size, pattern = '\0'): |
| 42 | return data + pattern * (size - len(data)) |
| 43 | |
| 44 | LK_MEM_ADDRs = {'8135': 0x81e00000, |
| 45 | '8127': 0x81e00000, |
| 46 | '6595': 0x41e00000, |
| 47 | '8172': 0x41e00000, |
| 48 | '8173': 0x41e00000, |
| 49 | '7623': 0x81e00000, |
| 50 | '8163': 0x41e00000} |
| 51 | |
| 52 | BOOTARG_OFFSET = 0x80 |
| 53 | |
| 54 | boot_args = { |
| 55 | '8135': struct.pack("26I", |
| 56 | 0x504c504c, 0x00000063, 0x00000000, 0x11009000, |
| 57 | 0x000e1000, 0x00500a01, 0x00000001, 0x34000000, |
| 58 | 0x10240d40, 0x02101010, 0x000a8200, 0x00000000, |
| 59 | 0x00000000, 0x00000000, 0x00000231, 0x00000000, |
| 60 | 0x00000000, 0x00000000, 0x822041c1, 0x51200655, |
| 61 | 0x92124805, 0x18420000, 0x3a00a284, 0xc0444890, |
| 62 | 0x1980a991, 0x04000099), |
| 63 | |
| 64 | '8127': struct.pack("27I", |
| 65 | 0x504C504C, 0x00000063, 0x00000000, 0x11002000, |
| 66 | 0x000E1000, 0x00000301, 0x00000001, 0x37C00000, |
| 67 | 0x00000000, 0x00000000, 0x00000000, 0x00000003, |
| 68 | 0x00000000, 0x00000000, 0x00002116, 0x00000000, |
| 69 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 70 | 0x00000000, 0x00000000, 0xAFB50204, 0x00000000, |
| 71 | 0x00000000, 0x00000000, 0x00008127), |
| 72 | |
| 73 | '6595': struct.pack("26I", |
| 74 | 0x504c504c, 0x00000063, 0x00000000, 0x11002000, |
| 75 | 0x000e1000, 0xEBFE0101, 0x00000001, 0x80000000, |
| 76 | 0x00000000, 0xE59304C0, 0xE3500000, 0x00000000, |
| 77 | 0x00000000, 0x00000000, 0x00000231, 0x00000000, |
| 78 | 0x00000000, 0x00000000, 0x822041c1, 0x51200655, |
| 79 | 0x92124805, 0x18420000, 0x40079a84, 0xE1A09000, |
| 80 | 0x00000000, 0x00000000), |
| 81 | |
| 82 | '8172': struct.pack("102I", |
| 83 | 0x504C504C, 0x00000063, 0x00000000, 0x11002000, |
| 84 | 0x000E1000, 0x00000301, 0x00000001, 0x76C00000, |
| 85 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 86 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 87 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 88 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 89 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 90 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 91 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 92 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 93 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 94 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 95 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 96 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 97 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 98 | 0xB6C00000, 0x00000000, 0x00000000, 0x00000000, |
| 99 | 0x00000000, 0x00000000, 0x00000000, 0x00000A2B, |
| 100 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 101 | 0x00000000, 0x00000000, 0x00000000, 0x42079A84, |
| 102 | 0x00000000, 0x00000000, 0x00000000, 0x002198C0, |
| 103 | 0x00000000, 0x00000000, 0x00000007, 0x00000005, |
| 104 | 0x0012FB00, 0x00000500, 0x00000000, 0x00000000, |
| 105 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 106 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 107 | 0x00000000, 0x00000000, 0xB6C00000, 0x00000000, |
| 108 | 0x09400000, 0x00000000), |
| 109 | |
| 110 | '8173': struct.pack("44I", |
| 111 | 0x504C504C, 0x00000063, 0x00000000, 0x11002000, |
| 112 | 0x000E1000, 0x00000301, 0x00000001, 0x7F800000, |
| 113 | 0x00000000, 0x00000000, 0x00000000, 0x00000001, |
| 114 | 0x00000000, 0x00000000, 0x000014E7, 0x00000000, |
| 115 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 116 | 0x00000000, 0x00000000, 0x40079A84, 0x00000000, |
| 117 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 118 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 119 | 0x00000000, 0x00000000, 0x001997C0, 0x00000000, |
| 120 | 0x00000007, 0x00000005, 0x0012C000, 0x00004000, |
| 121 | 0xBF800000, 0x00000000, 0x00800000, 0x00000000), |
| 122 | |
| 123 | '7623': struct.pack("27I", |
| 124 | 0x504C504C, 0x00000063, 0x00000000, 0x11004000, |
| 125 | 0x000E1000, 0xFFFC4201, 0x00000001, 0x3FE00000, |
| 126 | 0xDFF4FFBF, 0xFE7FFFFF, 0xDFEFFFFF, 0x00000000, |
| 127 | 0x00000000, 0x00000000, 0x000011FD, 0xFFFDB7FE, |
| 128 | 0xF1FF2FFF, 0xFFFFFFAF, 0xFFF29FFF, 0xBF7BDE7F, |
| 129 | 0x3FFD997F, 0xFFEFFFBF, 0xAFB50204, 0xFECF3FFF, |
| 130 | 0x00000000, 0x00000000, 0x00008590), |
| 131 | |
| 132 | '8163': struct.pack("88I", |
| 133 | 0x504C504C, 0x00000063, 0x00000000, 0x11002000, |
| 134 | 0x000E1000, 0xFFFF0301, 0x00000001, 0x3FC00000, |
| 135 | 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, |
| 136 | 0x00000001, 0x00000000, 0x40000000, 0x00000000, |
| 137 | 0x3FC00000, 0x00000000, 0x00000000, 0x00000000, |
| 138 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 139 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 140 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 141 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 142 | 0x00000000, 0x00000000, 0x00000001, 0xFFFFFFFF, |
| 143 | 0x40000000, 0x00000000, 0x40000000, 0x00000000, |
| 144 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 145 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 146 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 147 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 148 | 0x7FC00000, 0x00000000, 0x00400000, 0x00000000, |
| 149 | 0x00000000, 0x00000000, 0x00000000, 0x0000E4DC, |
| 150 | 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, |
| 151 | 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x42058A04, |
| 152 | 0xFFFFFFFF, 0x00000000, 0x00000000, 0x00178980, |
| 153 | 0x11003000, 0x00000000, 0x00000006, 0x00000004, |
| 154 | 0x0012C000, 0x00004000, 0x2E312E30, 0x25003030)} |
| 155 | |
| 156 | def main(argv): |
| 157 | lk = read(argv[2]) |
| 158 | |
| 159 | print("CHIP_ID: %s" % argv[1]) |
| 160 | boot_arg = boot_args[argv[1]] |
| 161 | LK_MEM_ADDR = LK_MEM_ADDRs[argv[1]] |
| 162 | |
| 163 | lk_wrapper = struct.pack("21I", |
| 164 | #LK_WRAPPER: |
| 165 | 0xe1a0000f, # 0: mov r0, pc |
| 166 | 0xe2400008, # 4: sub r0, r0, #8 |
| 167 | 0xe59f1030, # 8: ldr r1, [pc, #48] ; 40 <COPY+0x1c> |
| 168 | 0xe0800001, # c: add r0, r0, r1 |
| 169 | 0xe59f102c, # 10: ldr r1, [pc, #44] ; 44 <COPY+0x20> |
| 170 | 0xe59f202c, # 14: ldr r2, [pc, #44] ; 48 <COPY+0x24> |
| 171 | 0xe0812002, # 18: add r2, r1, r2 |
| 172 | 0xe59f3028, # 1c: ldr r3, [pc, #40] ; 4c <COPY+0x28> |
| 173 | 0xe0822003, # 20: add r2, r2, r3 |
| 174 | #COPY: |
| 175 | 0xe1510002, # 24: cmp r1, r2 |
| 176 | 0x34903004, # 28: ldrcc r3, [r0], #4 |
| 177 | 0x34813004, # 2c: strcc r3, [r1], #4 |
| 178 | 0x3afffffb, # 30: bcc 24 <COPY> |
| 179 | 0xe59f4008, # 34: ldr r4, [pc, #8] ; 44 <COPY+0x20> |
| 180 | 0xe59f5008, # 38: ldr r5, [pc, #8] ; 48 <COPY+0x24> |
| 181 | 0xe59ff00c, # 3c: ldr pc, [pc, #12] ; 50 <COPY+0x2c> |
| 182 | BOOTARG_OFFSET, # 40: BOOTARG_OFFSET .word 0x11111111 |
| 183 | LK_MEM_ADDR - len(boot_arg), # 44: BOOTARG_ADDR .word 0x22222222 |
| 184 | len(boot_arg), # 48: BOOTARG_SIZE .word 0x33333333 |
| 185 | len(lk), # 4c: LK_SIZE .word 0x44444444 |
| 186 | LK_MEM_ADDR # 50: LK_ADDR .word 0x55555555 |
| 187 | ) |
| 188 | |
| 189 | o = padding(lk_wrapper, BOOTARG_OFFSET, '\0') + boot_arg + lk |
| 190 | # padding to even-sized output |
| 191 | write(argv[3], padding(o, len(o)+len(o)%2, '\0')) |
| 192 | |
| 193 | |
| 194 | if __name__ == "__main__": |
| 195 | main(sys.argv) |
| 196 | |
| 197 | |