1# Functional test that boots the ASPEED SoCs with firmware
2#
3# Copyright (C) 2022 ASPEED Technology Inc
4#
5# This work is licensed under the terms of the GNU GPL, version 2 or
6# later.  See the COPYING file in the top-level directory.
7
8import time
9import os
10
11from avocado_qemu import QemuSystemTest
12from avocado_qemu import wait_for_console_pattern
13from avocado_qemu import exec_command
14from avocado_qemu import exec_command_and_wait_for_pattern
15from avocado_qemu import interrupt_interactive_console_until_pattern
16from avocado.utils import archive
17from avocado import skipIf
18
19
20class AST1030Machine(QemuSystemTest):
21    """Boots the zephyr os and checks that the console is operational"""
22
23    timeout = 10
24
25    def test_ast1030_zephyros(self):
26        """
27        :avocado: tags=arch:arm
28        :avocado: tags=machine:ast1030-evb
29        """
30        tar_url = ('https://github.com/AspeedTech-BMC'
31                   '/zephyr/releases/download/v00.01.04/ast1030-evb-demo.zip')
32        tar_hash = '4c6a8ce3a8ba76ef1a65dae419ae3409343c4b20'
33        tar_path = self.fetch_asset(tar_url, asset_hash=tar_hash)
34        archive.extract(tar_path, self.workdir)
35        kernel_file = self.workdir + "/ast1030-evb-demo/zephyr.elf"
36        self.vm.set_console()
37        self.vm.add_args('-kernel', kernel_file,
38                         '-nographic')
39        self.vm.launch()
40        wait_for_console_pattern(self, "Booting Zephyr OS")
41        exec_command_and_wait_for_pattern(self, "help",
42                                          "Available commands")
43
44class AST2x00Machine(QemuSystemTest):
45
46    timeout = 90
47
48    def wait_for_console_pattern(self, success_message, vm=None):
49        wait_for_console_pattern(self, success_message,
50                                 failure_message='Kernel panic - not syncing',
51                                 vm=vm)
52
53    def do_test_arm_aspeed(self, image):
54        self.vm.set_console()
55        self.vm.add_args('-drive', 'file=' + image + ',if=mtd,format=raw',
56                         '-net', 'nic')
57        self.vm.launch()
58
59        self.wait_for_console_pattern("U-Boot 2016.07")
60        self.wait_for_console_pattern("## Loading kernel from FIT Image at 20080000")
61        self.wait_for_console_pattern("Starting kernel ...")
62        self.wait_for_console_pattern("Booting Linux on physical CPU 0x0")
63        wait_for_console_pattern(self,
64                "aspeed-smc 1e620000.spi: read control register: 203b0641")
65        self.wait_for_console_pattern("ftgmac100 1e660000.ethernet eth0: irq ")
66        self.wait_for_console_pattern("systemd[1]: Set hostname to")
67
68    def test_arm_ast2400_palmetto_openbmc_v2_9_0(self):
69        """
70        :avocado: tags=arch:arm
71        :avocado: tags=machine:palmetto-bmc
72        """
73
74        image_url = ('https://github.com/openbmc/openbmc/releases/download/2.9.0/'
75                     'obmc-phosphor-image-palmetto.static.mtd')
76        image_hash = ('3e13bbbc28e424865dc42f35ad672b10f2e82cdb11846bb28fa625b48beafd0d')
77        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
78                                      algorithm='sha256')
79
80        self.do_test_arm_aspeed(image_path)
81
82    def test_arm_ast2500_romulus_openbmc_v2_9_0(self):
83        """
84        :avocado: tags=arch:arm
85        :avocado: tags=machine:romulus-bmc
86        """
87
88        image_url = ('https://github.com/openbmc/openbmc/releases/download/2.9.0/'
89                     'obmc-phosphor-image-romulus.static.mtd')
90        image_hash = ('820341076803f1955bc31e647a512c79f9add4f5233d0697678bab4604c7bb25')
91        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
92                                      algorithm='sha256')
93
94        self.do_test_arm_aspeed(image_path)
95
96    def do_test_arm_aspeed_buildroot_start(self, image, cpu_id):
97        self.require_netdev('user')
98
99        self.vm.set_console()
100        self.vm.add_args('-drive', 'file=' + image + ',if=mtd,format=raw',
101                         '-net', 'nic', '-net', 'user')
102        self.vm.launch()
103
104        self.wait_for_console_pattern('U-Boot 2019.04')
105        self.wait_for_console_pattern('## Loading kernel from FIT Image')
106        self.wait_for_console_pattern('Starting kernel ...')
107        self.wait_for_console_pattern('Booting Linux on physical CPU ' + cpu_id)
108        self.wait_for_console_pattern('lease of 10.0.2.15')
109        # the line before login:
110        self.wait_for_console_pattern('Aspeed EVB')
111        time.sleep(0.1)
112        exec_command(self, 'root')
113        time.sleep(0.1)
114
115    def do_test_arm_aspeed_buildroot_poweroff(self):
116        exec_command_and_wait_for_pattern(self, 'poweroff',
117                                          'reboot: System halted');
118
119    def test_arm_ast2500_evb_buildroot(self):
120        """
121        :avocado: tags=arch:arm
122        :avocado: tags=machine:ast2500-evb
123        """
124
125        image_url = ('https://github.com/legoater/qemu-aspeed-boot/raw/master/'
126                     'images/ast2500-evb/buildroot-2022.11-2-g15d3648df9/flash.img')
127        image_hash = ('f96d11db521fe7a2787745e9e391225deeeec3318ee0fc07c8b799b8833dd474')
128        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
129                                      algorithm='sha256')
130
131        self.vm.add_args('-device',
132                         'tmp105,bus=aspeed.i2c.bus.3,address=0x4d,id=tmp-test');
133        self.do_test_arm_aspeed_buildroot_start(image_path, '0x0')
134
135        exec_command_and_wait_for_pattern(self,
136             'echo lm75 0x4d > /sys/class/i2c-dev/i2c-3/device/new_device',
137             'i2c i2c-3: new_device: Instantiated device lm75 at 0x4d');
138        exec_command_and_wait_for_pattern(self,
139                             'cat /sys/class/hwmon/hwmon1/temp1_input', '0')
140        self.vm.command('qom-set', path='/machine/peripheral/tmp-test',
141                        property='temperature', value=18000);
142        exec_command_and_wait_for_pattern(self,
143                             'cat /sys/class/hwmon/hwmon1/temp1_input', '18000')
144
145        self.do_test_arm_aspeed_buildroot_poweroff()
146
147    def test_arm_ast2600_evb_buildroot(self):
148        """
149        :avocado: tags=arch:arm
150        :avocado: tags=machine:ast2600-evb
151        """
152
153        image_url = ('https://github.com/legoater/qemu-aspeed-boot/raw/master/'
154                     'images/ast2600-evb/buildroot-2022.11-2-g15d3648df9/flash.img')
155        image_hash = ('e598d86e5ea79671ca8b59212a326c911bc8bea728dec1a1f5390d717a28bb8b')
156        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
157                                      algorithm='sha256')
158
159        self.vm.add_args('-device',
160                         'tmp105,bus=aspeed.i2c.bus.3,address=0x4d,id=tmp-test');
161        self.vm.add_args('-device',
162                         'ds1338,bus=aspeed.i2c.bus.3,address=0x32');
163        self.vm.add_args('-device',
164                         'i2c-echo,bus=aspeed.i2c.bus.3,address=0x42');
165        self.do_test_arm_aspeed_buildroot_start(image_path, '0xf00')
166
167        exec_command_and_wait_for_pattern(self,
168             'echo lm75 0x4d > /sys/class/i2c-dev/i2c-3/device/new_device',
169             'i2c i2c-3: new_device: Instantiated device lm75 at 0x4d');
170        exec_command_and_wait_for_pattern(self,
171                             'cat /sys/class/hwmon/hwmon0/temp1_input', '0')
172        self.vm.command('qom-set', path='/machine/peripheral/tmp-test',
173                        property='temperature', value=18000);
174        exec_command_and_wait_for_pattern(self,
175                             'cat /sys/class/hwmon/hwmon0/temp1_input', '18000')
176
177        exec_command_and_wait_for_pattern(self,
178             'echo ds1307 0x32 > /sys/class/i2c-dev/i2c-3/device/new_device',
179             'i2c i2c-3: new_device: Instantiated device ds1307 at 0x32');
180        year = time.strftime("%Y")
181        exec_command_and_wait_for_pattern(self, 'hwclock -f /dev/rtc1', year);
182
183        exec_command_and_wait_for_pattern(self,
184             'echo slave-24c02 0x1064 > /sys/bus/i2c/devices/i2c-3/new_device',
185             'i2c i2c-3: new_device: Instantiated device slave-24c02 at 0x64');
186        exec_command(self, 'i2cset -y 3 0x42 0x64 0x00 0xaa i');
187        time.sleep(0.1)
188        exec_command_and_wait_for_pattern(self,
189             'hexdump /sys/bus/i2c/devices/3-1064/slave-eeprom',
190             '0000000 ffaa ffff ffff ffff ffff ffff ffff ffff');
191        self.do_test_arm_aspeed_buildroot_poweroff()
192
193
194class AST2x00MachineSDK(QemuSystemTest):
195
196    EXTRA_BOOTARGS = (
197        'quiet '
198        'systemd.mask=org.openbmc.HostIpmi.service '
199        'systemd.mask=xyz.openbmc_project.Chassis.Control.Power@0.service '
200        'systemd.mask=modprobe@fuse.service '
201        'systemd.mask=rngd.service '
202        'systemd.mask=obmc-console@ttyS2.service '
203    )
204
205    # FIXME: Although these tests boot a whole distro they are still
206    # slower than comparable machine models. There may be some
207    # optimisations which bring down the runtime. In the meantime they
208    # have generous timeouts and are disable for CI which aims for all
209    # tests to run in less than 60 seconds.
210    timeout = 240
211
212    def wait_for_console_pattern(self, success_message, vm=None):
213        wait_for_console_pattern(self, success_message,
214                                 failure_message='Kernel panic - not syncing',
215                                 vm=vm)
216
217    def do_test_arm_aspeed_sdk_start(self, image):
218        self.require_netdev('user')
219        self.vm.set_console()
220        self.vm.add_args('-drive', 'file=' + image + ',if=mtd,format=raw',
221                         '-net', 'nic', '-net', 'user')
222        self.vm.launch()
223
224        self.wait_for_console_pattern('U-Boot 2019.04')
225        interrupt_interactive_console_until_pattern(
226            self, 'Hit any key to stop autoboot:', 'ast#')
227        exec_command_and_wait_for_pattern(
228            self, 'setenv bootargs ${bootargs} ' + self.EXTRA_BOOTARGS, 'ast#')
229        exec_command_and_wait_for_pattern(
230            self, 'boot', '## Loading kernel from FIT Image')
231        self.wait_for_console_pattern('Starting kernel ...')
232
233    def do_test_arm_aspeed_sdk_poweroff(self):
234        exec_command_and_wait_for_pattern(self, 'poweroff', 'shutdown-sh#');
235
236
237    @skipIf(os.getenv('GITLAB_CI'), 'Running on GitLab')
238    def test_arm_ast2500_evb_sdk(self):
239        """
240        :avocado: tags=arch:arm
241        :avocado: tags=machine:ast2500-evb
242        """
243
244        image_url = ('https://github.com/AspeedTech-BMC/openbmc/releases/'
245                     'download/v08.03/ast2500-default-obmc.tar.gz')
246        image_hash = ('8ff14c0247ed34b76841c57dc1de67fab6509c98cbdbab42d40d165a410e4105')
247        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
248                                      algorithm='sha256')
249        archive.extract(image_path, self.workdir)
250
251        self.do_test_arm_aspeed_sdk_start(
252            self.workdir + '/ast2500-default/image-bmc')
253        self.wait_for_console_pattern('ast2500-default login:')
254        # TODO: this is broken for some reason
255#        exec_command_and_wait_for_pattern(self, 'root', 'Password:')
256#        exec_command_and_wait_for_pattern(self, '0penBmc', 'root@ast2500-default:~#')
257
258#        self.do_test_arm_aspeed_sdk_poweroff()
259
260    @skipIf(os.getenv('GITLAB_CI'), 'Running on GitLab')
261    def test_arm_ast2600_evb_sdk(self):
262        """
263        :avocado: tags=arch:arm
264        :avocado: tags=machine:ast2600-evb
265        """
266
267        image_url = ('https://github.com/AspeedTech-BMC/openbmc/releases/'
268                     'download/v08.03/ast2600-default-obmc.tar.gz')
269        image_hash = ('d52b2db8591bb1a0261a79474260dd44917f3c4e8914fbe0cd92f1891a5ccaed')
270        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
271                                      algorithm='sha256')
272        archive.extract(image_path, self.workdir)
273
274        self.vm.add_args('-device',
275                         'tmp105,bus=aspeed.i2c.bus.5,address=0x4d,id=tmp-test');
276        self.vm.add_args('-device',
277                         'ds1338,bus=aspeed.i2c.bus.5,address=0x32');
278        self.do_test_arm_aspeed_sdk_start(
279            self.workdir + '/ast2600-default/image-bmc')
280        self.wait_for_console_pattern('ast2600-default login:')
281        exec_command_and_wait_for_pattern(self, 'root', 'Password:')
282        exec_command_and_wait_for_pattern(self, '0penBmc', 'root@ast2600-default:~#')
283
284        exec_command_and_wait_for_pattern(self,
285             'echo lm75 0x4d > /sys/class/i2c-dev/i2c-5/device/new_device',
286             'i2c i2c-5: new_device: Instantiated device lm75 at 0x4d');
287        exec_command_and_wait_for_pattern(self,
288                             'cat /sys/class/hwmon/hwmon19/temp1_input', '0')
289        self.vm.command('qom-set', path='/machine/peripheral/tmp-test',
290                        property='temperature', value=18000);
291        exec_command_and_wait_for_pattern(self,
292                             'cat /sys/class/hwmon/hwmon19/temp1_input', '18000')
293
294        exec_command_and_wait_for_pattern(self,
295             'echo ds1307 0x32 > /sys/class/i2c-dev/i2c-5/device/new_device',
296             'i2c i2c-5: new_device: Instantiated device ds1307 at 0x32');
297        year = time.strftime("%Y")
298        exec_command_and_wait_for_pattern(self, 'hwclock -f /dev/rtc1', year);
299
300        self.do_test_arm_aspeed_sdk_poweroff()
301