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.do_test_arm_aspeed_buildroot_start(image_path, '0xf00')
164
165        exec_command_and_wait_for_pattern(self,
166             'echo lm75 0x4d > /sys/class/i2c-dev/i2c-3/device/new_device',
167             'i2c i2c-3: new_device: Instantiated device lm75 at 0x4d');
168        exec_command_and_wait_for_pattern(self,
169                             'cat /sys/class/hwmon/hwmon0/temp1_input', '0')
170        self.vm.command('qom-set', path='/machine/peripheral/tmp-test',
171                        property='temperature', value=18000);
172        exec_command_and_wait_for_pattern(self,
173                             'cat /sys/class/hwmon/hwmon0/temp1_input', '18000')
174
175        exec_command_and_wait_for_pattern(self,
176             'echo ds1307 0x32 > /sys/class/i2c-dev/i2c-3/device/new_device',
177             'i2c i2c-3: new_device: Instantiated device ds1307 at 0x32');
178        year = time.strftime("%Y")
179        exec_command_and_wait_for_pattern(self, 'hwclock -f /dev/rtc1', year);
180
181        self.do_test_arm_aspeed_buildroot_poweroff()
182
183
184class AST2x00MachineSDK(QemuSystemTest):
185
186    EXTRA_BOOTARGS = (
187        'quiet '
188        'systemd.mask=org.openbmc.HostIpmi.service '
189        'systemd.mask=xyz.openbmc_project.Chassis.Control.Power@0.service '
190        'systemd.mask=modprobe@fuse.service '
191        'systemd.mask=rngd.service '
192        'systemd.mask=obmc-console@ttyS2.service '
193    )
194
195    # FIXME: Although these tests boot a whole distro they are still
196    # slower than comparable machine models. There may be some
197    # optimisations which bring down the runtime. In the meantime they
198    # have generous timeouts and are disable for CI which aims for all
199    # tests to run in less than 60 seconds.
200    timeout = 240
201
202    def wait_for_console_pattern(self, success_message, vm=None):
203        wait_for_console_pattern(self, success_message,
204                                 failure_message='Kernel panic - not syncing',
205                                 vm=vm)
206
207    def do_test_arm_aspeed_sdk_start(self, image):
208        self.require_netdev('user')
209        self.vm.set_console()
210        self.vm.add_args('-drive', 'file=' + image + ',if=mtd,format=raw',
211                         '-net', 'nic', '-net', 'user')
212        self.vm.launch()
213
214        self.wait_for_console_pattern('U-Boot 2019.04')
215        interrupt_interactive_console_until_pattern(
216            self, 'Hit any key to stop autoboot:', 'ast#')
217        exec_command_and_wait_for_pattern(
218            self, 'setenv bootargs ${bootargs} ' + self.EXTRA_BOOTARGS, 'ast#')
219        exec_command_and_wait_for_pattern(
220            self, 'boot', '## Loading kernel from FIT Image')
221        self.wait_for_console_pattern('Starting kernel ...')
222
223    @skipIf(os.getenv('GITLAB_CI'), 'Running on GitLab')
224    def test_arm_ast2500_evb_sdk(self):
225        """
226        :avocado: tags=arch:arm
227        :avocado: tags=machine:ast2500-evb
228        """
229
230        image_url = ('https://github.com/AspeedTech-BMC/openbmc/releases/'
231                     'download/v08.01/ast2500-default-obmc.tar.gz')
232        image_hash = ('5375f82b4c43a79427909342a1e18b4e48bd663e38466862145d27bb358796fd')
233        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
234                                      algorithm='sha256')
235        archive.extract(image_path, self.workdir)
236
237        self.do_test_arm_aspeed_sdk_start(
238            self.workdir + '/ast2500-default/image-bmc')
239        self.wait_for_console_pattern('ast2500-default login:')
240
241    @skipIf(os.getenv('GITLAB_CI'), 'Running on GitLab')
242    def test_arm_ast2600_evb_sdk(self):
243        """
244        :avocado: tags=arch:arm
245        :avocado: tags=machine:ast2600-evb
246        """
247
248        image_url = ('https://github.com/AspeedTech-BMC/openbmc/releases/'
249                     'download/v08.01/ast2600-default-obmc.tar.gz')
250        image_hash = ('f12ef15e8c1f03a214df3b91c814515c5e2b2f56119021398c1dbdd626817d15')
251        image_path = self.fetch_asset(image_url, asset_hash=image_hash,
252                                      algorithm='sha256')
253        archive.extract(image_path, self.workdir)
254
255        self.vm.add_args('-device',
256                         'tmp105,bus=aspeed.i2c.bus.5,address=0x4d,id=tmp-test');
257        self.vm.add_args('-device',
258                         'ds1338,bus=aspeed.i2c.bus.5,address=0x32');
259        self.do_test_arm_aspeed_sdk_start(
260            self.workdir + '/ast2600-default/image-bmc')
261        self.wait_for_console_pattern('ast2600-default login:')
262        exec_command_and_wait_for_pattern(self, 'root', 'Password:')
263        exec_command_and_wait_for_pattern(self, '0penBmc', 'root@ast2600-default:~#')
264
265        exec_command_and_wait_for_pattern(self,
266             'echo lm75 0x4d > /sys/class/i2c-dev/i2c-5/device/new_device',
267             'i2c i2c-5: new_device: Instantiated device lm75 at 0x4d');
268        exec_command_and_wait_for_pattern(self,
269                             'cat /sys/class/hwmon/hwmon19/temp1_input', '0')
270        self.vm.command('qom-set', path='/machine/peripheral/tmp-test',
271                        property='temperature', value=18000);
272        exec_command_and_wait_for_pattern(self,
273                             'cat /sys/class/hwmon/hwmon19/temp1_input', '18000')
274
275        exec_command_and_wait_for_pattern(self,
276             'echo ds1307 0x32 > /sys/class/i2c-dev/i2c-5/device/new_device',
277             'i2c i2c-5: new_device: Instantiated device ds1307 at 0x32');
278        year = time.strftime("%Y")
279        exec_command_and_wait_for_pattern(self, 'hwclock -f /dev/rtc1', year);
280