1#!/bin/sh
2#
3# Copyright (c) 2012 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#
7# This script can change GBB flags in system live firmware or a given image
8# file.
9
10SCRIPT_BASE="$(dirname "$0")"
11. "$SCRIPT_BASE/common_minimal.sh"
12load_shflags || exit 1
13
14# DEFINE_string name default_value description flag
15DEFINE_string file "" "Path to firmware image. Default to system firmware." "f"
16DEFINE_boolean check_wp $FLAGS_TRUE "Check write protection states first." ""
17
18# Globals
19# ----------------------------------------------------------------------------
20set -e
21
22# Values from vboot_reference/firmware/include/gbb_header.h
23GBBFLAGS_DESCRIPTION="
24  Defined flags (some values may be not supported by all systems):
25
26  GBB_FLAG_DEV_SCREEN_SHORT_DELAY     0x00000001
27  GBB_FLAG_LOAD_OPTION_ROMS           0x00000002
28  GBB_FLAG_ENABLE_ALTERNATE_OS        0x00000004
29  GBB_FLAG_FORCE_DEV_SWITCH_ON        0x00000008
30  GBB_FLAG_FORCE_DEV_BOOT_USB         0x00000010
31  GBB_FLAG_DISABLE_FW_ROLLBACK_CHECK  0x00000020
32  GBB_FLAG_ENTER_TRIGGERS_TONORM      0x00000040
33  GBB_FLAG_FORCE_DEV_BOOT_LEGACY      0x00000080
34  GBB_FLAG_FAFT_KEY_OVERIDE           0x00000100
35  GBB_FLAG_DISABLE_EC_SOFTWARE_SYNC   0x00000200
36  GBB_FLAG_DEFAULT_DEV_BOOT_LEGACY    0x00000400
37  GBB_FLAG_DISABLE_PD_SOFTWARE_SYNC   0x00000800
38
39  To get a developer-friendly device, try 0x11 (short_delay + boot_usb).
40  For factory-related tests (always DEV), try 0x39.
41"
42
43FLAGS_HELP="Changes ChromeOS Firmware GBB Flags value.
44
45  Usage: $0 [option_flags] GBB_FLAGS_VALUE
46  $GBBFLAGS_DESCRIPTION"
47
48FLASHROM_COMMON_OPT="-p host"
49FLASHROM_READ_OPT="$FLASHROM_COMMON_OPT -i GBB -r"
50FLASHROM_WRITE_OPT="$FLASHROM_COMMON_OPT -i GBB --fast-verify -w"
51
52# Check write protection
53# ----------------------------------------------------------------------------
54check_write_protection() {
55  local hw_wp="" sw_wp=""
56  if ! crossystem "wpsw_boot?0"; then
57    hw_wp="on"
58  fi
59  local wp_states="$(flashrom --wp-status 2>/dev/null | grep WP)"
60  local wp_disabled="$(echo "$wp_states" | grep "WP:.*is disabled.")"
61  local wp_zero_len="$(echo "$wp_states" | grep "WP:.*, len=0x00000000")"
62  if [ -z "$wp_disabled" -a -z "$wp_zero_len" ]; then
63    sw_wp="on"
64  fi
65  if [ -n "$hw_wp" -a -n "$sw_wp" ]; then
66    return $FLAGS_FALSE
67  fi
68  return $FLAGS_TRUE
69}
70
71# Main
72# ----------------------------------------------------------------------------
73main() {
74  if [ "$#" != "1" ]; then
75    flags_help
76    exit 1
77  fi
78
79  local value="$(($1))"
80  local image_file="$FLAGS_file"
81
82  if [ -z "$FLAGS_file" ]; then
83    image_file="$(make_temp_file)"
84    flashrom $FLASHROM_READ_OPT "$image_file"
85  fi
86
87  # Process file
88  local old_value="$(gbb_utility -g --flags "$image_file")"
89  printf "Setting GBB flags from %s to 0x%x.." "$old_value" "$value" >&2
90  gbb_utility -s --flags="$value" "$image_file"
91
92  if [ -z "$FLAGS_file" ]; then
93    if [ "$FLAGS_check_wp" = "$FLAGS_TRUE" ]; then
94      if ! check_write_protection; then
95        echo ""
96        echo "WARNING: System GBB Flags are NOT changed!!!"
97        echo "ERROR: You must disable write protection before setting flags."
98        exit 1
99      fi
100    fi
101    flashrom $FLASHROM_WRITE_OPT "$image_file"
102  fi
103}
104
105# Parse command line
106FLAGS "$@" || exit 1
107ORIGINAL_PARAMS="$@"
108eval set -- "$FLAGS_ARGV"
109
110main "$@"
111