freedombone-image-hardware-setup 7.0KB

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