install.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. paragon_ext=0
  59. datapartcontent=""
  60. function usage {
  61. cat >&2 <<EOF
  62. Syntax: $me [-f filesystem] [-c cfgfssize] [-d datafssize] [-D datafscontent] [-k] [-n] [-g]
  63. [-p panictime] [±q] [-s serialspeed] [±t] <target> <device> <archive>
  64. Partition sizes are in MiB. Filesystem type is currently ignored (ext4).
  65. To keep filesystem on data partition use -k.
  66. Use -n to not format boot/root partition.
  67. Defaults: -c 1 -p 10 -s 115200; -t = enable serial console
  68. EOF
  69. exit $1
  70. }
  71. while getopts "c:d:D:ef:ghknp:qs:tx:" ch; do
  72. case $ch {
  73. (c) if (( (cfgfs = OPTARG) < 0 || cfgfs > 16 )); then
  74. print -u2 "$me: -c $OPTARG out of bounds"
  75. exit 1
  76. fi ;;
  77. (d) if (( (datafssz = OPTARG) < 0 )); then
  78. print -u2 "$me: -d $OPTARG out of bounds"
  79. exit 1
  80. fi ;;
  81. (e) paragon_ext=1 ;;
  82. (f) if [[ $OPTARG != @(ext2|ext3|ext4|xfs) ]]; then
  83. print -u2 "$me: filesystem $OPTARG invalid"
  84. exit 1
  85. fi
  86. fs=$OPTARG ;;
  87. (h) usage 0 ;;
  88. (k) keep=1 ;;
  89. (g) grub=1 ;;
  90. (p) if (( (panicreboot = OPTARG) < 0 || panicreboot > 300 )); then
  91. print -u2 "$me: -p $OPTARG out of bounds"
  92. exit 1
  93. fi ;;
  94. (q) quiet=1 ;;
  95. (+q) quiet=0 ;;
  96. (s) if [[ $OPTARG != @(96|192|384|576|1152)00 ]]; then
  97. print -u2 "$me: serial speed $OPTARG invalid"
  98. exit 1
  99. fi
  100. speed=$OPTARG ;;
  101. (n) noformat=1 ;;
  102. (t) serial=1 ;;
  103. (+t) serial=0 ;;
  104. (D) if [[ ! -d $OPTARG ]]; then
  105. print -u2 "$me: -D $OPTARG must be an existing directory"
  106. exit 1
  107. fi
  108. datapartcontent=$OPTARG;;
  109. (*) usage 1 ;;
  110. }
  111. done
  112. shift $((OPTIND - 1))
  113. (( $# == 3 )) || usage 1
  114. f=0
  115. case $ostype {
  116. (Linux)
  117. tools="bc mkfs.ext4 mkfs.vfat tune2fs partprobe"
  118. ;;
  119. (Darwin)
  120. tools="bc diskutil"
  121. if [[ $paragon_ext == 0 ]]; then
  122. export PATH=/usr/local/opt/e2fsprogs/sbin:/usr/local/opt/e2fsprogs/bin:$PATH
  123. tools="$tools fuse-ext2 umount mkfs.ext4 tune2fs"
  124. fi
  125. ;;
  126. (*)
  127. print -u2 Sorry, not ported to the OS "'$ostype'" yet.
  128. exit 1
  129. ;;
  130. }
  131. for tool in $tools; do
  132. print -n Checking if $tool is installed...
  133. if whence -p $tool >/dev/null; then
  134. print " okay"
  135. else
  136. print " failed"
  137. f=1
  138. fi
  139. done
  140. (( f )) && exit 1
  141. target=$1
  142. tgt=$2
  143. src=$3
  144. case $target {
  145. (banana-pro|orange-pi0|pcengines-apu|phytec-wega|raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|raspberry-pi3p|raspberry-pi3p-64|solidrun-imx6|solidrun-clearfog|default) ;;
  146. (*)
  147. print -u2 "Unknown target '$target', exiting"
  148. exit 1 ;;
  149. }
  150. if [[ ! -b $tgt ]]; then
  151. print -u2 "'$tgt' is not a block device, exiting"
  152. exit 1
  153. fi
  154. if [[ ! -f $src ]]; then
  155. print -u2 "'$src' is not a file, exiting"
  156. exit 1
  157. fi
  158. (( quiet )) || print "Installing $src on $tgt."
  159. case $ostype {
  160. (Darwin)
  161. R=/Volumes/ADKROOT; diskutil unmount $R || umount $R
  162. B=/Volumes/ADKBOOT; diskutil unmount $B || umount $B
  163. D=/Volumes/ADKDATA; diskutil unmount $D || umount $D
  164. basedev=$tgt
  165. rootpart=${basedev}s1
  166. datapart=${basedev}s2
  167. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 || $target = raspberry-pi3p || $target = raspberry-pi3p-64 || $target = phytec-wega ]]; then
  168. bootpart=${basedev}s1
  169. rootpart=${basedev}s2
  170. datapart=${basedev}s3
  171. fi
  172. match=\'${basedev}\''?(s+([0-9]))'
  173. function mount_fs {
  174. if [[ $paragon_ext == 0 && $3 = ext4 ]]; then
  175. mkdir -p $2
  176. fuse-ext2 "$1" "$2" -o rw+
  177. fi
  178. }
  179. function umount_fs {
  180. (( quiet )) || print "Unmounting filesystem on ${1}..."
  181. if [[ $paragon_ext == 0 ]]; then
  182. umount "$1" || diskutil unmount "$1" || true
  183. rmdir $2 || true
  184. else
  185. diskutil unmount "$1"
  186. fi
  187. }
  188. function create_fs {
  189. (( quiet )) || printf "Creating filesystem on ${1}"
  190. if [[ $paragon_ext == 0 && $3 = ext4 ]]; then
  191. mkfs.ext4 -L "$2" "$1"
  192. else
  193. if [[ $3 = ext4 ]]; then
  194. fstype=UFSD_EXTFS4
  195. fi
  196. if [[ $3 = vfat ]]; then
  197. fstype=fat32
  198. fi
  199. diskutil eraseVolume $fstype "$2" "$1"
  200. fi
  201. }
  202. function tune_fs {
  203. if [[ $paragon_ext == 0 && $3 = ext4 ]]; then
  204. tune2fs -c 0 -i 0 "$1"
  205. fi
  206. }
  207. ;;
  208. (Linux)
  209. basedev=$tgt
  210. partitionsep=""
  211. if [[ $basedev = /dev/loop* ]]; then
  212. (( quiet )) || print "${tgt} is a loop device"
  213. partitionsep=p
  214. fi
  215. rootpart=${basedev}${partitionsep}1
  216. datapart=${basedev}${partitionsep}2
  217. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 || $target = raspberry-pi3p || $target = raspberry-pi3p-64 ]]; then
  218. bootpart=${basedev}${partitionsep}1
  219. rootpart=${basedev}${partitionsep}2
  220. datapart=${basedev}${partitionsep}3
  221. fi
  222. match=\'${basedev}${partitionsep}\''+([0-9])'
  223. function mount_fs {
  224. (( quiet )) || print "Mounting filesystem on ${1}..."
  225. mount -t "$3" "$1" "$2"
  226. }
  227. function umount_fs {
  228. (( quiet )) || print "Unmounting filesystem on ${1}..."
  229. umount "$1"
  230. }
  231. function create_fs {
  232. (( quiet )) || print "Creating filesystem on ${1}..."
  233. mkfs.$3 "$1"
  234. }
  235. function tune_fs {
  236. tune2fs -c 0 -i 0 "$1"
  237. }
  238. ;;
  239. }
  240. mount |&
  241. while read -p dev rest; do
  242. eval [[ \$dev = $match ]] || continue
  243. print -u2 "Block device $tgt is in use, please umount first."
  244. exit 1
  245. done
  246. if (( !quiet )); then
  247. print "WARNING: This will overwrite $basedev - type Yes to continue!"
  248. read x
  249. [[ $x = Yes ]] || exit 0
  250. fi
  251. if ! T=$(mktemp -d /tmp/openadk.XXXXXXXXXX); then
  252. print -u2 Error creating temporary directory.
  253. exit 1
  254. fi
  255. if [[ $ostype != Darwin ]]; then
  256. R=$T/rootmnt
  257. B=$T/bootmnt
  258. D=$T/datamnt
  259. mkdir -p "$R" "$B" "$D"
  260. fi
  261. # get disk size
  262. dksz=$(dkgetsz "$tgt")
  263. # partition layouts:
  264. # n̲a̲m̲e̲ p̲a̲r̲t̲#̲0̲ p̲̲a̲r̲t̲#̲1̲ p̲̲a̲r̲t̲#̲2̲ p̲̲a̲r̲t̲#̲3̲
  265. # default: 0x83(system) 0x83(?data) -(unused) 0x88(cfgfs)
  266. # raspberry: 0x0B(boot) 0x83(system) 0x83(?data) 0x88(cfgfs)
  267. syspartno=0
  268. # sizes:
  269. # boot(raspberry) - fixed (100 MiB)
  270. # cfgfs - fixed (parameter, max. 16 MiB)
  271. # data - flexible (parameter)
  272. # system - everything else
  273. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 || $target = raspberry-pi3p || $target = raspberry-pi3p-64 || $target = phytec-wega ]]; then
  274. syspartno=1
  275. bootfssz=100
  276. if (( grub )); then
  277. print -u2 "Cannot combine GRUB with $target"
  278. rm -rf "$T"
  279. exit 1
  280. fi
  281. else
  282. bootfssz=0
  283. fi
  284. heads=64
  285. secs=32
  286. (( cyls = dksz / heads / secs ))
  287. if (( cyls < (bootfssz + cfgfs + datafssz + 2) )); then
  288. print -u2 "Size of $tgt is $dksz, this looks fishy?"
  289. rm -rf "$T"
  290. exit 1
  291. fi
  292. if stat -qs .>/dev/null 2>&1; then
  293. statcmd='stat -f %z' # BSD stat (or so we assume)
  294. else
  295. statcmd='stat -c %s' # GNU stat
  296. fi
  297. if (( grub )); then
  298. tar -xOf "$src" boot/grub/core.img >"$T/core.img"
  299. integer coreimgsz=$($statcmd "$T/core.img")
  300. else
  301. coreimgsz=65024
  302. fi
  303. if (( coreimgsz < 1024 )); then
  304. print -u2 core.img is probably too small: $coreimgsz
  305. rm -rf "$T"
  306. exit 1
  307. fi
  308. if (( coreimgsz > 65024 )); then
  309. print -u2 core.img is larger than 64K-512: $coreimgsz
  310. rm -rf "$T"
  311. exit 1
  312. fi
  313. (( coreendsec = (coreimgsz + 511) / 512 ))
  314. if [[ $basedev = /dev/svnd+([0-9]) ]]; then
  315. # BSD svnd0 mode: protect sector #1
  316. corestartsec=2
  317. (( ++coreendsec ))
  318. corepatchofs=$((0x614))
  319. else
  320. corestartsec=1
  321. corepatchofs=$((0x414))
  322. fi
  323. # partition offset: at least coreendsec+1 but aligned on a multiple of secs
  324. #(( partofs = ((coreendsec / secs) + 1) * secs ))
  325. # we just use 2048 all the time, since some loaders are longer
  326. partofs=2048
  327. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 || $target = raspberry-pi3p || $target = raspberry-pi3p-64 || $target = phytec-wega ]]; then
  328. (( spartofs = partofs + (100 * 2048) ))
  329. else
  330. spartofs=$partofs
  331. fi
  332. (( quiet )) || if (( grub )); then
  333. print Preparing MBR and GRUB2...
  334. else
  335. print Preparing MBR...
  336. fi
  337. dd if=/dev/zero of="$T/firsttrack" count=$partofs 2>/dev/null
  338. # add another MiB to clear the first partition
  339. dd if=/dev/zero bs=1048576 count=1 >>"$T/firsttrack" 2>/dev/null
  340. echo $corestartsec $coreendsec | mksh "$ADK_TOPDIR/scripts/bootgrub.mksh" \
  341. -A -g $((cyls - bootfssz - cfgfs - datafssz)):$heads:$secs -M 1:0x83 \
  342. -O $spartofs | dd of="$T/firsttrack" conv=notrunc 2>/dev/null
  343. (( grub )) && dd if="$T/core.img" of="$T/firsttrack" conv=notrunc \
  344. seek=$corestartsec 2>/dev/null
  345. # set partition where it can find /boot/grub
  346. (( grub )) && print -n '\0\0\0\0' | \
  347. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$corepatchofs 2>/dev/null
  348. # create cfgfs partition (mostly taken from bootgrub.mksh)
  349. set -A thecode
  350. typeset -Uui8 thecode
  351. mbrpno=0
  352. set -A g_code $cyls $heads $secs
  353. (( psz = g_code[0] * g_code[1] * g_code[2] ))
  354. (( pofs = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  355. set -A o_code # g_code equivalent for partition offset
  356. (( o_code[2] = pofs % g_code[2] + 1 ))
  357. (( o_code[1] = pofs / g_code[2] ))
  358. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  359. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  360. # boot flag; C/H/S offset
  361. thecode[mbrpno++]=0x00
  362. (( thecode[mbrpno++] = o_code[1] - 1 ))
  363. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  364. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  365. (( thecode[mbrpno++] = cylno & 0x00FF ))
  366. # partition type; C/H/S end
  367. (( thecode[mbrpno++] = 0x88 ))
  368. (( thecode[mbrpno++] = g_code[1] - 1 ))
  369. (( cylno = g_code[0] > 1024 ? 1023 : g_code[0] - 1 ))
  370. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  371. (( thecode[mbrpno++] = cylno & 0x00FF ))
  372. # partition offset, size (LBA)
  373. (( thecode[mbrpno++] = pofs & 0xFF ))
  374. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  375. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  376. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  377. (( pssz = psz - pofs ))
  378. (( thecode[mbrpno++] = pssz & 0xFF ))
  379. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  380. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  381. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  382. # write partition table entry
  383. ostr=
  384. curptr=0
  385. while (( curptr < 16 )); do
  386. ostr=$ostr\\0${thecode[curptr++]#8#}
  387. done
  388. print -n "$ostr" | \
  389. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1EE)) 2>/dev/null
  390. if (( datafssz )); then
  391. # create data partition (copy of the above :)
  392. set -A thecode
  393. typeset -Uui8 thecode
  394. mbrpno=0
  395. set -A g_code $cyls $heads $secs
  396. (( psz = (cyls - cfgfs) * g_code[1] * g_code[2] ))
  397. (( pofs = (cyls - cfgfs - datafssz) * g_code[1] * g_code[2] ))
  398. set -A o_code # g_code equivalent for partition offset
  399. (( o_code[2] = pofs % g_code[2] + 1 ))
  400. (( o_code[1] = pofs / g_code[2] ))
  401. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  402. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  403. # boot flag; C/H/S offset
  404. thecode[mbrpno++]=0x00
  405. (( thecode[mbrpno++] = o_code[1] - 1 ))
  406. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  407. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  408. (( thecode[mbrpno++] = cylno & 0x00FF ))
  409. # partition type; C/H/S end
  410. (( thecode[mbrpno++] = 0x83 ))
  411. (( thecode[mbrpno++] = g_code[1] - 1 ))
  412. (( cylno = (g_code[0] - cfgfs) > 1024 ? 1023 : g_code[0] - cfgfs - 1 ))
  413. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  414. (( thecode[mbrpno++] = cylno & 0x00FF ))
  415. # partition offset, size (LBA)
  416. (( thecode[mbrpno++] = pofs & 0xFF ))
  417. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  418. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  419. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  420. (( pssz = psz - pofs ))
  421. (( thecode[mbrpno++] = pssz & 0xFF ))
  422. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  423. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  424. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  425. # write partition table entry
  426. ostr=
  427. curptr=0
  428. while (( curptr < 16 )); do
  429. ostr=$ostr\\0${thecode[curptr++]#8#}
  430. done
  431. print -n "$ostr" | \
  432. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) 2>/dev/null
  433. fi
  434. if [[ $target = raspberry-pi || $target = raspberry-pi0 || $target = raspberry-pi2 || $target = raspberry-pi3 || $target = raspberry-pi3-64 || $target = raspberry-pi3p || $target = raspberry-pi3p-64 || $target = phytec-wega ]]; then
  435. # move system and data partition from #0/#1 to #1/#2
  436. dd if="$T/firsttrack" bs=1 skip=$((0x1BE)) count=32 of="$T/x" 2>/dev/null
  437. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1CE)) if="$T/x" 2>/dev/null
  438. # create boot partition (copy of the above :)
  439. set -A thecode
  440. typeset -Uui8 thecode
  441. mbrpno=0
  442. set -A g_code $cyls $heads $secs
  443. psz=$spartofs
  444. pofs=$partofs
  445. set -A o_code # g_code equivalent for partition offset
  446. (( o_code[2] = pofs % g_code[2] + 1 ))
  447. (( o_code[1] = pofs / g_code[2] ))
  448. (( o_code[0] = o_code[1] / g_code[1] + 1 ))
  449. (( o_code[1] = o_code[1] % g_code[1] + 1 ))
  450. # boot flag; C/H/S offset
  451. thecode[mbrpno++]=0x00
  452. (( thecode[mbrpno++] = o_code[1] - 1 ))
  453. (( cylno = o_code[0] > 1024 ? 1023 : o_code[0] - 1 ))
  454. (( thecode[mbrpno++] = o_code[2] | ((cylno & 0x0300) >> 2) ))
  455. (( thecode[mbrpno++] = cylno & 0x00FF ))
  456. # partition type; C/H/S end
  457. (( thecode[mbrpno++] = 0x0B ))
  458. (( thecode[mbrpno++] = g_code[1] - 1 ))
  459. (( cylno = (spartofs / 2048) > 1024 ? 1023 : (spartofs / 2048) - 1 ))
  460. (( thecode[mbrpno++] = g_code[2] | ((cylno & 0x0300) >> 2) ))
  461. (( thecode[mbrpno++] = cylno & 0x00FF ))
  462. # partition offset, size (LBA)
  463. (( thecode[mbrpno++] = pofs & 0xFF ))
  464. (( thecode[mbrpno++] = (pofs >> 8) & 0xFF ))
  465. (( thecode[mbrpno++] = (pofs >> 16) & 0xFF ))
  466. (( thecode[mbrpno++] = (pofs >> 24) & 0xFF ))
  467. (( pssz = psz - pofs ))
  468. (( thecode[mbrpno++] = pssz & 0xFF ))
  469. (( thecode[mbrpno++] = (pssz >> 8) & 0xFF ))
  470. (( thecode[mbrpno++] = (pssz >> 16) & 0xFF ))
  471. (( thecode[mbrpno++] = (pssz >> 24) & 0xFF ))
  472. # write partition table entry
  473. ostr=
  474. curptr=0
  475. while (( curptr < 16 )); do
  476. ostr=$ostr\\0${thecode[curptr++]#8#}
  477. done
  478. print -n "$ostr" | \
  479. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1BE)) 2>/dev/null
  480. fi
  481. # disk signature
  482. rnddev=/dev/urandom
  483. [[ -c /dev/arandom ]] && rnddev=/dev/arandom
  484. dd if=$rnddev bs=4 count=1 2>/dev/null | \
  485. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1B8)) 2>/dev/null
  486. print -n '\0\0' | \
  487. dd of="$T/firsttrack" conv=notrunc bs=1 seek=$((0x1BC)) 2>/dev/null
  488. partuuid=$(dd if="$T/firsttrack" bs=1 count=4 skip=$((0x1B8)) 2>/dev/null | \
  489. hexdump -e '1/4 "%08x"')-0$((syspartno+1))
  490. ((keep)) || if (( datafssz )); then
  491. (( quiet )) || print Cleaning out data partition...
  492. dd if=/dev/zero of="$tgt" bs=1048576 count=1 seek=$((cyls - cfgfs - datafssz)) > /dev/null 2>&1
  493. fi
  494. (( quiet )) || print Cleaning out root partition...
  495. dd if=/dev/zero bs=1048576 of="$tgt" count=1 seek=$((spartofs / 2048)) > /dev/null 2>&1
  496. (( quiet )) || if (( grub )); then
  497. print Writing MBR and GRUB2 to target device... system PARTUUID=$partuuid
  498. else
  499. print Writing MBR to target device... system PARTUUID=$partuuid
  500. fi
  501. dd if="$T/firsttrack" of="$tgt" > /dev/null 2>&1
  502. if [[ $ostype = Linux ]]; then
  503. echo partprobe $tgt
  504. partprobe -s $tgt
  505. sync
  506. fdisk -l $tgt
  507. ls -la ${tgt}*
  508. fi
  509. fwdir=$(dirname "$src")
  510. case $target {
  511. (banana-pro|orange-pi0)
  512. dd if="$fwdir/u-boot-sunxi-with-spl.bin" of="$tgt" bs=1024 seek=8 > /dev/null 2>&1
  513. ;;
  514. (solidrun-clearfog)
  515. dd if="$fwdir/u-boot-spl.kwb" of="$tgt" bs=512 seek=1 > /dev/null 2>&1
  516. ;;
  517. (solidrun-imx6)
  518. dd if="$fwdir/SPL" of="$tgt" bs=1024 seek=1 > /dev/null 2>&1
  519. dd if="$fwdir/u-boot.img" of="$tgt" bs=1024 seek=69 > /dev/null 2>&1
  520. ;;
  521. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|raspberry-pi3p|raspberry-pi3p-64)
  522. (( noformat )) || create_fs "$bootpart" ADKBOOT vfat
  523. ;;
  524. (phytec-wega)
  525. (( noformat )) || create_fs "$bootpart" ADKBOOT ext4
  526. ;;
  527. }
  528. (( noformat )) || create_fs "$rootpart" ADKROOT ext4
  529. (( noformat )) || tune_fs "$rootpart"
  530. (( quiet )) || print Extracting installation archive...
  531. mount_fs "$rootpart" "$R" ext4
  532. xz -dc "$src" | (cd "$R"; tar -xpf -)
  533. if (( datafssz )); then
  534. mkdir -m0755 "$R"/data
  535. ((keep)) || create_fs "$datapart" ADKDATA ext4
  536. ((keep)) || tune_fs "$datapart"
  537. case $target {
  538. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|raspberry-pi3p|raspberry-pi3p-64|phytec-wega)
  539. echo "/dev/mmcblk0p3 /data ext4 rw 0 0" >> "$R"/etc/fstab
  540. ;;
  541. (banana-pro|orange-pi0|solidrun-imx6|solidrun-clearfog)
  542. echo "/dev/mmcblk0p2 /data ext4 rw 0 0" >> "$R"/etc/fstab
  543. ;;
  544. }
  545. if [[ -d $datapartcontent ]]; then
  546. mount_fs "$datapart" "$D" ext4
  547. # strip trailing slash
  548. case $datapartcontent in
  549. *[!/]*/) datapartcontent=${datapartcontent%"${x##*[!/]}"};;
  550. esac
  551. cp -R $datapartcontent/* "$D"
  552. fi
  553. fi
  554. (( quiet )) || print Finishing up with bootloader and kernel ...
  555. case $target {
  556. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|raspberry-pi3p|raspberry-pi3p-64)
  557. mount_fs "$bootpart" "$B" vfat
  558. for x in "$R"/boot/*; do
  559. [[ -e "$x" ]] && mv -f "$R"/boot/* "$B/"
  560. break
  561. done
  562. for x in "$fwdir"/*.dtb; do
  563. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$B/"
  564. break
  565. done
  566. # use static dtoverlay, rename to *.dtb
  567. mkdir "$B/"overlays
  568. for x in "$fwdir"/overlays/*.dtbo; do
  569. y=$(basename ${x} .dtbo)
  570. [[ -e "$x" ]] && cp "$fwdir"/overlays/${y}.dtbo "$B/"overlays/${y}.dtb
  571. done
  572. umount_fs "$B"
  573. ;;
  574. (phytec-wega)
  575. mount_fs "$bootpart" "$B" ext4
  576. for x in "$R"/boot/*; do
  577. [[ -e "$x" ]] && mv -f "$R"/boot/* "$B/"
  578. break
  579. done
  580. for x in "$fwdir"/*.dtb; do
  581. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$B/"
  582. break
  583. done
  584. umount_fs "$B"
  585. ;;
  586. (solidrun-clearfog)
  587. for x in "$fwdir"/*.dtb; do
  588. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  589. break
  590. done
  591. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  592. -n "SolidrunClearfog" \
  593. -d $fwdir/boot.script.clearfog $R/boot/boot.scr.uimg
  594. ;;
  595. (solidrun-imx6)
  596. for x in "$fwdir"/*.dtb; do
  597. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  598. break
  599. done
  600. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  601. -n "SolidrunImx6" \
  602. -d $fwdir/boot.script.imx6 $R/boot/boot.scr.uimg
  603. ;;
  604. (orange-pi0)
  605. for x in "$fwdir"/*.dtb; do
  606. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  607. break
  608. done
  609. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  610. -n "OrangePI Zero" \
  611. -d $fwdir/boot.script.opi $R/boot/boot.scr.uimg
  612. ;;
  613. (banana-pro)
  614. for x in "$fwdir"/*.dtb; do
  615. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  616. break
  617. done
  618. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  619. -n "BananaPro" \
  620. -d $fwdir/boot.script.bpi $R/boot/boot.scr.uimg
  621. ;;
  622. }
  623. cd "$R"
  624. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  625. (( quiet )) || print Fixing up permissions...
  626. chown 0:0 tmp
  627. chmod 1777 tmp
  628. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  629. if (( grub )); then
  630. (( quiet )) || print Configuring GRUB2 bootloader...
  631. mkdir -p boot/grub
  632. (
  633. print set default=0
  634. print set timeout=1
  635. if (( serial )); then
  636. print serial --unit=0 --speed=$speed
  637. print terminal_output serial
  638. print terminal_input serial
  639. consargs="console=ttyS0,$speed console=tty0"
  640. else
  641. print terminal_output console
  642. print terminal_input console
  643. consargs="console=tty0"
  644. fi
  645. print
  646. print 'menuentry "GNU/Linux (OpenADK)" {'
  647. linuxargs="root=PARTUUID=$partuuid $consargs rootwait"
  648. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  649. print "\tlinux /boot/kernel $linuxargs"
  650. print '}'
  651. ) >boot/grub/grub.cfg
  652. fi
  653. (( quiet )) || print Finishing up...
  654. cd "$ADK_TOPDIR"
  655. umount_fs "$R"
  656. if (( datafssz )); then
  657. umount_fs $datapart
  658. fi
  659. rm -rf "$T"
  660. exit 0