freedombone-image-hardware-setup 8.3KB

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