init 27 KB

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