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
15  # Given:
16  #   * Hardware write protect is disabled
17  #       (so we can use bootloader to read and change RDP level)
18  #   * Software write protect is enabled
19  #   * RDP is at level 1
20  #
21  # Then:
22  #   * Reading from flash without changing the RDP level should fail
23  #     (and we should not have read any bytes from flash).
24  #   * The firmware should still be functional because mass erase is NOT
25  #     triggered since we are NOT changing the RDP level.
26  echo "Reading firmware without modifying RDP level"
27  # This should fail and the file should be empty
28  if read_from_flash_in_bootloader_mode_without_modifying_RDP_level \
29    "${file_read_from_flash}"; then
30    echo "Should not be able to read from flash"
31    exit 1
32  fi
33
34  check_file_size_equals_zero "${file_read_from_flash}"
35
36  echo "Checking that firmware is still functional"
37  check_firmware_is_functional
38
39  rm -rf "${file_read_from_flash}"
40}
41
42test_read_from_flash_in_bootloader_mode_while_setting_RDP_to_level_0() {
43  local file_read_from_flash="test.bin"
44  local original_fw_file="$1"
45  local file_expected_byte_size="$(get_file_size ${original_fw_file})"
46
47  # Given:
48  #   * Hardware write protect is disabled
49  #       (so we can use bootloader to read and change RDP level)
50  #   * Software write protect is enabled
51  #   * RDP is at level 1
52  #
53  # Then:
54  #   * Setting the RDP level to 0 (after being at level 1) should trigger
55  #     a mass erase.
56  #   * A mass erase sets all flash bytes to 0xFF, so all bytes read from flash
57  #     should have that value.
58  #   * Since the flash was mass erased, the firmware should no longer function.
59  echo "Reading firmware after setting RDP to level 0"
60  # This command partially fails (and returns an error) because it causes the
61  # flash to be mass erased, but we should still have a file with the contents
62  # that we can compare against.
63  read_from_flash_in_bootloader_mode_while_setting_RDP_to_level_0 \
64    "${file_read_from_flash}" || true
65
66  echo "Checking that value read is made up entirely of OxFF bytes"
67  check_file_contains_all_0xFF_bytes \
68    "${file_read_from_flash}" "${file_expected_byte_size}"
69
70  # Make sure the flash was really erased
71  echo "Checking that firmware is non-functional"
72  check_firmware_is_not_functional
73
74  rm -rf "${file_read_from_flash}"
75}
76
77echo "Running test to validate RDP level 1"
78
79readonly ORIGINAL_FW_FILE="$1"
80
81check_file_exists "${ORIGINAL_FW_FILE}"
82
83echo "Making sure hardware write protect is DISABLED and software write \
84protect is ENABLED"
85check_hw_write_protect_disabled_and_sw_write_protect_enabled
86
87echo "Validating initial state"
88check_has_mp_rw_firmware
89check_has_mp_ro_firmware
90check_running_rw_firmware
91check_is_rollback_set_to_initial_val
92
93echo "Checking that firmware is functional"
94check_firmware_is_functional
95
96test_read_from_flash_in_bootloader_mode_without_modifying_RDP_level
97
98test_read_from_flash_in_bootloader_mode_while_setting_RDP_to_level_0 \
99  "${ORIGINAL_FW_FILE}"
100