install.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. #!/usr/bin/env bash
  2. #-
  3. # Copyright © 2010-2023
  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. (atmel-ngw100|banana-pro|banana-pro-zero|orange-pi0|pcengines-apu|phytec-imx6|phytec-wega|raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|raspberry-pi4|raspberry-pi4-64|rockpi4-plus|solidrun-imx6|solidrun-clearfog|imgtec-ci20|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-pi4 || $target = raspberry-pi4-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-pi4 || $target = raspberry-pi4-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-pi4 || $target = raspberry-pi4-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-pi4 || $target = raspberry-pi4-64 || $target = phytec-wega || $target = rockpi4-plus ]]; 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-pi4 || $target = raspberry-pi4-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. (rockpi4-plus)
  512. dd if="$fwdir/idbloader.img" of="$tgt" bs=512 seek=64 > /dev/null 2>&1
  513. dd if="$fwdir/u-boot.itb" of="$tgt" bs=512 seek=16384 > /dev/null 2>&1
  514. ;;
  515. (imgtec-ci20)
  516. dd if="$fwdir/u-boot-spl.bin" of="$tgt" obs=512 seek=1 > /dev/null 2>&1
  517. dd if="$fwdir/u-boot-dtb.img" of="$tgt" obs=1k seek=14 > /dev/null 2>&1
  518. ;;
  519. (banana-pro|banana-pro-zero|orange-pi0)
  520. dd if="$fwdir/u-boot-sunxi-with-spl.bin" of="$tgt" bs=1024 seek=8 > /dev/null 2>&1
  521. ;;
  522. (solidrun-clearfog)
  523. dd if="$fwdir/u-boot-spl.kwb" of="$tgt" bs=512 seek=1 > /dev/null 2>&1
  524. ;;
  525. (solidrun-imx6|phytec-imx6)
  526. dd if="$fwdir/SPL" of="$tgt" bs=1024 seek=1 > /dev/null 2>&1
  527. dd if="$fwdir/u-boot.img" of="$tgt" bs=1024 seek=69 > /dev/null 2>&1
  528. ;;
  529. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|raspberry-pi4|raspberry-pi4-64)
  530. (( noformat )) || create_fs "$bootpart" ADKBOOT vfat
  531. ;;
  532. (phytec-wega)
  533. (( noformat )) || create_fs "$bootpart" ADKBOOT ext4
  534. ;;
  535. }
  536. case $target {
  537. (atmel-ngw100)
  538. (( noformat )) || create_fs "$rootpart" ADKROOT ext2
  539. (( noformat )) || tune_fs "$rootpart"
  540. ;;
  541. (*)
  542. (( noformat )) || create_fs "$rootpart" ADKROOT ext4
  543. (( noformat )) || tune_fs "$rootpart"
  544. }
  545. (( quiet )) || print Extracting installation archive...
  546. mount_fs "$rootpart" "$R" ext4
  547. xz -dc "$src" | (cd "$R"; tar -xpf -)
  548. if (( datafssz )); then
  549. mkdir -m0755 "$R"/data
  550. ((keep)) || create_fs "$datapart" ADKDATA ext4
  551. ((keep)) || tune_fs "$datapart"
  552. case $target {
  553. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|raspberry-pi4|raspberry-pi4-64|phytec-wega)
  554. echo "/dev/mmcblk0p3 /data ext4 rw 0 0" >> "$R"/etc/fstab
  555. ;;
  556. (banana-pro|orange-pi0|solidrun-clearfog|rockpi4-plus)
  557. echo "/dev/mmcblk0p2 /data ext4 rw 0 0" >> "$R"/etc/fstab
  558. ;;
  559. (solidrun-imx6|phytec-imx6)
  560. echo "/dev/mmcblk1p2 /data ext4 rw 0 0" >> "$R"/etc/fstab
  561. ;;
  562. }
  563. if [[ -d $datapartcontent ]]; then
  564. mount_fs "$datapart" "$D" ext4
  565. # strip trailing slash
  566. case $datapartcontent in
  567. *[!/]*/) datapartcontent=${datapartcontent%"${x##*[!/]}"};;
  568. esac
  569. cp -R $datapartcontent/* "$D"
  570. fi
  571. fi
  572. (( quiet )) || print Finishing up with bootloader and kernel ...
  573. case $target {
  574. (raspberry-pi|raspberry-pi0|raspberry-pi2|raspberry-pi3|raspberry-pi3-64|raspberry-pi4|raspberry-pi4-64)
  575. mount_fs "$bootpart" "$B" vfat
  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. # use static dtoverlay, rename to *.dtb
  585. mkdir "$B/"overlays
  586. for x in "$fwdir"/overlays/*.dtbo; do
  587. y=$(basename ${x} .dtbo)
  588. [[ -e "$x" ]] && cp "$fwdir"/overlays/${y}.dtbo "$B/"overlays/${y}.dtbo
  589. done
  590. for x in "$fwdir"/overlays/*.dtb; do
  591. y=$(basename ${x} .dtb)
  592. [[ -e "$x" ]] && cp "$fwdir"/overlays/${y}.dtb "$B/"overlays/${y}.dtb
  593. done
  594. umount_fs "$B"
  595. ;;
  596. (phytec-wega)
  597. mount_fs "$bootpart" "$B" ext4
  598. for x in "$R"/boot/*; do
  599. [[ -e "$x" ]] && mv -f "$R"/boot/* "$B/"
  600. break
  601. done
  602. for x in "$fwdir"/*.dtb; do
  603. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$B/"
  604. break
  605. done
  606. umount_fs "$B"
  607. ;;
  608. (rockpi4-plus)
  609. for x in "$fwdir"/*.dtb; do
  610. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  611. break
  612. done
  613. mkimage -A arm64 -O linux -T script -C none -a 0 -e 0 \
  614. -n "RockPI4-Plus" \
  615. -d $fwdir/boot.script.rockpi4 $R/boot/boot.scr.uimg
  616. ;;
  617. (solidrun-clearfog)
  618. for x in "$fwdir"/*.dtb; do
  619. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  620. break
  621. done
  622. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  623. -n "SolidrunClearfog" \
  624. -d $fwdir/boot.script.clearfog $R/boot/boot.scr.uimg
  625. ;;
  626. (solidrun-imx6)
  627. for x in "$fwdir"/*.dtb; do
  628. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  629. break
  630. done
  631. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  632. -n "SolidrunImx6" \
  633. -d $fwdir/boot.script.imx6 $R/boot/boot.scr.uimg
  634. ;;
  635. (phytec-imx6)
  636. for x in "$fwdir"/*.dtb; do
  637. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  638. break
  639. done
  640. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  641. -n "PhytecImx6" \
  642. -d $fwdir/boot.script.phytec $R/boot/boot.scr.uimg
  643. ;;
  644. (orange-pi0)
  645. for x in "$fwdir"/*.dtb; do
  646. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  647. break
  648. done
  649. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  650. -n "OrangePI Zero" \
  651. -d $fwdir/boot.script.opi $R/boot/boot.scr.uimg
  652. ;;
  653. (banana-pro)
  654. for x in "$fwdir"/*.dtb; do
  655. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  656. break
  657. done
  658. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  659. -n "BananaPro" \
  660. -d $fwdir/boot.script.bpi $R/boot/boot.scr.uimg
  661. ;;
  662. (banana-pro-zero)
  663. for x in "$fwdir"/*.dtb; do
  664. [[ -e "$x" ]] && cp "$fwdir"/*.dtb "$R/boot/"
  665. break
  666. done
  667. mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
  668. -n "BananaProZero" \
  669. -d $fwdir/boot.script.bpizero $R/boot/boot.scr.uimg
  670. ;;
  671. }
  672. cd "$R"
  673. dd if=$rnddev bs=16 count=1 >>etc/.rnd 2>/dev/null
  674. (( quiet )) || print Fixing up permissions...
  675. chown 0:0 tmp
  676. chmod 1777 tmp
  677. [[ -f usr/bin/sudo ]] && chmod 4755 usr/bin/sudo
  678. if (( grub )); then
  679. (( quiet )) || print Configuring GRUB2 bootloader...
  680. mkdir -p boot/grub
  681. (
  682. print set default=0
  683. print set timeout=1
  684. if (( serial )); then
  685. print serial --unit=0 --speed=$speed
  686. print terminal_output serial
  687. print terminal_input serial
  688. consargs="console=ttyS0,$speed console=tty0"
  689. else
  690. print terminal_output console
  691. print terminal_input console
  692. consargs="console=tty0"
  693. fi
  694. print
  695. print 'menuentry "GNU/Linux (OpenADK)" {'
  696. linuxargs="root=PARTUUID=$partuuid $consargs rootwait"
  697. (( panicreboot )) && linuxargs="$linuxargs panic=$panicreboot"
  698. print "\tlinux /boot/kernel $linuxargs"
  699. print '}'
  700. ) >boot/grub/grub.cfg
  701. fi
  702. (( quiet )) || print Finishing up...
  703. cd "$ADK_TOPDIR"
  704. umount_fs "$R"
  705. if (( datafssz )); then
  706. umount_fs $datapart
  707. fi
  708. rm -rf "$T"
  709. exit 0