install.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. #!/usr/bin/env bash
  2. #-
  3. # Copyright © 2010-2016
  4. # Waldemar Brodkorb <wbx@openadk.org>
  5. # Thorsten Glaser <tg@mirbsd.org>
  6. #
  7. # Provided that these terms and disclaimer and all copyright notices
  8. # are retained or reproduced in an accompanying document, permission
  9. # is granted to deal in this work without restriction, including un‐
  10. # limited rights to use, publicly perform, distribute, sell, modify,
  11. # merge, give away, or sublicence.
  12. #
  13. # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
  14. # the utmost extent permitted by applicable law, neither express nor
  15. # implied; without malicious intent or gross negligence. In no event
  16. # may a licensor, author or contributor be held liable for indirect,
  17. # direct, other damage, loss, or other issues arising in any way out
  18. # of dealing in the work, even if advised of the possibility of such
  19. # damage or existence of a defect, except proven that it results out
  20. # of said person’s immediate fault when using the work as intended.
  21. #
  22. # Alternatively, this work may be distributed under the terms of the
  23. # General Public License, any version, as published by the Free Soft-
  24. # ware Foundation.
  25. #-
  26. # Prepare a USB stick or CF/SD/MMC card or hard disc for installation
  27. # of OpenADK
  28. ADK_TOPDIR=$(pwd)
  29. HOST=$(gcc -dumpmachine)
  30. me=$0
  31. case :$PATH: in
  32. (*:$ADK_TOPDIR/host_$HOST/usr/bin:*) ;;
  33. (*) export PATH=$PATH:$ADK_TOPDIR/host_$HOST/usr/bin ;;
  34. esac
  35. test -n "$KSH_VERSION" || exec mksh "$me" "$@"
  36. if test -z "$KSH_VERSION"; then
  37. echo >&2 Fatal error: could not run myself with mksh!
  38. exit 255
  39. fi
  40. ### run with mksh from here onwards ###
  41. me=${me##*/}
  42. if (( USER_ID )); then
  43. print -u2 Installation is only possible as root!
  44. exit 1
  45. fi
  46. ADK_TOPDIR=$(realpath .)
  47. ostype=$(uname -s)
  48. fs=ext4
  49. cfgfs=1
  50. datafssz=0
  51. noformat=0
  52. quiet=0
  53. serial=0
  54. speed=115200
  55. panicreboot=10
  56. keep=0
  57. grub=0
  58. function usage {
  59. cat >&2 <<EOF
  60. Syntax: $me [-f filesystem] [-c cfgfssize] [-d datafssize] [-k] [-n] [-g]
  61. [-p panictime] [±q] [-s serialspeed] [±t] <target> <device> <archive>
  62. Partition sizes are in MiB. Filesystem type is currently ignored (ext4).
  63. To keep filesystem on data partition use -k.
  64. Use -n to not format boot/root partition.
  65. Defaults: -c 1 -p 10 -s 115200; -t = enable serial console
  66. EOF
  67. exit $1
  68. }
  69. while getopts "c:d:f:ghknp:qs:t" ch; do
  70. case $ch {
  71. (c) if (( (cfgfs = OPTARG) < 0 || cfgfs > 16 )); then
  72. print -u2 "$me: -c $OPTARG out of bounds"
  73. exit 1
  74. fi ;;
  75. (d) if (( (datafssz = OPTARG) < 0 )); then
  76. print -u2 "$me: -d $OPTARG out of bounds"
  77. exit 1
  78. fi ;;
  79. (f) if [[ $OPTARG != @(ext2|ext3|ext4|xfs) ]]; then
  80. print -u2 "$me: filesystem $OPTARG invalid"
  81. exit 1
  82. fi
  83. fs=$OPTARG ;;
  84. (h) usage 0 ;;
  85. (k) keep=1 ;;
  86. (g) grub=1 ;;
  87. (p) if (( (panicreboot = OPTARG) < 0 || panicreboot > 300 )); then
  88. print -u2 "$me: -p $OPTARG out of bounds"
  89. exit 1
  90. fi ;;
  91. (q) quiet=1 ;;
  92. (+q) quiet=0 ;;
  93. (s) if [[ $OPTARG != @(96|192|384|576|1152)00 ]]; then
  94. print -u2 "$me: serial speed $OPTARG invalid"
  95. exit 1
  96. fi
  97. speed=$OPTARG ;;
  98. (n) noformat=1 ;;
  99. (t) serial=1 ;;
  100. (+t) serial=0 ;;
  101. (*) usage 1 ;;
  102. }
  103. done
  104. shift $((OPTIND - 1))
  105. (( $# == 3 )) || usage 1
  106. f=0
  107. case $ostype {
  108. (Linux)
  109. tools="bc mkfs.$fs tune2fs partprobe"
  110. ;;
  111. (Darwin)
  112. tools="bc diskutil"
  113. ;;
  114. (*)
  115. print -u2 Sorry, not ported to the OS "'$ostype'" yet.
  116. exit 1
  117. ;;
  118. }
  119. for tool in $tools; do
  120. print -n Checking if $tool is installed...
  121. if whence -p $tool >/dev/null; then
  122. print " okay"
  123. else
  124. print " failed"
  125. f=1
  126. fi
  127. done
  128. (( f )) && exit 1
  129. target=$1
  130. tgt=$2
  131. src=$3
  132. case $target {
  133. (banana-pro|pcengines-apu|raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|solidrun-imx6|solidrun-clearfog|default) ;;
  134. (*)
  135. print -u2 "Unknown target '$target', exiting"
  136. exit 1 ;;
  137. }
  138. if [[ ! -b $tgt ]]; then
  139. print -u2 "'$tgt' is not a block device, exiting"
  140. exit 1
  141. fi
  142. if [[ ! -f $src ]]; then
  143. print -u2 "'$src' is not a file, exiting"
  144. exit 1
  145. fi
  146. (( quiet )) || print "Installing $src on $tgt."
  147. case $ostype {
  148. (Darwin)
  149. R=/Volumes/ADKROOT; diskutil unmount $R
  150. B=/Volumes/ADKBOOT; diskutil unmount $B
  151. D=/Volumes/ADKDATA; diskutil unmount $D
  152. basedev=$tgt
  153. rootpart=${basedev}s1
  154. datapart=${basedev}s2
  155. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 ]]; then
  156. bootpart=${basedev}s1
  157. rootpart=${basedev}s2
  158. datapart=${basedev}s3
  159. fi
  160. match=\'${basedev}\''?(s+([0-9]))'
  161. function mount_fs {
  162. }
  163. function umount_fs {
  164. diskutil unmount "$1"
  165. }
  166. function create_fs {
  167. if [[ $3 = ext4 ]]; then
  168. fstype=UFSD_EXTFS4
  169. fi
  170. if [[ $3 = vfat ]]; then
  171. fstype=fat32
  172. fi
  173. diskutil eraseVolume $fstype "$2" "$1"
  174. }
  175. function tune_fs {
  176. }
  177. ;;
  178. (Linux)
  179. basedev=$tgt
  180. partitionsep=""
  181. if [[ $basedev = /dev/loop* ]]; then
  182. (( quiet )) || print "${tgt} is a loop device"
  183. partitionsep=p
  184. fi
  185. rootpart=${basedev}${partitionsep}1
  186. datapart=${basedev}${partitionsep}2
  187. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 ]]; then
  188. bootpart=${basedev}${partitionsep}1
  189. rootpart=${basedev}${partitionsep}2
  190. datapart=${basedev}${partitionsep}3
  191. fi
  192. match=\'${basedev}${partitionsep}\''+([0-9])'
  193. function mount_fs {
  194. mount -t "$3" "$1" "$2"
  195. }
  196. function umount_fs {
  197. umount "$1"
  198. }
  199. function create_fs {
  200. (( quiet )) || print "Creating filesystem on ${1}..."
  201. mkfs.$3 "$1"
  202. }
  203. function tune_fs {
  204. tune2fs -c 0 -i 0 "$1"
  205. }
  206. ;;
  207. }
  208. mount |&
  209. while read -p dev rest; do
  210. eval [[ \$dev = $match ]] || continue
  211. print -u2 "Block device $tgt is in use, please umount first."
  212. exit 1
  213. done
  214. if (( !quiet )); then
  215. print "WARNING: This will overwrite $basedev - type Yes to continue!"
  216. read x
  217. [[ $x = Yes ]] || exit 0
  218. fi
  219. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  220. print -u2 Error creating temporary directory.
  221. exit 1
  222. fi
  223. if [[ $ostype != Darwin ]]; then
  224. R=$T/rootmnt
  225. B=$T/bootmnt
  226. D=$T/datamnt
  227. mkdir -p "$R" "$B" "$D"
  228. fi
  229. # get disk size
  230. dksz=$(dkgetsz "$tgt")
  231. # partition layouts:
  232. # n̲a̲m̲e̲ p̲a̲r̲t̲#̲0̲ p̲̲a̲r̲t̲#̲1̲ p̲̲a̲r̲t̲#̲2̲ p̲̲a̲r̲t̲#̲3̲
  233. # default: 0x83(system) 0x83(?data) -(unused) 0x88(cfgfs)
  234. # raspberry: 0x0B(boot) 0x83(system) 0x83(?data) 0x88(cfgfs)
  235. syspartno=0
  236. # sizes:
  237. # boot(raspberry) - fixed (100 MiB)
  238. # cfgfs - fixed (parameter, max. 16 MiB)
  239. # data - flexible (parameter)
  240. # system - everything else
  241. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 ]]; then
  242. syspartno=1
  243. bootfssz=100
  244. if (( grub )); then
  245. print -u2 "Cannot combine GRUB with $target"
  246. rm -rf "$T"
  247. exit 1
  248. fi
  249. else
  250. bootfssz=0
  251. fi
  252. heads=64
  253. secs=32
  254. (( cyls = dksz / heads / secs ))
  255. if (( cyls < (bootfssz + cfgfs + datafssz + 2) )); then
  256. print -u2 "Size of $tgt is $dksz, this looks fishy?"
  257. rm -rf "$T"
  258. exit 1
  259. fi
  260. if stat -qs .>/dev/null 2>&1; then
  261. statcmd='stat -f %z' # BSD stat (or so we assume)
  262. else
  263. statcmd='stat -c %s' # GNU stat
  264. fi
  265. if (( grub )); then
  266. tar -xOf "$src" boot/grub/core.img >"$T/core.img"
  267. integer coreimgsz=$($statcmd "$T/core.img")
  268. else
  269. coreimgsz=65024
  270. fi
  271. if (( coreimgsz < 1024 )); then
  272. print -u2 core.img is probably too small: $coreimgsz
  273. rm -rf "$T"
  274. exit 1
  275. fi
  276. if (( coreimgsz > 65024 )); then
  277. print -u2 core.img is larger than 64K-512: $coreimgsz
  278. rm -rf "$T"
  279. exit 1
  280. fi
  281. (( coreendsec = (coreimgsz + 511) / 512 ))
  282. if [[ $basedev = /dev/svnd+([0-9]) ]]; then
  283. # BSD svnd0 mode: protect sector #1
  284. corestartsec=2
  285. (( ++coreendsec ))
  286. corepatchofs=$((0x614))
  287. else
  288. corestartsec=1
  289. corepatchofs=$((0x414))
  290. fi
  291. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  292. #(( partofs = ((coreendsec / secs) + 1) * secs ))
  293. # we just use 2048 all the time, since some loaders are longer
  294. partofs=2048
  295. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 ]]; then
  296. (( spartofs = partofs + (100 * 2048) ))
  297. else
  298. spartofs=$partofs
  299. fi
  300. (( quiet )) || if (( grub )); then
  301. print Preparing MBR and GRUB2...
  302. else
  303. print Preparing MBR...
  304. fi
  305. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  306. # add another MiB to clear the first partition
  307. dd if=/dev/zero bs=1048576 count=1 >>"$T/firsttrack" 2>/dev/null
  308. echo $corestartsec $coreendsec | mksh "$ADK_TOPDIR/scripts/bootgrub.mksh" \
  309. -A -g $((cyls - bootfssz - cfgfs - datafssz)):$heads:$secs -M 1:0x83 \
  310. -O $spartofs | dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  311. (( grub )) && dd if="$T/core.img" of="$T/firsttrack" conv=notrunc \
  312. seek=$corestartsec 2>/dev/null
  313. # set partition where it can find /boot/grub
  314. (( grub )) && print -n '\0\0\0\0' | \
  315. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs 2>/dev/null
  316. # create cfgfs partition (mostly taken from bootgrub.mksh)
  317. set -A thecode
  318. typeset -Uui8 thecode
  319. mbrpno=0
  320. set -A g_code $cyls $heads $secs
  321. (( psz = g_code[0] * g_code[1] * g_code[2] ))
  322. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  323. set -A o_code # g_code equivalent for partition offset
  324. (( o_code[2] = pofs % g_code[2] + 1 ))
  325. (( o_code[1] = pofs / g_code[2] ))
  326. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  327. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  328. # boot flag; C/H/S offset
  329. thecode[mbrpno++]=0x00
  330. (( thecode[mbrpno++] = o_code[1] - 1 ))
  331. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  332. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  333. (( thecode[mbrpno++] = cylno & 0x00FF ))
  334. # partition type; C/H/S end
  335. (( thecode[mbrpno++] = 0x88 ))
  336. (( thecode[mbrpno++] = g_code[1] - 1 ))
  337. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  338. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  339. (( thecode[mbrpno++] = cylno & 0x00FF ))
  340. # partition offset, size (LBA)
  341. (( thecode[mbrpno++] = pofs & 0xFF ))
  342. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  343. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  344. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  345. (( pssz = psz - pofs ))
  346. (( thecode[mbrpno++] = pssz & 0xFF ))
  347. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  348. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  349. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  350. # write partition table entry
  351. ostr=
  352. curptr=0
  353. while (( curptr < 16 )); do
  354. ostr=$ostr\\0${thecode[curptr++]#8#}
  355. done
  356. print -n "$ostr" | \
  357. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1EE)) 2>/dev/null
  358. if (( datafssz )); then
  359. # create data partition (copy of the above :)
  360. set -A thecode
  361. typeset -Uui8 thecode
  362. mbrpno=0
  363. set -A g_code $cyls $heads $secs
  364. (( psz = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  365. (( pofs = (cyls - cfgfs - datafssz) * g_code[1] * g_code[2] ))
  366. set -A o_code # g_code equivalent for partition offset
  367. (( o_code[2] = pofs % g_code[2] + 1 ))
  368. (( o_code[1] = pofs / g_code[2] ))
  369. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  370. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  371. # boot flag; C/H/S offset
  372. thecode[mbrpno++]=0x00
  373. (( thecode[mbrpno++] = o_code[1] - 1 ))
  374. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  375. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  376. (( thecode[mbrpno++] = cylno & 0x00FF ))
  377. # partition type; C/H/S end
  378. (( thecode[mbrpno++] = 0x83 ))
  379. (( thecode[mbrpno++] = g_code[1] - 1 ))
  380. (( cylno = (g_code[0] - cfgfs) > 1024 ? 1023 : g_code[0] - cfgfs - 1 ))
  381. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  382. (( thecode[mbrpno++] = cylno & 0x00FF ))
  383. # partition offset, size (LBA)
  384. (( thecode[mbrpno++] = pofs & 0xFF ))
  385. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  386. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  387. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  388. (( pssz = psz - pofs ))
  389. (( thecode[mbrpno++] = pssz & 0xFF ))
  390. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  391. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  392. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  393. # write partition table entry
  394. ostr=
  395. curptr=0
  396. while (( curptr < 16 )); do
  397. ostr=$ostr\\0${thecode[curptr++]#8#}
  398. done
  399. print -n "$ostr" | \
  400. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  401. fi
  402. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 ]]; then
  403. # move system and data partition from #0/#1 to #1/#2
  404. dd if="$T/firsttrack" bs=1 skip=$((0x1BE)) count=32 of="$T/x" 2>/dev/null
  405. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) if="$T/x" 2>/dev/null
  406. # create boot partition (copy of the above :)
  407. set -A thecode
  408. typeset -Uui8 thecode
  409. mbrpno=0
  410. set -A g_code $cyls $heads $secs
  411. psz=$spartofs
  412. pofs=$partofs
  413. set -A o_code # g_code equivalent for partition offset
  414. (( o_code[2] = pofs % g_code[2] + 1 ))
  415. (( o_code[1] = pofs / g_code[2] ))
  416. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  417. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  418. # boot flag; C/H/S offset
  419. thecode[mbrpno++]=0x00
  420. (( thecode[mbrpno++] = o_code[1] - 1 ))
  421. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  422. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  423. (( thecode[mbrpno++] = cylno & 0x00FF ))
  424. # partition type; C/H/S end
  425. (( thecode[mbrpno++] = 0x0B ))
  426. (( thecode[mbrpno++] = g_code[1] - 1 ))
  427. (( cylno = (spartofs / 2048) > 1024 ? 1023 : (spartofs / 2048) - 1 ))
  428. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  429. (( thecode[mbrpno++] = cylno & 0x00FF ))
  430. # partition offset, size (LBA)
  431. (( thecode[mbrpno++] = pofs & 0xFF ))
  432. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  433. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  434. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  435. (( pssz = psz - pofs ))
  436. (( thecode[mbrpno++] = pssz & 0xFF ))
  437. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  438. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  439. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  440. # write partition table entry
  441. ostr=
  442. curptr=0
  443. while (( curptr < 16 )); do
  444. ostr=$ostr\\0${thecode[curptr++]#8#}
  445. done
  446. print -n "$ostr" | \
  447. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1BE)) 2>/dev/null
  448. fi
  449. # disk signature
  450. rnddev=/dev/urandom
  451. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  452. dd if=$rnddev bs=4 count=1 2>/dev/null | \
  453. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1B8)) 2>/dev/null
  454. print -n '\0\0' | \
  455. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1BC)) 2>/dev/null
  456. partuuid=$(dd if="$T/firsttrack" bs=1 count=4 skip=$((0x1B8)) 2>/dev/null | \
  457. hexdump -e '1/4 "%08x"')-0$((syspartno+1))
  458. ((keep)) || if (( datafssz )); then
  459. (( quiet )) || print Cleaning out data partition...
  460. dd if=/dev/zero of="$tgt" bs=1048576 count=1 seek=$((cyls - cfgfs - datafssz)) > /dev/null 2>&1
  461. fi
  462. (( quiet )) || print Cleaning out root partition...
  463. dd if=/dev/zero bs=1048576 of="$tgt" count=1 seek=$((spartofs / 2048)) > /dev/null 2>&1
  464. (( quiet )) || if (( grub )); then
  465. print Writing MBR and GRUB2 to target device... system PARTUUID=$partuuid
  466. else
  467. print Writing MBR to target device... system PARTUUID=$partuuid
  468. fi
  469. dd if="$T/firsttrack" of="$tgt" > /dev/null 2>&1
  470. if [[ $ostype = Linux ]]; then
  471. partprobe $tgt
  472. sync
  473. fi
  474. fwdir=$(dirname "$src")
  475. case $target {
  476. (banana-pro)
  477. dd if="$fwdir/u-boot-sunxi-with-spl.bin" of="$tgt" bs=1024 seek=8 > /dev/null 2>&1
  478. ;;
  479. (solidrun-clearfog)
  480. dd if="$fwdir/u-boot-spl.kwb" of="$tgt" bs=512 seek=1 > /dev/null 2>&1
  481. ;;
  482. (solidrun-imx6)
  483. dd if="$fwdir/SPL" of="$tgt" bs=1024 seek=1 > /dev/null 2>&1
  484. dd if="$fwdir/u-boot.img" of="$tgt" bs=1024 seek=69 > /dev/null 2>&1
  485. ;;
  486. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64)
  487. (( noformat )) || create_fs "$bootpart" ADKBOOT vfat
  488. ;;
  489. }
  490. (( noformat )) || create_fs "$rootpart" ADKROOT ext4
  491. (( noformat )) || tune_fs "$rootpart"
  492. (( quiet )) || print Extracting installation archive...
  493. mount_fs "$rootpart" "$R" ext4
  494. xz -dc "$src" | (cd "$R"; tar -xpf -)
  495. if (( datafssz )); then
  496. mkdir -m0755 "$R"/data
  497. ((keep)) || create_fs "$datapart" ADKDATA ext4
  498. ((keep)) || tune_fs "$datapart"
  499. case $target {
  500. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64)
  501. echo "/dev/mmcblk0p3 /data ext4 rw 0 0" >> "$R"/etc/fstab
  502. ;;
  503. (banana-pro|solidrun-imx6|solidrun-clearfog)
  504. echo "/dev/mmcblk0p2 /data ext4 rw 0 0" >> "$R"/etc/fstab
  505. ;;
  506. }
  507. fi
  508. case $target {
  509. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64)
  510. mount_fs "$bootpart" "$B" vfat
  511. for x in "$R"/boot/*; do
  512. [[ -e "$x" ]] && mv -f "$R"/boot/* "$B/"
  513. break
  514. done
  515. for x in "$fwdir"/*.dtb; do
  516. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$B/"
  517. break
  518. done
  519. # use static dtoverlay, rename to *.dtb
  520. mkdir "$B/"overlays
  521. for x in "$fwdir"/overlays/*.dtbo; do
  522. y=$(basename ${x} .dtbo)
  523. [[ -e "$x" ]] && cp "$fwdir"/overlays/${y}.dtbo "$B/"overlays/${y}.dtb
  524. done
  525. umount_fs "$B"
  526. ;;
  527. (solidrun-clearfog)
  528. for x in "$fwdir"/*.dtb; do
  529. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  530. break
  531. done
  532. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  533. -n "SolidrunClearfog" \
  534. -d $fwdir/boot.script.clearfog $R/boot/boot.scr.uimg
  535. ;;
  536. (solidrun-imx6)
  537. for x in "$fwdir"/*.dtb; do
  538. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  539. break
  540. done
  541. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  542. -n "SolidrunImx6" \
  543. -d $fwdir/boot.script.imx6 $R/boot/boot.scr.uimg
  544. ;;
  545. (banana-pro)
  546. for x in "$fwdir"/*.dtb; do
  547. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  548. break
  549. done
  550. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  551. -n "BananaPro" \
  552. -d $fwdir/boot.script.bpi $R/boot/boot.scr.uimg
  553. ;;
  554. }
  555. cd "$R"
  556. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  557. (( quiet )) || print Fixing up permissions...
  558. chown 0:0 tmp
  559. chmod 1777 tmp
  560. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  561. if (( grub )); then
  562. (( quiet )) || print Configuring GRUB2 bootloader...
  563. mkdir -p boot/grub
  564. (
  565. print set default=0
  566. print set timeout=1
  567. if (( serial )); then
  568. print serial --unit=0 --speed=$speed
  569. print terminal_output serial
  570. print terminal_input serial
  571. consargs="console=ttyS0,$speed console=tty0"
  572. else
  573. print terminal_output console
  574. print terminal_input console
  575. consargs="console=tty0"
  576. fi
  577. print
  578. print 'menuentry "GNU/Linux (OpenADK)" {'
  579. linuxargs="root=PARTUUID=$partuuid $consargs"
  580. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  581. print "\tlinux /boot/kernel $linuxargs"
  582. print '}'
  583. ) >boot/grub/grub.cfg
  584. fi
  585. (( quiet )) || print Finishing up...
  586. cd "$ADK_TOPDIR"
  587. umount_fs "$R"
  588. if (( datafssz )); then
  589. umount_fs $datapart
  590. fi
  591. rm -rf "$T"
  592. exit 0