Advertisement
Guest User

Untitled

a guest
Nov 25th, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.12 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # This file is part of getbootstate
  4. #
  5. # Copyright (C) 2007,2008 Nokia Corporation. All rights reserved.
  6. #
  7. # Contact: Peter De Schrijver <peter.de-schrijver@nokia.com>
  8. #
  9. # This software, including documentation, is protected by copyright controlled by Nokia Corporation. All rights are reserved.
  10. # Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written
  11. # consent of Nokia Corporation. This material also contains confidential information which may not be disclosed to others
  12. # without the prior written consent of Nokia.
  13.  
  14. FS_MODULES="mbcache ext2 jbd ext3"
  15. MODULE_PATH=/lib/modules/`uname -r` #fixed by kernel-power
  16.  
  17. echo_g()
  18. {
  19.     echo "getbootstate: $1"
  20. }
  21.  
  22. init_system()
  23. {
  24.         mount -t proc none /proc
  25.         mount -t sysfs none /sys
  26.         mount -t tmpfs none -o size=512K /tmp
  27.  
  28.         mkdir /tmp/dev
  29.  
  30.  
  31.     # The twl4030 can either be named twl4030-adc or twl4030-madc.
  32.     # So we check for both.
  33.  
  34.     madc_minor=$(grep twl4030-adc /proc/misc | cut -d " " -f 2)
  35.    
  36.     if [ -z $madc_minor ] ; then
  37.         madc_minor=$(grep twl4030-madc /proc/misc | cut -d " " -f 2)
  38.     fi
  39.  
  40.     if [ -n $madc_minor ] ; then
  41.         mknod /tmp/dev/twl4030-adc c 10 $madc_minor
  42.     fi
  43. }
  44.  
  45. fini_system()
  46. {
  47.     if [ "$have_ptys" = "1" ] ; then
  48.         umount /dev/pts
  49.     fi
  50.     umount /sys
  51.     umount /tmp
  52.     # /etc/mtab is linked /proc/mounts, and unmount tries to change
  53.     # /etc/mtab which causes warnings. To prevent them, we unmount /proc
  54.     # last and direct its warning message to /dev/null
  55.     umount /proc 2> /dev/null
  56.     if [ -e /proc/mounts ]; then
  57.         rm -f /proc/mounts # remove stray mounts
  58.     fi
  59. }
  60.  
  61. set_default_root_params() {
  62.         root_dev=mtdblock4
  63.         root_dev_name="Flash"
  64.         root_fstype="jffs2"
  65.         root_fsoptions="rpsize=1024,rpuid=0,rpuid=30000"
  66.         root_mounted=1
  67. }
  68.  
  69. check_partitions()
  70. {
  71.         grep mtdblock4 /proc/partitions >/dev/null
  72.         if [ $? = 0 ] ; then
  73.                 mtd_found=1
  74.         else
  75.                 mtd_found=0
  76.         fi
  77.  
  78.         grep sda1 /proc/partitions >/dev/null
  79.         if [ $? = 0 ] ; then
  80.                 sda_found=1
  81.         else
  82.                 sda_found=0
  83.         fi
  84.  
  85.         grep mmc /proc/partitions >/dev/null
  86.         if [ $? = 0 ] ; then
  87.                 mmc_found=1
  88.         else
  89.                 mmc_found=0
  90.         fi
  91. }
  92.  
  93. search_root_onmmc() {
  94. # if user set up mmc booting, try to find the first mmc device
  95. # which contains /sbin/init, it will be used as root
  96. # also fallback to the mtdblock4 (our flash)
  97.  
  98.         parts=`awk '{print $4}' /proc/partitions | grep 'mmcblk[0-9][0-9]*p[0-9]'`
  99.         [ -z "$parts" ] && return 1
  100.  
  101.         for i in $parts; do
  102.                 if mount_device "$i"; then
  103.                         if [ -x /mnt/new_root/sbin/init ]; then
  104.                                 root_dev="$i"
  105.                                 return 0;
  106.                         fi
  107.                         umount_device "$i"
  108.                 fi
  109.         done
  110.         return 1
  111.  
  112. }
  113.  
  114. show_roots()
  115. {
  116.         echo "Choose bootup option:"
  117.         if [ $mtd_found -eq 1 ]; then
  118.                 echo "  1) Flash"
  119.         fi
  120.         if [ $mmc_found -eq 1 ]; then
  121.                 echo "  2) MMC"
  122.         fi
  123.         if [ $sda_found -eq 1 ]; then
  124.                 echo "  3) USB Mass Storage Device"
  125.         fi
  126.         echo "  9) Start a shell"
  127.         echo ""
  128. }
  129.  
  130. start_pty_system() {
  131.     if [ -e /dev/ptmx ] ; then
  132.         rm /dev/ptmx
  133.     fi
  134.     mknod /dev/ptmx c 5 2
  135.     chmod 666 /dev/ptmx
  136.  
  137.     if [ ! -d /dev/pts ] ; then
  138.         mkdir /dev/pts
  139.     fi
  140.     mount -t devpts devpts -o gid=5,mode=620 /dev/pts
  141.  
  142.     have_ptys=1
  143. }
  144.  
  145. start_shell()
  146. {
  147.     # Make sure we have PTY system supported, so that we would support ssh
  148.     # connections
  149.     start_pty_system
  150.  
  151.         echo "Starting shell"
  152.  
  153.     # We use getty instead of just running /bin/sh because we want to have
  154.     # job control working (e.g., we want Ctrl-C). With /bin/sh this does
  155.     # not work, not sure why. Note, "-n -l /bin/sh" options make sure
  156.     # that username/passeord are not asked for.
  157.     /sbin/getty 115200 ttyS0 -n -l /bin/sh
  158. }
  159.  
  160. show_rd_versions()
  161. {
  162.         bg_color=0xffff
  163.         y=0
  164.         x=0
  165.         line_height=20
  166.  
  167.         text2screen -c -x 0 -y 0 -w 800 -h 160
  168.         text2screen -t "Kernel version:" -s 2 -x $x -y $y -B $bg_color
  169.         y=$(expr $y + $line_height)
  170.         text2screen -t "  `uname -r` " -s 2 -x $x -y $y -B $bg_color
  171.         y=$(expr $y + $line_height)
  172.         text2screen -t "  `uname -v`" -s 2 -x $x -y $y -B $bg_color
  173.  
  174.         y=$(expr $y + $line_height + $line_height)
  175.         text2screen -t " no initfs \o/" -s 2 -x $x -y $y -B $bg_color
  176.  
  177.         y=340
  178.         text2screen -c -y 320 -x 0 -w 320 -h 120
  179.         text2screen -t "Component versions:" -x $x -y $y -s 2 -B $bg_color
  180.         y=$(expr $y + $line_height)
  181.         while read line ; do
  182.                 text2screen -t "  $line" -x $x -y $y -s 2 -B $bg_color
  183.                 y=$(expr $y + $line_height)
  184.         done < /proc/component_version
  185. }
  186.  
  187. load_mmc_modules()
  188. {
  189.         for mod in $FS_MODULES ; do
  190.                 if [ -f $MODULE_PATH/$mod.ko ]; then
  191.                         insmod $MODULE_PATH/$mod.ko
  192.                 fi
  193.         done
  194. }
  195.  
  196. prepare_core() {
  197.     if [ -f /etc/osso_software_version ] ; then
  198.         release=`cat /etc/osso_software_version`
  199.         echo "/media/mmc1/core-dumps/%e-%s-%p-$release.core" >/proc/sys/kernel/core_pattern
  200.     fi
  201.     ulimit -c unlimited
  202. }
  203.  
  204. mount_device() {
  205.     echo_g "Mounting $1"
  206.         root_dev="$1"
  207.  
  208.         [ -z "$root_dev" ] && return 1
  209.     if [ ! -e /mnt/new_root ]; then
  210.         mkdir -p /mnt/new_root 
  211.     fi
  212.  
  213.         if [ -z $root_fstype ]; then
  214.                 if [ -z $root_fsoptions ]; then
  215.                         time mount /dev/$root_dev /mnt/new_root
  216.                 else
  217.                         time mount -o $root_fsoptions /dev/$root_dev /mnt/new_root
  218.                 fi
  219.         else
  220.                 if [ -z $root_fsoptions ]; then
  221.                         time mount -t $root_fstype /dev/$root_dev /mnt/new_root
  222.                 else
  223.                         time mount -t $root_fstype -o $root_fsoptions /dev/$root_dev /mnt/new_root
  224.                 fi
  225.         fi
  226.         grep -q new_root /proc/mounts
  227. }
  228.  
  229. umount_device() {
  230.          grep -q "$1"  /proc/mounts  && umount /dev/$1
  231. }
  232.  
  233. mount_new_root()
  234. {
  235.         if ! grep -q new_root /proc/mounts; then
  236.                 mount_device "$root_dev"
  237.         fi
  238. }
  239.  
  240. enter_state()
  241. {
  242.     echo active > /sys/devices/platform/gpio-switch/cmt_bsi/state
  243.     STATE=`getbootstate`
  244.     echo_g "Entering state '$STATE'."
  245.     case $STATE in
  246.         BOOT)
  247.             def_runlevel=6
  248.             ;;
  249.         USER|FLASH)
  250.             def_runlevel=2
  251.             ;;
  252.         ACT_DEAD)
  253.             def_runlevel=5
  254.             ;;
  255.         LOCAL|TEST)
  256.             def_runlevel=3
  257.             ;;
  258.         SHUTDOWN)
  259.             def_runlevel=0
  260.             ;;
  261.         *)
  262.                     if [ $rd_mode -ne 0 ]; then
  263.                     start_shell
  264.                 return
  265.                     fi
  266.             echo_g "Houston, we have a problem, powering off..."
  267.             text2screen -c -x 0 -y 0 -h 80 -w 800
  268.             text2screen -t "Malfunction !" -x 0 -y 0 -s 4
  269.             text2screen -t "Device shutdown in 10s" -x 0 -y 20 -s 4
  270.             sleep 10
  271.             poweroff
  272.             def_runlevel=0
  273.             ;;
  274.     esac
  275. }
  276.  
  277. boot()
  278. {
  279.         if [ $root_mounted -ne 1 ]; then
  280.                 mount_new_root
  281.                 if [ $? -ne 0 ]; then
  282.                         return 1
  283.                 fi
  284.                 cd /mnt/new_root
  285.                 if [ ! -d mnt/initfs ]; then
  286.                         mkdir -p mnt/initfs
  287.                 fi
  288.         fini_system
  289.         pivot_root . mnt/initfs
  290.         CHROOT="/usr/sbin/chroot ."
  291.     else
  292.                 if [ ! -x /sbin/init ]; then
  293.                         echo_g "/sbin/init not found on root device"
  294.                         return 1
  295.                 fi
  296.         fini_system
  297.     fi
  298.    
  299.     # let me see then, what thereat is and this mistery explore
  300.     /sbin/fiasco-do-update
  301.  
  302.     exec $CHROOT /sbin/init $def_runlevel <dev/console >dev/console 2>&1
  303. }
  304.  
  305. read_cfg_from_file()
  306. {
  307.     CFG=/etc/pmconfig
  308.  
  309.     awk -f /sbin/read_cfg.awk $CFG
  310. }
  311.  
  312. init_system
  313.  
  314. modprobe fbcon
  315.  
  316. read_cfg_from_file
  317.  
  318. # set UART timeout to 10s
  319. echo 10 > /sys/devices/platform/serial8250.0/sleep_timeout
  320.  
  321. # Change ownership to allow locking for pulseaudio
  322. chown pulse:pulse /sys/power/vdd2_lock
  323.  
  324. serial_console=0
  325. rd_mode=$(cal-tool --get-rd-mode 2> /dev/null)
  326. if [ "$rd_mode" = "enabled" ]; then
  327.         rd_mode=1
  328.         echo_g "R&D mode enabled"
  329.         show_rd_versions
  330.     if [ $(cal-tool -f | sed 's/,/\n/g' | grep serial-console) = "serial-console" ] ; then
  331.         serial_console=1;
  332.     fi
  333.     # enable sleep mode indication leds
  334.     echo active > /sys/devices/platform/gpio-switch/sleep_ind/state
  335.     awk '/sleep_ind/{ if ($2 == 1) { VALUE="active"; } else { VALUE="inactive"; } print VALUE > "/sys/devices/platform/gpio-switch/sleep_ind/state"  }' < /etc/pmconfig
  336. else
  337.         rd_mode=0
  338. fi
  339.  
  340. prepare_core
  341.  
  342. enter_state
  343.  
  344.  
  345. want_menu=0
  346. root_mounted=0
  347. if [ $serial_console -ne 0 ]; then
  348.         echo_g "Press any key for interactive startup"
  349.         key_pressed 1
  350.         if [ $? -ne 0 ]; then
  351.                 want_menu=1
  352.         fi
  353. fi
  354.  
  355. if [ $want_menu -ne 1 ]; then
  356.         default_root=$(cal-tool --get-root-device 2> /dev/null)
  357.         set_default_root_params
  358.         SLIDE_STATE=`cat /sys/devices/platform/gpio-switch/slide/state`
  359.         if [ x"$SLIDE_STATE" = "xopen" ] ; then
  360.         echo_g "slide open, attempting to use bootmenu"
  361.         [ -f /bootmenu.sh ] && . /bootmenu.sh
  362.         fi
  363.                                  
  364.         case "$default_root" in
  365.         mmc)
  366.                 load_mmc_modules
  367.                 if search_root_onmmc; then
  368.                         root_dev_name="MMC"
  369.                         root_dev_set=1
  370.                         root_fstype=""
  371.                         root_fsoptions=""
  372.                 else
  373.                         echo_g "No mmc root found, try default"
  374.                         set_default_root_params
  375.                 fi
  376.                 ;;
  377.         usb)
  378.                 root_dev=sda1
  379.                 root_dev_name="USB hard drive"
  380.                 root_dev_set=1
  381.                 root_fstype=""
  382.                 root_fsoptions=""
  383.                 ;;
  384.         esac
  385.         boot
  386. fi
  387.  
  388. root="invalid"
  389. while [ true ]; do
  390.         check_partitions
  391.         show_roots
  392.         root_dev_set=0
  393.         read root
  394.         case "$root" in
  395.             1)
  396.                 set_default_root_params
  397.             root_dev_set=1
  398.                 ;;
  399.             2)
  400.                 if search_root_onmmc; then
  401.                         root_dev_name="MMC"
  402.                         root_dev_set=1
  403.                         root_fstype=""
  404.                         root_fsoptions=""
  405.                 else
  406.                         echo_g "Could not find suitable partition for root on mmc"
  407.                 fi
  408.                 ;;
  409.             3)
  410.                 root_dev=sda1
  411.                 root_dev_name="USB hard drive"
  412.                 root_dev_set=1
  413.                 root_fstype=""
  414.                 root_fsoptions=""
  415.                 ;;
  416.             9)
  417.                 start_shell
  418.                 ;;
  419.             *)
  420.                 echo_g "Invalid selection"
  421.                 ;;
  422.         esac
  423.         if [ $root_dev_set -eq 1 ]; then
  424.                 boot
  425.         fi
  426. done
  427.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement