freedombone-image-hardware-setup 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. # Setup boot.cmd
  132. if grep -q btrfs /etc/fstab ; then
  133. fstype=btrfs
  134. else
  135. fstype=ext4
  136. fi
  137. # shellcheck disable=SC2012,SC2086
  138. kernelVersion=$(ls /usr/lib/*/$dtb | head -1 | cut -d/ -f4)
  139. version=$(echo "$kernelVersion" | sed 's/linux-image-\(.*\)/\1/')
  140. initRd=initrd.img-$version
  141. vmlinuz=vmlinuz-$version
  142. # Create boot.cmd
  143. cat >> /boot/boot.cmd <<EOF
  144. setenv mmcdev 0
  145. setenv mmcpart 1
  146. setenv mmcroot /dev/mmcblk0p2 ro
  147. setenv mmcrootfstype $fstype rootwait fixrtc
  148. setenv mmcrootflags subvol=@
  149. setenv console ttyS0,115200n8
  150. setenv kernel_file $vmlinuz
  151. setenv initrd_file $initRd
  152. setenv fdtfile $dtb
  153. setenv loadaddr 0x46000000
  154. setenv initrd_addr 0x48000000
  155. setenv fdtaddr 0x47000000
  156. setenv initrd_high 0xffffffff
  157. setenv fdt_high 0xffffffff
  158. setenv loadkernel load mmc \${mmcdev}:\${mmcpart} \${loadaddr} \${kernel_file}
  159. setenv loadinitrd load mmc \${mmcdev}:\${mmcpart} \${initrd_addr} \${initrd_file}\\; setenv initrd_size \\\${filesize}
  160. setenv loadfdt load mmc \${mmcdev}:\${mmcpart} \${fdtaddr} /dtbs/\${fdtfile}
  161. setenv loadfiles run loadkernel\\; run loadinitrd\\; run loadfdt
  162. 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
  163. run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
  164. EOF
  165. # boot.scr for Allwinner A20 based device
  166. mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
  167. # Copy all DTBs
  168. mkdir -p /boot/dtbs
  169. # shellcheck disable=SC2086
  170. cp /usr/lib/$kernelVersion/* /boot/dtbs
  171. # extra boot modules
  172. echo "rtc_sunxi" >> /etc/initramfs-tools/modules
  173. }
  174. setup_flash_kernel() {
  175. if [ ! -d /etc/flash-kernel ] ; then
  176. mkdir /etc/flash-kernel
  177. fi
  178. printf "%s" "$1" > /etc/flash-kernel/machine
  179. command_line=""
  180. if [ -n "$2" ] ; then
  181. command_line="console=$2"
  182. fi
  183. if [ -n "$command_line" ] ; then
  184. echo "flash-kernel flash-kernel/linux_cmdline string \"$command_line\"" | debconf-set-selections
  185. fi
  186. apt-get install -y flash-kernel
  187. }
  188. case "$MACHINE" in
  189. beaglebone)
  190. beaglebone_setup_boot
  191. beaglebone_flash
  192. beaglebone_repack_kernel
  193. enable_serial_console ttyO0
  194. ;;
  195. cubietruck)
  196. a20_setup_boot sun7i-a20-cubietruck.dtb
  197. enable_serial_console ttyS0
  198. ;;
  199. a20-olinuxino-lime)
  200. a20_setup_boot sun7i-a20-olinuxino-lime.dtb
  201. enable_serial_console ttyS0
  202. ;;
  203. a20-olinuxino-lime2)
  204. a20_setup_boot sun7i-a20-olinuxino-lime2.dtb
  205. enable_serial_console ttyS0
  206. ;;
  207. a20-olinuxino-micro)
  208. a20_setup_boot sun7i-a20-olinuxino-micro.dtb
  209. enable_serial_console ttyS0
  210. ;;
  211. cubieboard2)
  212. a20_setup_boot sun7i-a20-cubieboard2.dtb
  213. enable_serial_console ttyS0
  214. ;;
  215. pcduino3)
  216. a20_setup_boot sun7i-a20-pcduino3.dtb
  217. enable_serial_console ttyS0
  218. ;;
  219. esac