freedombone-image-hardware-setup 8.3KB

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