1
0

install.sh 20 KB

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