run_vbutil_kernel_arg_tests.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/bash -u
  2. #
  3. # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. #
  7. # Quick test of vbutil_kernel args, to make sure we can pack and unpack
  8. # less-than-full-sized components.
  9. #
  10. # Load common constants and variables for tests.
  11. . "$(dirname "$0")/common.sh"
  12. # directories
  13. DEVKEYS="${ROOT_DIR}/tests/devkeys"
  14. DATA_DIR="${SCRIPT_DIR}/preamble_tests/data"
  15. TMPDIR="${TEST_DIR}/vbutil_kernel_arg_tests_dir"
  16. [ -d "${TMPDIR}" ] || mkdir -p "${TMPDIR}"
  17. # Arbitrarily chosen keys and config file.
  18. KEYBLOCK="${DATA_DIR}/kb_0_0.keyblock"
  19. SIGNPRIVATE="${DATA_DIR}/data_0.vbprivk"
  20. SIGNPUBLIC="${DATA_DIR}/root_0.vbpubk"
  21. CONFIG="${DATA_DIR}/dummy_config.txt"
  22. # Create some big and little files for the kernel and bootloader
  23. BIG="${TMPDIR}/big.bin"
  24. dd if=/dev/urandom bs=32768 count=1 of="${BIG}" 2>/dev/null
  25. SMALL="${TMPDIR}/small.bin"
  26. dd if=/dev/urandom bs=16 count=1 of="${SMALL}" 2>/dev/null
  27. declare -a KERN_VALS
  28. declare -a BOOT_VALS
  29. KERN_VALS=("--vmlinuz=${BIG}" "--vmlinuz=${SMALL}")
  30. BOOT_VALS=("--bootloader=${BIG}" "--bootloader=${SMALL}")
  31. tests=0
  32. errs=0
  33. # Pack a bunch of stuff
  34. k=0
  35. while [ "$k" -lt "${#KERN_VALS[*]}" ]; do
  36. b=0
  37. while [ "$b" -lt "${#BOOT_VALS[*]}" ]; do
  38. echo -n "pack kern_${k}_${b}.vblock ... "
  39. : $(( tests++ ))
  40. "${FUTILITY}" vbutil_kernel \
  41. --pack "${TMPDIR}/kern_${k}_${b}.vblock" \
  42. --keyblock "${KEYBLOCK}" \
  43. --signprivate "${SIGNPRIVATE}" \
  44. --version 1 \
  45. --arch arm \
  46. --config "${CONFIG}" \
  47. "${KERN_VALS[$k]}" \
  48. "${BOOT_VALS[$k]}" >/dev/null
  49. if [ "$?" -ne 0 ]; then
  50. echo -e "${COL_RED}FAILED${COL_STOP}"
  51. : $(( errs++ ))
  52. else
  53. echo -e "${COL_GREEN}PASSED${COL_STOP}"
  54. fi
  55. : $(( b++ ))
  56. done
  57. : $(( k++ ))
  58. done
  59. # Now unpack it
  60. for v in ${TMPDIR}/kern_*.vblock; do
  61. : $(( tests++ ))
  62. vv=$(basename "$v")
  63. echo -n "verify $vv ... "
  64. "${FUTILITY}" vbutil_kernel --verify "$v" >/dev/null
  65. if [ "$?" -ne 0 ]; then
  66. echo -e "${COL_RED}FAILED${COL_STOP}"
  67. : $(( errs++ ))
  68. else
  69. echo -e "${COL_GREEN}PASSED${COL_STOP}"
  70. fi
  71. : $(( tests++ ))
  72. echo -n "verify $vv signed ... "
  73. "${FUTILITY}" vbutil_kernel --verify "$v" \
  74. --signpubkey "${SIGNPUBLIC}" >/dev/null
  75. if [ "$?" -ne 0 ]; then
  76. echo -e "${COL_RED}FAILED${COL_STOP}"
  77. : $(( errs++ ))
  78. else
  79. echo -e "${COL_GREEN}PASSED${COL_STOP}"
  80. fi
  81. done
  82. # Test repacking a USB image for the SSD, the way the installer does.
  83. set -e
  84. # Pack for USB
  85. USB_KERN="${TMPDIR}/usb_kern.bin"
  86. USB_KEYBLOCK="${DEVKEYS}/recovery_kernel.keyblock"
  87. USB_SIGNPRIVATE="${DEVKEYS}/recovery_kernel_data_key.vbprivk"
  88. USB_SIGNPUBKEY="${DEVKEYS}/recovery_key.vbpubk"
  89. echo -n "pack USB kernel ... "
  90. : $(( tests++ ))
  91. "${FUTILITY}" vbutil_kernel \
  92. --pack "${USB_KERN}" \
  93. --keyblock "${USB_KEYBLOCK}" \
  94. --signprivate "${USB_SIGNPRIVATE}" \
  95. --version 1 \
  96. --config "${CONFIG}" \
  97. --bootloader "${BIG}" \
  98. --vmlinuz "${BIG}" \
  99. --arch arm
  100. if [ "$?" -ne 0 ]; then
  101. echo -e "${COL_RED}FAILED${COL_STOP}"
  102. : $(( errs++ ))
  103. else
  104. echo -e "${COL_GREEN}PASSED${COL_STOP}"
  105. fi
  106. # And verify it.
  107. echo -n "verify USB kernel ... "
  108. : $(( tests++ ))
  109. "${FUTILITY}" vbutil_kernel \
  110. --verify "${USB_KERN}" \
  111. --signpubkey "${USB_SIGNPUBKEY}" >/dev/null
  112. if [ "$?" -ne 0 ]; then
  113. echo -e "${COL_RED}FAILED${COL_STOP}"
  114. : $(( errs++ ))
  115. else
  116. echo -e "${COL_GREEN}PASSED${COL_STOP}"
  117. fi
  118. # Now we re-sign the same image using the normal keys. This is the kernel
  119. # image that is put on the hard disk by the installer. Note: To save space on
  120. # the USB image, we're only emitting the new verfication block, and the
  121. # installer just replaces that part of the hard disk's kernel partition.
  122. SSD_KERN="${TMPDIR}/ssd_kern.bin"
  123. SSD_KEYBLOCK="${DEVKEYS}/kernel.keyblock"
  124. SSD_SIGNPRIVATE="${DEVKEYS}/kernel_data_key.vbprivk"
  125. SSD_SIGNPUBKEY="${DEVKEYS}/kernel_subkey.vbpubk"
  126. echo -n "repack to SSD kernel ... "
  127. : $(( tests++ ))
  128. "${FUTILITY}" vbutil_kernel \
  129. --repack "${SSD_KERN}" \
  130. --vblockonly \
  131. --keyblock "${SSD_KEYBLOCK}" \
  132. --signprivate "${SSD_SIGNPRIVATE}" \
  133. --oldblob "${TMPDIR}/usb_kern.bin" >/dev/null
  134. if [ "$?" -ne 0 ]; then
  135. echo -e "${COL_RED}FAILED${COL_STOP}"
  136. : $(( errs++ ))
  137. else
  138. echo -e "${COL_GREEN}PASSED${COL_STOP}"
  139. fi
  140. # To verify it, we have to replace the vblock from the original image.
  141. tempfile="${TMPDIR}/foo.bin"
  142. cat "${SSD_KERN}" > "$tempfile"
  143. dd if="${USB_KERN}" bs=65536 skip=1 >> $tempfile 2>/dev/null
  144. echo -n "verify SSD kernel ... "
  145. : $(( tests++ ))
  146. "${FUTILITY}" vbutil_kernel \
  147. --verify "$tempfile" \
  148. --signpubkey "${SSD_SIGNPUBKEY}" >/dev/null
  149. if [ "$?" -ne 0 ]; then
  150. echo -e "${COL_RED}FAILED${COL_STOP}"
  151. : $(( errs++ ))
  152. else
  153. echo -e "${COL_GREEN}PASSED${COL_STOP}"
  154. fi
  155. # Finally make sure that the kernel command line stays good.
  156. orig=$(cat "${CONFIG}" | tr '\012' ' ')
  157. packed=$("${FUTILITY}" dump_kernel_config "${USB_KERN}")
  158. echo -n "check USB kernel config ..."
  159. : $(( tests++ ))
  160. if [ "$orig" != "$packed" ]; then
  161. echo -e "${COL_RED}FAILED${COL_STOP}"
  162. : $(( errs++ ))
  163. else
  164. echo -e "${COL_GREEN}PASSED${COL_STOP}"
  165. fi
  166. repacked=$("${FUTILITY}" dump_kernel_config "${tempfile}")
  167. echo -n "check SSD kernel config ..."
  168. : $(( tests++ ))
  169. if [ "$orig" != "$packed" ]; then
  170. echo -e "${COL_RED}FAILED${COL_STOP}"
  171. : $(( errs++ ))
  172. else
  173. echo -e "${COL_GREEN}PASSED${COL_STOP}"
  174. fi
  175. # Summary
  176. ME=$(basename "$0")
  177. if [ "$errs" -ne 0 ]; then
  178. echo -e "${COL_RED}${ME}: ${errs}/${tests} tests failed${COL_STOP}"
  179. exit 1
  180. fi
  181. happy "${ME}: All ${tests} tests passed"
  182. exit 0