VbeShim.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/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. # SPDX-License-Identifier: BSD-2-Clause-Patent
  11. #
  12. ###
  13. set -e -u
  14. STEM=$(dirname -- "$0")/$(basename -- "$0" .sh)
  15. #
  16. # Install exit handler -- remove temporary files.
  17. #
  18. exit_handler()
  19. {
  20. rm -f -- "$STEM".bin "$STEM".disasm "$STEM".offsets "$STEM".insns \
  21. "$STEM".bytes
  22. }
  23. trap exit_handler EXIT
  24. #
  25. # Assemble the source file.
  26. #
  27. nasm -o "$STEM".bin -- "$STEM".asm
  28. #
  29. # Disassemble it, in order to get a binary dump associated with the source.
  30. # (ndisasm doesn't recognize the "--" end-of-options delimiter.)
  31. #
  32. ndisasm "$STEM".bin >"$STEM".disasm
  33. #
  34. # Create three files, each with one column of the disassembly.
  35. #
  36. # The first column contains the offsets, and it starts the comment.
  37. #
  38. cut -c 1-8 -- "$STEM".disasm \
  39. | sed -e 's,^, /* ,' >"$STEM".offsets
  40. #
  41. # The second column contains the assembly-language instructions, and it closes
  42. # the comment. We first pad it to 30 characters.
  43. #
  44. cut -c 29- -- "$STEM".disasm \
  45. | sed -e 's,$, ,' \
  46. -e 's,^\(.\{30\}\).*$,\1 */,' >"$STEM".insns
  47. #
  48. # The third column contains the bytes corresponding to the instruction,
  49. # represented as C integer constants. First strip trailing whitespace from the
  50. # middle column of the input disassembly, then process pairs of nibbles.
  51. #
  52. cut -c 11-28 -- "$STEM".disasm \
  53. | sed -e 's, \+$,,' -e 's/\(..\)/ 0x\1,/g' >"$STEM".bytes
  54. #
  55. # Write the output file, recombining the columns. The output should have CRLF
  56. # line endings.
  57. #
  58. {
  59. printf '//\n'
  60. printf '// THIS FILE WAS GENERATED BY "%s". DO NOT EDIT.\n' \
  61. "$(basename -- "$0")"
  62. printf '//\n'
  63. printf '#ifndef _VBE_SHIM_H_\n'
  64. printf '#define _VBE_SHIM_H_\n'
  65. printf 'STATIC CONST UINT8 mVbeShim[] = {\n'
  66. paste -d ' ' -- "$STEM".offsets "$STEM".insns "$STEM".bytes
  67. printf '};\n'
  68. printf '#endif\n'
  69. } \
  70. | unix2dos >"$STEM".h