neochapay

Untitled

Sep 13th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.87 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Hybris adaptation bootstrapping initramfs init script.
  4. #
  5. # Copyright (c) 2014 Jolla Oy
  6. #
  7. # This program is free software; you can redistribute it and/or modify it under
  8. # the terms of the GNU General Public License version 2 as published by the
  9. # Free Software Foundation.
  10. #
  11. # Authors:
  12. #   - Tom Swindell <t.swindell@rubyx.co.uk>
  13. #   - David Greaves <david@dgreaves.com>
  14. #
  15.  
  16. # This init script runs in early boot in initrd and does a switch_root
  17. # to the real rootfs. It can also be copied into rootfs and provide
  18. # monitoring of the boot process there too.
  19.  
  20. # Be careful - you can't run any external commands until busybox has installed.
  21.  
  22. # General logging
  23.  
  24. set -x
  25. exec > /init.log 2>&1
  26. echo "Running Mer Boat Loader"
  27.  
  28. DEFAULT_OS="sailfishos"
  29. DATA_PARTITION=/dev/block/mmcblk0p25
  30. TELNET_DEBUG_PORT=2323
  31. DONE_SWITCH=no
  32. USB_FUNCTIONS=rndis
  33. ANDROID_USB=/sys/class/android_usb/android0
  34. LOCAL_IP=192.168.2.15
  35. EXPLICIT_BUSYBOX="/bin/busybox-static"
  36.  
  37. export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
  38.  
  39.  
  40. ###########################################
  41. #                                         #
  42. #               CORE FUNCTIONS            #
  43. #                                         #
  44. ###########################################
  45.  
  46.  
  47. set_welcome_msg(){
  48. cat <<EOF > /etc/issue.net
  49. Welcome to the Mer/SailfishOS Boat loader debug init system.
  50.  
  51. Log so far is in /init.log
  52.  
  53. To make post-switch_root halt before starting systemd, perform:
  54. EOF
  55. if [ "$DONE_SWITCH" = "no" ]; then
  56. cat <<EOF >> /etc/issue.net
  57. touch /target/init_enter_debug2
  58. EOF
  59. else
  60. cat <<EOF >> /etc/issue.net
  61. touch /init_enter_debug2
  62. EOF
  63. fi
  64. cat <<EOF >> /etc/issue.net
  65. (When run post-switch_root, telnet is on port 2323, not 23)
  66.  
  67. EOF
  68.  
  69. HALT_BOOT="${1:-y}"
  70. if [ "$HALT_BOOT" = "y" ]; then
  71. cat <<EOF >> /etc/issue.net
  72. You may inject commands into init shell process (PID 1):
  73.  
  74. To see output of commands as they're injected:
  75. tail -f /init.log &
  76. To run a command:
  77. echo "ls -l /" >/init-ctl/stdin
  78.  
  79. (Be careful if you experiment with exec as you need to terminate
  80. daemons and disable busybox hotplug handling)
  81.  
  82. To allow init to continue:
  83. echo "continue" >/init-ctl/stdin
  84.  
  85. EOF
  86. fi
  87.  
  88. if [ "$DONE_SWITCH" = "no" ]; then
  89.    cat <<EOF >> /etc/issue.net
  90. In order to work safely with the device's mmc you should
  91. echo "umount_stowaways" >/init-ctl/stdin
  92.  
  93. Then you can mount and modify exported mass storage on host. When done
  94. echo "mount_stowaways" >/init-ctl/stdin
  95.  
  96.  
  97. EOF
  98. fi
  99. }
  100.  
  101. get_opt() {
  102.   for param in $(cat /proc/cmdline); do
  103.     echo "$param" | grep "^$1=*" | cut -d'=' -f2
  104.   done
  105. }
  106.  
  107. do_mount_devprocsys()
  108. {
  109.     echo "########################## mounting devprocsys"
  110.  #   mkdir /dev
  111.  #   mount -t devtmpfs devtmpfs /dev
  112.     # telnetd needs /dev/pts/ entries
  113.     mkdir /dev/pts
  114.     mount -t devpts devpts /dev/pts
  115.  
  116.     mkdir /proc
  117.     mkdir /sys
  118.     mount -t sysfs sysfs /sys
  119.     mount -t proc proc /proc
  120. }
  121.  
  122. do_hotplug_scan()
  123. {
  124.     echo /sbin/mdev > /proc/sys/kernel/hotplug
  125.     mdev -s
  126.     # There is no way to know when all hotplug events have been processed :(
  127.     sleep 2
  128. }
  129.  
  130.  
  131. mount_stowaways() {
  132.     echo "########################## mounting stowaways"
  133.  
  134.     rm -rf /data
  135.     rm -rf /target
  136.    
  137.     mkdir /data
  138.     mkdir /target
  139.    
  140.     mount $DATA_PARTITION /data
  141.     mount --bind /data/.stowaways/${DEFAULT_OS} /target
  142.     mkdir /target/data # in new fs
  143.     mount --bind /data /target/data
  144.  
  145.     mkdir /data/omp
  146. }
  147.  
  148. write() {
  149.   echo -n "$2" > "$1"
  150. }
  151.  
  152. umount_stowaways() {
  153.     if [ ! -z $DATA_PARTITION ]; then
  154.     umount /target/data
  155.     umount /target
  156.     umount /data
  157.     fi
  158. }
  159.  
  160. inject_loop() {
  161.     INJ_DIR=/init-ctl
  162.     INJ_STDIN=$INJ_DIR/stdin
  163.  
  164.     mkdir $INJ_DIR
  165.     mkfifo $INJ_STDIN
  166.     echo "This entire directory is for debugging init - it can safely be removed" > $INJ_DIR/README
  167.  
  168.     echo "########################## Beginning inject loop"
  169.     while : ; do
  170.         while read IN; do
  171.         if [ "$IN" = "continue" ]; then break 2;fi
  172.             $IN
  173.         done <$INJ_STDIN
  174.     done
  175.     rm -rf $INJ_DIR # Clean up if we exited nicely
  176.     echo "########################## inject loop done"
  177. }
  178.  
  179. usb_setup() {
  180.       write $ANDROID_USB/enable        0
  181.       write $ANDROID_USB/functions     ""
  182.       write $ANDROID_USB/enable        1
  183.       usleep 500000 # 0.5 delay to attempt to remove rndis function
  184.       write $ANDROID_USB/enable        0
  185.       write $ANDROID_USB/idVendor      18D1
  186.       write $ANDROID_USB/idProduct     D001
  187.       write $ANDROID_USB/iManufacturer "Mer Boat Loader"
  188.       write $ANDROID_USB/iProduct      "$CUSTOMPRODUCT"
  189.       write $ANDROID_USB/iSerial       "$1"
  190.       write $ANDROID_USB/functions     $USB_FUNCTIONS
  191.       write $ANDROID_USB/enable        1
  192. }
  193.  
  194.  
  195.  
  196. run_debug_session() {
  197.     CUSTOMPRODUCT=$1
  198.     echo "########################## Debug session : $1"
  199.     usb_setup "Mer Debug setting up (DONE_SWITCH=$DONE_SWITCH)"
  200.  
  201.     USB_IFACE=notfound
  202.     /bin/ifconfig rndis0 $LOCAL_IP && USB_IFACE=rndis0
  203.     if [ x$USB_IFACE = xnotfound ]; then
  204.         /bin/ifconfig usb0 $LOCAL_IP && USB_IFACE=usb0
  205.     fi
  206.     # Report for the logs
  207.     /bin/ifconfig -a > /data/omp/ifconfig.log  2>&1
  208.  
  209.     # Unable to set up USB interface? Reboot.
  210.     if [ x$USB_IFACE = xnotfound ]; then
  211.         usb_info "Mer Debug: ERROR: could not setup USB as usb0 or rndis0"
  212.         #dmesg
  213.         #sleep 60 # plenty long enough to check usb on host
  214.         #reboot -f
  215.     fi
  216.    
  217.     # Create /etc/udhcpd.conf file.
  218.     echo "start 192.168.2.20" > /etc/udhcpd.conf
  219.     echo "end 192.168.2.90" >> /etc/udhcpd.conf
  220.     echo "lease_file /var/udhcpd.leases" >> /etc/udhcpd.conf
  221.     echo "interface $USB_IFACE" >> /etc/udhcpd.conf
  222.     echo "option subnet 255.255.255.0" >> /etc/udhcpd.conf
  223.  
  224.     # Be explicit about busybox so this works in a rootfs too
  225.     echo "########################## starting dhcpd"
  226.     $EXPLICIT_BUSYBOX udhcpd
  227.  
  228.     cp /init.log /data/omp/init1.log
  229.    
  230.     HALT_BOOT="${2:-y}"
  231.     set_welcome_msg $HALT_BOOT
  232.     # Non-blocking telnetd
  233.     echo "########################## starting telnetd"
  234.     # We run telnetd on different ports pre/post-switch_root This
  235.     # avoids problems with an unterminated pre-switch_root telnetd
  236.     # hogging the port
  237.     $EXPLICIT_BUSYBOX telnetd -b ${LOCAL_IP}:${TELNET_DEBUG_PORT} -l /bin/sh > /data/omp/telnet.log  2>&1
  238.  
  239.     cp /init.log /data/omp/init2.log
  240.    
  241.     # For some reason this does not work in rootfs
  242.     usb_info "Mer Debug telnet on port $TELNET_DEBUG_PORT on $USB_IFACE $LOCAL_IP - also running udhcpd"
  243.  
  244.     if [ "$HALT_BOOT" = "y" ]; then
  245.         # Some logging output
  246.         ps -wlT
  247.         ps -ef
  248.         netstat -lnp
  249.         cat /proc/mounts
  250.         sync
  251.  
  252.         cp /init.log /data/omp/init3.log
  253.        
  254.         # Run command injection loop = can be exited via 'continue'
  255.         #inject_loop
  256.     fi
  257.    
  258.     cp /init.log /data/omp/init4.log
  259. }
  260.  
  261. check_kernel_config() {
  262.     echo "Checking kernel config"
  263.     if [ ! -e /proc/config.gz ]; then
  264.     echo "No /proc/config.gz. Enable CONFIG_IKCONFIG and CONFIG_IKCONFIG_PROC" >> /diagnosis.log
  265.     else
  266.     # Must be =y
  267.     for x in CONFIG_CGROUPS CONFIG_AUTOFS4_FS CONFIG_DEVTMPFS_MOUNT CONFIG_DEVTMPFS CONFIG_UNIX CONFIG_HOTPLUG CONFIG_INOTIFY_USER CONFIG_SYSVIPC CONFIG_NET CONFIG_PROC_FS CONFIG_SIGNALFD CONFIG_SYSFS CONFIG_TMPFS_POSIX_ACL CONFIG_VT; do
  268.     zcat /proc/config.gz | grep -E "^$x=y\$" || echo "$x=y not found in /proc/config.gz" >> /diagnosis.log
  269.     done
  270.     # Must not be =y
  271.     for x in CONFIG_ANDROID_LOW_MEMORY_KILLER CONFIG_ANDROID_PARANOID_NETWORK CONFIG_DUMMY CONFIG_SYSFS_DEPRECATED; do
  272.     zcat /proc/config.gz | grep -E "^$x=y\$" && echo "$x=y found in /proc/config.gz, must be disabled" >> /diagnosis.log
  273.     done
  274.     fi
  275. }
  276.  
  277. usb_info() {
  278.     # make sure USB is settled
  279.     echo "########################## usb_info: $1"
  280.     sleep 1
  281.     write $ANDROID_USB/iSerial       "$1"
  282. }
  283.  
  284. bootsplash() {
  285.     zcat /bootsplash.gz > /dev/graphics/fb0
  286. }
  287.  
  288. ############### OLD STUFF ################
  289. #
  290. # if [ "$DONE_SWITCH" = "no" ]; then
  291. EXPLICIT_BUSYBOX=""
  292.  
  293. do_mount_devprocsys
  294. mount_stowaways
  295.  
  296. # if [ -d /data/.stowaways ]; then
  297. #   #cat /dev/block/mmcblk0p25 > /dev/graphics/fb0
  298. #   dmesg > /target/dmesg.out
  299. #   exec switch_root /target /custominit --log-level=debug --log-target=kmsg &> /target/init-stderrout 2>&1
  300. # else
  301.     run_debug_session "Failed to boot init in real rootfs"
  302. # fi
  303.  
  304. cp /init.log /data/omp/init.log
  305.  
  306.  
  307. #exec switch_root /target /sbin/init --log-level=debug --log-target=kmsg &> /target/init-stderrout
  308.  
  309. #
  310. #     # No target debug unless we debug here too (for now)
  311. #
  312. #     DBG_REASON=""
  313. #     [ -e /diagnosis.log ] && DBG_REASON="Refusing to boot. See /diagnosis.log (in initrd only)"
  314. #     [ "$(get_opt bootmode)" = "debug" ] && DBG_REASON="bootmode=debug on kernel command line"
  315. #     [ x$ALWAYSDEBUG = x1 ] && DBG_REASON="Always debug: rndis + mass_storage"
  316. #     [ -f /target/init_enter_debug ] && DBG_REASON="/init_enter_debug exists"
  317. #
  318. #     if ! [ "$DBG_REASON" = "" ] ; then
  319. #     # During debug we export mmc too (some variations in location here)
  320. #     lun=/sys/class/android_usb/f_mass_storage/lun/file
  321. #     if [ -f $lun ]; then echo /dev/mmcblk0 > $lun; fi
  322. #     lun=/sys/class/android_usb/f_mass_storage/lun0/file
  323. #     if [ -f $lun ]; then echo /dev/mmcblk0 > $lun; fi
  324. #     USB_FUNCTIONS=rndis,mass_storage
  325.  
  326.  
  327. #
  328. #     # Tidy up before we switch_root (rootfs init-debug leaves these running during bootup)
  329. #     killall telnetd
  330. #     killall udhcpd
  331. #
  332. #     USB_FUNCTIONS=rndis
  333. #     usb_setup "Mer Debug: done debug, disabling storage"
  334. #     fi
  335. #
  336. #     # Disable mdev hotplug now - let udev handle it in main boot
  337. #     echo "" > /proc/sys/kernel/hotplug
  338. #
  339. #     if [ -f "/target/init-debug" ]; then
  340. #     exec switch_root /target /init-debug &> /target/init-debug-stderrout
  341. #     else
  342. #     # Prefer /sbin/preinit over /sbin/init
  343. #     [ -x /target/sbin/preinit ] && INIT=/sbin/preinit || INIT=/sbin/init
  344. #     if [ -x "/target$INIT" ]; then
  345. #         exec switch_root /target $INIT --log-level=debug --log-target=kmsg &> /target/init-stderrout
  346. #     fi
  347. #     fi
  348. #     run_debug_session "Failed to boot init in real rootfs"
  349. #
  350. # else
  351. #     # We're in the real rootfs running as init-debug
  352. #     EXPLICIT_BUSYBOX="/bin/busybox-static"
  353. #     TELNET_DEBUG_PORT=2323
  354. #
  355. #     do_mount_devprocsys
  356. #
  357. #     HALT_BOOT="n"
  358. #     [ -f /init_enter_debug2 ] && HALT_BOOT="y"
  359. #     run_debug_session "init-debug in real rootfs" $HALT_BOOT
  360. #
  361. #     # If we don't do this then udev will not be able to create /dev/block/*
  362. #     rm /dev/block
  363. #
  364. #     # Now try to boot the real init
  365. #     # Prefer /sbin/preinit over /sbin/init
  366. #     [ -x /sbin/preinit ] && INIT=/sbin/preinit || INIT=/sbin/init
  367. #     exec $INIT --log-level=debug --log-target=kmsg &> /boot/systemd_stdouterr
  368. #     run_debug_session "init in real rootfs failed"
  369. # fi
Add Comment
Please, Sign In to add comment