summaryrefslogtreecommitdiff
path: root/util/bcmp.sh
blob: 81dede55828c94af5e0aa9e4258473611b321525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
#
# Copyright 2021 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# compare local tree Cr50 source files with a different git branch.
#
# This script is supposed to run in a Cr50 source tree. The script will remove
# and rebuild Cr50 image, then scan the .o and .d files in the build directory
# to figure out all *.[cSh] files used to build the image.
#
# Once the list of files is created, all files' contents are compared with a
# different git branch. By default the other branch is
# cros/firmware-cr50-9308.B, it can be changed as a command line argument
# passed to this script.

tmpf="/tmp/bcmp.$$"
trap '{ rm -f "${tmpf}" ; }' EXIT
default_compare_to="cros/firmware-cr50-stab-14294.B"

find_src_file() {
  local f="$1"
  local log="$2"

  if [[ -f ${f} ]]; then
    echo "${cr50_dir} ${f}" >> "${log}"
    return
  fi

  g="${f/\.c/.S}"
  if [[ "${g}" != "${f}" ]]; then
    # Could be an assembler source file.
    if [[ -f ${g} ]]; then
      echo "${cr50_dir} ${g}" >> "${log}"
      return
    fi
  fi
  g="${f#board/cr50/}"
  if [[ -f ${tpm2_dir}/${g} ]]; then
    echo "${tpm2_dir} ${f}" >> "${log}"
    return
  fi
  case "${f}" in
    (*dcrypto/fips_module* | *linkedtpm2*) ;;
    (*) echo "neither ${f} nor ${g} found in the trees" >&2
  esac
}

branch_exists() {
  local tree="$1"
  local branch="$2"

  git -C "${tree}" show-branch "${branch}" > /dev/null 2>&1
}

if [[ ! -f Makefile.rules || ! -f board/cr50/board.h ]]; then
  echo "this script must run in the root ec directory" >&2
  exit 1
fi

if [[ "$#" != 0 ]]; then
  compare_to="$1"
else
  compare_to="${default_compare_to}"
fi

cr50_dir="$(pwd)"
tpm2_dir="$(readlink -f ../../third_party/tpm2/)"

if ! branch_exists "${tpm2_dir}" "${compare_to}"; then
  echo "Branch ${compare_to} not found in ${tpm2_dir}" >&2
  exit 1
fi

cr50_compare_to="${compare_to}"
if ! branch_exists "${cr50_dir}" "${cr50_compare_to}"; then
  # This could be a new branch with mangled name.
  cr50_compare_to="${cr50_compare_to}-cr50_stab"
  if ! branch_exists "${cr50_dir}" "${cr50_compare_to}"; then
    echo "Branch ${compare_to} not found in ${cr50_dir}" >&2
    exit 1
  fi
fi

echo "Will rebuild CR50"
if ! make BOARD=cr50 -j > /dev/null ; then
  echo "building cr50 failed" >&2
  exit 1
fi

echo "Now checking .c and .S files"
for f in $(find build/cr50/RW/ -name '*.o' |
             sed 's|build/cr50/RW/||;s/\.o/.c/'); do
  find_src_file "${f}" "${tmpf}"
done

echo "Now checking .h files"
for f in $(find build/cr50/RW/ -name '*.d' -exec cat {} + |
             sed 's/ /\n/g' |
             sed 's^/mnt/host/source/src/platform/\(ec\|cr50\)/^^' |
             sed 's^/mnt/host/source/src/third_party/tpm2/^^' |
             sed 's/:$//'|
             sort -u |
             grep '\.h$' ); do
  find_src_file "${f}" "${tmpf}"
done

sort "${tmpf}" | while read -r dir file; do
  if [[ ${dir} == */cr50 ]]; then
    branch="${cr50_compare_to}"
  else
    branch="${compare_to}"
    # TPM2 .o files are placed in board/cr50 in the build directory. Strip the
    # prefix so that the matching .c file can be found in the TPM2 root.
    file="${file#board/cr50/}"
  fi

  if ! git -C "${dir}" diff "${branch}" "${file}" 2>/dev/null; then
    echo "Problems comparing ${file}, does it exist in ${branch} in ${dir}?" >&2
  fi
done