d2cba49a1fde3c83de8190845fb54fcc68a6f6e9
[vboot.git] / scripts / image_signing / set_gbb_flags.sh
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
10 SCRIPT_BASE="$(dirname "$0")"
11 . "$SCRIPT_BASE/common_minimal.sh"
12 load_shflags || exit 1
13
14 # DEFINE_string name default_value description flag
15 DEFINE_string file "" "Path to firmware image. Default to system firmware." "f"
16 DEFINE_boolean check_wp $FLAGS_TRUE "Check write protection states first." ""
17
18 # Globals
19 # ----------------------------------------------------------------------------
20 set -e
21
22 # Values from vboot_reference/firmware/include/gbb_header.h
23 GBBFLAGS_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   GBB_FLAG_DISABLE_LID_SHUTDOWN              0x00001000
39   GBB_FLAG_FORCE_DEV_BOOT_FASTBOOT_FULL_CAP  0x00002000
40
41   To get a developer-friendly device, try 0x11 (short_delay + boot_usb).
42   For factory-related tests (always DEV), try 0x39.
43   For early development (disable EC/PD software sync), try 0xa39.
44 "
45
46 FLAGS_HELP="Changes ChromeOS Firmware GBB Flags value.
47
48   Usage: $0 [option_flags] GBB_FLAGS_VALUE
49   $GBBFLAGS_DESCRIPTION"
50
51 FLASHROM_COMMON_OPT="-p host"
52 FLASHROM_READ_OPT="$FLASHROM_COMMON_OPT -i GBB -r"
53 FLASHROM_WRITE_OPT="$FLASHROM_COMMON_OPT -i GBB --fast-verify -w"
54
55 # Check write protection
56 # ----------------------------------------------------------------------------
57 check_write_protection() {
58   local hw_wp="" sw_wp=""
59   if ! crossystem "wpsw_boot?0"; then
60     hw_wp="on"
61   fi
62   local wp_states="$(flashrom --wp-status 2>/dev/null | grep WP)"
63   local wp_disabled="$(echo "$wp_states" | grep "WP:.*is disabled.")"
64   local wp_zero_len="$(echo "$wp_states" | grep "WP:.*, len=0x00000000")"
65   if [ -z "$wp_disabled" -a -z "$wp_zero_len" ]; then
66     sw_wp="on"
67   fi
68   if [ -n "$hw_wp" -a -n "$sw_wp" ]; then
69     return $FLAGS_FALSE
70   fi
71   return $FLAGS_TRUE
72 }
73
74 # Main
75 # ----------------------------------------------------------------------------
76 main() {
77   if [ "$#" != "1" ]; then
78     flags_help
79     exit 1
80   fi
81
82   local value="$(($1))"
83   local image_file="$FLAGS_file"
84
85   if [ -z "$FLAGS_file" ]; then
86     image_file="$(make_temp_file)"
87     flashrom $FLASHROM_READ_OPT "$image_file"
88   fi
89
90   # Process file
91   local old_value="$(gbb_utility -g --flags "$image_file")"
92   printf "Setting GBB flags from %s to 0x%x.." "$old_value" "$value" >&2
93   gbb_utility -s --flags="$value" "$image_file"
94
95   if [ -z "$FLAGS_file" ]; then
96     if [ "$FLAGS_check_wp" = "$FLAGS_TRUE" ]; then
97       if ! check_write_protection; then
98         echo ""
99         echo "WARNING: System GBB Flags are NOT changed!!!"
100         echo "ERROR: You must disable write protection before setting flags."
101         exit 1
102       fi
103     fi
104     flashrom $FLASHROM_WRITE_OPT "$image_file"
105   fi
106 }
107
108 # Parse command line
109 FLAGS "$@" || exit 1
110 ORIGINAL_PARAMS="$@"
111 eval set -- "$FLAGS_ARGV"
112
113 main "$@"