freedombone-image-hardware-setup 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/bin/sh
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Hardware setup based on bin/freedombox-hardware-setup from freedom-maker
  10. #
  11. # License
  12. # =======
  13. #
  14. # This program is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU Affero General Public License as published by
  16. # the Free Software Foundation, either version 3 of the License, or
  17. # (at your option) any later version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU Affero General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU Affero General Public License
  25. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. PROJECT_NAME='freedombone'
  27. export TEXTDOMAIN=${PROJECT_NAME}-image-hardware-setup
  28. export TEXTDOMAINDIR="/usr/share/locale"
  29. enable_serial_console() {
  30. # By default, spawn a console on the serial port
  31. device="$1"
  32. echo "Adding a getty on the serial port"
  33. echo "T0:12345:respawn:/sbin/getty -L $device 115200 vt100" >> /etc/inittab
  34. }
  35. beaglebone_setup_boot() {
  36. # Setup uEnv.txt
  37. if grep -q btrfs /etc/fstab ; then
  38. fstype=btrfs
  39. else
  40. fstype=ext4
  41. fi
  42. # shellcheck disable=SC2012
  43. kernelVersion=$(ls /usr/lib/*/am335x-boneblack.dtb | head -1 | cut -d/ -f4)
  44. version=$(echo "$kernelVersion" | sed 's/linux-image-\(.*\)/\1/')
  45. initRd=initrd.img-$version
  46. vmlinuz=vmlinuz-$version
  47. bbb_loadaddr='0x82000000'
  48. bbb_initrd_addr='0x88080000'
  49. bbb_fdtaddr='0x88000000'
  50. # uEnv.txt for Beaglebone
  51. # based on https://github.com/beagleboard/image-builder/blob/master/target/boot/beagleboard.org.txt
  52. cat >> /boot/uEnv.txt <<EOF
  53. mmcroot=/dev/mmcblk0p2 ro
  54. mmcrootfstype=$fstype rootwait fixrtc
  55. mmcrootflags=subvol=@
  56. console=ttyO0,115200n8
  57. kernel_file=$vmlinuz
  58. initrd_file=$initRd
  59. loadaddr=$bbb_loadaddr
  60. initrd_addr=$bbb_initrd_addr
  61. fdtaddr=$bbb_fdtaddr
  62. initrd_high=0xffffffff
  63. fdt_high=0xffffffff
  64. loadkernel=load mmc \${mmcdev}:\${mmcpart} \${loadaddr} \${kernel_file}
  65. loadinitrd=load mmc \${mmcdev}:\${mmcpart} \${initrd_addr} \${initrd_file}; setenv initrd_size \${filesize}
  66. loadfdt=load mmc \${mmcdev}:\${mmcpart} \${fdtaddr} /dtbs/\${fdtfile}
  67. loadfiles=run loadkernel; run loadinitrd; run loadfdt
  68. mmcargs=setenv bootargs init=/lib/systemd/systemd console=tty0 console=\${console} root=\${mmcroot} rootfstype=\${mmcrootfstype} rootflags=\${mmcrootflags} ifnames=0 slub_debug=FZP slab_nomerge page_poison=1
  69. uenvcmd=run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
  70. EOF
  71. mkdir -p /boot/dtbs
  72. cp /usr/lib/linux-image-*-armmp/* /boot/dtbs
  73. }
  74. beaglebone_flash() {
  75. # allow flash-kernel to work without valid /proc contents
  76. # ** this doesn't *really* work, since there are too many checks
  77. # that fail in an emulated environment! We'll have to do it by
  78. # hand below anyway...
  79. export FK_MACHINE="TI AM335x BeagleBone Black"
  80. apt-get install -y flash-kernel
  81. }
  82. beaglebone_repack_kernel() {
  83. # process installed kernel to create uImage, uInitrd, dtb
  84. # using flash-kernel would be a good approach, except it fails in the
  85. # cross build environment due to too many environment checks...
  86. #FK_MACHINE="TI AM335x BeagleBone" flash-kernel
  87. # so, let's do it manually...
  88. # flash-kernel's hook-functions provided to mkinitramfs have the
  89. # unfortunate side-effect of creating /conf/param.conf in the initrd
  90. # when run from our emulated chroot environment, which means our root=
  91. # on the kernel command line is completely ignored! repack the initrd
  92. # to remove this evil...
  93. echo "info: repacking beaglebone kernel and initrd"
  94. bbb_dtb='am335x-boneblack'
  95. # shellcheck disable=SC2012
  96. kernelVersion=$(ls /usr/lib/*/${bbb_dtb}.dtb | head -1 | cut -d/ -f4)
  97. version=$(echo "$kernelVersion" | sed 's/linux-image-\(.*\)/\1/')
  98. initRd=initrd.img-$version
  99. vmlinuz=vmlinuz-$version
  100. # optionally use a separately compiled kernel
  101. bbb_dtb_file=/usr/lib/$kernelVersion/${bbb_dtb}.dtb
  102. #if [ -f /boot/bbb.tar.gz ]; then
  103. # cd /boot
  104. # tar -xzvf /boot/bbb.tar.gz
  105. # if [ -f /boot/bbb/dtbs/${bbb_dtb}.dtb ]; then
  106. # if [ -f /boot/bbb/zImage ]; then
  107. # bbb_dtb_file=/boot/bbb/dtbs/${bbb_dtb}.dtb
  108. # vmlinuz=/boot/bbb/zImage
  109. # fi
  110. # fi
  111. #fi
  112. mkdir /tmp/initrd-repack
  113. (cd /tmp/initrd-repack || exit 2468246 ; \
  114. zcat "/boot/$initRd" | cpio -i ; \
  115. rm -f conf/param.conf ; \
  116. find . | cpio --quiet -o -H newc | \
  117. gzip -9 > "/boot/$initRd" )
  118. rm -rf /tmp/initrd-repack
  119. (cd /boot || exit 246824684 ; \
  120. cp "${bbb_dtb_file}" dtb ; \
  121. cat "$vmlinuz" dtb >> temp-kernel ; \
  122. mkimage -A arm -O linux -T kernel -n "Debian kernel ${version}" \
  123. -C none -a 0x82000000 -e 0x82000000 -d temp-kernel uImage ; \
  124. rm -f temp-kernel ; \
  125. mkimage -A arm -O linux -T ramdisk -C gzip -a 0x88080000 -e 0x88080000 \
  126. -n "Debian ramdisk ${version}" \
  127. -d "$initRd" uInitrd )
  128. }
  129. a20_setup_boot() {
  130. dtb="$1"
  131. a20_root_device='mmcblk0p2'
  132. if [ "$2" ]; then
  133. a20_root_device="$2"
  134. fi
  135. # Setup boot.cmd
  136. if grep -q btrfs /etc/fstab ; then
  137. fstype=btrfs
  138. else
  139. fstype=ext4
  140. fi
  141. # shellcheck disable=SC2012,SC2086
  142. kernelVersion=$(ls /usr/lib/*/$dtb | head -1 | cut -d/ -f4)
  143. version=$(echo "$kernelVersion" | sed 's/linux-image-\(.*\)/\1/')
  144. initRd=initrd.img-$version
  145. vmlinuz=vmlinuz-$version
  146. # Create boot.cmd
  147. cat >> /boot/boot.cmd <<EOF
  148. setenv mmcdev 0
  149. setenv mmcpart 1
  150. setenv mmcroot /dev/${a20_root_device} ro
  151. setenv mmcrootfstype $fstype rootwait fixrtc
  152. setenv mmcrootflags subvol=@
  153. setenv console ttyS0,115200n8
  154. setenv kernel_file $vmlinuz
  155. setenv initrd_file $initRd
  156. setenv fdtfile $dtb
  157. setenv loadaddr 0x46000000
  158. setenv initrd_addr 0x48000000
  159. setenv fdtaddr 0x47000000
  160. setenv initrd_high 0xffffffff
  161. setenv fdt_high 0xffffffff
  162. setenv loadkernel load mmc \${mmcdev}:\${mmcpart} \${loadaddr} \${kernel_file}
  163. setenv loadinitrd load mmc \${mmcdev}:\${mmcpart} \${initrd_addr} \${initrd_file}\\; setenv initrd_size \\\${filesize}
  164. setenv loadfdt load mmc \${mmcdev}:\${mmcpart} \${fdtaddr} /dtbs/\${fdtfile}
  165. setenv loadfiles run loadkernel\\; run loadinitrd\\; run loadfdt
  166. setenv mmcargs setenv bootargs init=/lib/systemd/systemd console=\${console} root=\${mmcroot} rootfstype=\${mmcrootfstype} rootflags=\${mmcrootflags} ifnames=0 slub_debug=FZP slab_nomerge page_poison=1
  167. run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
  168. EOF
  169. # boot.scr for Allwinner A20 based device
  170. mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
  171. # Copy all DTBs
  172. mkdir -p /boot/dtbs
  173. # shellcheck disable=SC2086
  174. cp /usr/lib/$kernelVersion/* /boot/dtbs
  175. # extra boot modules
  176. echo "rtc_sunxi" >> /etc/initramfs-tools/modules
  177. }
  178. setup_flash_kernel() {
  179. if [ ! -d /etc/flash-kernel ] ; then
  180. mkdir /etc/flash-kernel
  181. fi
  182. printf "%s" "$1" > /etc/flash-kernel/machine
  183. command_line=""
  184. if [ -n "$2" ] ; then
  185. command_line="console=$2"
  186. fi
  187. if [ -n "$command_line" ] ; then
  188. echo "flash-kernel flash-kernel/linux_cmdline string \"$command_line\"" | debconf-set-selections
  189. fi
  190. apt-get install -y flash-kernel
  191. }
  192. case "$MACHINE" in
  193. beaglebone)
  194. beaglebone_setup_boot
  195. beaglebone_flash
  196. beaglebone_repack_kernel
  197. enable_serial_console ttyO0
  198. ;;
  199. cubietruck)
  200. a20_setup_boot sun7i-a20-cubietruck.dtb "$EXTERNAL_DRIVE"
  201. enable_serial_console ttyS0
  202. ;;
  203. a20-olinuxino-lime)
  204. a20_setup_boot sun7i-a20-olinuxino-lime.dtb "$EXTERNAL_DRIVE"
  205. enable_serial_console ttyS0
  206. ;;
  207. a20-olinuxino-lime2)
  208. a20_setup_boot sun7i-a20-olinuxino-lime2.dtb "$EXTERNAL_DRIVE"
  209. enable_serial_console ttyS0
  210. ;;
  211. a20-olinuxino-micro)
  212. a20_setup_boot sun7i-a20-olinuxino-micro.dtb "$EXTERNAL_DRIVE"
  213. enable_serial_console ttyS0
  214. ;;
  215. banana-pro)
  216. a20_setup_boot sun7i-a20-bananapro.dtb "$EXTERNAL_DRIVE"
  217. enable_serial_console ttyS0
  218. ;;
  219. cubieboard2)
  220. a20_setup_boot sun7i-a20-cubieboard2.dtb "$EXTERNAL_DRIVE"
  221. enable_serial_console ttyS0
  222. ;;
  223. pcduino3)
  224. a20_setup_boot sun7i-a20-pcduino3.dtb "$EXTERNAL_DRIVE"
  225. enable_serial_console ttyS0
  226. ;;
  227. esac