Making MacBook Pro 2018 T2 work with ArchLinux (dualboot)

There was quite a lot of hype about the fact that, due to the new T2 chip, it is impossible to install linux on the new 2018 MacBooks with a touch bar. Time passed, and at the end of 2019, third-party developers implemented a number of drivers and kernel patches to interact with the T2 chip. The main driver for MacBook models 2018 and newer implements VHCI (touch / keyboard / etc.) operation, as well as sound operation.

Project mbp2018-bridge-drv divided into 3 main components:

  • BCE (Buffer Copy Engine) - establishes the main communication channel with T2. VHCI and Audio require this component.
  • VHCI is a USB virtual host controller; the keyboard, mouse, and other system components are provided by this component (other drivers use this host controller to provide more functionality.
  • Audio - driver for the T2 audio interface, currently only audio output through the built-in MacBook speakers is supported


The second project is called macbook12-spi-driver, and it implements input driver capability for keyboard, SPI trackpad, touchbar for MacBook Pro late 2016 and later. Some of the keyboard/trackpad drivers are now included in the kernel as of version 5.3.

Support for devices such as wi-fi, touchpad, etc was also implemented using kernel patches. Current kernel version 5.3.5-1

What is currently working

  1. NVMe
  2. Keyboard
  3. USB-C (Thunderbolt was not tested, when the module is automatically loaded, it freezes the system tightly)
  4. Touchbar (with the ability to turn on the Fn keys, backlight, ESC, etc.)
  5. Sound (built-in speakers only)
  6. Wi-Fi module (via brcmfmac and iw only)
  7. DisplayPort over USB-C
  8. Sensors
  9. Suspend/Resume (partially)
  10. etc ..

This tutorial is applicable for macbookpro15,1 and macbookpro15,2. Based on an article from a github in English hence. Not everything from this article worked, so I had to look for a solution on my own.

What you need to install

  • USB-C dock adapter to USB (at least three USB inputs for connecting a mouse, keyboard, usb modem or phone in tethering mode). This is only necessary during the first stages of installation.
  • USB keyboard
  • USB / USB-C flash drive at least 4GB

1. Turn off the prohibition of booting from external media

https://support.apple.com/en-us/HT208330
https://www.ninjastik.com/support/2018-macbook-pro-boot-from-usb/

2. Allocate free space using Disk Utility

For convenience, I immediately allocated 30GB to the disk by formatting it in exfat in the Disk Utility itself. Partitioning a physical disk Disk Utility.

3. We form an iso image

The options are:

  1. You can go the easy way and download the finished image with the 5.3.5-1 kernel and patches from aunali1 link to finished image
  2. Create an image yourself via archlive (requires a system with the arch distribution kit)

    Set archiso

    pacman -S archiso

    
    cp -r /usr/share/archiso/configs/releng/ archlive
    cd archlive
    

    Add the repository to pacman.conf:

    
    [mbp]
    Server = https://packages.aunali1.com/archlinux/$repo/$arch
    

    Ignore the original kernel in pacman.conf:

    IgnorePkg   = linux linux-headers
    

    We add the necessary packages, at the end we add the linux-mbp kernel and linux-mbp-headers

    ...
    wvdial
    xl2tpd
    linux-mbp
    linux-mbp-headers
    

    Change the script to work in interactive mode (replace pacstrap -C with pacstrap -i -C):

    sudo nano /usr/bin/mkarchiso

    # Install desired packages to airootfs
    _pacman ()
    {
        _msg_info "Installing packages to '${work_dir}/airootfs/'..."
    
        if [[ "${quiet}" = "y" ]]; then
            pacstrap -i -C "${pacman_conf}" -c -G -M "${work_dir}/airootfs" $* &> /dev/null
        else
            pacstrap -i -C "${pacman_conf}" -c -G -M "${work_dir}/airootfs" $*
        fi
    
        _msg_info "Packages installed successfully!"
    }

    We form an image:

    sudo ./build.sh -v

    Press Y to skip ignored packages, then write the iso image to a usb stick:

    sudo dd if=out/archlinux*.iso of=/dev/sdb bs=1M

4. First boot

We reboot with the inserted flash drive and keyboard. Press options when the apple appears, select EFI BOOT.

Next, you need to press the "e" key and enter at the end of the command line module_blacklist=thunderbolt. If this is not done, then the system may not boot, and the Thunderbolt ICM Error will crash.

Using fdisk / cfdisk, we find our partition (I have it nvme0n1p4), format it and install the arch. You can use official instructions or outsider.

We do not create a boot partition, we will write the bootloader in / dev / nvme0n1p1
After the environment is fully formed in /mnt and before moving to arch-chroot, we write:

mount /dev/nvme0n1p1 /mnt/boot
arch-chroot /mnt /bin/bash

Add to /etc/pacman.conf:


[mbp]
Server = https://packages.aunali1.com/archlinux/$repo/$arch

Installing the kernel:


sudo pacman -S linux-mbp linux-mbp-headers
sudo mkinitcpio -p linux-mbp

We register thunderbolt and applesmc in /etc/modprobe.d/blacklist.conf

blacklist thunderbolt
blacklist applesmc

Keyboard, touchbar, etc

Install yay:


sudo pacman -S git gcc make fakeroot binutils
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si

Install modules for the touchbar:


git clone --branch mbp15 https://github.com/roadrunner2/macbook12-spi-driver.git
cd macbook12-spi-driver
make install

Add modules to autoload: /etc/modules-load.d/apple.conf

industrialio_triggered_buffer
apple-ibridge
apple-ib-tb
apple-ib-als

Install kernel modules for the keyboard to work. In the repository anuali1 there is a ready package, it is called apple-bce-dkms-git. To install it, write in the console:

pacman -S apple-bce-dkms-git

In this case, the kernel module would be called apple-bce. In the case of self-assembly, it is called bce. Accordingly, if you want to register a module in the MODULES section of the mkinicpio.conf file, then do not forget which module you installed.

Assembly by hand:


git clone https://github.com/MCMrARM/mbp2018-bridge-drv.git
cd mbp2018-bridge-drv
make
cp bce.ko /usr/lib/modules/extramodules-mbp/bce.ko

Add the bce or apple-bce module to autoload: /etc/modules-load.d/bce.conf

bce

If you want to use the Fn buttons by default, then write to the /etc/modprobe.d/apple-tb.conf file:

options apple-ib-tb fnmode=2

Update kernel and initramfs.


mkinitcpio -p linux-mbp

Install iwd:

sudo pacman -S networkmanager iwd

5. Loader

After all the main packages are installed inside the chroot, you can proceed to install the bootloader.

I never managed to get grub to work. Grub boots from an external usb drive, but when you try to register it in nvme via

grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=grub

the system flew into a kernel panic, and after rebooting a new item through options did not appear. I did not find any intelligible solution to this problem, and therefore I decided to try to implement booting using systemd-boot.

  1. Run
    bootctl --path=/boot install

    and fly away to kernel panic. Turn off the MacBook, turn it on again, press options (we do not turn off the usb-c hub with the keyboard)

  2. We check that a new EFI BOOT entry has appeared in addition to the external device
  3. We choose to boot from an external usb-disk, as during the first installation (do not forget to write module_blacklist=thunderbolt)
  4. We mount our disk and go to the environment through arch-chroot


mount /dev/nvme0n1p4 /mnt
mount /dev/nvme0n1p1 /mnt/boot
arch-chroot /mnt

If it is necessary for the keyboard to work until the system is fully loaded (this is necessary if luks / dm-crypt encryption is used), then we write it in the /etc/mkinicpio.conf file in the MODULES section:

MODULES=(ext4 applespi intel_lpss_pci spi_pxa2xx_platform bce)

Update kernel and initramfs.


mkinicpio -p linux-mbp

Setting up systemd-boot

We edit the /boot/loader/loader.conf file, delete everything inside, and add the following:

default arch
timeout 5
editor 1

Go to the /boot/loader/entries folder, create the arch.conf file and write:

title arch
linux /vmlinuz-linux-mbp
initrd /initramfs-linux-mbp.img
options root=/dev/<b>nvme0n1p4</b> rw pcie_ports=compat

In case you used luks and lvm, then

options cryptdevice=/dev/<b>nvme0n1p4</b>:luks root=/dev/mapper/vz0-root rw pcie_ports=compat

We reboot into MacOS.

6. Wi-Fi setup

As it turned out in the end, MacOS stores the firmware files for the wi-fi adapter in the folder /usr/share/firmware/wifi , and you can take them from there in the form of blobs and feed them to the brcmfmac kernel module. In order to find out exactly which files your adapter uses, open a terminal in MacOS and write:

ioreg -l | grep C-4364

We get a long list. We only need files from the section RequestedFiles:

"RequestedFiles" = ({"Firmware"="<b>C-4364__s-B2/maui.trx</b>","TxCap"="C-4364__s-B2/maui-X3.txcb","Regulatory"="C-4364__s-B2/<b>maui-X3.clmb</b>","NVRAM"="C-4364__s-B2/<b>P-maui-X3_M-HRPN_V-m__m-7.7.txt</b>"})

In your case, the file names may differ. We copy them from the /usr/share/firmware/wifi folder to a USB flash drive and rename them to the following form:

    maui.trx -> brcmfmac4364-pcie.bin
    maui-X3.clmb -> brcmfmac4364-pcie.clm_blob
    P-maui-X3_M-HRPN_V-m__m-7.7.txt -> brcmfmac4364-pcie.Apple Inc.-<b>MacBookPro15,2.txt</b>

In this case, the last text file contains the model name, if your model is not macbookpro15,2, then you need to rename this file according to your macbook model.

Reboot to Arch.

Copy the files from the flash drive to the folder /lib/firmware/brcm/


sudo cp brcmfmac4364-pcie.bin /lib/firmware/brcm/
sudo cp brcmfmac4364-pcie.clm_blob /lib/firmware/brcm/
sudo cp 'brcmfmac4364-pcie.Apple Inc.-<b>MacBookPro15,2.txt' /lib/firmware/brcm/

Checking the functionality of the module:


rmmod brcmfmac
modprobe brcmfmac

We make sure that the network interface appeared through ifconfig / ip.
Set up wifi via iwctl

Attention. Via netctl, nmcli, etc. the interface does not work, only through iwd.

We force NetworkManager to use iwd. To do this, create the file /etc/NetworkManager/NetworkManager.conf and write:

[device]
wifi.backend=iwd

Starting the NetworkManager Service


sudo systemctl start NetworkManager.service
sudo systemctl enable NetworkManager.service

7. Sound

In order for the sound to work, you need to install pulseaudio:


sudo pacman -S pulseaudio

Download three files:

We move them:

    /usr/share/alsa/cards/AppleT2.conf
    /usr/share/pulseaudio/alsa-mixer/profile-sets/apple-t2.conf
    /usr/lib/udev/rules.d/91-pulseaudio-custom.rules

8.Suspend/Resume

At the moment 16.10.2019 you have to choose either sound or suspend / resume. We are waiting for the author of the bce module to finish the functionality.

To build a module with susped/resume support, do the following:


git clone https://github.com/MCMrARM/mbp2018-bridge-drv.git
cd mbp2018-bridge-drv
git checkout suspend
make
cp bce.ko /usr/lib/modules/extramodules-mbp/bce.ko
modprobe bce

If you installed the ready-made apple-bce module from the anuali1 repository, then you must first remove it and only after that build and install the bce module with support for suspend mode.

Also, you need to add the applesmc module to the blacklist (if you have not done this before) and make sure that the /boot/loader/entries/arch.conf parameter is added at the end of the options line pcie_ports=compat.

Currently, the touchbar driver crashes when entering suspend mode, and the thunderbolt driver sometimes hangs the system for more than 30 seconds, and when resumed, for several minutes. This can be fixed by automatically unloading problematic modules.

Create a script /lib/systemd/system-sleep/rmmod.sh:

#!/bin/sh
if [ "" == "pre" ]; then
        rmmod thunderbolt
        rmmod apple_ib_tb
elif [ "" == "post" ]; then
        modprobe apple_ib_tb
        modprobe thunderbolt
fi

Make it executable:

sudo chmod +x /lib/systemd/system-sleep/rmmod.sh

That's all for now. The result is a fully functional system, with the exception of some nuances with suspend / resume. No crashes and no kernel panic are observed for several days of uptime. I hope that in the near future the author of the bce module will finish it, and we will get full support for suspend / resume and sound.

Source: habr.com

Add a comment