install.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #!/usr/bin/env bash
  2. #-
  3. # Copyright © 2010-2017
  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.ext4 mkfs.vfat 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|orange-pi0|pcengines-apu|phytec-wega|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 || $target = phytec-wega ]]; 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. (( quiet )) || print "Unmounting filesystem on ${1}..."
  165. diskutil unmount "$1"
  166. }
  167. function create_fs {
  168. (( quiet )) || print "Creating filesystem on ${2}..."
  169. if [[ $3 = ext4 ]]; then
  170. fstype=UFSD_EXTFS4
  171. fi
  172. if [[ $3 = vfat ]]; then
  173. fstype=fat32
  174. fi
  175. diskutil eraseVolume $fstype "$2" "$1"
  176. }
  177. function tune_fs {
  178. }
  179. ;;
  180. (Linux)
  181. basedev=$tgt
  182. partitionsep=""
  183. if [[ $basedev = /dev/loop* ]]; then
  184. (( quiet )) || print "${tgt} is a loop device"
  185. partitionsep=p
  186. fi
  187. rootpart=${basedev}${partitionsep}1
  188. datapart=${basedev}${partitionsep}2
  189. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 ]]; then
  190. bootpart=${basedev}${partitionsep}1
  191. rootpart=${basedev}${partitionsep}2
  192. datapart=${basedev}${partitionsep}3
  193. fi
  194. match=\'${basedev}${partitionsep}\''+([0-9])'
  195. function mount_fs {
  196. (( quiet )) || print "Mounting filesystem on ${1}..."
  197. mount -t "$3" "$1" "$2"
  198. }
  199. function umount_fs {
  200. (( quiet )) || print "Unmounting filesystem on ${1}..."
  201. umount "$1"
  202. }
  203. function create_fs {
  204. (( quiet )) || print "Creating filesystem on ${1}..."
  205. mkfs.$3 "$1"
  206. }
  207. function tune_fs {
  208. tune2fs -c 0 -i 0 "$1"
  209. }
  210. ;;
  211. }
  212. mount |&
  213. while read -p dev rest; do
  214. eval [[ \$dev = $match ]] || continue
  215. print -u2 "Block device $tgt is in use, please umount first."
  216. exit 1
  217. done
  218. if (( !quiet )); then
  219. print "WARNING: This will overwrite $basedev - type Yes to continue!"
  220. read x
  221. [[ $x = Yes ]] || exit 0
  222. fi
  223. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  224. print -u2 Error creating temporary directory.
  225. exit 1
  226. fi
  227. if [[ $ostype != Darwin ]]; then
  228. R=$T/rootmnt
  229. B=$T/bootmnt
  230. D=$T/datamnt
  231. mkdir -p "$R" "$B" "$D"
  232. fi
  233. # get disk size
  234. dksz=$(dkgetsz "$tgt")
  235. # partition layouts:
  236. # n̲a̲m̲e̲ p̲a̲r̲t̲#̲0̲ p̲̲a̲r̲t̲#̲1̲ p̲̲a̲r̲t̲#̲2̲ p̲̲a̲r̲t̲#̲3̲
  237. # default: 0x83(system) 0x83(?data) -(unused) 0x88(cfgfs)
  238. # raspberry: 0x0B(boot) 0x83(system) 0x83(?data) 0x88(cfgfs)
  239. syspartno=0
  240. # sizes:
  241. # boot(raspberry) - fixed (100 MiB)
  242. # cfgfs - fixed (parameter, max. 16 MiB)
  243. # data - flexible (parameter)
  244. # system - everything else
  245. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 || $target = phytec-wega ]]; then
  246. syspartno=1
  247. bootfssz=100
  248. if (( grub )); then
  249. print -u2 "Cannot combine GRUB with $target"
  250. rm -rf "$T"
  251. exit 1
  252. fi
  253. else
  254. bootfssz=0
  255. fi
  256. heads=64
  257. secs=32
  258. (( cyls = dksz / heads / secs ))
  259. if (( cyls < (bootfssz + cfgfs + datafssz + 2) )); then
  260. print -u2 "Size of $tgt is $dksz, this looks fishy?"
  261. rm -rf "$T"
  262. exit 1
  263. fi
  264. if stat -qs .>/dev/null 2>&1; then
  265. statcmd='stat -f %z' # BSD stat (or so we assume)
  266. else
  267. statcmd='stat -c %s' # GNU stat
  268. fi
  269. if (( grub )); then
  270. tar -xOf "$src" boot/grub/core.img >"$T/core.img"
  271. integer coreimgsz=$($statcmd "$T/core.img")
  272. else
  273. coreimgsz=65024
  274. fi
  275. if (( coreimgsz < 1024 )); then
  276. print -u2 core.img is probably too small: $coreimgsz
  277. rm -rf "$T"
  278. exit 1
  279. fi
  280. if (( coreimgsz > 65024 )); then
  281. print -u2 core.img is larger than 64K-512: $coreimgsz
  282. rm -rf "$T"
  283. exit 1
  284. fi
  285. (( coreendsec = (coreimgsz + 511) / 512 ))
  286. if [[ $basedev = /dev/svnd+([0-9]) ]]; then
  287. # BSD svnd0 mode: protect sector #1
  288. corestartsec=2
  289. (( ++coreendsec ))
  290. corepatchofs=$((0x614))
  291. else
  292. corestartsec=1
  293. corepatchofs=$((0x414))
  294. fi
  295. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  296. #(( partofs = ((coreendsec / secs) + 1) * secs ))
  297. # we just use 2048 all the time, since some loaders are longer
  298. partofs=2048
  299. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 || $target = phytec-wega ]]; then
  300. (( spartofs = partofs + (100 * 2048) ))
  301. else
  302. spartofs=$partofs
  303. fi
  304. (( quiet )) || if (( grub )); then
  305. print Preparing MBR and GRUB2...
  306. else
  307. print Preparing MBR...
  308. fi
  309. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  310. # add another MiB to clear the first partition
  311. dd if=/dev/zero bs=1048576 count=1 >>"$T/firsttrack" 2>/dev/null
  312. echo $corestartsec $coreendsec | mksh "$ADK_TOPDIR/scripts/bootgrub.mksh" \
  313. -A -g $((cyls - bootfssz - cfgfs - datafssz)):$heads:$secs -M 1:0x83 \
  314. -O $spartofs | dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  315. (( grub )) && dd if="$T/core.img" of="$T/firsttrack" conv=notrunc \
  316. seek=$corestartsec 2>/dev/null
  317. # set partition where it can find /boot/grub
  318. (( grub )) && print -n '\0\0\0\0' | \
  319. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs 2>/dev/null
  320. # create cfgfs partition (mostly taken from bootgrub.mksh)
  321. set -A thecode
  322. typeset -Uui8 thecode
  323. mbrpno=0
  324. set -A g_code $cyls $heads $secs
  325. (( psz = g_code[0] * g_code[1] * g_code[2] ))
  326. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  327. set -A o_code # g_code equivalent for partition offset
  328. (( o_code[2] = pofs % g_code[2] + 1 ))
  329. (( o_code[1] = pofs / g_code[2] ))
  330. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  331. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  332. # boot flag; C/H/S offset
  333. thecode[mbrpno++]=0x00
  334. (( thecode[mbrpno++] = o_code[1] - 1 ))
  335. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  336. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  337. (( thecode[mbrpno++] = cylno & 0x00FF ))
  338. # partition type; C/H/S end
  339. (( thecode[mbrpno++] = 0x88 ))
  340. (( thecode[mbrpno++] = g_code[1] - 1 ))
  341. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  342. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  343. (( thecode[mbrpno++] = cylno & 0x00FF ))
  344. # partition offset, size (LBA)
  345. (( thecode[mbrpno++] = pofs & 0xFF ))
  346. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  347. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  348. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  349. (( pssz = psz - pofs ))
  350. (( thecode[mbrpno++] = pssz & 0xFF ))
  351. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  352. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  353. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  354. # write partition table entry
  355. ostr=
  356. curptr=0
  357. while (( curptr < 16 )); do
  358. ostr=$ostr\\0${thecode[curptr++]#8#}
  359. done
  360. print -n "$ostr" | \
  361. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1EE)) 2>/dev/null
  362. if (( datafssz )); then
  363. # create data partition (copy of the above :)
  364. set -A thecode
  365. typeset -Uui8 thecode
  366. mbrpno=0
  367. set -A g_code $cyls $heads $secs
  368. (( psz = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  369. (( pofs = (cyls - cfgfs - datafssz) * g_code[1] * g_code[2] ))
  370. set -A o_code # g_code equivalent for partition offset
  371. (( o_code[2] = pofs % g_code[2] + 1 ))
  372. (( o_code[1] = pofs / g_code[2] ))
  373. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  374. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  375. # boot flag; C/H/S offset
  376. thecode[mbrpno++]=0x00
  377. (( thecode[mbrpno++] = o_code[1] - 1 ))
  378. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  379. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  380. (( thecode[mbrpno++] = cylno & 0x00FF ))
  381. # partition type; C/H/S end
  382. (( thecode[mbrpno++] = 0x83 ))
  383. (( thecode[mbrpno++] = g_code[1] - 1 ))
  384. (( cylno = (g_code[0] - cfgfs) > 1024 ? 1023 : g_code[0] - cfgfs - 1 ))
  385. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  386. (( thecode[mbrpno++] = cylno & 0x00FF ))
  387. # partition offset, size (LBA)
  388. (( thecode[mbrpno++] = pofs & 0xFF ))
  389. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  390. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  391. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  392. (( pssz = psz - pofs ))
  393. (( thecode[mbrpno++] = pssz & 0xFF ))
  394. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  395. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  396. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  397. # write partition table entry
  398. ostr=
  399. curptr=0
  400. while (( curptr < 16 )); do
  401. ostr=$ostr\\0${thecode[curptr++]#8#}
  402. done
  403. print -n "$ostr" | \
  404. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  405. fi
  406. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 || $target = phytec-wega ]]; then
  407. # move system and data partition from #0/#1 to #1/#2
  408. dd if="$T/firsttrack" bs=1 skip=$((0x1BE)) count=32 of="$T/x" 2>/dev/null
  409. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) if="$T/x" 2>/dev/null
  410. # create boot partition (copy of the above :)
  411. set -A thecode
  412. typeset -Uui8 thecode
  413. mbrpno=0
  414. set -A g_code $cyls $heads $secs
  415. psz=$spartofs
  416. pofs=$partofs
  417. set -A o_code # g_code equivalent for partition offset
  418. (( o_code[2] = pofs % g_code[2] + 1 ))
  419. (( o_code[1] = pofs / g_code[2] ))
  420. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  421. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  422. # boot flag; C/H/S offset
  423. thecode[mbrpno++]=0x00
  424. (( thecode[mbrpno++] = o_code[1] - 1 ))
  425. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  426. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  427. (( thecode[mbrpno++] = cylno & 0x00FF ))
  428. # partition type; C/H/S end
  429. (( thecode[mbrpno++] = 0x0B ))
  430. (( thecode[mbrpno++] = g_code[1] - 1 ))
  431. (( cylno = (spartofs / 2048) > 1024 ? 1023 : (spartofs / 2048) - 1 ))
  432. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  433. (( thecode[mbrpno++] = cylno & 0x00FF ))
  434. # partition offset, size (LBA)
  435. (( thecode[mbrpno++] = pofs & 0xFF ))
  436. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  437. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  438. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  439. (( pssz = psz - pofs ))
  440. (( thecode[mbrpno++] = pssz & 0xFF ))
  441. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  442. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  443. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  444. # write partition table entry
  445. ostr=
  446. curptr=0
  447. while (( curptr < 16 )); do
  448. ostr=$ostr\\0${thecode[curptr++]#8#}
  449. done
  450. print -n "$ostr" | \
  451. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1BE)) 2>/dev/null
  452. fi
  453. # disk signature
  454. rnddev=/dev/urandom
  455. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  456. dd if=$rnddev bs=4 count=1 2>/dev/null | \
  457. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1B8)) 2>/dev/null
  458. print -n '\0\0' | \
  459. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1BC)) 2>/dev/null
  460. partuuid=$(dd if="$T/firsttrack" bs=1 count=4 skip=$((0x1B8)) 2>/dev/null | \
  461. hexdump -e '1/4 "%08x"')-0$((syspartno+1))
  462. ((keep)) || if (( datafssz )); then
  463. (( quiet )) || print Cleaning out data partition...
  464. dd if=/dev/zero of="$tgt" bs=1048576 count=1 seek=$((cyls - cfgfs - datafssz)) > /dev/null 2>&1
  465. fi
  466. (( quiet )) || print Cleaning out root partition...
  467. dd if=/dev/zero bs=1048576 of="$tgt" count=1 seek=$((spartofs / 2048)) > /dev/null 2>&1
  468. (( quiet )) || if (( grub )); then
  469. print Writing MBR and GRUB2 to target device... system PARTUUID=$partuuid
  470. else
  471. print Writing MBR to target device... system PARTUUID=$partuuid
  472. fi
  473. dd if="$T/firsttrack" of="$tgt" > /dev/null 2>&1
  474. if [[ $ostype = Linux ]]; then
  475. echo partprobe $tgt
  476. partprobe -s $tgt
  477. sync
  478. fdisk -l $tgt
  479. ls -la ${tgt}*
  480. fi
  481. fwdir=$(dirname "$src")
  482. case $target {
  483. (banana-pro|orange-pi0)
  484. dd if="$fwdir/u-boot-sunxi-with-spl.bin" of="$tgt" bs=1024 seek=8 > /dev/null 2>&1
  485. ;;
  486. (solidrun-clearfog)
  487. dd if="$fwdir/u-boot-spl.kwb" of="$tgt" bs=512 seek=1 > /dev/null 2>&1
  488. ;;
  489. (solidrun-imx6)
  490. dd if="$fwdir/SPL" of="$tgt" bs=1024 seek=1 > /dev/null 2>&1
  491. dd if="$fwdir/u-boot.img" of="$tgt" bs=1024 seek=69 > /dev/null 2>&1
  492. ;;
  493. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64)
  494. (( noformat )) || create_fs "$bootpart" ADKBOOT vfat
  495. ;;
  496. (phytec-wega)
  497. (( noformat )) || create_fs "$bootpart" ADKBOOT ext4
  498. ;;
  499. }
  500. (( noformat )) || create_fs "$rootpart" ADKROOT ext4
  501. (( noformat )) || tune_fs "$rootpart"
  502. (( quiet )) || print Extracting installation archive...
  503. mount_fs "$rootpart" "$R" ext4
  504. xz -dc "$src" | (cd "$R"; tar -xpf -)
  505. if (( datafssz )); then
  506. mkdir -m0755 "$R"/data
  507. ((keep)) || create_fs "$datapart" ADKDATA ext4
  508. ((keep)) || tune_fs "$datapart"
  509. case $target {
  510. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|phytec-wega)
  511. echo "/dev/mmcblk0p3 /data ext4 rw 0 0" >> "$R"/etc/fstab
  512. ;;
  513. (banana-pro|orange-pi0|solidrun-imx6|solidrun-clearfog)
  514. echo "/dev/mmcblk0p2 /data ext4 rw 0 0" >> "$R"/etc/fstab
  515. ;;
  516. }
  517. fi
  518. (( quiet )) || print Finishing up with bootloader and kernel ...
  519. case $target {
  520. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64)
  521. mount_fs "$bootpart" "$B" vfat
  522. for x in "$R"/boot/*; do
  523. [[ -e "$x" ]] && mv -f "$R"/boot/* "$B/"
  524. break
  525. done
  526. for x in "$fwdir"/*.dtb; do
  527. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$B/"
  528. break
  529. done
  530. # use static dtoverlay, rename to *.dtb
  531. mkdir "$B/"overlays
  532. for x in "$fwdir"/overlays/*.dtbo; do
  533. y=$(basename ${x} .dtbo)
  534. [[ -e "$x" ]] && cp "$fwdir"/overlays/${y}.dtbo "$B/"overlays/${y}.dtb
  535. done
  536. umount_fs "$B"
  537. ;;
  538. (phytec-wega)
  539. mount_fs "$bootpart" "$B" ext4
  540. for x in "$R"/boot/*; do
  541. [[ -e "$x" ]] && mv -f "$R"/boot/* "$B/"
  542. break
  543. done
  544. for x in "$fwdir"/*.dtb; do
  545. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$B/"
  546. break
  547. done
  548. umount_fs "$B"
  549. ;;
  550. (solidrun-clearfog)
  551. for x in "$fwdir"/*.dtb; do
  552. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  553. break
  554. done
  555. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  556. -n "SolidrunClearfog" \
  557. -d $fwdir/boot.script.clearfog $R/boot/boot.scr.uimg
  558. ;;
  559. (solidrun-imx6)
  560. for x in "$fwdir"/*.dtb; do
  561. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  562. break
  563. done
  564. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  565. -n "SolidrunImx6" \
  566. -d $fwdir/boot.script.imx6 $R/boot/boot.scr.uimg
  567. ;;
  568. (orange-pi0)
  569. for x in "$fwdir"/*.dtb; do
  570. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  571. break
  572. done
  573. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  574. -n "OrangePI Zero" \
  575. -d $fwdir/boot.script.opi $R/boot/boot.scr.uimg
  576. ;;
  577. (banana-pro)
  578. for x in "$fwdir"/*.dtb; do
  579. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  580. break
  581. done
  582. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  583. -n "BananaPro" \
  584. -d $fwdir/boot.script.bpi $R/boot/boot.scr.uimg
  585. ;;
  586. }
  587. cd "$R"
  588. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  589. (( quiet )) || print Fixing up permissions...
  590. chown 0:0 tmp
  591. chmod 1777 tmp
  592. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  593. if (( grub )); then
  594. (( quiet )) || print Configuring GRUB2 bootloader...
  595. mkdir -p boot/grub
  596. (
  597. print set default=0
  598. print set timeout=1
  599. if (( serial )); then
  600. print serial --unit=0 --speed=$speed
  601. print terminal_output serial
  602. print terminal_input serial
  603. consargs="console=ttyS0,$speed console=tty0"
  604. else
  605. print terminal_output console
  606. print terminal_input console
  607. consargs="console=tty0"
  608. fi
  609. print
  610. print 'menuentry "GNU/Linux (OpenADK)" {'
  611. linuxargs="root=PARTUUID=$partuuid $consargs"
  612. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  613. print "\tlinux /boot/kernel $linuxargs"
  614. print '}'
  615. ) >boot/grub/grub.cfg
  616. fi
  617. (( quiet )) || print Finishing up...
  618. cd "$ADK_TOPDIR"
  619. umount_fs "$R"
  620. if (( datafssz )); then
  621. umount_fs $datapart
  622. fi
  623. rm -rf "$T"
  624. exit 0