From 677742a30e7acd48be42f47383f7933076b14c98 Mon Sep 17 00:00:00 2001
From: William Castro <castrwilliam@gmail.com>
Date: Mon, 12 May 2014 13:42:06 -0700
Subject: [PATCH 1/6] add documentation and infrastructure for booting with
 MultiROM

---
 README.md                | 23 ++++++++++++++++++++
 mkinicpio/hooks/imgmount | 40 ++++++++++++++++++++++++++++++-----
 rom_info.txt             | 45 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+), 5 deletions(-)
 create mode 100644 rom_info.txt

diff --git a/README.md b/README.md
index fa0a962..00cdf4e 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,29 @@ first.
 
 6: Profit
 
+## Booting with MultiROM
+
+You will need to follow the guide at http://forum.xda-developers.com/showthread.php?t=2457063 first.
+
+1: The initcpio hooks have been modified to support this, so if you were using the old ones, you have to replace them with the new. You currently can't boot the same installation via both fastboot and MultiROM. 
+
+2: Boot the Nexus into Android recovery mode. Make sure /data is mounted. /data/media/0/multirom cannot be seen from normal Android.
+
+3: Create a ROM folder for ArchLinuxARM.
+
+  adb shell mkdir /data/media/0/multirom/roms/ArchLinuxARM
+
+4: Push the included rom_info.txt file. (it's a plaintext config file documented at https://github.com/Tasssadar/multirom/wiki/Add-support-for-new-ROM-type)
+
+  adb push rom_info.txt /data/media/0/multirom/roms/ArchLinuxARM/
+  
+5: Move your root image to /data/media/0/multirom/roms/ArchLinuxARM/root.img
+
+6: Make sure you have the kernel and ramdisk in /data/media/0/multirom/roms/ArchLinuxARM/boot/vmlinuz and /data/media/0/multirom/roms/ArchLinuxARM/boot/initrd.img.
+
+6a: Mount the image, and copy them (with the same name) to /boot/ in it.
+
+
 ## To get WiFi Working
 
 A normal Nexus 7 Flo boot will call /system/bin/conn_init which does a whole
diff --git a/mkinicpio/hooks/imgmount b/mkinicpio/hooks/imgmount
index f97bd85..d8b1309 100644
--- a/mkinicpio/hooks/imgmount
+++ b/mkinicpio/hooks/imgmount
@@ -1,13 +1,43 @@
 #!/usr/bin/ash
 
+# You can set these variables manually inside their respective functions if
+# you do not wish to boot using MultiROM:
+# 
+# To boot using a loop-mounted image stored in /data as the root FS:
+# loop=/path/to/arch.img
+# loopfstype=ext4
+#
+# To boot using a bind-mounted subdirectory of /data as the root FS:
+# rootsubdir=/local/arch
+#
+# loop= and rootsubdir= are relative to /data on Android, not /.
+
+
 run_hook () {
-    mount_handler="img_mount_handler"
+    if [ -z "$rootsubdir" ]; then
+        if [ -z "$loop" ]; then
+            error "no loop= or rootsubdir= provided on cmdline"
+            return 1
+        else
+            msg "root is an image at: $loop"
+            mount_handler="img_mount_handler"
+        fi
+    else
+        msg "root is a subdirectory at: $rootsubdir"
+        mount_handler="dir_mount_handler"
+    fi
 }
 
 img_mount_handler() {
-    mkdir /data
-    mount -t ext4 /dev/mmcblk0p30 /data
+    mkdir /host
+    mount -t $rootfstype $root /host
     mknod /dev/loop256 b 7 256
-    losetup /dev/loop256 /data/arch.img
-    mount -t ext4 -o rw,noatime /dev/loop256 /new_root
+    losetup /dev/loop256 /host/$loop
+    mount -t $loopfstype -o rw,noatime /dev/loop256 /new_root
+}
+
+dir_mount_handler() {
+    mkdir /host
+    mount -t $rootfstype $root /host
+    mount --bind /host/$rootsubdir /new_root
 }
diff --git a/rom_info.txt b/rom_info.txt
new file mode 100644
index 0000000..3fe1765
--- /dev/null
+++ b/rom_info.txt
@@ -0,0 +1,45 @@
+# This file contains info about ROMs capabilites and boot process.
+# It should be placed in ROM's folder (eg. /media/multirom/roms/*rom_name*)
+# and must be named "rom_info.txt".
+# Make sure you got the syntax correct, as the parser is probably pretty
+# dumb. Lines with comments must start with #. Beware the whitespaces.
+# If you need to use " character in string, just use it, no need to escape it
+# MultiROM searches for first and last " on the line.
+# These comments should not be deleted.
+
+# So far, the only supported ROM type for these files is kexec-based linux
+type="kexec"
+
+# Paths to root of the ROM.
+# Both image and folder can be specified at one time, MultiROM will use 
+# the one which it can find. If both are present, folder is used.
+# Must _not_ contain spaces.
+# Path is from root of the root partition, but you will usually want
+# to use following alias:
+# - %m - ROM's folder (eg. /media/multirom/roms/*rom_name*)
+root_dir="%m/root"
+root_img="%m/root.img"
+root_img_fs="ext4"
+
+# Path to kernel and initrd. Kernel path _must_ be specified.
+# Paths are relative to the folder in which is this file
+# Those can be outside the root folder/image, or use %r if it is in there:
+# kernel_path="%r/boot/vmlinuz"
+# If ROM is in images, it will mount the image and load it from there.
+# You can use  * _at the end of the filename_ as wildcard character.
+kernel_path="%r/boot/vmlinuz"
+initrd_path="%r/boot/initrd.img"
+
+# Set up the cmdline
+# img_cmdline and dir_cmdline are appended to base_cmdline.
+# Several aliases are used:
+#  - %b - base command line from bootloader. You want this as first thing in cmdline.
+#  - %d - root device. is either "UUID=..." (USB drive) or "/dev/mmcblk0p9" or "/dev/mmcblk0p10"
+#  - %r - root fs type
+#  - %s - root directory, from root of the root device
+#  - %i - root image, from root of the root device
+#  - %f - fs of the root image
+base_cmdline="%b root=%d rootfstype=%r rw console=tty0 rootflags=defaults,noatime,nodiratime"
+img_cmdline="loop=%i loopfstype=%f"
+dir_cmdline="rootsubdir=%s"
+

From f6a8c0700eb243c69f647fa156e8640eadb0a700 Mon Sep 17 00:00:00 2001
From: William Castro <castrwilliam@gmail.com>
Date: Mon, 12 May 2014 13:47:27 -0700
Subject: [PATCH 2/6] seperate directory for systemd units

---
 firmware.service => system/firmware.service | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename firmware.service => system/firmware.service (100%)

diff --git a/firmware.service b/system/firmware.service
similarity index 100%
rename from firmware.service
rename to system/firmware.service

From b18084fbfcac37d220fa3788eaaa51d9f19d4ba1 Mon Sep 17 00:00:00 2001
From: William Castro <castrwilliam@gmail.com>
Date: Mon, 12 May 2014 13:51:05 -0700
Subject: [PATCH 3/6] add documentation and infrastructure for ADB and RNDIS
 over USB

---
 README.md            | 13 +++++++++++++
 system/adbd.service  | 11 +++++++++++
 tmpfiles.d/adbd.conf |  3 +++
 3 files changed, 27 insertions(+)
 create mode 100644 system/adbd.service
 create mode 100644 tmpfiles.d/adbd.conf

diff --git a/README.md b/README.md
index 00cdf4e..ee5fbc9 100644
--- a/README.md
+++ b/README.md
@@ -98,6 +98,16 @@ exec awesome
 Since the MSMFB_SW_REFRESHER does not seem to be working i wrote this instead.
 Just compile and put in you ~/ or whereever so xinit can run it.
 
+## Enabling ADB and RNDIS (reverse USB tethering)
+1: Create this link for ADB to be able to find the shell, etc.
+  ln -s / /system
+  
+2: Add the included systemd-tmpfiles config files. They go in /etc/tmpfiles.d, and require a reboot to apply them.
+
+ADB shell works, but you will need to set the size ($COLUMNS and $LINES) and terminal type ($TERM) manually. 
+
+To use RNDIS, plug the Nexus into a computer and bridge the virtual ethernet adapter with your local network. Then, bring up the link and get an address as usual. The interface name should be 'usb0'.
+
 # Kernel stuff
 
 You can use the kernel from here https://github.com/crondog/kernel_msm
@@ -152,6 +162,9 @@ CONFIG_SECURITY_SELINUX_BOOTPARAM=y
 
 # Whats Working
 Wifi
+ADB
+USB Reverse Tethering/RNDIS (guest)
+USB OTG (host)
 fb0
 Audio -- Need to setup Pulse Audio as systemwide. Not sure why it does not work as a normal user
 You will also need the alsaucm files from libasound2-data_1.0.27.2-1ubuntu6_all.deb
diff --git a/system/adbd.service b/system/adbd.service
new file mode 100644
index 0000000..20b20a4
--- /dev/null
+++ b/system/adbd.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=ADB Daemon
+After=systemd-tmpfiles-setup.service
+
+[Service]
+Type=simple
+ExecStart=/bin/bash -c '/sbin/adbd 2> /var/log/adbd.err 1> /var/log/adbd.out'
+
+[Install]
+WantedBy=basic.target
+Before=network.service
diff --git a/tmpfiles.d/adbd.conf b/tmpfiles.d/adbd.conf
new file mode 100644
index 0000000..87b4b0a
--- /dev/null
+++ b/tmpfiles.d/adbd.conf
@@ -0,0 +1,3 @@
+w /sys/class/android_usb/android0/functions	- - - - 	adb,rndis
+w /sys/class/android_usb/android0/enable	- - - - 	1
+

From 655eab3ab6735b8d71bb26df73303e1792c0afd6 Mon Sep 17 00:00:00 2001
From: William Castro <castrwilliam@gmail.com>
Date: Mon, 12 May 2014 13:53:43 -0700
Subject: [PATCH 4/6] add systemd unit for refresher and document it

---
 README.md                |  2 ++
 system/refresher.service | 10 ++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 system/refresher.service

diff --git a/README.md b/README.md
index ee5fbc9..63a1460 100644
--- a/README.md
+++ b/README.md
@@ -98,6 +98,8 @@ exec awesome
 Since the MSMFB_SW_REFRESHER does not seem to be working i wrote this instead.
 Just compile and put in you ~/ or whereever so xinit can run it.
 
+You can also use the included systemd unit file for the refresher.
+
 ## Enabling ADB and RNDIS (reverse USB tethering)
 1: Create this link for ADB to be able to find the shell, etc.
   ln -s / /system
diff --git a/system/refresher.service b/system/refresher.service
new file mode 100644
index 0000000..3507280
--- /dev/null
+++ b/system/refresher.service
@@ -0,0 +1,10 @@
+[Unit]
+Before=graphical.target
+
+[Service]
+Type=simple
+ExecStart=/bin/refresher
+
+[Install]
+WantedBy=multi-user.target
+

From b7c6525dde111b43ca16a4db1158c4c985742437 Mon Sep 17 00:00:00 2001
From: William Castro <castrwilliam@gmail.com>
Date: Mon, 12 May 2014 13:55:46 -0700
Subject: [PATCH 5/6] document method for sharing Wi-Fi config with Android

---
 README.md | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 63a1460..806cf0e 100644
--- a/README.md
+++ b/README.md
@@ -78,7 +78,22 @@ initramfs (which I am haven't done yet)
 
 Note: Disable the wpa_supplicant hook in /etc/dhcpcd.conf
 
-## To get fbterm console
+# Setting up shared Wi-Fi configuration between Arch and Android
+
+1: Make sure /data is mounted at boot:
+  mkdir /data
+  echo '/dev/disk/by-partlabel/userdata /data ext4 errors=remount-ro 0 0' >> /etc/fstab
+  mount /data
+
+2: Forcibly symlink the Android wpa_supplicant.conf to the Linux location.
+  ln -fs /data/misc/wifi/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant-nl80211-wlan0.conf
+  
+3: Create a name for the Android wifi group
+  groupadd -g $(stat -c '%g' /data/misc/wifi) -r android_wifi
+  
+4: Add your main user to said group, so you can access the control sockets. You will need to manually their path if you want to edit the WiFi network configureation.
+
+  ## To get fbterm console
 Copy fbterm/getty@.service to /etc/systemd/system/ and fbterm/fbterm-login to
 /opt/fbterm-login and then systemctl enable getty@tty1.service
 

From aa7c5a30bf291d1626455aa0f50ac60844d0f0d7 Mon Sep 17 00:00:00 2001
From: William Castro <castrwilliam@gmail.com>
Date: Mon, 12 May 2014 13:57:22 -0700
Subject: [PATCH 6/6] improve README.md section on kernel config

---
 README.md | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 806cf0e..da9a6aa 100644
--- a/README.md
+++ b/README.md
@@ -143,7 +143,21 @@ messages is still a no go as the device is registered too late to work
 I used the following additional configs to get this working. Some might not be
 necessary but it helps with debugging
 
+Required for display/graphics:
+
 CONFIG_FB_MSM_DEFAULT_DEPTH_BGRA8888 (with RGBA patch from robclark)
+CONFIG_DRM=y
+CONFIG_MSM_KGSL_DRM=y
+
+Required for Wi-Fi/network:
+
+CONFIG_ANDROID_PARANOID_NETWORK=n
+CONFIG_MODULES=y
+CONFIG_PRIMA_WLAN=y
+CONFIG_WCNSS_CORE=y
+
+Required to boot:
+
 CONFIG_FHANDLE=y
 CONFIG_DEVTMPFS=y
 CONFIG_UTS_NS=y
@@ -154,13 +168,20 @@ CONFIG_NET_NS=y
 CONFIG_DEVPTS_MULTIPLE_INSTANCES=y
 CONFIG_FANOTIFY=y
 CONFIG_FANOTIFY_ACCESS_PERMISSIONS=y
+CONFIG_SECURITY_SELINUX_BOOTPARAM=y
+
+Debugging:
+
 CONFIG_DETECT_HUNG_TASK=y
 CONFIG_DEBUG_SPINLOCK=y
 CONFIG_DEBUG_MUTEXES=y
 CONFIG_DEBUG_ATOMIC_SLEEP=y
 CONFIG_STACKTRACE=y
 CONFIG_DEBUG_BUGVERBOSE=y
-CONFIG_SECURITY_SELINUX_BOOTPARAM=y
+CONFIG_LOG_BUF_SHIFT=21
+CONFIG_PANIC_TIMEOUT=0
+
+If you use the kernel at https://github.com/crondog/kernel_msm, there is a default configuration named flo_defconfig which contains most of the needed options.
 
 ## Build initramfs (Manually)
 1: Edit makebootimage.sh with paths
