From b368acb7553ec432c94efe3733e6ff7f26ff2daa Mon Sep 17 00:00:00 2001
From: Sergei Trofimov <sergei.trofimov@arm.com>
Date: Mon, 26 Feb 2018 16:21:14 +0000
Subject: [PATCH] plaform/juno: fix ip address from uart

In recent builds, it seems doing "ip addr list eth0" returns "no such
device" when running as a regular user. Doing so as root, will give the
information on the device.
---
 devlib/platform/arm.py | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/devlib/platform/arm.py b/devlib/platform/arm.py
index 199bb2f..17dd323 100644
--- a/devlib/platform/arm.py
+++ b/devlib/platform/arm.py
@@ -100,22 +100,26 @@ class VersatileExpressPlatform(Platform):
                                     baudrate=self.baudrate,
                                     timeout=30,
                                     init_dtr=0) as tty:
-            tty.sendline('')
+            tty.sendline('su')  # this is, apprently, required to query network device
+                                # info by name on recent Juno builds...
             self.logger.debug('Waiting for the Android shell prompt.')
             tty.expect(target.shell_prompt)
 
             self.logger.debug('Waiting for IP address...')
             wait_start_time = time.time()
-            while True:
-                tty.sendline('ip addr list eth0')
-                time.sleep(1)
-                try:
-                    tty.expect(r'inet ([1-9]\d*.\d+.\d+.\d+)', timeout=10)
-                    return tty.match.group(1)
-                except pexpect.TIMEOUT:
-                    pass  # We have our own timeout -- see below.
-                if (time.time() - wait_start_time) > self.ready_timeout:
-                    raise TargetError('Could not acquire IP address.')
+            try:
+                while True:
+                    tty.sendline('ip addr list eth0')
+                    time.sleep(1)
+                    try:
+                        tty.expect(r'inet ([1-9]\d*.\d+.\d+.\d+)', timeout=10)
+                        return tty.match.group(1)
+                    except pexpect.TIMEOUT:
+                        pass  # We have our own timeout -- see below.
+                    if (time.time() - wait_start_time) > self.ready_timeout:
+                        raise TargetError('Could not acquire IP address.')
+            finally:
+                tty.sendline('exit')  # exit shell created by "su" call at the start
 
     def _set_hard_reset_method(self, hard_reset_method):
         if hard_reset_method == 'dtr':