tpm-nvsize 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #! /bin/sh -e
  2. # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. #
  6. # Finds the largest NV space that can be defined on the TPM in this state
  7. # (i.e. without removing existing spaces).
  8. #
  9. # The TPM must be unowned, and physical presence must be on.
  10. low=1
  11. high=1500
  12. try=$high
  13. # Binary search with no upper bound
  14. while true; do
  15. ## echo trying $try [ $low $high ]
  16. if /usr/bin/tpmc definespace 0xf004 $(printf "0x%x" $try) 0x1 \
  17. > /dev/null 2>&1; then
  18. # definespace success: end, or $try must grow
  19. if [ $try -eq $low ]; then
  20. echo $low
  21. exit 0
  22. elif [ $try -lt $high ]; then
  23. low=$try
  24. try=$(( ( $high + $low ) / 2 ))
  25. else
  26. # special case: when try == high, expand the search
  27. low=$try
  28. try=$(( $try * 2 ))
  29. high=$try
  30. fi
  31. else
  32. # check for unexpected errors
  33. result=$?
  34. if [ $result -ne 17 ]; then
  35. echo running tpmc definespace 0xf004 0x1 0x1
  36. /usr/bin/tpmc definespace 0xf004 0x1 0x1
  37. echo please correct this condition and try again
  38. exit 1
  39. fi
  40. # definespace failure: end, or $try must shrink
  41. if [ $try -eq $low ]; then
  42. echo 0
  43. exit 0
  44. fi
  45. high=$try
  46. try=$(( ( $high + $low ) / 2 ))
  47. fi
  48. done