1+ import sys
12from contextlib import contextmanager
23from functools import cached_property
34
1011class UbootConsoleClient (CompositeClient ):
1112 @cached_property
1213 def prompt (self ) -> str :
14+ """
15+ U-Boot prompt to expect
16+ """
17+
1318 return self .call ("get_prompt" )
1419
1520 @contextmanager
16- def reboot_to_console (self ) -> None :
21+ def reboot_to_console (self , * , debug = False ) -> None :
1722 """
1823 Reboot to U-Boot console
1924
2025 Power cycle the target and wait for the U-Boot prompt
26+
27+ Must be used as a context manager, other methods can only be
28+ used within the reboot_to_console context
29+
30+ >>> with uboot.reboot_to_console(debug=True): # doctest: +SKIP
31+ ... uboot.set_env("foo", "bar")
32+ ... uboot.setup_dhcp()
33+ >>> # uboot.set_env("foo", "baz") # invalid use
2134 """
2235
2336 self .logger .info ("Power cycling target..." )
@@ -26,6 +39,9 @@ def reboot_to_console(self) -> None:
2639 self .logger .info ("Waiting for U-Boot prompt..." )
2740
2841 with self .serial .pexpect () as p :
42+ if debug :
43+ p .logfile_read = sys .stdout .buffer
44+
2945 for _ in range (100 ): # TODO: configurable retries
3046 try :
3147 p .send (ESC )
@@ -44,6 +60,10 @@ def reboot_to_console(self) -> None:
4460 delattr (self , "p" )
4561
4662 def run_command (self , cmd : str , timeout : int = 60 , * , _internal_log = True ) -> bytes :
63+ """
64+ Run raw command in the U-Boot console
65+ """
66+
4767 if _internal_log :
4868 self .logger .info (f"Running command: { cmd } " )
4969 if not hasattr (self , "p" ):
@@ -55,6 +75,10 @@ def run_command(self, cmd: str, timeout: int = 60, *, _internal_log=True) -> byt
5575 return self .p .before
5676
5777 def run_command_checked (self , cmd : str , timeout : int = 60 , check = True ) -> list [str ]:
78+ """
79+ Run command in the U-Boot console and check the exit code
80+ """
81+
5882 self .logger .info (f"Running command checked: { cmd } " )
5983 output = self .run_command ("{}; echo $?" .format (cmd ), _internal_log = False )
6084 parsed = output .strip ().decode ().splitlines ()
@@ -73,6 +97,10 @@ def run_command_checked(self, cmd: str, timeout: int = 60, check=True) -> list[s
7397 return parsed [1 :- 1 ]
7498
7599 def setup_dhcp (self , timeout : int = 60 ) -> DhcpInfo :
100+ """
101+ Setup dhcp in U-Boot
102+ """
103+
76104 self .logger .info ("Running DHCP to obtain network configuration..." )
77105
78106 autoload = self .get_env ("autoload" , timeout = timeout )
@@ -116,6 +144,10 @@ def get_env(self, key: str, timeout: int = 5) -> str | None:
116144 raise TimeoutError (f"Timed out getting var { key } " ) from err
117145
118146 def set_env (self , key : str , value : str | None , timeout : int = 5 ) -> None :
147+ """
148+ Set U-Boot environment variable value
149+ """
150+
119151 if value is not None :
120152 cmd = "setenv {} '{}'" .format (key , value )
121153 else :
@@ -127,5 +159,9 @@ def set_env(self, key: str, value: str | None, timeout: int = 5) -> None:
127159 raise TimeoutError (f"Timed out setting var { key } " ) from err
128160
129161 def set_env_dict (self , env : dict [str , str | None ]) -> None :
162+ """
163+ Set multiple U-Boot environment variable value
164+ """
165+
130166 for key , value in env .items ():
131167 self .set_env (key , value )
0 commit comments