slitaz-arm rev 31

Add 'spi' SliTaz Raspberry Pi Tool
author Christophe Lincoln <pankso@slitaz.org>
date Wed Mar 05 23:40:22 2014 +0100 (2014-03-05)
parents b2a49be1fc53
children b38c9a3fca9a
files spi
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/spi	Wed Mar 05 23:40:22 2014 +0100
     1.3 @@ -0,0 +1,405 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# Spi - SliTaz Raspberry Pi Tool
     1.7 +#
     1.8 +# Copyright (C) 2012-2014 SliTaz ARM - BSD License
     1.9 +# Author: Christophe Lincoln <pankso@slitaz.org>
    1.10 +#
    1.11 +. /lib/libtaz.sh
    1.12 +
    1.13 +# Path's
    1.14 +[ "$work" ] || work="$(pwd)"
    1.15 +distro="$work/distro"
    1.16 +data="$work/rpi/data"
    1.17 +boot="$work/rpi/boot"
    1.18 +rootfs="$work/rpi/rootfs"
    1.19 +kernel="$data/linux"
    1.20 +firmware="$data/firmware"
    1.21 +tools="$data/tools"
    1.22 +
    1.23 +# URL's
    1.24 +rpi_url="http://mirror.slitaz.org/arm/rpi/"
    1.25 +fw_url="https://github.com/raspberrypi/firmware/raw/master/boot/"
    1.26 +tools_url="https://raw.github.com/raspberrypi/tools/master/mkimage/"
    1.27 +lkgit_url="git://github.com/raspberrypi/linux.git"
    1.28 +
    1.29 +# List's
    1.30 +fwlist="bootcode.bin fixup.dat start.elf"
    1.31 +toolslist="imagetool-uncompressed.py args-uncompressed.txt \
    1.32 +boot-uncompressed.txt" #first32k.bin
    1.33 +
    1.34 +#
    1.35 +# Functions
    1.36 +#
    1.37 +
    1.38 +usage() {
    1.39 +	cat << EOT
    1.40 +
    1.41 +$(boldify "Usage:") $(basename $0) [command] [--option]
    1.42 +
    1.43 +$(boldify "Commands:")
    1.44 +  info          Display paths and distro info
    1.45 +  install       Install SliTaz RPi to sdcard
    1.46 +  gen           Generate a new SliTaz RPi distro
    1.47 +  linux         Build the Raspberry Pi Linux kernel
    1.48 +  get-fw        Download or update RPi Firmware
    1.49 +  get-tools     Download or update RPi Tools (mkimage)
    1.50 +  get-prebuilt  Get a prebuilt SliTaz ARM toolchain
    1.51 +  oclock        Display overclocking information
    1.52 +
    1.53 +$(boldify "Options:")
    1.54 +  --up          Update firmware, tools or kernel
    1.55 +  --clean       Clean the distro tree (Sat can also be used)
    1.56 +  --turbo       Force the RPi to run at the highest arm_freq
    1.57 +  --oclock=     Set the RPi overclocking mode
    1.58 +
    1.59 +EOT
    1.60 +}
    1.61 +
    1.62 +error() {
    1.63 +	echo "[ $(colorize 31 'ERROR') ] $@"
    1.64 +}
    1.65 +
    1.66 +header() {
    1.67 +	newline
    1.68 +	boldify "$@"
    1.69 +	separator
    1.70 +}
    1.71 +
    1.72 +# Get RPi firmware
    1.73 +get_fw() {
    1.74 +	mkdir -p $firmware
    1.75 +	for fw in $fwlist
    1.76 +	do
    1.77 +		[ "$up" ] && rm -f $firmware/$fw
    1.78 +		if [ ! -f "$firmware/$fw" ]; then
    1.79 +			echo -n "Fetching: $fw"
    1.80 +			wget -q --no-check-certificate ${fw_url}${fw} \
    1.81 +				-O $firmware/${fw}; status
    1.82 +		fi
    1.83 +	done
    1.84 +}
    1.85 +
    1.86 +# Get RPi tools
    1.87 +get_tools() {
    1.88 +	mkdir -p $data
    1.89 +	for t in $toolslist
    1.90 +	do
    1.91 +		[ "$up" ] && rm -f $data/$t
    1.92 +		if [ ! -f "$data/$t" ]; then
    1.93 +			echo -n "Fetching: $t"
    1.94 +			wget -q --no-check-certificate ${tools_url}${t} \
    1.95 +				-O $data/${t}; status
    1.96 +		fi
    1.97 +	done
    1.98 +}
    1.99 +
   1.100 +# Configure overclocking
   1.101 +set_oclock() {
   1.102 +	case "$oclock" in
   1.103 +		none)
   1.104 +			arm_freq=700
   1.105 +			core_freq=250
   1.106 +			sdram_freq=400
   1.107 +			over_voltage=0 ;;
   1.108 +		modest)
   1.109 +			arm_freq=800
   1.110 +			core_freq=300
   1.111 +			sdram_freq=400
   1.112 +			over_voltage=0 ;;
   1.113 +		medium)
   1.114 +			arm_freq=900
   1.115 +			core_freq=333
   1.116 +			sdram_freq=450
   1.117 +			over_voltage=2 ;;
   1.118 +		hight)
   1.119 +			arm_freq=950
   1.120 +			core_freq=450
   1.121 +			sdram_freq=450
   1.122 +			over_voltage=6 ;;
   1.123 +		turbo)
   1.124 +			arm_freq=1000
   1.125 +			core_freq=500
   1.126 +			sdram_freq=500
   1.127 +			over_voltage=6 ;;
   1.128 +	esac
   1.129 +	cat >> ${boot}/config.txt << EOT
   1.130 +arm_freq=$arm_freq
   1.131 +core_freq=$core_freq
   1.132 +sdram_freq=$sdram_freq
   1.133 +over_voltage=$over_voltage
   1.134 +EOT
   1.135 +}
   1.136 +
   1.137 +#
   1.138 +# Commands
   1.139 +#
   1.140 +
   1.141 +case "$1" in
   1.142 +	info)
   1.143 +		header "SliTaz Raspberry Pi info"
   1.144 +		echo "Firmware : $fwlist" 
   1.145 +		echo "RPi path : $work/rpi"
   1.146 +		colorize 36 "/boot/cmdline.txt:"
   1.147 +		cat ${boot}/cmdline.txt
   1.148 +		colorize 36 "/boot/config.txt:"
   1.149 +		cat ${boot}/config.txt
   1.150 +		separator && newline ;;
   1.151 +	
   1.152 +	install)
   1.153 +		rpiboot="/media/rpi/boot"
   1.154 +		rpiroot="/media/rpi/rootfs"
   1.155 +		header "SliTaz Raspberry Pi install"
   1.156 +		if [ ! "$dev" ]; then
   1.157 +			echo "Missing: --dev= cmdline option" && exit 1
   1.158 +		fi
   1.159 +		
   1.160 +		# Store sdcard partition(s) list
   1.161 +		fdisk -l /dev/${dev} | grep "^/dev/$dev" | awk '{print $1}' \
   1.162 +			> ${data}/sdcard.part
   1.163 +		partnb=$(cat ${data}/sdcard.part | wc -l)
   1.164 +		if [ "$partnb" != 3 ]; then
   1.165 +			error "SliTaz RPi need 3 partitions on the sdcard" 
   1.166 +			newline && exit 1
   1.167 +		fi
   1.168 +		
   1.169 +		# Mount sdcard
   1.170 +		if mount | grep -q "^/dev/$dev[1-3]"; then
   1.171 +			debug "Unmounting: /dev/$dev"
   1.172 +			umount /dev/${dev}1 2>/dev/null || exit 1
   1.173 +			umount /dev/${dev}3 2>/dev/null || exit 1
   1.174 +		fi
   1.175 +		echo -n "Mounting: /dev/$dev partitions"
   1.176 +		mkdir -p ${rpiboot} ${rpiroot}
   1.177 +		mount /dev/${dev}1 ${rpiboot}
   1.178 +		mount /dev/${dev}3 ${rpiroot}; status
   1.179 +		echo -n "Installing: boot files"
   1.180 +		cp -rf ${boot}/* ${rpiboot}; status
   1.181 +		echo -n "Installing: rootfs files"
   1.182 +		cp -a ${rootfs}/* ${rpiroot}; status
   1.183 +		
   1.184 +		# Unmount
   1.185 +		echo -n "Unmounting: RPi sdcard"
   1.186 +		umount ${rpiboot} || exit 1
   1.187 +		umount ${rpiroot} || exit 1
   1.188 +		status
   1.189 +		
   1.190 +		# Boot flag
   1.191 +		#echo -n "Setting boot flag on: /dev/${dev}1"
   1.192 +		#fdisk /dev/${dev} >/dev/null << EOF
   1.193 +#a
   1.194 +#1
   1.195 +#w
   1.196 +#EOF
   1.197 +		#status
   1.198 +		rm -f ${data}/sdcard.part
   1.199 +		separator && newline ;;
   1.200 +		
   1.201 +	ls-dev)
   1.202 +		newline
   1.203 +		fdisk -l | grep "^Disk /dev/sd*" 
   1.204 +		newline ;;
   1.205 +	
   1.206 +	gen)
   1.207 +		# Separate boot files since the Raspberry Pi boots off a FAT32 /boot 
   1.208 +		# partition on the sdcard.
   1.209 +		: ${flavor=rpi}
   1.210 +		: ${oclock=none}
   1.211 +		check_root
   1.212 +		rm -rf ${rootfs} ${boot}
   1.213 +		mkdir -p ${boot}
   1.214 +		
   1.215 +		# Use the rootfs generated by sat
   1.216 +		if [ ! -x "/usr/bin/sat" ]; then
   1.217 +			error "Sat is not installed" && exit 1
   1.218 +		fi
   1.219 +		[ "$clean" ] && sat clean --work="$work"
   1.220 +		if [ ! -d "$distro/rootfs/etc" ]; then
   1.221 +			sat gen --work="$work" --flavor="$flavor" --kmap --spk --rpi
   1.222 +		fi
   1.223 +		header "SliTaz Raspberry Pi distro"
   1.224 +		dsize="$(du -sh ${distro}/rootfs | awk '{print $1}')"
   1.225 +		echo -n "Copying the rootfs: $dsize"
   1.226 +		cp -a ${distro}/rootfs ${rootfs}; status
   1.227 +		
   1.228 +		# Boot firmware
   1.229 +		echo -n "Installing: firmware files"
   1.230 +		get_fw
   1.231 +		cp $firmware/* ${boot}; status
   1.232 +		
   1.233 +		# /boot/cmdline.txt
   1.234 +		echo -n "Creating: /boot/cmdline.txt"
   1.235 +		echo 'root=/dev/mmcblk0p3 rootwait quiet' >> \
   1.236 +			${boot}/cmdline.txt; status
   1.237 +		
   1.238 +		# /boot/config.txt
   1.239 +		echo -n "Creating: /boot/config.txt"
   1.240 +		echo 'kernel=kernel.img' > ${boot}/config.txt
   1.241 +		set_oclock; status
   1.242 +		if [ "$turbo" ]; then
   1.243 +			echo "force_turbo=1" >> ${boot}/config.txt
   1.244 +		fi
   1.245 +		
   1.246 +		# /etc/fstab
   1.247 +		echo -n "Creating: /etc/fstab"
   1.248 +		cat > ${rootfs}/etc/fstab << EOT
   1.249 +# /etc/fstab: Slitaz ARM static filesystems.
   1.250 +#
   1.251 +proc				/proc		proc	defaults			0 0
   1.252 +sysfs				/sys		sysfs	defaults			0 0
   1.253 +/dev/mmcblk0p1		/boot		auto	noauto,noatime		1 2
   1.254 +/dev/mmcblk0p2		none		swap	sw					0 0
   1.255 +/dev/mmcblk0p3		/			ext3	noatime				0 1
   1.256 +EOT
   1.257 +		status
   1.258 +		
   1.259 +		# Kernel
   1.260 +		kvers=$(ls $data/linux-*/fs/lib/modules)
   1.261 +		kpkg="$rootfs/var/lib/tazpkg/installed/linux"
   1.262 +		if [ -d "$data/linux-$kvers/fs" ]; then
   1.263 +			echo -n "Installing: Linux Kernel $kvers"
   1.264 +			rm -rf ${rootfs}/lib/modules
   1.265 +			cp -rf ${data}/linux-${kvers}/fs/lib/* ${rootfs}/lib
   1.266 +			cp -f  ${data}/linux-${kvers}/fs/boot/* ${boot}
   1.267 +			mkdir -p ${kpkg}
   1.268 +			cd ${data}/linux-${kvers}
   1.269 +			cp -f files.list md5sum receipt ${kpkg}
   1.270 +			status
   1.271 +		else
   1.272 +			echo "RPi Kernel: not used"
   1.273 +		fi
   1.274 +		mkdir -p ${rootfs}/boot
   1.275 +		separator
   1.276 +		echo -n "Boot: $(du -sh $boot | awk '{print $1}') "
   1.277 +		echo "- Rootfs: $(du -sh $rootfs | awk '{print $1}')" 
   1.278 +		newline ;;
   1.279 +	
   1.280 +	linux)
   1.281 +		# Native SliTaz Toolchain and cross toolchain must be installed
   1.282 +		check_root
   1.283 +		#: ${arch=armv6hf}
   1.284 +		: ${arch=arm}
   1.285 +		install="$data/linux-install"
   1.286 +		if [ ! -d "/cross/$arch" ]; then
   1.287 +			error "Missing cross toolchain in: /cross/$arch"
   1.288 +			exit 1
   1.289 +		fi
   1.290 +		# Kernel source
   1.291 +		[ -d "$kernel" ] || git clone --depth 1 ${lkgit_url}
   1.292 +		
   1.293 +		if [ ! -d "$install" ]; then
   1.294 +			cd ${kernel}
   1.295 +			export PATH=$PATH:/cross/${arch}/tools/bin
   1.296 +			export HOST_SYSTEM=${arch}-slitaz-linux-gnueabi
   1.297 +			make ARCH=arm bcmrpi_defconfig &&
   1.298 +			make ARCH=arm CROSS_COMPILE=${HOST_SYSTEM}- zImage &&
   1.299 +			make ARCH=arm CROSS_COMPILE=${HOST_SYSTEM}- modules &&
   1.300 +			make ARCH=arm CROSS_COMPILE=${HOST_SYSTEM}- \
   1.301 +				INSTALL_MOD_PATH=${install} modules_install || exit 1
   1.302 +			mkdir -p ${install}/boot
   1.303 +			cp -a arch/arm/boot/zImage ${install}/boot
   1.304 +		fi
   1.305 +		
   1.306 +		# Pack
   1.307 +		fs="$data/linux-$kvers/fs"
   1.308 +		kvers=$(ls ${install}/lib/modules)
   1.309 +		newline
   1.310 +		echo "Kernel version: $kvers"
   1.311 +		if [ -d "$install" ]; then
   1.312 +			rm -rf ${data}/linux-${kvers}
   1.313 +			mkdir -p ${data}/linux-${kvers}
   1.314 +			cp -a ${install} ${fs}
   1.315 +			rm ${fs}/lib/modules/build ${fs}/lib/modules/source
   1.316 +		fi
   1.317 +		get_tools
   1.318 +		echo "Compressing: zImage to kernel.img"
   1.319 +		cd ${data}
   1.320 +		python imagetool-uncompressed.py ${fs}/boot/zImage
   1.321 +		mv kernel.img ${fs}/boot && rm ${fs}/boot/zImage
   1.322 +		echo "Creating package: receipt"
   1.323 +		cat > linux-$kvers/receipt << EOT
   1.324 +# SliTaz package receipt
   1.325 +
   1.326 +PACKAGE="linux"
   1.327 +VERSION="$kvers"
   1.328 +SHORT_DESC="SliTaz Linux Kernel for the Raspberry Pi."
   1.329 +WEB_SITE="http://www.kernel.org"
   1.330 +
   1.331 +EOT
   1.332 +		tazpkg pack linux-$kvers ;;
   1.333 +	
   1.334 +	get-fw)
   1.335 +		get_fw ;;
   1.336 +		
   1.337 +	get-tools)
   1.338 +		get_tools ;;
   1.339 +		
   1.340 +	get-linux)
   1.341 +		# Precook RPi kernel
   1.342 +		rm -f ${data}/linux-version.txt
   1.343 +		if busybox wget -q -s ${rpi_url}/last-linux.txt; then
   1.344 +			echo -n "Fetching latest Kernel string..."
   1.345 +			wget -q ${rpi_url}/last-linux.txt \
   1.346 +				-O ${data}/linux-version.txt || exit 1
   1.347 +			status
   1.348 +		else
   1.349 +			echo "Mirror is unreatchable" && exit 1
   1.350 +		fi
   1.351 +		kvers=$(cat $data/linux-version.txt)
   1.352 +		[ "$up" ] && rm -rf ${data}/linux-${kvers}*
   1.353 +		echo "Kernel version: $kvers"
   1.354 +		# Download
   1.355 +		if [ ! -f "$data/linux-$kvers.tazpkg" ]; then
   1.356 +			echo -n "Fetching latest Linux package..."
   1.357 +			wget -q ${rpi_url}/linux-${kvers}.tazpkg \
   1.358 +				-O ${data}/linux-${kvers}.tazpkg; status
   1.359 +		fi
   1.360 +		# Extract
   1.361 +		if [ ! -d "$data/linux-$kvers" ]; then
   1.362 +			cd ${data} && tazpkg extract linux-${kvers}.tazpkg
   1.363 +		fi ;;
   1.364 +		
   1.365 +	get-prebuilt)
   1.366 +		# --> in cross ??
   1.367 +		: ${arch=arm}
   1.368 +		name="slitaz-$arch-toolchain"
   1.369 +		vers="20140304"
   1.370 +		tarball="$name-$vers.tar.bz2"
   1.371 +		url="http://mirror.slitaz.org/packages/cross/"
   1.372 +		mkdir -p /cross
   1.373 +		cd /cross
   1.374 +		if [ ! -f "$tarball" ]; then
   1.375 +			if busybox wget -qs ${url%/}/${tarball}; then
   1.376 +				busybox wget ${url%/}/${tarball}
   1.377 +			else
   1.378 +				echo "Toolchain URL is unreachable" && exit 1
   1.379 +			fi 
   1.380 +		fi
   1.381 +		if [ ! -d "${name}-${vers}" ]; then
   1.382 +			echo "Extracting: $tarball"
   1.383 +			tar xjf ${tarball}
   1.384 +		fi
   1.385 +		echo "Copying: ${name}-${vers}/${arch}"
   1.386 +		mkdir -p ${arch}
   1.387 +		cp -a ${name}-${vers}/${arch}/* ${arch} 
   1.388 +		echo "Tools path: /cross/${arch}/tools/bin" ;;
   1.389 +	
   1.390 +	oclock)
   1.391 +		cat << EOT
   1.392 +
   1.393 +$(boldify "Overclocking modes:")
   1.394 +none    700MHz  ARM 250MHz core 400MHz SDRAM 0 overvolt
   1.395 +modest  800MHz  ARM 300MHz core 400MHz SDRAM 0 overvolt
   1.396 +medium  900MHz  ARM 333MHz core 450MHz SDRAM 2 overvolt
   1.397 +high    950MHz  ARM 450MHz core 450MHz SDRAM 6 overvolt
   1.398 +turbo   1000MHz ARM 500MHz core 500MHz SDRAM 6 overvolt
   1.399 +
   1.400 +EOT
   1.401 +		if [ -f "$boot/config.txt" ]; then
   1.402 +			boldify "Current config.txt:"
   1.403 +			cat ${boot}/config.txt
   1.404 +			newline
   1.405 +		fi ;;
   1.406 +	*) usage ;;
   1.407 +esac
   1.408 +exit 0