vboot: Add GBB flag to turn on serial output
[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   GBB_FLAG_ENABLE_SERIAL                     0x00004000
41
42   To get a developer-friendly device, try 0x11 (short_delay + boot_usb).
43   For factory-related tests (always DEV), try 0x39.
44   For early development (disable EC/PD software sync), try 0xa39.
45 "
46
47 FLAGS_HELP="Changes ChromeOS Firmware GBB Flags value.
48
49   Usage: $0 [option_flags] GBB_FLAGS_VALUE
50   $GBBFLAGS_DESCRIPTION"
51
52 FLASHROM_COMMON_OPT="-p host"
53 FLASHROM_READ_OPT="$FLASHROM_COMMON_OPT -i GBB -r"
54 FLASHROM_WRITE_OPT="$FLASHROM_COMMON_OPT -i GBB --fast-verify -w"
55
56 # Check write protection
57 # ----------------------------------------------------------------------------
58 check_write_protection() {
59   local hw_wp="" sw_wp=""
60   if ! crossystem "wpsw_boot?0"; then
61     hw_wp="on"
62   fi
63   local wp_states="$(flashrom --wp-status 2>/dev/null | grep WP)"
64   local wp_disabled="$(echo "$wp_states" | grep "WP:.*is disabled.")"
65   local wp_zero_len="$(echo "$wp_states" | grep "WP:.*, len=0x00000000")"
66   if [ -z "$wp_disabled" -a -z "$wp_zero_len" ]; then
67     sw_wp="on"
68   fi
69   if [ -n "$hw_wp" -a -n "$sw_wp" ]; then
70     return $FLAGS_FALSE
71   fi
72   return $FLAGS_TRUE
73 }
74
75 # Main
76 # ----------------------------------------------------------------------------
77 main() {
78   if [ "$#" != "1" ]; then
79     flags_help
80     exit 1
81   fi
82
83   local value="$(($1))"
84   local image_file="$FLAGS_file"
85
86   if [ -z "$FLAGS_file" ]; then
87     image_file="$(make_temp_file)"
88     flashrom $FLASHROM_READ_OPT "$image_file"
89   fi
90
91   # Process file
92   local old_value="$(gbb_utility -g --flags "$image_file")"
93   printf "Setting GBB flags from %s to 0x%x.." "$old_value" "$value" >&2
94   gbb_utility -s --flags="$value" "$image_file"
95
96   if [ -z "$FLAGS_file" ]; then
97     if [ "$FLAGS_check_wp" = "$FLAGS_TRUE" ]; then
98       if ! check_write_protection; then
99         echo ""
100         echo "WARNING: System GBB Flags are NOT changed!!!"
101         echo "ERROR: You must disable write protection before setting flags."
102         exit 1
103       fi
104     fi
105     flashrom $FLASHROM_WRITE_OPT "$image_file"
106   fi
107 }
108
109 # Parse command line
110 FLAGS "$@" || exit 1
111 ORIGINAL_PARAMS="$@"
112 eval set -- "$FLAGS_ARGV"
113
114 main "$@"