# PinePhone - Instructions for creating a PureOS image for PinePhone

### Instructions for creating a PureOS Image for PinePhone by mozzwald

You will need `u-boot-tools` package for your distro and it helps to have `binfmt` and `qemu` packages for chrooting to the image. Paths and devices may be different on your system. Always verify before blindly copying and pasting commands.

#### Make a new working directory

```bash
mkdir ~/purepp
cd ~/purepp
```

#### Download and extract PinePhone phosh image (check for newer releases if you like)

```bash
wget https://images.postmarketos.org/pinephone/pine-pinephone-20200218-phosh.img.xz
unxz -d pine-pinephone-20200218-phosh.img.xz
```

#### Download and extract PureOS Librem 5 image (check for newer releases if you like)

```bash
wget http://downloads.pureos.net/amber/phone/gnome/librem5/2020-02-25/librem5.img.xz
unxz -d librem5.img.xz
```

#### Make temporary dirs for mounting images

```bash
mkdir pmos-boot pmos-root pureos-boot pureos-root
```

#### List used loop devices

```bash
losetup -a
```

#### List all loop devices

```bash
ls /dev/loop*
```

**The remaining commands should be run with sudo or as root user (sudo su)**

#### Pick an unused loop device (5 in my case) and mount pmOS image with partitions

```bash
losetup -P /dev/loop5 pine-pinephone-20200218-phosh.img
```

#### Pick an unused loop device (6 in my case) and mount PureOS image with partitions

```bash
losetup -P /dev/loop6 librem5.img
```

#### Get pmOS PinePhone bootloader

```bash
dd if=/dev/loop5 of=uboot-pmos.bin count=2048
```

#### Put pmOS PinePhone bootloader on PureOS image

```bash
dd if=uboot-pmos.bin of=/dev/loop6 bs=1024 skip=8 seek=8
```

#### Mount partitions

```bash
mount /dev/loop5p1 pmos-boot
mount /dev/loop5p2 pmos-root
mount /dev/loop6p1 pureos-boot
mount /dev/loop6p2 pureos-root
```

#### Copy PinePhone kernel & dtb to PureOS image

```bash
cp pmos-boot/vmlinuz-postmarketos-allwinner pureos-boot/
cp pmos-boot/sun50i-a64-pinephone.dtb pureos-boot/
```

#### Copy PinePhone modules to PureOS image

```bash
cp -pRv pmos-root/lib/modules/5.5.0 pureos-root/lib/modules/
```

#### Copy PinePhone firmware to PureOS image

```bash
cp -pRv pmos-root/lib/firmware pureos-root/lib/
```

#### Make u-boot initramfs from PureOS initramfs

```bash
mkimage -A arm -O linux -T ramdisk -C gzip -d pureos-boot/initrd.img pureos-boot/uInitrd.img
```

#### Get uboot-script

```bash
wget https://mozzwald.com/pp/pureos-uboot-script.cmd -O pureos-boot/boot.cmd
```

#### Compile uboot script

```bash
mkimage -A arm64 -T script -C none -d pureos-boot/boot.cmd pureos-boot/boot.scr
```

#### Put helper files into rootfs

```bash
wget https://mozzwald.com/pp/pureos-pp-helpers.tar.gz
cd pureos-root
tar -pxvf ../pureos-pp-helpers.tar.gz
```

#### If you have binfmt setup for arm64/aarch64 then you can chroot and enable modem service and do whatever else you want. Otherwise this needs done after bootup

```bash
cp /usr/bin/qemu-aarch64-static usr/bin/
chroot ./
systemctl enable pinephone.service # enable modem service
exit
cd ..
```

#### Unmount disk images

```bash
umount pmos-boot pmos-root pureos-boot pureos-root
losetup -d /dev/loop5
losetup -d /dev/loop6
```

#### Copy the new image to SD card

```bash
dd if=librem5.img of=/dev/your-sd-card-device bs=1M
```

#### Optionally resize PureOS Root partition on SD card

## Put in PinePhone and boot. WIN!

```
Default username: purism
Default password: 123456
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arkannis.net/os/pinephone/pinephone-instructions-for-creating-a-pureos-image-for-pinephone.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
