From owner-dev-commits-src-all@freebsd.org Wed Dec 30 12:25:00 2020 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id BC63D4C1963; Wed, 30 Dec 2020 12:25:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4D5Vq04xJlz3K8c; Wed, 30 Dec 2020 12:25:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 982CA1E300; Wed, 30 Dec 2020 12:25:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 0BUCP0dN071770; Wed, 30 Dec 2020 12:25:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 0BUCP05K071767; Wed, 30 Dec 2020 12:25:00 GMT (envelope-from git) Date: Wed, 30 Dec 2020 12:25:00 GMT Message-Id: <202012301225.0BUCP05K071767@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Michal Meloun Subject: git: 59f46e34cf64 - main - sys/tools: Add a tool for generating arm and arm64 kernel images. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mmel X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 59f46e34cf640a057e7445960a807004ce4de9ff Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: "Commit messages for all branches of the src repository." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Dec 2020 12:25:00 -0000 The branch main has been updated by mmel: URL: https://cgit.FreeBSD.org/src/commit/?id=59f46e34cf640a057e7445960a807004ce4de9ff commit 59f46e34cf640a057e7445960a807004ce4de9ff Author: Michal Meloun AuthorDate: 2020-12-29 13:49:41 +0000 Commit: Michal Meloun CommitDate: 2020-12-30 12:22:04 +0000 sys/tools: Add a tool for generating arm and arm64 kernel images. This tool can generate kernel images without changing the offsets in the final executable. It replaces the ELF header by properly sized zeroed block then emits a relative jump to _start(for 'v7jump' or 'v8jump' option) or the booti header (for 'v8booti' option) to the beginning of the converted file. Submited by: ian --- sys/tools/arm_kernel_boothdr.awk | 190 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) diff --git a/sys/tools/arm_kernel_boothdr.awk b/sys/tools/arm_kernel_boothdr.awk new file mode 100644 index 000000000000..5526102606f6 --- /dev/null +++ b/sys/tools/arm_kernel_boothdr.awk @@ -0,0 +1,190 @@ +#!/usr/bin/awk -f +#- +# SPDX-License-Identifier: BSD-2-Clause-FreeBSD +# +# Copyright 2019 Ian Lepore +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ + +BEGIN { + # Init global vars. + gBytesOut = 0; # How many output bytes we've written so far + gKernbase = 0; # Address of first byte of loaded kernel image + gStart = 0; # Address of _start symbol + gStartOff = 0; # Offset of _start symbol from start of image + gEnd = 0; # Address of _end symbol + gEndOff = 0; # Offset of _end symbol from start of image + + # The type of header we're writing is set using -v hdrtype= on + # the command line, ensure we got a valid value for it. + if (hdrtype != "v7jump" && + hdrtype != "v8jump" && + hdrtype != "v8booti") { + print "arm_kernel_boothdr.awk: " \ + "missing or invalid '-v hdrtype=' argument" >"/dev/stderr" + gHdrType = "error_reported" + exit 1 + } + + gHdrType = hdrtype +} + +function addr_to_offset(addr) { + # Turn an address into an offset from the start of the loaded image. + return addr % gKernbase +} + +function hexstr_to_num(str) { + + # Prepend a 0x onto the string, then coerce it to a number by doing + # arithmetic with it, which makes awk run it through strtod(), + # which handles hex numbers that have a 0x prefix. + + return 0 + ("0x" str) +} + +function write_le32(num) { + + for (i = 0; i < 4; i++) { + printf("%c", num % 256); + num /= 256 + } + gBytesOut += 4 +} + +function write_le64(num) { + + for (i = 0; i < 8; i++) { + printf("%c", num % 256); + num /= 256 + } + gBytesOut += 8 +} + +function write_padding() { + + # Write enough padding bytes so that the header fills all the + # remaining space before the _start symbol. + + while (gBytesOut++ < gStartOff) { + printf("%c", 0); + } +} + +function write_v7jump() { + + # Write the machine code for "b _start"... + # 0xea is armv7 "branch always" and the low 24 bits is the signed + # offset from the current PC, in words. We know the gStart offset + # is in the first 2mb, so it'll fit in 24 bits. + + write_le32(hexstr_to_num("ea000000") + (gStartOff / 4) - 2) +} + +function write_v8jump() { + + # Write the machine code for "b _start"... + # 0x14 is armv8 "branch always" and the low 26 bits is the signed + # offset from the current PC, in words. We know the gStart offset + # is in the first 2mb, so it'll fit in 26 bits. + + write_le32(hexstr_to_num("14000000") + (gStartOff / 4)) +} + +function write_v8booti() { + + # We are writing this struct... + # + # struct Image_header { + # uint32_t code0; /* Executable code */ + # uint32_t code1; /* Executable code */ + # uint64_t text_offset; /* Image load offset, LE */ + # uint64_t image_size; /* Effective Image size, LE */ + # uint64_t flags; /* Kernel flags, LE */ + # uint64_t res1[3]; /* reserved */ + # uint32_t magic; /* Magic number */ + # uint32_t res2; + # }; + # + # We write 'b _start' into code0. The image size is everything from + # the start of the loaded image to the offset given by the _end symbol. + + write_v8jump() # code0 + write_le32(0) # code1 + write_le64(0) # text_offset + write_le64(gEndOff) # image_size + write_le64(0) # flags + write_le64(0) # res1[0] + write_le64(0) # res1[1] + write_le64(0) # res1[2] + write_le32(hexstr_to_num("644d5241")) # magic (LE "ARMd" (d is 0x64)) + write_le32(0) # res2 +} + +/kernbase/ { + # If the symbol name is exactly "kernbase" save its address. + if ($8 == "kernbase") { + gKernbase = hexstr_to_num($2) + } +} + +/_start/ { + # If the symbol name is exactly "_start" save its address. + if ($8 == "_start") { + gStart = hexstr_to_num($2) + } +} + +/_end/ { + # If the symbol name is exactly "_end" remember its value. + if ($8 == "_end") { + gEnd = hexstr_to_num($2) + } +} + +END { + # Note that this function runs even if BEGIN calls exit(1)! + if (gHdrType == "error_reported") { + exit 1 + } + + # Make sure we got all three required symbols. + if (gKernbase == 0 || gStart == 0 || gEnd == 0) { + print "arm_kernel_boothdr.awk: " \ + "missing kernbase/_start/_end symbol(s)" >"/dev/stderr" + exit 1 + } + + gStartOff = addr_to_offset(gStart) + gEndOff = addr_to_offset(gEnd) + + if (gHdrType == "v7jump") { + write_v7jump() + } else if (gHdrType == "v8jump") { + write_v8jump() + } else if (gHdrType == "v8booti") { + write_v8booti() + } + write_padding() +}