freedombone-image-hardware-setup 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. bbb_version=$1
  39. # Setup uEnv.txt
  40. if grep -q btrfs /etc/fstab ; then
  41. fstype=btrfs
  42. else
  43. fstype=ext4
  44. fi
  45. if [ ! $bbb_version ]; then
  46. kernelVersion=$(ls /usr/lib/*/am335x-boneblack.dtb | head -1 | cut -d/ -f4)
  47. else
  48. kernelVersion=$(ls /usr/lib/*/am335x-boneblack-${1}.dtb | head -1 | cut -d/ -f4)
  49. fi
  50. version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
  51. initRd=initrd.img-$version
  52. vmlinuz=vmlinuz-$version
  53. bbb_loadaddr='0x82000000'
  54. bbb_initrd_addr='0x88080000'
  55. bbb_fdtaddr='0x88000000'
  56. # uEnv.txt for Beaglebone
  57. # based on https://github.com/beagleboard/image-builder/blob/master/target/boot/beagleboard.org.txt
  58. cat >> /boot/uEnv.txt <<EOF
  59. mmcroot=/dev/mmcblk0p2 ro
  60. mmcrootfstype=$fstype rootwait fixrtc
  61. mmcrootflags=subvol=@
  62. console=ttyO0,115200n8
  63. kernel_file=$vmlinuz
  64. initrd_file=$initRd
  65. loadaddr=$bbb_loadaddr
  66. initrd_addr=$bbb_initrd_addr
  67. fdtaddr=$bbb_fdtaddr
  68. initrd_high=0xffffffff
  69. fdt_high=0xffffffff
  70. loadkernel=load mmc \${mmcdev}:\${mmcpart} \${loadaddr} \${kernel_file}
  71. loadinitrd=load mmc \${mmcdev}:\${mmcpart} \${initrd_addr} \${initrd_file}; setenv initrd_size \${filesize}
  72. loadfdt=load mmc \${mmcdev}:\${mmcpart} \${fdtaddr} /dtbs/\${fdtfile}
  73. loadfiles=run loadkernel; run loadinitrd; run loadfdt
  74. mmcargs=setenv bootargs init=/lib/systemd/systemd console=tty0 console=\${console} root=\${mmcroot} rootfstype=\${mmcrootfstype} rootflags=\${mmcrootflags} ifnames=0
  75. uenvcmd=run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
  76. EOF
  77. mkdir -p /boot/dtbs
  78. cp /usr/lib/linux-image-*-armmp/* /boot/dtbs
  79. }
  80. beaglebone_flash() {
  81. # allow flash-kernel to work without valid /proc contents
  82. # ** this doesn't *really* work, since there are too many checks
  83. # that fail in an emulated environment! We'll have to do it by
  84. # hand below anyway...
  85. export FK_MACHINE="TI AM335x BeagleBone"
  86. apt-get install -y flash-kernel
  87. }
  88. beaglebone_repack_kernel() {
  89. bbb_version=$1
  90. # process installed kernel to create uImage, uInitrd, dtb
  91. # using flash-kernel would be a good approach, except it fails in the
  92. # cross build environment due to too many environment checks...
  93. #FK_MACHINE="TI AM335x BeagleBone" flash-kernel
  94. # so, let's do it manually...
  95. # flash-kernel's hook-functions provided to mkinitramfs have the
  96. # unfortunate side-effect of creating /conf/param.conf in the initrd
  97. # when run from our emulated chroot environment, which means our root=
  98. # on the kernel command line is completely ignored! repack the initrd
  99. # to remove this evil...
  100. echo "info: repacking beaglebone kernel and initrd"
  101. bbb_dtb='am335x-boneblack'
  102. if [ $bbb_version ]; then
  103. bbb_dtb="am335x-boneblack-${bbb_version}"
  104. fi
  105. kernelVersion=$(ls /usr/lib/*/${bbb_dtb}.dtb | head -1 | cut -d/ -f4)
  106. version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
  107. initRd=initrd.img-$version
  108. vmlinuz=vmlinuz-$version
  109. # optionally use a separately compiled kernel
  110. bbb_dtb_file=/usr/lib/$kernelVersion/${bbb_dtb}.dtb
  111. #if [ -f /boot/bbb.tar.gz ]; then
  112. # cd /boot
  113. # tar -xzvf /boot/bbb.tar.gz
  114. # if [ -f /boot/bbb/dtbs/${bbb_dtb}.dtb ]; then
  115. # if [ -f /boot/bbb/zImage ]; then
  116. # bbb_dtb_file=/boot/bbb/dtbs/${bbb_dtb}.dtb
  117. # vmlinuz=/boot/bbb/zImage
  118. # fi
  119. # fi
  120. #fi
  121. mkdir /tmp/initrd-repack
  122. (cd /tmp/initrd-repack ; \
  123. zcat /boot/$initRd | cpio -i ; \
  124. rm -f conf/param.conf ; \
  125. find . | cpio --quiet -o -H newc | \
  126. gzip -9 > /boot/$initRd )
  127. rm -rf /tmp/initrd-repack
  128. (cd /boot ; \
  129. cp ${bbb_dtb_file} dtb ; \
  130. cat $vmlinuz dtb >> temp-kernel ; \
  131. mkimage -A arm -O linux -T kernel -n "Debian kernel ${version}" \
  132. -C none -a 0x82000000 -e 0x82000000 -d temp-kernel uImage ; \
  133. rm -f temp-kernel ; \
  134. mkimage -A arm -O linux -T ramdisk -C gzip -a 0x88080000 -e 0x88080000 \
  135. -n "Debian ramdisk ${version}" \
  136. -d $initRd uInitrd )
  137. }
  138. a20_setup_boot() {
  139. dtb="$1"
  140. # Setup boot.cmd
  141. if grep -q btrfs /etc/fstab ; then
  142. fstype=btrfs
  143. else
  144. fstype=ext4
  145. fi
  146. kernelVersion=$(ls /usr/lib/*/$dtb | head -1 | cut -d/ -f4)
  147. version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
  148. initRd=initrd.img-$version
  149. vmlinuz=vmlinuz-$version
  150. # Create boot.cmd
  151. cat >> /boot/boot.cmd <<EOF
  152. setenv mmcdev 0
  153. setenv mmcpart 1
  154. setenv mmcroot /dev/mmcblk0p2 ro
  155. setenv mmcrootfstype $fstype rootwait fixrtc
  156. setenv mmcrootflags subvol=@
  157. setenv console ttyS0,115200n8
  158. setenv kernel_file $vmlinuz
  159. setenv initrd_file $initRd
  160. setenv fdtfile $dtb
  161. setenv loadaddr 0x46000000
  162. setenv initrd_addr 0x48000000
  163. setenv fdtaddr 0x47000000
  164. setenv initrd_high 0xffffffff
  165. setenv fdt_high 0xffffffff
  166. setenv loadkernel load mmc \${mmcdev}:\${mmcpart} \${loadaddr} \${kernel_file}
  167. setenv loadinitrd load mmc \${mmcdev}:\${mmcpart} \${initrd_addr} \${initrd_file}\\; setenv initrd_size \\\${filesize}
  168. setenv loadfdt load mmc \${mmcdev}:\${mmcpart} \${fdtaddr} /dtbs/\${fdtfile}
  169. setenv loadfiles run loadkernel\\; run loadinitrd\\; run loadfdt
  170. setenv mmcargs setenv bootargs init=/lib/systemd/systemd console=\${console} root=\${mmcroot} rootfstype=\${mmcrootfstype} rootflags=\${mmcrootflags} ifnames=0
  171. run loadfiles; run mmcargs; bootz \${loadaddr} \${initrd_addr}:\${initrd_size} \${fdtaddr}
  172. EOF
  173. # boot.scr for Allwinner A20 based device
  174. mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
  175. # Copy all DTBs
  176. mkdir -p /boot/dtbs
  177. cp /usr/lib/$kernelVersion/* /boot/dtbs
  178. # extra boot modules
  179. echo "rtc_sunxi" >> /etc/initramfs-tools/modules
  180. }
  181. case "$MACHINE" in
  182. beaglebone)
  183. beaglebone_setup_boot
  184. beaglebone_flash
  185. beaglebone_repack_kernel
  186. enable_serial_console ttyO0
  187. ;;
  188. beaglebonewifi)
  189. beaglebone_setup_boot wireless
  190. beaglebone_flash
  191. beaglebone_repack_kernel wireless
  192. enable_serial_console ttyO0
  193. ;;
  194. cubietruck)
  195. a20_setup_boot sun7i-a20-cubietruck.dtb
  196. enable_serial_console ttyS0
  197. ;;
  198. a20-olinuxino-lime)
  199. a20_setup_boot sun7i-a20-olinuxino-lime.dtb
  200. enable_serial_console ttyS0
  201. ;;
  202. a20-olinuxino-lime2)
  203. a20_setup_boot sun7i-a20-olinuxino-lime2.dtb
  204. enable_serial_console ttyS0
  205. ;;
  206. a20-olinuxino-micro)
  207. a20_setup_boot sun7i-a20-olinuxino-micro.dtb
  208. enable_serial_console ttyS0
  209. ;;
  210. cubieboard2)
  211. a20_setup_boot sun7i-a20-cubieboard2.dtb
  212. enable_serial_console ttyS0
  213. ;;
  214. esac