init 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. #!/bin/bash
  2. # ============================================================
  3. # This is the standard Ubuntu initramfs initialization process
  4. # ============================================================
  5. # Default PATH differs between shells, and is not automatically exported
  6. # by klibc dash. Make it consistent.
  7. export PATH=/sbin:/usr/sbin:/bin:/usr/bin
  8. [ -d /dev ] || mkdir -m 0755 /dev
  9. [ -d /root ] || mkdir -m 0700 /root
  10. [ -d /sys ] || mkdir /sys
  11. [ -d /proc ] || mkdir /proc
  12. [ -d /tmp ] || mkdir /tmp
  13. mkdir -p /var/lock
  14. mount -t sysfs -o nodev,noexec,nosuid sysfs /sys
  15. mount -t proc -o nodev,noexec,nosuid proc /proc
  16. # Some things don't work properly without /etc/mtab.
  17. ln -sf /proc/mounts /etc/mtab
  18. echo "1 1 1 1" > /proc/sys/kernel/printk
  19. grep -q '\<quiet\>' /proc/cmdline || echo "Loading, please wait..."
  20. # Note that this only becomes /dev on the real filesystem if udev's scripts
  21. # are used; which they will be, but it's worth pointing out
  22. if ! mount -t devtmpfs -o nosuid,mode=0755 udev /dev; then
  23. echo "W: devtmpfs not available, falling back to tmpfs for /dev"
  24. mount -t tmpfs -o nosuid,mode=0755 udev /dev
  25. [ -e /dev/console ] || mknod -m 0600 /dev/console c 5 1
  26. [ -e /dev/null ] || mknod /dev/null c 1 3
  27. fi
  28. mkdir /dev/pts
  29. mount -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /dev/pts || true
  30. mount -t tmpfs -o "noexec,nosuid,size=80%,mode=0755" tmpfs /run
  31. mkdir -m 0755 /run/initramfs
  32. # Export the dpkg architecture
  33. export DPKG_ARCH=
  34. . /conf/arch.conf
  35. # Set modprobe env
  36. export MODPROBE_OPTIONS="-qb"
  37. # Export relevant variables
  38. export ROOT=
  39. export ROOTDELAY=
  40. export ROOTFLAGS=
  41. export ROOTFSTYPE=
  42. export IP=
  43. export BOOT=
  44. export BOOTIF=
  45. export UBIMTD=
  46. export break=
  47. export init=/sbin/init
  48. export quiet=y
  49. export readonly=y
  50. export rootmnt=/root
  51. export debug=
  52. export panic=
  53. export blacklist=
  54. export resume=
  55. export resume_offset=
  56. export drop_caps=
  57. export fastboot=n
  58. export forcefsck=n
  59. export fsckfix=
  60. export recovery=
  61. export BOOT_CARDTYPE="unknown"
  62. export OTHER_CARDTYPE="unknown"
  63. export BOOT_CARDDEV="9"
  64. export OTHER_CARDDEV="9"
  65. export CARD_TYPE="SD"
  66. # mdadm needs hostname to be set. This has to be done before the udev rules are called!
  67. if [ -f "/etc/hostname" ]; then
  68. /bin/hostname -F /etc/hostname >/dev/null 2>&1
  69. fi
  70. # Bring in the main config
  71. . /conf/initramfs.conf
  72. for conf in conf/conf.d/*; do
  73. [ -f ${conf} ] && . ${conf}
  74. done
  75. . /scripts/functions
  76. echo "1 1 1 1" > /proc/sys/kernel/printk
  77. BOOTDELAY=60
  78. DEFAULTBOOT="1"
  79. # Parse command line options
  80. for x in $(cat /proc/cmdline); do
  81. case $x in
  82. boot_card_type=*)
  83. BOOT_CARDTYPE=${x#boot_card_type=}
  84. ;;
  85. boot_card_dev=*)
  86. BOOT_CARDDEV=${x#boot_card_dev=}
  87. ;;
  88. cardtype=*)
  89. CARD_TYPE=${x#cardtype=}
  90. ;;
  91. boot_delay=*)
  92. BOOTDELAY="${x#boot_delay=}"
  93. case ${BOOTDELAY} in
  94. *[![:digit:].]*)
  95. BOOTDELAY=60
  96. ;;
  97. esac
  98. ;;
  99. default_boot=*)
  100. DEFAULTBOOT="${x#default_boot=}"
  101. ;;
  102. quiet)
  103. quiet=y
  104. ;;
  105. ro)
  106. readonly=y
  107. ;;
  108. rw)
  109. readonly=n
  110. ;;
  111. break=*)
  112. break=${x#break=}
  113. ;;
  114. break)
  115. break=premount
  116. ;;
  117. esac
  118. done
  119. # Default to BOOT=local if no boot script defined.
  120. if [ -z "${BOOT}" ]; then
  121. BOOT=local
  122. fi
  123. if [ -n "${noresume}" ]; then
  124. export noresume
  125. unset resume
  126. else
  127. resume=${RESUME:-}
  128. fi
  129. maybe_break top
  130. # export BOOT variable value for compcache,
  131. # so we know if we run from casper
  132. export BOOT
  133. # Don't do log messages here to avoid confusing graphical boots
  134. run_scripts /scripts/init-top
  135. #maybe_break modules
  136. #[ -n "${netconsole}" ] && modprobe netconsole netconsole="${netconsole}"
  137. load_modules
  138. maybe_break premount
  139. run_scripts /scripts/init-premount
  140. maybe_break mount
  141. # Always load local and nfs (since these might be needed for /etc or
  142. # /usr, irrespective of the boot script used to mount the rootfs).
  143. . /scripts/local
  144. . /scripts/nfs
  145. . /scripts/${BOOT}
  146. parse_numeric ${ROOT}
  147. maybe_break mountroot
  148. mount_top
  149. mount_premount
  150. #mountroot
  151. if read_fstab_entry /usr; then
  152. mountfs /usr
  153. fi
  154. # Mount cleanup
  155. mount_bottom
  156. nfs_bottom
  157. local_bottom
  158. chown 0:0 /bin/*
  159. chown 0:0 /sbin/*
  160. echo "1 1 1 1" > /proc/sys/kernel/printk
  161. sleep 1
  162. # === Variables ===
  163. [ ! $BOOTDELAY -gt 4 ] && BOOTDELAY=5
  164. export SCRIPT_VERSION="v2.3"
  165. export O_BOARD="OD"
  166. export ODROID_DTB="odroid.dtb"
  167. export ODROID_FSTAB="fstab.odroid"
  168. export ODROID_IMAGE="Kernel"
  169. export ODROID_ASYSTEMIMAGE="androsystem.img"
  170. export BKP_PREFIX="OD"
  171. export NUMCARDS="1"
  172. export UPGRADE_ANDROID="no"
  173. export ASYSTEM_PART="2"
  174. export ADATA_PART="5"
  175. export ACACHE_PART="3"
  176. export ADATA_PART_ORIG="3"
  177. export ACACHE_PART_ORIG="4"
  178. export LINUX_PART="7"
  179. export LINUX2_PART="8"
  180. export OELEC_PART="6"
  181. _desc=""
  182. _ini_prefix="multiboot"
  183. _actmount="mnt1"
  184. _numos=0
  185. _menu_no=49
  186. _menu_key="1"
  187. _menuitem_1=""
  188. _menuitem_2=""
  189. _menuitem_3=""
  190. _menuitem_4=""
  191. _menuitem_5=""
  192. _menuitem_6=""
  193. _menuitem_7=""
  194. _menuitem_8=""
  195. _mndscr_1=""
  196. _mndscr_2=""
  197. _mndscr_3=""
  198. _mndscr_4=""
  199. _mndscr_5=""
  200. _mndscr_6=""
  201. _mndscr_7=""
  202. _mndscr_8=""
  203. _mncrd_1="1"
  204. _mncrd_2="1"
  205. _mncrd_3="1"
  206. _mncrd_4="1"
  207. _mncrd_5="1"
  208. _mncrd_6="1"
  209. _mncrd_7="1"
  210. _mncrd_8="1"
  211. _boot_delay=2
  212. _defboot="none"
  213. _defdescr="none"
  214. _defcard="1"
  215. boot_fail="yes"
  216. # =========================================================
  217. # === Set the variables depending on used Odroid board ===
  218. if [ -f /ODROID_C2 ]; then
  219. O_BOARD="C2"
  220. ADATA_PART_ORIG="4"
  221. ACACHE_PART_ORIG="3"
  222. ODROID_DTB="meson64_odroidc2.dtb"
  223. ODROID_FSTAB="fstab.odroidc2"
  224. ODROID_IMAGE="Image"
  225. ODROID_ASYSTEMIMAGE="rootsystem.img"
  226. BKP_PREFIX="C2"
  227. BOOT_CARDDEV="0"
  228. BOOT_CARDTYPE="${CARD_TYPE}"
  229. if [ -b /dev/mmcblk1 ]; then
  230. BOOT_CARDTYPE="EMMC"
  231. NUMCARDS="2"
  232. OTHER_CARDDEV="1"
  233. OTHER_CARDTYPE="SD"
  234. fi
  235. elif [ -f /ODROID_XU ]; then
  236. O_BOARD="XU"
  237. ADATA_PART_ORIG="3"
  238. ACACHE_PART_ORIG="4"
  239. ODROID_DTB="exynos5422-odroidxu3.dtb"
  240. ODROID_FSTAB="fstab.odroidxu3"
  241. ODROID_IMAGE="zImage"
  242. ODROID_ASYSTEMIMAGE="system.img"
  243. BKP_PREFIX="XU"
  244. if [ "${BOOT_CARDTYPE}" = "none" ]; then
  245. BOOT_CARDTYPE="SD"
  246. if [ -b /dev/mmcblk1 ]; then
  247. NUMCARDS="2"
  248. BOOT_CARDDEV="1"
  249. OTHER_CARDDEV="0"
  250. OTHER_CARDTYPE="EMMC"
  251. else
  252. BOOT_CARDDEV="0"
  253. fi
  254. else
  255. [ "${BOOT_CARDTYPE}" = "eMMC" ] && BOOT_CARDTYPE="EMMC"
  256. if [ "${BOOT_CARDDEV}" = 1 ]; then
  257. NUMCARDS="2"
  258. if [ "${BOOT_CARDTYPE}" = "SD" ]; then
  259. OTHER_CARDDEV="0"
  260. OTHER_CARDTYPE="EMMC"
  261. else
  262. OTHER_CARDDEV="1"
  263. OTHER_CARDTYPE="SD"
  264. BOOT_CARDDEV="0"
  265. fi
  266. fi
  267. fi
  268. fi
  269. # =========================================================
  270. # === Functions ===
  271. # Get OS description from boot.ini
  272. #-----------------------------------------------------
  273. get_descr() {
  274. _desc=""
  275. local _descx=
  276. _descx=`cat /${_actmount}/${_ini_prefix}/$1 | grep \#DESCRIPTION`
  277. if [ ! "${_descx}" = "" ]; then
  278. _desc=$(echo ${_descx} | sed -r 's/^.{13}//')
  279. fi
  280. if [ "${_desc}" = "" ]; then
  281. _desc="${2}"
  282. fi
  283. }
  284. #-----------------------------------------------------
  285. #----------------------------------------------------------------------------------------------------
  286. restore_bootini() {
  287. printf "\033[31m\033[1m %s\033[22m\033[37m" "${1}"
  288. cp /mnt1/${_ini_prefix}/boot.ini.multi /mnt1/boot.ini > /dev/null 2>&1
  289. [ "${O_BOARD}" = "XU" ] && cp /mnt1/${_ini_prefix}/boot.scr.multi /mnt1/boot.scr > /dev/null 2>&1
  290. umount /mnt_a > /dev/null 2>&1
  291. sleep 3
  292. }
  293. #----------------------------------------------------------------------------------------------------
  294. #-----------------------------------------------------------------------------------------------------------------------
  295. change_afstab() {
  296. # === Configure Android fstab to mount the right partitions ===
  297. if [ -f /mnt_a/${ODROID_FSTAB} ]; then
  298. if [ "${1}" = "yes" ]; then
  299. # Two cards, boot to Android on NON Boot card
  300. if [ "${O_BOARD}" = "C2" ]; then
  301. sed -i "s/\/dev\/block\/mmcblk0/\/dev\/block\/mmcblk1/g" /mnt_a/${ODROID_FSTAB}
  302. # on C2 prepare boot.ini for boot from sdcard
  303. sed -i "s/root=\/dev\/mmcblk0p2/root=\/dev\/mmcblk1p2/g" /mnt1/boot.ini
  304. else
  305. if [ "${OTHER_CARDTYPE}" = "SD" ]; then
  306. cp /mnt_a/${ODROID_FSTAB}.other /mnt_a/${ODROID_FSTAB}
  307. sed -i "s/setenv akernel zImage/setenv akernel \/multiboot\/zImage.andro_1/g" /mnt1/boot.ini
  308. else
  309. cp /mnt_a/${ODROID_FSTAB}.orig /mnt_a/${ODROID_FSTAB}
  310. sed -i "s/setenv akernel zImage/setenv akernel \/multiboot\/zImage.android/g" /mnt1/boot.ini
  311. fi
  312. fi
  313. else
  314. # One or two cards, boot to Android on Boot card
  315. if [ "${O_BOARD}" = "C2" ]; then
  316. # Restore boot device if modified earlier
  317. sed -i "s/\/dev\/block\/mmcblk1/\/dev\/block\/mmcblk0/g" /mnt_a/${ODROID_FSTAB}
  318. else
  319. if [ "${NUMCARDS}" = "1" ]; then
  320. cp /mnt_a/${ODROID_FSTAB}.orig /mnt_a/${ODROID_FSTAB}
  321. sed -i "s/setenv akernel zImage/setenv akernel \/multiboot\/zImage.android/g" /mnt1/boot.ini
  322. else
  323. # Two cards, boot to Android on Boot card
  324. if [ "${BOOT_CARDTYPE}" = "SD" ] && [ "${BOOT_CARDDEV}" = "1" ]; then
  325. cp /mnt_a/${ODROID_FSTAB}.other /mnt_a/${ODROID_FSTAB}
  326. sed -i "s/setenv akernel zImage/setenv akernel \/multiboot\/zImage.andro_1/g" /mnt1/boot.ini
  327. else
  328. cp /mnt_a/${ODROID_FSTAB}.orig /mnt_a/${ODROID_FSTAB}
  329. sed -i "s/setenv akernel zImage/setenv akernel \/multiboot\/zImage.android/g" /mnt1/boot.ini
  330. fi
  331. fi
  332. fi
  333. fi
  334. if [ "${O_BOARD}" = "XU" ]; then
  335. cp -f /mnt_a/${ODROID_FSTAB} /mnt_a/${ODROID_FSTAB}.sdboot
  336. fi
  337. else
  338. restore_bootini "${ODROID_FSTAB} not found"
  339. return 1
  340. fi
  341. return 0
  342. }
  343. #-----------------------------------------------------------------------------------------------------------------------
  344. #----------------------------------------------------------------------------------------------------------------------
  345. # =======================================================================
  346. # On C2 boot drive is always mmcblk0 and is mounted on /mnt1
  347. # On XU3/XU4 boot drive can be mmcblk0 or mmcblk1 and is mounted on /mnt1
  348. # =======================================================================
  349. do_boot() {
  350. [ "${1}" = "none" ] && return 0
  351. local change_drive="no"
  352. local _oncard=""
  353. _actmount="mnt1"
  354. if [ "${3}" = "2" ]; then
  355. # Operating system on NON BOOT card
  356. _actmount="mnt2"
  357. change_drive="yes"
  358. fi
  359. if [ "${NUMCARDS}" = "2" ]; then
  360. if [ "${_actmount}" = "mnt1" ]; then
  361. _oncard=" on ${BOOT_CARDTYPE}"
  362. else
  363. _oncard=" on ${OTHER_CARDTYPE}"
  364. fi
  365. fi
  366. if [ -f /${_actmount}/${_ini_prefix}/${1} ]; then
  367. boot_fail="yes"
  368. printf "\r\033[2K"
  369. printf " Booting to \033[32m%s\033[37m\033[36m%s\033[37m" "${2}" "${_oncard}"
  370. # === Copy selected boot.ini to BOOT partition
  371. cp /${_actmount}/${_ini_prefix}/${1} /mnt1/boot.ini > /dev/null 2>&1
  372. if [ ! $? -eq 0 ]; then
  373. restore_bootini "ERROR copying /${_actmount}/${_ini_prefix}/${1}"
  374. return 1
  375. fi
  376. sleep 1
  377. if [ "${change_drive}" = "yes" ]; then
  378. # === boot.ini from NON-BOOT card, prepare it for boot from BOOT card ===
  379. if [ "${O_BOARD}" = "C2" ]; then
  380. # for kernel/initrd/dtb loading
  381. sed -i "s/fatload mmc 0:1/fatload mmc 1:1/g" /mnt1/boot.ini
  382. # for boot.ini loading from /multiboot (it was changed with previous sed operation!)
  383. sed -i "s/1:1 0x60000000/0:1 0x60000000/g" /mnt1/boot.ini
  384. else
  385. # for kernel/initrd/dtb loading
  386. sed -i "s/setenv change_osdev no/setenv change_osdev yes/g" /mnt1/boot.ini
  387. fi
  388. fi
  389. if [ "${1}" = "boot.ini.android" ]; then
  390. # === mount destination android system partition to prepare android fstab ===
  391. mkdir /mnt_a > /dev/null 2>&1
  392. if [ "${change_drive}" = "yes" ]; then
  393. mount -t ext4 /dev/mmcblk${OTHER_CARDDEV}p2 /mnt_a > /dev/null 2>&1
  394. else
  395. mount -t ext4 /dev/mmcblk${BOOT_CARDDEV}p2 /mnt_a > /dev/null 2>&1
  396. fi
  397. if [ $? -ne 0 ]; then
  398. restore_bootini "Mount Android system partition ERROR"
  399. return 1
  400. fi
  401. # === Configure Android fstab to mount the right partitions ===
  402. change_afstab "${change_drive}"
  403. [ $? -ne 0 ] && return 1
  404. umount /mnt_a > /dev/null 2>&1
  405. # =============================================================
  406. fi
  407. if [ "${O_BOARD}" = "XU" ]; then
  408. # Convert boot.ini to boot.scr
  409. mkimage -C none -A arm -T script -d /mnt1/boot.ini /mnt1/boot.scr > /dev/null 2>&1
  410. if [ $? -ne 0 ]; then
  411. restore_bootini "mkimage (boot.scr) ERROR"
  412. return 1
  413. fi
  414. if [ -f /mnt1/multiboot/newscr ]; then
  415. mkimage -C none -A arm -T script -d /mnt1/multiboot/boot.ini.multi /mnt1/multiboot/boot.scr.multi > /dev/null 2>&1
  416. if [ $? -ne 0 ]; then
  417. restore_bootini "mkimage (boot.scr.multi) ERROR"
  418. return 1
  419. fi
  420. rm -f /mnt1/multiboot/newscr > /dev/null 2>&1
  421. fi
  422. fi
  423. sleep 1
  424. umount /mnt1
  425. umount /mnt2 > /dev/null 2>&1
  426. fsck.fat -aw /dev/mmcblk${BOOT_CARDDEV}p1 > /dev/null 2>&1
  427. reboot
  428. fi
  429. }
  430. #----------------------------------------------------------------------------------------------------------------------
  431. #------------------------------------------------------
  432. set_menu_key() {
  433. _menu_key="\\$(printf '%03o' ${_menu_no})"
  434. if [ $_menu_no -eq 49 ]; then
  435. _menuitem_1="${1}"
  436. _mndscr_1="${_desc}"
  437. [ "${_actmount}" = "mnt2" ] && _mncrd_1="2"
  438. elif [ $_menu_no -eq 50 ]; then
  439. _menuitem_2="${1}"
  440. _mndscr_2="${_desc}"
  441. [ "${_actmount}" = "mnt2" ] && _mncrd_2="2"
  442. elif [ $_menu_no -eq 51 ]; then
  443. _menuitem_3="${1}"
  444. _mndscr_3="${_desc}"
  445. [ "${_actmount}" = "mnt2" ] && _mncrd_3="2"
  446. elif [ $_menu_no -eq 52 ]; then
  447. _menuitem_4="${1}"
  448. _mndscr_4="${_desc}"
  449. [ "${_actmount}" = "mnt2" ] && _mncrd_4="2"
  450. elif [ $_menu_no -eq 53 ]; then
  451. _menuitem_5="${1}"
  452. _mndscr_5="${_desc}"
  453. [ "${_actmount}" = "mnt2" ] && _mncrd_5="2"
  454. elif [ $_menu_no -eq 54 ]; then
  455. _menuitem_6="${1}"
  456. _mndscr_6="${_desc}"
  457. [ "${_actmount}" = "mnt2" ] && _mncrd_6="2"
  458. elif [ $_menu_no -eq 55 ]; then
  459. _menuitem_7="${1}"
  460. _mndscr_7="${_desc}"
  461. [ "${_actmount}" = "mnt2" ] && _mncrd_7="2"
  462. elif [ $_menu_no -eq 56 ]; then
  463. _menuitem_8="${1}"
  464. _mndscr_8="${_desc}"
  465. [ "${_actmount}" = "mnt2" ] && _mncrd_8="2"
  466. fi
  467. let _menu_no=_menu_no+1
  468. let _numos=_numos+1
  469. }
  470. #------------------------------------------------------
  471. #---------------------------------------------------------------------------------------------
  472. # shellcheck disable=SC2059
  473. get_menu_items() {
  474. local _oncard=""
  475. if [ "${NUMCARDS}" = "2" ]; then
  476. if [ "${_actmount}" = "mnt1" ]; then
  477. _oncard=" on ${BOOT_CARDTYPE}"
  478. else
  479. _oncard=" on ${OTHER_CARDTYPE}"
  480. fi
  481. fi
  482. if [ -f /${_actmount}/${_ini_prefix}/boot.ini.android ]; then
  483. get_descr "boot.ini.android" "Android"
  484. set_menu_key "boot.ini.android"
  485. printf "\033[36m \033[32m\033[1m${_menu_key}\033[22m\033[36m boot to \033[32m${_desc}\033[36m${_oncard}\n"
  486. fi
  487. if [ -f /${_actmount}/${_ini_prefix}/boot.ini.linux ]; then
  488. get_descr "boot.ini.linux" "Linux"
  489. set_menu_key "boot.ini.linux"
  490. printf "\033[36m \033[33m\033[1m${_menu_key}\033[22m\033[36m boot to \033[33m${_desc}\033[36m${_oncard}\n"
  491. fi
  492. if [ -f /${_actmount}/${_ini_prefix}/boot.ini.oelec ]; then
  493. get_descr "boot.ini.oelec" "OpenElec"
  494. set_menu_key "boot.ini.oelec"
  495. printf "\033[36m \033[37m\033[1m${_menu_key}\033[22m\033[36m boot to \033[37m${_desc}\033[36m${_oncard}\n"
  496. fi
  497. if [ -f /${_actmount}/${_ini_prefix}/boot.ini.linux2 ]; then
  498. get_descr "boot.ini.linux2" "Linux #2"
  499. set_menu_key "boot.ini.linux2"
  500. printf "\033[36m \033[33m\033[1m${_menu_key}\033[22m\033[36m boot to \033[33m${_desc}\033[36m${_oncard}\n"
  501. fi
  502. }
  503. #---------------------------------------------------------------------------------------------
  504. #-----------------------------------------------------------------------------------------------------
  505. do_halt() {
  506. umount /mnt1 > /dev/null 2>&1
  507. umount /mnt2 > /dev/null 2>&1
  508. local B="X"
  509. local _reboot_delay=60
  510. while [ $_reboot_delay -gt 0 ]; do
  511. printf "\r\033[2K"
  512. printf " \033[31m\033[1m%s, press R to reboot %s\033[22m\033[37m\r" "${1}" "$_reboot_delay"
  513. if read -n 1 -t 1 -s B; then
  514. _reboot_delay=60
  515. if [ "${B}" = "r" ] || [ "${B}" = "R" ]; then
  516. printf "\r\033[2K"
  517. reboot
  518. fi
  519. else
  520. let _reboot_delay=_reboot_delay-1
  521. fi
  522. done
  523. reboot
  524. }
  525. #-----------------------------------------------------------------------------------------------------
  526. #-----------------------------------------------------------------------------------------------------------------
  527. os_detect() {
  528. _desc=""
  529. _ini_prefix="multiboot"
  530. _actmount="mnt1"
  531. _numos=0
  532. _menu_no=49
  533. _menu_key="1"
  534. _menuitem_1=""
  535. _menuitem_2=""
  536. _menuitem_3=""
  537. _menuitem_4=""
  538. _menuitem_5=""
  539. _menuitem_6=""
  540. _menuitem_7=""
  541. _menuitem_8=""
  542. _mndscr_1=""
  543. _mndscr_2=""
  544. _mndscr_3=""
  545. _mndscr_4=""
  546. _mndscr_5=""
  547. _mndscr_6=""
  548. _mndscr_7=""
  549. _mndscr_8=""
  550. _mncrd_1="1"
  551. _mncrd_2="1"
  552. _mncrd_3="1"
  553. _mncrd_4="1"
  554. _mncrd_5="1"
  555. _mncrd_6="1"
  556. _mncrd_7="1"
  557. _mncrd_8="1"
  558. _boot_delay=2
  559. _defboot="none"
  560. _defdescr="none"
  561. _defcard="1"
  562. # prepare cards mount points
  563. mkdir /mnt1 > /dev/null 2>&1
  564. mkdir /mnt2 > /dev/null 2>&1
  565. umount /mnt1 > /dev/null 2>&1
  566. umount /mnt2 > /dev/null 2>&1
  567. umount /mnt > /dev/null 2>&1
  568. clear
  569. # Detect new partitions after install
  570. blkid > /dev/null 2>&1
  571. # ================================
  572. # Mount available cards
  573. # ================================
  574. mount -t vfat /dev/mmcblk${BOOT_CARDDEV}p1 /mnt1 > /dev/null 2>&1
  575. if [ $? -eq 0 ]; then
  576. if [ "${NUMCARDS}" = "2" ]; then
  577. mount -t vfat /dev/mmcblk${OTHER_CARDDEV}p1 /mnt2 > /dev/null 2>&1
  578. if [ $? -ne 0 ]; then
  579. OTHER_CARDDEV="9"
  580. fi
  581. fi
  582. else
  583. # === SOMETHING IS VERY WRONG ===
  584. do_halt "BOOT CARD NOT MOUNTABLE"
  585. fi
  586. clear
  587. printf "\n"
  588. printf "\033[36m\033[1m\n"
  589. if [ "${O_BOARD}" = "C2" ]; then
  590. printf " ============================\n"
  591. printf " = ODROID C2 BOOT MENU %s =\n" "${SCRIPT_VERSION}"
  592. printf " ============================\n"
  593. else
  594. printf " =================================\n"
  595. printf " = ODROID XU3/XU4 BOOT MENU %s =\n" "${SCRIPT_VERSION}"
  596. printf " =================================\n"
  597. fi
  598. if [ "${NUMCARDS}" = "2" ]; then
  599. printf "\033[22m Two cards detected, booted from \033[1m%s\033[22m\033[37m\n\n" "${BOOT_CARDTYPE}"
  600. else
  601. printf "\033[22m Booted from \033[1m%s\033[22m\033[37m\n\n" "${BOOT_CARDTYPE}"
  602. fi
  603. #===================================================
  604. # === Detect available OS's and print menu items ===
  605. #===================================================
  606. _menu_no=49
  607. # Scan boot card first
  608. _actmount="mnt1"
  609. if [ -b /dev/disk/by-label/sduserdata ] || [ -b /dev/disk/by-label/emuserdata ]; then
  610. get_menu_items
  611. if [ "${NUMCARDS}" = "2" ]; then
  612. _actmount="mnt2"
  613. [ ! ${_numos} -eq 0 ] && printf "\n"
  614. get_menu_items
  615. fi
  616. [ ${_numos} -eq 0 ] && printf "\033[36m \033[36m\033[1m!\033[22m NO OS DETECTED\033[36m\n"
  617. else
  618. [ ${_numos} -eq 0 ] && printf "\033[36m \033[36m\033[1m!\033[22m NO MULTIBOOT CARD DETECTED\033[36m\n"
  619. fi
  620. printf "\n"
  621. printf " \033[36m\033[1mB\033[22m Backup Android/Linux/OpenElec\033[36m\n"
  622. printf " \033[36m\033[1mU\033[22m Update Android\033[36m\n"
  623. printf " \033[33m\033[1mI\033[22m Prepare the card for MultiBoot, Install OS's\033[36m\n"
  624. printf "\n"
  625. printf " \033[36m\033[1mX\033[22m Exit to busybox shell\033[36m\n"
  626. printf " \033[33m\033[1mR\033[22m Reboot\033[36m\n"
  627. printf "\n"
  628. printf "\n"
  629. # === Get default boot ===
  630. if [ "${DEFAULTBOOT}" = "1" ] && [ ! "${_menuitem_1}" = "none" ]; then
  631. _defboot="${_menuitem_1}"
  632. _defdescr="${_mndscr_1}"
  633. _defcard="${_mncrd_1}"
  634. elif [ "${DEFAULTBOOT}" = "2" ] && [ ! "${_menuitem_2}" = "none" ]; then
  635. _defboot="${_menuitem_2}"
  636. _defdescr="${_mndscr_2}"
  637. _defcard="${_mncrd_2}"
  638. elif [ "${DEFAULTBOOT}" = "3" ] && [ ! "${_menuitem_3}" = "none" ]; then
  639. _defboot="${_menuitem_3}"
  640. _defdescr="${_mndscr_3}"
  641. _defcard="${_mncrd_3}"
  642. elif [ "${DEFAULTBOOT}" = "4" ] && [ ! "${_menuitem_4}" = "none" ]; then
  643. _defboot="${_menuitem_4}"
  644. _defdescr="${_mndscr_4}"
  645. _defcard="${_mncrd_4}"
  646. elif [ "${DEFAULTBOOT}" = "5" ] && [ ! "${_menuitem_5}" = "none" ]; then
  647. _defboot="${_menuitem_5}"
  648. _defdescr="${_mndscr_5}"
  649. _defcard="${_mncrd_5}"
  650. elif [ "${DEFAULTBOOT}" = "6" ] && [ ! "${_menuitem_6}" = "none" ]; then
  651. _defboot="${_menuitem_6}"
  652. _defdescr="${_mndscr_6}"
  653. _defcard="${_mncrd_6}"
  654. elif [ "${DEFAULTBOOT}" = "7" ] && [ ! "${_menuitem_7}" = "none" ]; then
  655. _defboot="${_menuitem_7}"
  656. _defdescr="${_mndscr_7}"
  657. _defcard="${_mncrd_7}"
  658. elif [ "${DEFAULTBOOT}" = "8" ] && [ ! "${_menuitem_8}" = "none" ]; then
  659. _defboot="${_menuitem_8}"
  660. _defdescr="${_mndscr_8}"
  661. _defcard="${_mncrd_8}"
  662. fi
  663. if [ "${_defboot}" = "none" ]; then
  664. if [ ! "${_menuitem_1}" = "none" ]; then
  665. _defboot="${_menuitem_1}"
  666. _defdescr="${_mndscr_1}"
  667. _defcard="${_mncrd_1}"
  668. DEFAULTBOOT="1"
  669. elif [ ! "${_menuitem_2}" = "none" ]; then
  670. _defboot="${_menuitem_2}"
  671. _defdescr="${_mndscr_2}"
  672. _defcard="${_mncrd_2}"
  673. DEFAULTBOOT="2"
  674. elif [ ! "${_menuitem_3}" = "none" ]; then
  675. _defboot="${_menuitem_3}"
  676. _defdescr="${_mndscr_3}"
  677. _defcard="${_mncrd_3}"
  678. DEFAULTBOOT="3"
  679. elif [ ! "${_menuitem_4}" = "none" ]; then
  680. _defboot="${_menuitem_4}"
  681. _defdescr="${_mndscr_4}"
  682. _defcard="${_mncrd_4}"
  683. DEFAULTBOOT="4"
  684. elif [ ! "${_menuitem_5}" = "none" ]; then
  685. _defboot="${_menuitem_5}"
  686. _defdescr="${_mndscr_5}"
  687. _defcard="${_mncrd_5}"
  688. DEFAULTBOOT="5"
  689. elif [ ! "${_menuitem_6}" = "none" ]; then
  690. _defboot="${_menuitem_6}"
  691. _defdescr="${_mndscr_6}"
  692. _defcard="${_mncrd_6}"
  693. DEFAULTBOOT="6"
  694. elif [ ! "${_menuitem_7}" = "none" ]; then
  695. _defboot="${_menuitem_7}"
  696. _defdescr="${_mndscr_7}"
  697. _defcard="${_mncrd_7}"
  698. DEFAULTBOOT="7"
  699. elif [ ! "${_menuitem_8}" = "none" ]; then
  700. _defboot="${_menuitem_8}"
  701. _defdescr="${_mndscr_8}"
  702. _defcard="${_mncrd_8}"
  703. DEFAULTBOOT="8"
  704. fi
  705. fi
  706. }
  707. #-----------------------------------------------------------------------------------------------------------------
  708. os_detect
  709. # ======================================
  710. # === Main loop, never exit ===
  711. # === Wait for user OS/action select ===
  712. # ======================================
  713. _boot_delay=$BOOTDELAY
  714. # === Detect available Operating Systems ===
  715. while true; do
  716. printf "\r\033[2K"
  717. printf "\033[37m Select an option or boot to menu item \033[36m\033[1m%s\033[22m in\033[31m\033[1m %s \033[22m\033[37mseconds" "${DEFAULTBOOT}" "${_boot_delay}"
  718. A="Z"
  719. if read -n 1 -t 1 -s A; then
  720. _boot_delay=$BOOTDELAY
  721. if [ "${A}" = "B" ]; then
  722. # === BACKUP ===
  723. if [ -f /backup.script ]; then
  724. umount /mnt1 > /dev/null 2>&1
  725. umount /mnt2 > /dev/null 2>&1
  726. sleep 1
  727. # =========================
  728. # Execute the backup script
  729. /backup.script
  730. # =========================
  731. A="Z"
  732. os_detect
  733. _boot_delay=$BOOTDELAY
  734. else
  735. printf "\r\033[2K"
  736. printf " BACKUP SCRIPT NOT FOUND!"
  737. sleep 4
  738. fi
  739. elif [ "${A}" = "I" ]; then
  740. # === UNIVERSAL INSTALLER ===
  741. if [ -f /install.script ]; then
  742. umount /mnt1 > /dev/null 2>&1
  743. umount /mnt2 > /dev/null 2>&1
  744. sleep 1
  745. # ============================
  746. # Execute the installer script
  747. UPGRADE_ANDROID="no"
  748. /install.script
  749. # ============================
  750. A="Z"
  751. os_detect
  752. _boot_delay=$BOOTDELAY
  753. else
  754. printf "\r\033[2K"
  755. printf " INSTALL SCRIPT NOT FOUND!"
  756. sleep 4
  757. fi
  758. elif [ "${A}" = "U" ]; then
  759. # === ANDROID UPGRADE ====
  760. if [ -f /install.script ]; then
  761. umount /mnt1 > /dev/null 2>&1
  762. umount /mnt2 > /dev/null 2>&1
  763. sleep 1
  764. # =========================
  765. # Execute the backup script
  766. UPGRADE_ANDROID="yes"
  767. /install.script
  768. # =========================
  769. A="Z"
  770. os_detect
  771. _boot_delay=$BOOTDELAY
  772. else
  773. printf "\r\033[2K"
  774. printf " BACKUP SCRIPT NOT FOUND!"
  775. sleep 4
  776. fi
  777. elif [ "${A}" = "X" ]; then
  778. # === ENTER BUSYBOX ===
  779. clear
  780. printf "\n\n\n Entering \033[32mBusybox\033[37m shell\n"
  781. printf " Execute \033[32mexit\033[37m to return to menu\n\n"
  782. umount /mnt1 > /dev/null 2>&1
  783. umount /mnt2 > /dev/null 2>&1
  784. sleep 1
  785. #For C2 show cursor
  786. [ "${O_BOARD}" = "C2" ] && tput cnorm
  787. #===============
  788. break="bbox"
  789. maybe_break bbox
  790. #===============
  791. printf "\n \033[32mBusybox exited\033[37m\n"
  792. sleep 2
  793. #For C2 hide cursor
  794. [ "${O_BOARD}" = "C2" ] && tput civis
  795. A="Z"
  796. os_detect
  797. _boot_delay=$BOOTDELAY
  798. elif [ "${A}" = "R" ]; then
  799. # REBOOT ===
  800. _reboot_delay=10
  801. B="X"
  802. while [ $_reboot_delay -gt 0 ]; do
  803. printf "\r\033[2K"
  804. printf " \033[33m\033[1mREBOOT in %s sec, press \033[22ma\033[1m to abort, \033[22mr\033[1m to reboot \033[22m\033[37m\r" "$_reboot_delay"
  805. if read -n 1 -t 1 -s B; then
  806. _reboot_delay=10
  807. if [ "${B}" = "a" ] || [ "${B}" = "A" ]; then
  808. printf "\r\033[2K"
  809. break
  810. elif [ "${B}" = "r" ] || [ "${B}" = "R" ]; then
  811. _reboot_delay=0
  812. break
  813. fi
  814. else
  815. let _reboot_delay=_reboot_delay-1
  816. fi
  817. done
  818. if [ $_reboot_delay -eq 0 ]; then
  819. cp -f /*.ppm /mnt1 > /dev/null 2>&1
  820. umount /mnt1 > /dev/null 2>&1
  821. umount /mnt2 > /dev/null 2>&1
  822. reboot
  823. fi
  824. elif [ "${A}" = "s" ]; then
  825. # SCREENSHOT ===
  826. fbcat > /mb_menu.ppm
  827. elif [ $_numos -gt 0 ]; then
  828. # === BOOT to selected OS
  829. boot_fail="no"
  830. [ "${A}" = "1" ] && [ ! "${_menuitem_1}" = "none" ] && do_boot "${_menuitem_1}" "${_mndscr_1}" "${_mncrd_1}"
  831. [ "${A}" = "2" ] && [ ! "${_menuitem_2}" = "none" ] && do_boot "${_menuitem_2}" "${_mndscr_2}" "${_mncrd_2}"
  832. [ "${A}" = "3" ] && [ ! "${_menuitem_3}" = "none" ] && do_boot "${_menuitem_3}" "${_mndscr_3}" "${_mncrd_3}"
  833. [ "${A}" = "4" ] && [ ! "${_menuitem_4}" = "none" ] && do_boot "${_menuitem_4}" "${_mndscr_4}" "${_mncrd_4}"
  834. [ "${A}" = "5" ] && [ ! "${_menuitem_5}" = "none" ] && do_boot "${_menuitem_5}" "${_mndscr_5}" "${_mncrd_5}"
  835. [ "${A}" = "6" ] && [ ! "${_menuitem_6}" = "none" ] && do_boot "${_menuitem_6}" "${_mndscr_6}" "${_mncrd_6}"
  836. [ "${A}" = "7" ] && [ ! "${_menuitem_7}" = "none" ] && do_boot "${_menuitem_7}" "${_mndscr_7}" "${_mncrd_7}"
  837. [ "${A}" = "8" ] && [ ! "${_menuitem_8}" = "none" ] && do_boot "${_menuitem_8}" "${_mndscr_8}" "${_mncrd_8}"
  838. if [ "${boot_fail}" = "yes" ]; then
  839. # === BOOT ERROR ===
  840. sleep 4
  841. os_detect
  842. _boot_delay=$BOOTDELAY
  843. fi
  844. fi
  845. else
  846. let _boot_delay=_boot_delay-1
  847. if [ $_boot_delay -eq 0 ]; then
  848. # Default boot
  849. [ $_numos -gt 0 ] && [ ! "${_defboot}" = "none" ] && do_boot "${_defboot}" "${_defdescr}" "${_defcard}"
  850. # === BOOT ERROR ===
  851. sleep 4
  852. os_detect
  853. _boot_delay=$BOOTDELAY
  854. fi
  855. fi
  856. printf "\r"
  857. done
  858. # Just in case...
  859. printf "\n\n"
  860. umount /mnt1 > /dev/null 2>&1
  861. umount /mnt2 > /dev/null 2>&1
  862. printf "\033[36m REBOOT ...\n"
  863. reboot
  864. #---------------------------------------------------------------------------