#!/bin/ash
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Zyxel NSA310 u-Boot env setup and linux installer - stage 2
# by Michal Osowiecki
#
# This script will format /dev/sda2 partition,extract linux archive
# to /dev/sda2,modify uBoot env and restart box
#
# You have to place these files in root of your usb stick
# uImage - linux kernel
# initrd.gz - second stage initrd running nsa310.sh
# usb_key_func.sh - autorun script
# nsa310_check_file_C0 - usb_key_func.sh verification file
# checksumfile - checksum file
#
# nsa310.sh - stage2 script - run from initrd
# feel free to modify it for your distribution
# linux.tgz - stage2 gzipped tar of your linux distributio
echo "Formatting data partition..."
mkfs.ext4 /dev/sda2
mount /dev/sda1 /mnt/sda1
mount /dev/sda2 /mnt/sda2
mount /dev/sdb1 /mnt/sdb1
cp /mnt/sdb1/uImage /mnt/sda1/
cd /mnt/sda2
echo "Extracting linux..."
tar -xzf /mnt/sdb1/linux.tgz
cd /
sync
umount /dev/sda1
umount /dev/sda2
umount /dev/sdb1
echo "Setting uBoot env.."
fw_setenv arcNumber 4022
fw_setenv mainlineLinux yes
fw_setenv bootargs 'console=ttyS0,115200 root=/dev/sda2'
fw_setenv bootcmd 'ide reset;ext2load ide 0:1 0x800000 /uImage;bootm 0x800000'
sleep 3
reboot -f