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
10import tempfile
11import subprocess
12
13from avocado_qemu import LinuxSSHMixIn
14from avocado_qemu import QemuSystemTest
15from avocado_qemu import wait_for_console_pattern
16from avocado_qemu import exec_command
17from avocado_qemu import exec_command_and_wait_for_pattern
18from avocado_qemu import interrupt_interactive_console_until_pattern
19from avocado_qemu import has_cmd
20from avocado.utils import archive
21from avocado import skipUnless
22from avocado import skipUnless
23
24
25class AST1030Machine(QemuSystemTest):
26    """Boots the zephyr os and checks that the console is operational"""
27
28    timeout = 10
29
30    def test_ast1030_zephyros_1_04(self):
31        """
32        :avocado: tags=arch:arm
33        :avocado: tags=machine:ast1030-evb
34        :avocado: tags=os:zephyr
35        """
36        tar_url = ('https://github.com/AspeedTech-BMC'
37                   '/zephyr/releases/download/v00.01.04/ast1030-evb-demo.zip')
38        tar_hash = '4c6a8ce3a8ba76ef1a65dae419ae3409343c4b20'
39        tar_path = self.fetch_asset(tar_url, asset_hash=tar_hash)
40        archive.extract(tar_path, self.workdir)
41        kernel_file = self.workdir + "/ast1030-evb-demo/zephyr.elf"
42        self.vm.set_console()
43        self.vm.add_args('-kernel', kernel_file,
44                         '-nographic')
45        self.vm.launch()
46        wait_for_console_pattern(self, "Booting Zephyr OS")
47        exec_command_and_wait_for_pattern(self, "help",
48                                          "Available commands")
49
50    def test_ast1030_zephyros_1_07(self):
51        """
52        :avocado: tags=arch:arm
53        :avocado: tags=machine:ast1030-evb
54        :avocado: tags=os:zephyr
55        """
56        tar_url = ('https://github.com/AspeedTech-BMC'
57                   '/zephyr/releases/download/v00.01.07/ast1030-evb-demo.zip')
58        tar_hash = '40ac87eabdcd3b3454ce5aad11fedc72a33ecda2'
59        tar_path = self.fetch_asset(tar_url, asset_hash=tar_hash)
60        archive.extract(tar_path, self.workdir)
61        kernel_file = self.workdir + "/ast1030-evb-demo/zephyr.bin"
62        self.vm.set_console()
63        self.vm.add_args('-kernel', kernel_file,
64                         '-nographic')
65        self.vm.launch()
66        wait_for_console_pattern(self, "Booting Zephyr OS")
67        for shell_cmd in [
68                'kernel stacks',
69                'otp info conf',
70                'otp info scu',
71                'hwinfo devid',
72                'crypto aes256_cbc_vault',
73                'random get',
74                'jtag JTAG1 sw_xfer high TMS',
75                'adc ADC0 resolution 12',
76                'adc ADC0 read 42',
77                'adc ADC1 read 69',
78                'i2c scan I2C_0',
79                'i3c attach I3C_0',
80                'hash test',
81                'kernel uptime',
82                'kernel reboot warm',
83                'kernel uptime',
84                'kernel reboot cold',
85                'kernel uptime',
86        ]: exec_command_and_wait_for_pattern(self, shell_cmd, "uart:~$")
87
88class AST2x00Machine(QemuSystemTest):
89
90    timeout = 180
91
92    def wait_for_console_pattern(self, success_message, vm=None):
93        wait_for_console_pattern(self, success_message,
94                                 failure_message='Kernel panic - not syncing',
95                                 vm=vm)
96
97    def do_test_arm_aspeed(self, image):
98        self.vm.set_console()
99        self.vm.add_args('-drive', 'file=' + image + ',if=mtd,format=raw',
100                         '-net', 'nic')
101        self.vm.launch()
102
103        self.wait_for_console_pattern("U-Boot 2016.07")
104        self.wait_for_console_pattern("## Loading kernel from FIT Image at 20080000")
105        self.wait_for_console_pattern("Starting kernel ...")
106        self.wait_for_console_pattern("Booting Linux on physical CPU 0x0")
107        wait_for_console_pattern(self,
108                "aspeed-smc 1e620000.spi: read control register: 203b0641")
109        self.wait_for_console_pattern("ftgmac100 1e660000.ethernet eth0: irq ")
110        self.wait_for_console_pattern("systemd[1]: Set hostname to")
111
112    def test_arm_ast2400_palmetto_openbmc_v2_9_0(self):
113        """
114        :avocado: tags=arch:arm
115        :avocado: tags=machine:palmetto-bmc
116        """
117
118        image_url = ('https://github.com/openbmc/openbmc/releases/download/2.9.0/'
119                     'obmc-phosphor-image-palmetto.static.mtd')
120        image_hash = ('3e13bbbc28e424865dc42f35ad672b10f2e82cdb11846bb28fa625b48beafd0d')
121        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
122                                      algorithm='sha256')
123
124        self.do_test_arm_aspeed(image_path)
125
126    def test_arm_ast2500_romulus_openbmc_v2_9_0(self):
127        """
128        :avocado: tags=arch:arm
129        :avocado: tags=machine:romulus-bmc
130        """
131
132        image_url = ('https://github.com/openbmc/openbmc/releases/download/2.9.0/'
133                     'obmc-phosphor-image-romulus.static.mtd')
134        image_hash = ('820341076803f1955bc31e647a512c79f9add4f5233d0697678bab4604c7bb25')
135        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
136                                      algorithm='sha256')
137
138        self.do_test_arm_aspeed(image_path)
139
140    def do_test_arm_aspeed_buildroot_start(self, image, cpu_id, pattern='Aspeed EVB'):
141        self.require_netdev('user')
142
143        self.vm.set_console()
144        self.vm.add_args('-drive', 'file=' + image + ',if=mtd,format=raw',
145                         '-net', 'nic', '-net', 'user')
146        self.vm.launch()
147
148        self.wait_for_console_pattern('U-Boot 2019.04')
149        self.wait_for_console_pattern('## Loading kernel from FIT Image')
150        self.wait_for_console_pattern('Starting kernel ...')
151        self.wait_for_console_pattern('Booting Linux on physical CPU ' + cpu_id)
152        self.wait_for_console_pattern('lease of 10.0.2.15')
153        # the line before login:
154        self.wait_for_console_pattern(pattern)
155        time.sleep(0.1)
156        exec_command(self, 'root')
157        time.sleep(0.1)
158        exec_command(self, "passw0rd")
159
160    def do_test_arm_aspeed_buildroot_poweroff(self):
161        exec_command_and_wait_for_pattern(self, 'poweroff',
162                                          'reboot: System halted');
163
164    def test_arm_ast2500_evb_buildroot(self):
165        """
166        :avocado: tags=arch:arm
167        :avocado: tags=machine:ast2500-evb
168        """
169
170        image_url = ('https://github.com/legoater/qemu-aspeed-boot/raw/master/'
171                     'images/ast2500-evb/buildroot-2023.11/flash.img')
172        image_hash = ('c23db6160cf77d0258397eb2051162c8473a56c441417c52a91ba217186e715f')
173        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
174                                      algorithm='sha256')
175
176        self.vm.add_args('-device',
177                         'tmp105,bus=aspeed.i2c.bus.3,address=0x4d,id=tmp-test');
178        self.do_test_arm_aspeed_buildroot_start(image_path, '0x0', 'Aspeed AST2500 EVB')
179
180        exec_command_and_wait_for_pattern(self,
181             'echo lm75 0x4d > /sys/class/i2c-dev/i2c-3/device/new_device',
182             'i2c i2c-3: new_device: Instantiated device lm75 at 0x4d');
183        exec_command_and_wait_for_pattern(self,
184                             'cat /sys/class/hwmon/hwmon1/temp1_input', '0')
185        self.vm.cmd('qom-set', path='/machine/peripheral/tmp-test',
186                    property='temperature', value=18000);
187        exec_command_and_wait_for_pattern(self,
188                             'cat /sys/class/hwmon/hwmon1/temp1_input', '18000')
189
190        self.do_test_arm_aspeed_buildroot_poweroff()
191
192    def test_arm_ast2600_evb_buildroot(self):
193        """
194        :avocado: tags=arch:arm
195        :avocado: tags=machine:ast2600-evb
196        """
197
198        image_url = ('https://github.com/legoater/qemu-aspeed-boot/raw/master/'
199                     'images/ast2600-evb/buildroot-2023.11/flash.img')
200        image_hash = ('b62808daef48b438d0728ee07662290490ecfa65987bb91294cafb1bb7ad1a68')
201        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
202                                      algorithm='sha256')
203
204        self.vm.add_args('-device',
205                         'tmp105,bus=aspeed.i2c.bus.3,address=0x4d,id=tmp-test');
206        self.vm.add_args('-device',
207                         'ds1338,bus=aspeed.i2c.bus.3,address=0x32');
208        self.vm.add_args('-device',
209                         'i2c-echo,bus=aspeed.i2c.bus.3,address=0x42');
210        self.do_test_arm_aspeed_buildroot_start(image_path, '0xf00', 'Aspeed AST2600 EVB')
211
212        exec_command_and_wait_for_pattern(self,
213             'echo lm75 0x4d > /sys/class/i2c-dev/i2c-3/device/new_device',
214             'i2c i2c-3: new_device: Instantiated device lm75 at 0x4d');
215        exec_command_and_wait_for_pattern(self,
216                             'cat /sys/class/hwmon/hwmon1/temp1_input', '0')
217        self.vm.cmd('qom-set', path='/machine/peripheral/tmp-test',
218                    property='temperature', value=18000);
219        exec_command_and_wait_for_pattern(self,
220                             'cat /sys/class/hwmon/hwmon1/temp1_input', '18000')
221
222        exec_command_and_wait_for_pattern(self,
223             'echo ds1307 0x32 > /sys/class/i2c-dev/i2c-3/device/new_device',
224             'i2c i2c-3: new_device: Instantiated device ds1307 at 0x32');
225        year = time.strftime("%Y")
226        exec_command_and_wait_for_pattern(self, 'hwclock -f /dev/rtc1', year);
227
228        exec_command_and_wait_for_pattern(self,
229             'echo slave-24c02 0x1064 > /sys/bus/i2c/devices/i2c-3/new_device',
230             'i2c i2c-3: new_device: Instantiated device slave-24c02 at 0x64');
231        exec_command(self, 'i2cset -y 3 0x42 0x64 0x00 0xaa i');
232        time.sleep(0.1)
233        exec_command_and_wait_for_pattern(self,
234             'hexdump /sys/bus/i2c/devices/3-1064/slave-eeprom',
235             '0000000 ffaa ffff ffff ffff ffff ffff ffff ffff');
236        self.do_test_arm_aspeed_buildroot_poweroff()
237
238    @skipUnless(*has_cmd('swtpm'))
239    def test_arm_ast2600_evb_buildroot_tpm(self):
240        """
241        :avocado: tags=arch:arm
242        :avocado: tags=machine:ast2600-evb
243        """
244
245        image_url = ('https://github.com/legoater/qemu-aspeed-boot/raw/master/'
246                     'images/ast2600-evb/buildroot-2023.02-tpm/flash.img')
247        image_hash = ('a46009ae8a5403a0826d607215e731a8c68d27c14c41e55331706b8f9c7bd997')
248        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
249                                      algorithm='sha256')
250
251        # force creation of VM object, which also defines self._sd
252        vm = self.vm
253
254        socket = os.path.join(self._sd.name, 'swtpm-socket')
255
256        subprocess.run(['swtpm', 'socket', '-d', '--tpm2',
257                        '--tpmstate', f'dir={self.vm.temp_dir}',
258                        '--ctrl', f'type=unixio,path={socket}'])
259
260        self.vm.add_args('-chardev', f'socket,id=chrtpm,path={socket}')
261        self.vm.add_args('-tpmdev', 'emulator,id=tpm0,chardev=chrtpm')
262        self.vm.add_args('-device',
263                         'tpm-tis-i2c,tpmdev=tpm0,bus=aspeed.i2c.bus.12,address=0x2e')
264        self.do_test_arm_aspeed_buildroot_start(image_path, '0xf00', 'Aspeed AST2600 EVB')
265
266        exec_command_and_wait_for_pattern(self,
267            'echo tpm_tis_i2c 0x2e > /sys/bus/i2c/devices/i2c-12/new_device',
268            'tpm_tis_i2c 12-002e: 2.0 TPM (device-id 0x1, rev-id 1)');
269        exec_command_and_wait_for_pattern(self,
270            'cat /sys/class/tpm/tpm0/pcr-sha256/0',
271            'B804724EA13F52A9072BA87FE8FDCC497DFC9DF9AA15B9088694639C431688E0');
272
273        self.do_test_arm_aspeed_buildroot_poweroff()
274
275class AST2x00MachineSDK(QemuSystemTest, LinuxSSHMixIn):
276
277    EXTRA_BOOTARGS = (
278        'quiet '
279        'systemd.mask=org.openbmc.HostIpmi.service '
280        'systemd.mask=xyz.openbmc_project.Chassis.Control.Power@0.service '
281        'systemd.mask=modprobe@fuse.service '
282        'systemd.mask=rngd.service '
283        'systemd.mask=obmc-console@ttyS2.service '
284    )
285
286    # FIXME: Although these tests boot a whole distro they are still
287    # slower than comparable machine models. There may be some
288    # optimisations which bring down the runtime. In the meantime they
289    # have generous timeouts and are disable for CI which aims for all
290    # tests to run in less than 60 seconds.
291    timeout = 240
292
293    def wait_for_console_pattern(self, success_message, vm=None):
294        wait_for_console_pattern(self, success_message,
295                                 failure_message='Kernel panic - not syncing',
296                                 vm=vm)
297
298    def do_test_arm_aspeed_sdk_start(self, image):
299        self.require_netdev('user')
300        self.vm.set_console()
301        self.vm.add_args('-drive', 'file=' + image + ',if=mtd,format=raw',
302                         '-net', 'nic', '-net', 'user,hostfwd=:127.0.0.1:0-:22')
303        self.vm.launch()
304
305        self.wait_for_console_pattern('U-Boot 2019.04')
306        interrupt_interactive_console_until_pattern(
307            self, 'Hit any key to stop autoboot:', 'ast#')
308        exec_command_and_wait_for_pattern(
309            self, 'setenv bootargs ${bootargs} ' + self.EXTRA_BOOTARGS, 'ast#')
310        exec_command_and_wait_for_pattern(
311            self, 'boot', '## Loading kernel from FIT Image')
312        self.wait_for_console_pattern('Starting kernel ...')
313
314    def do_test_aarch64_aspeed_sdk_start(self, image):
315        self.vm.set_console()
316        self.vm.add_args('-drive', 'file=' + image + ',if=mtd,format=raw',
317                         '-net', 'nic', '-net', 'user,hostfwd=:127.0.0.1:0-:22')
318
319        self.vm.launch()
320
321        self.wait_for_console_pattern('U-Boot 2023.10')
322        self.wait_for_console_pattern('## Loading kernel from FIT Image')
323        self.wait_for_console_pattern('Starting kernel ...')
324
325    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
326    def test_arm_ast2500_evb_sdk(self):
327        """
328        :avocado: tags=arch:arm
329        :avocado: tags=machine:ast2500-evb
330        :avocado: tags=flaky
331        """
332
333        image_url = ('https://github.com/AspeedTech-BMC/openbmc/releases/'
334                     'download/v08.06/ast2500-default-obmc.tar.gz')
335        image_hash = ('e1755f3cadff69190438c688d52dd0f0d399b70a1e14b1d3d5540fc4851d38ca')
336        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
337                                      algorithm='sha256')
338        archive.extract(image_path, self.workdir)
339
340        self.do_test_arm_aspeed_sdk_start(
341            self.workdir + '/ast2500-default/image-bmc')
342        self.wait_for_console_pattern('nodistro.0 ast2500-default ttyS4')
343
344    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
345    def test_arm_ast2600_evb_sdk(self):
346        """
347        :avocado: tags=arch:arm
348        :avocado: tags=machine:ast2600-evb
349        :avocado: tags=flaky
350        """
351
352        image_url = ('https://github.com/AspeedTech-BMC/openbmc/releases/'
353                     'download/v08.06/ast2600-a2-obmc.tar.gz')
354        image_hash = ('9083506135f622d5e7351fcf7d4e1c7125cee5ba16141220c0ba88931f3681a4')
355        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
356                                      algorithm='sha256')
357        archive.extract(image_path, self.workdir)
358
359        self.vm.add_args('-device',
360                         'tmp105,bus=aspeed.i2c.bus.5,address=0x4d,id=tmp-test');
361        self.vm.add_args('-device',
362                         'ds1338,bus=aspeed.i2c.bus.5,address=0x32');
363        self.do_test_arm_aspeed_sdk_start(
364            self.workdir + '/ast2600-a2/image-bmc')
365        self.wait_for_console_pattern('nodistro.0 ast2600-a2 ttyS4')
366
367        self.ssh_connect('root', '0penBmc', False)
368        self.ssh_command('dmesg -c > /dev/null')
369
370        self.ssh_command_output_contains(
371             'echo lm75 0x4d > /sys/class/i2c-dev/i2c-5/device/new_device ; '
372             'dmesg -c',
373             'i2c i2c-5: new_device: Instantiated device lm75 at 0x4d');
374        self.ssh_command_output_contains(
375                             'cat /sys/class/hwmon/hwmon19/temp1_input', '0')
376        self.vm.cmd('qom-set', path='/machine/peripheral/tmp-test',
377                    property='temperature', value=18000);
378        self.ssh_command_output_contains(
379                             'cat /sys/class/hwmon/hwmon19/temp1_input', '18000')
380
381        self.ssh_command_output_contains(
382             'echo ds1307 0x32 > /sys/class/i2c-dev/i2c-5/device/new_device ; '
383             'dmesg -c',
384             'i2c i2c-5: new_device: Instantiated device ds1307 at 0x32');
385        year = time.strftime("%Y")
386        self.ssh_command_output_contains('/sbin/hwclock -f /dev/rtc1', year);
387
388    def test_aarch64_ast2700_evb_sdk_v09_02(self):
389        """
390        :avocado: tags=arch:aarch64
391        :avocado: tags=machine:ast2700-evb
392        """
393
394        image_url = ('https://github.com/AspeedTech-BMC/openbmc/releases/'
395                     'download/v09.02/ast2700-default-obmc.tar.gz')
396        image_hash = 'ac969c2602f4e6bdb69562ff466b89ae3fe1d86e1f6797bb7969d787f82116a7'
397        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
398                                      algorithm='sha256')
399        archive.extract(image_path, self.workdir)
400
401        num_cpu = 4
402        image_dir = self.workdir + '/ast2700-default/'
403        uboot_size = os.path.getsize(image_dir + 'u-boot-nodtb.bin')
404        uboot_dtb_load_addr = hex(0x400000000 + uboot_size)
405
406        load_images_list = [
407            {
408                'addr': '0x400000000',
409                'file': image_dir + 'u-boot-nodtb.bin'
410            },
411            {
412                'addr': str(uboot_dtb_load_addr),
413                'file': image_dir + 'u-boot.dtb'
414            },
415            {
416                'addr': '0x430000000',
417                'file': image_dir + 'bl31.bin'
418            },
419            {
420                'addr': '0x430080000',
421                'file': image_dir + 'optee/tee-raw.bin'
422            }
423        ]
424
425        for load_image in load_images_list:
426            addr = load_image['addr']
427            file = load_image['file']
428            self.vm.add_args('-device',
429                             f'loader,force-raw=on,addr={addr},file={file}')
430
431        for i in range(num_cpu):
432            self.vm.add_args('-device',
433                             f'loader,addr=0x430000000,cpu-num={i}')
434
435        self.vm.add_args('-smp', str(num_cpu))
436        self.do_test_aarch64_aspeed_sdk_start(image_dir + 'image-bmc')
437        self.wait_for_console_pattern('nodistro.0 ast2700-default ttyS12')
438        self.ssh_connect('root', '0penBmc', False)
439
440class AST2x00MachineMMC(QemuSystemTest):
441
442    timeout = 240
443
444    def wait_for_console_pattern(self, success_message, vm=None):
445        wait_for_console_pattern(self, success_message,
446                                 failure_message='Kernel panic - not syncing',
447                                 vm=vm)
448
449    def test_arm_aspeed_emmc_boot(self):
450        """
451        :avocado: tags=arch:arm
452        :avocado: tags=machine:rainier-bmc
453        :avocado: tags=device:emmc
454        """
455
456        image_url = ('https://fileserver.linaro.org/s/B6pJTwWEkzSDi36/download/'
457                     'mmc-p10bmc-20240617.qcow2')
458        image_hash = ('d523fb478d2b84d5adc5658d08502bc64b1486955683814f89c6137518acd90b')
459        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
460                                      algorithm='sha256')
461
462        self.require_netdev('user')
463
464        self.vm.set_console()
465        self.vm.add_args('-drive',
466                         'file=' + image_path + ',if=sd,id=sd2,index=2',
467                         '-net', 'nic', '-net', 'user')
468        self.vm.launch()
469
470        self.wait_for_console_pattern('U-Boot SPL 2019.04')
471        self.wait_for_console_pattern('Trying to boot from MMC1')
472        self.wait_for_console_pattern('U-Boot 2019.04')
473        self.wait_for_console_pattern('eMMC 2nd Boot')
474        self.wait_for_console_pattern('## Loading kernel from FIT Image')
475        self.wait_for_console_pattern('Starting kernel ...')
476        self.wait_for_console_pattern('Booting Linux on physical CPU 0xf00')
477        self.wait_for_console_pattern('mmcblk0: p1 p2 p3 p4 p5 p6 p7')
478        self.wait_for_console_pattern('IBM eBMC (OpenBMC for IBM Enterprise')
479