From cae3b427e372e6a511be1bb625525e2385f5b4ca Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Sat, 14 Sep 2013 07:33:11 +0200
Subject: [PATCH] New package: linux3.10-rpi-3.10.11 (latest kernel from
 rpi-3.10.y branch).

---
 srcpkgs/linux3.10-headers-rpi                 |   1 +
 srcpkgs/linux3.10-rpi/INSTALL                 |  16 ++
 .../linux3.10-rpi/files/args-uncompressed.txt |  12 ++
 .../linux3.10-rpi/files/boot-uncompressed.txt |  17 ++
 .../files/imagetool-uncompressed.py           |  41 ++++
 srcpkgs/linux3.10-rpi/template                | 180 ++++++++++++++++++
 6 files changed, 267 insertions(+)
 create mode 120000 srcpkgs/linux3.10-headers-rpi
 create mode 100644 srcpkgs/linux3.10-rpi/INSTALL
 create mode 100644 srcpkgs/linux3.10-rpi/files/args-uncompressed.txt
 create mode 100644 srcpkgs/linux3.10-rpi/files/boot-uncompressed.txt
 create mode 100644 srcpkgs/linux3.10-rpi/files/imagetool-uncompressed.py
 create mode 100644 srcpkgs/linux3.10-rpi/template

diff --git a/srcpkgs/linux3.10-headers-rpi b/srcpkgs/linux3.10-headers-rpi
new file mode 120000
index 0000000000..1402249c32
--- /dev/null
+++ b/srcpkgs/linux3.10-headers-rpi
@@ -0,0 +1 @@
+linux3.10-rpi
\ No newline at end of file
diff --git a/srcpkgs/linux3.10-rpi/INSTALL b/srcpkgs/linux3.10-rpi/INSTALL
new file mode 100644
index 0000000000..918c21b4d8
--- /dev/null
+++ b/srcpkgs/linux3.10-rpi/INSTALL
@@ -0,0 +1,16 @@
+case ${ACTION} in
+post)
+	cat <<EOF
+=========================================================================
+
+To enable this kernel add the following line to /boot/config.txt:
+
+	kernel=kernel-${VERSION}
+
+Or copy /boot/kernel-${VERSION}.img to /boot/kernel.img (do not forget to
+keep a backup of current file!).
+
+=========================================================================
+EOF
+	;;
+esac
diff --git a/srcpkgs/linux3.10-rpi/files/args-uncompressed.txt b/srcpkgs/linux3.10-rpi/files/args-uncompressed.txt
new file mode 100644
index 0000000000..09398b47ee
--- /dev/null
+++ b/srcpkgs/linux3.10-rpi/files/args-uncompressed.txt
@@ -0,0 +1,12 @@
+; kernel args (place at 0x00000100)
+0x00000005
+0x54410001
+0x00000001
+0x00001000
+0x00000000
+0x00000004
+0x54410002
+0x08000000
+0x00000000
+0x00000000
+0x00000000
diff --git a/srcpkgs/linux3.10-rpi/files/boot-uncompressed.txt b/srcpkgs/linux3.10-rpi/files/boot-uncompressed.txt
new file mode 100644
index 0000000000..1cf5888278
--- /dev/null
+++ b/srcpkgs/linux3.10-rpi/files/boot-uncompressed.txt
@@ -0,0 +1,17 @@
+; bootloader (place at 0x00000000)
+0xea000006
+0xe1a00000
+0xe1a00000
+0xe1a00000
+0xe1a00000
+0xe1a00000
+0xe1a00000
+0xe1a00000
+
+0xe3a00000
+0xe3a01042
+0xe3811c0c
+0xe59f2000
+0xe59ff000
+0x00000100
+0x00008000
diff --git a/srcpkgs/linux3.10-rpi/files/imagetool-uncompressed.py b/srcpkgs/linux3.10-rpi/files/imagetool-uncompressed.py
new file mode 100644
index 0000000000..1d4f9c9b7a
--- /dev/null
+++ b/srcpkgs/linux3.10-rpi/files/imagetool-uncompressed.py
@@ -0,0 +1,41 @@
+#!/usr/bin/python2
+
+import os
+import re
+import sys
+
+try:
+   linuxdir = sys.argv[1]
+except:
+   linuxdir = "linux"
+   
+re_line = re.compile(r"0x(?P<value>[0-9a-f]{8})")
+
+mem = [0 for i in range(32768)]
+
+def load_to_mem(name, addr):
+   f = open(name)
+
+   for l in f.readlines():
+      m = re_line.match(l)
+
+      if m:
+         value = int(m.group("value"), 16)
+
+         for i in range(4):
+            mem[addr] = int(value >> i * 8 & 0xff)
+            addr += 1
+
+   f.close()
+
+load_to_mem("boot-uncompressed.txt", 0x00000000)
+load_to_mem("args-uncompressed.txt", 0x00000100)
+
+f = open("first32k.bin", "wb")
+
+for m in mem:
+   f.write(chr(m))
+
+f.close()
+
+os.system("cat first32k.bin Image > kernel.img")
diff --git a/srcpkgs/linux3.10-rpi/template b/srcpkgs/linux3.10-rpi/template
new file mode 100644
index 0000000000..7f1254e1a1
--- /dev/null
+++ b/srcpkgs/linux3.10-rpi/template
@@ -0,0 +1,180 @@
+# Template file for 'linux3.10-rpi'
+#
+# Latest commit as of 20130914.
+_githash="eb9b537"
+
+pkgname=linux3.10-rpi
+version=3.10.11
+revision=1
+maintainer="Juan RP <xtraeme@gmail.com>"
+homepage="http://www.kernel.org"
+license="GPL-2"
+short_desc="The Linux kernel and modules for the Raspberry Pi (3.10 series [${_githash}])"
+
+_kernver="${version}_${revision}"
+hostmakedepends="perl python kmod>=11_2 uboot-mkimage bc openssl which elfutils"
+makedepends="ncurses-devel"
+only_for_archs="armv6l"
+
+do_fetch() {
+	url="git://github.com/raspberrypi/linux.git"
+	msg_normal "Cloning from $url (rpi-3.10.y branch)...\n"
+	git clone -b rpi-3.10.y --depth 1 $url ${pkgname}-${version}
+	cd ${pkgname}-${version}
+	git checkout ${_githash}
+	# Remove .git directory, otherwise scripts/setkernelversion.sh
+	# modifies KERNELRELEASE and appends + to it.
+	rm -rf .git
+}
+
+do_configure() {
+	if [ "$CROSS_BUILD" ]; then
+		_args="CROSS_COMPILE=${XBPS_CROSS_TRIPLET}-"
+	fi
+
+	# Copy required files to create the bootable image.
+	cp ${FILESDIR}/args-uncompressed.txt arch/arm/boot/
+	cp ${FILESDIR}/boot-uncompressed.txt arch/arm/boot/
+	cp ${FILESDIR}/imagetool-uncompressed.py arch/arm/boot/
+
+	make ${makejobs} ${_args} ARCH=arm bcmrpi_defconfig
+
+	# Always use our revision to CONFIG_LOCALVERSION to match our pkg version.
+	sed -i -e "s|^\(CONFIG_LOCALVERSION=\).*|\1\"_${revision}\"|" .config
+}
+
+do_build() {
+	if [ "$CROSS_BUILD" ]; then
+		_args="CROSS_COMPILE=${XBPS_CROSS_TRIPLET}-"
+	fi
+	make ${makejobs} ${_args} ARCH=arm prepare
+	make ${makejobs} ${_args} ARCH=arm uImage modules
+}
+
+do_install() {
+	local hdrdest
+
+	# Run depmod after compressing modules.
+	sed -i '2iexit 0' scripts/depmod.sh
+
+	# Install kernel, firmware and modules
+	make ARCH=arm INSTALL_MOD_PATH=${DESTDIR} modules_install
+
+	# Generate kernel.img and install it to destdir.
+	cd arch/arm/boot
+	/usr/bin/python2 imagetool-uncompressed.py
+	cd ${wrksrc}
+	vinstall arch/arm/boot/kernel.img 644 boot kernel-${_kernver}.img
+
+	hdrdest=${DESTDIR}/usr/src/kernel-headers-rpi-${_kernver}
+
+	# Switch to /usr.
+	vmkdir usr
+	mv ${DESTDIR}/lib ${DESTDIR}/usr
+
+	cd ${DESTDIR}/usr/lib/modules/${_kernver}
+	rm -f source build
+	ln -sf ../../../src/kernel-headers-rpi-${_kernver} build
+
+	cd ${wrksrc}
+	# Install required headers to build external modules
+	install -Dm644 Makefile ${hdrdest}/Makefile
+	install -Dm644 kernel/Makefile ${hdrdest}/kernel/Makefile
+	install -Dm644 .config ${hdrdest}/.config
+	mkdir -p ${hdrdest}/include
+
+	# Remove firmware stuff provided by the "linux-firmware" pkg.
+	rm -rf ${DESTDIR}/usr/lib/firmware
+
+	for i in acpi asm-generic config crypto drm generated linux math-emu \
+		media net pcmcia scsi sound trace video xen; do
+		[ -d include/$i ] && cp -a include/$i ${hdrdest}/include
+	done
+
+	cd ${wrksrc}
+	# Copy files necessary for later builds.
+	cp Module.symvers ${hdrdest}
+	cp -a scripts ${hdrdest}
+
+	# fix permissions on scripts dir
+	chmod og-w -R ${hdrdest}/scripts
+
+	# copy arch includes for external modules
+	mkdir -p ${hdrdest}/arch/arm/mach-bcm2708
+	cp -a arch/arm/include ${hdrdest}/arch/arm
+	cp -a arch/arm/mach-bcm2708/include ${hdrdest}/arch/arm/mach-bcm2708
+
+	mkdir -p ${hdrdest}/arch/arm/kernel
+
+	cp arch/arm/Makefile ${hdrdest}/arch/arm
+	if [ "$MACHINE_ARCH" = "i686" ]; then
+		cp arch/arm/Makefile_32.cpu ${hdrdest}/arch/arm
+	fi
+	cp arch/arm/kernel/asm-offsets.s ${hdrdest}/arch/arm/kernel
+
+	# Add docbook makefile
+	install -Dm644 Documentation/DocBook/Makefile \
+		${hdrdest}/Documentation/DocBook/Makefile
+
+	# Add md headers
+	mkdir -p ${hdrdest}/drivers/md
+	cp drivers/md/*.h ${hdrdest}/drivers/md
+
+	# Add inotify.h
+	mkdir -p ${hdrdest}/include/linux
+	cp include/linux/inotify.h ${hdrdest}/include/linux
+
+	# Add wireless headers
+	mkdir -p ${hdrdest}/net/mac80211/
+	cp net/mac80211/*.h ${hdrdest}/net/mac80211
+
+	# Copy in Kconfig files
+	for i in $(find . -name "Kconfig*"); do
+		mkdir -p ${hdrdest}/$(echo $i | sed 's|/Kconfig.*||')
+		cp $i ${hdrdest}/$i
+	done
+
+	# Remove unneeded architectures
+	for arch in alpha arm26 avr32 blackfin cris frv h8300 \
+		ia64 m* p* s* um v850 x86 xtensa; do
+		rm -rf ${hdrdest}/arch/${arch}
+	done
+
+	# Compress all modules with xz to save a few MBs.
+	msg_normal "$pkgver: compressing kernel modules with gzip, please wait...\n"
+	find ${DESTDIR} -name '*.ko' -exec gzip -9 {} \;
+
+	# ... and run depmod again.
+	depmod -b ${DESTDIR}/usr -F System.map ${_kernver}
+}
+
+linux3.10-headers-rpi_package() {
+	preserve=yes
+	nostrip=yes
+	noverifyrdeps=yes
+	short_desc="Linux kernel headers for the Raspberry Pi (3.10 series [${_githash}])"
+	pkg_install() {
+		vmove usr/src
+		vmove usr/lib/modules/${_kernver}/build
+	}
+}
+
+linux3.10-rpi_package() {
+	preserve=yes
+	nostrip=yes
+	noverifyrdeps=yes
+	triggers="kernel-hooks"
+	depends="kmod>=11_2"
+	# These files could be modified when an external module is built.
+	mutable_files="
+		/usr/lib/modules/${_kernver}/modules.dep
+		/usr/lib/modules/${_kernver}/modules.dep.bin
+		/usr/lib/modules/${_kernver}/modules.symbols
+		/usr/lib/modules/${_kernver}/modules.symbols.bin
+		/usr/lib/modules/${_kernver}/modules.alias
+		/usr/lib/modules/${_kernver}/modules.alias.bin
+		/usr/lib/modules/${_kernver}/modules.devname"
+	pkg_install() {
+		vmove all
+	}
+}