vitali64.duckdns.org:
blog fases cgit pastebin laboratory-fe moinmoinwiki
« Go back to the main page

KII Pro Adventures

A wild KII Pro has appeared! 08/02/2023 (mod: 12/03/2023)


This article will be constantly updated.

The VideoStrong KII Pro is an inexpensive chinese TV Box that uses the Amlogic S905 SoC. It runs a version of Android 5.1.1 modified to include chinese spyware, a custom launcher dashboard, and Kodi 16.

This TV Box's SoC is pretty well supported by many GNU+Linux distributions, and Amlogic themselves made a kernel that supported their SoC.

The KII Pro isn't the only TV Box using the Amlogic S905 series of SoCs. In fact, there are many other TV Boxes using a similar SoC. As such, many people succeeded at running U-Boot and a Linux kernel on some of them.

While the KII Pro has an eMMC that contains the vendor firmware, it also has an SD card slot.

The TV Box has 2 boot modes, apparently. One where it boots from the eMMC and another one where it can boot from an SD card.

This article is about porting postmarketOS to it and getting it to a state where we can use it as a TV Box with mostly libre software.

Let's get started!

This TV Box has a DTS file in mainline.

The KII Pro is already supported by a number of GNU+Linux distributions. Here are some I know of:

Seems like Armbian sets an X11 config. Maybe it's needed in postmarketOS too?

https://github.com/armbian/build/blob/master/config/sources/families/include/meson64_common.inc
[...]
    case "${BOARD}" in
        "odroidc2" | "nanopik2-s905" | "lepotato" | "lafrite" | "khadas-vim1" | "khadas-vim2" | "aml-s9xx-box" )
            cat <<- EOF > "$destination"/etc/X11/xorg.conf.d/02-driver.conf
                Section "OutputClass"
                    Identifier "Amlogic"
                    MatchDriver "meson"
                    Driver "modesetting"
                    Option "PrimaryGPU" "true"
                EndSection
                Section "Screen"
                    Identifier      "Default Screen"
                    Device          "Meson"
                    Monitor         "foo"
                    DefaultDepth  24
                    SubSection "Display"
                       Depth    24
                       Modes    "1920x1080" "1440x900" "1280x720" "1280x1024" "1280x960" "1024x768" "800x600" "640x480" "720x400"
                    EndSubSection
                EndSection
            EOF
            ;;
[...]

(Yes it is, else X11 doesn't work)

Also worth noting: Armbian chainloads a mainline U-Boot binary while Manjaro just hooks up the vendor U-Boot to load the kernel.

In the Armbian image there's 4.2MB of free space at the start and 653MB at the end. Weird, maybe that's required by the firmware? (Spoiler: It's not)

I quickly made a device package, now I should be able to flash, right?

When I tried to boot it up though it didn't work... Looking at the SD card's partitions I noticed the boot partition was Ext2 while the Armbian one was ... FAT32. Maybe the boot firmware needs a FAT32 partition?

Meanwhile I successfully booted Manjaro from an SD card, I used the GTKing Pro Manjaro ARM image with the DTB file for the TV Box and it boots up as expected.

The /boot partition is from a package called boot-gtking-pro. It even has a PKGBUILD. That shouldn't be too hard to translate that to Alpine Linux's APKBUILD. https://gitlab.manjaro.org/manjaro-arm/packages/core/boot-gtking-pro/-/blob/master/PKGBUILD

Let's try loading a working mainline linux kernel!

Linux

U-Boot isn't actually required for loading a linux kernel. It would be nice but it's not really necessary. For loading the kernel we can just use Amlogic's U-Boot fork which is flashed as the 1st stage bootloader.

The image generated by pmbootstrap doesn't work, weird....

After a few days, I found out why. It was because pmbootstrap generated a uImage from the compressed kernel image... Well, let's install the kernel image uncompressed then! At first it didn't work but regenerating the uImage manually with mkimage from u-boot-tools did the trick - the entry point wasn't correct.

$ mkimage -A arm64 -O linux -T kernel -C none -a 0x1080000 -e 0x1080000 -n linux-next -d vmlinuz uImage

I've set the entry point correctly and

...

It boots!

postmarketOS running on the KII Pro

One Merge Request to postmarketOS then!

Some stuff like audio didn't work though.

Audio

According to chewitt, xdarklight and jbrunet:

<xdarklight> hexdump0815: audio over HDMI works fine on all SoCs 
             nowadays. only analog audio output (3.5mm jack) depends 
             on the I2S codec on the PCB
<xdarklight> f_: hexdump0815: on S905 (and earlier SoC) I typically 
             run alsamixer and set the "HDMI Source" to I2S. on AXG/G12A and newer SoCs you probably need ALSA UCM
<f_> xdarklight: Sure. Thanks! 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<jbrunet> xdarklight: alsa API (alsamixer/amixer) is enough on every SoC 
          to properly set the routes. This is actually the only way to 
          tweak alsa kcontrols.
<jbrunet> There is an infinite number of valid config which depends on 
          the platform but also the current use-case
<jbrunet> Each SoC boots up in the reset configuration of the registers 
          which does not provide a valid confguration on Amlogic.
<jbrunet> For example, on G12 which of the 3 Playback PCM should feed HDMI. 
          All 3 are equally valid. You may even which to switch from one the 
          other at runtime.
<jbrunet> This can't be handled in the kernel.
<jbrunet> Once you've got a config you are happy with, you can use 'alsactl' 
          to save a restore the card controls (easy to automate on boot)
<jbrunet> Alsa UCM is something a bit more complicated to help libraries 
          such as pulseaudio or pipewire figure out things automatically. 
          I have something ongoing for it but it is not there yet.
<f_> Hi!
<f_> <xdarklight> f_: I'd check dmesg first to see if an ALSA card shows up. 
                  I'm not aware of any other alsamixer issues
<f_> Sure.
<f_> So I need to cut down my kernel config. Which options are needed (gxbb)?
<f_> I'd also prefer keeping the kernel image small, so using modules.
<xdarklight> jbrunet: ah, thanks. so UCM is just a shortcut so you don't 
             have to manually toggle lots of things
<xdarklight> f_: CONFIG_SND_SOC_HDMI_CODEC, CONFIG_SND_MESON_AIU, 
             CONFIG_SND_MESON_CARD_UTILS, CONFIG_SND_MESON_CODEC_GLUE and 
             CONFIG_SND_MESON_GX_SOUND_CARD come to my mind
<f_> xdarklight: That's it?
<f_> These are all related to the sound. Is there anything else needed in 
     the kernel?
<xdarklight> f_: I think only the basic toggles to enable sound 
             (CONFIG_SOUND and CONFIG_SND). 
             aarch64 defconfig doesn't have a lot of "common" sound symbols 
             (only a bunch of codecs and SoC specific toggles): 
             https://github.com/torvalds/linux/blob/master/arch/arm64/configs/defconfig#L806
<f_> Yes
<f_> But which options are required at all for the Meson GXBB SoC?
<f_> I would like to compile a kernel without anything that won't get used.
<f_> 3h of compiling is too much.
<f_> I should make my XMPP/Matrix account join this channel, now that I 
     think about it.
[...]
<xdarklight> f_[xmpp]: the config symbols I mentioned previously should be 
             all. the KII Pro doesn't seem to have any special codecs 
             (at least not listed in .dts) other than HDMI audio. 
             other boards may need for example CONFIG_SND_SOC_SPDIF 
             (SPDIF out) or CONFIG_SND_SOC_ES7134 (an analog codec that's 
             soldered to some boards) 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
<chewitt> f_[xmpp]: You are probably missing confs 
          https://github.com/chewitt/alsa-lib/commit/5b81165cf606fba1d8ca6b4cd12b99ebdc91cbc1
<chewitt> and mixer settings 
          https://github.com/LibreELEC/LibreELEC.tv/blob/master/packages/audio/alsa-utils/scripts/soundconfig#L168-L169
<chewitt> in LE these are 'restored' via udev 
          https://github.com/LibreELEC/LibreELEC.tv/blob/master/packages/audio/alsa-utils/udev.d/90-alsa-restore.rules

U-Boot

The KII Pro is quite similar to the Amlogic P201 reference board, and the P201 is supported by U-Boot thanks to the people at https://linux-meson.com so all we need is to compile and put it in the TV Box right?

In order for the TV Box to happily boot from an SD Card, we need something called "bootscripts" or "autoscripts".

I did found some instructions here: https://github.com/hexdump0815/u-boot-misc/blob/master/readme.gxb

Looking at the patches hexdump0815 applies in their notes, I see this: https://github.com/hexdump0815/u-boot-misc/blob/master/misc.gxb/add-usb-kbd-uenv-txt-and-own-strings.patch

So those instructions may have been actually used to build the U-Boot images Armbian uses!

Maybe I can use these to compile U-Boot properly?

According to chewitt (#linux-meson libera.chat):

<chewitt> https://u-boot.readthedocs.io/en/latest/board/amlogic/p201.html
<chewitt> ^ so different u-boot and signing recipe
<f_> Sure.
<chewitt> the challenge with all TV Box devices is .. the boot FIPs 
          needed for signing u-boot
<chewitt> https://github.com/LibreELEC/amlogic-boot-fip has all known GXBB 
          (S905) FIP sources
<chewitt> there are not many
<chewitt> none specific for your box
<chewitt> if you are not scared at the idea of shorting pins on the emmc 
          storage you can build u-boot using the p200 or p201 recipe and 
          substituting the right dts for your box
<chewitt> shorting will allow you to disable emmc and thus force the box 
          to boot from an SD card
<chewitt> or .. you do what LE does and just hook the vendor u-boot to run 
          your kernel 
<f_> Yeah I'll just hook the vendor u-boot. That's much easier.

At some point, after I disconnected (2023-02-04), hexdump0815 told me this:

<hexdump0815> f_: i hope you read this offline in the weblog - you can 
              chainload mainline u-boot from the legacy u-boot: 
              https://github.com/hexdump0815/imagebuilder/blob/main/systems/amlogic_gx/extra-files/boot/s905_autoscript.txt
<hexdump0815> f_: i got this working on some simple s905 tv box with nand 
              i think using the mainline u-boot built as described here: 
              https://github.com/hexdump0815/u-boot-misc/blob/master/readme.gxb

Nice!

After talking with hexdump0815 for a while, they told me that just applying a p201 defconfig patch and building the binary was enough. I then discovered kind of a bug in U-Boot itself. hexdump0815's U-Boot binaries (in their repo) were based on U-Boot v2020.07. I tried compiling the latest version, v2023.01, and HDMI didn't work. Seems like U-Boot v2020.07 is the latest version that's known to work. I tried compiling that version with the patch and it worked!

So that also means pmbootstrap doesn't need to generate a uImage!

So basically the boot process now looks like this:

                                                   +---------+     +-------------------+     +-----------------+
                                             +---->| sd card |---->| 's905_autoscript' |---->| v2020.07 u-boot |
 +---------------+  if sd and multiboot mode |     +---------+     +-------------------+     +-----------------+
 | vendor u-boot |---------------------------+                                                        |
 +---------------+              else if emmc |     +------+     +-----------------------------+       v 
                                             +---->| emmc |---->| vendor's android 5.1.1 fork |   +----------+
                                                   +------+     +-----------------------------+   | extlinux |
                                                                                                  +----------+
                                                                                                       |
                                                                                                       v
                                                                                                +--------------+
                                                                                                | postmarketOS |
                                                                                                +--------------+

I will extend this section later on, while trying to run the latest version of U-Boot.

Conclusion

It's working! If you own a VideoStrong KII Pro and want to test this out the MR is live along with a wiki page!

EDIT: The MR got merged into postmarketOS. If you own a KII Pro you just need to make sure you run pmbootstrap pull . It's now all in the pmaports repository. Enjoy!

Full chat logs: (I'm f_ and f_[xmpp])

Thanks go to everyone who contributed in making mainline linux on Amlogic SoCs a reality!

« Go back to the articles index