1#!/bin/bash
2
3# Copyright 2019 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
7set -e
8
9# shellcheck source=./common.sh
10. "$(dirname "$(readlink -f "${0}")")/common.sh"
11
12test_read_from_flash_in_bootloader_mode_without_modifying_RDP_level() {
13  local file_read_from_flash="test.bin"
14  local original_fw_file="$1"
15
16  # Given:
17  #   * Hardware write protect is disabled
18  #   * Software write protect is disabled
19  #   * RDP is at level 0
20  #
21  # Then:
22  #   * Reading from flash without changing the RDP level should succeed
23  #     (we're already at level 0). Thus we should be able to read the entire
24  #     firmware out of flash and it should exactly match the firmware that we
25  #     flashed for testing.
26  echo "Reading firmware without modifying RDP level"
27  read_from_flash_in_bootloader_mode_without_modifying_RDP_level \
28    "${file_read_from_flash}"
29  if [[ $? -ne 0 ]]; then
30    echo "Failed to read firmware"
31    exit 1
32  fi
33
34  echo "Checking that value read matches the flashed version"
35  check_files_match "${file_read_from_flash}" "${original_fw_file}"
36
37  echo "Checking that firmware is still functional"
38  check_firmware_is_functional
39
40  rm -rf "${file_read_from_flash}"
41}
42
43test_read_from_flash_in_bootloader_mode_while_setting_RDP_to_level_0() {
44  local file_read_from_flash="test.bin"
45  local original_fw_file="$1"
46
47  # Given:
48  #   * Hardware write protect is disabled
49  #   * Software write protect is disabled
50  #   * RDP is at level 0
51  #
52  # Then:
53  #   * Changing the RDP level to 0 should have no effect (we're already at
54  #     level 0). Thus we should be able to read the entire firmware out of
55  #     flash and it should exactly match the firmware that we flashed for
56  #     testing.
57  echo "Reading firmware while setting RDP to level 0"
58  read_from_flash_in_bootloader_mode_while_setting_RDP_to_level_0 \
59    "${file_read_from_flash}"
60  if [[ $? -ne 0 ]]; then
61    echo "Failed to read firmware"
62    exit 1
63  fi
64
65  echo "Checking that value read matches the flashed version"
66  check_files_match "${file_read_from_flash}" "${original_fw_file}"
67
68  echo "Checking that firmware is still functional"
69  check_firmware_is_functional
70
71  rm -rf "${file_read_from_flash}"
72}
73
74echo "Running test to validate that we can read when RDP is set to level 0"
75
76readonly ORIGINAL_FW_FILE="$1"
77
78check_file_exists "${ORIGINAL_FW_FILE}"
79
80echo "Making sure all write protect is disabled"
81check_hw_and_sw_write_protect_disabled
82
83echo "Validating initial state"
84check_has_mp_rw_firmware
85check_has_mp_ro_firmware
86check_running_rw_firmware
87check_rollback_is_unset
88
89echo "Checking that firmware is functional"
90check_firmware_is_functional
91
92test_read_from_flash_in_bootloader_mode_without_modifying_RDP_level \
93  "${ORIGINAL_FW_FILE}"
94
95
96test_read_from_flash_in_bootloader_mode_while_setting_RDP_to_level_0 \
97  "${ORIGINAL_FW_FILE}"
98