Yegor Timoshenko | c2e4941 | 2018-10-07 01:58:27 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Jonathan Neuschäfer | 55b4645 | 2018-04-19 16:23:54 +0200 | [diff] [blame] | 2 | # This file is part of the coreboot project. |
| 3 | # |
| 4 | # Copyright (C) 2018 Jonathan Neuschäfer |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; version 2 of the License. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | |
| 15 | import sys, os, struct, uuid, zlib, io |
| 16 | |
| 17 | # This script wraps the bootblock in a GPT partition, because that's what |
| 18 | # SiFive's bootrom will load. |
| 19 | |
| 20 | |
| 21 | # Size of a GPT disk block, in bytes |
| 22 | BLOCK_SIZE = 512 |
| 23 | BLOCK_MASK = BLOCK_SIZE - 1 |
| 24 | |
| 25 | # Size of the bootcode part of the MBR |
| 26 | MBR_BOOTCODE_SIZE = 0x1be |
| 27 | |
Philipp Hug | 2326a28 | 2018-07-07 15:54:37 +0200 | [diff] [blame] | 28 | # MBR trampoline to bootblock |
| 29 | MBR_BOOTCODE = bytes([ |
| 30 | # j pc + 0x0800 |
| 31 | 0x6f, 0x00, 0x10, 0x00, |
| 32 | ]) |
| 33 | |
Jonathan Neuschäfer | 55b4645 | 2018-04-19 16:23:54 +0200 | [diff] [blame] | 34 | # A protecive MBR, without the bootcode part |
| 35 | PROTECTIVE_MBR_FOOTER = bytes([ |
| 36 | 0x00, 0x00, 0x02, 0x00, 0xee, 0xff, 0xff, 0xff, |
| 37 | 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, |
| 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 44 | 0x55, 0xaa |
| 45 | ]) |
| 46 | |
| 47 | |
| 48 | # A "protective MBR"[1], which may also contain some boot code. |
| 49 | # [1]: https://en.wikipedia.org/wiki/GUID_Partition_Table#PROTECTIVE-MBR |
| 50 | class ProtectiveMBR: |
| 51 | def __init__(self): |
Philipp Hug | 2326a28 | 2018-07-07 15:54:37 +0200 | [diff] [blame] | 52 | self.bootcode = MBR_BOOTCODE + bytes(MBR_BOOTCODE_SIZE - len(MBR_BOOTCODE)) |
Jonathan Neuschäfer | 55b4645 | 2018-04-19 16:23:54 +0200 | [diff] [blame] | 53 | |
| 54 | def generate(self, stream): |
| 55 | assert len(self.bootcode) == MBR_BOOTCODE_SIZE |
| 56 | mbr = self.bootcode + PROTECTIVE_MBR_FOOTER |
| 57 | assert len(mbr) == BLOCK_SIZE |
| 58 | stream.write(mbr) |
| 59 | |
| 60 | |
| 61 | # Generate a GUID from a string |
| 62 | class GUID(uuid.UUID): |
| 63 | def __init__(self, string): |
| 64 | super().__init__(string) |
| 65 | |
| 66 | def get_bytes(self): |
| 67 | return self.bytes_le |
| 68 | |
| 69 | DUMMY_GUID_DISK_UNIQUE = GUID('17145242-abaa-441d-916a-3f26c970aba2') |
| 70 | DUMMY_GUID_PART_UNIQUE = GUID('7552133d-c8de-4a20-924c-0e85f5ea81f2') |
| 71 | GUID_TYPE_FSBL = GUID('5B193300-FC78-40CD-8002-E86C45580B47') |
| 72 | |
| 73 | |
| 74 | # A GPT disk header |
| 75 | # https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_table_header_(LBA_1) |
| 76 | class GPTHeader: |
| 77 | def __init__(self): |
| 78 | self.current_lba = 1 |
| 79 | self.backup_lba = 1 |
| 80 | self.first_usable_lba = 2 |
| 81 | self.last_usable_lba = 0xff # dummy value |
| 82 | self.uniq = DUMMY_GUID_DISK_UNIQUE |
| 83 | self.part_entries_lba = 2 |
| 84 | self.part_entries_number = 0 |
| 85 | self.part_entries_crc32 = 0 |
| 86 | self.part_entry_size = 128 |
| 87 | |
| 88 | def pack_with_crc(self, crc): |
| 89 | header_size = 92 |
| 90 | header = struct.pack('<8sIIIIQQQQ16sQIII', |
Xiang Wang | 7db270c | 2018-11-21 15:25:33 +0800 | [diff] [blame] | 91 | b'EFI PART', 0x10000, header_size, crc, 0, |
Jonathan Neuschäfer | 55b4645 | 2018-04-19 16:23:54 +0200 | [diff] [blame] | 92 | self.current_lba, self.backup_lba, self.first_usable_lba, |
| 93 | self.last_usable_lba, self.uniq.get_bytes(), |
| 94 | self.part_entries_lba, self.part_entries_number, |
| 95 | self.part_entry_size, self.part_entries_crc32) |
| 96 | assert len(header) == header_size |
| 97 | return header |
| 98 | |
| 99 | def generate(self, stream): |
| 100 | crc = zlib.crc32(self.pack_with_crc(0)) |
| 101 | header = self.pack_with_crc(crc) |
| 102 | stream.write(header.ljust(BLOCK_SIZE, b'\0')) |
| 103 | |
| 104 | |
| 105 | # A GPT partition entry. |
| 106 | # https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_entries_(LBA_2-33) |
| 107 | class GPTPartition: |
| 108 | def __init__(self): |
| 109 | self.type = GUID('00000000-0000-0000-0000-000000000000') |
| 110 | self.uniq = GUID('00000000-0000-0000-0000-000000000000') |
| 111 | self.first_lba = 0 |
| 112 | self.last_lba = 0 |
| 113 | self.attr = 0 |
| 114 | self.name = '' |
| 115 | |
| 116 | def generate(self, stream): |
| 117 | name_utf16 = self.name.encode('UTF-16LE') |
| 118 | part = struct.pack('<16s16sQQQ72s', |
| 119 | self.type.get_bytes(), self.uniq.get_bytes(), |
| 120 | self.first_lba, self.last_lba, self.attr, |
| 121 | name_utf16.ljust(72, b'\0')) |
| 122 | assert len(part) == 128 |
| 123 | stream.write(part) |
| 124 | |
| 125 | |
| 126 | class GPTImage: |
| 127 | # The final image consists of: |
| 128 | # - A protective MBR |
| 129 | # - A GPT header |
| 130 | # - A few GPT partition entries |
| 131 | # - The content of the bootblock |
| 132 | def __init__(self): |
| 133 | self.mbr = ProtectiveMBR() |
| 134 | self.header = GPTHeader() |
| 135 | self.partitions = [ GPTPartition() for i in range(8) ] |
| 136 | self.bootblock = b'' |
| 137 | |
| 138 | |
| 139 | # Fix up a few numbers to ensure consistency between the different |
| 140 | # components. |
| 141 | def fixup(self): |
| 142 | # Align the bootblock to a whole number to LBA blocks |
| 143 | bootblock_size = (len(self.bootblock) + BLOCK_SIZE - 1) & ~BLOCK_MASK |
| 144 | self.bootblock = self.bootblock.ljust(bootblock_size) |
| 145 | |
| 146 | # Propagate the number of partition entries |
| 147 | self.header.part_entries_number = len(self.partitions) |
| 148 | self.header.first_usable_lba = 2 + self.header.part_entries_number // 4 |
| 149 | |
| 150 | # Create a partition entry for the bootblock |
| 151 | self.partitions[0].type = GUID_TYPE_FSBL |
| 152 | self.partitions[0].uniq = DUMMY_GUID_PART_UNIQUE |
| 153 | self.partitions[0].first_lba = self.header.first_usable_lba |
| 154 | self.partitions[0].last_lba = \ |
| 155 | self.header.first_usable_lba + bootblock_size // BLOCK_SIZE |
| 156 | |
| 157 | # Calculate the CRC32 checksum of the partitions array |
| 158 | partition_array = io.BytesIO() |
| 159 | for part in self.partitions: |
| 160 | part.generate(partition_array) |
| 161 | self.header.part_entries_crc32 = zlib.crc32(partition_array.getvalue()) |
| 162 | |
| 163 | |
| 164 | def generate(self, stream): |
| 165 | self.mbr.generate(stream) |
| 166 | self.header.generate(stream) |
| 167 | for part in self.partitions: |
| 168 | part.generate(stream) |
| 169 | stream.write(self.bootblock) |
| 170 | |
| 171 | |
| 172 | if __name__ == '__main__': |
| 173 | if len(sys.argv) != 3: |
| 174 | print('Usage:', file=sys.stderr) |
| 175 | print(' %s bootblock.raw.bin bootblock.bin' % sys.argv[0], |
| 176 | file=sys.stderr) |
| 177 | sys.exit(1) |
| 178 | |
| 179 | image = GPTImage() |
| 180 | |
| 181 | with open(sys.argv[1], 'rb') as f: |
| 182 | image.bootblock = f.read() |
| 183 | |
| 184 | image.fixup() |
| 185 | |
Philipp Hug | 2326a28 | 2018-07-07 15:54:37 +0200 | [diff] [blame] | 186 | # Verify if first partition is at expected lba, otherwise trampoline will |
| 187 | # fail |
| 188 | if image.partitions[0].first_lba != 4: |
| 189 | print('Warning: First partition not at expected location (LBA 4)') |
| 190 | sys.exit(1) |
| 191 | |
Jonathan Neuschäfer | 55b4645 | 2018-04-19 16:23:54 +0200 | [diff] [blame] | 192 | with open(sys.argv[2], 'wb') as f: |
| 193 | image.generate(f) |