Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: reboot_mode.py

Issue 2656004: add c++ version of the firmware utilities (Closed) Base URL: ssh://gitrw.chromium.org/firmware-utils.git
Patch Set: removed python scripts and add comments for range names in gpio Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « reboot_mode.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2
3 # Copyright (c) 2010 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 """A script to manipulate ChromeOS CMOS reboot Field.
8
9 A few bits in a byte in CMOS (called RDB, short for 'Reboot Data Byte'
10 hereafter) are dedicated to information exchange between Linux and BIOS.
11
12 The CMOS contents are available through /dev/nvrom. The location of RDB is
13 reported by BIOS through ACPI. The few bits in RDB operated on by this script
14 are defined in the all_mode_fields dictionary below along with their
15 functions.
16
17 When invoked without parameters this script prints values of the fields. When
18 invoked with any of the bit field names as parameter(s), the script sets the
19 bit(s) as required.
20
21 Additional parameters allow to specify alternative ACPI and NVRAM files for
22 testing.
23 """
24
25 __author__ = 'The Chromium OS Authors'
26
27 import optparse
28 import sys
29
30 class RebootModeError(Exception):
31 pass
32
33 # global variables
34 parser = optparse.OptionParser()
35 acpi_file = '/sys/devices/platform/chromeos_acpi/CHNV'
36 nvram_file = '/dev/nvram'
37
38 # Offset of RDB in NVRAM
39 rdb_offset = 0
40
41 # NVRAM contents read on startup
42 rdb_value = ''
43
44 # All bitfields in RDB this script provides access to. Field names prepended
45 # by `--' become this script's command line options
46 all_mode_fields = {
47 'recovery' : 0x80,
48 'debug_reset' : 0x40,
49 'try_firmware_b': 0x20
50 }
51
52 # A dictionary of fields to be updated as requested by the command line
53 # parameters
54 fields_to_set = {}
55
56
57 def OptionHandler(option, opt_str, value, p):
58 """Process bit field name command line parameter.
59
60 Verifies that the value is in range (0 or 1) and adds the appropriate
61 element to fields_to_set dictionary. Should the same field specified in the
62 command line more than once, only the last value will be used.
63
64 Raises:
65 RebootModeError in case the parameter value is out of range.
66 """
67
68 global fields_to_set
69 key = opt_str[2:]
70 if value < 0 or value > 1:
71 raise RebootModeError('--%s should be either 0 or 1' % key)
72
73 fields_to_set[key] = value
74
75
76 def PrepareOptions():
77 global parser
78 for field in all_mode_fields:
79 parser.add_option('--' + field, action='callback', type='int',
80 callback=OptionHandler)
81 parser.add_option('--acpi_file', dest='acpi_file')
82 parser.add_option('--nvram_file', dest='nvram_file')
83
84
85 def ParseOptions():
86 global acpi_file
87 global nvram_file
88 (opts, args) = parser.parse_args()
89
90 if opts.acpi_file is not None:
91 acpi_file = opts.acpi_file
92
93 if opts.nvram_file is not None:
94 nvram_file = opts.nvram_file
95
96
97 def GetRDBOffset():
98 global rdb_offset
99 try:
100 f = open(acpi_file, 'r')
101 rdb_offset = int(f.read())
102 f.close()
103 except IOError, e:
104 raise RebootModeError('Trying to read %s got this: %s' % (acpi_file, e))
105 except ValueError:
106 raise RebootModeError('%s contents are corrupted' % acpi_file)
107
108
109 def ReadNvram():
110 global rdb_value
111 try:
112 f = open(nvram_file, 'rb')
113 f.seek(rdb_offset)
114 rdb_value = ord(f.read(1)[0])
115 f.close()
116 except IOError, e:
117 if e[0] == 2:
118 raise RebootModeError(
119 '%s does not exist. Is nvram module installed?' % nvram_file)
120 if e[0] == 13:
121 raise RebootModeError('Access to %s denied. Are you root?' % nvram_file)
122 raise RebootModeError(e[1])
123 except IndexError, e:
124 raise RebootModeError('Failed reading mode byte from %s' % nvram_file)
125
126
127 def PrintCurrentMode():
128 print 'Current reboot mode settings:'
129 for (field, mask) in all_mode_fields.iteritems():
130 print '%-15s: %d' % (field, int(not not (rdb_value & mask)))
131
132
133 def SetNewMode():
134 new_mode = 0
135 updated_bits_mask = 0
136
137 for (opt, value) in fields_to_set.iteritems():
138 mask = all_mode_fields[opt]
139 if value:
140 new_mode |= mask
141 updated_bits_mask |= mask
142
143 if (rdb_value & updated_bits_mask) == new_mode:
144 print 'No update required'
145 return
146
147 f = open(nvram_file, 'rb+')
148 f.seek(rdb_offset)
149 new_mode |= (rdb_value & ~updated_bits_mask)
150 f.write('%c' % new_mode)
151 f.close()
152
153
154 def DoRebootMode():
155 PrepareOptions()
156 ParseOptions()
157 GetRDBOffset()
158 ReadNvram()
159 if fields_to_set:
160 SetNewMode()
161 else:
162 PrintCurrentMode()
163
164
165 if __name__ == '__main__':
166 try:
167 DoRebootMode()
168 except RebootModeError, e:
169 print >> sys.stderr, '%s: %s' % (sys.argv[0].split('/')[-1], e)
170 sys.exit(1)
171 sys.exit(0)
OLDNEW
« no previous file with comments | « reboot_mode.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698