blob: 7a6eba63a570da4c3bb86a1270481810f459e409 [file] [log] [blame]
Vishal Bhoj82c80712015-12-15 21:13:33 +05301#!/bin/sh
2###
3# @file
4# Shell script to assemble and dump the fake Int10h handler from NASM source to
5# a C array.
6#
7# Copyright (C) 2014, Red Hat, Inc.
8# Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>
9#
10# This program and the accompanying materials are licensed and made available
11# under the terms and conditions of the BSD License which accompanies this
12# distribution. The full text of the license may be found at
13# http://opensource.org/licenses/bsd-license.php
14#
15# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
16# WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17#
18###
19
20set -e -u
21
22STEM=$(dirname -- "$0")/$(basename -- "$0" .sh)
23
24#
25# Install exit handler -- remove temporary files.
26#
27exit_handler()
28{
29 rm -f -- "$STEM".bin "$STEM".disasm "$STEM".offsets "$STEM".insns \
30 "$STEM".bytes
31}
32trap exit_handler EXIT
33
34#
35# Assemble the source file.
36#
37nasm -o "$STEM".bin -- "$STEM".asm
38
39#
40# Disassemble it, in order to get a binary dump associated with the source.
41# (ndisasm doesn't recognize the "--" end-of-options delimiter.)
42#
43ndisasm "$STEM".bin >"$STEM".disasm
44
45#
46# Create three files, each with one column of the disassembly.
47#
48# The first column contains the offsets, and it starts the comment.
49#
50cut -c 1-8 -- "$STEM".disasm \
51| sed -e 's,^, /* ,' >"$STEM".offsets
52
53#
54# The second column contains the assembly-language instructions, and it closes
55# the comment. We first pad it to 30 characters.
56#
57cut -c 29- -- "$STEM".disasm \
58| sed -e 's,$, ,' \
59 -e 's,^\(.\{30\}\).*$,\1 */,' >"$STEM".insns
60
61#
62# The third column contains the bytes corresponding to the instruction,
63# represented as C integer constants. First strip trailing whitespace from the
64# middle column of the input disassembly, then process pairs of nibbles.
65#
66cut -c 11-28 -- "$STEM".disasm \
67| sed -e 's, \+$,,' -e 's/\(..\)/ 0x\1,/g' >"$STEM".bytes
68
69#
70# Write the output file, recombining the columns. The output should have CRLF
71# line endings.
72#
73{
74 printf '//\n'
75 printf '// THIS FILE WAS GENERATED BY "%s". DO NOT EDIT.\n' \
76 "$(basename -- "$0")"
77 printf '//\n'
78 printf '#ifndef _VBE_SHIM_H_\n'
79 printf '#define _VBE_SHIM_H_\n'
80 printf 'STATIC CONST UINT8 mVbeShim[] = {\n'
81 paste -d ' ' -- "$STEM".offsets "$STEM".insns "$STEM".bytes
82 printf '};\n'
83 printf '#endif\n'
84} \
85| unix2dos >"$STEM".h