summaryrefslogtreecommitdiff
path: root/lava_dispatcher/device/fastboot.py
blob: 06c2e68148974de86d97d2c1270cee7245250f33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright (C) 2014 Linaro Limited
#
# Author: Tyler Baker <tyler.baker@linaro.org>
# Author: Antonio Terceiro <antonio.terceiro@linaro.org>
# Derived From: dummy.py
#
# This file is part of LAVA Dispatcher.
#
# LAVA Dispatcher is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LAVA Dispatcher is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along
# with this program; if not, see <http://www.gnu.org/licenses>.

import contextlib
import logging
import subprocess
import pexpect

from lava_dispatcher.device.target import Target
import lava_dispatcher.device.fastboot_drivers as drivers
from lava_dispatcher import deployment_data
from lava_dispatcher.client.base import (
    NetworkCommandRunner,
)
from lava_dispatcher.errors import (
    CriticalError,
    OperationFailed,
)


class FastbootTarget(Target):

    def __init__(self, context, config):
        super(FastbootTarget, self).__init__(context, config)
        self.proc = None
        self._target_type = None
        self._booted = False
        self._reset_boot = False
        self._in_test_shell = False
        self._image_deployment = False
        self._ramdisk_deployment = False
        self._use_boot_cmds = False

        driver = self.config.fastboot_driver
        if driver is None:
            raise CriticalError(
                "Required configuration entry missing: fastboot_driver")

        driver_class = drivers.__getattribute__(driver)

        self.driver = driver_class(self)

    def deploy_linaro_kernel(self, kernel, ramdisk, dtb, modules, rootfs, nfsrootfs,
                             bootloader, firmware, bl1, bl2, bl31, rootfstype,
                             bootloadertype, target_type):
        self._target_type = target_type
        self._use_boot_cmds = True
        if rootfs is not None:
            self._image_deployment = True
        else:
            self._ramdisk_deployment = True
        self.deployment_data = deployment_data.get(self._target_type)
        deploy_attempts = self.config.boot_retries
        attempts = 0
        deployed = False
        while (attempts < deploy_attempts) and (not deployed):
            logging.info("Deploying test image. Attempt: %d", attempts + 1)
            try:
                self.driver.deploy_linaro_kernel(kernel, ramdisk, dtb, modules, rootfs, nfsrootfs,
                                                 bootloader, firmware, bl1, bl2, bl31, rootfstype,
                                                 bootloadertype, self._target_type, self.scratch_dir)
                deployed = True
            except (subprocess.CalledProcessError, pexpect.TIMEOUT) as e:
                msg = "Deployment failed: %s" % e
                logging.error(msg)
                attempts += 1
                continue

        if not deployed:
            logging.error("Infrastructure Error: image deployment failed")
            msg = "Deployment Failed"
            logging.critical(msg)
            raise CriticalError(msg)

    def deploy_android(self, boot, system, userdata, rootfstype,
                       bootloadertype, target_type):
        self._target_type = target_type
        self._image_deployment = True
        self.deployment_data = deployment_data.get(self._target_type)
        deploy_attempts = self.config.boot_retries
        attempts = 0
        deployed = False
        while (attempts < deploy_attempts) and (not deployed):
            logging.info("Deploying test image. Attempt: %d", attempts + 1)
            try:
                self._enter_fastboot()
                self.driver.deploy_android(boot, system, userdata, rootfstype,
                                           bootloadertype, self._target_type,
                                           self.scratch_dir)
                deployed = True
            except (subprocess.CalledProcessError, pexpect.TIMEOUT) as e:
                msg = "Deployment failed: %s" % e
                logging.error(msg)
                attempts += 1
                continue

        if not deployed:
            logging.error("Infrastructure Error: image deployment failed")
            msg = "Deployment Failed"
            logging.critical(msg)
            raise CriticalError(msg)

    def get_device_version(self):
        # this is tricky, because fastboot does not have a visible version
        # number. For now let's use just the adb version number.
        return subprocess.check_output(
            "%s version | sed 's/.* version //'" % self.config.adb_command,
            shell=True
        ).strip()

    def is_booted(self):
        return self._booted

    def reset_boot(self, in_test_shell=False):
        self._reset_boot = True
        self._booted = False

    def power_on(self):
        try:
            if self._booted and self._target_type != 'android':
                self._setup_prompt()
                return self.proc
            self._enter_fastboot()
            if self._use_boot_cmds:
                boot_cmds = ''.join(self._load_boot_cmds(default=self.driver.get_default_boot_cmds()))
                self.driver.boot(boot_cmds)
            else:
                self.driver.boot()
            if self.proc is None:
                self.proc = self.driver.connect()
            self._monitor_boot(self.proc, self.tester_ps1, self.tester_ps1_pattern)
            self._booted = True
            return self.proc
        except subprocess.CalledProcessError:
            logging.info("Infrastructure Error: fastboot exception caught.")
            msg = 'Fastboot boot failed'
            raise OperationFailed(msg)

    def power_off(self, proc):
        super(FastbootTarget, self).power_off(proc)
        if self.config.power_off_cmd:
            self.context.run_command(self.config.power_off_cmd)
        self.driver.finalize(proc)

    @contextlib.contextmanager
    def file_system(self, partition, directory):
        if self._reset_boot:
            self._reset_boot = False
            if self._in_test_shell:
                self._in_test_shell = False
                raise Exception("Operation timed out, resetting platform!")
        if not self._booted:
            if self._target_type == 'android':
                self.context.client.boot_linaro_android_image()
            else:
                self.context.client.boot_linaro_image()
        if self._image_deployment:
            if self._target_type != 'android':
                pat = self.tester_ps1_pattern
                incrc = self.tester_ps1_includes_rc
                runner = NetworkCommandRunner(self, pat, incrc)
                with self._python_file_system(runner, directory, pat) as root:
                    logging.debug("Accessing the file system at %s" % root)
                    yield root
            else:
                with self.driver.adb_file_system(partition, directory) as root:
                    logging.debug("Accessing the file system at %s", root)
                    yield root
        else:
                pat = self.tester_ps1_pattern
                incrc = self.tester_ps1_includes_rc
                runner = NetworkCommandRunner(self, pat, incrc)
                with self._busybox_file_system(runner, directory) as root:
                    logging.debug("Accessing the file system at %s" % root)
                    yield root

    def _enter_fastboot(self):
        if self.proc is not None:
            logging.warning('Device already powered on, powering off first')
            self.power_off(self.proc)
            self.proc = None
        # Device needs to be forced into fastboot mode
        if not self.driver.in_fastboot():
            if self.config.fastboot_driver == 'capri' or \
               self.config.fastboot_driver == 'pxa1928dkb' or \
               self.config.fastboot_driver == 'optimusa80':
                # Connect to serial
                self.proc = self.driver.connect()
                # Hard reset the platform
                if self.config.hard_reset_command:
                    self._hard_reboot(self.proc)
                else:
                    self._soft_reboot(self.proc)
                # Enter u-boot
                self._enter_bootloader(self.proc)
                # Enter fastboot mode
                self.proc.sendline(self.config.start_fastboot_command)
            else:
                # Enter fastboot mode
                self.driver.enter_fastboot()

    def _setup_prompt(self):
        self.proc.sendline("")
        self.proc.sendline("")
        self.proc.sendline('export PS1="%s"' % self.tester_ps1)

target_class = FastbootTarget