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