#!/bin/bash 

source /usr/share/slackdev/buildkit.sh

## ONLY USED FOR MAKING TEST INITRDS ################################


# Create an initial RAM disk for the armv5 
# series.
# This will make its way into the kernel.SlackBuild once 
# Slackware ARM supports it.
#
# 08-May-2009
# mozes@slackware.com
#
# Useful info at
#   http://www.denx.de/wiki/search/DULG/?scope=topic&regex=on&bookview=on&search=\.*
# 
# For bootstrapping, I've built a 2.6.30-rc4 Kernel for armv5 on an existing ARMedslack 12.2
# machine under qemu, then converted its hard disk into one I can mount as loop back
# and rsync its data to a USB stick.
# Check this web site for information about converting QEMU qcow2 formats into 'raw':
#   http://qemu-forum.ipi.fi/viewtopic.php?p=12362
#
#   qemu-img convert -f qcow2 harddisk.img -O raw harddisk.raw
#   modprobe loop
#   losetup /dev/loop1 harddisk.raw
#   fdisk -ul /dev/loop1
#   losetup -d /dev/loop1
#
#   Look at the output of fdisk:
#
#Disk /dev/loop1: 16.1 GB, 16106127360 bytes
#64 heads, 32 sectors/track, 15360 cylinders, total 31457280 sectors
#Units = sectors of 1 * 512 = 512 bytes
#Disk identifier: 0xeb3f32f7
#
#      Device Boot      Start         End      Blocks   Id  System
#/dev/loop1p1              32      411647      205808   82  Linux swap
#/dev/loop1p2   *      411648    31457279    15522816   83  Linux
#
# The only filesystem starts at sector 411648, and the sectors are 512 bytes:
# echo "411648 * 512" | bc
#   210763776
#
#   losetup -o 210763776 /dev/loop1 harddisk.raw
#   mount /dev/loop1 /mnt/floppy
# From here, sync all the data onto the USB stick.
# Following this documentation.  I prefer the Debian method - I'd rather use a USB stick
# than overwrite the SD.
#   http://www.cyrius.com/debian/kirkwood/sheevaplug/unpack.html
#   http://dev.gentoo.org/~armin76/arm/sheevaplug/install.xml
#
# Building Kernel:
#  http://www.plugcomputer.org/plugwiki/index.php/Compiling_Linux_Kernel_for_the_Plug_Computer

TMPBUILD=/tmp/pk-mkinitrd
rm -rf $TMPBUILD/initrd-tree
mkdir -vpm755 $TMPBUILD/initrd-tree
tar xf /usr/share/mkinitrd/initrd-tree.tar.gz -C $TMPBUILD/initrd-tree

VERSION=2.6.33
SLKARCH=armv5

   # Generic requirements:
   # Filesystems:
   INITRDFS="vfat:jbd:jbd2:nls:exportfs:binfmt_misc:md:dm-mod:mbcache:ext2:ext3:ext4:reiserfs:jfs:xfs"
   # Generic SCSI drivers & low-level drivers for discs/media:
   INITRDSCSI="sg:scsi_mod:sd_mod:cdrom:sr_mod:scsi_tgt:mmc_block"
   # Network filesystems:
   INITRDNETFS="nfs:lockd:nfs_common"
   # USB hubs & support mods, including interface devices (USB keyboards etc)
   # followed by some specific device drivers.
   INITRDUSB="usbcore:uhci_hcd:usbhid:ohci_hcd:hid:usb-storage:ehci-hcd:ums-cypress:ums-usbat:ums-freecom:ums-isd200:ums-sddr09:ums-sddr55:ums-alauda:ums-jumpshot:ums-onetouch"

  # Network interface cards:
   INITRDNETDEV="mv643xx_eth"

  # Wait 10 seconds for the USB discs to spin up.  The SheevaPlug's
   # USB recognition can be a bit hit and miss, so it's best to
   # wait for longer than usual.
   mkinitrd \
      -w 10 \
      -k $VERSION-$SLKARCH \
      -s $TMPBUILD/initrd-tree \
      -m $INITRDSCSI:$INITRDUSB:$INITRDFS:$INITRDNETDEV:$INITRDNETFS \
      -o $TMPBUILD/uinitrd-armv5.gz

exit

# Create a uInitrd for U-boot:
cd $TMPBUILD
mkimage \
  -A arm \
  -O linux \
  -T ramdisk \
  -C gzip \
  -n 'Slackware ARM Initial RAM disk' \
  -d $TMPBUILD/uInitrd-armv5.gz \
  /tmp/test-uinitrd.gz
#  $PKGSTORE/../kernels/$SLKARCH/uinitrd-$SLKARCH
#  uInitrd-$SLKARCH
#  $PKGSTORE/../kernels/$SLKARCH/uInitrd-$SLKARCH


# Repack it into a U-Boot image:
# This is how you unpack a uInitrd file:
#dd if=../uInitrd bs=64 skip=1 | gzip -dc | cpio -div
# Repack it - using 'mkimage' from a/u-boot-tools package:
# find . | cpio -o -H newc | gzip -9fv > ../tmprepack.gz 
#mkimage \
#  -T ramdisk \
#  -C gzip \
#  -n 'Slackware ARM Initial RAM disk' \
#  -d ../tmprepack.gz \
#  uInitrd-new

